1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template {
57 struct pending_template *next;
58 struct tinst_level *tinst;
61 static GTY(()) struct pending_template *pending_templates;
62 static GTY(()) struct pending_template *last_pending_template;
64 int processing_template_parmlist;
65 static int template_header_count;
67 static GTY(()) tree saved_trees;
68 static VEC(int,heap) *inline_parm_levels;
70 static GTY(()) struct tinst_level *current_tinst_level;
72 static GTY(()) tree saved_access_scope;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
82 static htab_t local_specializations;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry)))
92 htab_t decl_specializations;
94 static GTY ((param_is (spec_entry)))
95 htab_t type_specializations;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) VEC(tree,gc) *canonical_template_parms;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 static void push_access_scope (tree);
113 static void pop_access_scope (tree);
114 static void push_deduction_access_scope (tree);
115 static void pop_deduction_access_scope (tree);
116 static bool resolve_overloaded_unification (tree, tree, tree, tree,
117 unification_kind_t, int);
118 static int try_one_overload (tree, tree, tree, tree, tree,
119 unification_kind_t, int, bool);
120 static int unify (tree, tree, tree, tree, int);
121 static void add_pending_template (tree);
122 static tree reopen_tinst_level (struct tinst_level *);
123 static tree tsubst_initializer_list (tree, tree);
124 static tree get_class_bindings (tree, tree, tree);
125 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
127 static void tsubst_enum (tree, tree, tree);
128 static tree add_to_template_args (tree, tree);
129 static tree add_outermost_template_args (tree, tree);
130 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
131 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
133 static int type_unification_real (tree, tree, tree, const tree *,
134 unsigned int, int, unification_kind_t, int);
135 static void note_template_header (int);
136 static tree convert_nontype_argument_function (tree, tree);
137 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
138 static tree convert_template_argument (tree, tree, tree,
139 tsubst_flags_t, int, tree);
140 static int for_each_template_parm (tree, tree_fn_t, void*,
141 struct pointer_set_t*, bool);
142 static tree expand_template_argument_pack (tree);
143 static tree build_template_parm_index (int, int, int, int, tree, tree);
144 static bool inline_needs_template_parms (tree);
145 static void push_inline_template_parms_recursive (tree, int);
146 static tree retrieve_local_specialization (tree);
147 static void register_local_specialization (tree, tree);
148 static hashval_t hash_specialization (const void *p);
149 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
150 static int mark_template_parm (tree, void *);
151 static int template_parm_this_level_p (tree, void *);
152 static tree tsubst_friend_function (tree, tree);
153 static tree tsubst_friend_class (tree, tree);
154 static int can_complete_type_without_circularity (tree);
155 static tree get_bindings (tree, tree, tree, bool);
156 static int template_decl_level (tree);
157 static int check_cv_quals_for_unify (int, tree, tree);
158 static void template_parm_level_and_index (tree, int*, int*);
159 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
160 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
161 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
162 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
163 static void regenerate_decl_from_template (tree, tree);
164 static tree most_specialized_class (tree, tree, tsubst_flags_t);
165 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
166 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
167 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
168 static bool check_specialization_scope (void);
169 static tree process_partial_specialization (tree);
170 static void set_current_access_from_decl (tree);
171 static tree get_template_base (tree, tree, tree, tree);
172 static tree try_class_unification (tree, tree, tree, tree);
173 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
175 static bool template_template_parm_bindings_ok_p (tree, tree);
176 static int template_args_equal (tree, tree);
177 static void tsubst_default_arguments (tree);
178 static tree for_each_template_parm_r (tree *, int *, void *);
179 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
180 static void copy_default_args_to_explicit_spec (tree);
181 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
182 static int eq_local_specializations (const void *, const void *);
183 static bool dependent_template_arg_p (tree);
184 static bool any_template_arguments_need_structural_equality_p (tree);
185 static bool dependent_type_p_r (tree);
186 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
187 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_decl (tree, tree, tsubst_flags_t);
190 static void perform_typedefs_access_check (tree tmpl, tree targs);
191 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
193 static tree listify (tree);
194 static tree listify_autos (tree, tree);
195 static tree template_parm_to_arg (tree t);
196 static tree current_template_args (void);
197 static tree fixup_template_type_parm_type (tree, int);
198 static tree fixup_template_parm_index (tree, tree, int);
199 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
201 /* Make the current scope suitable for access checking when we are
202 processing T. T can be FUNCTION_DECL for instantiated function
203 template, or VAR_DECL for static member variable (need by
204 instantiate_decl). */
207 push_access_scope (tree t)
209 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
210 || TREE_CODE (t) == VAR_DECL);
212 if (DECL_FRIEND_CONTEXT (t))
213 push_nested_class (DECL_FRIEND_CONTEXT (t));
214 else if (DECL_CLASS_SCOPE_P (t))
215 push_nested_class (DECL_CONTEXT (t));
217 pushclass (NULL_TREE);
219 if (TREE_CODE (t) == FUNCTION_DECL)
221 saved_access_scope = tree_cons
222 (NULL_TREE, current_function_decl, saved_access_scope);
223 current_function_decl = t;
227 /* Restore the scope set up by push_access_scope. T is the node we
231 pop_access_scope (tree t)
233 if (TREE_CODE (t) == FUNCTION_DECL)
235 current_function_decl = TREE_VALUE (saved_access_scope);
236 saved_access_scope = TREE_CHAIN (saved_access_scope);
239 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
245 /* Do any processing required when DECL (a member template
246 declaration) is finished. Returns the TEMPLATE_DECL corresponding
247 to DECL, unless it is a specialization, in which case the DECL
248 itself is returned. */
251 finish_member_template_decl (tree decl)
253 if (decl == error_mark_node)
254 return error_mark_node;
256 gcc_assert (DECL_P (decl));
258 if (TREE_CODE (decl) == TYPE_DECL)
262 type = TREE_TYPE (decl);
263 if (type == error_mark_node)
264 return error_mark_node;
265 if (MAYBE_CLASS_TYPE_P (type)
266 && CLASSTYPE_TEMPLATE_INFO (type)
267 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
269 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
270 check_member_template (tmpl);
275 else if (TREE_CODE (decl) == FIELD_DECL)
276 error ("data member %qD cannot be a member template", decl);
277 else if (DECL_TEMPLATE_INFO (decl))
279 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
281 check_member_template (DECL_TI_TEMPLATE (decl));
282 return DECL_TI_TEMPLATE (decl);
288 error ("invalid member template declaration %qD", decl);
290 return error_mark_node;
293 /* Create a template info node. */
296 build_template_info (tree template_decl, tree template_args)
298 tree result = make_node (TEMPLATE_INFO);
299 TI_TEMPLATE (result) = template_decl;
300 TI_ARGS (result) = template_args;
304 /* Return the template info node corresponding to T, whatever T is. */
307 get_template_info (const_tree t)
309 tree tinfo = NULL_TREE;
311 if (!t || t == error_mark_node)
314 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
315 tinfo = DECL_TEMPLATE_INFO (t);
317 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
320 if (TAGGED_TYPE_P (t))
321 tinfo = TYPE_TEMPLATE_INFO (t);
322 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
323 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
328 /* Returns the template nesting level of the indicated class TYPE.
338 A<T>::B<U> has depth two, while A<T> has depth one.
339 Both A<T>::B<int> and A<int>::B<U> have depth one, if
340 they are instantiations, not specializations.
342 This function is guaranteed to return 0 if passed NULL_TREE so
343 that, for example, `template_class_depth (current_class_type)' is
347 template_class_depth (tree type)
352 type && TREE_CODE (type) != NAMESPACE_DECL;
353 type = (TREE_CODE (type) == FUNCTION_DECL)
354 ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
356 tree tinfo = get_template_info (type);
358 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
359 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
366 /* Subroutine of maybe_begin_member_template_processing.
367 Returns true if processing DECL needs us to push template parms. */
370 inline_needs_template_parms (tree decl)
372 if (! DECL_TEMPLATE_INFO (decl))
375 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
376 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
379 /* Subroutine of maybe_begin_member_template_processing.
380 Push the template parms in PARMS, starting from LEVELS steps into the
381 chain, and ending at the beginning, since template parms are listed
385 push_inline_template_parms_recursive (tree parmlist, int levels)
387 tree parms = TREE_VALUE (parmlist);
391 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
393 ++processing_template_decl;
394 current_template_parms
395 = tree_cons (size_int (processing_template_decl),
396 parms, current_template_parms);
397 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
399 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
401 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
403 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
405 if (parm == error_mark_node)
408 gcc_assert (DECL_P (parm));
410 switch (TREE_CODE (parm))
419 /* Make a CONST_DECL as is done in process_template_parm.
420 It is ugly that we recreate this here; the original
421 version built in process_template_parm is no longer
423 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
424 CONST_DECL, DECL_NAME (parm),
426 DECL_ARTIFICIAL (decl) = 1;
427 TREE_CONSTANT (decl) = 1;
428 TREE_READONLY (decl) = 1;
429 DECL_INITIAL (decl) = DECL_INITIAL (parm);
430 SET_DECL_TEMPLATE_PARM_P (decl);
441 /* Restore the template parameter context for a member template or
442 a friend template defined in a class definition. */
445 maybe_begin_member_template_processing (tree decl)
450 if (inline_needs_template_parms (decl))
452 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
453 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
455 if (DECL_TEMPLATE_SPECIALIZATION (decl))
458 parms = TREE_CHAIN (parms);
461 push_inline_template_parms_recursive (parms, levels);
464 /* Remember how many levels of template parameters we pushed so that
465 we can pop them later. */
466 VEC_safe_push (int, heap, inline_parm_levels, levels);
469 /* Undo the effects of maybe_begin_member_template_processing. */
472 maybe_end_member_template_processing (void)
477 if (VEC_length (int, inline_parm_levels) == 0)
480 last = VEC_pop (int, inline_parm_levels);
481 for (i = 0; i < last; ++i)
483 --processing_template_decl;
484 current_template_parms = TREE_CHAIN (current_template_parms);
489 /* Return a new template argument vector which contains all of ARGS,
490 but has as its innermost set of arguments the EXTRA_ARGS. */
493 add_to_template_args (tree args, tree extra_args)
500 if (args == NULL_TREE || extra_args == error_mark_node)
503 extra_depth = TMPL_ARGS_DEPTH (extra_args);
504 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
506 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
507 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
509 for (j = 1; j <= extra_depth; ++j, ++i)
510 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
515 /* Like add_to_template_args, but only the outermost ARGS are added to
516 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
517 (EXTRA_ARGS) levels are added. This function is used to combine
518 the template arguments from a partial instantiation with the
519 template arguments used to attain the full instantiation from the
520 partial instantiation. */
523 add_outermost_template_args (tree args, tree extra_args)
527 /* If there are more levels of EXTRA_ARGS than there are ARGS,
528 something very fishy is going on. */
529 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
531 /* If *all* the new arguments will be the EXTRA_ARGS, just return
533 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
536 /* For the moment, we make ARGS look like it contains fewer levels. */
537 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
539 new_args = add_to_template_args (args, extra_args);
541 /* Now, we restore ARGS to its full dimensions. */
542 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
547 /* Return the N levels of innermost template arguments from the ARGS. */
550 get_innermost_template_args (tree args, int n)
558 /* If N is 1, just return the innermost set of template arguments. */
560 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
562 /* If we're not removing anything, just return the arguments we were
564 extra_levels = TMPL_ARGS_DEPTH (args) - n;
565 gcc_assert (extra_levels >= 0);
566 if (extra_levels == 0)
569 /* Make a new set of arguments, not containing the outer arguments. */
570 new_args = make_tree_vec (n);
571 for (i = 1; i <= n; ++i)
572 SET_TMPL_ARGS_LEVEL (new_args, i,
573 TMPL_ARGS_LEVEL (args, i + extra_levels));
578 /* The inverse of get_innermost_template_args: Return all but the innermost
579 EXTRA_LEVELS levels of template arguments from the ARGS. */
582 strip_innermost_template_args (tree args, int extra_levels)
585 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
590 /* If N is 1, just return the outermost set of template arguments. */
592 return TMPL_ARGS_LEVEL (args, 1);
594 /* If we're not removing anything, just return the arguments we were
596 gcc_assert (extra_levels >= 0);
597 if (extra_levels == 0)
600 /* Make a new set of arguments, not containing the inner arguments. */
601 new_args = make_tree_vec (n);
602 for (i = 1; i <= n; ++i)
603 SET_TMPL_ARGS_LEVEL (new_args, i,
604 TMPL_ARGS_LEVEL (args, i));
609 /* We've got a template header coming up; push to a new level for storing
613 begin_template_parm_list (void)
615 /* We use a non-tag-transparent scope here, which causes pushtag to
616 put tags in this scope, rather than in the enclosing class or
617 namespace scope. This is the right thing, since we want
618 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
619 global template class, push_template_decl handles putting the
620 TEMPLATE_DECL into top-level scope. For a nested template class,
623 template <class T> struct S1 {
624 template <class T> struct S2 {};
627 pushtag contains special code to call pushdecl_with_scope on the
628 TEMPLATE_DECL for S2. */
629 begin_scope (sk_template_parms, NULL);
630 ++processing_template_decl;
631 ++processing_template_parmlist;
632 note_template_header (0);
635 /* This routine is called when a specialization is declared. If it is
636 invalid to declare a specialization here, an error is reported and
637 false is returned, otherwise this routine will return true. */
640 check_specialization_scope (void)
642 tree scope = current_scope ();
646 An explicit specialization shall be declared in the namespace of
647 which the template is a member, or, for member templates, in the
648 namespace of which the enclosing class or enclosing class
649 template is a member. An explicit specialization of a member
650 function, member class or static data member of a class template
651 shall be declared in the namespace of which the class template
653 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
655 error ("explicit specialization in non-namespace scope %qD", scope);
661 In an explicit specialization declaration for a member of a class
662 template or a member template that appears in namespace scope,
663 the member template and some of its enclosing class templates may
664 remain unspecialized, except that the declaration shall not
665 explicitly specialize a class member template if its enclosing
666 class templates are not explicitly specialized as well. */
667 if (current_template_parms)
669 error ("enclosing class templates are not explicitly specialized");
676 /* We've just seen template <>. */
679 begin_specialization (void)
681 begin_scope (sk_template_spec, NULL);
682 note_template_header (1);
683 return check_specialization_scope ();
686 /* Called at then end of processing a declaration preceded by
690 end_specialization (void)
693 reset_specialization ();
696 /* Any template <>'s that we have seen thus far are not referring to a
697 function specialization. */
700 reset_specialization (void)
702 processing_specialization = 0;
703 template_header_count = 0;
706 /* We've just seen a template header. If SPECIALIZATION is nonzero,
707 it was of the form template <>. */
710 note_template_header (int specialization)
712 processing_specialization = specialization;
713 template_header_count++;
716 /* We're beginning an explicit instantiation. */
719 begin_explicit_instantiation (void)
721 gcc_assert (!processing_explicit_instantiation);
722 processing_explicit_instantiation = true;
727 end_explicit_instantiation (void)
729 gcc_assert (processing_explicit_instantiation);
730 processing_explicit_instantiation = false;
733 /* An explicit specialization or partial specialization TMPL is being
734 declared. Check that the namespace in which the specialization is
735 occurring is permissible. Returns false iff it is invalid to
736 specialize TMPL in the current namespace. */
739 check_specialization_namespace (tree tmpl)
741 tree tpl_ns = decl_namespace_context (tmpl);
745 An explicit specialization shall be declared in the namespace of
746 which the template is a member, or, for member templates, in the
747 namespace of which the enclosing class or enclosing class
748 template is a member. An explicit specialization of a member
749 function, member class or static data member of a class template
750 shall be declared in the namespace of which the class template is
752 if (current_scope() != DECL_CONTEXT (tmpl)
753 && !at_namespace_scope_p ())
755 error ("specialization of %qD must appear at namespace scope", tmpl);
758 if (is_associated_namespace (current_namespace, tpl_ns))
759 /* Same or super-using namespace. */
763 permerror (input_location, "specialization of %qD in different namespace", tmpl);
764 permerror (input_location, " from definition of %q+#D", tmpl);
769 /* SPEC is an explicit instantiation. Check that it is valid to
770 perform this explicit instantiation in the current namespace. */
773 check_explicit_instantiation_namespace (tree spec)
777 /* DR 275: An explicit instantiation shall appear in an enclosing
778 namespace of its template. */
779 ns = decl_namespace_context (spec);
780 if (!is_ancestor (current_namespace, ns))
781 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
782 "(which does not enclose namespace %qD)",
783 spec, current_namespace, ns);
786 /* The TYPE is being declared. If it is a template type, that means it
787 is a partial specialization. Do appropriate error-checking. */
790 maybe_process_partial_specialization (tree type)
794 if (type == error_mark_node)
795 return error_mark_node;
797 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
799 error ("name of class shadows template template parameter %qD",
801 return error_mark_node;
804 context = TYPE_CONTEXT (type);
806 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
808 /* This is for ordinary explicit specialization and partial
809 specialization of a template class such as:
811 template <> class C<int>;
815 template <class T> class C<T*>;
817 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
819 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
820 && !COMPLETE_TYPE_P (type))
822 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
823 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
824 if (processing_template_decl)
826 if (push_template_decl (TYPE_MAIN_DECL (type))
828 return error_mark_node;
831 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
832 error ("specialization of %qT after instantiation", type);
834 else if (CLASS_TYPE_P (type)
835 && !CLASSTYPE_USE_TEMPLATE (type)
836 && CLASSTYPE_TEMPLATE_INFO (type)
837 && context && CLASS_TYPE_P (context)
838 && CLASSTYPE_TEMPLATE_INFO (context))
840 /* This is for an explicit specialization of member class
841 template according to [temp.expl.spec/18]:
843 template <> template <class U> class C<int>::D;
845 The context `C<int>' must be an implicit instantiation.
846 Otherwise this is just a member class template declared
849 template <> class C<int> { template <class U> class D; };
850 template <> template <class U> class C<int>::D;
852 In the first case, `C<int>::D' is a specialization of `C<T>::D'
853 while in the second case, `C<int>::D' is a primary template
854 and `C<T>::D' may not exist. */
856 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
857 && !COMPLETE_TYPE_P (type))
860 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
862 if (current_namespace
863 != decl_namespace_context (tmpl))
865 permerror (input_location, "specializing %q#T in different namespace", type);
866 permerror (input_location, " from definition of %q+#D", tmpl);
869 /* Check for invalid specialization after instantiation:
871 template <> template <> class C<int>::D<int>;
872 template <> template <class U> class C<int>::D; */
874 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
875 t; t = TREE_CHAIN (t))
877 tree inst = TREE_VALUE (t);
878 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
880 /* We already have a full specialization of this partial
881 instantiation. Reassign it to the new member
882 specialization template. */
886 elt.tmpl = most_general_template (tmpl);
887 elt.args = CLASSTYPE_TI_ARGS (inst);
890 htab_remove_elt (type_specializations, &elt);
893 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
895 slot = (spec_entry **)
896 htab_find_slot (type_specializations, &elt, INSERT);
897 *slot = ggc_alloc_spec_entry ();
900 else if (COMPLETE_OR_OPEN_TYPE_P (inst))
901 /* But if we've had an implicit instantiation, that's a
902 problem ([temp.expl.spec]/6). */
903 error ("specialization %qT after instantiation %qT",
907 /* Mark TYPE as a specialization. And as a result, we only
908 have one level of template argument for the innermost
910 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
911 CLASSTYPE_TI_ARGS (type)
912 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
915 else if (processing_specialization)
917 /* Someday C++0x may allow for enum template specialization. */
918 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
919 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
920 pedwarn (input_location, OPT_pedantic, "template specialization "
921 "of %qD not allowed by ISO C++", type);
924 error ("explicit specialization of non-template %qT", type);
925 return error_mark_node;
932 /* Returns nonzero if we can optimize the retrieval of specializations
933 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
934 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
937 optimize_specialization_lookup_p (tree tmpl)
939 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
940 && DECL_CLASS_SCOPE_P (tmpl)
941 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
943 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
944 /* The optimized lookup depends on the fact that the
945 template arguments for the member function template apply
946 purely to the containing class, which is not true if the
947 containing class is an explicit or partial
949 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
950 && !DECL_MEMBER_TEMPLATE_P (tmpl)
951 && !DECL_CONV_FN_P (tmpl)
952 /* It is possible to have a template that is not a member
953 template and is not a member of a template class:
955 template <typename T>
956 struct S { friend A::f(); };
958 Here, the friend function is a template, but the context does
959 not have template information. The optimized lookup relies
960 on having ARGS be the template arguments for both the class
961 and the function template. */
962 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
965 /* Retrieve the specialization (in the sense of [temp.spec] - a
966 specialization is either an instantiation or an explicit
967 specialization) of TMPL for the given template ARGS. If there is
968 no such specialization, return NULL_TREE. The ARGS are a vector of
969 arguments, or a vector of vectors of arguments, in the case of
970 templates with more than one level of parameters.
972 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
973 then we search for a partial specialization matching ARGS. This
974 parameter is ignored if TMPL is not a class template. */
977 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
979 if (args == error_mark_node)
982 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
984 /* There should be as many levels of arguments as there are
985 levels of parameters. */
986 gcc_assert (TMPL_ARGS_DEPTH (args)
987 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
989 if (optimize_specialization_lookup_p (tmpl))
992 tree class_specialization;
993 VEC(tree,gc) *methods;
997 /* The template arguments actually apply to the containing
998 class. Find the class specialization with those
1000 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1001 class_specialization
1002 = retrieve_specialization (class_template, args, 0);
1003 if (!class_specialization)
1005 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1006 for the specialization. */
1007 idx = class_method_index_for_fn (class_specialization, tmpl);
1010 /* Iterate through the methods with the indicated name, looking
1011 for the one that has an instance of TMPL. */
1012 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1013 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
1015 tree fn = OVL_CURRENT (fns);
1016 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1017 /* using-declarations can add base methods to the method vec,
1018 and we don't want those here. */
1019 && DECL_CONTEXT (fn) == class_specialization)
1028 htab_t specializations;
1032 elt.spec = NULL_TREE;
1034 if (DECL_CLASS_TEMPLATE_P (tmpl))
1035 specializations = type_specializations;
1037 specializations = decl_specializations;
1040 hash = hash_specialization (&elt);
1041 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1049 /* Like retrieve_specialization, but for local declarations. */
1052 retrieve_local_specialization (tree tmpl)
1056 if (local_specializations == NULL)
1059 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1060 htab_hash_pointer (tmpl));
1061 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1064 /* Returns nonzero iff DECL is a specialization of TMPL. */
1067 is_specialization_of (tree decl, tree tmpl)
1071 if (TREE_CODE (decl) == FUNCTION_DECL)
1075 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1081 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1083 for (t = TREE_TYPE (decl);
1085 t = CLASSTYPE_USE_TEMPLATE (t)
1086 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1087 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1094 /* Returns nonzero iff DECL is a specialization of friend declaration
1095 FRIEND_DECL according to [temp.friend]. */
1098 is_specialization_of_friend (tree decl, tree friend_decl)
1100 bool need_template = true;
1103 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1104 || TREE_CODE (decl) == TYPE_DECL);
1106 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1107 of a template class, we want to check if DECL is a specialization
1109 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1110 && DECL_TEMPLATE_INFO (friend_decl)
1111 && !DECL_USE_TEMPLATE (friend_decl))
1113 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1114 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1115 need_template = false;
1117 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1118 && !PRIMARY_TEMPLATE_P (friend_decl))
1119 need_template = false;
1121 /* There is nothing to do if this is not a template friend. */
1122 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1125 if (is_specialization_of (decl, friend_decl))
1129 A member of a class template may be declared to be a friend of a
1130 non-template class. In this case, the corresponding member of
1131 every specialization of the class template is a friend of the
1132 class granting friendship.
1134 For example, given a template friend declaration
1136 template <class T> friend void A<T>::f();
1138 the member function below is considered a friend
1140 template <> struct A<int> {
1144 For this type of template friend, TEMPLATE_DEPTH below will be
1145 nonzero. To determine if DECL is a friend of FRIEND, we first
1146 check if the enclosing class is a specialization of another. */
1148 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1150 && DECL_CLASS_SCOPE_P (decl)
1151 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1152 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1154 /* Next, we check the members themselves. In order to handle
1155 a few tricky cases, such as when FRIEND_DECL's are
1157 template <class T> friend void A<T>::g(T t);
1158 template <class T> template <T t> friend void A<T>::h();
1162 void A<int>::g(int);
1163 template <int> void A<int>::h();
1165 we need to figure out ARGS, the template arguments from
1166 the context of DECL. This is required for template substitution
1167 of `T' in the function parameter of `g' and template parameter
1168 of `h' in the above examples. Here ARGS corresponds to `int'. */
1170 tree context = DECL_CONTEXT (decl);
1171 tree args = NULL_TREE;
1172 int current_depth = 0;
1174 while (current_depth < template_depth)
1176 if (CLASSTYPE_TEMPLATE_INFO (context))
1178 if (current_depth == 0)
1179 args = TYPE_TI_ARGS (context);
1181 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1184 context = TYPE_CONTEXT (context);
1187 if (TREE_CODE (decl) == FUNCTION_DECL)
1192 tree friend_args_type;
1193 tree decl_args_type;
1195 /* Make sure that both DECL and FRIEND_DECL are templates or
1197 is_template = DECL_TEMPLATE_INFO (decl)
1198 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1199 if (need_template ^ is_template)
1201 else if (is_template)
1203 /* If both are templates, check template parameter list. */
1205 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1207 if (!comp_template_parms
1208 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1212 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1215 decl_type = TREE_TYPE (decl);
1217 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1218 tf_none, NULL_TREE);
1219 if (friend_type == error_mark_node)
1222 /* Check if return types match. */
1223 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1226 /* Check if function parameter types match, ignoring the
1227 `this' parameter. */
1228 friend_args_type = TYPE_ARG_TYPES (friend_type);
1229 decl_args_type = TYPE_ARG_TYPES (decl_type);
1230 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1231 friend_args_type = TREE_CHAIN (friend_args_type);
1232 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1233 decl_args_type = TREE_CHAIN (decl_args_type);
1235 return compparms (decl_args_type, friend_args_type);
1239 /* DECL is a TYPE_DECL */
1241 tree decl_type = TREE_TYPE (decl);
1243 /* Make sure that both DECL and FRIEND_DECL are templates or
1246 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1247 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1249 if (need_template ^ is_template)
1251 else if (is_template)
1254 /* If both are templates, check the name of the two
1255 TEMPLATE_DECL's first because is_friend didn't. */
1256 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1257 != DECL_NAME (friend_decl))
1260 /* Now check template parameter list. */
1262 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1264 return comp_template_parms
1265 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1269 return (DECL_NAME (decl)
1270 == DECL_NAME (friend_decl));
1276 /* Register the specialization SPEC as a specialization of TMPL with
1277 the indicated ARGS. IS_FRIEND indicates whether the specialization
1278 is actually just a friend declaration. Returns SPEC, or an
1279 equivalent prior declaration, if available. */
1282 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1286 spec_entry **slot = NULL;
1289 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1291 if (TREE_CODE (spec) == FUNCTION_DECL
1292 && uses_template_parms (DECL_TI_ARGS (spec)))
1293 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1294 register it; we want the corresponding TEMPLATE_DECL instead.
1295 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1296 the more obvious `uses_template_parms (spec)' to avoid problems
1297 with default function arguments. In particular, given
1298 something like this:
1300 template <class T> void f(T t1, T t = T())
1302 the default argument expression is not substituted for in an
1303 instantiation unless and until it is actually needed. */
1306 if (optimize_specialization_lookup_p (tmpl))
1307 /* We don't put these specializations in the hash table, but we might
1308 want to give an error about a mismatch. */
1309 fn = retrieve_specialization (tmpl, args, 0);
1317 hash = hash_specialization (&elt);
1319 slot = (spec_entry **)
1320 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1327 /* We can sometimes try to re-register a specialization that we've
1328 already got. In particular, regenerate_decl_from_template calls
1329 duplicate_decls which will update the specialization list. But,
1330 we'll still get called again here anyhow. It's more convenient
1331 to simply allow this than to try to prevent it. */
1334 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1336 if (DECL_TEMPLATE_INSTANTIATION (fn))
1338 if (DECL_ODR_USED (fn)
1339 || DECL_EXPLICIT_INSTANTIATION (fn))
1341 error ("specialization of %qD after instantiation",
1343 return error_mark_node;
1348 /* This situation should occur only if the first
1349 specialization is an implicit instantiation, the
1350 second is an explicit specialization, and the
1351 implicit instantiation has not yet been used. That
1352 situation can occur if we have implicitly
1353 instantiated a member function and then specialized
1356 We can also wind up here if a friend declaration that
1357 looked like an instantiation turns out to be a
1360 template <class T> void foo(T);
1361 class S { friend void foo<>(int) };
1362 template <> void foo(int);
1364 We transform the existing DECL in place so that any
1365 pointers to it become pointers to the updated
1368 If there was a definition for the template, but not
1369 for the specialization, we want this to look as if
1370 there were no definition, and vice versa. */
1371 DECL_INITIAL (fn) = NULL_TREE;
1372 duplicate_decls (spec, fn, is_friend);
1373 /* The call to duplicate_decls will have applied
1376 An explicit specialization of a function template
1377 is inline only if it is explicitly declared to be,
1378 and independently of whether its function template
1381 to the primary function; now copy the inline bits to
1382 the various clones. */
1383 FOR_EACH_CLONE (clone, fn)
1385 DECL_DECLARED_INLINE_P (clone)
1386 = DECL_DECLARED_INLINE_P (fn);
1387 DECL_SOURCE_LOCATION (clone)
1388 = DECL_SOURCE_LOCATION (fn);
1390 check_specialization_namespace (fn);
1395 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1397 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1398 /* Dup decl failed, but this is a new definition. Set the
1399 line number so any errors match this new
1401 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1407 return duplicate_decls (spec, fn, is_friend);
1409 /* A specialization must be declared in the same namespace as the
1410 template it is specializing. */
1411 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1412 && !check_specialization_namespace (tmpl))
1413 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1415 if (!optimize_specialization_lookup_p (tmpl))
1417 gcc_assert (tmpl && args && spec);
1418 *slot = ggc_alloc_spec_entry ();
1420 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1421 && PRIMARY_TEMPLATE_P (tmpl)
1422 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1423 /* TMPL is a forward declaration of a template function; keep a list
1424 of all specializations in case we need to reassign them to a friend
1425 template later in tsubst_friend_function. */
1426 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1427 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1433 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1434 TMPL and ARGS members, ignores SPEC. */
1437 eq_specializations (const void *p1, const void *p2)
1439 const spec_entry *e1 = (const spec_entry *)p1;
1440 const spec_entry *e2 = (const spec_entry *)p2;
1442 return (e1->tmpl == e2->tmpl
1443 && comp_template_args (e1->args, e2->args));
1446 /* Returns a hash for a template TMPL and template arguments ARGS. */
1449 hash_tmpl_and_args (tree tmpl, tree args)
1451 hashval_t val = DECL_UID (tmpl);
1452 return iterative_hash_template_arg (args, val);
1455 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1459 hash_specialization (const void *p)
1461 const spec_entry *e = (const spec_entry *)p;
1462 return hash_tmpl_and_args (e->tmpl, e->args);
1465 /* Recursively calculate a hash value for a template argument ARG, for use
1466 in the hash tables of template specializations. */
1469 iterative_hash_template_arg (tree arg, hashval_t val)
1471 unsigned HOST_WIDE_INT i;
1472 enum tree_code code;
1475 if (arg == NULL_TREE)
1476 return iterative_hash_object (arg, val);
1481 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1482 /* We can get one of these when re-hashing a previous entry in the middle
1483 of substituting into a pack expansion. Just look through it. */
1484 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1486 code = TREE_CODE (arg);
1487 tclass = TREE_CODE_CLASS (code);
1489 val = iterative_hash_object (code, val);
1496 case IDENTIFIER_NODE:
1497 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1501 int i, len = TREE_VEC_LENGTH (arg);
1502 for (i = 0; i < len; ++i)
1503 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1507 case TYPE_PACK_EXPANSION:
1508 case EXPR_PACK_EXPANSION:
1509 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1511 case TYPE_ARGUMENT_PACK:
1512 case NONTYPE_ARGUMENT_PACK:
1513 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1516 for (; arg; arg = TREE_CHAIN (arg))
1517 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1521 for (; arg; arg = OVL_NEXT (arg))
1522 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1528 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1530 val = iterative_hash_template_arg (field, val);
1531 val = iterative_hash_template_arg (value, val);
1537 if (!DECL_ARTIFICIAL (arg))
1539 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1540 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1542 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1545 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1548 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1549 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1551 case TEMPLATE_PARM_INDEX:
1552 val = iterative_hash_template_arg
1553 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1554 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1555 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1558 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1559 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1560 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1563 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1565 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1569 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1570 code = TREE_CODE (TREE_OPERAND (arg, 1));
1571 val = iterative_hash_object (code, val);
1572 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1575 /* A lambda can't appear in a template arg, but don't crash on
1577 gcc_assert (seen_error ());
1581 case STATIC_CAST_EXPR:
1582 case REINTERPRET_CAST_EXPR:
1583 case CONST_CAST_EXPR:
1584 case DYNAMIC_CAST_EXPR:
1586 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1587 /* Now hash operands as usual. */
1597 if (TYPE_CANONICAL (arg))
1598 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1600 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1601 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1602 /* Otherwise just compare the types during lookup. */
1605 case tcc_declaration:
1607 return iterative_hash_expr (arg, val);
1610 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1612 unsigned n = TREE_OPERAND_LENGTH (arg);
1613 for (i = 0; i < n; ++i)
1614 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1622 /* Unregister the specialization SPEC as a specialization of TMPL.
1623 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1624 if the SPEC was listed as a specialization of TMPL.
1626 Note that SPEC has been ggc_freed, so we can't look inside it. */
1629 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1634 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1635 elt.args = TI_ARGS (tinfo);
1636 elt.spec = NULL_TREE;
1638 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1641 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1642 gcc_assert (new_spec != NULL_TREE);
1643 (*slot)->spec = new_spec;
1650 /* Compare an entry in the local specializations hash table P1 (which
1651 is really a pointer to a TREE_LIST) with P2 (which is really a
1655 eq_local_specializations (const void *p1, const void *p2)
1657 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1660 /* Hash P1, an entry in the local specializations table. */
1663 hash_local_specialization (const void* p1)
1665 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1668 /* Like register_specialization, but for local declarations. We are
1669 registering SPEC, an instantiation of TMPL. */
1672 register_local_specialization (tree spec, tree tmpl)
1676 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1677 htab_hash_pointer (tmpl), INSERT);
1678 *slot = build_tree_list (spec, tmpl);
1681 /* TYPE is a class type. Returns true if TYPE is an explicitly
1682 specialized class. */
1685 explicit_class_specialization_p (tree type)
1687 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1689 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1692 /* Print the list of functions at FNS, going through all the overloads
1693 for each element of the list. Alternatively, FNS can not be a
1694 TREE_LIST, in which case it will be printed together with all the
1697 MORE and *STR should respectively be FALSE and NULL when the function
1698 is called from the outside. They are used internally on recursive
1699 calls. print_candidates manages the two parameters and leaves NULL
1700 in *STR when it ends. */
1703 print_candidates_1 (tree fns, bool more, const char **str)
1706 char *spaces = NULL;
1708 for (fn = fns; fn; fn = OVL_NEXT (fn))
1709 if (TREE_CODE (fn) == TREE_LIST)
1711 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1712 print_candidates_1 (TREE_VALUE (fn2),
1713 TREE_CHAIN (fn2) || more, str);
1719 /* Pick the prefix string. */
1720 if (!more && !OVL_NEXT (fns))
1722 error ("candidate is: %+#D", OVL_CURRENT (fn));
1726 *str = _("candidates are:");
1727 spaces = get_spaces (*str);
1729 error ("%s %+#D", *str, OVL_CURRENT (fn));
1730 *str = spaces ? spaces : *str;
1740 /* Print the list of candidate FNS in an error message. FNS can also
1741 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1744 print_candidates (tree fns)
1746 const char *str = NULL;
1747 print_candidates_1 (fns, false, &str);
1748 gcc_assert (str == NULL);
1751 /* Returns the template (one of the functions given by TEMPLATE_ID)
1752 which can be specialized to match the indicated DECL with the
1753 explicit template args given in TEMPLATE_ID. The DECL may be
1754 NULL_TREE if none is available. In that case, the functions in
1755 TEMPLATE_ID are non-members.
1757 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1758 specialization of a member template.
1760 The TEMPLATE_COUNT is the number of references to qualifying
1761 template classes that appeared in the name of the function. See
1762 check_explicit_specialization for a more accurate description.
1764 TSK indicates what kind of template declaration (if any) is being
1765 declared. TSK_TEMPLATE indicates that the declaration given by
1766 DECL, though a FUNCTION_DECL, has template parameters, and is
1767 therefore a template function.
1769 The template args (those explicitly specified and those deduced)
1770 are output in a newly created vector *TARGS_OUT.
1772 If it is impossible to determine the result, an error message is
1773 issued. The error_mark_node is returned to indicate failure. */
1776 determine_specialization (tree template_id,
1779 int need_member_template,
1785 tree explicit_targs;
1786 tree candidates = NULL_TREE;
1787 /* A TREE_LIST of templates of which DECL may be a specialization.
1788 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1789 corresponding TREE_PURPOSE is the set of template arguments that,
1790 when used to instantiate the template, would produce a function
1791 with the signature of DECL. */
1792 tree templates = NULL_TREE;
1794 struct cp_binding_level *b;
1796 *targs_out = NULL_TREE;
1798 if (template_id == error_mark_node || decl == error_mark_node)
1799 return error_mark_node;
1801 fns = TREE_OPERAND (template_id, 0);
1802 explicit_targs = TREE_OPERAND (template_id, 1);
1804 if (fns == error_mark_node)
1805 return error_mark_node;
1807 /* Check for baselinks. */
1808 if (BASELINK_P (fns))
1809 fns = BASELINK_FUNCTIONS (fns);
1811 if (!is_overloaded_fn (fns))
1813 error ("%qD is not a function template", fns);
1814 return error_mark_node;
1817 /* Count the number of template headers specified for this
1820 for (b = current_binding_level;
1821 b->kind == sk_template_parms;
1825 for (; fns; fns = OVL_NEXT (fns))
1827 tree fn = OVL_CURRENT (fns);
1829 if (TREE_CODE (fn) == TEMPLATE_DECL)
1831 tree decl_arg_types;
1834 /* In case of explicit specialization, we need to check if
1835 the number of template headers appearing in the specialization
1836 is correct. This is usually done in check_explicit_specialization,
1837 but the check done there cannot be exhaustive when specializing
1838 member functions. Consider the following code:
1840 template <> void A<int>::f(int);
1841 template <> template <> void A<int>::f(int);
1843 Assuming that A<int> is not itself an explicit specialization
1844 already, the first line specializes "f" which is a non-template
1845 member function, whilst the second line specializes "f" which
1846 is a template member function. So both lines are syntactically
1847 correct, and check_explicit_specialization does not reject
1850 Here, we can do better, as we are matching the specialization
1851 against the declarations. We count the number of template
1852 headers, and we check if they match TEMPLATE_COUNT + 1
1853 (TEMPLATE_COUNT is the number of qualifying template classes,
1854 plus there must be another header for the member template
1857 Notice that if header_count is zero, this is not a
1858 specialization but rather a template instantiation, so there
1859 is no check we can perform here. */
1860 if (header_count && header_count != template_count + 1)
1863 /* Check that the number of template arguments at the
1864 innermost level for DECL is the same as for FN. */
1865 if (current_binding_level->kind == sk_template_parms
1866 && !current_binding_level->explicit_spec_p
1867 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1868 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1869 (current_template_parms))))
1872 /* DECL might be a specialization of FN. */
1873 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1874 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1876 /* For a non-static member function, we need to make sure
1877 that the const qualification is the same. Since
1878 get_bindings does not try to merge the "this" parameter,
1879 we must do the comparison explicitly. */
1880 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1881 && !same_type_p (TREE_VALUE (fn_arg_types),
1882 TREE_VALUE (decl_arg_types)))
1885 /* Skip the "this" parameter and, for constructors of
1886 classes with virtual bases, the VTT parameter. A
1887 full specialization of a constructor will have a VTT
1888 parameter, but a template never will. */
1890 = skip_artificial_parms_for (decl, decl_arg_types);
1892 = skip_artificial_parms_for (fn, fn_arg_types);
1894 /* Check that the number of function parameters matches.
1896 template <class T> void f(int i = 0);
1897 template <> void f<int>();
1898 The specialization f<int> is invalid but is not caught
1899 by get_bindings below. */
1900 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1903 /* Function templates cannot be specializations; there are
1904 no partial specializations of functions. Therefore, if
1905 the type of DECL does not match FN, there is no
1907 if (tsk == tsk_template)
1909 if (compparms (fn_arg_types, decl_arg_types))
1910 candidates = tree_cons (NULL_TREE, fn, candidates);
1914 /* See whether this function might be a specialization of this
1916 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1919 /* We cannot deduce template arguments that when used to
1920 specialize TMPL will produce DECL. */
1923 /* Save this template, and the arguments deduced. */
1924 templates = tree_cons (targs, fn, templates);
1926 else if (need_member_template)
1927 /* FN is an ordinary member function, and we need a
1928 specialization of a member template. */
1930 else if (TREE_CODE (fn) != FUNCTION_DECL)
1931 /* We can get IDENTIFIER_NODEs here in certain erroneous
1934 else if (!DECL_FUNCTION_MEMBER_P (fn))
1935 /* This is just an ordinary non-member function. Nothing can
1936 be a specialization of that. */
1938 else if (DECL_ARTIFICIAL (fn))
1939 /* Cannot specialize functions that are created implicitly. */
1943 tree decl_arg_types;
1945 /* This is an ordinary member function. However, since
1946 we're here, we can assume it's enclosing class is a
1947 template class. For example,
1949 template <typename T> struct S { void f(); };
1950 template <> void S<int>::f() {}
1952 Here, S<int>::f is a non-template, but S<int> is a
1953 template class. If FN has the same type as DECL, we
1954 might be in business. */
1956 if (!DECL_TEMPLATE_INFO (fn))
1957 /* Its enclosing class is an explicit specialization
1958 of a template class. This is not a candidate. */
1961 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1962 TREE_TYPE (TREE_TYPE (fn))))
1963 /* The return types differ. */
1966 /* Adjust the type of DECL in case FN is a static member. */
1967 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1968 if (DECL_STATIC_FUNCTION_P (fn)
1969 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1970 decl_arg_types = TREE_CHAIN (decl_arg_types);
1972 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1975 candidates = tree_cons (NULL_TREE, fn, candidates);
1979 if (templates && TREE_CHAIN (templates))
1985 It is possible for a specialization with a given function
1986 signature to be instantiated from more than one function
1987 template. In such cases, explicit specification of the
1988 template arguments must be used to uniquely identify the
1989 function template specialization being specialized.
1991 Note that here, there's no suggestion that we're supposed to
1992 determine which of the candidate templates is most
1993 specialized. However, we, also have:
1997 Partial ordering of overloaded function template
1998 declarations is used in the following contexts to select
1999 the function template to which a function template
2000 specialization refers:
2002 -- when an explicit specialization refers to a function
2005 So, we do use the partial ordering rules, at least for now.
2006 This extension can only serve to make invalid programs valid,
2007 so it's safe. And, there is strong anecdotal evidence that
2008 the committee intended the partial ordering rules to apply;
2009 the EDG front end has that behavior, and John Spicer claims
2010 that the committee simply forgot to delete the wording in
2011 [temp.expl.spec]. */
2012 tree tmpl = most_specialized_instantiation (templates);
2013 if (tmpl != error_mark_node)
2016 TREE_CHAIN (templates) = NULL_TREE;
2020 if (templates == NULL_TREE && candidates == NULL_TREE)
2022 error ("template-id %qD for %q+D does not match any template "
2023 "declaration", template_id, decl);
2024 if (header_count && header_count != template_count + 1)
2025 inform (input_location, "saw %d %<template<>%>, need %d for "
2026 "specializing a member function template",
2027 header_count, template_count + 1);
2028 return error_mark_node;
2030 else if ((templates && TREE_CHAIN (templates))
2031 || (candidates && TREE_CHAIN (candidates))
2032 || (templates && candidates))
2034 error ("ambiguous template specialization %qD for %q+D",
2036 candidates = chainon (candidates, templates);
2037 print_candidates (candidates);
2038 return error_mark_node;
2041 /* We have one, and exactly one, match. */
2044 tree fn = TREE_VALUE (candidates);
2045 *targs_out = copy_node (DECL_TI_ARGS (fn));
2046 /* DECL is a re-declaration or partial instantiation of a template
2048 if (TREE_CODE (fn) == TEMPLATE_DECL)
2050 /* It was a specialization of an ordinary member function in a
2052 return DECL_TI_TEMPLATE (fn);
2055 /* It was a specialization of a template. */
2056 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2057 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2059 *targs_out = copy_node (targs);
2060 SET_TMPL_ARGS_LEVEL (*targs_out,
2061 TMPL_ARGS_DEPTH (*targs_out),
2062 TREE_PURPOSE (templates));
2065 *targs_out = TREE_PURPOSE (templates);
2066 return TREE_VALUE (templates);
2069 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2070 but with the default argument values filled in from those in the
2074 copy_default_args_to_explicit_spec_1 (tree spec_types,
2077 tree new_spec_types;
2082 if (spec_types == void_list_node)
2083 return void_list_node;
2085 /* Substitute into the rest of the list. */
2087 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2088 TREE_CHAIN (tmpl_types));
2090 /* Add the default argument for this parameter. */
2091 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2092 TREE_VALUE (spec_types),
2096 /* DECL is an explicit specialization. Replicate default arguments
2097 from the template it specializes. (That way, code like:
2099 template <class T> void f(T = 3);
2100 template <> void f(double);
2103 works, as required.) An alternative approach would be to look up
2104 the correct default arguments at the call-site, but this approach
2105 is consistent with how implicit instantiations are handled. */
2108 copy_default_args_to_explicit_spec (tree decl)
2113 tree new_spec_types;
2117 tree object_type = NULL_TREE;
2118 tree in_charge = NULL_TREE;
2119 tree vtt = NULL_TREE;
2121 /* See if there's anything we need to do. */
2122 tmpl = DECL_TI_TEMPLATE (decl);
2123 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2124 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2125 if (TREE_PURPOSE (t))
2130 old_type = TREE_TYPE (decl);
2131 spec_types = TYPE_ARG_TYPES (old_type);
2133 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2135 /* Remove the this pointer, but remember the object's type for
2137 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2138 spec_types = TREE_CHAIN (spec_types);
2139 tmpl_types = TREE_CHAIN (tmpl_types);
2141 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2143 /* DECL may contain more parameters than TMPL due to the extra
2144 in-charge parameter in constructors and destructors. */
2145 in_charge = spec_types;
2146 spec_types = TREE_CHAIN (spec_types);
2148 if (DECL_HAS_VTT_PARM_P (decl))
2151 spec_types = TREE_CHAIN (spec_types);
2155 /* Compute the merged default arguments. */
2157 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2159 /* Compute the new FUNCTION_TYPE. */
2163 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2168 /* Put the in-charge parameter back. */
2169 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2170 TREE_VALUE (in_charge),
2173 new_type = build_method_type_directly (object_type,
2174 TREE_TYPE (old_type),
2178 new_type = build_function_type (TREE_TYPE (old_type),
2180 new_type = cp_build_type_attribute_variant (new_type,
2181 TYPE_ATTRIBUTES (old_type));
2182 new_type = build_exception_variant (new_type,
2183 TYPE_RAISES_EXCEPTIONS (old_type));
2184 TREE_TYPE (decl) = new_type;
2187 /* Check to see if the function just declared, as indicated in
2188 DECLARATOR, and in DECL, is a specialization of a function
2189 template. We may also discover that the declaration is an explicit
2190 instantiation at this point.
2192 Returns DECL, or an equivalent declaration that should be used
2193 instead if all goes well. Issues an error message if something is
2194 amiss. Returns error_mark_node if the error is not easily
2197 FLAGS is a bitmask consisting of the following flags:
2199 2: The function has a definition.
2200 4: The function is a friend.
2202 The TEMPLATE_COUNT is the number of references to qualifying
2203 template classes that appeared in the name of the function. For
2206 template <class T> struct S { void f(); };
2209 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2210 classes are not counted in the TEMPLATE_COUNT, so that in
2212 template <class T> struct S {};
2213 template <> struct S<int> { void f(); }
2214 template <> void S<int>::f();
2216 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2217 invalid; there should be no template <>.)
2219 If the function is a specialization, it is marked as such via
2220 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2221 is set up correctly, and it is added to the list of specializations
2222 for that template. */
2225 check_explicit_specialization (tree declarator,
2230 int have_def = flags & 2;
2231 int is_friend = flags & 4;
2232 int specialization = 0;
2233 int explicit_instantiation = 0;
2234 int member_specialization = 0;
2235 tree ctype = DECL_CLASS_CONTEXT (decl);
2236 tree dname = DECL_NAME (decl);
2241 if (!processing_specialization)
2244 tsk = tsk_excessive_parms;
2247 tsk = current_tmpl_spec_kind (template_count);
2252 if (processing_specialization)
2255 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2257 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2260 /* This could be something like:
2262 template <class T> void f(T);
2263 class S { friend void f<>(int); } */
2267 /* This case handles bogus declarations like template <>
2268 template <class T> void f<int>(); */
2270 error ("template-id %qD in declaration of primary template",
2277 case tsk_invalid_member_spec:
2278 /* The error has already been reported in
2279 check_specialization_scope. */
2280 return error_mark_node;
2282 case tsk_invalid_expl_inst:
2283 error ("template parameter list used in explicit instantiation");
2289 error ("definition provided for explicit instantiation");
2291 explicit_instantiation = 1;
2294 case tsk_excessive_parms:
2295 case tsk_insufficient_parms:
2296 if (tsk == tsk_excessive_parms)
2297 error ("too many template parameter lists in declaration of %qD",
2299 else if (template_header_count)
2300 error("too few template parameter lists in declaration of %qD", decl);
2302 error("explicit specialization of %qD must be introduced by "
2303 "%<template <>%>", decl);
2307 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2309 member_specialization = 1;
2315 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2317 /* This case handles bogus declarations like template <>
2318 template <class T> void f<int>(); */
2320 if (uses_template_parms (declarator))
2321 error ("function template partial specialization %qD "
2322 "is not allowed", declarator);
2324 error ("template-id %qD in declaration of primary template",
2329 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2330 /* This is a specialization of a member template, without
2331 specialization the containing class. Something like:
2333 template <class T> struct S {
2334 template <class U> void f (U);
2336 template <> template <class U> void S<int>::f(U) {}
2338 That's a specialization -- but of the entire template. */
2346 if (specialization || member_specialization)
2348 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2349 for (; t; t = TREE_CHAIN (t))
2350 if (TREE_PURPOSE (t))
2352 permerror (input_location,
2353 "default argument specified in explicit specialization");
2358 if (specialization || member_specialization || explicit_instantiation)
2360 tree tmpl = NULL_TREE;
2361 tree targs = NULL_TREE;
2363 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2364 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2368 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2373 /* If there is no class context, the explicit instantiation
2374 must be at namespace scope. */
2375 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2377 /* Find the namespace binding, using the declaration
2379 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2381 if (fns == error_mark_node || !is_overloaded_fn (fns))
2383 error ("%qD is not a template function", dname);
2384 fns = error_mark_node;
2388 tree fn = OVL_CURRENT (fns);
2389 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2390 CP_DECL_CONTEXT (fn)))
2391 error ("%qD is not declared in %qD",
2392 decl, current_namespace);
2396 declarator = lookup_template_function (fns, NULL_TREE);
2399 if (declarator == error_mark_node)
2400 return error_mark_node;
2402 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2404 if (!explicit_instantiation)
2405 /* A specialization in class scope. This is invalid,
2406 but the error will already have been flagged by
2407 check_specialization_scope. */
2408 return error_mark_node;
2411 /* It's not valid to write an explicit instantiation in
2414 class C { template void f(); }
2416 This case is caught by the parser. However, on
2419 template class C { void f(); };
2421 (which is invalid) we can get here. The error will be
2428 else if (ctype != NULL_TREE
2429 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2432 /* Find the list of functions in ctype that have the same
2433 name as the declared function. */
2434 tree name = TREE_OPERAND (declarator, 0);
2435 tree fns = NULL_TREE;
2438 if (constructor_name_p (name, ctype))
2440 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2442 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2443 : !CLASSTYPE_DESTRUCTORS (ctype))
2445 /* From [temp.expl.spec]:
2447 If such an explicit specialization for the member
2448 of a class template names an implicitly-declared
2449 special member function (clause _special_), the
2450 program is ill-formed.
2452 Similar language is found in [temp.explicit]. */
2453 error ("specialization of implicitly-declared special member function");
2454 return error_mark_node;
2457 name = is_constructor ? ctor_identifier : dtor_identifier;
2460 if (!DECL_CONV_FN_P (decl))
2462 idx = lookup_fnfields_1 (ctype, name);
2464 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2468 VEC(tree,gc) *methods;
2471 /* For a type-conversion operator, we cannot do a
2472 name-based lookup. We might be looking for `operator
2473 int' which will be a specialization of `operator T'.
2474 So, we find *all* the conversion operators, and then
2475 select from them. */
2478 methods = CLASSTYPE_METHOD_VEC (ctype);
2480 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2481 VEC_iterate (tree, methods, idx, ovl);
2484 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2485 /* There are no more conversion functions. */
2488 /* Glue all these conversion functions together
2489 with those we already have. */
2490 for (; ovl; ovl = OVL_NEXT (ovl))
2491 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2495 if (fns == NULL_TREE)
2497 error ("no member function %qD declared in %qT", name, ctype);
2498 return error_mark_node;
2501 TREE_OPERAND (declarator, 0) = fns;
2504 /* Figure out what exactly is being specialized at this point.
2505 Note that for an explicit instantiation, even one for a
2506 member function, we cannot tell apriori whether the
2507 instantiation is for a member template, or just a member
2508 function of a template class. Even if a member template is
2509 being instantiated, the member template arguments may be
2510 elided if they can be deduced from the rest of the
2512 tmpl = determine_specialization (declarator, decl,
2514 member_specialization,
2518 if (!tmpl || tmpl == error_mark_node)
2519 /* We couldn't figure out what this declaration was
2521 return error_mark_node;
2524 tree gen_tmpl = most_general_template (tmpl);
2526 if (explicit_instantiation)
2528 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2529 is done by do_decl_instantiation later. */
2531 int arg_depth = TMPL_ARGS_DEPTH (targs);
2532 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2534 if (arg_depth > parm_depth)
2536 /* If TMPL is not the most general template (for
2537 example, if TMPL is a friend template that is
2538 injected into namespace scope), then there will
2539 be too many levels of TARGS. Remove some of them
2544 new_targs = make_tree_vec (parm_depth);
2545 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2546 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2547 = TREE_VEC_ELT (targs, i);
2551 return instantiate_template (tmpl, targs, tf_error);
2554 /* If we thought that the DECL was a member function, but it
2555 turns out to be specializing a static member function,
2556 make DECL a static member function as well. */
2557 if (DECL_STATIC_FUNCTION_P (tmpl)
2558 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2559 revert_static_member_fn (decl);
2561 /* If this is a specialization of a member template of a
2562 template class, we want to return the TEMPLATE_DECL, not
2563 the specialization of it. */
2564 if (tsk == tsk_template)
2566 tree result = DECL_TEMPLATE_RESULT (tmpl);
2567 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2568 DECL_INITIAL (result) = NULL_TREE;
2572 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2573 DECL_SOURCE_LOCATION (result)
2574 = DECL_SOURCE_LOCATION (decl);
2575 /* We want to use the argument list specified in the
2576 definition, not in the original declaration. */
2577 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2578 for (parm = DECL_ARGUMENTS (result); parm;
2579 parm = DECL_CHAIN (parm))
2580 DECL_CONTEXT (parm) = result;
2582 return register_specialization (tmpl, gen_tmpl, targs,
2586 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2587 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
2589 /* Inherit default function arguments from the template
2590 DECL is specializing. */
2591 copy_default_args_to_explicit_spec (decl);
2593 /* This specialization has the same protection as the
2594 template it specializes. */
2595 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2596 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2598 /* 7.1.1-1 [dcl.stc]
2600 A storage-class-specifier shall not be specified in an
2601 explicit specialization...
2603 The parser rejects these, so unless action is taken here,
2604 explicit function specializations will always appear with
2607 The action recommended by the C++ CWG in response to C++
2608 defect report 605 is to make the storage class and linkage
2609 of the explicit specialization match the templated function:
2611 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2613 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2615 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2616 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2618 /* This specialization has the same linkage and visibility as
2619 the function template it specializes. */
2620 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2621 if (! TREE_PUBLIC (decl))
2623 DECL_INTERFACE_KNOWN (decl) = 1;
2624 DECL_NOT_REALLY_EXTERN (decl) = 1;
2626 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2627 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2629 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2630 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2634 /* If DECL is a friend declaration, declared using an
2635 unqualified name, the namespace associated with DECL may
2636 have been set incorrectly. For example, in:
2638 template <typename T> void f(T);
2640 struct S { friend void f<int>(int); }
2643 we will have set the DECL_CONTEXT for the friend
2644 declaration to N, rather than to the global namespace. */
2645 if (DECL_NAMESPACE_SCOPE_P (decl))
2646 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2648 if (is_friend && !have_def)
2649 /* This is not really a declaration of a specialization.
2650 It's just the name of an instantiation. But, it's not
2651 a request for an instantiation, either. */
2652 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2653 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2654 /* This is indeed a specialization. In case of constructors
2655 and destructors, we need in-charge and not-in-charge
2656 versions in V3 ABI. */
2657 clone_function_decl (decl, /*update_method_vec_p=*/0);
2659 /* Register this specialization so that we can find it
2661 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2668 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2669 parameters. These are represented in the same format used for
2670 DECL_TEMPLATE_PARMS. */
2673 comp_template_parms (const_tree parms1, const_tree parms2)
2678 if (parms1 == parms2)
2681 for (p1 = parms1, p2 = parms2;
2682 p1 != NULL_TREE && p2 != NULL_TREE;
2683 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2685 tree t1 = TREE_VALUE (p1);
2686 tree t2 = TREE_VALUE (p2);
2689 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2690 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2692 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2695 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2697 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2698 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2700 /* If either of the template parameters are invalid, assume
2701 they match for the sake of error recovery. */
2702 if (parm1 == error_mark_node || parm2 == error_mark_node)
2705 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2708 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2709 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2710 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2712 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2717 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2718 /* One set of parameters has more parameters lists than the
2725 /* Determine whether PARM is a parameter pack. */
2728 template_parameter_pack_p (const_tree parm)
2730 /* Determine if we have a non-type template parameter pack. */
2731 if (TREE_CODE (parm) == PARM_DECL)
2732 return (DECL_TEMPLATE_PARM_P (parm)
2733 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2735 /* If this is a list of template parameters, we could get a
2736 TYPE_DECL or a TEMPLATE_DECL. */
2737 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2738 parm = TREE_TYPE (parm);
2740 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2741 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2742 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2745 /* Determine if T is a function parameter pack. */
2748 function_parameter_pack_p (const_tree t)
2750 if (t && TREE_CODE (t) == PARM_DECL)
2751 return FUNCTION_PARAMETER_PACK_P (t);
2755 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2756 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2759 get_function_template_decl (const_tree primary_func_tmpl_inst)
2761 if (! primary_func_tmpl_inst
2762 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2763 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2766 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2769 /* Return true iff the function parameter PARAM_DECL was expanded
2770 from the function parameter pack PACK. */
2773 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2775 if (DECL_ARTIFICIAL (param_decl)
2776 || !function_parameter_pack_p (pack))
2779 /* The parameter pack and its pack arguments have the same
2781 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2784 /* Determine whether ARGS describes a variadic template args list,
2785 i.e., one that is terminated by a template argument pack. */
2788 template_args_variadic_p (tree args)
2793 if (args == NULL_TREE)
2796 args = INNERMOST_TEMPLATE_ARGS (args);
2797 nargs = TREE_VEC_LENGTH (args);
2802 last_parm = TREE_VEC_ELT (args, nargs - 1);
2804 return ARGUMENT_PACK_P (last_parm);
2807 /* Generate a new name for the parameter pack name NAME (an
2808 IDENTIFIER_NODE) that incorporates its */
2811 make_ith_pack_parameter_name (tree name, int i)
2813 /* Munge the name to include the parameter index. */
2814 #define NUMBUF_LEN 128
2815 char numbuf[NUMBUF_LEN];
2819 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2820 newname_len = IDENTIFIER_LENGTH (name)
2821 + strlen (numbuf) + 2;
2822 newname = (char*)alloca (newname_len);
2823 snprintf (newname, newname_len,
2824 "%s#%i", IDENTIFIER_POINTER (name), i);
2825 return get_identifier (newname);
2828 /* Return true if T is a primary function
2829 or class template instantiation. */
2832 primary_template_instantiation_p (const_tree t)
2837 if (TREE_CODE (t) == FUNCTION_DECL)
2838 return DECL_LANG_SPECIFIC (t)
2839 && DECL_TEMPLATE_INSTANTIATION (t)
2840 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2841 else if (CLASS_TYPE_P (t))
2842 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2843 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2847 /* Return true if PARM is a template template parameter. */
2850 template_template_parameter_p (const_tree parm)
2852 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2855 /* Return the template parameters of T if T is a
2856 primary template instantiation, NULL otherwise. */
2859 get_primary_template_innermost_parameters (const_tree t)
2861 tree parms = NULL, template_info = NULL;
2863 if ((template_info = get_template_info (t))
2864 && primary_template_instantiation_p (t))
2865 parms = INNERMOST_TEMPLATE_PARMS
2866 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2871 /* Return the template parameters of the LEVELth level from the full list
2872 of template parameters PARMS. */
2875 get_template_parms_at_level (tree parms, int level)
2879 || TREE_CODE (parms) != TREE_LIST
2880 || level > TMPL_PARMS_DEPTH (parms))
2883 for (p = parms; p; p = TREE_CHAIN (p))
2884 if (TMPL_PARMS_DEPTH (p) == level)
2890 /* Returns the template arguments of T if T is a template instantiation,
2894 get_template_innermost_arguments (const_tree t)
2896 tree args = NULL, template_info = NULL;
2898 if ((template_info = get_template_info (t))
2899 && TI_ARGS (template_info))
2900 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2905 /* Return the argument pack elements of T if T is a template argument pack,
2909 get_template_argument_pack_elems (const_tree t)
2911 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2912 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2915 return ARGUMENT_PACK_ARGS (t);
2918 /* Structure used to track the progress of find_parameter_packs_r. */
2919 struct find_parameter_pack_data
2921 /* TREE_LIST that will contain all of the parameter packs found by
2923 tree* parameter_packs;
2925 /* Set of AST nodes that have been visited by the traversal. */
2926 struct pointer_set_t *visited;
2929 /* Identifies all of the argument packs that occur in a template
2930 argument and appends them to the TREE_LIST inside DATA, which is a
2931 find_parameter_pack_data structure. This is a subroutine of
2932 make_pack_expansion and uses_parameter_packs. */
2934 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2937 struct find_parameter_pack_data* ppd =
2938 (struct find_parameter_pack_data*)data;
2939 bool parameter_pack_p = false;
2941 /* Identify whether this is a parameter pack or not. */
2942 switch (TREE_CODE (t))
2944 case TEMPLATE_PARM_INDEX:
2945 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2946 parameter_pack_p = true;
2949 case TEMPLATE_TYPE_PARM:
2950 case TEMPLATE_TEMPLATE_PARM:
2951 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2952 parameter_pack_p = true;
2956 if (FUNCTION_PARAMETER_PACK_P (t))
2958 /* We don't want to walk into the type of a PARM_DECL,
2959 because we don't want to see the type parameter pack. */
2961 parameter_pack_p = true;
2966 /* Not a parameter pack. */
2970 if (parameter_pack_p)
2972 /* Add this parameter pack to the list. */
2973 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2977 cp_walk_tree (&TYPE_CONTEXT (t),
2978 &find_parameter_packs_r, ppd, ppd->visited);
2980 /* This switch statement will return immediately if we don't find a
2982 switch (TREE_CODE (t))
2984 case TEMPLATE_PARM_INDEX:
2987 case BOUND_TEMPLATE_TEMPLATE_PARM:
2988 /* Check the template itself. */
2989 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2990 &find_parameter_packs_r, ppd, ppd->visited);
2991 /* Check the template arguments. */
2992 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2997 case TEMPLATE_TYPE_PARM:
2998 case TEMPLATE_TEMPLATE_PARM:
3005 if (TYPE_PTRMEMFUNC_P (t))
3011 if (TYPE_TEMPLATE_INFO (t))
3012 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
3013 &find_parameter_packs_r, ppd, ppd->visited);
3019 cp_walk_tree (&TREE_TYPE (t),
3020 &find_parameter_packs_r, ppd, ppd->visited);
3024 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3029 case TYPE_PACK_EXPANSION:
3030 case EXPR_PACK_EXPANSION:
3035 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3040 case IDENTIFIER_NODE:
3041 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3053 /* Determines if the expression or type T uses any parameter packs. */
3055 uses_parameter_packs (tree t)
3057 tree parameter_packs = NULL_TREE;
3058 struct find_parameter_pack_data ppd;
3059 ppd.parameter_packs = ¶meter_packs;
3060 ppd.visited = pointer_set_create ();
3061 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3062 pointer_set_destroy (ppd.visited);
3063 return parameter_packs != NULL_TREE;
3066 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3067 representation a base-class initializer into a parameter pack
3068 expansion. If all goes well, the resulting node will be an
3069 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3072 make_pack_expansion (tree arg)
3075 tree parameter_packs = NULL_TREE;
3076 bool for_types = false;
3077 struct find_parameter_pack_data ppd;
3079 if (!arg || arg == error_mark_node)
3082 if (TREE_CODE (arg) == TREE_LIST)
3084 /* The only time we will see a TREE_LIST here is for a base
3085 class initializer. In this case, the TREE_PURPOSE will be a
3086 _TYPE node (representing the base class expansion we're
3087 initializing) and the TREE_VALUE will be a TREE_LIST
3088 containing the initialization arguments.
3090 The resulting expansion looks somewhat different from most
3091 expansions. Rather than returning just one _EXPANSION, we
3092 return a TREE_LIST whose TREE_PURPOSE is a
3093 TYPE_PACK_EXPANSION containing the bases that will be
3094 initialized. The TREE_VALUE will be identical to the
3095 original TREE_VALUE, which is a list of arguments that will
3096 be passed to each base. We do not introduce any new pack
3097 expansion nodes into the TREE_VALUE (although it is possible
3098 that some already exist), because the TREE_PURPOSE and
3099 TREE_VALUE all need to be expanded together with the same
3100 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3101 resulting TREE_PURPOSE will mention the parameter packs in
3102 both the bases and the arguments to the bases. */
3105 tree parameter_packs = NULL_TREE;
3107 /* Determine which parameter packs will be used by the base
3109 ppd.visited = pointer_set_create ();
3110 ppd.parameter_packs = ¶meter_packs;
3111 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3114 if (parameter_packs == NULL_TREE)
3116 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3117 pointer_set_destroy (ppd.visited);
3118 return error_mark_node;
3121 if (TREE_VALUE (arg) != void_type_node)
3123 /* Collect the sets of parameter packs used in each of the
3124 initialization arguments. */
3125 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3127 /* Determine which parameter packs will be expanded in this
3129 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3134 pointer_set_destroy (ppd.visited);
3136 /* Create the pack expansion type for the base type. */
3137 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3138 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3139 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3141 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3142 they will rarely be compared to anything. */
3143 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3145 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3148 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3151 /* Build the PACK_EXPANSION_* node. */
3153 ? cxx_make_type (TYPE_PACK_EXPANSION)
3154 : make_node (EXPR_PACK_EXPANSION);
3155 SET_PACK_EXPANSION_PATTERN (result, arg);
3156 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3158 /* Propagate type and const-expression information. */
3159 TREE_TYPE (result) = TREE_TYPE (arg);
3160 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3163 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3164 they will rarely be compared to anything. */
3165 SET_TYPE_STRUCTURAL_EQUALITY (result);
3167 /* Determine which parameter packs will be expanded. */
3168 ppd.parameter_packs = ¶meter_packs;
3169 ppd.visited = pointer_set_create ();
3170 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3171 pointer_set_destroy (ppd.visited);
3173 /* Make sure we found some parameter packs. */
3174 if (parameter_packs == NULL_TREE)
3177 error ("expansion pattern %<%T%> contains no argument packs", arg);
3179 error ("expansion pattern %<%E%> contains no argument packs", arg);
3180 return error_mark_node;
3182 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3187 /* Checks T for any "bare" parameter packs, which have not yet been
3188 expanded, and issues an error if any are found. This operation can
3189 only be done on full expressions or types (e.g., an expression
3190 statement, "if" condition, etc.), because we could have expressions like:
3192 foo(f(g(h(args)))...)
3194 where "args" is a parameter pack. check_for_bare_parameter_packs
3195 should not be called for the subexpressions args, h(args),
3196 g(h(args)), or f(g(h(args))), because we would produce erroneous
3199 Returns TRUE and emits an error if there were bare parameter packs,
3200 returns FALSE otherwise. */
3202 check_for_bare_parameter_packs (tree t)
3204 tree parameter_packs = NULL_TREE;
3205 struct find_parameter_pack_data ppd;
3207 if (!processing_template_decl || !t || t == error_mark_node)
3210 if (TREE_CODE (t) == TYPE_DECL)
3213 ppd.parameter_packs = ¶meter_packs;
3214 ppd.visited = pointer_set_create ();
3215 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3216 pointer_set_destroy (ppd.visited);
3218 if (parameter_packs)
3220 error ("parameter packs not expanded with %<...%>:");
3221 while (parameter_packs)
3223 tree pack = TREE_VALUE (parameter_packs);
3224 tree name = NULL_TREE;
3226 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3227 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3228 name = TYPE_NAME (pack);
3229 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3230 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3232 name = DECL_NAME (pack);
3235 inform (input_location, " %qD", name);
3237 inform (input_location, " <anonymous>");
3239 parameter_packs = TREE_CHAIN (parameter_packs);
3248 /* Expand any parameter packs that occur in the template arguments in
3251 expand_template_argument_pack (tree args)
3253 tree result_args = NULL_TREE;
3254 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3255 int num_result_args = -1;
3256 int non_default_args_count = -1;
3258 /* First, determine if we need to expand anything, and the number of
3259 slots we'll need. */
3260 for (in_arg = 0; in_arg < nargs; ++in_arg)
3262 tree arg = TREE_VEC_ELT (args, in_arg);
3263 if (arg == NULL_TREE)
3265 if (ARGUMENT_PACK_P (arg))
3267 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3268 if (num_result_args < 0)
3269 num_result_args = in_arg + num_packed;
3271 num_result_args += num_packed;
3275 if (num_result_args >= 0)
3280 /* If no expansion is necessary, we're done. */
3281 if (num_result_args < 0)
3284 /* Expand arguments. */
3285 result_args = make_tree_vec (num_result_args);
3286 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3287 non_default_args_count =
3288 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3289 for (in_arg = 0; in_arg < nargs; ++in_arg)
3291 tree arg = TREE_VEC_ELT (args, in_arg);
3292 if (ARGUMENT_PACK_P (arg))
3294 tree packed = ARGUMENT_PACK_ARGS (arg);
3295 int i, num_packed = TREE_VEC_LENGTH (packed);
3296 for (i = 0; i < num_packed; ++i, ++out_arg)
3297 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3298 if (non_default_args_count > 0)
3299 non_default_args_count += num_packed;
3303 TREE_VEC_ELT (result_args, out_arg) = arg;
3307 if (non_default_args_count >= 0)
3308 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3312 /* Checks if DECL shadows a template parameter.
3314 [temp.local]: A template-parameter shall not be redeclared within its
3315 scope (including nested scopes).
3317 Emits an error and returns TRUE if the DECL shadows a parameter,
3318 returns FALSE otherwise. */
3321 check_template_shadow (tree decl)
3325 /* If we're not in a template, we can't possibly shadow a template
3327 if (!current_template_parms)
3330 /* Figure out what we're shadowing. */
3331 if (TREE_CODE (decl) == OVERLOAD)
3332 decl = OVL_CURRENT (decl);
3333 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3335 /* If there's no previous binding for this name, we're not shadowing
3336 anything, let alone a template parameter. */
3340 /* If we're not shadowing a template parameter, we're done. Note
3341 that OLDDECL might be an OVERLOAD (or perhaps even an
3342 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3344 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3347 /* We check for decl != olddecl to avoid bogus errors for using a
3348 name inside a class. We check TPFI to avoid duplicate errors for
3349 inline member templates. */
3351 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3354 error ("declaration of %q+#D", decl);
3355 error (" shadows template parm %q+#D", olddecl);
3359 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3360 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3361 template parameters. */
3364 build_template_parm_index (int index,
3371 tree t = make_node (TEMPLATE_PARM_INDEX);
3372 TEMPLATE_PARM_IDX (t) = index;
3373 TEMPLATE_PARM_LEVEL (t) = level;
3374 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3375 TEMPLATE_PARM_NUM_SIBLINGS (t) = num_siblings;
3376 TEMPLATE_PARM_DECL (t) = decl;
3377 TREE_TYPE (t) = type;
3378 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3379 TREE_READONLY (t) = TREE_READONLY (decl);
3384 /* Find the canonical type parameter for the given template type
3385 parameter. Returns the canonical type parameter, which may be TYPE
3386 if no such parameter existed. */
3389 canonical_type_parameter (tree type)
3392 int idx = TEMPLATE_TYPE_IDX (type);
3393 if (!canonical_template_parms)
3394 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3396 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3397 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3399 list = VEC_index (tree, canonical_template_parms, idx);
3400 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3401 list = TREE_CHAIN (list);
3404 return TREE_VALUE (list);
3407 VEC_replace(tree, canonical_template_parms, idx,
3408 tree_cons (NULL_TREE, type,
3409 VEC_index (tree, canonical_template_parms, idx)));
3414 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3415 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3416 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3417 new one is created. */
3420 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3421 tsubst_flags_t complain)
3423 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3424 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3425 != TEMPLATE_PARM_LEVEL (index) - levels)
3426 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
3428 tree orig_decl = TEMPLATE_PARM_DECL (index);
3431 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3432 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3433 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3434 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3435 DECL_ARTIFICIAL (decl) = 1;
3436 SET_DECL_TEMPLATE_PARM_P (decl);
3438 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3439 TEMPLATE_PARM_LEVEL (index) - levels,
3440 TEMPLATE_PARM_ORIG_LEVEL (index),
3441 TEMPLATE_PARM_NUM_SIBLINGS (index),
3443 TEMPLATE_PARM_DESCENDANTS (index) = t;
3444 TEMPLATE_PARM_PARAMETER_PACK (t)
3445 = TEMPLATE_PARM_PARAMETER_PACK (index);
3447 /* Template template parameters need this. */
3448 if (TREE_CODE (decl) == TEMPLATE_DECL)
3449 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3450 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3454 return TEMPLATE_PARM_DESCENDANTS (index);
3457 /* Process information from new template parameter PARM and append it
3458 to the LIST being built. This new parameter is a non-type
3459 parameter iff IS_NON_TYPE is true. This new parameter is a
3460 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3461 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3462 parameter list PARM belongs to. This is used used to create a
3463 proper canonical type for the type of PARM that is to be created,
3464 iff PARM is a type. If the size is not known, this parameter shall
3468 process_template_parm (tree list, location_t parm_loc, tree parm,
3469 bool is_non_type, bool is_parameter_pack,
3470 unsigned num_template_parms)
3477 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3478 defval = TREE_PURPOSE (parm);
3482 tree p = tree_last (list);
3484 if (p && TREE_VALUE (p) != error_mark_node)
3487 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3488 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3490 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3500 parm = TREE_VALUE (parm);
3502 SET_DECL_TEMPLATE_PARM_P (parm);
3504 if (TREE_TYPE (parm) == error_mark_node)
3506 err_parm_list = build_tree_list (defval, parm);
3507 TREE_VALUE (err_parm_list) = error_mark_node;
3508 return chainon (list, err_parm_list);
3514 The top-level cv-qualifiers on the template-parameter are
3515 ignored when determining its type. */
3516 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3517 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3519 err_parm_list = build_tree_list (defval, parm);
3520 TREE_VALUE (err_parm_list) = error_mark_node;
3521 return chainon (list, err_parm_list);
3524 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3526 /* This template parameter is not a parameter pack, but it
3527 should be. Complain about "bare" parameter packs. */
3528 check_for_bare_parameter_packs (TREE_TYPE (parm));
3530 /* Recover by calling this a parameter pack. */
3531 is_parameter_pack = true;
3535 /* A template parameter is not modifiable. */
3536 TREE_CONSTANT (parm) = 1;
3537 TREE_READONLY (parm) = 1;
3538 decl = build_decl (parm_loc,
3539 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3540 TREE_CONSTANT (decl) = 1;
3541 TREE_READONLY (decl) = 1;
3542 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3543 = build_template_parm_index (idx, processing_template_decl,
3544 processing_template_decl,
3546 decl, TREE_TYPE (parm));
3548 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3549 = is_parameter_pack;
3554 parm = TREE_VALUE (TREE_VALUE (parm));
3556 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3558 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3559 /* This is for distinguishing between real templates and template
3560 template parameters */
3561 TREE_TYPE (parm) = t;
3562 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3567 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3568 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3569 decl = build_decl (parm_loc,
3570 TYPE_DECL, parm, t);
3573 TYPE_NAME (t) = decl;
3574 TYPE_STUB_DECL (t) = decl;
3576 TEMPLATE_TYPE_PARM_INDEX (t)
3577 = build_template_parm_index (idx, processing_template_decl,
3578 processing_template_decl,
3580 decl, TREE_TYPE (parm));
3581 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3582 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3584 DECL_ARTIFICIAL (decl) = 1;
3585 SET_DECL_TEMPLATE_PARM_P (decl);
3587 parm = build_tree_list (defval, parm);
3588 return chainon (list, parm);
3591 /* The end of a template parameter list has been reached. Process the
3592 tree list into a parameter vector, converting each parameter into a more
3593 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3597 end_template_parm_list (tree parms)
3601 tree saved_parmlist = make_tree_vec (list_length (parms));
3603 current_template_parms
3604 = tree_cons (size_int (processing_template_decl),
3605 saved_parmlist, current_template_parms);
3607 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3609 next = TREE_CHAIN (parm);
3610 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3611 TREE_CHAIN (parm) = NULL_TREE;
3614 --processing_template_parmlist;
3616 return saved_parmlist;
3619 /* Create a new type almost identical to TYPE but which has the
3620 following differences:
3622 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3623 template sibling parameters of T.
3625 2/ T has a new canonical type that matches the new number
3628 3/ From now on, T is going to be what lookups referring to the
3629 name of TYPE will return. No lookup should return TYPE anymore.
3631 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3633 This is a subroutine of fixup_template_parms. */
3636 fixup_template_type_parm_type (tree type, int num_parms)
3638 tree orig_idx = TEMPLATE_TYPE_PARM_INDEX (type), idx;
3640 /* This is the decl which name is inserted into the symbol table for
3641 the template parm type. So whenever we lookup the type name, this
3642 is the DECL we get. */
3645 /* Do not fix up the type twice. */
3646 if (orig_idx && TEMPLATE_PARM_NUM_SIBLINGS (orig_idx) != 0)
3649 t = copy_type (type);
3650 decl = TYPE_NAME (t);
3652 TYPE_MAIN_VARIANT (t) = t;
3653 TYPE_NEXT_VARIANT (t)= NULL_TREE;
3654 TYPE_POINTER_TO (t) = 0;
3655 TYPE_REFERENCE_TO (t) = 0;
3657 idx = build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx),
3658 TEMPLATE_PARM_LEVEL (orig_idx),
3659 TEMPLATE_PARM_ORIG_LEVEL (orig_idx),
3662 TEMPLATE_PARM_DESCENDANTS (idx) = TEMPLATE_PARM_DESCENDANTS (orig_idx);
3663 TEMPLATE_PARM_PARAMETER_PACK (idx) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx);
3664 TEMPLATE_TYPE_PARM_INDEX (t) = idx;
3666 TYPE_STUB_DECL (t) = decl;
3667 TEMPLATE_TYPE_DECL (t) = decl;
3668 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
3669 TREE_TYPE (DECL_TEMPLATE_RESULT (decl)) = t;
3671 /* Update the type associated to the type name stored in the symbol
3672 table. Now, whenever the type name is looked up, the resulting
3673 type is properly fixed up. */
3674 TREE_TYPE (decl) = t;
3676 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3681 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3682 identical to I, but that is fixed up as to:
3684 1/ carry the number of sibling parms (NUM_PARMS) of the template
3685 parm represented by I.
3687 2/ replace all references to template parm types declared before I
3688 (in the same template parm list as I) by references to template
3689 parm types contained in ARGS. ARGS should contain the list of
3690 template parms that have been fixed up so far, in a form suitable
3691 to be passed to tsubst.
3693 This is a subroutine of fixup_template_parms. */
3696 fixup_template_parm_index (tree i, tree args, int num_parms)
3698 tree index, decl, type;
3701 || TREE_CODE (i) != TEMPLATE_PARM_INDEX
3702 /* Do not fix up the index twice. */
3703 || (TEMPLATE_PARM_NUM_SIBLINGS (i) != 0))
3706 decl = TEMPLATE_PARM_DECL (i);
3707 type = TREE_TYPE (decl);
3709 index = build_template_parm_index (TEMPLATE_PARM_IDX (i),
3710 TEMPLATE_PARM_LEVEL (i),
3711 TEMPLATE_PARM_ORIG_LEVEL (i),
3715 TEMPLATE_PARM_DESCENDANTS (index) = TEMPLATE_PARM_DESCENDANTS (i);
3716 TEMPLATE_PARM_PARAMETER_PACK (index) = TEMPLATE_PARM_PARAMETER_PACK (i);
3718 type = tsubst (type, args, tf_none, NULL_TREE);
3720 TREE_TYPE (decl) = type;
3721 TREE_TYPE (index) = type;
3727 This is a subroutine of fixup_template_parms.
3729 It computes the canonical type of the type of the template
3730 parameter PARM_DESC and update all references to that type so that
3731 they use the newly computed canonical type. No access check is
3732 performed during the fixup. PARM_DESC is a TREE_LIST which
3733 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3734 default argument of the template parm if any. IDX is the index of
3735 the template parameter, starting at 0. NUM_PARMS is the number of
3736 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3737 TREE_VEC containing the full set of template parameters in a form
3738 suitable to be passed to substs functions as their ARGS
3739 argument. This is what current_template_args returns for a given
3740 template. The innermost vector of args in ARGLIST is the set of
3741 template parms that have been fixed up so far. This function adds
3742 the fixed up parameter into that vector. */
3745 fixup_template_parm (tree parm_desc,
3750 tree parm = TREE_VALUE (parm_desc);
3751 tree fixedup_args = INNERMOST_TEMPLATE_ARGS (arglist);
3753 push_deferring_access_checks (dk_no_check);
3755 if (TREE_CODE (parm) == TYPE_DECL)
3757 /* PARM is a template type parameter. Fix up its type, add
3758 the fixed-up template parm to the vector of fixed-up
3759 template parms so far, and substitute the fixed-up
3760 template parms into the default argument of this
3763 fixup_template_type_parm_type (TREE_TYPE (parm), num_parms);
3764 TREE_TYPE (parm) = t;
3766 TREE_VEC_ELT (fixedup_args, idx) = template_parm_to_arg (parm_desc);
3768 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3770 /* PARM is a template template parameter. This is going to
3772 tree tparms, targs, innermost_args, t;
3775 /* First, fix up the parms of the template template parm
3776 because the parms are involved in defining the new canonical
3777 type of the template template parm. */
3779 /* So we need to substitute the template parm types that have
3780 been fixed up so far into the template parms of this template
3781 template parm. E.g, consider this:
3783 template<class T, template<T u> class TT> class S;
3785 In this case we want to substitute T into the
3786 template parameters of TT.
3788 So let's walk the template parms of PARM here, and
3789 tsubst ARGLIST into into each of the template
3792 /* For this substitution we need to build the full set of
3793 template parameters and use that as arguments for the
3794 tsubsting function. */
3795 tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
3797 /* This will contain the innermost parms of PARM into which
3798 we have substituted so far. */
3799 innermost_args = make_tree_vec (TREE_VEC_LENGTH (tparms));
3800 targs = add_to_template_args (arglist, innermost_args);
3801 for (j = 0; j < TREE_VEC_LENGTH (tparms); ++j)
3805 parameter = TREE_VEC_ELT (tparms, j);
3807 /* INNERMOST_ARGS needs to have at least the same number
3808 of elements as the index PARAMETER, ortherwise
3809 tsubsting into PARAMETER will result in partially
3810 instantiating it, reducing its tempate parm
3811 level. Let's tactically fill INNERMOST_ARGS for that
3813 TREE_VEC_ELT (innermost_args, j) =
3814 template_parm_to_arg (parameter);
3816 fixup_template_parm (parameter, j,
3817 TREE_VEC_LENGTH (tparms),
3821 /* Now fix up the type of the template template parm. */
3823 t = fixup_template_type_parm_type (TREE_TYPE (parm), num_parms);
3824 TREE_TYPE (parm) = t;
3826 TREE_VEC_ELT (fixedup_args, idx) =
3827 template_parm_to_arg (parm_desc);
3829 else if (TREE_CODE (parm) == PARM_DECL)
3831 /* PARM is a non-type template parameter. We need to:
3833 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3834 proper number of sibling parameters.
3836 * Make lookups of the template parameter return a reference
3837 to the fixed-up index. No lookup should return references
3838 to the former index anymore.
3840 * Substitute the template parms that got fixed up so far
3842 * into the type of PARM. */
3844 tree index = DECL_INITIAL (parm);
3846 /* PUSHED_DECL is the decl added to the symbol table with
3847 the name of the parameter. E,g:
3849 template<class T, T u> //#0
3850 auto my_function(T t) -> decltype(u); //#1
3852 Here, when looking up u at //#1, we get the decl of u
3853 resulting from the declaration in #0. This is what
3854 PUSHED_DECL is. We need to replace the reference to the
3855 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3856 fixed-up TEMPLATE_PARM_INDEX. */
3857 tree pushed_decl = TEMPLATE_PARM_DECL (index);
3859 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3860 fixup the type of PUSHED_DECL as well and luckily
3861 fixup_template_parm_index does it for us too. */
3862 tree fixed_up_index =
3863 fixup_template_parm_index (index, arglist, num_parms);
3865 DECL_INITIAL (pushed_decl) = DECL_INITIAL (parm) = fixed_up_index;
3867 /* Add this fixed up PARM to the template parms we've fixed
3868 up so far and use that to substitute the fixed-up
3869 template parms into the type of PARM. */
3870 TREE_VEC_ELT (fixedup_args, idx) =
3871 template_parm_to_arg (parm_desc);
3872 TREE_TYPE (parm) = tsubst (TREE_TYPE (parm), arglist,
3873 tf_none, NULL_TREE);
3876 TREE_PURPOSE (parm_desc) =
3877 tsubst_template_arg (TREE_PURPOSE (parm_desc),
3878 arglist, tf_none, parm);
3880 pop_deferring_access_checks ();
3883 /* Walk the current template parms and properly compute the canonical
3884 types of the dependent types created during
3885 cp_parser_template_parameter_list. */
3888 fixup_template_parms (void)
3895 parameter_vec = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3896 if (parameter_vec == NULL_TREE)
3899 num_parms = TREE_VEC_LENGTH (parameter_vec);
3901 /* This vector contains the current innermost template parms that
3902 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3903 to be passed to tsubst* functions as their ARGS argument. */
3904 fixedup_args = make_tree_vec (num_parms);
3906 /* This vector contains the full set of template parms in a form
3907 suitable to be passed to substs functions as their ARGS
3909 arglist = current_template_args ();
3910 arglist = add_outermost_template_args (arglist, fixedup_args);
3912 /* Let's do the proper fixup now. */
3913 for (i = 0; i < num_parms; ++i)
3914 fixup_template_parm (TREE_VEC_ELT (parameter_vec, i),
3915 i, num_parms, arglist);
3918 /* end_template_decl is called after a template declaration is seen. */
3921 end_template_decl (void)
3923 reset_specialization ();
3925 if (! processing_template_decl)
3928 /* This matches the pushlevel in begin_template_parm_list. */
3931 --processing_template_decl;
3932 current_template_parms = TREE_CHAIN (current_template_parms);
3935 /* Takes a TREE_LIST representing a template parameter and convert it
3936 into an argument suitable to be passed to the type substitution
3937 functions. Note that If the TREE_LIST contains an error_mark
3938 node, the returned argument is error_mark_node. */
3941 template_parm_to_arg (tree t)
3945 || TREE_CODE (t) != TREE_LIST)
3948 if (error_operand_p (TREE_VALUE (t)))
3949 return error_mark_node;
3953 if (TREE_CODE (t) == TYPE_DECL
3954 || TREE_CODE (t) == TEMPLATE_DECL)
3958 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3960 /* Turn this argument into a TYPE_ARGUMENT_PACK
3961 with a single element, which expands T. */
3962 tree vec = make_tree_vec (1);
3963 #ifdef ENABLE_CHECKING
3964 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3965 (vec, TREE_VEC_LENGTH (vec));
3967 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3969 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3970 SET_ARGUMENT_PACK_ARGS (t, vec);
3975 t = DECL_INITIAL (t);
3977 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3979 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3980 with a single element, which expands T. */
3981 tree vec = make_tree_vec (1);
3982 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3983 #ifdef ENABLE_CHECKING
3984 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3985 (vec, TREE_VEC_LENGTH (vec));
3987 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3989 t = make_node (NONTYPE_ARGUMENT_PACK);
3990 SET_ARGUMENT_PACK_ARGS (t, vec);
3991 TREE_TYPE (t) = type;
3997 /* Within the declaration of a template, return all levels of template
3998 parameters that apply. The template parameters are represented as
3999 a TREE_VEC, in the form documented in cp-tree.h for template
4003 current_template_args (void)
4006 tree args = NULL_TREE;
4007 int length = TMPL_PARMS_DEPTH (current_template_parms);
4010 /* If there is only one level of template parameters, we do not
4011 create a TREE_VEC of TREE_VECs. Instead, we return a single
4012 TREE_VEC containing the arguments. */
4014 args = make_tree_vec (length);
4016 for (header = current_template_parms; header; header = TREE_CHAIN (header))
4018 tree a = copy_node (TREE_VALUE (header));
4021 TREE_TYPE (a) = NULL_TREE;
4022 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4023 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4025 #ifdef ENABLE_CHECKING
4026 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4030 TREE_VEC_ELT (args, --l) = a;
4035 if (length > 1 && TREE_VEC_ELT (args, 0) == NULL_TREE)
4036 /* This can happen for template parms of a template template
4039 template<template<class T, class U> class TT> struct S;
4041 Consider the level of the parms of TT; T and U both have
4042 level 2; TT has no template parm of level 1. So in this case
4043 the first element of full_template_args is NULL_TREE. If we
4044 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4045 of 2. This will make tsubst wrongly consider that T and U
4046 have level 1. Instead, let's create a dummy vector as the
4047 first element of full_template_args so that TMPL_ARG_DEPTH
4048 returns the correct depth for args. */
4049 TREE_VEC_ELT (args, 0) = make_tree_vec (1);
4053 /* Update the declared TYPE by doing any lookups which were thought to be
4054 dependent, but are not now that we know the SCOPE of the declarator. */
4057 maybe_update_decl_type (tree orig_type, tree scope)
4059 tree type = orig_type;
4061 if (type == NULL_TREE)
4064 if (TREE_CODE (orig_type) == TYPE_DECL)
4065 type = TREE_TYPE (type);
4067 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4068 && dependent_type_p (type)
4069 /* Don't bother building up the args in this case. */
4070 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4072 /* tsubst in the args corresponding to the template parameters,
4073 including auto if present. Most things will be unchanged, but
4074 make_typename_type and tsubst_qualified_id will resolve
4075 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4076 tree args = current_template_args ();
4077 tree auto_node = type_uses_auto (type);
4081 tree auto_vec = make_tree_vec (1);
4082 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4083 args = add_to_template_args (args, auto_vec);
4085 pushed = push_scope (scope);
4086 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4091 if (type == error_mark_node)
4094 if (TREE_CODE (orig_type) == TYPE_DECL)
4096 if (same_type_p (type, TREE_TYPE (orig_type)))
4099 type = TYPE_NAME (type);
4104 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4105 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4106 a member template. Used by push_template_decl below. */
4109 build_template_decl (tree decl, tree parms, bool member_template_p)
4111 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4112 DECL_TEMPLATE_PARMS (tmpl) = parms;
4113 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4114 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4119 struct template_parm_data
4121 /* The level of the template parameters we are currently
4125 /* The index of the specialization argument we are currently
4129 /* An array whose size is the number of template parameters. The
4130 elements are nonzero if the parameter has been used in any one
4131 of the arguments processed so far. */
4134 /* An array whose size is the number of template arguments. The
4135 elements are nonzero if the argument makes use of template
4136 parameters of this level. */
4137 int* arg_uses_template_parms;
4140 /* Subroutine of push_template_decl used to see if each template
4141 parameter in a partial specialization is used in the explicit
4142 argument list. If T is of the LEVEL given in DATA (which is
4143 treated as a template_parm_data*), then DATA->PARMS is marked
4147 mark_template_parm (tree t, void* data)
4151 struct template_parm_data* tpd = (struct template_parm_data*) data;
4153 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4155 level = TEMPLATE_PARM_LEVEL (t);
4156 idx = TEMPLATE_PARM_IDX (t);
4160 level = TEMPLATE_TYPE_LEVEL (t);
4161 idx = TEMPLATE_TYPE_IDX (t);
4164 if (level == tpd->level)
4166 tpd->parms[idx] = 1;
4167 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4170 /* Return zero so that for_each_template_parm will continue the
4171 traversal of the tree; we want to mark *every* template parm. */
4175 /* Process the partial specialization DECL. */
4178 process_partial_specialization (tree decl)
4180 tree type = TREE_TYPE (decl);
4181 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
4182 tree specargs = CLASSTYPE_TI_ARGS (type);
4183 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4184 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4187 int nargs = TREE_VEC_LENGTH (inner_args);
4190 bool did_error_intro = false;
4191 struct template_parm_data tpd;
4192 struct template_parm_data tpd2;
4194 gcc_assert (current_template_parms);
4196 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4197 ntparms = TREE_VEC_LENGTH (inner_parms);
4199 /* We check that each of the template parameters given in the
4200 partial specialization is used in the argument list to the
4201 specialization. For example:
4203 template <class T> struct S;
4204 template <class T> struct S<T*>;
4206 The second declaration is OK because `T*' uses the template
4207 parameter T, whereas
4209 template <class T> struct S<int>;
4211 is no good. Even trickier is:
4222 The S2<T> declaration is actually invalid; it is a
4223 full-specialization. Of course,
4226 struct S2<T (*)(U)>;
4228 or some such would have been OK. */
4229 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4230 tpd.parms = XALLOCAVEC (int, ntparms);
4231 memset (tpd.parms, 0, sizeof (int) * ntparms);
4233 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4234 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4235 for (i = 0; i < nargs; ++i)
4237 tpd.current_arg = i;
4238 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4239 &mark_template_parm,
4242 /*include_nondeduced_p=*/false);
4244 for (i = 0; i < ntparms; ++i)
4245 if (tpd.parms[i] == 0)
4247 /* One of the template parms was not used in the
4249 if (!did_error_intro)
4251 error ("template parameters not used in partial specialization:");
4252 did_error_intro = true;
4255 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4258 if (did_error_intro)
4259 return error_mark_node;
4261 /* [temp.class.spec]
4263 The argument list of the specialization shall not be identical to
4264 the implicit argument list of the primary template. */
4265 if (comp_template_args
4267 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4269 error ("partial specialization %qT does not specialize any template arguments", type);
4271 /* [temp.class.spec]
4273 A partially specialized non-type argument expression shall not
4274 involve template parameters of the partial specialization except
4275 when the argument expression is a simple identifier.
4277 The type of a template parameter corresponding to a specialized
4278 non-type argument shall not be dependent on a parameter of the
4281 Also, we verify that pack expansions only occur at the
4282 end of the argument list. */
4283 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4285 for (i = 0; i < nargs; ++i)
4287 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4288 tree arg = TREE_VEC_ELT (inner_args, i);
4289 tree packed_args = NULL_TREE;
4292 if (ARGUMENT_PACK_P (arg))
4294 /* Extract the arguments from the argument pack. We'll be
4295 iterating over these in the following loop. */
4296 packed_args = ARGUMENT_PACK_ARGS (arg);
4297 len = TREE_VEC_LENGTH (packed_args);
4300 for (j = 0; j < len; j++)
4303 /* Get the Jth argument in the parameter pack. */
4304 arg = TREE_VEC_ELT (packed_args, j);
4306 if (PACK_EXPANSION_P (arg))
4308 /* Pack expansions must come at the end of the
4310 if ((packed_args && j < len - 1)
4311 || (!packed_args && i < nargs - 1))
4313 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4314 error ("parameter pack argument %qE must be at the "
4315 "end of the template argument list", arg);
4317 error ("parameter pack argument %qT must be at the "
4318 "end of the template argument list", arg);
4322 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4323 /* We only care about the pattern. */
4324 arg = PACK_EXPANSION_PATTERN (arg);
4326 if (/* These first two lines are the `non-type' bit. */
4328 && TREE_CODE (arg) != TEMPLATE_DECL
4329 /* This next line is the `argument expression is not just a
4330 simple identifier' condition and also the `specialized
4331 non-type argument' bit. */
4332 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
4334 if ((!packed_args && tpd.arg_uses_template_parms[i])
4335 || (packed_args && uses_template_parms (arg)))
4336 error ("template argument %qE involves template parameter(s)",
4340 /* Look at the corresponding template parameter,
4341 marking which template parameters its type depends
4343 tree type = TREE_TYPE (parm);
4347 /* We haven't yet initialized TPD2. Do so now. */
4348 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4349 /* The number of parameters here is the number in the
4350 main template, which, as checked in the assertion
4352 tpd2.parms = XALLOCAVEC (int, nargs);
4354 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4357 /* Mark the template parameters. But this time, we're
4358 looking for the template parameters of the main
4359 template, not in the specialization. */
4360 tpd2.current_arg = i;
4361 tpd2.arg_uses_template_parms[i] = 0;
4362 memset (tpd2.parms, 0, sizeof (int) * nargs);
4363 for_each_template_parm (type,
4364 &mark_template_parm,
4367 /*include_nondeduced_p=*/false);
4369 if (tpd2.arg_uses_template_parms [i])
4371 /* The type depended on some template parameters.
4372 If they are fully specialized in the
4373 specialization, that's OK. */
4376 for (j = 0; j < nargs; ++j)
4377 if (tpd2.parms[j] != 0
4378 && tpd.arg_uses_template_parms [j])
4381 error_n (input_location, count,
4382 "type %qT of template argument %qE depends "
4383 "on a template parameter",
4384 "type %qT of template argument %qE depends "
4385 "on template parameters",
4394 /* We should only get here once. */
4395 gcc_assert (!COMPLETE_TYPE_P (type));
4397 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4398 = tree_cons (specargs, inner_parms,
4399 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4400 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4402 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4403 inst = TREE_CHAIN (inst))
4405 tree inst_type = TREE_VALUE (inst);
4406 if (COMPLETE_TYPE_P (inst_type)
4407 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
4409 tree spec = most_specialized_class (inst_type, maintmpl, tf_none);
4410 if (spec && TREE_TYPE (spec) == type)
4411 permerror (input_location,
4412 "partial specialization of %qT after instantiation "
4413 "of %qT", type, inst_type);
4420 /* Check that a template declaration's use of default arguments and
4421 parameter packs is not invalid. Here, PARMS are the template
4422 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4423 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4427 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4428 declaration (but not a definition); 1 indicates a declaration, 2
4429 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4430 emitted for extraneous default arguments.
4432 Returns TRUE if there were no errors found, FALSE otherwise. */
4435 check_default_tmpl_args (tree decl, tree parms, int is_primary,
4436 int is_partial, int is_friend_decl)
4439 int last_level_to_check;
4441 bool no_errors = true;
4445 A default template-argument shall not be specified in a
4446 function template declaration or a function template definition, nor
4447 in the template-parameter-list of the definition of a member of a
4450 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
4451 /* You can't have a function template declaration in a local
4452 scope, nor you can you define a member of a class template in a
4456 if (current_class_type
4457 && !TYPE_BEING_DEFINED (current_class_type)
4458 && DECL_LANG_SPECIFIC (decl)
4459 && DECL_DECLARES_FUNCTION_P (decl)
4460 /* If this is either a friend defined in the scope of the class
4461 or a member function. */
4462 && (DECL_FUNCTION_MEMBER_P (decl)
4463 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4464 : DECL_FRIEND_CONTEXT (decl)
4465 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4467 /* And, if it was a member function, it really was defined in
4468 the scope of the class. */
4469 && (!DECL_FUNCTION_MEMBER_P (decl)
4470 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4471 /* We already checked these parameters when the template was
4472 declared, so there's no need to do it again now. This function
4473 was defined in class scope, but we're processing it's body now
4474 that the class is complete. */
4477 /* Core issue 226 (C++0x only): the following only applies to class
4479 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
4483 If a template-parameter has a default template-argument, all
4484 subsequent template-parameters shall have a default
4485 template-argument supplied. */
4486 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4488 tree inner_parms = TREE_VALUE (parm_level);
4489 int ntparms = TREE_VEC_LENGTH (inner_parms);
4490 int seen_def_arg_p = 0;
4493 for (i = 0; i < ntparms; ++i)
4495 tree parm = TREE_VEC_ELT (inner_parms, i);
4497 if (parm == error_mark_node)
4500 if (TREE_PURPOSE (parm))
4502 else if (seen_def_arg_p
4503 && !template_parameter_pack_p (TREE_VALUE (parm)))
4505 error ("no default argument for %qD", TREE_VALUE (parm));
4506 /* For better subsequent error-recovery, we indicate that
4507 there should have been a default argument. */
4508 TREE_PURPOSE (parm) = error_mark_node;
4514 /* Don't complain about an enclosing partial
4516 && parm_level == parms
4517 && TREE_CODE (decl) == TYPE_DECL
4519 && template_parameter_pack_p (TREE_VALUE (parm)))
4521 /* A primary class template can only have one
4522 parameter pack, at the end of the template
4525 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
4526 error ("parameter pack %qE must be at the end of the"
4527 " template parameter list", TREE_VALUE (parm));
4529 error ("parameter pack %qT must be at the end of the"
4530 " template parameter list",
4531 TREE_TYPE (TREE_VALUE (parm)));
4533 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4541 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4545 /* For an ordinary class template, default template arguments are
4546 allowed at the innermost level, e.g.:
4547 template <class T = int>
4549 but, in a partial specialization, they're not allowed even
4550 there, as we have in [temp.class.spec]:
4552 The template parameter list of a specialization shall not
4553 contain default template argument values.
4555 So, for a partial specialization, or for a function template
4556 (in C++98/C++03), we look at all of them. */
4559 /* But, for a primary class template that is not a partial
4560 specialization we look at all template parameters except the
4562 parms = TREE_CHAIN (parms);
4564 /* Figure out what error message to issue. */
4565 if (is_friend_decl == 2)
4566 msg = G_("default template arguments may not be used in function template "
4567 "friend re-declaration");
4568 else if (is_friend_decl)
4569 msg = G_("default template arguments may not be used in function template "
4570 "friend declarations");
4571 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4572 msg = G_("default template arguments may not be used in function templates "
4573 "without -std=c++0x or -std=gnu++0x");
4574 else if (is_partial)
4575 msg = G_("default template arguments may not be used in "
4576 "partial specializations");
4578 msg = G_("default argument for template parameter for class enclosing %qD");
4580 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4581 /* If we're inside a class definition, there's no need to
4582 examine the parameters to the class itself. On the one
4583 hand, they will be checked when the class is defined, and,
4584 on the other, default arguments are valid in things like:
4585 template <class T = double>
4586 struct S { template <class U> void f(U); };
4587 Here the default argument for `S' has no bearing on the
4588 declaration of `f'. */
4589 last_level_to_check = template_class_depth (current_class_type) + 1;
4591 /* Check everything. */
4592 last_level_to_check = 0;
4594 for (parm_level = parms;
4595 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4596 parm_level = TREE_CHAIN (parm_level))
4598 tree inner_parms = TREE_VALUE (parm_level);
4602 ntparms = TREE_VEC_LENGTH (inner_parms);
4603 for (i = 0; i < ntparms; ++i)
4605 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4608 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4613 if (is_friend_decl == 2)
4620 /* Clear out the default argument so that we are not
4622 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4626 /* At this point, if we're still interested in issuing messages,
4627 they must apply to classes surrounding the object declared. */
4629 msg = G_("default argument for template parameter for class "
4636 /* Worker for push_template_decl_real, called via
4637 for_each_template_parm. DATA is really an int, indicating the
4638 level of the parameters we are interested in. If T is a template
4639 parameter of that level, return nonzero. */
4642 template_parm_this_level_p (tree t, void* data)
4644 int this_level = *(int *)data;
4647 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4648 level = TEMPLATE_PARM_LEVEL (t);
4650 level = TEMPLATE_TYPE_LEVEL (t);
4651 return level == this_level;
4654 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4655 parameters given by current_template_args, or reuses a
4656 previously existing one, if appropriate. Returns the DECL, or an
4657 equivalent one, if it is replaced via a call to duplicate_decls.
4659 If IS_FRIEND is true, DECL is a friend declaration. */
4662 push_template_decl_real (tree decl, bool is_friend)
4670 int new_template_p = 0;
4671 /* True if the template is a member template, in the sense of
4673 bool member_template_p = false;
4675 if (decl == error_mark_node || !current_template_parms)
4676 return error_mark_node;
4678 /* See if this is a partial specialization. */
4679 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4680 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4681 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4683 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4687 /* For a friend, we want the context of the friend function, not
4688 the type of which it is a friend. */
4689 ctx = CP_DECL_CONTEXT (decl);
4690 else if (CP_DECL_CONTEXT (decl)
4691 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4692 /* In the case of a virtual function, we want the class in which
4694 ctx = CP_DECL_CONTEXT (decl);
4696 /* Otherwise, if we're currently defining some class, the DECL
4697 is assumed to be a member of the class. */
4698 ctx = current_scope ();
4700 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4703 if (!DECL_CONTEXT (decl))
4704 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4706 /* See if this is a primary template. */
4707 if (is_friend && ctx)
4708 /* A friend template that specifies a class context, i.e.
4709 template <typename T> friend void A<T>::f();
4713 primary = template_parm_scope_p ();
4717 if (DECL_CLASS_SCOPE_P (decl))
4718 member_template_p = true;
4719 if (TREE_CODE (decl) == TYPE_DECL
4720 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4722 error ("template class without a name");
4723 return error_mark_node;
4725 else if (TREE_CODE (decl) == FUNCTION_DECL)
4727 if (DECL_DESTRUCTOR_P (decl))
4731 A destructor shall not be a member template. */
4732 error ("destructor %qD declared as member template", decl);
4733 return error_mark_node;
4735 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4736 && (!prototype_p (TREE_TYPE (decl))
4737 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4738 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4739 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4740 == void_list_node)))
4742 /* [basic.stc.dynamic.allocation]
4744 An allocation function can be a function
4745 template. ... Template allocation functions shall
4746 have two or more parameters. */
4747 error ("invalid template declaration of %qD", decl);
4748 return error_mark_node;
4751 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4752 && CLASS_TYPE_P (TREE_TYPE (decl)))
4756 error ("template declaration of %q#D", decl);
4757 return error_mark_node;
4761 /* Check to see that the rules regarding the use of default
4762 arguments are not being violated. */
4763 check_default_tmpl_args (decl, current_template_parms,
4764 primary, is_partial, /*is_friend_decl=*/0);
4766 /* Ensure that there are no parameter packs in the type of this
4767 declaration that have not been expanded. */
4768 if (TREE_CODE (decl) == FUNCTION_DECL)
4770 /* Check each of the arguments individually to see if there are
4771 any bare parameter packs. */
4772 tree type = TREE_TYPE (decl);
4773 tree arg = DECL_ARGUMENTS (decl);
4774 tree argtype = TYPE_ARG_TYPES (type);
4776 while (arg && argtype)
4778 if (!FUNCTION_PARAMETER_PACK_P (arg)
4779 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4781 /* This is a PARM_DECL that contains unexpanded parameter
4782 packs. We have already complained about this in the
4783 check_for_bare_parameter_packs call, so just replace
4784 these types with ERROR_MARK_NODE. */
4785 TREE_TYPE (arg) = error_mark_node;
4786 TREE_VALUE (argtype) = error_mark_node;
4789 arg = DECL_CHAIN (arg);
4790 argtype = TREE_CHAIN (argtype);
4793 /* Check for bare parameter packs in the return type and the
4794 exception specifiers. */
4795 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4796 /* Errors were already issued, set return type to int
4797 as the frontend doesn't expect error_mark_node as
4799 TREE_TYPE (type) = integer_type_node;
4800 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4801 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4803 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4805 TREE_TYPE (decl) = error_mark_node;
4806 return error_mark_node;
4810 return process_partial_specialization (decl);
4812 args = current_template_args ();
4815 || TREE_CODE (ctx) == FUNCTION_DECL
4816 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4817 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4819 if (DECL_LANG_SPECIFIC (decl)
4820 && DECL_TEMPLATE_INFO (decl)
4821 && DECL_TI_TEMPLATE (decl))
4822 tmpl = DECL_TI_TEMPLATE (decl);
4823 /* If DECL is a TYPE_DECL for a class-template, then there won't
4824 be DECL_LANG_SPECIFIC. The information equivalent to
4825 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4826 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4827 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4828 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4830 /* Since a template declaration already existed for this
4831 class-type, we must be redeclaring it here. Make sure
4832 that the redeclaration is valid. */
4833 redeclare_class_template (TREE_TYPE (decl),
4834 current_template_parms);
4835 /* We don't need to create a new TEMPLATE_DECL; just use the
4836 one we already had. */
4837 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4841 tmpl = build_template_decl (decl, current_template_parms,
4845 if (DECL_LANG_SPECIFIC (decl)
4846 && DECL_TEMPLATE_SPECIALIZATION (decl))
4848 /* A specialization of a member template of a template
4850 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4851 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4852 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4858 tree a, t, current, parms;
4860 tree tinfo = get_template_info (decl);
4864 error ("template definition of non-template %q#D", decl);
4865 return error_mark_node;
4868 tmpl = TI_TEMPLATE (tinfo);
4870 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4871 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4872 && DECL_TEMPLATE_SPECIALIZATION (decl)
4873 && DECL_MEMBER_TEMPLATE_P (tmpl))
4877 /* The declaration is a specialization of a member
4878 template, declared outside the class. Therefore, the
4879 innermost template arguments will be NULL, so we
4880 replace them with the arguments determined by the
4881 earlier call to check_explicit_specialization. */
4882 args = DECL_TI_ARGS (decl);
4885 = build_template_decl (decl, current_template_parms,
4887 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4888 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4889 DECL_TI_TEMPLATE (decl) = new_tmpl;
4890 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4891 DECL_TEMPLATE_INFO (new_tmpl)
4892 = build_template_info (tmpl, args);
4894 register_specialization (new_tmpl,
4895 most_general_template (tmpl),
4901 /* Make sure the template headers we got make sense. */
4903 parms = DECL_TEMPLATE_PARMS (tmpl);
4904 i = TMPL_PARMS_DEPTH (parms);
4905 if (TMPL_ARGS_DEPTH (args) != i)
4907 error ("expected %d levels of template parms for %q#D, got %d",
4908 i, decl, TMPL_ARGS_DEPTH (args));
4911 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4913 a = TMPL_ARGS_LEVEL (args, i);
4914 t = INNERMOST_TEMPLATE_PARMS (parms);
4916 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4918 if (current == decl)
4919 error ("got %d template parameters for %q#D",
4920 TREE_VEC_LENGTH (a), decl);
4922 error ("got %d template parameters for %q#T",
4923 TREE_VEC_LENGTH (a), current);
4924 error (" but %d required", TREE_VEC_LENGTH (t));
4925 return error_mark_node;
4928 if (current == decl)
4930 else if (current == NULL_TREE)
4931 /* Can happen in erroneous input. */
4934 current = (TYPE_P (current)
4935 ? TYPE_CONTEXT (current)
4936 : DECL_CONTEXT (current));
4939 /* Check that the parms are used in the appropriate qualifying scopes
4940 in the declarator. */
4941 if (!comp_template_args
4943 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4946 template arguments to %qD do not match original template %qD",
4947 decl, DECL_TEMPLATE_RESULT (tmpl));
4948 if (!uses_template_parms (TI_ARGS (tinfo)))
4949 inform (input_location, "use template<> for an explicit specialization");
4950 /* Avoid crash in import_export_decl. */
4951 DECL_INTERFACE_KNOWN (decl) = 1;
4952 return error_mark_node;
4956 DECL_TEMPLATE_RESULT (tmpl) = decl;
4957 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4959 /* Push template declarations for global functions and types. Note
4960 that we do not try to push a global template friend declared in a
4961 template class; such a thing may well depend on the template
4962 parameters of the class. */
4963 if (new_template_p && !ctx
4964 && !(is_friend && template_class_depth (current_class_type) > 0))
4966 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4967 if (tmpl == error_mark_node)
4968 return error_mark_node;
4970 /* Hide template friend classes that haven't been declared yet. */
4971 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4973 DECL_ANTICIPATED (tmpl) = 1;
4974 DECL_FRIEND_P (tmpl) = 1;
4980 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4983 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4984 if (DECL_CONV_FN_P (tmpl))
4986 int depth = TMPL_PARMS_DEPTH (parms);
4988 /* It is a conversion operator. See if the type converted to
4989 depends on innermost template operands. */
4991 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4993 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4996 /* Give template template parms a DECL_CONTEXT of the template
4997 for which they are a parameter. */
4998 parms = INNERMOST_TEMPLATE_PARMS (parms);
4999 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5001 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5002 if (TREE_CODE (parm) == TEMPLATE_DECL)
5003 DECL_CONTEXT (parm) = tmpl;
5007 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5008 back to its most general template. If TMPL is a specialization,
5009 ARGS may only have the innermost set of arguments. Add the missing
5010 argument levels if necessary. */
5011 if (DECL_TEMPLATE_INFO (tmpl))
5012 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5014 info = build_template_info (tmpl, args);
5016 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5017 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5018 else if (DECL_LANG_SPECIFIC (decl))
5019 DECL_TEMPLATE_INFO (decl) = info;
5021 return DECL_TEMPLATE_RESULT (tmpl);
5025 push_template_decl (tree decl)
5027 return push_template_decl_real (decl, false);
5030 /* Called when a class template TYPE is redeclared with the indicated
5031 template PARMS, e.g.:
5033 template <class T> struct S;
5034 template <class T> struct S {}; */
5037 redeclare_class_template (tree type, tree parms)
5043 if (!TYPE_TEMPLATE_INFO (type))
5045 error ("%qT is not a template type", type);
5049 tmpl = TYPE_TI_TEMPLATE (type);
5050 if (!PRIMARY_TEMPLATE_P (tmpl))
5051 /* The type is nested in some template class. Nothing to worry
5052 about here; there are no new template parameters for the nested
5058 error ("template specifiers not specified in declaration of %qD",
5063 parms = INNERMOST_TEMPLATE_PARMS (parms);
5064 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5066 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5068 error_n (input_location, TREE_VEC_LENGTH (parms),
5069 "redeclared with %d template parameter",
5070 "redeclared with %d template parameters",
5071 TREE_VEC_LENGTH (parms));
5072 inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
5073 "previous declaration %q+D used %d template parameter",
5074 "previous declaration %q+D used %d template parameters",
5075 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5079 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5086 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5087 || TREE_VEC_ELT (parms, i) == error_mark_node)
5090 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5091 if (tmpl_parm == error_mark_node)
5094 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5095 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5096 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5098 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5100 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5101 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5102 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5103 || (TREE_CODE (tmpl_parm) != PARM_DECL
5104 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5105 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5106 || (TREE_CODE (tmpl_parm) == PARM_DECL
5107 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5108 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5110 error ("template parameter %q+#D", tmpl_parm);
5111 error ("redeclared here as %q#D", parm);
5115 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5117 /* We have in [temp.param]:
5119 A template-parameter may not be given default arguments
5120 by two different declarations in the same scope. */
5121 error_at (input_location, "redefinition of default argument for %q#D", parm);
5122 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5123 "original definition appeared here");
5127 if (parm_default != NULL_TREE)
5128 /* Update the previous template parameters (which are the ones
5129 that will really count) with the new default value. */
5130 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5131 else if (tmpl_default != NULL_TREE)
5132 /* Update the new parameters, too; they'll be used as the
5133 parameters for any members. */
5134 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5140 /* Simplify EXPR if it is a non-dependent expression. Returns the
5141 (possibly simplified) expression. */
5144 fold_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5146 if (expr == NULL_TREE)
5149 /* If we're in a template, but EXPR isn't value dependent, simplify
5150 it. We're supposed to treat:
5152 template <typename T> void f(T[1 + 1]);
5153 template <typename T> void f(T[2]);
5155 as two declarations of the same function, for example. */
5156 if (processing_template_decl
5157 && !type_dependent_expression_p (expr)
5158 && potential_constant_expression (expr)
5159 && !value_dependent_expression_p (expr))
5161 HOST_WIDE_INT saved_processing_template_decl;
5163 saved_processing_template_decl = processing_template_decl;
5164 processing_template_decl = 0;
5165 expr = tsubst_copy_and_build (expr,
5168 /*in_decl=*/NULL_TREE,
5169 /*function_p=*/false,
5170 /*integral_constant_expression_p=*/true);
5171 processing_template_decl = saved_processing_template_decl;
5177 fold_non_dependent_expr (tree expr)
5179 return fold_non_dependent_expr_sfinae (expr, tf_error);
5182 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5183 must be a function or a pointer-to-function type, as specified
5184 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5185 and check that the resulting function has external linkage. */
5188 convert_nontype_argument_function (tree type, tree expr)
5193 fn = instantiate_type (type, fns, tf_none);
5194 if (fn == error_mark_node)
5195 return error_mark_node;
5198 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5199 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5200 if (TREE_CODE (fn_no_ptr) == BASELINK)
5201 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5203 /* [temp.arg.nontype]/1
5205 A template-argument for a non-type, non-template template-parameter
5208 -- the address of an object or function with external linkage. */
5209 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
5211 error ("%qE is not a valid template argument for type %qT "
5212 "because function %qD has not external linkage",
5213 expr, type, fn_no_ptr);
5220 /* Subroutine of convert_nontype_argument.
5221 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5222 Emit an error otherwise. */
5225 check_valid_ptrmem_cst_expr (tree type, tree expr,
5226 tsubst_flags_t complain)
5229 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
5231 if (complain & tf_error)
5233 error ("%qE is not a valid template argument for type %qT",
5235 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5240 /* Returns TRUE iff the address of OP is value-dependent.
5242 14.6.2.4 [temp.dep.temp]:
5243 A non-integral non-type template-argument is dependent if its type is
5244 dependent or it has either of the following forms
5247 and contains a nested-name-specifier which specifies a class-name that
5248 names a dependent type.
5250 We generalize this to just say that the address of a member of a
5251 dependent class is value-dependent; the above doesn't cover the
5252 address of a static data member named with an unqualified-id. */
5255 has_value_dependent_address (tree op)
5257 /* We could use get_inner_reference here, but there's no need;
5258 this is only relevant for template non-type arguments, which
5259 can only be expressed as &id-expression. */
5262 tree ctx = CP_DECL_CONTEXT (op);
5263 if (TYPE_P (ctx) && dependent_type_p (ctx))
5270 /* Attempt to convert the non-type template parameter EXPR to the
5271 indicated TYPE. If the conversion is successful, return the
5272 converted value. If the conversion is unsuccessful, return
5273 NULL_TREE if we issued an error message, or error_mark_node if we
5274 did not. We issue error messages for out-and-out bad template
5275 parameters, but not simply because the conversion failed, since we
5276 might be just trying to do argument deduction. Both TYPE and EXPR
5277 must be non-dependent.
5279 The conversion follows the special rules described in
5280 [temp.arg.nontype], and it is much more strict than an implicit
5283 This function is called twice for each template argument (see
5284 lookup_template_class for a more accurate description of this
5285 problem). This means that we need to handle expressions which
5286 are not valid in a C++ source, but can be created from the
5287 first call (for instance, casts to perform conversions). These
5288 hacks can go away after we fix the double coercion problem. */
5291 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
5295 /* Detect immediately string literals as invalid non-type argument.
5296 This special-case is not needed for correctness (we would easily
5297 catch this later), but only to provide better diagnostic for this
5298 common user mistake. As suggested by DR 100, we do not mention
5299 linkage issues in the diagnostic as this is not the point. */
5300 /* FIXME we're making this OK. */
5301 if (TREE_CODE (expr) == STRING_CST)
5303 if (complain & tf_error)
5304 error ("%qE is not a valid template argument for type %qT "
5305 "because string literals can never be used in this context",
5310 /* Add the ADDR_EXPR now for the benefit of
5311 value_dependent_expression_p. */
5312 if (TYPE_PTROBV_P (type)
5313 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
5314 expr = decay_conversion (expr);
5316 /* If we are in a template, EXPR may be non-dependent, but still
5317 have a syntactic, rather than semantic, form. For example, EXPR
5318 might be a SCOPE_REF, rather than the VAR_DECL to which the
5319 SCOPE_REF refers. Preserving the qualifying scope is necessary
5320 so that access checking can be performed when the template is
5321 instantiated -- but here we need the resolved form so that we can
5322 convert the argument. */
5323 if (TYPE_REF_OBJ_P (type)
5324 && has_value_dependent_address (expr))
5325 /* If we want the address and it's value-dependent, don't fold. */;
5326 else if (!type_unknown_p (expr))
5327 expr = fold_non_dependent_expr_sfinae (expr, complain);
5328 if (error_operand_p (expr))
5329 return error_mark_node;
5330 expr_type = TREE_TYPE (expr);
5331 if (TREE_CODE (type) == REFERENCE_TYPE)
5332 expr = mark_lvalue_use (expr);
5334 expr = mark_rvalue_use (expr);
5336 /* HACK: Due to double coercion, we can get a
5337 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5338 which is the tree that we built on the first call (see
5339 below when coercing to reference to object or to reference to
5340 function). We just strip everything and get to the arg.
5341 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5343 if (TREE_CODE (expr) == NOP_EXPR)
5345 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
5347 /* ??? Maybe we could use convert_from_reference here, but we
5348 would need to relax its constraints because the NOP_EXPR
5349 could actually change the type to something more cv-qualified,
5350 and this is not folded by convert_from_reference. */
5351 tree addr = TREE_OPERAND (expr, 0);
5352 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
5353 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
5354 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
5355 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5356 (TREE_TYPE (expr_type),
5357 TREE_TYPE (TREE_TYPE (addr))));
5359 expr = TREE_OPERAND (addr, 0);
5360 expr_type = TREE_TYPE (expr);
5363 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5364 parameter is a pointer to object, through decay and
5365 qualification conversion. Let's strip everything. */
5366 else if (TYPE_PTROBV_P (type))
5369 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
5370 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
5371 /* Skip the ADDR_EXPR only if it is part of the decay for
5372 an array. Otherwise, it is part of the original argument
5373 in the source code. */
5374 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
5375 expr = TREE_OPERAND (expr, 0);
5376 expr_type = TREE_TYPE (expr);
5380 /* [temp.arg.nontype]/5, bullet 1
5382 For a non-type template-parameter of integral or enumeration type,
5383 integral promotions (_conv.prom_) and integral conversions
5384 (_conv.integral_) are applied. */
5385 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5387 tree t = build_integral_nontype_arg_conv (type, expr, complain);
5388 t = maybe_constant_value (t);
5389 if (t != error_mark_node)
5392 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
5393 return error_mark_node;
5395 /* Notice that there are constant expressions like '4 % 0' which
5396 do not fold into integer constants. */
5397 if (TREE_CODE (expr) != INTEGER_CST)
5399 if (complain & tf_error)
5401 int errs = errorcount, warns = warningcount;
5402 expr = cxx_constant_value (expr);
5403 if (errorcount > errs || warningcount > warns)
5404 inform (EXPR_LOC_OR_HERE (expr),
5405 "in template argument for type %qT ", type);
5406 if (expr == error_mark_node)
5408 /* else cxx_constant_value complained but gave us
5409 a real constant, so go ahead. */
5410 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
5416 /* [temp.arg.nontype]/5, bullet 2
5418 For a non-type template-parameter of type pointer to object,
5419 qualification conversions (_conv.qual_) and the array-to-pointer
5420 conversion (_conv.array_) are applied. */
5421 else if (TYPE_PTROBV_P (type))
5423 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5425 A template-argument for a non-type, non-template template-parameter
5426 shall be one of: [...]
5428 -- the name of a non-type template-parameter;
5429 -- the address of an object or function with external linkage, [...]
5430 expressed as "& id-expression" where the & is optional if the name
5431 refers to a function or array, or if the corresponding
5432 template-parameter is a reference.
5434 Here, we do not care about functions, as they are invalid anyway
5435 for a parameter of type pointer-to-object. */
5437 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
5438 /* Non-type template parameters are OK. */
5440 else if (TREE_CODE (expr) != ADDR_EXPR
5441 && TREE_CODE (expr_type) != ARRAY_TYPE)
5443 if (TREE_CODE (expr) == VAR_DECL)
5445 error ("%qD is not a valid template argument "
5446 "because %qD is a variable, not the address of "
5451 /* Other values, like integer constants, might be valid
5452 non-type arguments of some other type. */
5453 return error_mark_node;
5459 decl = ((TREE_CODE (expr) == ADDR_EXPR)
5460 ? TREE_OPERAND (expr, 0) : expr);
5461 if (TREE_CODE (decl) != VAR_DECL)
5463 error ("%qE is not a valid template argument of type %qT "
5464 "because %qE is not a variable",
5468 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
5470 error ("%qE is not a valid template argument of type %qT "
5471 "because %qD does not have external linkage",
5477 expr = decay_conversion (expr);
5478 if (expr == error_mark_node)
5479 return error_mark_node;
5481 expr = perform_qualification_conversions (type, expr);
5482 if (expr == error_mark_node)
5483 return error_mark_node;
5485 /* [temp.arg.nontype]/5, bullet 3
5487 For a non-type template-parameter of type reference to object, no
5488 conversions apply. The type referred to by the reference may be more
5489 cv-qualified than the (otherwise identical) type of the
5490 template-argument. The template-parameter is bound directly to the
5491 template-argument, which must be an lvalue. */
5492 else if (TYPE_REF_OBJ_P (type))
5494 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
5496 return error_mark_node;
5498 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
5500 error ("%qE is not a valid template argument for type %qT "
5501 "because of conflicts in cv-qualification", expr, type);
5505 if (!real_lvalue_p (expr))
5507 error ("%qE is not a valid template argument for type %qT "
5508 "because it is not an lvalue", expr, type);
5512 /* [temp.arg.nontype]/1
5514 A template-argument for a non-type, non-template template-parameter
5515 shall be one of: [...]
5517 -- the address of an object or function with external linkage. */
5518 if (TREE_CODE (expr) == INDIRECT_REF
5519 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
5521 expr = TREE_OPERAND (expr, 0);
5524 error ("%q#D is not a valid template argument for type %qT "
5525 "because a reference variable does not have a constant "
5526 "address", expr, type);
5533 error ("%qE is not a valid template argument for type %qT "
5534 "because it is not an object with external linkage",
5539 if (!DECL_EXTERNAL_LINKAGE_P (expr))
5541 error ("%qE is not a valid template argument for type %qT "
5542 "because object %qD has not external linkage",
5547 expr = build_nop (type, build_address (expr));
5549 /* [temp.arg.nontype]/5, bullet 4
5551 For a non-type template-parameter of type pointer to function, only
5552 the function-to-pointer conversion (_conv.func_) is applied. If the
5553 template-argument represents a set of overloaded functions (or a
5554 pointer to such), the matching function is selected from the set
5556 else if (TYPE_PTRFN_P (type))
5558 /* If the argument is a template-id, we might not have enough
5559 context information to decay the pointer. */
5560 if (!type_unknown_p (expr_type))
5562 expr = decay_conversion (expr);
5563 if (expr == error_mark_node)
5564 return error_mark_node;
5567 expr = convert_nontype_argument_function (type, expr);
5568 if (!expr || expr == error_mark_node)
5571 if (TREE_CODE (expr) != ADDR_EXPR)
5573 error ("%qE is not a valid template argument for type %qT", expr, type);
5574 error ("it must be the address of a function with external linkage");
5578 /* [temp.arg.nontype]/5, bullet 5
5580 For a non-type template-parameter of type reference to function, no
5581 conversions apply. If the template-argument represents a set of
5582 overloaded functions, the matching function is selected from the set
5584 else if (TYPE_REFFN_P (type))
5586 if (TREE_CODE (expr) == ADDR_EXPR)
5588 error ("%qE is not a valid template argument for type %qT "
5589 "because it is a pointer", expr, type);
5590 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5594 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5595 if (!expr || expr == error_mark_node)
5598 expr = build_nop (type, build_address (expr));
5600 /* [temp.arg.nontype]/5, bullet 6
5602 For a non-type template-parameter of type pointer to member function,
5603 no conversions apply. If the template-argument represents a set of
5604 overloaded member functions, the matching member function is selected
5605 from the set (_over.over_). */
5606 else if (TYPE_PTRMEMFUNC_P (type))
5608 expr = instantiate_type (type, expr, tf_none);
5609 if (expr == error_mark_node)
5610 return error_mark_node;
5612 /* [temp.arg.nontype] bullet 1 says the pointer to member
5613 expression must be a pointer-to-member constant. */
5614 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5615 return error_mark_node;
5617 /* There is no way to disable standard conversions in
5618 resolve_address_of_overloaded_function (called by
5619 instantiate_type). It is possible that the call succeeded by
5620 converting &B::I to &D::I (where B is a base of D), so we need
5621 to reject this conversion here.
5623 Actually, even if there was a way to disable standard conversions,
5624 it would still be better to reject them here so that we can
5625 provide a superior diagnostic. */
5626 if (!same_type_p (TREE_TYPE (expr), type))
5628 error ("%qE is not a valid template argument for type %qT "
5629 "because it is of type %qT", expr, type,
5631 /* If we are just one standard conversion off, explain. */
5632 if (can_convert (type, TREE_TYPE (expr)))
5633 inform (input_location,
5634 "standard conversions are not allowed in this context");
5638 /* [temp.arg.nontype]/5, bullet 7
5640 For a non-type template-parameter of type pointer to data member,
5641 qualification conversions (_conv.qual_) are applied. */
5642 else if (TYPE_PTRMEM_P (type))
5644 /* [temp.arg.nontype] bullet 1 says the pointer to member
5645 expression must be a pointer-to-member constant. */
5646 if (!check_valid_ptrmem_cst_expr (type, expr, complain))
5647 return error_mark_node;
5649 expr = perform_qualification_conversions (type, expr);
5650 if (expr == error_mark_node)
5653 /* A template non-type parameter must be one of the above. */
5657 /* Sanity check: did we actually convert the argument to the
5659 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5660 (type, TREE_TYPE (expr)));
5664 /* Subroutine of coerce_template_template_parms, which returns 1 if
5665 PARM_PARM and ARG_PARM match using the rule for the template
5666 parameters of template template parameters. Both PARM and ARG are
5667 template parameters; the rest of the arguments are the same as for
5668 coerce_template_template_parms.
5671 coerce_template_template_parm (tree parm,
5673 tsubst_flags_t complain,
5677 if (arg == NULL_TREE || arg == error_mark_node
5678 || parm == NULL_TREE || parm == error_mark_node)
5681 if (TREE_CODE (arg) != TREE_CODE (parm))
5684 switch (TREE_CODE (parm))
5687 /* We encounter instantiations of templates like
5688 template <template <template <class> class> class TT>
5691 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5692 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5694 if (!coerce_template_template_parms
5695 (parmparm, argparm, complain, in_decl, outer_args))
5701 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5702 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5703 /* Argument is a parameter pack but parameter is not. */
5708 /* The tsubst call is used to handle cases such as
5710 template <int> class C {};
5711 template <class T, template <T> class TT> class D {};
5714 i.e. the parameter list of TT depends on earlier parameters. */
5715 if (!uses_template_parms (TREE_TYPE (arg))
5717 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5721 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5722 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5723 /* Argument is a parameter pack but parameter is not. */
5736 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5737 template template parameters. Both PARM_PARMS and ARG_PARMS are
5738 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5741 Consider the example:
5742 template <class T> class A;
5743 template<template <class U> class TT> class B;
5745 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5746 the parameters to A, and OUTER_ARGS contains A. */
5749 coerce_template_template_parms (tree parm_parms,
5751 tsubst_flags_t complain,
5755 int nparms, nargs, i;
5759 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5760 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5762 nparms = TREE_VEC_LENGTH (parm_parms);
5763 nargs = TREE_VEC_LENGTH (arg_parms);
5765 /* Determine whether we have a parameter pack at the end of the
5766 template template parameter's template parameter list. */
5767 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5769 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5771 if (parm == error_mark_node)
5774 switch (TREE_CODE (parm))
5778 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5783 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5793 && !(variadic_p && nargs >= nparms - 1))
5796 /* Check all of the template parameters except the parameter pack at
5797 the end (if any). */
5798 for (i = 0; i < nparms - variadic_p; ++i)
5800 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5801 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5804 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5805 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5807 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5815 /* Check each of the template parameters in the template
5816 argument against the template parameter pack at the end of
5817 the template template parameter. */
5818 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5821 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5823 for (; i < nargs; ++i)
5825 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5828 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5830 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5839 /* Verifies that the deduced template arguments (in TARGS) for the
5840 template template parameters (in TPARMS) represent valid bindings,
5841 by comparing the template parameter list of each template argument
5842 to the template parameter list of its corresponding template
5843 template parameter, in accordance with DR150. This
5844 routine can only be called after all template arguments have been
5845 deduced. It will return TRUE if all of the template template
5846 parameter bindings are okay, FALSE otherwise. */
5848 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5850 int i, ntparms = TREE_VEC_LENGTH (tparms);
5853 /* We're dealing with template parms in this process. */
5854 ++processing_template_decl;
5856 targs = INNERMOST_TEMPLATE_ARGS (targs);
5858 for (i = 0; i < ntparms; ++i)
5860 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5861 tree targ = TREE_VEC_ELT (targs, i);
5863 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5865 tree packed_args = NULL_TREE;
5868 if (ARGUMENT_PACK_P (targ))
5870 /* Look inside the argument pack. */
5871 packed_args = ARGUMENT_PACK_ARGS (targ);
5872 len = TREE_VEC_LENGTH (packed_args);
5875 for (idx = 0; idx < len; ++idx)
5877 tree targ_parms = NULL_TREE;
5880 /* Extract the next argument from the argument
5882 targ = TREE_VEC_ELT (packed_args, idx);
5884 if (PACK_EXPANSION_P (targ))
5885 /* Look at the pattern of the pack expansion. */
5886 targ = PACK_EXPANSION_PATTERN (targ);
5888 /* Extract the template parameters from the template
5890 if (TREE_CODE (targ) == TEMPLATE_DECL)
5891 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5892 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5893 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5895 /* Verify that we can coerce the template template
5896 parameters from the template argument to the template
5897 parameter. This requires an exact match. */
5899 && !coerce_template_template_parms
5900 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5915 --processing_template_decl;
5919 /* Since type attributes aren't mangled, we need to strip them from
5920 template type arguments. */
5923 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
5926 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
5928 mv = TYPE_MAIN_VARIANT (arg);
5929 arg = strip_typedefs (arg);
5930 if (TYPE_ALIGN (arg) != TYPE_ALIGN (mv)
5931 || TYPE_ATTRIBUTES (arg) != TYPE_ATTRIBUTES (mv))
5933 if (complain & tf_warning)
5934 warning (0, "ignoring attributes on template argument %qT", arg);
5935 arg = build_aligned_type (arg, TYPE_ALIGN (mv));
5936 arg = cp_build_type_attribute_variant (arg, TYPE_ATTRIBUTES (mv));
5941 /* Convert the indicated template ARG as necessary to match the
5942 indicated template PARM. Returns the converted ARG, or
5943 error_mark_node if the conversion was unsuccessful. Error and
5944 warning messages are issued under control of COMPLAIN. This
5945 conversion is for the Ith parameter in the parameter list. ARGS is
5946 the full set of template arguments deduced so far. */
5949 convert_template_argument (tree parm,
5952 tsubst_flags_t complain,
5958 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5960 if (TREE_CODE (arg) == TREE_LIST
5961 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5963 /* The template argument was the name of some
5964 member function. That's usually
5965 invalid, but static members are OK. In any
5966 case, grab the underlying fields/functions
5967 and issue an error later if required. */
5968 orig_arg = TREE_VALUE (arg);
5969 TREE_TYPE (arg) = unknown_type_node;
5974 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5975 requires_type = (TREE_CODE (parm) == TYPE_DECL
5976 || requires_tmpl_type);
5978 /* When determining whether an argument pack expansion is a template,
5979 look at the pattern. */
5980 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5981 arg = PACK_EXPANSION_PATTERN (arg);
5983 /* Deal with an injected-class-name used as a template template arg. */
5984 if (requires_tmpl_type && CLASS_TYPE_P (arg))
5986 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
5987 if (TREE_CODE (t) == TEMPLATE_DECL)
5989 if (cxx_dialect >= cxx0x)
5990 /* OK under DR 1004. */;
5991 else if (complain & tf_warning_or_error)
5992 pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
5993 " used as template template argument", TYPE_NAME (arg));
5994 else if (flag_pedantic_errors)
6002 ((TREE_CODE (arg) == TEMPLATE_DECL
6003 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
6004 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6005 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
6008 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
6009 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
6010 arg = TYPE_STUB_DECL (arg);
6012 is_type = TYPE_P (arg) || is_tmpl_type;
6014 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
6015 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
6017 permerror (input_location, "to refer to a type member of a template parameter, "
6018 "use %<typename %E%>", orig_arg);
6020 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
6021 TREE_OPERAND (arg, 1),
6023 complain & tf_error);
6027 if (is_type != requires_type)
6031 if (complain & tf_error)
6033 error ("type/value mismatch at argument %d in template "
6034 "parameter list for %qD",
6037 error (" expected a constant of type %qT, got %qT",
6039 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
6040 else if (requires_tmpl_type)
6041 error (" expected a class template, got %qE", orig_arg);
6043 error (" expected a type, got %qE", orig_arg);
6046 return error_mark_node;
6048 if (is_tmpl_type ^ requires_tmpl_type)
6050 if (in_decl && (complain & tf_error))
6052 error ("type/value mismatch at argument %d in template "
6053 "parameter list for %qD",
6056 error (" expected a type, got %qT", DECL_NAME (arg));
6058 error (" expected a class template, got %qT", orig_arg);
6060 return error_mark_node;
6065 if (requires_tmpl_type)
6067 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
6068 /* The number of argument required is not known yet.
6069 Just accept it for now. */
6070 val = TREE_TYPE (arg);
6073 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6076 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6078 if (coerce_template_template_parms (parmparm, argparm,
6084 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6086 if (val != error_mark_node)
6088 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
6089 val = TREE_TYPE (val);
6090 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
6091 val = make_pack_expansion (val);
6096 if (in_decl && (complain & tf_error))
6098 error ("type/value mismatch at argument %d in "
6099 "template parameter list for %qD",
6101 error (" expected a template of type %qD, got %qT",
6105 val = error_mark_node;
6111 /* We only form one instance of each template specialization.
6112 Therefore, if we use a non-canonical variant (i.e., a
6113 typedef), any future messages referring to the type will use
6114 the typedef, which is confusing if those future uses do not
6115 themselves also use the typedef. */
6117 val = canonicalize_type_argument (val, complain);
6121 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
6123 if (invalid_nontype_parm_type_p (t, complain))
6124 return error_mark_node;
6126 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
6128 if (same_type_p (t, TREE_TYPE (orig_arg)))
6132 /* Not sure if this is reachable, but it doesn't hurt
6134 error ("type mismatch in nontype parameter pack");
6135 val = error_mark_node;
6138 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
6139 /* We used to call digest_init here. However, digest_init
6140 will report errors, which we don't want when complain
6141 is zero. More importantly, digest_init will try too
6142 hard to convert things: for example, `0' should not be
6143 converted to pointer type at this point according to
6144 the standard. Accepting this is not merely an
6145 extension, since deciding whether or not these
6146 conversions can occur is part of determining which
6147 function template to call, or whether a given explicit
6148 argument specification is valid. */
6149 val = convert_nontype_argument (t, orig_arg, complain);
6153 if (val == NULL_TREE)
6154 val = error_mark_node;
6155 else if (val == error_mark_node && (complain & tf_error))
6156 error ("could not convert template argument %qE to %qT", orig_arg, t);
6158 if (TREE_CODE (val) == SCOPE_REF)
6160 /* Strip typedefs from the SCOPE_REF. */
6161 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
6162 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
6164 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
6165 QUALIFIED_NAME_IS_TEMPLATE (val));
6172 /* Coerces the remaining template arguments in INNER_ARGS (from
6173 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6174 Returns the coerced argument pack. PARM_IDX is the position of this
6175 parameter in the template parameter list. ARGS is the original
6176 template argument list. */
6178 coerce_template_parameter_pack (tree parms,
6186 tsubst_flags_t complain)
6188 tree parm = TREE_VEC_ELT (parms, parm_idx);
6189 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6192 tree packed_types = NULL_TREE;
6194 if (arg_idx > nargs)
6197 packed_args = make_tree_vec (nargs - arg_idx);
6199 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
6200 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
6202 /* When the template parameter is a non-type template
6203 parameter pack whose type uses parameter packs, we need
6204 to look at each of the template arguments
6205 separately. Build a vector of the types for these
6206 non-type template parameters in PACKED_TYPES. */
6208 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
6209 packed_types = tsubst_pack_expansion (expansion, args,
6212 if (packed_types == error_mark_node)
6213 return error_mark_node;
6215 /* Check that we have the right number of arguments. */
6217 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
6218 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
6221 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
6222 error ("wrong number of template arguments (%d, should be %d)",
6223 nargs, needed_parms);
6224 return error_mark_node;
6227 /* If we aren't able to check the actual arguments now
6228 (because they haven't been expanded yet), we can at least
6229 verify that all of the types used for the non-type
6230 template parameter pack are, in fact, valid for non-type
6231 template parameters. */
6233 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
6235 int j, len = TREE_VEC_LENGTH (packed_types);
6236 for (j = 0; j < len; ++j)
6238 tree t = TREE_VEC_ELT (packed_types, j);
6239 if (invalid_nontype_parm_type_p (t, complain))
6240 return error_mark_node;
6245 /* Convert the remaining arguments, which will be a part of the
6246 parameter pack "parm". */
6247 for (; arg_idx < nargs; ++arg_idx)
6249 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
6250 tree actual_parm = TREE_VALUE (parm);
6252 if (packed_types && !PACK_EXPANSION_P (arg))
6254 /* When we have a vector of types (corresponding to the
6255 non-type template parameter pack that uses parameter
6256 packs in its type, as mention above), and the
6257 argument is not an expansion (which expands to a
6258 currently unknown number of arguments), clone the
6259 parm and give it the next type in PACKED_TYPES. */
6260 actual_parm = copy_node (actual_parm);
6261 TREE_TYPE (actual_parm) =
6262 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
6265 if (arg != error_mark_node)
6266 arg = convert_template_argument (actual_parm,
6267 arg, new_args, complain, parm_idx,
6269 if (arg == error_mark_node)
6271 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
6274 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
6275 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
6276 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
6279 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
6280 TREE_TYPE (argument_pack)
6281 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
6282 TREE_CONSTANT (argument_pack) = 1;
6285 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
6286 #ifdef ENABLE_CHECKING
6287 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
6288 TREE_VEC_LENGTH (packed_args));
6290 return argument_pack;
6293 /* Convert all template arguments to their appropriate types, and
6294 return a vector containing the innermost resulting template
6295 arguments. If any error occurs, return error_mark_node. Error and
6296 warning messages are issued under control of COMPLAIN.
6298 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6299 for arguments not specified in ARGS. Otherwise, if
6300 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6301 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6302 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6306 coerce_template_parms (tree parms,
6309 tsubst_flags_t complain,
6310 bool require_all_args,
6311 bool use_default_args)
6313 int nparms, nargs, parm_idx, arg_idx, lost = 0;
6316 tree new_inner_args;
6317 int saved_unevaluated_operand;
6318 int saved_inhibit_evaluation_warnings;
6320 /* When used as a boolean value, indicates whether this is a
6321 variadic template parameter list. Since it's an int, we can also
6322 subtract it from nparms to get the number of non-variadic
6326 if (args == error_mark_node)
6327 return error_mark_node;
6329 nparms = TREE_VEC_LENGTH (parms);
6331 /* Determine if there are any parameter packs. */
6332 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
6334 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
6335 if (template_parameter_pack_p (tparm))
6339 inner_args = INNERMOST_TEMPLATE_ARGS (args);
6340 /* If there are 0 or 1 parameter packs, we need to expand any argument
6341 packs so that we can deduce a parameter pack from some non-packed args
6342 followed by an argument pack, as in variadic85.C. If there are more
6343 than that, we need to leave argument packs intact so the arguments are
6344 assigned to the right parameter packs. This should only happen when
6345 dealing with a nested class inside a partial specialization of a class
6346 template, as in variadic92.C. */
6347 if (variadic_p <= 1)
6348 inner_args = expand_template_argument_pack (inner_args);
6350 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
6351 if ((nargs > nparms && !variadic_p)
6352 || (nargs < nparms - variadic_p
6354 && (!use_default_args
6355 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
6356 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
6358 if (complain & tf_error)
6363 error ("wrong number of template arguments "
6364 "(%d, should be %d or more)", nargs, nparms);
6367 error ("wrong number of template arguments "
6368 "(%d, should be %d)", nargs, nparms);
6371 error ("provided for %q+D", in_decl);
6374 return error_mark_node;
6377 /* We need to evaluate the template arguments, even though this
6378 template-id may be nested within a "sizeof". */
6379 saved_unevaluated_operand = cp_unevaluated_operand;
6380 cp_unevaluated_operand = 0;
6381 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
6382 c_inhibit_evaluation_warnings = 0;
6383 new_inner_args = make_tree_vec (nparms);
6384 new_args = add_outermost_template_args (args, new_inner_args);
6385 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
6390 /* Get the Ith template parameter. */
6391 parm = TREE_VEC_ELT (parms, parm_idx);
6393 if (parm == error_mark_node)
6395 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
6399 /* Calculate the next argument. */
6400 if (arg_idx < nargs)
6401 arg = TREE_VEC_ELT (inner_args, arg_idx);
6405 if (template_parameter_pack_p (TREE_VALUE (parm))
6406 && !(arg && ARGUMENT_PACK_P (arg)))
6408 /* All remaining arguments will be placed in the
6409 template parameter pack PARM. */
6410 arg = coerce_template_parameter_pack (parms, parm_idx, args,
6411 inner_args, arg_idx,
6415 /* Store this argument. */
6416 if (arg == error_mark_node)
6418 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
6420 /* We are done with all of the arguments. */
6427 if (PACK_EXPANSION_P (arg))
6429 if (complain & tf_error)
6431 /* FIXME this restriction was removed by N2555; see
6433 /* If ARG is a pack expansion, but PARM is not a
6434 template parameter pack (if it were, we would have
6435 handled it above), we're trying to expand into a
6436 fixed-length argument list. */
6437 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
6438 sorry ("cannot expand %<%E%> into a fixed-length "
6439 "argument list", arg);
6441 sorry ("cannot expand %<%T%> into a fixed-length "
6442 "argument list", arg);
6447 else if (require_all_args)
6449 /* There must be a default arg in this case. */
6450 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
6452 /* The position of the first default template argument,
6453 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6455 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6456 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
6461 if (arg == error_mark_node)
6463 if (complain & tf_error)
6464 error ("template argument %d is invalid", arg_idx + 1);
6467 /* This only occurs if there was an error in the template
6468 parameter list itself (which we would already have
6469 reported) that we are trying to recover from, e.g., a class
6470 template with a parameter list such as
6471 template<typename..., typename>. */
6474 arg = convert_template_argument (TREE_VALUE (parm),
6475 arg, new_args, complain,
6478 if (arg == error_mark_node)
6480 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
6482 cp_unevaluated_operand = saved_unevaluated_operand;
6483 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
6486 return error_mark_node;
6488 #ifdef ENABLE_CHECKING
6489 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
6490 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
6491 TREE_VEC_LENGTH (new_inner_args));
6494 return new_inner_args;
6497 /* Returns 1 if template args OT and NT are equivalent. */
6500 template_args_equal (tree ot, tree nt)
6504 if (nt == NULL_TREE || ot == NULL_TREE)
6507 if (TREE_CODE (nt) == TREE_VEC)
6508 /* For member templates */
6509 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
6510 else if (PACK_EXPANSION_P (ot))
6511 return PACK_EXPANSION_P (nt)
6512 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
6513 PACK_EXPANSION_PATTERN (nt));
6514 else if (ARGUMENT_PACK_P (ot))
6519 if (!ARGUMENT_PACK_P (nt))
6522 opack = ARGUMENT_PACK_ARGS (ot);
6523 npack = ARGUMENT_PACK_ARGS (nt);
6524 len = TREE_VEC_LENGTH (opack);
6525 if (TREE_VEC_LENGTH (npack) != len)
6527 for (i = 0; i < len; ++i)
6528 if (!template_args_equal (TREE_VEC_ELT (opack, i),
6529 TREE_VEC_ELT (npack, i)))
6533 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
6535 /* We get here probably because we are in the middle of substituting
6536 into the pattern of a pack expansion. In that case the
6537 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6538 interested in. So we want to use the initial pack argument for
6540 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
6541 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
6542 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
6543 return template_args_equal (ot, nt);
6545 else if (TYPE_P (nt))
6546 return TYPE_P (ot) && same_type_p (ot, nt);
6547 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
6550 return cp_tree_equal (ot, nt);
6553 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6554 of template arguments. Returns 0 otherwise. */
6557 comp_template_args (tree oldargs, tree newargs)
6561 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
6564 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
6566 tree nt = TREE_VEC_ELT (newargs, i);
6567 tree ot = TREE_VEC_ELT (oldargs, i);
6569 if (! template_args_equal (ot, nt))
6576 add_pending_template (tree d)
6578 tree ti = (TYPE_P (d)
6579 ? CLASSTYPE_TEMPLATE_INFO (d)
6580 : DECL_TEMPLATE_INFO (d));
6581 struct pending_template *pt;
6584 if (TI_PENDING_TEMPLATE_FLAG (ti))
6587 /* We are called both from instantiate_decl, where we've already had a
6588 tinst_level pushed, and instantiate_template, where we haven't.
6590 level = !current_tinst_level || current_tinst_level->decl != d;
6593 push_tinst_level (d);
6595 pt = ggc_alloc_pending_template ();
6597 pt->tinst = current_tinst_level;
6598 if (last_pending_template)
6599 last_pending_template->next = pt;
6601 pending_templates = pt;
6603 last_pending_template = pt;
6605 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
6612 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6613 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6614 documentation for TEMPLATE_ID_EXPR. */
6617 lookup_template_function (tree fns, tree arglist)
6621 if (fns == error_mark_node || arglist == error_mark_node)
6622 return error_mark_node;
6624 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
6625 gcc_assert (fns && (is_overloaded_fn (fns)
6626 || TREE_CODE (fns) == IDENTIFIER_NODE));
6628 if (BASELINK_P (fns))
6630 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
6632 BASELINK_FUNCTIONS (fns),
6637 type = TREE_TYPE (fns);
6638 if (TREE_CODE (fns) == OVERLOAD || !type)
6639 type = unknown_type_node;
6641 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
6644 /* Within the scope of a template class S<T>, the name S gets bound
6645 (in build_self_reference) to a TYPE_DECL for the class, not a
6646 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6647 or one of its enclosing classes, and that type is a template,
6648 return the associated TEMPLATE_DECL. Otherwise, the original
6651 Also handle the case when DECL is a TREE_LIST of ambiguous
6652 injected-class-names from different bases. */
6655 maybe_get_template_decl_from_type_decl (tree decl)
6657 if (decl == NULL_TREE)
6660 /* DR 176: A lookup that finds an injected-class-name (10.2
6661 [class.member.lookup]) can result in an ambiguity in certain cases
6662 (for example, if it is found in more than one base class). If all of
6663 the injected-class-names that are found refer to specializations of
6664 the same class template, and if the name is followed by a
6665 template-argument-list, the reference refers to the class template
6666 itself and not a specialization thereof, and is not ambiguous. */
6667 if (TREE_CODE (decl) == TREE_LIST)
6669 tree t, tmpl = NULL_TREE;
6670 for (t = decl; t; t = TREE_CHAIN (t))
6672 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
6675 else if (tmpl != elt)
6678 if (tmpl && t == NULL_TREE)
6684 return (decl != NULL_TREE
6685 && DECL_SELF_REFERENCE_P (decl)
6686 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6687 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6690 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6691 parameters, find the desired type.
6693 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6695 IN_DECL, if non-NULL, is the template declaration we are trying to
6698 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6699 the class we are looking up.
6701 Issue error and warning messages under control of COMPLAIN.
6703 If the template class is really a local class in a template
6704 function, then the FUNCTION_CONTEXT is the function in which it is
6707 ??? Note that this function is currently called *twice* for each
6708 template-id: the first time from the parser, while creating the
6709 incomplete type (finish_template_type), and the second type during the
6710 real instantiation (instantiate_template_class). This is surely something
6711 that we want to avoid. It also causes some problems with argument
6712 coercion (see convert_nontype_argument for more information on this). */
6715 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
6716 int entering_scope, tsubst_flags_t complain)
6718 tree templ = NULL_TREE, parmlist;
6725 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6727 tree value = innermost_non_namespace_value (d1);
6728 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6733 push_decl_namespace (context);
6734 templ = lookup_name (d1);
6735 templ = maybe_get_template_decl_from_type_decl (templ);
6737 pop_decl_namespace ();
6740 context = DECL_CONTEXT (templ);
6742 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6744 tree type = TREE_TYPE (d1);
6746 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6747 an implicit typename for the second A. Deal with it. */
6748 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6749 type = TREE_TYPE (type);
6751 if (CLASSTYPE_TEMPLATE_INFO (type))
6753 templ = CLASSTYPE_TI_TEMPLATE (type);
6754 d1 = DECL_NAME (templ);
6757 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6758 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6760 templ = TYPE_TI_TEMPLATE (d1);
6761 d1 = DECL_NAME (templ);
6763 else if (TREE_CODE (d1) == TEMPLATE_DECL
6764 && DECL_TEMPLATE_RESULT (d1)
6765 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6768 d1 = DECL_NAME (templ);
6769 context = DECL_CONTEXT (templ);
6772 /* Issue an error message if we didn't find a template. */
6775 if (complain & tf_error)
6776 error ("%qT is not a template", d1);
6777 return error_mark_node;
6780 if (TREE_CODE (templ) != TEMPLATE_DECL
6781 /* Make sure it's a user visible template, if it was named by
6783 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6784 && !PRIMARY_TEMPLATE_P (templ)))
6786 if (complain & tf_error)
6788 error ("non-template type %qT used as a template", d1);
6790 error ("for template declaration %q+D", in_decl);
6792 return error_mark_node;
6795 complain &= ~tf_user;
6797 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6799 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6800 template arguments */
6806 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6808 /* Consider an example where a template template parameter declared as
6810 template <class T, class U = std::allocator<T> > class TT
6812 The template parameter level of T and U are one level larger than
6813 of TT. To proper process the default argument of U, say when an
6814 instantiation `TT<int>' is seen, we need to build the full
6815 arguments containing {int} as the innermost level. Outer levels,
6816 available when not appearing as default template argument, can be
6817 obtained from the arguments of the enclosing template.
6819 Suppose that TT is later substituted with std::vector. The above
6820 instantiation is `TT<int, std::allocator<T> >' with TT at
6821 level 1, and T at level 2, while the template arguments at level 1
6822 becomes {std::vector} and the inner level 2 is {int}. */
6824 outer = DECL_CONTEXT (templ);
6826 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6827 else if (current_template_parms)
6828 /* This is an argument of the current template, so we haven't set
6829 DECL_CONTEXT yet. */
6830 outer = current_template_args ();
6833 arglist = add_to_template_args (outer, arglist);
6835 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6837 /*require_all_args=*/true,
6838 /*use_default_args=*/true);
6839 if (arglist2 == error_mark_node
6840 || (!uses_template_parms (arglist2)
6841 && check_instantiated_args (templ, arglist2, complain)))
6842 return error_mark_node;
6844 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6849 tree template_type = TREE_TYPE (templ);
6852 tree found = NULL_TREE;
6855 int is_dependent_type;
6856 int use_partial_inst_tmpl = false;
6858 gen_tmpl = most_general_template (templ);
6859 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6860 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6861 arg_depth = TMPL_ARGS_DEPTH (arglist);
6863 if (arg_depth == 1 && parm_depth > 1)
6865 /* We've been given an incomplete set of template arguments.
6868 template <class T> struct S1 {
6869 template <class U> struct S2 {};
6870 template <class U> struct S2<U*> {};
6873 we will be called with an ARGLIST of `U*', but the
6874 TEMPLATE will be `template <class T> template
6875 <class U> struct S1<T>::S2'. We must fill in the missing
6878 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6880 arg_depth = TMPL_ARGS_DEPTH (arglist);
6883 /* Now we should have enough arguments. */
6884 gcc_assert (parm_depth == arg_depth);
6886 /* From here on, we're only interested in the most general
6889 /* Calculate the BOUND_ARGS. These will be the args that are
6890 actually tsubst'd into the definition to create the
6894 /* We have multiple levels of arguments to coerce, at once. */
6896 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6898 tree bound_args = make_tree_vec (parm_depth);
6900 for (i = saved_depth,
6901 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6902 i > 0 && t != NULL_TREE;
6903 --i, t = TREE_CHAIN (t))
6906 if (i == saved_depth)
6907 a = coerce_template_parms (TREE_VALUE (t),
6910 /*require_all_args=*/true,
6911 /*use_default_args=*/true);
6913 /* Outer levels should have already been coerced. */
6914 a = TMPL_ARGS_LEVEL (arglist, i);
6916 /* Don't process further if one of the levels fails. */
6917 if (a == error_mark_node)
6919 /* Restore the ARGLIST to its full size. */
6920 TREE_VEC_LENGTH (arglist) = saved_depth;
6921 return error_mark_node;
6924 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6926 /* We temporarily reduce the length of the ARGLIST so
6927 that coerce_template_parms will see only the arguments
6928 corresponding to the template parameters it is
6930 TREE_VEC_LENGTH (arglist)--;
6933 /* Restore the ARGLIST to its full size. */
6934 TREE_VEC_LENGTH (arglist) = saved_depth;
6936 arglist = bound_args;
6940 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6941 INNERMOST_TEMPLATE_ARGS (arglist),
6944 /*require_all_args=*/true,
6945 /*use_default_args=*/true);
6947 if (arglist == error_mark_node)
6948 /* We were unable to bind the arguments. */
6949 return error_mark_node;
6951 /* In the scope of a template class, explicit references to the
6952 template class refer to the type of the template, not any
6953 instantiation of it. For example, in:
6955 template <class T> class C { void f(C<T>); }
6957 the `C<T>' is just the same as `C'. Outside of the
6958 class, however, such a reference is an instantiation. */
6960 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6961 || currently_open_class (template_type))
6962 /* comp_template_args is expensive, check it last. */
6963 && comp_template_args (TYPE_TI_ARGS (template_type),
6965 return template_type;
6967 /* If we already have this specialization, return it. */
6968 elt.tmpl = gen_tmpl;
6970 hash = hash_specialization (&elt);
6971 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6977 is_dependent_type = uses_template_parms (arglist);
6979 /* If the deduced arguments are invalid, then the binding
6981 if (!is_dependent_type
6982 && check_instantiated_args (gen_tmpl,
6983 INNERMOST_TEMPLATE_ARGS (arglist),
6985 return error_mark_node;
6987 if (!is_dependent_type
6988 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6989 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6990 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6992 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6993 DECL_NAME (gen_tmpl),
6994 /*tag_scope=*/ts_global);
6998 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
7001 context = global_namespace;
7003 /* Create the type. */
7004 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
7006 if (!is_dependent_type)
7008 set_current_access_from_decl (TYPE_NAME (template_type));
7009 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
7010 tsubst (ENUM_UNDERLYING_TYPE (template_type),
7011 arglist, complain, in_decl),
7012 SCOPED_ENUM_P (template_type), NULL);
7016 /* We don't want to call start_enum for this type, since
7017 the values for the enumeration constants may involve
7018 template parameters. And, no one should be interested
7019 in the enumeration constants for such a type. */
7020 t = cxx_make_type (ENUMERAL_TYPE);
7021 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
7023 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
7024 ENUM_FIXED_UNDERLYING_TYPE_P (t)
7025 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
7029 t = make_class_type (TREE_CODE (template_type));
7030 CLASSTYPE_DECLARED_CLASS (t)
7031 = CLASSTYPE_DECLARED_CLASS (template_type);
7032 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
7033 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
7035 /* A local class. Make sure the decl gets registered properly. */
7036 if (context == current_function_decl)
7037 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
7039 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
7040 /* This instantiation is another name for the primary
7041 template type. Set the TYPE_CANONICAL field
7043 TYPE_CANONICAL (t) = template_type;
7044 else if (any_template_arguments_need_structural_equality_p (arglist))
7045 /* Some of the template arguments require structural
7046 equality testing, so this template class requires
7047 structural equality testing. */
7048 SET_TYPE_STRUCTURAL_EQUALITY (t);
7051 /* If we called start_enum or pushtag above, this information
7052 will already be set up. */
7055 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
7057 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
7058 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
7059 DECL_SOURCE_LOCATION (type_decl)
7060 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
7063 type_decl = TYPE_NAME (t);
7065 TREE_PRIVATE (type_decl)
7066 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
7067 TREE_PROTECTED (type_decl)
7068 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
7069 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
7071 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
7072 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
7075 /* Let's consider the explicit specialization of a member
7076 of a class template specialization that is implicitely instantiated,
7081 template<class U> struct M {}; //#0
7086 struct S<int>::M<char> //#1
7090 [temp.expl.spec]/4 says this is valid.
7092 In this case, when we write:
7095 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7098 When we encounter #1, we want to store the partial instantiation
7099 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7101 For all cases other than this "explicit specialization of member of a
7102 class template", we just want to store the most general template into
7103 the CLASSTYPE_TI_TEMPLATE of M.
7105 This case of "explicit specialization of member of a class template"
7107 1/ the enclosing class is an instantiation of, and therefore not
7108 the same as, the context of the most general template, and
7109 2/ we aren't looking at the partial instantiation itself, i.e.
7110 the innermost arguments are not the same as the innermost parms of
7111 the most general template.
7113 So it's only when 1/ and 2/ happens that we want to use the partial
7114 instantiation of the member template in lieu of its most general
7117 if (PRIMARY_TEMPLATE_P (gen_tmpl)
7118 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
7119 /* the enclosing class must be an instantiation... */
7120 && CLASS_TYPE_P (context)
7121 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
7123 tree partial_inst_args;
7124 TREE_VEC_LENGTH (arglist)--;
7125 ++processing_template_decl;
7127 tsubst (INNERMOST_TEMPLATE_ARGS
7128 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
7129 arglist, complain, NULL_TREE);
7130 --processing_template_decl;
7131 TREE_VEC_LENGTH (arglist)++;
7132 use_partial_inst_tmpl =
7133 /*...and we must not be looking at the partial instantiation
7135 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
7139 if (!use_partial_inst_tmpl)
7140 /* This case is easy; there are no member templates involved. */
7144 /* This is a full instantiation of a member template. Find
7145 the partial instantiation of which this is an instance. */
7147 /* Temporarily reduce by one the number of levels in the ARGLIST
7148 so as to avoid comparing the last set of arguments. */
7149 TREE_VEC_LENGTH (arglist)--;
7150 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
7151 TREE_VEC_LENGTH (arglist)++;
7152 found = CLASSTYPE_TI_TEMPLATE (found);
7155 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
7158 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
7159 &elt, hash, INSERT);
7160 *slot = ggc_alloc_spec_entry ();
7163 /* Note this use of the partial instantiation so we can check it
7164 later in maybe_process_partial_specialization. */
7165 DECL_TEMPLATE_INSTANTIATIONS (templ)
7166 = tree_cons (arglist, t,
7167 DECL_TEMPLATE_INSTANTIATIONS (templ));
7169 if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
7170 /* Now that the type has been registered on the instantiations
7171 list, we set up the enumerators. Because the enumeration
7172 constants may involve the enumeration type itself, we make
7173 sure to register the type first, and then create the
7174 constants. That way, doing tsubst_expr for the enumeration
7175 constants won't result in recursive calls here; we'll find
7176 the instantiation and exit above. */
7177 tsubst_enum (template_type, t, arglist);
7179 if (is_dependent_type)
7180 /* If the type makes use of template parameters, the
7181 code that generates debugging information will crash. */
7182 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
7184 /* Possibly limit visibility based on template args. */
7185 TREE_PUBLIC (type_decl) = 1;
7186 determine_visibility (type_decl);
7192 /* Wrapper for lookup_template_class_1. */
7195 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
7196 int entering_scope, tsubst_flags_t complain)
7199 timevar_push (TV_TEMPLATE_INST);
7200 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
7201 entering_scope, complain);
7202 timevar_pop (TV_TEMPLATE_INST);
7210 /* True when we should also visit template parameters that occur in
7211 non-deduced contexts. */
7212 bool include_nondeduced_p;
7213 struct pointer_set_t *visited;
7216 /* Called from for_each_template_parm via walk_tree. */
7219 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
7222 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
7223 tree_fn_t fn = pfd->fn;
7224 void *data = pfd->data;
7227 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
7228 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
7229 pfd->include_nondeduced_p))
7230 return error_mark_node;
7232 switch (TREE_CODE (t))
7235 if (TYPE_PTRMEMFUNC_P (t))
7241 if (!TYPE_TEMPLATE_INFO (t))
7243 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
7244 fn, data, pfd->visited,
7245 pfd->include_nondeduced_p))
7246 return error_mark_node;
7250 if (for_each_template_parm (TYPE_MIN_VALUE (t),
7251 fn, data, pfd->visited,
7252 pfd->include_nondeduced_p)
7253 || for_each_template_parm (TYPE_MAX_VALUE (t),
7254 fn, data, pfd->visited,
7255 pfd->include_nondeduced_p))
7256 return error_mark_node;
7260 /* Since we're not going to walk subtrees, we have to do this
7262 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
7263 pfd->visited, pfd->include_nondeduced_p))
7264 return error_mark_node;
7268 /* Check the return type. */
7269 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7270 pfd->include_nondeduced_p))
7271 return error_mark_node;
7273 /* Check the parameter types. Since default arguments are not
7274 instantiated until they are needed, the TYPE_ARG_TYPES may
7275 contain expressions that involve template parameters. But,
7276 no-one should be looking at them yet. And, once they're
7277 instantiated, they don't contain template parameters, so
7278 there's no point in looking at them then, either. */
7282 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
7283 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
7284 pfd->visited, pfd->include_nondeduced_p))
7285 return error_mark_node;
7287 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7288 want walk_tree walking into them itself. */
7294 case UNDERLYING_TYPE:
7295 if (pfd->include_nondeduced_p
7296 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
7298 pfd->include_nondeduced_p))
7299 return error_mark_node;
7304 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
7305 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
7306 pfd->visited, pfd->include_nondeduced_p))
7307 return error_mark_node;
7312 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
7313 && for_each_template_parm (DECL_INITIAL (t), fn, data,
7314 pfd->visited, pfd->include_nondeduced_p))
7315 return error_mark_node;
7316 if (DECL_CONTEXT (t)
7317 && pfd->include_nondeduced_p
7318 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
7319 pfd->visited, pfd->include_nondeduced_p))
7320 return error_mark_node;
7323 case BOUND_TEMPLATE_TEMPLATE_PARM:
7324 /* Record template parameters such as `T' inside `TT<T>'. */
7325 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
7326 pfd->include_nondeduced_p))
7327 return error_mark_node;
7330 case TEMPLATE_TEMPLATE_PARM:
7331 case TEMPLATE_TYPE_PARM:
7332 case TEMPLATE_PARM_INDEX:
7333 if (fn && (*fn)(t, data))
7334 return error_mark_node;
7336 return error_mark_node;
7340 /* A template template parameter is encountered. */
7341 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
7342 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
7343 pfd->include_nondeduced_p))
7344 return error_mark_node;
7346 /* Already substituted template template parameter */
7352 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
7354 pfd->include_nondeduced_p))
7355 return error_mark_node;
7359 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
7360 && pfd->include_nondeduced_p
7361 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7362 (TREE_TYPE (t)), fn, data,
7363 pfd->visited, pfd->include_nondeduced_p))
7364 return error_mark_node;
7369 /* If there's no type, then this thing must be some expression
7370 involving template parameters. */
7371 if (!fn && !TREE_TYPE (t))
7372 return error_mark_node;
7377 case REINTERPRET_CAST_EXPR:
7378 case CONST_CAST_EXPR:
7379 case STATIC_CAST_EXPR:
7380 case DYNAMIC_CAST_EXPR:
7384 case PSEUDO_DTOR_EXPR:
7386 return error_mark_node;
7393 /* We didn't find any template parameters we liked. */
7397 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7398 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7399 call FN with the parameter and the DATA.
7400 If FN returns nonzero, the iteration is terminated, and
7401 for_each_template_parm returns 1. Otherwise, the iteration
7402 continues. If FN never returns a nonzero value, the value
7403 returned by for_each_template_parm is 0. If FN is NULL, it is
7404 considered to be the function which always returns 1.
7406 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7407 parameters that occur in non-deduced contexts. When false, only
7408 visits those template parameters that can be deduced. */
7411 for_each_template_parm (tree t, tree_fn_t fn, void* data,
7412 struct pointer_set_t *visited,
7413 bool include_nondeduced_p)
7415 struct pair_fn_data pfd;
7421 pfd.include_nondeduced_p = include_nondeduced_p;
7423 /* Walk the tree. (Conceptually, we would like to walk without
7424 duplicates, but for_each_template_parm_r recursively calls
7425 for_each_template_parm, so we would need to reorganize a fair
7426 bit to use walk_tree_without_duplicates, so we keep our own
7429 pfd.visited = visited;
7431 pfd.visited = pointer_set_create ();
7432 result = cp_walk_tree (&t,
7433 for_each_template_parm_r,
7435 pfd.visited) != NULL_TREE;
7440 pointer_set_destroy (pfd.visited);
7447 /* Returns true if T depends on any template parameter. */
7450 uses_template_parms (tree t)
7453 int saved_processing_template_decl;
7455 saved_processing_template_decl = processing_template_decl;
7456 if (!saved_processing_template_decl)
7457 processing_template_decl = 1;
7459 dependent_p = dependent_type_p (t);
7460 else if (TREE_CODE (t) == TREE_VEC)
7461 dependent_p = any_dependent_template_arguments_p (t);
7462 else if (TREE_CODE (t) == TREE_LIST)
7463 dependent_p = (uses_template_parms (TREE_VALUE (t))
7464 || uses_template_parms (TREE_CHAIN (t)));
7465 else if (TREE_CODE (t) == TYPE_DECL)
7466 dependent_p = dependent_type_p (TREE_TYPE (t));
7469 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
7470 || TREE_CODE (t) == OVERLOAD
7471 || TREE_CODE (t) == BASELINK
7472 || TREE_CODE (t) == IDENTIFIER_NODE
7473 || TREE_CODE (t) == TRAIT_EXPR
7474 || TREE_CODE (t) == CONSTRUCTOR
7475 || CONSTANT_CLASS_P (t))
7476 dependent_p = (type_dependent_expression_p (t)
7477 || value_dependent_expression_p (t));
7480 gcc_assert (t == error_mark_node);
7481 dependent_p = false;
7484 processing_template_decl = saved_processing_template_decl;
7489 /* Returns true if T depends on any template parameter with level LEVEL. */
7492 uses_template_parms_level (tree t, int level)
7494 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
7495 /*include_nondeduced_p=*/true);
7498 static int tinst_depth;
7499 extern int max_tinst_depth;
7500 #ifdef GATHER_STATISTICS
7503 static int tinst_level_tick;
7504 static int last_template_error_tick;
7506 /* We're starting to instantiate D; record the template instantiation context
7507 for diagnostics and to restore it later. */
7510 push_tinst_level (tree d)
7512 struct tinst_level *new_level;
7514 if (tinst_depth >= max_tinst_depth)
7516 last_template_error_tick = tinst_level_tick;
7517 if (TREE_CODE (d) == TREE_LIST)
7518 error ("template instantiation depth exceeds maximum of %d (use "
7519 "-ftemplate-depth= to increase the maximum) substituting %qS",
7520 max_tinst_depth, d);
7522 error ("template instantiation depth exceeds maximum of %d (use "
7523 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7524 max_tinst_depth, d);
7526 print_instantiation_context ();
7531 new_level = ggc_alloc_tinst_level ();
7532 new_level->decl = d;
7533 new_level->locus = input_location;
7534 new_level->in_system_header_p = in_system_header;
7535 new_level->next = current_tinst_level;
7536 current_tinst_level = new_level;
7539 #ifdef GATHER_STATISTICS
7540 if (tinst_depth > depth_reached)
7541 depth_reached = tinst_depth;
7548 /* We're done instantiating this template; return to the instantiation
7552 pop_tinst_level (void)
7554 /* Restore the filename and line number stashed away when we started
7555 this instantiation. */
7556 input_location = current_tinst_level->locus;
7557 current_tinst_level = current_tinst_level->next;
7562 /* We're instantiating a deferred template; restore the template
7563 instantiation context in which the instantiation was requested, which
7564 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7567 reopen_tinst_level (struct tinst_level *level)
7569 struct tinst_level *t;
7572 for (t = level; t; t = t->next)
7575 current_tinst_level = level;
7580 /* Returns the TINST_LEVEL which gives the original instantiation
7583 struct tinst_level *
7584 outermost_tinst_level (void)
7586 struct tinst_level *level = current_tinst_level;
7589 level = level->next;
7593 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7596 parameter_of_template_p (tree parm, tree templ)
7601 if (!parm || !templ)
7604 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
7605 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7607 parms = DECL_TEMPLATE_PARMS (templ);
7608 parms = INNERMOST_TEMPLATE_PARMS (parms);
7610 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7611 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
7617 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7618 vector of template arguments, as for tsubst.
7620 Returns an appropriate tsubst'd friend declaration. */
7623 tsubst_friend_function (tree decl, tree args)
7627 if (TREE_CODE (decl) == FUNCTION_DECL
7628 && DECL_TEMPLATE_INSTANTIATION (decl)
7629 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
7630 /* This was a friend declared with an explicit template
7631 argument list, e.g.:
7635 to indicate that f was a template instantiation, not a new
7636 function declaration. Now, we have to figure out what
7637 instantiation of what template. */
7639 tree template_id, arglist, fns;
7642 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
7644 /* Friend functions are looked up in the containing namespace scope.
7645 We must enter that scope, to avoid finding member functions of the
7646 current class with same name. */
7647 push_nested_namespace (ns);
7648 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
7649 tf_warning_or_error, NULL_TREE,
7650 /*integral_constant_expression_p=*/false);
7651 pop_nested_namespace (ns);
7652 arglist = tsubst (DECL_TI_ARGS (decl), args,
7653 tf_warning_or_error, NULL_TREE);
7654 template_id = lookup_template_function (fns, arglist);
7656 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7657 tmpl = determine_specialization (template_id, new_friend,
7659 /*need_member_template=*/0,
7660 TREE_VEC_LENGTH (args),
7662 return instantiate_template (tmpl, new_args, tf_error);
7665 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
7667 /* The NEW_FRIEND will look like an instantiation, to the
7668 compiler, but is not an instantiation from the point of view of
7669 the language. For example, we might have had:
7671 template <class T> struct S {
7672 template <class U> friend void f(T, U);
7675 Then, in S<int>, template <class U> void f(int, U) is not an
7676 instantiation of anything. */
7677 if (new_friend == error_mark_node)
7678 return error_mark_node;
7680 DECL_USE_TEMPLATE (new_friend) = 0;
7681 if (TREE_CODE (decl) == TEMPLATE_DECL)
7683 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
7684 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
7685 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
7688 /* The mangled name for the NEW_FRIEND is incorrect. The function
7689 is not a template instantiation and should not be mangled like
7690 one. Therefore, we forget the mangling here; we'll recompute it
7691 later if we need it. */
7692 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
7694 SET_DECL_RTL (new_friend, NULL);
7695 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
7698 if (DECL_NAMESPACE_SCOPE_P (new_friend))
7701 tree new_friend_template_info;
7702 tree new_friend_result_template_info;
7704 int new_friend_is_defn;
7706 /* We must save some information from NEW_FRIEND before calling
7707 duplicate decls since that function will free NEW_FRIEND if
7709 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
7710 new_friend_is_defn =
7711 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7712 (template_for_substitution (new_friend)))
7714 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
7716 /* This declaration is a `primary' template. */
7717 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
7719 new_friend_result_template_info
7720 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
7723 new_friend_result_template_info = NULL_TREE;
7725 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7726 if (new_friend_is_defn)
7727 DECL_INITIAL (new_friend) = error_mark_node;
7729 /* Inside pushdecl_namespace_level, we will push into the
7730 current namespace. However, the friend function should go
7731 into the namespace of the template. */
7732 ns = decl_namespace_context (new_friend);
7733 push_nested_namespace (ns);
7734 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
7735 pop_nested_namespace (ns);
7737 if (old_decl == error_mark_node)
7738 return error_mark_node;
7740 if (old_decl != new_friend)
7742 /* This new friend declaration matched an existing
7743 declaration. For example, given:
7745 template <class T> void f(T);
7746 template <class U> class C {
7747 template <class T> friend void f(T) {}
7750 the friend declaration actually provides the definition
7751 of `f', once C has been instantiated for some type. So,
7752 old_decl will be the out-of-class template declaration,
7753 while new_friend is the in-class definition.
7755 But, if `f' was called before this point, the
7756 instantiation of `f' will have DECL_TI_ARGS corresponding
7757 to `T' but not to `U', references to which might appear
7758 in the definition of `f'. Previously, the most general
7759 template for an instantiation of `f' was the out-of-class
7760 version; now it is the in-class version. Therefore, we
7761 run through all specialization of `f', adding to their
7762 DECL_TI_ARGS appropriately. In particular, they need a
7763 new set of outer arguments, corresponding to the
7764 arguments for this class instantiation.
7766 The same situation can arise with something like this:
7769 template <class T> class C {
7773 when `C<int>' is instantiated. Now, `f(int)' is defined
7776 if (!new_friend_is_defn)
7777 /* On the other hand, if the in-class declaration does
7778 *not* provide a definition, then we don't want to alter
7779 existing definitions. We can just leave everything
7784 tree new_template = TI_TEMPLATE (new_friend_template_info);
7785 tree new_args = TI_ARGS (new_friend_template_info);
7787 /* Overwrite whatever template info was there before, if
7788 any, with the new template information pertaining to
7790 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7792 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7794 /* We should have called reregister_specialization in
7796 gcc_assert (retrieve_specialization (new_template,
7800 /* Instantiate it if the global has already been used. */
7801 if (DECL_ODR_USED (old_decl))
7802 instantiate_decl (old_decl, /*defer_ok=*/true,
7803 /*expl_inst_class_mem_p=*/false);
7809 /* Indicate that the old function template is a partial
7811 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7812 = new_friend_result_template_info;
7814 gcc_assert (new_template
7815 == most_general_template (new_template));
7816 gcc_assert (new_template != old_decl);
7818 /* Reassign any specializations already in the hash table
7819 to the new more general template, and add the
7820 additional template args. */
7821 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7825 tree spec = TREE_VALUE (t);
7828 elt.tmpl = old_decl;
7829 elt.args = DECL_TI_ARGS (spec);
7830 elt.spec = NULL_TREE;
7832 htab_remove_elt (decl_specializations, &elt);
7835 = add_outermost_template_args (new_args,
7836 DECL_TI_ARGS (spec));
7838 register_specialization
7839 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7842 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7846 /* The information from NEW_FRIEND has been merged into OLD_DECL
7847 by duplicate_decls. */
7848 new_friend = old_decl;
7853 tree context = DECL_CONTEXT (new_friend);
7857 template <class T> class C {
7858 template <class U> friend void C1<U>::f (); // case 1
7859 friend void C2<T>::f (); // case 2
7861 we only need to make sure CONTEXT is a complete type for
7862 case 2. To distinguish between the two cases, we note that
7863 CONTEXT of case 1 remains dependent type after tsubst while
7864 this isn't true for case 2. */
7865 ++processing_template_decl;
7866 dependent_p = dependent_type_p (context);
7867 --processing_template_decl;
7870 && !complete_type_or_else (context, NULL_TREE))
7871 return error_mark_node;
7873 if (COMPLETE_TYPE_P (context))
7875 /* Check to see that the declaration is really present, and,
7876 possibly obtain an improved declaration. */
7877 tree fn = check_classfn (context,
7878 new_friend, NULL_TREE);
7888 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7889 template arguments, as for tsubst.
7891 Returns an appropriate tsubst'd friend type or error_mark_node on
7895 tsubst_friend_class (tree friend_tmpl, tree args)
7901 context = CP_DECL_CONTEXT (friend_tmpl);
7903 if (context != global_namespace)
7905 if (TREE_CODE (context) == NAMESPACE_DECL)
7906 push_nested_namespace (context);
7908 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7911 /* Look for a class template declaration. We look for hidden names
7912 because two friend declarations of the same template are the
7913 same. For example, in:
7916 template <typename> friend class F;
7918 template <typename> struct B {
7919 template <typename> friend class F;
7922 both F templates are the same. */
7923 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7924 /*block_p=*/true, 0,
7925 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7927 /* But, if we don't find one, it might be because we're in a
7928 situation like this:
7936 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7937 for `S<int>', not the TEMPLATE_DECL. */
7938 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7940 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7941 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7944 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7946 /* The friend template has already been declared. Just
7947 check to see that the declarations match, and install any new
7948 default parameters. We must tsubst the default parameters,
7949 of course. We only need the innermost template parameters
7950 because that is all that redeclare_class_template will look
7952 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7953 > TMPL_ARGS_DEPTH (args))
7956 location_t saved_input_location;
7957 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7958 args, tf_warning_or_error);
7960 saved_input_location = input_location;
7961 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7962 redeclare_class_template (TREE_TYPE (tmpl), parms);
7963 input_location = saved_input_location;
7967 friend_type = TREE_TYPE (tmpl);
7971 /* The friend template has not already been declared. In this
7972 case, the instantiation of the template class will cause the
7973 injection of this template into the global scope. */
7974 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7975 if (tmpl == error_mark_node)
7976 return error_mark_node;
7978 /* The new TMPL is not an instantiation of anything, so we
7979 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7980 the new type because that is supposed to be the corresponding
7981 template decl, i.e., TMPL. */
7982 DECL_USE_TEMPLATE (tmpl) = 0;
7983 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7984 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7985 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7986 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7988 /* Inject this template into the global scope. */
7989 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7992 if (context != global_namespace)
7994 if (TREE_CODE (context) == NAMESPACE_DECL)
7995 pop_nested_namespace (context);
7997 pop_nested_class ();
8003 /* Returns zero if TYPE cannot be completed later due to circularity.
8004 Otherwise returns one. */
8007 can_complete_type_without_circularity (tree type)
8009 if (type == NULL_TREE || type == error_mark_node)
8011 else if (COMPLETE_TYPE_P (type))
8013 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
8014 return can_complete_type_without_circularity (TREE_TYPE (type));
8015 else if (CLASS_TYPE_P (type)
8016 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
8022 /* Apply any attributes which had to be deferred until instantiation
8023 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8024 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8027 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
8028 tree args, tsubst_flags_t complain, tree in_decl)
8030 tree last_dep = NULL_TREE;
8034 for (t = attributes; t; t = TREE_CHAIN (t))
8035 if (ATTR_IS_DEPENDENT (t))
8038 attributes = copy_list (attributes);
8042 if (DECL_P (*decl_p))
8044 if (TREE_TYPE (*decl_p) == error_mark_node)
8046 p = &DECL_ATTRIBUTES (*decl_p);
8049 p = &TYPE_ATTRIBUTES (*decl_p);
8053 tree late_attrs = NULL_TREE;
8054 tree *q = &late_attrs;
8056 for (*p = attributes; *p; )
8059 if (ATTR_IS_DEPENDENT (t))
8061 *p = TREE_CHAIN (t);
8062 TREE_CHAIN (t) = NULL_TREE;
8063 /* If the first attribute argument is an identifier, don't
8064 pass it through tsubst. Attributes like mode, format,
8065 cleanup and several target specific attributes expect it
8068 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
8069 && TREE_VALUE (TREE_VALUE (t))
8070 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
8071 == IDENTIFIER_NODE))
8074 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
8076 /*integral_constant_expression_p=*/false);
8077 if (chain != TREE_CHAIN (TREE_VALUE (t)))
8079 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
8084 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
8085 /*integral_constant_expression_p=*/false);
8087 q = &TREE_CHAIN (t);
8090 p = &TREE_CHAIN (t);
8093 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
8097 /* Perform (or defer) access check for typedefs that were referenced
8098 from within the template TMPL code.
8099 This is a subroutine of instantiate_template and instantiate_class_template.
8100 TMPL is the template to consider and TARGS is the list of arguments of
8104 perform_typedefs_access_check (tree tmpl, tree targs)
8106 location_t saved_location;
8108 qualified_typedef_usage_t *iter;
8111 || (!CLASS_TYPE_P (tmpl)
8112 && TREE_CODE (tmpl) != FUNCTION_DECL))
8115 saved_location = input_location;
8116 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
8117 get_types_needing_access_check (tmpl),
8120 tree type_decl = iter->typedef_decl;
8121 tree type_scope = iter->context;
8123 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
8126 if (uses_template_parms (type_decl))
8127 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
8128 if (uses_template_parms (type_scope))
8129 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
8131 /* Make access check error messages point to the location
8132 of the use of the typedef. */
8133 input_location = iter->locus;
8134 perform_or_defer_access_check (TYPE_BINFO (type_scope),
8135 type_decl, type_decl);
8137 input_location = saved_location;
8141 instantiate_class_template_1 (tree type)
8143 tree templ, args, pattern, t, member;
8147 unsigned int saved_maximum_field_alignment;
8149 if (type == error_mark_node)
8150 return error_mark_node;
8152 if (COMPLETE_OR_OPEN_TYPE_P (type)
8153 || uses_template_parms (type))
8156 /* Figure out which template is being instantiated. */
8157 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
8158 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
8160 /* Determine what specialization of the original template to
8162 t = most_specialized_class (type, templ, tf_warning_or_error);
8163 if (t == error_mark_node)
8165 TYPE_BEING_DEFINED (type) = 1;
8166 return error_mark_node;
8170 /* This TYPE is actually an instantiation of a partial
8171 specialization. We replace the innermost set of ARGS with
8172 the arguments appropriate for substitution. For example,
8175 template <class T> struct S {};
8176 template <class T> struct S<T*> {};
8178 and supposing that we are instantiating S<int*>, ARGS will
8179 presently be {int*} -- but we need {int}. */
8180 pattern = TREE_TYPE (t);
8181 args = TREE_PURPOSE (t);
8185 pattern = TREE_TYPE (templ);
8186 args = CLASSTYPE_TI_ARGS (type);
8189 /* If the template we're instantiating is incomplete, then clearly
8190 there's nothing we can do. */
8191 if (!COMPLETE_TYPE_P (pattern))
8194 /* If we've recursively instantiated too many templates, stop. */
8195 if (! push_tinst_level (type))
8198 /* Now we're really doing the instantiation. Mark the type as in
8199 the process of being defined. */
8200 TYPE_BEING_DEFINED (type) = 1;
8202 /* We may be in the middle of deferred access check. Disable
8204 push_deferring_access_checks (dk_no_deferred);
8206 push_to_top_level ();
8207 /* Use #pragma pack from the template context. */
8208 saved_maximum_field_alignment = maximum_field_alignment;
8209 maximum_field_alignment = TYPE_PRECISION (pattern);
8211 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8213 /* Set the input location to the most specialized template definition.
8214 This is needed if tsubsting causes an error. */
8215 typedecl = TYPE_MAIN_DECL (pattern);
8216 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
8217 DECL_SOURCE_LOCATION (typedecl);
8219 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
8220 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
8221 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
8222 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
8223 TYPE_HAS_COPY_ASSIGN (type) = TYPE_HAS_COPY_ASSIGN (pattern);
8224 TYPE_HAS_CONST_COPY_ASSIGN (type) = TYPE_HAS_CONST_COPY_ASSIGN (pattern);
8225 TYPE_HAS_COPY_CTOR (type) = TYPE_HAS_COPY_CTOR (pattern);
8226 TYPE_HAS_CONST_COPY_CTOR (type) = TYPE_HAS_CONST_COPY_CTOR (pattern);
8227 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
8228 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
8229 TYPE_PACKED (type) = TYPE_PACKED (pattern);
8230 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
8231 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
8232 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
8233 if (ANON_AGGR_TYPE_P (pattern))
8234 SET_ANON_AGGR_TYPE_P (type);
8235 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
8237 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
8238 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
8240 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
8242 pbinfo = TYPE_BINFO (pattern);
8244 /* We should never instantiate a nested class before its enclosing
8245 class; we need to look up the nested class by name before we can
8246 instantiate it, and that lookup should instantiate the enclosing
8248 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
8249 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
8251 base_list = NULL_TREE;
8252 if (BINFO_N_BASE_BINFOS (pbinfo))
8258 /* We must enter the scope containing the type, as that is where
8259 the accessibility of types named in dependent bases are
8261 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
8263 /* Substitute into each of the bases to determine the actual
8265 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
8268 tree access = BINFO_BASE_ACCESS (pbinfo, i);
8269 tree expanded_bases = NULL_TREE;
8272 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
8275 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
8276 args, tf_error, NULL_TREE);
8277 if (expanded_bases == error_mark_node)
8280 len = TREE_VEC_LENGTH (expanded_bases);
8283 for (idx = 0; idx < len; idx++)
8286 /* Extract the already-expanded base class. */
8287 base = TREE_VEC_ELT (expanded_bases, idx);
8289 /* Substitute to figure out the base class. */
8290 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
8293 if (base == error_mark_node)
8296 base_list = tree_cons (access, base, base_list);
8297 if (BINFO_VIRTUAL_P (pbase_binfo))
8298 TREE_TYPE (base_list) = integer_type_node;
8302 /* The list is now in reverse order; correct that. */
8303 base_list = nreverse (base_list);
8306 pop_scope (pushed_scope);
8308 /* Now call xref_basetypes to set up all the base-class
8310 xref_basetypes (type, base_list);
8312 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
8313 (int) ATTR_FLAG_TYPE_IN_PLACE,
8314 args, tf_error, NULL_TREE);
8315 fixup_attribute_variants (type);
8317 /* Now that our base classes are set up, enter the scope of the
8318 class, so that name lookups into base classes, etc. will work
8319 correctly. This is precisely analogous to what we do in
8320 begin_class_definition when defining an ordinary non-template
8321 class, except we also need to push the enclosing classes. */
8322 push_nested_class (type);
8324 /* Now members are processed in the order of declaration. */
8325 for (member = CLASSTYPE_DECL_LIST (pattern);
8326 member; member = TREE_CHAIN (member))
8328 tree t = TREE_VALUE (member);
8330 if (TREE_PURPOSE (member))
8334 /* Build new CLASSTYPE_NESTED_UTDS. */
8337 bool class_template_p;
8339 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
8340 && TYPE_LANG_SPECIFIC (t)
8341 && CLASSTYPE_IS_TEMPLATE (t));
8342 /* If the member is a class template, then -- even after
8343 substitution -- there may be dependent types in the
8344 template argument list for the class. We increment
8345 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8346 that function will assume that no types are dependent
8347 when outside of a template. */
8348 if (class_template_p)
8349 ++processing_template_decl;
8350 newtag = tsubst (t, args, tf_error, NULL_TREE);
8351 if (class_template_p)
8352 --processing_template_decl;
8353 if (newtag == error_mark_node)
8356 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
8358 tree name = TYPE_IDENTIFIER (t);
8360 if (class_template_p)
8361 /* Unfortunately, lookup_template_class sets
8362 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8363 instantiation (i.e., for the type of a member
8364 template class nested within a template class.)
8365 This behavior is required for
8366 maybe_process_partial_specialization to work
8367 correctly, but is not accurate in this case;
8368 the TAG is not an instantiation of anything.
8369 (The corresponding TEMPLATE_DECL is an
8370 instantiation, but the TYPE is not.) */
8371 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
8373 /* Now, we call pushtag to put this NEWTAG into the scope of
8374 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8375 pushtag calling push_template_decl. We don't have to do
8376 this for enums because it will already have been done in
8379 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
8380 pushtag (name, newtag, /*tag_scope=*/ts_current);
8383 else if (TREE_CODE (t) == FUNCTION_DECL
8384 || DECL_FUNCTION_TEMPLATE_P (t))
8386 /* Build new TYPE_METHODS. */
8389 if (TREE_CODE (t) == TEMPLATE_DECL)
8390 ++processing_template_decl;
8391 r = tsubst (t, args, tf_error, NULL_TREE);
8392 if (TREE_CODE (t) == TEMPLATE_DECL)
8393 --processing_template_decl;
8394 set_current_access_from_decl (r);
8395 finish_member_declaration (r);
8399 /* Build new TYPE_FIELDS. */
8400 if (TREE_CODE (t) == STATIC_ASSERT)
8403 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
8404 tf_warning_or_error, NULL_TREE,
8405 /*integral_constant_expression_p=*/true);
8406 finish_static_assert (condition,
8407 STATIC_ASSERT_MESSAGE (t),
8408 STATIC_ASSERT_SOURCE_LOCATION (t),
8411 else if (TREE_CODE (t) != CONST_DECL)
8415 /* The file and line for this declaration, to
8416 assist in error message reporting. Since we
8417 called push_tinst_level above, we don't need to
8419 input_location = DECL_SOURCE_LOCATION (t);
8421 if (TREE_CODE (t) == TEMPLATE_DECL)
8422 ++processing_template_decl;
8423 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
8424 if (TREE_CODE (t) == TEMPLATE_DECL)
8425 --processing_template_decl;
8426 if (TREE_CODE (r) == VAR_DECL)
8430 [t]he initialization (and any associated
8431 side-effects) of a static data member does
8432 not occur unless the static data member is
8433 itself used in a way that requires the
8434 definition of the static data member to
8437 Therefore, we do not substitute into the
8438 initialized for the static data member here. */
8439 finish_static_data_member_decl
8442 /*init_const_expr_p=*/false,
8443 /*asmspec_tree=*/NULL_TREE,
8446 else if (TREE_CODE (r) == FIELD_DECL)
8448 /* Determine whether R has a valid type and can be
8449 completed later. If R is invalid, then it is
8450 replaced by error_mark_node so that it will not be
8451 added to TYPE_FIELDS. */
8452 tree rtype = TREE_TYPE (r);
8453 if (can_complete_type_without_circularity (rtype))
8454 complete_type (rtype);
8456 if (!COMPLETE_TYPE_P (rtype))
8458 cxx_incomplete_type_error (r, rtype);
8459 r = error_mark_node;
8463 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8464 such a thing will already have been added to the field
8465 list by tsubst_enum in finish_member_declaration in the
8466 CLASSTYPE_NESTED_UTDS case above. */
8467 if (!(TREE_CODE (r) == TYPE_DECL
8468 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
8469 && DECL_ARTIFICIAL (r)))
8471 set_current_access_from_decl (r);
8472 finish_member_declaration (r);
8479 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
8481 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8483 tree friend_type = t;
8484 bool adjust_processing_template_decl = false;
8486 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8488 /* template <class T> friend class C; */
8489 friend_type = tsubst_friend_class (friend_type, args);
8490 adjust_processing_template_decl = true;
8492 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
8494 /* template <class T> friend class C::D; */
8495 friend_type = tsubst (friend_type, args,
8496 tf_warning_or_error, NULL_TREE);
8497 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
8498 friend_type = TREE_TYPE (friend_type);
8499 adjust_processing_template_decl = true;
8501 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
8502 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
8504 /* This could be either
8508 when dependent_type_p is false or
8510 template <class U> friend class T::C;
8513 friend_type = tsubst (friend_type, args,
8514 tf_warning_or_error, NULL_TREE);
8515 /* Bump processing_template_decl for correct
8516 dependent_type_p calculation. */
8517 ++processing_template_decl;
8518 if (dependent_type_p (friend_type))
8519 adjust_processing_template_decl = true;
8520 --processing_template_decl;
8522 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
8523 && hidden_name_p (TYPE_NAME (friend_type)))
8527 where C hasn't been declared yet. Let's lookup name
8528 from namespace scope directly, bypassing any name that
8529 come from dependent base class. */
8530 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
8532 /* The call to xref_tag_from_type does injection for friend
8534 push_nested_namespace (ns);
8536 xref_tag_from_type (friend_type, NULL_TREE,
8537 /*tag_scope=*/ts_current);
8538 pop_nested_namespace (ns);
8540 else if (uses_template_parms (friend_type))
8541 /* friend class C<T>; */
8542 friend_type = tsubst (friend_type, args,
8543 tf_warning_or_error, NULL_TREE);
8548 where C is already declared or
8550 friend class C<int>;
8552 We don't have to do anything in these cases. */
8554 if (adjust_processing_template_decl)
8555 /* Trick make_friend_class into realizing that the friend
8556 we're adding is a template, not an ordinary class. It's
8557 important that we use make_friend_class since it will
8558 perform some error-checking and output cross-reference
8560 ++processing_template_decl;
8562 if (friend_type != error_mark_node)
8563 make_friend_class (type, friend_type, /*complain=*/false);
8565 if (adjust_processing_template_decl)
8566 --processing_template_decl;
8570 /* Build new DECL_FRIENDLIST. */
8573 /* The file and line for this declaration, to
8574 assist in error message reporting. Since we
8575 called push_tinst_level above, we don't need to
8577 input_location = DECL_SOURCE_LOCATION (t);
8579 if (TREE_CODE (t) == TEMPLATE_DECL)
8581 ++processing_template_decl;
8582 push_deferring_access_checks (dk_no_check);
8585 r = tsubst_friend_function (t, args);
8586 add_friend (type, r, /*complain=*/false);
8587 if (TREE_CODE (t) == TEMPLATE_DECL)
8589 pop_deferring_access_checks ();
8590 --processing_template_decl;
8596 if (CLASSTYPE_LAMBDA_EXPR (type))
8597 maybe_add_lambda_conv_op (type);
8599 /* Set the file and line number information to whatever is given for
8600 the class itself. This puts error messages involving generated
8601 implicit functions at a predictable point, and the same point
8602 that would be used for non-template classes. */
8603 input_location = DECL_SOURCE_LOCATION (typedecl);
8605 unreverse_member_declarations (type);
8606 finish_struct_1 (type);
8607 TYPE_BEING_DEFINED (type) = 0;
8609 /* We don't instantiate default arguments for member functions. 14.7.1:
8611 The implicit instantiation of a class template specialization causes
8612 the implicit instantiation of the declarations, but not of the
8613 definitions or default arguments, of the class member functions,
8614 member classes, static data members and member templates.... */
8616 /* Some typedefs referenced from within the template code need to be access
8617 checked at template instantiation time, i.e now. These types were
8618 added to the template at parsing time. Let's get those and perform
8619 the access checks then. */
8620 perform_typedefs_access_check (pattern, args);
8621 perform_deferred_access_checks ();
8622 pop_nested_class ();
8623 maximum_field_alignment = saved_maximum_field_alignment;
8624 pop_from_top_level ();
8625 pop_deferring_access_checks ();
8628 /* The vtable for a template class can be emitted in any translation
8629 unit in which the class is instantiated. When there is no key
8630 method, however, finish_struct_1 will already have added TYPE to
8631 the keyed_classes list. */
8632 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
8633 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
8638 /* Wrapper for instantiate_class_template_1. */
8641 instantiate_class_template (tree type)
8644 timevar_push (TV_TEMPLATE_INST);
8645 ret = instantiate_class_template_1 (type);
8646 timevar_pop (TV_TEMPLATE_INST);
8651 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8657 else if (TYPE_P (t))
8658 r = tsubst (t, args, complain, in_decl);
8661 if (!(complain & tf_warning))
8662 ++c_inhibit_evaluation_warnings;
8663 r = tsubst_expr (t, args, complain, in_decl,
8664 /*integral_constant_expression_p=*/true);
8665 if (!(complain & tf_warning))
8666 --c_inhibit_evaluation_warnings;
8671 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8672 NONTYPE_ARGUMENT_PACK. */
8675 make_fnparm_pack (tree spec_parm)
8677 /* Collect all of the extra "packed" parameters into an
8681 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
8682 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
8683 int i, len = list_length (spec_parm);
8685 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8686 parmvec = make_tree_vec (len);
8687 parmtypevec = make_tree_vec (len);
8688 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
8690 TREE_VEC_ELT (parmvec, i) = spec_parm;
8691 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
8694 /* Build the argument packs. */
8695 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
8696 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
8697 TREE_TYPE (argpack) = argtypepack;
8702 /* Substitute ARGS into T, which is an pack expansion
8703 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8704 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8705 (if only a partial substitution could be performed) or
8706 ERROR_MARK_NODE if there was an error. */
8708 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
8712 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
8716 htab_t saved_local_specializations = NULL;
8718 gcc_assert (PACK_EXPANSION_P (t));
8719 pattern = PACK_EXPANSION_PATTERN (t);
8721 /* Determine the argument packs that will instantiate the parameter
8722 packs used in the expansion expression. While we're at it,
8723 compute the number of arguments to be expanded and make sure it
8725 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
8726 pack = TREE_CHAIN (pack))
8728 tree parm_pack = TREE_VALUE (pack);
8729 tree arg_pack = NULL_TREE;
8730 tree orig_arg = NULL_TREE;
8732 if (TREE_CODE (parm_pack) == PARM_DECL)
8734 if (!cp_unevaluated_operand)
8735 arg_pack = retrieve_local_specialization (parm_pack);
8738 /* We can't rely on local_specializations for a parameter
8739 name used later in a function declaration (such as in a
8740 late-specified return type). Even if it exists, it might
8741 have the wrong value for a recursive call. Just make a
8742 dummy decl, since it's only used for its type. */
8743 arg_pack = tsubst_decl (parm_pack, args, complain);
8744 if (arg_pack && FUNCTION_PARAMETER_PACK_P (arg_pack))
8745 /* Partial instantiation of the parm_pack, we can't build
8746 up an argument pack yet. */
8747 arg_pack = NULL_TREE;
8749 arg_pack = make_fnparm_pack (arg_pack);
8754 int level, idx, levels;
8755 template_parm_level_and_index (parm_pack, &level, &idx);
8757 levels = TMPL_ARGS_DEPTH (args);
8758 if (level <= levels)
8759 arg_pack = TMPL_ARG (args, level, idx);
8762 orig_arg = arg_pack;
8763 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
8764 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
8766 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
8767 /* This can only happen if we forget to expand an argument
8768 pack somewhere else. Just return an error, silently. */
8770 result = make_tree_vec (1);
8771 TREE_VEC_ELT (result, 0) = error_mark_node;
8775 /* For clarity in the comments below let's use the
8776 representation 'argument_pack<elements>' to denote an
8777 argument pack and its elements.
8779 In the 'if' block below, we want to detect cases where
8780 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
8781 check if ARG_PACK is an argument pack which sole element is
8782 the expansion of PARM_PACK. That argument pack is typically
8783 created by template_parm_to_arg when passed a parameter
8786 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
8787 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
8789 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
8790 tree pattern = PACK_EXPANSION_PATTERN (expansion);
8791 /* So we have an argument_pack<P...>. We want to test if P
8792 is actually PARM_PACK. We will not use cp_tree_equal to
8793 test P and PARM_PACK because during type fixup (by
8794 fixup_template_parm) P can be a pre-fixup version of a
8795 type and PARM_PACK be its post-fixup version.
8796 cp_tree_equal would consider them as different even
8797 though we would want to consider them compatible for our
8798 precise purpose here.
8800 Thus we are going to consider that P and PARM_PACK are
8801 compatible if they have the same DECL. */
8802 if ((/* If ARG_PACK is a type parameter pack named by the
8803 same DECL as parm_pack ... */
8805 && TYPE_P (parm_pack)
8806 && TYPE_NAME (pattern) == TYPE_NAME (parm_pack))
8807 /* ... or if ARG_PACK is a non-type parameter
8808 named by the same DECL as parm_pack ... */
8809 || (TREE_CODE (pattern) == TEMPLATE_PARM_INDEX
8810 && TREE_CODE (parm_pack) == PARM_DECL
8811 && TEMPLATE_PARM_DECL (pattern)
8812 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack))))
8813 && template_parameter_pack_p (pattern))
8814 /* ... then the argument pack that the parameter maps to
8815 is just an expansion of the parameter itself, such as
8816 one would find in the implicit typedef of a class
8817 inside the class itself. Consider this parameter
8818 "unsubstituted", so that we will maintain the outer
8820 arg_pack = NULL_TREE;
8826 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8828 /* It's all-or-nothing with incomplete argument packs. */
8829 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8830 return error_mark_node;
8832 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8837 else if (len != my_len)
8840 /* We got explicit args for some packs but not others;
8841 do nothing now and try again after deduction. */
8843 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8844 error ("mismatched argument pack lengths while expanding "
8848 error ("mismatched argument pack lengths while expanding "
8851 return error_mark_node;
8854 /* Keep track of the parameter packs and their corresponding
8856 packs = tree_cons (parm_pack, arg_pack, packs);
8857 TREE_TYPE (packs) = orig_arg;
8860 /* We can't substitute for this parameter pack. */
8861 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8863 unsubstituted_packs);
8866 /* We cannot expand this expansion expression, because we don't have
8867 all of the argument packs we need. Substitute into the pattern
8868 and return a PACK_EXPANSION_*. The caller will need to deal with
8870 if (unsubstituted_packs)
8873 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8874 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8875 /*integral_constant_expression_p=*/false);
8877 new_pat = tsubst (pattern, args, complain, in_decl);
8878 return make_pack_expansion (new_pat);
8881 /* We could not find any argument packs that work. */
8883 return error_mark_node;
8885 if (cp_unevaluated_operand)
8887 /* We're in a late-specified return type, so create our own local
8888 specializations table; the current table is either NULL or (in the
8889 case of recursive unification) might have bindings that we don't
8890 want to use or alter. */
8891 saved_local_specializations = local_specializations;
8892 local_specializations = htab_create (37,
8893 hash_local_specialization,
8894 eq_local_specializations,
8898 /* For each argument in each argument pack, substitute into the
8900 result = make_tree_vec (len + incomplete);
8901 for (i = 0; i < len + incomplete; ++i)
8903 /* For parameter pack, change the substitution of the parameter
8904 pack to the ith argument in its argument pack, then expand
8906 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8908 tree parm = TREE_PURPOSE (pack);
8910 if (TREE_CODE (parm) == PARM_DECL)
8912 /* Select the Ith argument from the pack. */
8913 tree arg = make_node (ARGUMENT_PACK_SELECT);
8914 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8915 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8917 register_local_specialization (arg, parm);
8923 template_parm_level_and_index (parm, &level, &idx);
8927 /* Select the Ith argument from the pack. */
8928 value = make_node (ARGUMENT_PACK_SELECT);
8929 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8930 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8933 /* Update the corresponding argument. */
8934 TMPL_ARG (args, level, idx) = value;
8938 /* Substitute into the PATTERN with the altered arguments. */
8939 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8940 TREE_VEC_ELT (result, i) =
8941 tsubst_expr (pattern, args, complain, in_decl,
8942 /*integral_constant_expression_p=*/false);
8944 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8947 /* When we have incomplete argument packs, the last "expanded"
8948 result is itself a pack expansion, which allows us
8949 to deduce more arguments. */
8950 TREE_VEC_ELT (result, i) =
8951 make_pack_expansion (TREE_VEC_ELT (result, i));
8953 if (TREE_VEC_ELT (result, i) == error_mark_node)
8955 result = error_mark_node;
8960 /* Update ARGS to restore the substitution from parameter packs to
8961 their argument packs. */
8962 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8964 tree parm = TREE_PURPOSE (pack);
8966 if (TREE_CODE (parm) == PARM_DECL)
8967 register_local_specialization (TREE_TYPE (pack), parm);
8971 template_parm_level_and_index (parm, &level, &idx);
8973 /* Update the corresponding argument. */
8974 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8975 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8978 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8982 if (saved_local_specializations)
8984 htab_delete (local_specializations);
8985 local_specializations = saved_local_specializations;
8991 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8992 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8993 parameter packs; all parms generated from a function parameter pack will
8994 have the same DECL_PARM_INDEX. */
8997 get_pattern_parm (tree parm, tree tmpl)
8999 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
9002 if (DECL_ARTIFICIAL (parm))
9004 for (patparm = DECL_ARGUMENTS (pattern);
9005 patparm; patparm = DECL_CHAIN (patparm))
9006 if (DECL_ARTIFICIAL (patparm)
9007 && DECL_NAME (parm) == DECL_NAME (patparm))
9012 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
9013 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
9014 gcc_assert (DECL_PARM_INDEX (patparm)
9015 == DECL_PARM_INDEX (parm));
9021 /* Substitute ARGS into the vector or list of template arguments T. */
9024 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9027 int len, need_new = 0, i, expanded_len_adjust = 0, out;
9030 if (t == error_mark_node)
9031 return error_mark_node;
9033 len = TREE_VEC_LENGTH (t);
9034 elts = XALLOCAVEC (tree, len);
9036 for (i = 0; i < len; i++)
9038 tree orig_arg = TREE_VEC_ELT (t, i);
9041 if (TREE_CODE (orig_arg) == TREE_VEC)
9042 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
9043 else if (PACK_EXPANSION_P (orig_arg))
9045 /* Substitute into an expansion expression. */
9046 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
9048 if (TREE_CODE (new_arg) == TREE_VEC)
9049 /* Add to the expanded length adjustment the number of
9050 expanded arguments. We subtract one from this
9051 measurement, because the argument pack expression
9052 itself is already counted as 1 in
9053 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9054 the argument pack is empty. */
9055 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
9057 else if (ARGUMENT_PACK_P (orig_arg))
9059 /* Substitute into each of the arguments. */
9060 new_arg = TYPE_P (orig_arg)
9061 ? cxx_make_type (TREE_CODE (orig_arg))
9062 : make_node (TREE_CODE (orig_arg));
9064 SET_ARGUMENT_PACK_ARGS (
9066 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
9067 args, complain, in_decl));
9069 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
9070 new_arg = error_mark_node;
9072 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
9073 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
9075 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
9077 if (TREE_TYPE (new_arg) == error_mark_node)
9078 new_arg = error_mark_node;
9082 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
9084 if (new_arg == error_mark_node)
9085 return error_mark_node;
9088 if (new_arg != orig_arg)
9095 /* Make space for the expanded arguments coming from template
9097 t = make_tree_vec (len + expanded_len_adjust);
9098 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9099 arguments for a member template.
9100 In that case each TREE_VEC in ORIG_T represents a level of template
9101 arguments, and ORIG_T won't carry any non defaulted argument count.
9102 It will rather be the nested TREE_VECs that will carry one.
9103 In other words, ORIG_T carries a non defaulted argument count only
9104 if it doesn't contain any nested TREE_VEC. */
9105 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
9107 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
9108 count += expanded_len_adjust;
9109 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
9111 for (i = 0, out = 0; i < len; i++)
9113 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
9114 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
9115 && TREE_CODE (elts[i]) == TREE_VEC)
9119 /* Now expand the template argument pack "in place". */
9120 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
9121 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
9125 TREE_VEC_ELT (t, out) = elts[i];
9133 /* Return the result of substituting ARGS into the template parameters
9134 given by PARMS. If there are m levels of ARGS and m + n levels of
9135 PARMS, then the result will contain n levels of PARMS. For
9136 example, if PARMS is `template <class T> template <class U>
9137 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9138 result will be `template <int*, double, class V>'. */
9141 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
9146 /* When substituting into a template, we must set
9147 PROCESSING_TEMPLATE_DECL as the template parameters may be
9148 dependent if they are based on one-another, and the dependency
9149 predicates are short-circuit outside of templates. */
9150 ++processing_template_decl;
9152 for (new_parms = &r;
9153 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
9154 new_parms = &(TREE_CHAIN (*new_parms)),
9155 parms = TREE_CHAIN (parms))
9158 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
9161 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
9165 if (parms == error_mark_node)
9168 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
9170 if (tuple == error_mark_node)
9173 TREE_VEC_ELT (new_vec, i) =
9174 tsubst_template_parm (tuple, args, complain);
9178 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
9179 - TMPL_ARGS_DEPTH (args)),
9180 new_vec, NULL_TREE);
9183 --processing_template_decl;
9188 /* Return the result of substituting ARGS into one template parameter
9189 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9190 parameter and which TREE_PURPOSE is the default argument of the
9191 template parameter. */
9194 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
9196 tree default_value, parm_decl;
9198 if (args == NULL_TREE
9200 || t == error_mark_node)
9203 gcc_assert (TREE_CODE (t) == TREE_LIST);
9205 default_value = TREE_PURPOSE (t);
9206 parm_decl = TREE_VALUE (t);
9208 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
9209 if (TREE_CODE (parm_decl) == PARM_DECL
9210 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
9211 parm_decl = error_mark_node;
9212 default_value = tsubst_template_arg (default_value, args,
9213 complain, NULL_TREE);
9215 return build_tree_list (default_value, parm_decl);
9218 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9219 type T. If T is not an aggregate or enumeration type, it is
9220 handled as if by tsubst. IN_DECL is as for tsubst. If
9221 ENTERING_SCOPE is nonzero, T is the context for a template which
9222 we are presently tsubst'ing. Return the substituted value. */
9225 tsubst_aggr_type (tree t,
9227 tsubst_flags_t complain,
9234 switch (TREE_CODE (t))
9237 if (TYPE_PTRMEMFUNC_P (t))
9238 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
9240 /* Else fall through. */
9243 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
9248 int saved_unevaluated_operand;
9249 int saved_inhibit_evaluation_warnings;
9251 /* In "sizeof(X<I>)" we need to evaluate "I". */
9252 saved_unevaluated_operand = cp_unevaluated_operand;
9253 cp_unevaluated_operand = 0;
9254 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
9255 c_inhibit_evaluation_warnings = 0;
9257 /* First, determine the context for the type we are looking
9259 context = TYPE_CONTEXT (t);
9262 context = tsubst_aggr_type (context, args, complain,
9263 in_decl, /*entering_scope=*/1);
9264 /* If context is a nested class inside a class template,
9265 it may still need to be instantiated (c++/33959). */
9266 if (TYPE_P (context))
9267 context = complete_type (context);
9270 /* Then, figure out what arguments are appropriate for the
9271 type we are trying to find. For example, given:
9273 template <class T> struct S;
9274 template <class T, class U> void f(T, U) { S<U> su; }
9276 and supposing that we are instantiating f<int, double>,
9277 then our ARGS will be {int, double}, but, when looking up
9278 S we only want {double}. */
9279 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
9281 if (argvec == error_mark_node)
9282 r = error_mark_node;
9285 r = lookup_template_class (t, argvec, in_decl, context,
9286 entering_scope, complain);
9287 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
9290 cp_unevaluated_operand = saved_unevaluated_operand;
9291 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
9296 /* This is not a template type, so there's nothing to do. */
9300 return tsubst (t, args, complain, in_decl);
9304 /* Substitute into the default argument ARG (a default argument for
9305 FN), which has the indicated TYPE. */
9308 tsubst_default_argument (tree fn, tree type, tree arg)
9310 tree saved_class_ptr = NULL_TREE;
9311 tree saved_class_ref = NULL_TREE;
9313 /* This can happen in invalid code. */
9314 if (TREE_CODE (arg) == DEFAULT_ARG)
9317 /* This default argument came from a template. Instantiate the
9318 default argument here, not in tsubst. In the case of
9327 we must be careful to do name lookup in the scope of S<T>,
9328 rather than in the current class. */
9329 push_access_scope (fn);
9330 /* The "this" pointer is not valid in a default argument. */
9333 saved_class_ptr = current_class_ptr;
9334 cp_function_chain->x_current_class_ptr = NULL_TREE;
9335 saved_class_ref = current_class_ref;
9336 cp_function_chain->x_current_class_ref = NULL_TREE;
9339 push_deferring_access_checks(dk_no_deferred);
9340 /* The default argument expression may cause implicitly defined
9341 member functions to be synthesized, which will result in garbage
9342 collection. We must treat this situation as if we were within
9343 the body of function so as to avoid collecting live data on the
9346 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
9347 tf_warning_or_error, NULL_TREE,
9348 /*integral_constant_expression_p=*/false);
9350 pop_deferring_access_checks();
9352 /* Restore the "this" pointer. */
9355 cp_function_chain->x_current_class_ptr = saved_class_ptr;
9356 cp_function_chain->x_current_class_ref = saved_class_ref;
9359 /* Make sure the default argument is reasonable. */
9360 arg = check_default_argument (type, arg);
9362 pop_access_scope (fn);
9367 /* Substitute into all the default arguments for FN. */
9370 tsubst_default_arguments (tree fn)
9375 tmpl_args = DECL_TI_ARGS (fn);
9377 /* If this function is not yet instantiated, we certainly don't need
9378 its default arguments. */
9379 if (uses_template_parms (tmpl_args))
9381 /* Don't do this again for clones. */
9382 if (DECL_CLONED_FUNCTION_P (fn))
9385 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
9387 arg = TREE_CHAIN (arg))
9388 if (TREE_PURPOSE (arg))
9389 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9391 TREE_PURPOSE (arg));
9394 /* Substitute the ARGS into the T, which is a _DECL. Return the
9395 result of the substitution. Issue error and warning messages under
9396 control of COMPLAIN. */
9399 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
9401 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9402 location_t saved_loc;
9407 /* Set the filename and linenumber to improve error-reporting. */
9408 saved_loc = input_location;
9409 input_location = DECL_SOURCE_LOCATION (t);
9411 switch (TREE_CODE (t))
9415 /* We can get here when processing a member function template,
9416 member class template, or template template parameter. */
9417 tree decl = DECL_TEMPLATE_RESULT (t);
9422 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9424 /* Template template parameter is treated here. */
9425 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9426 if (new_type == error_mark_node)
9427 RETURN (error_mark_node);
9430 DECL_CHAIN (r) = NULL_TREE;
9431 TREE_TYPE (r) = new_type;
9432 DECL_TEMPLATE_RESULT (r)
9433 = build_decl (DECL_SOURCE_LOCATION (decl),
9434 TYPE_DECL, DECL_NAME (decl), new_type);
9435 DECL_TEMPLATE_PARMS (r)
9436 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9438 TYPE_NAME (new_type) = r;
9442 /* We might already have an instance of this template.
9443 The ARGS are for the surrounding class type, so the
9444 full args contain the tsubst'd args for the context,
9445 plus the innermost args from the template decl. */
9446 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
9447 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
9448 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
9449 /* Because this is a template, the arguments will still be
9450 dependent, even after substitution. If
9451 PROCESSING_TEMPLATE_DECL is not set, the dependency
9452 predicates will short-circuit. */
9453 ++processing_template_decl;
9454 full_args = tsubst_template_args (tmpl_args, args,
9456 --processing_template_decl;
9457 if (full_args == error_mark_node)
9458 RETURN (error_mark_node);
9460 /* If this is a default template template argument,
9461 tsubst might not have changed anything. */
9462 if (full_args == tmpl_args)
9465 hash = hash_tmpl_and_args (t, full_args);
9466 spec = retrieve_specialization (t, full_args, hash);
9467 if (spec != NULL_TREE)
9473 /* Make a new template decl. It will be similar to the
9474 original, but will record the current template arguments.
9475 We also create a new function declaration, which is just
9476 like the old one, but points to this new template, rather
9477 than the old one. */
9479 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
9480 DECL_CHAIN (r) = NULL_TREE;
9482 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
9484 if (TREE_CODE (decl) == TYPE_DECL)
9487 ++processing_template_decl;
9488 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9489 --processing_template_decl;
9490 if (new_type == error_mark_node)
9491 RETURN (error_mark_node);
9493 TREE_TYPE (r) = new_type;
9494 CLASSTYPE_TI_TEMPLATE (new_type) = r;
9495 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
9496 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
9497 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
9502 ++processing_template_decl;
9503 new_decl = tsubst (decl, args, complain, in_decl);
9504 --processing_template_decl;
9505 if (new_decl == error_mark_node)
9506 RETURN (error_mark_node);
9508 DECL_TEMPLATE_RESULT (r) = new_decl;
9509 DECL_TI_TEMPLATE (new_decl) = r;
9510 TREE_TYPE (r) = TREE_TYPE (new_decl);
9511 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
9512 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
9515 SET_DECL_IMPLICIT_INSTANTIATION (r);
9516 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
9517 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
9519 /* The template parameters for this new template are all the
9520 template parameters for the old template, except the
9521 outermost level of parameters. */
9522 DECL_TEMPLATE_PARMS (r)
9523 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
9526 if (PRIMARY_TEMPLATE_P (t))
9527 DECL_PRIMARY_TEMPLATE (r) = r;
9529 if (TREE_CODE (decl) != TYPE_DECL)
9530 /* Record this non-type partial instantiation. */
9531 register_specialization (r, t,
9532 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
9540 tree argvec = NULL_TREE;
9548 /* Nobody should be tsubst'ing into non-template functions. */
9549 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
9551 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
9556 /* If T is not dependent, just return it. We have to
9557 increment PROCESSING_TEMPLATE_DECL because
9558 value_dependent_expression_p assumes that nothing is
9559 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9560 ++processing_template_decl;
9561 dependent_p = value_dependent_expression_p (t);
9562 --processing_template_decl;
9566 /* Calculate the most general template of which R is a
9567 specialization, and the complete set of arguments used to
9569 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
9570 argvec = tsubst_template_args (DECL_TI_ARGS
9571 (DECL_TEMPLATE_RESULT
9572 (DECL_TI_TEMPLATE (t))),
9573 args, complain, in_decl);
9574 if (argvec == error_mark_node)
9575 RETURN (error_mark_node);
9577 /* Check to see if we already have this specialization. */
9578 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9579 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9587 /* We can see more levels of arguments than parameters if
9588 there was a specialization of a member template, like
9591 template <class T> struct S { template <class U> void f(); }
9592 template <> template <class U> void S<int>::f(U);
9594 Here, we'll be substituting into the specialization,
9595 because that's where we can find the code we actually
9596 want to generate, but we'll have enough arguments for
9597 the most general template.
9599 We also deal with the peculiar case:
9601 template <class T> struct S {
9602 template <class U> friend void f();
9604 template <class U> void f() {}
9606 template void f<double>();
9608 Here, the ARGS for the instantiation of will be {int,
9609 double}. But, we only need as many ARGS as there are
9610 levels of template parameters in CODE_PATTERN. We are
9611 careful not to get fooled into reducing the ARGS in
9614 template <class T> struct S { template <class U> void f(U); }
9615 template <class T> template <> void S<T>::f(int) {}
9617 which we can spot because the pattern will be a
9618 specialization in this case. */
9619 args_depth = TMPL_ARGS_DEPTH (args);
9621 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
9622 if (args_depth > parms_depth
9623 && !DECL_TEMPLATE_SPECIALIZATION (t))
9624 args = get_innermost_template_args (args, parms_depth);
9628 /* This special case arises when we have something like this:
9630 template <class T> struct S {
9631 friend void f<int>(int, double);
9634 Here, the DECL_TI_TEMPLATE for the friend declaration
9635 will be an IDENTIFIER_NODE. We are being called from
9636 tsubst_friend_function, and we want only to create a
9637 new decl (R) with appropriate types so that we can call
9638 determine_specialization. */
9639 gen_tmpl = NULL_TREE;
9642 if (DECL_CLASS_SCOPE_P (t))
9644 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
9648 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
9649 complain, t, /*entering_scope=*/1);
9654 ctx = DECL_CONTEXT (t);
9656 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9657 if (type == error_mark_node)
9658 RETURN (error_mark_node);
9660 /* We do NOT check for matching decls pushed separately at this
9661 point, as they may not represent instantiations of this
9662 template, and in any case are considered separate under the
9665 DECL_USE_TEMPLATE (r) = 0;
9666 TREE_TYPE (r) = type;
9667 /* Clear out the mangled name and RTL for the instantiation. */
9668 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9669 SET_DECL_RTL (r, NULL);
9670 /* Leave DECL_INITIAL set on deleted instantiations. */
9671 if (!DECL_DELETED_FN (r))
9672 DECL_INITIAL (r) = NULL_TREE;
9673 DECL_CONTEXT (r) = ctx;
9675 if (member && DECL_CONV_FN_P (r))
9676 /* Type-conversion operator. Reconstruct the name, in
9677 case it's the name of one of the template's parameters. */
9678 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
9680 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
9682 DECL_RESULT (r) = NULL_TREE;
9684 TREE_STATIC (r) = 0;
9685 TREE_PUBLIC (r) = TREE_PUBLIC (t);
9686 DECL_EXTERNAL (r) = 1;
9687 /* If this is an instantiation of a function with internal
9688 linkage, we already know what object file linkage will be
9689 assigned to the instantiation. */
9690 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
9691 DECL_DEFER_OUTPUT (r) = 0;
9692 DECL_CHAIN (r) = NULL_TREE;
9693 DECL_PENDING_INLINE_INFO (r) = 0;
9694 DECL_PENDING_INLINE_P (r) = 0;
9695 DECL_SAVED_TREE (r) = NULL_TREE;
9696 DECL_STRUCT_FUNCTION (r) = NULL;
9698 /* We'll re-clone as appropriate in instantiate_template. */
9699 DECL_CLONED_FUNCTION (r) = NULL_TREE;
9701 /* If we aren't complaining now, return on error before we register
9702 the specialization so that we'll complain eventually. */
9703 if ((complain & tf_error) == 0
9704 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9705 && !grok_op_properties (r, /*complain=*/false))
9706 RETURN (error_mark_node);
9708 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9709 this in the special friend case mentioned above where
9710 GEN_TMPL is NULL. */
9713 DECL_TEMPLATE_INFO (r)
9714 = build_template_info (gen_tmpl, argvec);
9715 SET_DECL_IMPLICIT_INSTANTIATION (r);
9716 register_specialization (r, gen_tmpl, argvec, false, hash);
9718 /* We're not supposed to instantiate default arguments
9719 until they are called, for a template. But, for a
9722 template <class T> void f ()
9723 { extern void g(int i = T()); }
9725 we should do the substitution when the template is
9726 instantiated. We handle the member function case in
9727 instantiate_class_template since the default arguments
9728 might refer to other members of the class. */
9730 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9731 && !uses_template_parms (argvec))
9732 tsubst_default_arguments (r);
9735 DECL_TEMPLATE_INFO (r) = NULL_TREE;
9737 /* Copy the list of befriending classes. */
9738 for (friends = &DECL_BEFRIENDING_CLASSES (r);
9740 friends = &TREE_CHAIN (*friends))
9742 *friends = copy_node (*friends);
9743 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
9748 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
9750 maybe_retrofit_in_chrg (r);
9751 if (DECL_CONSTRUCTOR_P (r))
9752 grok_ctor_properties (ctx, r);
9753 /* If this is an instantiation of a member template, clone it.
9754 If it isn't, that'll be handled by
9755 clone_constructors_and_destructors. */
9756 if (PRIMARY_TEMPLATE_P (gen_tmpl))
9757 clone_function_decl (r, /*update_method_vec_p=*/0);
9759 else if ((complain & tf_error) != 0
9760 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
9761 && !grok_op_properties (r, /*complain=*/true))
9762 RETURN (error_mark_node);
9764 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
9765 SET_DECL_FRIEND_CONTEXT (r,
9766 tsubst (DECL_FRIEND_CONTEXT (t),
9767 args, complain, in_decl));
9769 /* Possibly limit visibility based on template args. */
9770 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9771 if (DECL_VISIBILITY_SPECIFIED (t))
9773 DECL_VISIBILITY_SPECIFIED (r) = 0;
9775 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9777 determine_visibility (r);
9778 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
9779 && !processing_template_decl)
9780 defaulted_late_check (r);
9782 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9783 args, complain, in_decl);
9789 tree type = NULL_TREE;
9791 tree expanded_types = NULL_TREE;
9792 tree prev_r = NULL_TREE;
9793 tree first_r = NULL_TREE;
9795 if (FUNCTION_PARAMETER_PACK_P (t))
9797 /* If there is a local specialization that isn't a
9798 parameter pack, it means that we're doing a "simple"
9799 substitution from inside tsubst_pack_expansion. Just
9800 return the local specialization (which will be a single
9802 tree spec = retrieve_local_specialization (t);
9804 && TREE_CODE (spec) == PARM_DECL
9805 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
9808 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9809 the parameters in this function parameter pack. */
9810 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
9812 if (TREE_CODE (expanded_types) == TREE_VEC)
9814 len = TREE_VEC_LENGTH (expanded_types);
9816 /* Zero-length parameter packs are boring. Just substitute
9819 RETURN (tsubst (TREE_CHAIN (t), args, complain,
9824 /* All we did was update the type. Make a note of that. */
9825 type = expanded_types;
9826 expanded_types = NULL_TREE;
9830 /* Loop through all of the parameter's we'll build. When T is
9831 a function parameter pack, LEN is the number of expanded
9832 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9834 for (i = 0; i < len; ++i)
9838 if (DECL_TEMPLATE_PARM_P (t))
9839 SET_DECL_TEMPLATE_PARM_P (r);
9842 /* We're on the Ith parameter of the function parameter
9845 /* An argument of a function parameter pack is not a parameter
9847 FUNCTION_PARAMETER_PACK_P (r) = false;
9849 /* Get the Ith type. */
9850 type = TREE_VEC_ELT (expanded_types, i);
9853 /* Rename the parameter to include the index. */
9855 make_ith_pack_parameter_name (DECL_NAME (r), i);
9858 /* We're dealing with a normal parameter. */
9859 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9861 type = type_decays_to (type);
9862 TREE_TYPE (r) = type;
9863 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9865 if (DECL_INITIAL (r))
9867 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9868 DECL_INITIAL (r) = TREE_TYPE (r);
9870 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9874 DECL_CONTEXT (r) = NULL_TREE;
9876 if (!DECL_TEMPLATE_PARM_P (r))
9877 DECL_ARG_TYPE (r) = type_passed_as (type);
9879 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9880 args, complain, in_decl);
9882 /* Keep track of the first new parameter we
9883 generate. That's what will be returned to the
9888 /* Build a proper chain of parameters when substituting
9889 into a function parameter pack. */
9891 DECL_CHAIN (prev_r) = r;
9895 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
9896 complain, DECL_CHAIN (t));
9898 /* FIRST_R contains the start of the chain we've built. */
9908 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9909 if (type == error_mark_node)
9910 RETURN (error_mark_node);
9911 TREE_TYPE (r) = type;
9912 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9914 /* DECL_INITIAL gives the number of bits in a bit-field. */
9916 = tsubst_expr (DECL_INITIAL (t), args,
9918 /*integral_constant_expression_p=*/true);
9919 /* We don't have to set DECL_CONTEXT here; it is set by
9920 finish_member_declaration. */
9921 DECL_CHAIN (r) = NULL_TREE;
9922 if (VOID_TYPE_P (type))
9923 error ("instantiation of %q+D as type %qT", r, type);
9925 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9926 args, complain, in_decl);
9931 /* We reach here only for member using decls. */
9932 if (DECL_DEPENDENT_P (t))
9934 r = do_class_using_decl
9935 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9936 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9938 r = error_mark_node;
9941 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9942 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9948 DECL_CHAIN (r) = NULL_TREE;
9955 tree argvec = NULL_TREE;
9956 tree gen_tmpl = NULL_TREE;
9958 tree tmpl = NULL_TREE;
9960 tree type = NULL_TREE;
9963 if (TREE_CODE (t) == TYPE_DECL
9964 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9966 /* If this is the canonical decl, we don't have to
9967 mess with instantiations, and often we can't (for
9968 typename, template type parms and such). Note that
9969 TYPE_NAME is not correct for the above test if
9970 we've copied the type for a typedef. */
9971 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9972 if (type == error_mark_node)
9973 RETURN (error_mark_node);
9974 r = TYPE_NAME (type);
9978 /* Check to see if we already have the specialization we
9981 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9983 /* T is a static data member or namespace-scope entity.
9984 We have to substitute into namespace-scope variables
9985 (even though such entities are never templates) because
9988 template <class T> void f() { extern T t; }
9990 where the entity referenced is not known until
9991 instantiation time. */
9993 ctx = DECL_CONTEXT (t);
9994 if (DECL_CLASS_SCOPE_P (t))
9996 ctx = tsubst_aggr_type (ctx, args,
9998 in_decl, /*entering_scope=*/1);
9999 /* If CTX is unchanged, then T is in fact the
10000 specialization we want. That situation occurs when
10001 referencing a static data member within in its own
10002 class. We can use pointer equality, rather than
10003 same_type_p, because DECL_CONTEXT is always
10005 if (ctx == DECL_CONTEXT (t))
10011 tmpl = DECL_TI_TEMPLATE (t);
10012 gen_tmpl = most_general_template (tmpl);
10013 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
10014 hash = hash_tmpl_and_args (gen_tmpl, argvec);
10015 spec = retrieve_specialization (gen_tmpl, argvec, hash);
10020 /* A local variable. */
10022 /* Subsequent calls to pushdecl will fill this in. */
10024 spec = retrieve_local_specialization (t);
10026 /* If we already have the specialization we need, there is
10027 nothing more to do. */
10034 /* Create a new node for the specialization we need. */
10036 if (type == NULL_TREE)
10038 if (is_typedef_decl (t))
10039 type = DECL_ORIGINAL_TYPE (t);
10041 type = TREE_TYPE (t);
10042 if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
10043 type = strip_array_domain (type);
10044 type = tsubst (type, args, complain, in_decl);
10046 if (TREE_CODE (r) == VAR_DECL)
10048 /* Even if the original location is out of scope, the
10049 newly substituted one is not. */
10050 DECL_DEAD_FOR_LOCAL (r) = 0;
10051 DECL_INITIALIZED_P (r) = 0;
10052 DECL_TEMPLATE_INSTANTIATED (r) = 0;
10053 if (type == error_mark_node)
10054 RETURN (error_mark_node);
10055 if (TREE_CODE (type) == FUNCTION_TYPE)
10057 /* It may seem that this case cannot occur, since:
10062 declares a function, not a variable. However:
10065 template <typename T> void g() { T t; }
10066 template void g<f>();
10068 is an attempt to declare a variable with function
10070 error ("variable %qD has function type",
10071 /* R is not yet sufficiently initialized, so we
10072 just use its name. */
10074 RETURN (error_mark_node);
10076 type = complete_type (type);
10077 /* Wait until cp_finish_decl to set this again, to handle
10078 circular dependency (template/instantiate6.C). */
10079 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
10080 type = check_var_type (DECL_NAME (r), type);
10082 if (DECL_HAS_VALUE_EXPR_P (t))
10084 tree ve = DECL_VALUE_EXPR (t);
10085 ve = tsubst_expr (ve, args, complain, in_decl,
10086 /*constant_expression_p=*/false);
10087 if (REFERENCE_REF_P (ve))
10089 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
10090 ve = TREE_OPERAND (ve, 0);
10092 SET_DECL_VALUE_EXPR (r, ve);
10095 else if (DECL_SELF_REFERENCE_P (t))
10096 SET_DECL_SELF_REFERENCE_P (r);
10097 TREE_TYPE (r) = type;
10098 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
10099 DECL_CONTEXT (r) = ctx;
10100 /* Clear out the mangled name and RTL for the instantiation. */
10101 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
10102 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10103 SET_DECL_RTL (r, NULL);
10104 /* The initializer must not be expanded until it is required;
10105 see [temp.inst]. */
10106 DECL_INITIAL (r) = NULL_TREE;
10107 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
10108 SET_DECL_RTL (r, NULL);
10109 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
10110 if (TREE_CODE (r) == VAR_DECL)
10112 /* Possibly limit visibility based on template args. */
10113 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
10114 if (DECL_VISIBILITY_SPECIFIED (t))
10116 DECL_VISIBILITY_SPECIFIED (r) = 0;
10117 DECL_ATTRIBUTES (r)
10118 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
10120 determine_visibility (r);
10125 /* A static data member declaration is always marked
10126 external when it is declared in-class, even if an
10127 initializer is present. We mimic the non-template
10128 processing here. */
10129 DECL_EXTERNAL (r) = 1;
10131 register_specialization (r, gen_tmpl, argvec, false, hash);
10132 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
10133 SET_DECL_IMPLICIT_INSTANTIATION (r);
10135 else if (cp_unevaluated_operand)
10137 /* We're substituting this var in a decltype outside of its
10138 scope, such as for a lambda return type. Don't add it to
10139 local_specializations, do perform auto deduction. */
10140 tree auto_node = type_uses_auto (type);
10142 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
10143 /*constant_expression_p=*/false);
10145 if (auto_node && init)
10147 init = resolve_nondeduced_context (init);
10148 TREE_TYPE (r) = type
10149 = do_auto_deduction (type, init, auto_node);
10153 register_local_specialization (r, t);
10155 DECL_CHAIN (r) = NULL_TREE;
10157 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
10159 args, complain, in_decl);
10161 /* Preserve a typedef that names a type. */
10162 if (is_typedef_decl (r))
10164 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
10165 set_underlying_type (r);
10168 layout_decl (r, 0);
10173 gcc_unreachable ();
10178 /* Restore the file and line information. */
10179 input_location = saved_loc;
10184 /* Substitute into the ARG_TYPES of a function type. */
10187 tsubst_arg_types (tree arg_types,
10189 tsubst_flags_t complain,
10192 tree remaining_arg_types;
10193 tree type = NULL_TREE;
10195 tree expanded_args = NULL_TREE;
10198 if (!arg_types || arg_types == void_list_node)
10201 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
10202 args, complain, in_decl);
10203 if (remaining_arg_types == error_mark_node)
10204 return error_mark_node;
10206 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
10208 /* For a pack expansion, perform substitution on the
10209 entire expression. Later on, we'll handle the arguments
10211 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
10212 args, complain, in_decl);
10214 if (TREE_CODE (expanded_args) == TREE_VEC)
10215 /* So that we'll spin through the parameters, one by one. */
10216 i = TREE_VEC_LENGTH (expanded_args);
10219 /* We only partially substituted into the parameter
10220 pack. Our type is TYPE_PACK_EXPANSION. */
10221 type = expanded_args;
10222 expanded_args = NULL_TREE;
10230 type = TREE_VEC_ELT (expanded_args, i);
10232 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
10234 if (type == error_mark_node)
10235 return error_mark_node;
10236 if (VOID_TYPE_P (type))
10238 if (complain & tf_error)
10240 error ("invalid parameter type %qT", type);
10242 error ("in declaration %q+D", in_decl);
10244 return error_mark_node;
10247 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10248 top-level qualifiers as required. */
10249 type = cv_unqualified (type_decays_to (type));
10251 /* We do not substitute into default arguments here. The standard
10252 mandates that they be instantiated only when needed, which is
10253 done in build_over_call. */
10254 default_arg = TREE_PURPOSE (arg_types);
10256 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
10258 /* We've instantiated a template before its default arguments
10259 have been parsed. This can happen for a nested template
10260 class, and is not an error unless we require the default
10261 argument in a call of this function. */
10262 remaining_arg_types =
10263 tree_cons (default_arg, type, remaining_arg_types);
10264 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
10265 remaining_arg_types);
10268 remaining_arg_types =
10269 hash_tree_cons (default_arg, type, remaining_arg_types);
10272 return remaining_arg_types;
10275 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10276 *not* handle the exception-specification for FNTYPE, because the
10277 initial substitution of explicitly provided template parameters
10278 during argument deduction forbids substitution into the
10279 exception-specification:
10283 All references in the function type of the function template to the
10284 corresponding template parameters are replaced by the specified tem-
10285 plate argument values. If a substitution in a template parameter or
10286 in the function type of the function template results in an invalid
10287 type, type deduction fails. [Note: The equivalent substitution in
10288 exception specifications is done only when the function is instanti-
10289 ated, at which point a program is ill-formed if the substitution
10290 results in an invalid type.] */
10293 tsubst_function_type (tree t,
10295 tsubst_flags_t complain,
10302 /* The TYPE_CONTEXT is not used for function/method types. */
10303 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
10305 /* Substitute the return type. */
10306 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10307 if (return_type == error_mark_node)
10308 return error_mark_node;
10309 /* The standard does not presently indicate that creation of a
10310 function type with an invalid return type is a deduction failure.
10311 However, that is clearly analogous to creating an array of "void"
10312 or a reference to a reference. This is core issue #486. */
10313 if (TREE_CODE (return_type) == ARRAY_TYPE
10314 || TREE_CODE (return_type) == FUNCTION_TYPE)
10316 if (complain & tf_error)
10318 if (TREE_CODE (return_type) == ARRAY_TYPE)
10319 error ("function returning an array");
10321 error ("function returning a function");
10323 return error_mark_node;
10326 /* Substitute the argument types. */
10327 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
10328 complain, in_decl);
10329 if (arg_types == error_mark_node)
10330 return error_mark_node;
10332 /* Construct a new type node and return it. */
10333 if (TREE_CODE (t) == FUNCTION_TYPE)
10335 fntype = build_function_type (return_type, arg_types);
10336 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
10340 tree r = TREE_TYPE (TREE_VALUE (arg_types));
10341 if (! MAYBE_CLASS_TYPE_P (r))
10345 Type deduction may fail for any of the following
10348 -- Attempting to create "pointer to member of T" when T
10349 is not a class type. */
10350 if (complain & tf_error)
10351 error ("creating pointer to member function of non-class type %qT",
10353 return error_mark_node;
10356 fntype = build_method_type_directly (r, return_type,
10357 TREE_CHAIN (arg_types));
10359 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
10364 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10365 ARGS into that specification, and return the substituted
10366 specification. If there is no specification, return NULL_TREE. */
10369 tsubst_exception_specification (tree fntype,
10371 tsubst_flags_t complain,
10378 specs = TYPE_RAISES_EXCEPTIONS (fntype);
10379 new_specs = NULL_TREE;
10380 if (specs && TREE_PURPOSE (specs))
10382 /* A noexcept-specifier. */
10383 tree expr = TREE_PURPOSE (specs);
10384 if (expr == boolean_true_node || expr == boolean_false_node)
10388 /* Defer instantiation of noexcept-specifiers to avoid
10389 excessive instantiations (c++/49107). */
10390 new_specs = make_node (DEFERRED_NOEXCEPT);
10391 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
10393 /* We already partially instantiated this member template,
10394 so combine the new args with the old. */
10395 DEFERRED_NOEXCEPT_PATTERN (new_specs)
10396 = DEFERRED_NOEXCEPT_PATTERN (expr);
10397 DEFERRED_NOEXCEPT_ARGS (new_specs)
10398 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
10402 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
10403 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
10407 new_specs = tsubst_copy_and_build
10408 (expr, args, complain, in_decl, /*function_p=*/false,
10409 /*integral_constant_expression_p=*/true);
10410 new_specs = build_noexcept_spec (new_specs, complain);
10414 if (! TREE_VALUE (specs))
10421 tree expanded_specs = NULL_TREE;
10423 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
10425 /* Expand the pack expansion type. */
10426 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
10430 if (expanded_specs == error_mark_node)
10431 return error_mark_node;
10432 else if (TREE_CODE (expanded_specs) == TREE_VEC)
10433 len = TREE_VEC_LENGTH (expanded_specs);
10436 /* We're substituting into a member template, so
10437 we got a TYPE_PACK_EXPANSION back. Add that
10438 expansion and move on. */
10439 gcc_assert (TREE_CODE (expanded_specs)
10440 == TYPE_PACK_EXPANSION);
10441 new_specs = add_exception_specifier (new_specs,
10444 specs = TREE_CHAIN (specs);
10449 for (i = 0; i < len; ++i)
10451 if (expanded_specs)
10452 spec = TREE_VEC_ELT (expanded_specs, i);
10454 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
10455 if (spec == error_mark_node)
10457 new_specs = add_exception_specifier (new_specs, spec,
10461 specs = TREE_CHAIN (specs);
10467 /* Take the tree structure T and replace template parameters used
10468 therein with the argument vector ARGS. IN_DECL is an associated
10469 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10470 Issue error and warning messages under control of COMPLAIN. Note
10471 that we must be relatively non-tolerant of extensions here, in
10472 order to preserve conformance; if we allow substitutions that
10473 should not be allowed, we may allow argument deductions that should
10474 not succeed, and therefore report ambiguous overload situations
10475 where there are none. In theory, we could allow the substitution,
10476 but indicate that it should have failed, and allow our caller to
10477 make sure that the right thing happens, but we don't try to do this
10480 This function is used for dealing with types, decls and the like;
10481 for expressions, use tsubst_expr or tsubst_copy. */
10484 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10486 enum tree_code code;
10489 if (t == NULL_TREE || t == error_mark_node
10490 || t == integer_type_node
10491 || t == void_type_node
10492 || t == char_type_node
10493 || t == unknown_type_node
10494 || TREE_CODE (t) == NAMESPACE_DECL
10495 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
10499 return tsubst_decl (t, args, complain);
10501 if (args == NULL_TREE)
10504 code = TREE_CODE (t);
10506 if (code == IDENTIFIER_NODE)
10507 type = IDENTIFIER_TYPE_VALUE (t);
10509 type = TREE_TYPE (t);
10511 gcc_assert (type != unknown_type_node);
10513 /* Reuse typedefs. We need to do this to handle dependent attributes,
10514 such as attribute aligned. */
10516 && typedef_variant_p (t))
10518 tree decl = TYPE_NAME (t);
10520 if (DECL_CLASS_SCOPE_P (decl)
10521 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
10522 && uses_template_parms (DECL_CONTEXT (decl)))
10524 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10525 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
10526 r = retrieve_specialization (tmpl, gen_args, 0);
10528 else if (DECL_FUNCTION_SCOPE_P (decl)
10529 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
10530 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
10531 r = retrieve_local_specialization (decl);
10533 /* The typedef is from a non-template context. */
10539 r = cp_build_qualified_type_real
10540 (r, cp_type_quals (t) | cp_type_quals (r),
10541 complain | tf_ignore_bad_quals);
10544 /* Else we must be instantiating the typedef, so fall through. */
10548 && code != TYPENAME_TYPE
10549 && code != TEMPLATE_TYPE_PARM
10550 && code != IDENTIFIER_NODE
10551 && code != FUNCTION_TYPE
10552 && code != METHOD_TYPE)
10553 type = tsubst (type, args, complain, in_decl);
10554 if (type == error_mark_node)
10555 return error_mark_node;
10561 case ENUMERAL_TYPE:
10562 return tsubst_aggr_type (t, args, complain, in_decl,
10563 /*entering_scope=*/0);
10566 case IDENTIFIER_NODE:
10577 if (t == integer_type_node)
10580 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
10581 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
10585 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
10587 max = tsubst_expr (omax, args, complain, in_decl,
10588 /*integral_constant_expression_p=*/false);
10590 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10592 if (TREE_CODE (max) == NOP_EXPR
10593 && TREE_SIDE_EFFECTS (omax)
10594 && !TREE_TYPE (max))
10595 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
10597 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10598 with TREE_SIDE_EFFECTS that indicates this is not an integral
10599 constant expression. */
10600 if (processing_template_decl
10601 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
10603 gcc_assert (TREE_CODE (max) == NOP_EXPR);
10604 TREE_SIDE_EFFECTS (max) = 1;
10607 return compute_array_index_type (NULL_TREE, max, complain);
10610 case TEMPLATE_TYPE_PARM:
10611 case TEMPLATE_TEMPLATE_PARM:
10612 case BOUND_TEMPLATE_TEMPLATE_PARM:
10613 case TEMPLATE_PARM_INDEX:
10618 tree arg = NULL_TREE;
10622 gcc_assert (TREE_VEC_LENGTH (args) > 0);
10623 template_parm_level_and_index (t, &level, &idx);
10625 levels = TMPL_ARGS_DEPTH (args);
10626 if (level <= levels)
10628 arg = TMPL_ARG (args, level, idx);
10630 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
10631 /* See through ARGUMENT_PACK_SELECT arguments. */
10632 arg = ARGUMENT_PACK_SELECT_ARG (arg);
10635 if (arg == error_mark_node)
10636 return error_mark_node;
10637 else if (arg != NULL_TREE)
10639 if (ARGUMENT_PACK_P (arg))
10640 /* If ARG is an argument pack, we don't actually want to
10641 perform a substitution here, because substitutions
10642 for argument packs are only done
10643 element-by-element. We can get to this point when
10644 substituting the type of a non-type template
10645 parameter pack, when that type actually contains
10646 template parameter packs from an outer template, e.g.,
10648 template<typename... Types> struct A {
10649 template<Types... Values> struct B { };
10653 if (code == TEMPLATE_TYPE_PARM)
10656 gcc_assert (TYPE_P (arg));
10658 quals = cp_type_quals (arg) | cp_type_quals (t);
10660 return cp_build_qualified_type_real
10661 (arg, quals, complain | tf_ignore_bad_quals);
10663 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10665 /* We are processing a type constructed from a
10666 template template parameter. */
10667 tree argvec = tsubst (TYPE_TI_ARGS (t),
10668 args, complain, in_decl);
10669 if (argvec == error_mark_node)
10670 return error_mark_node;
10672 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10673 are resolving nested-types in the signature of a
10674 member function templates. Otherwise ARG is a
10675 TEMPLATE_DECL and is the real template to be
10677 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
10678 arg = TYPE_NAME (arg);
10680 r = lookup_template_class (arg,
10682 DECL_CONTEXT (arg),
10683 /*entering_scope=*/0,
10685 return cp_build_qualified_type_real
10686 (r, cp_type_quals (t), complain);
10689 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10690 return unshare_expr (arg);
10694 /* This can happen during the attempted tsubst'ing in
10695 unify. This means that we don't yet have any information
10696 about the template parameter in question. */
10699 /* If we get here, we must have been looking at a parm for a
10700 more deeply nested template. Make a new version of this
10701 template parameter, but with a lower level. */
10704 case TEMPLATE_TYPE_PARM:
10705 case TEMPLATE_TEMPLATE_PARM:
10706 case BOUND_TEMPLATE_TEMPLATE_PARM:
10707 if (cp_type_quals (t))
10709 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
10710 r = cp_build_qualified_type_real
10711 (r, cp_type_quals (t),
10712 complain | (code == TEMPLATE_TYPE_PARM
10713 ? tf_ignore_bad_quals : 0));
10718 TEMPLATE_TYPE_PARM_INDEX (r)
10719 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
10720 r, levels, args, complain);
10721 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
10722 TYPE_MAIN_VARIANT (r) = r;
10723 TYPE_POINTER_TO (r) = NULL_TREE;
10724 TYPE_REFERENCE_TO (r) = NULL_TREE;
10726 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
10727 /* We have reduced the level of the template
10728 template parameter, but not the levels of its
10729 template parameters, so canonical_type_parameter
10730 will not be able to find the canonical template
10731 template parameter for this level. Thus, we
10732 require structural equality checking to compare
10733 TEMPLATE_TEMPLATE_PARMs. */
10734 SET_TYPE_STRUCTURAL_EQUALITY (r);
10735 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
10736 SET_TYPE_STRUCTURAL_EQUALITY (r);
10738 TYPE_CANONICAL (r) = canonical_type_parameter (r);
10740 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
10742 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
10743 complain, in_decl);
10744 if (argvec == error_mark_node)
10745 return error_mark_node;
10747 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
10748 = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
10753 case TEMPLATE_PARM_INDEX:
10754 r = reduce_template_parm_level (t, type, levels, args, complain);
10758 gcc_unreachable ();
10766 tree purpose, value, chain;
10768 if (t == void_list_node)
10771 purpose = TREE_PURPOSE (t);
10774 purpose = tsubst (purpose, args, complain, in_decl);
10775 if (purpose == error_mark_node)
10776 return error_mark_node;
10778 value = TREE_VALUE (t);
10781 value = tsubst (value, args, complain, in_decl);
10782 if (value == error_mark_node)
10783 return error_mark_node;
10785 chain = TREE_CHAIN (t);
10786 if (chain && chain != void_type_node)
10788 chain = tsubst (chain, args, complain, in_decl);
10789 if (chain == error_mark_node)
10790 return error_mark_node;
10792 if (purpose == TREE_PURPOSE (t)
10793 && value == TREE_VALUE (t)
10794 && chain == TREE_CHAIN (t))
10796 return hash_tree_cons (purpose, value, chain);
10800 /* We should never be tsubsting a binfo. */
10801 gcc_unreachable ();
10804 /* A vector of template arguments. */
10805 gcc_assert (!type);
10806 return tsubst_template_args (t, args, complain, in_decl);
10809 case REFERENCE_TYPE:
10811 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
10816 Type deduction may fail for any of the following
10819 -- Attempting to create a pointer to reference type.
10820 -- Attempting to create a reference to a reference type or
10821 a reference to void.
10823 Core issue 106 says that creating a reference to a reference
10824 during instantiation is no longer a cause for failure. We
10825 only enforce this check in strict C++98 mode. */
10826 if ((TREE_CODE (type) == REFERENCE_TYPE
10827 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
10828 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
10830 static location_t last_loc;
10832 /* We keep track of the last time we issued this error
10833 message to avoid spewing a ton of messages during a
10834 single bad template instantiation. */
10835 if (complain & tf_error
10836 && last_loc != input_location)
10838 if (TREE_CODE (type) == VOID_TYPE)
10839 error ("forming reference to void");
10840 else if (code == POINTER_TYPE)
10841 error ("forming pointer to reference type %qT", type);
10843 error ("forming reference to reference type %qT", type);
10844 last_loc = input_location;
10847 return error_mark_node;
10849 else if (code == POINTER_TYPE)
10851 r = build_pointer_type (type);
10852 if (TREE_CODE (type) == METHOD_TYPE)
10853 r = build_ptrmemfunc_type (r);
10855 else if (TREE_CODE (type) == REFERENCE_TYPE)
10856 /* In C++0x, during template argument substitution, when there is an
10857 attempt to create a reference to a reference type, reference
10858 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10860 "If a template-argument for a template-parameter T names a type
10861 that is a reference to a type A, an attempt to create the type
10862 'lvalue reference to cv T' creates the type 'lvalue reference to
10863 A,' while an attempt to create the type type rvalue reference to
10864 cv T' creates the type T"
10866 r = cp_build_reference_type
10868 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
10870 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
10871 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
10873 if (r != error_mark_node)
10874 /* Will this ever be needed for TYPE_..._TO values? */
10881 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10882 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10886 Type deduction may fail for any of the following
10889 -- Attempting to create "pointer to member of T" when T
10890 is not a class type. */
10891 if (complain & tf_error)
10892 error ("creating pointer to member of non-class type %qT", r);
10893 return error_mark_node;
10895 if (TREE_CODE (type) == REFERENCE_TYPE)
10897 if (complain & tf_error)
10898 error ("creating pointer to member reference type %qT", type);
10899 return error_mark_node;
10901 if (TREE_CODE (type) == VOID_TYPE)
10903 if (complain & tf_error)
10904 error ("creating pointer to member of type void");
10905 return error_mark_node;
10907 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10908 if (TREE_CODE (type) == FUNCTION_TYPE)
10910 /* The type of the implicit object parameter gets its
10911 cv-qualifiers from the FUNCTION_TYPE. */
10913 tree method_type = build_memfn_type (type, r, type_memfn_quals (type));
10914 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10915 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10919 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10923 case FUNCTION_TYPE:
10928 fntype = tsubst_function_type (t, args, complain, in_decl);
10929 if (fntype == error_mark_node)
10930 return error_mark_node;
10932 /* Substitute the exception specification. */
10933 specs = tsubst_exception_specification (t, args, complain,
10934 in_decl, /*defer_ok*/true);
10935 if (specs == error_mark_node)
10936 return error_mark_node;
10938 fntype = build_exception_variant (fntype, specs);
10943 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10944 if (domain == error_mark_node)
10945 return error_mark_node;
10947 /* As an optimization, we avoid regenerating the array type if
10948 it will obviously be the same as T. */
10949 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10952 /* These checks should match the ones in grokdeclarator.
10956 The deduction may fail for any of the following reasons:
10958 -- Attempting to create an array with an element type that
10959 is void, a function type, or a reference type, or [DR337]
10960 an abstract class type. */
10961 if (TREE_CODE (type) == VOID_TYPE
10962 || TREE_CODE (type) == FUNCTION_TYPE
10963 || TREE_CODE (type) == REFERENCE_TYPE)
10965 if (complain & tf_error)
10966 error ("creating array of %qT", type);
10967 return error_mark_node;
10969 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10971 if (complain & tf_error)
10972 error ("creating array of %qT, which is an abstract class type",
10974 return error_mark_node;
10977 r = build_cplus_array_type (type, domain);
10979 if (TYPE_USER_ALIGN (t))
10981 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10982 TYPE_USER_ALIGN (r) = 1;
10988 case TYPENAME_TYPE:
10990 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10991 in_decl, /*entering_scope=*/1);
10992 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10993 complain, in_decl);
10995 if (ctx == error_mark_node || f == error_mark_node)
10996 return error_mark_node;
10998 if (!MAYBE_CLASS_TYPE_P (ctx))
11000 if (complain & tf_error)
11001 error ("%qT is not a class, struct, or union type", ctx);
11002 return error_mark_node;
11004 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
11006 /* Normally, make_typename_type does not require that the CTX
11007 have complete type in order to allow things like:
11009 template <class T> struct S { typename S<T>::X Y; };
11011 But, such constructs have already been resolved by this
11012 point, so here CTX really should have complete type, unless
11013 it's a partial instantiation. */
11014 ctx = complete_type (ctx);
11015 if (!COMPLETE_TYPE_P (ctx))
11017 if (complain & tf_error)
11018 cxx_incomplete_type_error (NULL_TREE, ctx);
11019 return error_mark_node;
11023 f = make_typename_type (ctx, f, typename_type,
11024 (complain & tf_error) | tf_keep_type_decl);
11025 if (f == error_mark_node)
11027 if (TREE_CODE (f) == TYPE_DECL)
11029 complain |= tf_ignore_bad_quals;
11033 if (TREE_CODE (f) != TYPENAME_TYPE)
11035 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
11037 if (complain & tf_error)
11038 error ("%qT resolves to %qT, which is not an enumeration type",
11041 return error_mark_node;
11043 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
11045 if (complain & tf_error)
11046 error ("%qT resolves to %qT, which is is not a class type",
11049 return error_mark_node;
11053 return cp_build_qualified_type_real
11054 (f, cp_type_quals (f) | cp_type_quals (t), complain);
11057 case UNBOUND_CLASS_TEMPLATE:
11059 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
11060 in_decl, /*entering_scope=*/1);
11061 tree name = TYPE_IDENTIFIER (t);
11062 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
11064 if (ctx == error_mark_node || name == error_mark_node)
11065 return error_mark_node;
11068 parm_list = tsubst_template_parms (parm_list, args, complain);
11069 return make_unbound_class_template (ctx, name, parm_list, complain);
11076 ++cp_unevaluated_operand;
11077 ++c_inhibit_evaluation_warnings;
11079 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
11081 /*integral_constant_expression_p=*/false);
11083 --cp_unevaluated_operand;
11084 --c_inhibit_evaluation_warnings;
11086 type = finish_typeof (type);
11087 return cp_build_qualified_type_real (type,
11089 | cp_type_quals (type),
11093 case DECLTYPE_TYPE:
11097 ++cp_unevaluated_operand;
11098 ++c_inhibit_evaluation_warnings;
11100 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
11102 /*integral_constant_expression_p=*/false);
11104 --cp_unevaluated_operand;
11105 --c_inhibit_evaluation_warnings;
11107 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
11108 type = lambda_capture_field_type (type);
11109 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
11110 type = lambda_return_type (type);
11112 type = finish_decltype_type
11113 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
11114 return cp_build_qualified_type_real (type,
11116 | cp_type_quals (type),
11120 case UNDERLYING_TYPE:
11122 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
11123 complain, in_decl);
11124 return finish_underlying_type (type);
11127 case TYPE_ARGUMENT_PACK:
11128 case NONTYPE_ARGUMENT_PACK:
11130 tree r = TYPE_P (t) ? cxx_make_type (code) : make_node (code);
11132 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
11136 SET_ARGUMENT_PACK_ARGS (r, packed_out);
11138 /* For template nontype argument packs, also substitute into
11140 if (code == NONTYPE_ARGUMENT_PACK)
11141 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
11159 /* We should use one of the expression tsubsts for these codes. */
11160 gcc_unreachable ();
11163 sorry ("use of %qs in template", tree_code_name [(int) code]);
11164 return error_mark_node;
11168 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11169 type of the expression on the left-hand side of the "." or "->"
11173 tsubst_baselink (tree baselink, tree object_type,
11174 tree args, tsubst_flags_t complain, tree in_decl)
11177 tree qualifying_scope;
11180 tree template_args = 0;
11181 bool template_id_p = false;
11183 /* A baselink indicates a function from a base class. Both the
11184 BASELINK_ACCESS_BINFO and the base class referenced may
11185 indicate bases of the template class, rather than the
11186 instantiated class. In addition, lookups that were not
11187 ambiguous before may be ambiguous now. Therefore, we perform
11188 the lookup again. */
11189 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
11190 qualifying_scope = tsubst (qualifying_scope, args,
11191 complain, in_decl);
11192 fns = BASELINK_FUNCTIONS (baselink);
11193 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
11194 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11196 template_id_p = true;
11197 template_args = TREE_OPERAND (fns, 1);
11198 fns = TREE_OPERAND (fns, 0);
11200 template_args = tsubst_template_args (template_args, args,
11201 complain, in_decl);
11203 name = DECL_NAME (get_first_fn (fns));
11204 if (IDENTIFIER_TYPENAME_P (name))
11205 name = mangle_conv_op_name_for_type (optype);
11206 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
11208 return error_mark_node;
11210 /* If lookup found a single function, mark it as used at this
11211 point. (If it lookup found multiple functions the one selected
11212 later by overload resolution will be marked as used at that
11214 if (BASELINK_P (baselink))
11215 fns = BASELINK_FUNCTIONS (baselink);
11216 if (!template_id_p && !really_overloaded_fn (fns))
11217 mark_used (OVL_CURRENT (fns));
11219 /* Add back the template arguments, if present. */
11220 if (BASELINK_P (baselink) && template_id_p)
11221 BASELINK_FUNCTIONS (baselink)
11222 = build_nt (TEMPLATE_ID_EXPR,
11223 BASELINK_FUNCTIONS (baselink),
11225 /* Update the conversion operator type. */
11226 BASELINK_OPTYPE (baselink) = optype;
11229 object_type = current_class_type;
11230 return adjust_result_of_qualified_name_lookup (baselink,
11235 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11236 true if the qualified-id will be a postfix-expression in-and-of
11237 itself; false if more of the postfix-expression follows the
11238 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11242 tsubst_qualified_id (tree qualified_id, tree args,
11243 tsubst_flags_t complain, tree in_decl,
11244 bool done, bool address_p)
11250 tree template_args;
11252 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
11254 /* Figure out what name to look up. */
11255 name = TREE_OPERAND (qualified_id, 1);
11256 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
11258 is_template = true;
11259 template_args = TREE_OPERAND (name, 1);
11261 template_args = tsubst_template_args (template_args, args,
11262 complain, in_decl);
11263 name = TREE_OPERAND (name, 0);
11267 is_template = false;
11268 template_args = NULL_TREE;
11271 /* Substitute into the qualifying scope. When there are no ARGS, we
11272 are just trying to simplify a non-dependent expression. In that
11273 case the qualifying scope may be dependent, and, in any case,
11274 substituting will not help. */
11275 scope = TREE_OPERAND (qualified_id, 0);
11278 scope = tsubst (scope, args, complain, in_decl);
11279 expr = tsubst_copy (name, args, complain, in_decl);
11284 if (dependent_scope_p (scope))
11285 return build_qualified_name (NULL_TREE, scope, expr,
11286 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
11288 if (!BASELINK_P (name) && !DECL_P (expr))
11290 if (TREE_CODE (expr) == BIT_NOT_EXPR)
11292 /* A BIT_NOT_EXPR is used to represent a destructor. */
11293 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
11295 error ("qualifying type %qT does not match destructor name ~%qT",
11296 scope, TREE_OPERAND (expr, 0));
11297 expr = error_mark_node;
11300 expr = lookup_qualified_name (scope, complete_dtor_identifier,
11301 /*is_type_p=*/0, false);
11304 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
11305 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
11306 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
11308 if (complain & tf_error)
11310 error ("dependent-name %qE is parsed as a non-type, but "
11311 "instantiation yields a type", qualified_id);
11312 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
11314 return error_mark_node;
11320 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
11322 /* Remember that there was a reference to this entity. */
11326 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
11328 if (complain & tf_error)
11329 qualified_name_lookup_error (scope,
11330 TREE_OPERAND (qualified_id, 1),
11331 expr, input_location);
11332 return error_mark_node;
11336 expr = lookup_template_function (expr, template_args);
11338 if (expr == error_mark_node && complain & tf_error)
11339 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
11340 expr, input_location);
11341 else if (TYPE_P (scope))
11343 expr = (adjust_result_of_qualified_name_lookup
11344 (expr, scope, current_class_type));
11345 expr = (finish_qualified_id_expr
11346 (scope, expr, done, address_p,
11347 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
11348 /*template_arg_p=*/false));
11351 /* Expressions do not generally have reference type. */
11352 if (TREE_CODE (expr) != SCOPE_REF
11353 /* However, if we're about to form a pointer-to-member, we just
11354 want the referenced member referenced. */
11355 && TREE_CODE (expr) != OFFSET_REF)
11356 expr = convert_from_reference (expr);
11361 /* Like tsubst, but deals with expressions. This function just replaces
11362 template parms; to finish processing the resultant expression, use
11366 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11368 enum tree_code code;
11371 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
11374 code = TREE_CODE (t);
11379 r = retrieve_local_specialization (t);
11384 /* This can happen for a parameter name used later in a function
11385 declaration (such as in a late-specified return type). Just
11386 make a dummy decl, since it's only used for its type. */
11387 gcc_assert (cp_unevaluated_operand != 0);
11388 /* We copy T because want to tsubst the PARM_DECL only,
11389 not the following PARM_DECLs that are chained to T. */
11391 r = tsubst_decl (c, args, complain);
11392 /* Give it the template pattern as its context; its true context
11393 hasn't been instantiated yet and this is good enough for
11395 DECL_CONTEXT (r) = DECL_CONTEXT (t);
11398 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
11399 r = ARGUMENT_PACK_SELECT_ARG (r);
11408 if (DECL_TEMPLATE_PARM_P (t))
11409 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
11410 /* There is no need to substitute into namespace-scope
11412 if (DECL_NAMESPACE_SCOPE_P (t))
11414 /* If ARGS is NULL, then T is known to be non-dependent. */
11415 if (args == NULL_TREE)
11416 return integral_constant_value (t);
11418 /* Unfortunately, we cannot just call lookup_name here.
11421 template <int I> int f() {
11423 struct S { void g() { E e = a; } };
11426 When we instantiate f<7>::S::g(), say, lookup_name is not
11427 clever enough to find f<7>::a. */
11429 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
11430 /*entering_scope=*/0);
11432 for (v = TYPE_VALUES (enum_type);
11434 v = TREE_CHAIN (v))
11435 if (TREE_PURPOSE (v) == DECL_NAME (t))
11436 return TREE_VALUE (v);
11438 /* We didn't find the name. That should never happen; if
11439 name-lookup found it during preliminary parsing, we
11440 should find it again here during instantiation. */
11441 gcc_unreachable ();
11446 if (DECL_CONTEXT (t))
11450 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
11451 /*entering_scope=*/1);
11452 if (ctx != DECL_CONTEXT (t))
11454 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
11457 if (complain & tf_error)
11458 error ("using invalid field %qD", t);
11459 return error_mark_node;
11468 case FUNCTION_DECL:
11469 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
11470 || local_variable_p (t))
11471 t = tsubst (t, args, complain, in_decl);
11476 /* An OVERLOAD will always be a non-dependent overload set; an
11477 overload set from function scope will just be represented with an
11478 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11479 gcc_assert (!uses_template_parms (t));
11483 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
11485 case TEMPLATE_DECL:
11486 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11487 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
11488 args, complain, in_decl);
11489 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
11490 return tsubst (t, args, complain, in_decl);
11491 else if (DECL_CLASS_SCOPE_P (t)
11492 && uses_template_parms (DECL_CONTEXT (t)))
11494 /* Template template argument like the following example need
11497 template <template <class> class TT> struct C {};
11498 template <class T> struct D {
11499 template <class U> struct E {};
11504 We are processing the template argument `E' in #1 for
11505 the template instantiation #2. Originally, `E' is a
11506 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11507 have to substitute this with one having context `D<int>'. */
11509 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
11510 return lookup_field (context, DECL_NAME(t), 0, false);
11513 /* Ordinary template template argument. */
11517 case REINTERPRET_CAST_EXPR:
11518 case CONST_CAST_EXPR:
11519 case STATIC_CAST_EXPR:
11520 case DYNAMIC_CAST_EXPR:
11523 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11524 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11527 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11533 ++cp_unevaluated_operand;
11534 ++c_inhibit_evaluation_warnings;
11535 /* We only want to compute the number of arguments. */
11536 expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
11537 complain, in_decl);
11538 --cp_unevaluated_operand;
11539 --c_inhibit_evaluation_warnings;
11541 if (TREE_CODE (expanded) == TREE_VEC)
11542 len = TREE_VEC_LENGTH (expanded);
11544 if (expanded == error_mark_node)
11545 return error_mark_node;
11546 else if (PACK_EXPANSION_P (expanded)
11547 || (TREE_CODE (expanded) == TREE_VEC
11549 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
11551 if (TREE_CODE (expanded) == TREE_VEC)
11552 expanded = TREE_VEC_ELT (expanded, len - 1);
11554 if (TYPE_P (expanded))
11555 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
11556 complain & tf_error);
11558 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
11559 complain & tf_error);
11562 return build_int_cst (size_type_node, len);
11568 case TRUTH_NOT_EXPR:
11571 case UNARY_PLUS_EXPR: /* Unary + */
11573 case AT_ENCODE_EXPR:
11577 case REALPART_EXPR:
11578 case IMAGPART_EXPR:
11580 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11581 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
11583 case COMPONENT_REF:
11588 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
11589 name = TREE_OPERAND (t, 1);
11590 if (TREE_CODE (name) == BIT_NOT_EXPR)
11592 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11593 complain, in_decl);
11594 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11596 else if (TREE_CODE (name) == SCOPE_REF
11597 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
11599 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
11600 complain, in_decl);
11601 name = TREE_OPERAND (name, 1);
11602 name = tsubst_copy (TREE_OPERAND (name, 0), args,
11603 complain, in_decl);
11604 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
11605 name = build_qualified_name (/*type=*/NULL_TREE,
11607 /*template_p=*/false);
11609 else if (TREE_CODE (name) == BASELINK)
11610 name = tsubst_baselink (name,
11611 non_reference (TREE_TYPE (object)),
11615 name = tsubst_copy (name, args, complain, in_decl);
11616 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
11622 case TRUNC_DIV_EXPR:
11623 case CEIL_DIV_EXPR:
11624 case FLOOR_DIV_EXPR:
11625 case ROUND_DIV_EXPR:
11626 case EXACT_DIV_EXPR:
11630 case TRUNC_MOD_EXPR:
11631 case FLOOR_MOD_EXPR:
11632 case TRUTH_ANDIF_EXPR:
11633 case TRUTH_ORIF_EXPR:
11634 case TRUTH_AND_EXPR:
11635 case TRUTH_OR_EXPR:
11648 case COMPOUND_EXPR:
11651 case PREDECREMENT_EXPR:
11652 case PREINCREMENT_EXPR:
11653 case POSTDECREMENT_EXPR:
11654 case POSTINCREMENT_EXPR:
11656 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11657 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11660 return build_qualified_name (/*type=*/NULL_TREE,
11661 tsubst_copy (TREE_OPERAND (t, 0),
11662 args, complain, in_decl),
11663 tsubst_copy (TREE_OPERAND (t, 1),
11664 args, complain, in_decl),
11665 QUALIFIED_NAME_IS_TEMPLATE (t));
11670 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11671 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11672 NULL_TREE, NULL_TREE);
11676 int n = VL_EXP_OPERAND_LENGTH (t);
11677 tree result = build_vl_exp (CALL_EXPR, n);
11679 for (i = 0; i < n; i++)
11680 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
11681 complain, in_decl);
11687 case PSEUDO_DTOR_EXPR:
11690 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11691 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11692 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11693 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11700 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11701 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
11702 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
11703 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
11710 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11711 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11712 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
11713 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
11717 case TEMPLATE_ID_EXPR:
11719 /* Substituted template arguments */
11720 tree fn = TREE_OPERAND (t, 0);
11721 tree targs = TREE_OPERAND (t, 1);
11723 fn = tsubst_copy (fn, args, complain, in_decl);
11725 targs = tsubst_template_args (targs, args, complain, in_decl);
11727 return lookup_template_function (fn, targs);
11732 tree purpose, value, chain;
11734 if (t == void_list_node)
11737 purpose = TREE_PURPOSE (t);
11739 purpose = tsubst_copy (purpose, args, complain, in_decl);
11740 value = TREE_VALUE (t);
11742 value = tsubst_copy (value, args, complain, in_decl);
11743 chain = TREE_CHAIN (t);
11744 if (chain && chain != void_type_node)
11745 chain = tsubst_copy (chain, args, complain, in_decl);
11746 if (purpose == TREE_PURPOSE (t)
11747 && value == TREE_VALUE (t)
11748 && chain == TREE_CHAIN (t))
11750 return tree_cons (purpose, value, chain);
11755 case ENUMERAL_TYPE:
11757 case TEMPLATE_TYPE_PARM:
11758 case TEMPLATE_TEMPLATE_PARM:
11759 case BOUND_TEMPLATE_TEMPLATE_PARM:
11760 case TEMPLATE_PARM_INDEX:
11762 case REFERENCE_TYPE:
11764 case FUNCTION_TYPE:
11767 case TYPENAME_TYPE:
11768 case UNBOUND_CLASS_TEMPLATE:
11770 case DECLTYPE_TYPE:
11772 return tsubst (t, args, complain, in_decl);
11774 case IDENTIFIER_NODE:
11775 if (IDENTIFIER_TYPENAME_P (t))
11777 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11778 return mangle_conv_op_name_for_type (new_type);
11784 /* This is handled by tsubst_copy_and_build. */
11785 gcc_unreachable ();
11788 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
11790 tsubst (TREE_TYPE (t), args, complain, in_decl));
11792 case CLEANUP_POINT_EXPR:
11793 /* We shouldn't have built any of these during initial template
11794 generation. Instead, they should be built during instantiation
11795 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11796 gcc_unreachable ();
11800 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
11801 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
11802 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
11803 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
11804 mark_used (TREE_OPERAND (r, 1));
11807 case EXPR_PACK_EXPANSION:
11808 error ("invalid use of pack expansion expression");
11809 return error_mark_node;
11811 case NONTYPE_ARGUMENT_PACK:
11812 error ("use %<...%> to expand argument pack");
11813 return error_mark_node;
11820 /* Instantiate any typedefs in the type. */
11821 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11822 r = fold_convert (type, t);
11823 gcc_assert (TREE_CODE (r) == code);
11828 /* These can sometimes show up in a partial instantiation, but never
11829 involve template parms. */
11830 gcc_assert (!uses_template_parms (t));
11834 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
11835 gcc_checking_assert (false);
11840 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11843 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
11846 tree new_clauses = NULL, nc, oc;
11848 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
11850 nc = copy_node (oc);
11851 OMP_CLAUSE_CHAIN (nc) = new_clauses;
11854 switch (OMP_CLAUSE_CODE (nc))
11856 case OMP_CLAUSE_LASTPRIVATE:
11857 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
11859 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
11860 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
11861 in_decl, /*integral_constant_expression_p=*/false);
11862 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
11863 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
11866 case OMP_CLAUSE_PRIVATE:
11867 case OMP_CLAUSE_SHARED:
11868 case OMP_CLAUSE_FIRSTPRIVATE:
11869 case OMP_CLAUSE_REDUCTION:
11870 case OMP_CLAUSE_COPYIN:
11871 case OMP_CLAUSE_COPYPRIVATE:
11872 case OMP_CLAUSE_IF:
11873 case OMP_CLAUSE_NUM_THREADS:
11874 case OMP_CLAUSE_SCHEDULE:
11875 case OMP_CLAUSE_COLLAPSE:
11876 OMP_CLAUSE_OPERAND (nc, 0)
11877 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
11878 in_decl, /*integral_constant_expression_p=*/false);
11880 case OMP_CLAUSE_NOWAIT:
11881 case OMP_CLAUSE_ORDERED:
11882 case OMP_CLAUSE_DEFAULT:
11883 case OMP_CLAUSE_UNTIED:
11886 gcc_unreachable ();
11890 return finish_omp_clauses (nreverse (new_clauses));
11893 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11896 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11899 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11901 tree purpose, value, chain;
11906 if (TREE_CODE (t) != TREE_LIST)
11907 return tsubst_copy_and_build (t, args, complain, in_decl,
11908 /*function_p=*/false,
11909 /*integral_constant_expression_p=*/false);
11911 if (t == void_list_node)
11914 purpose = TREE_PURPOSE (t);
11916 purpose = RECUR (purpose);
11917 value = TREE_VALUE (t);
11918 if (value && TREE_CODE (value) != LABEL_DECL)
11919 value = RECUR (value);
11920 chain = TREE_CHAIN (t);
11921 if (chain && chain != void_type_node)
11922 chain = RECUR (chain);
11923 return tree_cons (purpose, value, chain);
11927 /* Substitute one OMP_FOR iterator. */
11930 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11931 tree condv, tree incrv, tree *clauses,
11932 tree args, tsubst_flags_t complain, tree in_decl,
11933 bool integral_constant_expression_p)
11935 #define RECUR(NODE) \
11936 tsubst_expr ((NODE), args, complain, in_decl, \
11937 integral_constant_expression_p)
11938 tree decl, init, cond, incr, auto_node;
11940 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11941 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11942 decl = RECUR (TREE_OPERAND (init, 0));
11943 init = TREE_OPERAND (init, 1);
11944 auto_node = type_uses_auto (TREE_TYPE (decl));
11945 if (auto_node && init)
11947 tree init_expr = init;
11948 if (TREE_CODE (init_expr) == DECL_EXPR)
11949 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11950 init_expr = RECUR (init_expr);
11952 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11954 gcc_assert (!type_dependent_expression_p (decl));
11956 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11958 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11959 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11960 if (TREE_CODE (incr) == MODIFY_EXPR)
11961 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11962 RECUR (TREE_OPERAND (incr, 1)),
11965 incr = RECUR (incr);
11966 TREE_VEC_ELT (declv, i) = decl;
11967 TREE_VEC_ELT (initv, i) = init;
11968 TREE_VEC_ELT (condv, i) = cond;
11969 TREE_VEC_ELT (incrv, i) = incr;
11973 if (init && TREE_CODE (init) != DECL_EXPR)
11976 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11978 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11979 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11980 && OMP_CLAUSE_DECL (c) == decl)
11982 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11983 && OMP_CLAUSE_DECL (c) == decl)
11984 error ("iteration variable %qD should not be firstprivate", decl);
11985 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11986 && OMP_CLAUSE_DECL (c) == decl)
11987 error ("iteration variable %qD should not be reduction", decl);
11991 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11992 OMP_CLAUSE_DECL (c) = decl;
11993 c = finish_omp_clauses (c);
11996 OMP_CLAUSE_CHAIN (c) = *clauses;
12001 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
12002 if (COMPARISON_CLASS_P (cond))
12003 cond = build2 (TREE_CODE (cond), boolean_type_node,
12004 RECUR (TREE_OPERAND (cond, 0)),
12005 RECUR (TREE_OPERAND (cond, 1)));
12007 cond = RECUR (cond);
12008 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
12009 switch (TREE_CODE (incr))
12011 case PREINCREMENT_EXPR:
12012 case PREDECREMENT_EXPR:
12013 case POSTINCREMENT_EXPR:
12014 case POSTDECREMENT_EXPR:
12015 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
12016 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
12019 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12020 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12022 tree rhs = TREE_OPERAND (incr, 1);
12023 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12024 RECUR (TREE_OPERAND (incr, 0)),
12025 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12026 RECUR (TREE_OPERAND (rhs, 0)),
12027 RECUR (TREE_OPERAND (rhs, 1))));
12030 incr = RECUR (incr);
12033 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
12034 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
12036 tree lhs = RECUR (TREE_OPERAND (incr, 0));
12037 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
12038 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
12039 TREE_TYPE (decl), lhs,
12040 RECUR (TREE_OPERAND (incr, 2))));
12042 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
12043 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
12044 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
12046 tree rhs = TREE_OPERAND (incr, 2);
12047 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
12048 RECUR (TREE_OPERAND (incr, 0)),
12049 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
12050 RECUR (TREE_OPERAND (rhs, 0)),
12051 RECUR (TREE_OPERAND (rhs, 1))));
12054 incr = RECUR (incr);
12057 incr = RECUR (incr);
12061 TREE_VEC_ELT (declv, i) = decl;
12062 TREE_VEC_ELT (initv, i) = init;
12063 TREE_VEC_ELT (condv, i) = cond;
12064 TREE_VEC_ELT (incrv, i) = incr;
12068 /* Like tsubst_copy for expressions, etc. but also does semantic
12072 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
12073 bool integral_constant_expression_p)
12075 #define RECUR(NODE) \
12076 tsubst_expr ((NODE), args, complain, in_decl, \
12077 integral_constant_expression_p)
12081 if (t == NULL_TREE || t == error_mark_node)
12084 if (EXPR_HAS_LOCATION (t))
12085 input_location = EXPR_LOCATION (t);
12086 if (STATEMENT_CODE_P (TREE_CODE (t)))
12087 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
12089 switch (TREE_CODE (t))
12091 case STATEMENT_LIST:
12093 tree_stmt_iterator i;
12094 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
12095 RECUR (tsi_stmt (i));
12099 case CTOR_INITIALIZER:
12100 finish_mem_initializers (tsubst_initializer_list
12101 (TREE_OPERAND (t, 0), args));
12105 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
12109 tmp = RECUR (EXPR_STMT_EXPR (t));
12110 if (EXPR_STMT_STMT_EXPR_RESULT (t))
12111 finish_stmt_expr_expr (tmp, cur_stmt_expr);
12113 finish_expr_stmt (tmp);
12117 do_using_directive (USING_STMT_NAMESPACE (t));
12122 tree decl, pattern_decl;
12125 pattern_decl = decl = DECL_EXPR_DECL (t);
12126 if (TREE_CODE (decl) == LABEL_DECL)
12127 finish_label_decl (DECL_NAME (decl));
12128 else if (TREE_CODE (decl) == USING_DECL)
12130 tree scope = USING_DECL_SCOPE (decl);
12131 tree name = DECL_NAME (decl);
12134 scope = tsubst (scope, args, complain, in_decl);
12135 decl = lookup_qualified_name (scope, name,
12136 /*is_type_p=*/false,
12137 /*complain=*/false);
12138 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
12139 qualified_name_lookup_error (scope, name, decl, input_location);
12141 do_local_using_decl (decl, scope, name);
12145 init = DECL_INITIAL (decl);
12146 decl = tsubst (decl, args, complain, in_decl);
12147 if (decl != error_mark_node)
12149 /* By marking the declaration as instantiated, we avoid
12150 trying to instantiate it. Since instantiate_decl can't
12151 handle local variables, and since we've already done
12152 all that needs to be done, that's the right thing to
12154 if (TREE_CODE (decl) == VAR_DECL)
12155 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12156 if (TREE_CODE (decl) == VAR_DECL
12157 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
12158 /* Anonymous aggregates are a special case. */
12159 finish_anon_union (decl);
12162 int const_init = false;
12163 maybe_push_decl (decl);
12164 if (TREE_CODE (decl) == VAR_DECL
12165 && DECL_PRETTY_FUNCTION_P (decl))
12167 /* For __PRETTY_FUNCTION__ we have to adjust the
12169 const char *const name
12170 = cxx_printable_name (current_function_decl, 2);
12171 init = cp_fname_init (name, &TREE_TYPE (decl));
12175 tree t = RECUR (init);
12179 /* If we had an initializer but it
12180 instantiated to nothing,
12181 value-initialize the object. This will
12182 only occur when the initializer was a
12183 pack expansion where the parameter packs
12184 used in that expansion were of length
12186 init = build_value_init (TREE_TYPE (decl),
12188 if (TREE_CODE (init) == AGGR_INIT_EXPR)
12189 init = get_target_expr_sfinae (init, complain);
12195 if (TREE_CODE (decl) == VAR_DECL)
12196 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12198 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
12203 /* A DECL_EXPR can also be used as an expression, in the condition
12204 clause of an if/for/while construct. */
12209 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12210 RECUR (FOR_INIT_STMT (t));
12211 finish_for_init_stmt (stmt);
12212 tmp = RECUR (FOR_COND (t));
12213 finish_for_cond (tmp, stmt);
12214 tmp = RECUR (FOR_EXPR (t));
12215 finish_for_expr (tmp, stmt);
12216 RECUR (FOR_BODY (t));
12217 finish_for_stmt (stmt);
12220 case RANGE_FOR_STMT:
12223 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
12224 decl = RANGE_FOR_DECL (t);
12225 decl = tsubst (decl, args, complain, in_decl);
12226 maybe_push_decl (decl);
12227 expr = RECUR (RANGE_FOR_EXPR (t));
12228 stmt = cp_convert_range_for (stmt, decl, expr);
12229 RECUR (RANGE_FOR_BODY (t));
12230 finish_for_stmt (stmt);
12235 stmt = begin_while_stmt ();
12236 tmp = RECUR (WHILE_COND (t));
12237 finish_while_stmt_cond (tmp, stmt);
12238 RECUR (WHILE_BODY (t));
12239 finish_while_stmt (stmt);
12243 stmt = begin_do_stmt ();
12244 RECUR (DO_BODY (t));
12245 finish_do_body (stmt);
12246 tmp = RECUR (DO_COND (t));
12247 finish_do_stmt (tmp, stmt);
12251 stmt = begin_if_stmt ();
12252 tmp = RECUR (IF_COND (t));
12253 finish_if_stmt_cond (tmp, stmt);
12254 RECUR (THEN_CLAUSE (t));
12255 finish_then_clause (stmt);
12257 if (ELSE_CLAUSE (t))
12259 begin_else_clause (stmt);
12260 RECUR (ELSE_CLAUSE (t));
12261 finish_else_clause (stmt);
12264 finish_if_stmt (stmt);
12268 if (BIND_EXPR_BODY_BLOCK (t))
12269 stmt = begin_function_body ();
12271 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
12272 ? BCS_TRY_BLOCK : 0);
12274 RECUR (BIND_EXPR_BODY (t));
12276 if (BIND_EXPR_BODY_BLOCK (t))
12277 finish_function_body (stmt);
12279 finish_compound_stmt (stmt);
12283 finish_break_stmt ();
12286 case CONTINUE_STMT:
12287 finish_continue_stmt ();
12291 stmt = begin_switch_stmt ();
12292 tmp = RECUR (SWITCH_STMT_COND (t));
12293 finish_switch_cond (tmp, stmt);
12294 RECUR (SWITCH_STMT_BODY (t));
12295 finish_switch_stmt (stmt);
12298 case CASE_LABEL_EXPR:
12299 finish_case_label (EXPR_LOCATION (t),
12300 RECUR (CASE_LOW (t)),
12301 RECUR (CASE_HIGH (t)));
12306 tree decl = LABEL_EXPR_LABEL (t);
12309 label = finish_label_stmt (DECL_NAME (decl));
12310 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
12311 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
12316 tmp = GOTO_DESTINATION (t);
12317 if (TREE_CODE (tmp) != LABEL_DECL)
12318 /* Computed goto's must be tsubst'd into. On the other hand,
12319 non-computed gotos must not be; the identifier in question
12320 will have no binding. */
12323 tmp = DECL_NAME (tmp);
12324 finish_goto_stmt (tmp);
12328 tmp = finish_asm_stmt
12329 (ASM_VOLATILE_P (t),
12330 RECUR (ASM_STRING (t)),
12331 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
12332 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
12333 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
12334 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
12336 tree asm_expr = tmp;
12337 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
12338 asm_expr = TREE_OPERAND (asm_expr, 0);
12339 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
12346 stmt = begin_try_block ();
12347 RECUR (TRY_STMTS (t));
12348 finish_cleanup_try_block (stmt);
12349 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
12353 tree compound_stmt = NULL_TREE;
12355 if (FN_TRY_BLOCK_P (t))
12356 stmt = begin_function_try_block (&compound_stmt);
12358 stmt = begin_try_block ();
12360 RECUR (TRY_STMTS (t));
12362 if (FN_TRY_BLOCK_P (t))
12363 finish_function_try_block (stmt);
12365 finish_try_block (stmt);
12367 RECUR (TRY_HANDLERS (t));
12368 if (FN_TRY_BLOCK_P (t))
12369 finish_function_handler_sequence (stmt, compound_stmt);
12371 finish_handler_sequence (stmt);
12377 tree decl = HANDLER_PARMS (t);
12381 decl = tsubst (decl, args, complain, in_decl);
12382 /* Prevent instantiate_decl from trying to instantiate
12383 this variable. We've already done all that needs to be
12385 if (decl != error_mark_node)
12386 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
12388 stmt = begin_handler ();
12389 finish_handler_parms (decl, stmt);
12390 RECUR (HANDLER_BODY (t));
12391 finish_handler (stmt);
12396 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12399 case STATIC_ASSERT:
12402 tsubst_expr (STATIC_ASSERT_CONDITION (t),
12405 /*integral_constant_expression_p=*/true);
12406 finish_static_assert (condition,
12407 STATIC_ASSERT_MESSAGE (t),
12408 STATIC_ASSERT_SOURCE_LOCATION (t),
12409 /*member_p=*/false);
12414 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
12415 args, complain, in_decl);
12416 stmt = begin_omp_parallel ();
12417 RECUR (OMP_PARALLEL_BODY (t));
12418 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
12419 = OMP_PARALLEL_COMBINED (t);
12423 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
12424 args, complain, in_decl);
12425 stmt = begin_omp_task ();
12426 RECUR (OMP_TASK_BODY (t));
12427 finish_omp_task (tmp, stmt);
12432 tree clauses, body, pre_body;
12433 tree declv, initv, condv, incrv;
12436 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
12437 args, complain, in_decl);
12438 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12439 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12440 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12441 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
12443 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
12444 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
12445 &clauses, args, complain, in_decl,
12446 integral_constant_expression_p);
12448 stmt = begin_omp_structured_block ();
12450 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
12451 if (TREE_VEC_ELT (initv, i) == NULL
12452 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
12453 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
12454 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
12456 tree init = RECUR (TREE_VEC_ELT (initv, i));
12457 gcc_assert (init == TREE_VEC_ELT (declv, i));
12458 TREE_VEC_ELT (initv, i) = NULL_TREE;
12462 tree decl_expr = TREE_VEC_ELT (initv, i);
12463 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
12464 gcc_assert (init != NULL);
12465 TREE_VEC_ELT (initv, i) = RECUR (init);
12466 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
12468 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
12471 pre_body = push_stmt_list ();
12472 RECUR (OMP_FOR_PRE_BODY (t));
12473 pre_body = pop_stmt_list (pre_body);
12475 body = push_stmt_list ();
12476 RECUR (OMP_FOR_BODY (t));
12477 body = pop_stmt_list (body);
12479 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
12480 body, pre_body, clauses);
12482 add_stmt (finish_omp_structured_block (stmt));
12488 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
12489 stmt = push_stmt_list ();
12490 RECUR (OMP_BODY (t));
12491 stmt = pop_stmt_list (stmt);
12494 OMP_BODY (t) = stmt;
12495 OMP_CLAUSES (t) = tmp;
12503 stmt = push_stmt_list ();
12504 RECUR (OMP_BODY (t));
12505 stmt = pop_stmt_list (stmt);
12508 OMP_BODY (t) = stmt;
12513 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
12515 tree op1 = TREE_OPERAND (t, 1);
12516 tree lhs = RECUR (TREE_OPERAND (op1, 0));
12517 tree rhs = RECUR (TREE_OPERAND (op1, 1));
12518 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
12522 case EXPR_PACK_EXPANSION:
12523 error ("invalid use of pack expansion expression");
12524 return error_mark_node;
12526 case NONTYPE_ARGUMENT_PACK:
12527 error ("use %<...%> to expand argument pack");
12528 return error_mark_node;
12531 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
12533 return tsubst_copy_and_build (t, args, complain, in_decl,
12534 /*function_p=*/false,
12535 integral_constant_expression_p);
12542 /* T is a postfix-expression that is not being used in a function
12543 call. Return the substituted version of T. */
12546 tsubst_non_call_postfix_expression (tree t, tree args,
12547 tsubst_flags_t complain,
12550 if (TREE_CODE (t) == SCOPE_REF)
12551 t = tsubst_qualified_id (t, args, complain, in_decl,
12552 /*done=*/false, /*address_p=*/false);
12554 t = tsubst_copy_and_build (t, args, complain, in_decl,
12555 /*function_p=*/false,
12556 /*integral_constant_expression_p=*/false);
12561 /* Like tsubst but deals with expressions and performs semantic
12562 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12565 tsubst_copy_and_build (tree t,
12567 tsubst_flags_t complain,
12570 bool integral_constant_expression_p)
12572 #define RECUR(NODE) \
12573 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12574 /*function_p=*/false, \
12575 integral_constant_expression_p)
12579 if (t == NULL_TREE || t == error_mark_node)
12582 switch (TREE_CODE (t))
12586 /* Fall through. */
12587 case IDENTIFIER_NODE:
12591 bool non_integral_constant_expression_p;
12592 const char *error_msg;
12594 if (IDENTIFIER_TYPENAME_P (t))
12596 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12597 t = mangle_conv_op_name_for_type (new_type);
12600 /* Look up the name. */
12601 decl = lookup_name (t);
12603 /* By convention, expressions use ERROR_MARK_NODE to indicate
12604 failure, not NULL_TREE. */
12605 if (decl == NULL_TREE)
12606 decl = error_mark_node;
12608 decl = finish_id_expression (t, decl, NULL_TREE,
12610 integral_constant_expression_p,
12611 /*allow_non_integral_constant_expression_p=*/false,
12612 &non_integral_constant_expression_p,
12613 /*template_p=*/false,
12615 /*address_p=*/false,
12616 /*template_arg_p=*/false,
12621 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
12622 decl = unqualified_name_lookup_error (decl);
12626 case TEMPLATE_ID_EXPR:
12629 tree templ = RECUR (TREE_OPERAND (t, 0));
12630 tree targs = TREE_OPERAND (t, 1);
12633 targs = tsubst_template_args (targs, args, complain, in_decl);
12635 if (TREE_CODE (templ) == COMPONENT_REF)
12637 object = TREE_OPERAND (templ, 0);
12638 templ = TREE_OPERAND (templ, 1);
12641 object = NULL_TREE;
12642 templ = lookup_template_function (templ, targs);
12645 return build3 (COMPONENT_REF, TREE_TYPE (templ),
12646 object, templ, NULL_TREE);
12648 return baselink_for_fns (templ);
12653 tree r = RECUR (TREE_OPERAND (t, 0));
12655 if (REFERENCE_REF_P (t))
12657 /* A type conversion to reference type will be enclosed in
12658 such an indirect ref, but the substitution of the cast
12659 will have also added such an indirect ref. */
12660 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
12661 r = convert_from_reference (r);
12664 r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
12670 (tsubst (TREE_TYPE (t), args, complain, in_decl),
12671 RECUR (TREE_OPERAND (t, 0)));
12674 case REINTERPRET_CAST_EXPR:
12675 case CONST_CAST_EXPR:
12676 case DYNAMIC_CAST_EXPR:
12677 case STATIC_CAST_EXPR:
12682 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12683 if (integral_constant_expression_p
12684 && !cast_valid_in_integral_constant_expression_p (type))
12686 if (complain & tf_error)
12687 error ("a cast to a type other than an integral or "
12688 "enumeration type cannot appear in a constant-expression");
12689 return error_mark_node;
12692 op = RECUR (TREE_OPERAND (t, 0));
12694 switch (TREE_CODE (t))
12697 return build_functional_cast (type, op, complain);
12698 case REINTERPRET_CAST_EXPR:
12699 return build_reinterpret_cast (type, op, complain);
12700 case CONST_CAST_EXPR:
12701 return build_const_cast (type, op, complain);
12702 case DYNAMIC_CAST_EXPR:
12703 return build_dynamic_cast (type, op, complain);
12704 case STATIC_CAST_EXPR:
12705 return build_static_cast (type, op, complain);
12707 gcc_unreachable ();
12711 case POSTDECREMENT_EXPR:
12712 case POSTINCREMENT_EXPR:
12713 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12714 args, complain, in_decl);
12715 return build_x_unary_op (TREE_CODE (t), op1, complain);
12717 case PREDECREMENT_EXPR:
12718 case PREINCREMENT_EXPR:
12722 case TRUTH_NOT_EXPR:
12723 case UNARY_PLUS_EXPR: /* Unary + */
12724 case REALPART_EXPR:
12725 case IMAGPART_EXPR:
12726 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
12730 op1 = TREE_OPERAND (t, 0);
12731 if (TREE_CODE (op1) == LABEL_DECL)
12732 return finish_label_address_expr (DECL_NAME (op1),
12733 EXPR_LOCATION (op1));
12734 if (TREE_CODE (op1) == SCOPE_REF)
12735 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
12736 /*done=*/true, /*address_p=*/true);
12738 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
12740 return build_x_unary_op (ADDR_EXPR, op1, complain);
12745 case TRUNC_DIV_EXPR:
12746 case CEIL_DIV_EXPR:
12747 case FLOOR_DIV_EXPR:
12748 case ROUND_DIV_EXPR:
12749 case EXACT_DIV_EXPR:
12753 case TRUNC_MOD_EXPR:
12754 case FLOOR_MOD_EXPR:
12755 case TRUTH_ANDIF_EXPR:
12756 case TRUTH_ORIF_EXPR:
12757 case TRUTH_AND_EXPR:
12758 case TRUTH_OR_EXPR:
12773 return build_x_binary_op
12775 RECUR (TREE_OPERAND (t, 0)),
12776 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
12778 : TREE_CODE (TREE_OPERAND (t, 0))),
12779 RECUR (TREE_OPERAND (t, 1)),
12780 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
12782 : TREE_CODE (TREE_OPERAND (t, 1))),
12787 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
12788 /*address_p=*/false);
12790 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12791 args, complain, in_decl);
12792 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
12795 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
12796 return tsubst_copy (t, args, complain, in_decl);
12800 op1 = TREE_OPERAND (t, 0);
12803 /* When there are no ARGS, we are trying to evaluate a
12804 non-dependent expression from the parser. Trying to do
12805 the substitutions may not work. */
12807 op1 = TREE_TYPE (op1);
12811 ++cp_unevaluated_operand;
12812 ++c_inhibit_evaluation_warnings;
12813 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12814 /*function_p=*/false,
12815 /*integral_constant_expression_p=*/false);
12816 --cp_unevaluated_operand;
12817 --c_inhibit_evaluation_warnings;
12820 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
12821 complain & tf_error);
12823 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
12824 complain & tf_error);
12826 case AT_ENCODE_EXPR:
12828 op1 = TREE_OPERAND (t, 0);
12829 ++cp_unevaluated_operand;
12830 ++c_inhibit_evaluation_warnings;
12831 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12832 /*function_p=*/false,
12833 /*integral_constant_expression_p=*/false);
12834 --cp_unevaluated_operand;
12835 --c_inhibit_evaluation_warnings;
12836 return objc_build_encode_expr (op1);
12839 case NOEXCEPT_EXPR:
12840 op1 = TREE_OPERAND (t, 0);
12841 ++cp_unevaluated_operand;
12842 ++c_inhibit_evaluation_warnings;
12843 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
12844 /*function_p=*/false,
12845 /*integral_constant_expression_p=*/false);
12846 --cp_unevaluated_operand;
12847 --c_inhibit_evaluation_warnings;
12848 return finish_noexcept_expr (op1, complain);
12852 tree r = build_x_modify_expr
12853 (RECUR (TREE_OPERAND (t, 0)),
12854 TREE_CODE (TREE_OPERAND (t, 1)),
12855 RECUR (TREE_OPERAND (t, 2)),
12857 /* TREE_NO_WARNING must be set if either the expression was
12858 parenthesized or it uses an operator such as >>= rather
12859 than plain assignment. In the former case, it was already
12860 set and must be copied. In the latter case,
12861 build_x_modify_expr sets it and it must not be reset
12863 if (TREE_NO_WARNING (t))
12864 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
12869 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12870 args, complain, in_decl);
12871 /* Remember that there was a reference to this entity. */
12874 return build_x_arrow (op1);
12878 tree placement = RECUR (TREE_OPERAND (t, 0));
12879 tree init = RECUR (TREE_OPERAND (t, 3));
12880 VEC(tree,gc) *placement_vec;
12881 VEC(tree,gc) *init_vec;
12884 if (placement == NULL_TREE)
12885 placement_vec = NULL;
12888 placement_vec = make_tree_vector ();
12889 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
12890 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
12893 /* If there was an initializer in the original tree, but it
12894 instantiated to an empty list, then we should pass a
12895 non-NULL empty vector to tell build_new that it was an
12896 empty initializer() rather than no initializer. This can
12897 only happen when the initializer is a pack expansion whose
12898 parameter packs are of length zero. */
12899 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
12903 init_vec = make_tree_vector ();
12904 if (init == void_zero_node)
12905 gcc_assert (init_vec != NULL);
12908 for (; init != NULL_TREE; init = TREE_CHAIN (init))
12909 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
12913 ret = build_new (&placement_vec,
12914 tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
12915 RECUR (TREE_OPERAND (t, 2)),
12917 NEW_EXPR_USE_GLOBAL (t),
12920 if (placement_vec != NULL)
12921 release_tree_vector (placement_vec);
12922 if (init_vec != NULL)
12923 release_tree_vector (init_vec);
12929 return delete_sanity
12930 (RECUR (TREE_OPERAND (t, 0)),
12931 RECUR (TREE_OPERAND (t, 1)),
12932 DELETE_EXPR_USE_VEC (t),
12933 DELETE_EXPR_USE_GLOBAL (t),
12936 case COMPOUND_EXPR:
12937 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12938 RECUR (TREE_OPERAND (t, 1)),
12944 VEC(tree,gc) *call_args;
12945 unsigned int nargs, i;
12950 function = CALL_EXPR_FN (t);
12951 /* When we parsed the expression, we determined whether or
12952 not Koenig lookup should be performed. */
12953 koenig_p = KOENIG_LOOKUP_P (t);
12954 if (TREE_CODE (function) == SCOPE_REF)
12956 qualified_p = true;
12957 function = tsubst_qualified_id (function, args, complain, in_decl,
12959 /*address_p=*/false);
12961 else if (koenig_p && TREE_CODE (function) == IDENTIFIER_NODE)
12963 /* Do nothing; calling tsubst_copy_and_build on an identifier
12964 would incorrectly perform unqualified lookup again.
12966 Note that we can also have an IDENTIFIER_NODE if the earlier
12967 unqualified lookup found a member function; in that case
12968 koenig_p will be false and we do want to do the lookup
12969 again to find the instantiated member function.
12971 FIXME but doing that causes c++/15272, so we need to stop
12972 using IDENTIFIER_NODE in that situation. */
12973 qualified_p = false;
12977 if (TREE_CODE (function) == COMPONENT_REF)
12979 tree op = TREE_OPERAND (function, 1);
12981 qualified_p = (TREE_CODE (op) == SCOPE_REF
12982 || (BASELINK_P (op)
12983 && BASELINK_QUALIFIED_P (op)));
12986 qualified_p = false;
12988 function = tsubst_copy_and_build (function, args, complain,
12991 integral_constant_expression_p);
12993 if (BASELINK_P (function))
12994 qualified_p = true;
12997 nargs = call_expr_nargs (t);
12998 call_args = make_tree_vector ();
12999 for (i = 0; i < nargs; ++i)
13001 tree arg = CALL_EXPR_ARG (t, i);
13003 if (!PACK_EXPANSION_P (arg))
13004 VEC_safe_push (tree, gc, call_args,
13005 RECUR (CALL_EXPR_ARG (t, i)));
13008 /* Expand the pack expansion and push each entry onto
13010 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
13011 if (TREE_CODE (arg) == TREE_VEC)
13013 unsigned int len, j;
13015 len = TREE_VEC_LENGTH (arg);
13016 for (j = 0; j < len; ++j)
13018 tree value = TREE_VEC_ELT (arg, j);
13019 if (value != NULL_TREE)
13020 value = convert_from_reference (value);
13021 VEC_safe_push (tree, gc, call_args, value);
13026 /* A partial substitution. Add one entry. */
13027 VEC_safe_push (tree, gc, call_args, arg);
13032 /* We do not perform argument-dependent lookup if normal
13033 lookup finds a non-function, in accordance with the
13034 expected resolution of DR 218. */
13036 && ((is_overloaded_fn (function)
13037 /* If lookup found a member function, the Koenig lookup is
13038 not appropriate, even if an unqualified-name was used
13039 to denote the function. */
13040 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
13041 || TREE_CODE (function) == IDENTIFIER_NODE)
13042 /* Only do this when substitution turns a dependent call
13043 into a non-dependent call. */
13044 && type_dependent_expression_p_push (t)
13045 && !any_type_dependent_arguments_p (call_args))
13046 function = perform_koenig_lookup (function, call_args, false,
13049 if (TREE_CODE (function) == IDENTIFIER_NODE
13050 && !any_type_dependent_arguments_p (call_args))
13052 if (koenig_p && (complain & tf_warning_or_error))
13054 /* For backwards compatibility and good diagnostics, try
13055 the unqualified lookup again if we aren't in SFINAE
13057 tree unq = (tsubst_copy_and_build
13058 (function, args, complain, in_decl, true,
13059 integral_constant_expression_p));
13060 if (unq != function)
13063 if (TREE_CODE (fn) == COMPONENT_REF)
13064 fn = TREE_OPERAND (fn, 1);
13065 if (is_overloaded_fn (fn))
13066 fn = get_first_fn (fn);
13067 permerror (EXPR_LOC_OR_HERE (t),
13068 "%qD was not declared in this scope, "
13069 "and no declarations were found by "
13070 "argument-dependent lookup at the point "
13071 "of instantiation", function);
13072 if (DECL_CLASS_SCOPE_P (fn))
13074 inform (EXPR_LOC_OR_HERE (t),
13075 "declarations in dependent base %qT are "
13076 "not found by unqualified lookup",
13077 DECL_CLASS_CONTEXT (fn));
13078 if (current_class_ptr)
13079 inform (EXPR_LOC_OR_HERE (t),
13080 "use %<this->%D%> instead", function);
13082 inform (EXPR_LOC_OR_HERE (t),
13083 "use %<%T::%D%> instead",
13084 current_class_name, function);
13087 inform (0, "%q+D declared here, later in the "
13088 "translation unit", fn);
13092 if (TREE_CODE (function) == IDENTIFIER_NODE)
13094 unqualified_name_lookup_error (function);
13095 release_tree_vector (call_args);
13096 return error_mark_node;
13100 /* Remember that there was a reference to this entity. */
13101 if (DECL_P (function))
13102 mark_used (function);
13104 if (TREE_CODE (function) == OFFSET_REF)
13105 ret = build_offset_ref_call_from_tree (function, &call_args);
13106 else if (TREE_CODE (function) == COMPONENT_REF)
13108 tree instance = TREE_OPERAND (function, 0);
13109 tree fn = TREE_OPERAND (function, 1);
13111 if (processing_template_decl
13112 && (type_dependent_expression_p (instance)
13113 || (!BASELINK_P (fn)
13114 && TREE_CODE (fn) != FIELD_DECL)
13115 || type_dependent_expression_p (fn)
13116 || any_type_dependent_arguments_p (call_args)))
13117 ret = build_nt_call_vec (function, call_args);
13118 else if (!BASELINK_P (fn))
13119 ret = finish_call_expr (function, &call_args,
13120 /*disallow_virtual=*/false,
13121 /*koenig_p=*/false,
13124 ret = (build_new_method_call
13126 &call_args, NULL_TREE,
13127 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
13132 ret = finish_call_expr (function, &call_args,
13133 /*disallow_virtual=*/qualified_p,
13137 release_tree_vector (call_args);
13143 return build_x_conditional_expr
13144 (RECUR (TREE_OPERAND (t, 0)),
13145 RECUR (TREE_OPERAND (t, 1)),
13146 RECUR (TREE_OPERAND (t, 2)),
13149 case PSEUDO_DTOR_EXPR:
13150 return finish_pseudo_destructor_expr
13151 (RECUR (TREE_OPERAND (t, 0)),
13152 RECUR (TREE_OPERAND (t, 1)),
13153 tsubst (TREE_OPERAND (t, 2), args, complain, in_decl));
13157 tree purpose, value, chain;
13159 if (t == void_list_node)
13162 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
13163 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
13165 /* We have pack expansions, so expand those and
13166 create a new list out of it. */
13167 tree purposevec = NULL_TREE;
13168 tree valuevec = NULL_TREE;
13172 /* Expand the argument expressions. */
13173 if (TREE_PURPOSE (t))
13174 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
13175 complain, in_decl);
13176 if (TREE_VALUE (t))
13177 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
13178 complain, in_decl);
13180 /* Build the rest of the list. */
13181 chain = TREE_CHAIN (t);
13182 if (chain && chain != void_type_node)
13183 chain = RECUR (chain);
13185 /* Determine the number of arguments. */
13186 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
13188 len = TREE_VEC_LENGTH (purposevec);
13189 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
13191 else if (TREE_CODE (valuevec) == TREE_VEC)
13192 len = TREE_VEC_LENGTH (valuevec);
13195 /* Since we only performed a partial substitution into
13196 the argument pack, we only return a single list
13198 if (purposevec == TREE_PURPOSE (t)
13199 && valuevec == TREE_VALUE (t)
13200 && chain == TREE_CHAIN (t))
13203 return tree_cons (purposevec, valuevec, chain);
13206 /* Convert the argument vectors into a TREE_LIST */
13210 /* Grab the Ith values. */
13212 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
13215 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
13218 /* Build the list (backwards). */
13219 chain = tree_cons (purpose, value, chain);
13225 purpose = TREE_PURPOSE (t);
13227 purpose = RECUR (purpose);
13228 value = TREE_VALUE (t);
13230 value = RECUR (value);
13231 chain = TREE_CHAIN (t);
13232 if (chain && chain != void_type_node)
13233 chain = RECUR (chain);
13234 if (purpose == TREE_PURPOSE (t)
13235 && value == TREE_VALUE (t)
13236 && chain == TREE_CHAIN (t))
13238 return tree_cons (purpose, value, chain);
13241 case COMPONENT_REF:
13247 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
13248 args, complain, in_decl);
13249 /* Remember that there was a reference to this entity. */
13250 if (DECL_P (object))
13251 mark_used (object);
13252 object_type = TREE_TYPE (object);
13254 member = TREE_OPERAND (t, 1);
13255 if (BASELINK_P (member))
13256 member = tsubst_baselink (member,
13257 non_reference (TREE_TYPE (object)),
13258 args, complain, in_decl);
13260 member = tsubst_copy (member, args, complain, in_decl);
13261 if (member == error_mark_node)
13262 return error_mark_node;
13264 if (object_type && !CLASS_TYPE_P (object_type))
13266 if (SCALAR_TYPE_P (object_type))
13268 tree s = NULL_TREE;
13269 tree dtor = member;
13271 if (TREE_CODE (dtor) == SCOPE_REF)
13273 s = TREE_OPERAND (dtor, 0);
13274 dtor = TREE_OPERAND (dtor, 1);
13276 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
13278 dtor = TREE_OPERAND (dtor, 0);
13280 return finish_pseudo_destructor_expr (object, s, dtor);
13284 else if (TREE_CODE (member) == SCOPE_REF
13285 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
13290 /* Lookup the template functions now that we know what the
13292 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
13293 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
13294 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
13295 /*is_type_p=*/false,
13296 /*complain=*/false);
13297 if (BASELINK_P (member))
13299 BASELINK_FUNCTIONS (member)
13300 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
13302 member = (adjust_result_of_qualified_name_lookup
13303 (member, BINFO_TYPE (BASELINK_BINFO (member)),
13308 qualified_name_lookup_error (object_type, tmpl, member,
13310 return error_mark_node;
13313 else if (TREE_CODE (member) == SCOPE_REF
13314 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
13315 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
13317 if (complain & tf_error)
13319 if (TYPE_P (TREE_OPERAND (member, 0)))
13320 error ("%qT is not a class or namespace",
13321 TREE_OPERAND (member, 0));
13323 error ("%qD is not a class or namespace",
13324 TREE_OPERAND (member, 0));
13326 return error_mark_node;
13328 else if (TREE_CODE (member) == FIELD_DECL)
13329 return finish_non_static_data_member (member, object, NULL_TREE);
13331 return finish_class_member_access_expr (object, member,
13332 /*template_p=*/false,
13338 (RECUR (TREE_OPERAND (t, 0)));
13342 VEC(constructor_elt,gc) *n;
13343 constructor_elt *ce;
13344 unsigned HOST_WIDE_INT idx;
13345 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13346 bool process_index_p;
13348 bool need_copy_p = false;
13351 if (type == error_mark_node)
13352 return error_mark_node;
13354 /* digest_init will do the wrong thing if we let it. */
13355 if (type && TYPE_PTRMEMFUNC_P (type))
13358 /* We do not want to process the index of aggregate
13359 initializers as they are identifier nodes which will be
13360 looked up by digest_init. */
13361 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
13363 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
13364 newlen = VEC_length (constructor_elt, n);
13365 FOR_EACH_VEC_ELT (constructor_elt, n, idx, ce)
13367 if (ce->index && process_index_p)
13368 ce->index = RECUR (ce->index);
13370 if (PACK_EXPANSION_P (ce->value))
13372 /* Substitute into the pack expansion. */
13373 ce->value = tsubst_pack_expansion (ce->value, args, complain,
13376 if (ce->value == error_mark_node
13377 || PACK_EXPANSION_P (ce->value))
13379 else if (TREE_VEC_LENGTH (ce->value) == 1)
13380 /* Just move the argument into place. */
13381 ce->value = TREE_VEC_ELT (ce->value, 0);
13384 /* Update the length of the final CONSTRUCTOR
13385 arguments vector, and note that we will need to
13387 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
13388 need_copy_p = true;
13392 ce->value = RECUR (ce->value);
13397 VEC(constructor_elt,gc) *old_n = n;
13399 n = VEC_alloc (constructor_elt, gc, newlen);
13400 FOR_EACH_VEC_ELT (constructor_elt, old_n, idx, ce)
13402 if (TREE_CODE (ce->value) == TREE_VEC)
13404 int i, len = TREE_VEC_LENGTH (ce->value);
13405 for (i = 0; i < len; ++i)
13406 CONSTRUCTOR_APPEND_ELT (n, 0,
13407 TREE_VEC_ELT (ce->value, i));
13410 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
13414 r = build_constructor (init_list_type_node, n);
13415 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
13417 if (TREE_HAS_CONSTRUCTOR (t))
13418 return finish_compound_literal (type, r, complain);
13420 TREE_TYPE (r) = type;
13426 tree operand_0 = TREE_OPERAND (t, 0);
13427 if (TYPE_P (operand_0))
13429 operand_0 = tsubst (operand_0, args, complain, in_decl);
13430 return get_typeid (operand_0);
13434 operand_0 = RECUR (operand_0);
13435 return build_typeid (operand_0);
13446 tree r = tsubst_copy (t, args, complain, in_decl);
13448 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
13449 /* If the original type was a reference, we'll be wrapped in
13450 the appropriate INDIRECT_REF. */
13451 r = convert_from_reference (r);
13456 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
13457 tsubst (TREE_TYPE (t), args, complain, in_decl));
13459 case OFFSETOF_EXPR:
13460 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
13464 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
13465 complain, in_decl);
13467 tree type2 = TRAIT_EXPR_TYPE2 (t);
13469 type2 = tsubst_copy (type2, args, complain, in_decl);
13471 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
13476 tree old_stmt_expr = cur_stmt_expr;
13477 tree stmt_expr = begin_stmt_expr ();
13479 cur_stmt_expr = stmt_expr;
13480 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
13481 integral_constant_expression_p);
13482 stmt_expr = finish_stmt_expr (stmt_expr, false);
13483 cur_stmt_expr = old_stmt_expr;
13485 /* If the resulting list of expression statement is empty,
13486 fold it further into void_zero_node. */
13487 if (empty_expr_stmt_p (stmt_expr))
13488 stmt_expr = void_zero_node;
13494 t = tsubst_copy (t, args, complain, in_decl);
13495 /* As in finish_id_expression, we resolve enumeration constants
13496 to their underlying values. */
13497 if (TREE_CODE (t) == CONST_DECL)
13499 used_types_insert (TREE_TYPE (t));
13500 return DECL_INITIAL (t);
13506 tree r = build_lambda_expr ();
13508 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
13509 TREE_TYPE (r) = type;
13510 CLASSTYPE_LAMBDA_EXPR (type) = r;
13512 LAMBDA_EXPR_LOCATION (r)
13513 = LAMBDA_EXPR_LOCATION (t);
13514 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
13515 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
13516 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
13517 LAMBDA_EXPR_DISCRIMINATOR (r)
13518 = (LAMBDA_EXPR_DISCRIMINATOR (t));
13519 LAMBDA_EXPR_CAPTURE_LIST (r)
13520 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
13521 LAMBDA_EXPR_EXTRA_SCOPE (r)
13522 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
13523 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
13524 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
13526 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
13527 determine_visibility (TYPE_NAME (type));
13528 /* Now that we know visibility, instantiate the type so we have a
13529 declaration of the op() for later calls to lambda_function. */
13530 complete_type (type);
13532 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
13534 apply_lambda_return_type (r, type);
13536 return build_lambda_object (r);
13540 /* We can get here for a constant initializer of non-dependent type.
13541 FIXME stop folding in cp_parser_initializer_clause. */
13542 gcc_assert (TREE_CONSTANT (t));
13544 tree r = get_target_expr (RECUR (TARGET_EXPR_INITIAL (t)));
13545 TREE_CONSTANT (r) = true;
13550 /* Handle Objective-C++ constructs, if appropriate. */
13553 = objcp_tsubst_copy_and_build (t, args, complain,
13554 in_decl, /*function_p=*/false);
13558 return tsubst_copy (t, args, complain, in_decl);
13564 /* Verify that the instantiated ARGS are valid. For type arguments,
13565 make sure that the type's linkage is ok. For non-type arguments,
13566 make sure they are constants if they are integral or enumerations.
13567 Emit an error under control of COMPLAIN, and return TRUE on error. */
13570 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
13572 if (ARGUMENT_PACK_P (t))
13574 tree vec = ARGUMENT_PACK_ARGS (t);
13575 int len = TREE_VEC_LENGTH (vec);
13576 bool result = false;
13579 for (i = 0; i < len; ++i)
13580 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
13584 else if (TYPE_P (t))
13586 /* [basic.link]: A name with no linkage (notably, the name
13587 of a class or enumeration declared in a local scope)
13588 shall not be used to declare an entity with linkage.
13589 This implies that names with no linkage cannot be used as
13592 DR 757 relaxes this restriction for C++0x. */
13593 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
13594 : no_linkage_check (t, /*relaxed_p=*/false));
13598 /* DR 488 makes use of a type with no linkage cause
13599 type deduction to fail. */
13600 if (complain & tf_error)
13602 if (TYPE_ANONYMOUS_P (nt))
13603 error ("%qT is/uses anonymous type", t);
13605 error ("template argument for %qD uses local type %qT",
13610 /* In order to avoid all sorts of complications, we do not
13611 allow variably-modified types as template arguments. */
13612 else if (variably_modified_type_p (t, NULL_TREE))
13614 if (complain & tf_error)
13615 error ("%qT is a variably modified type", t);
13619 /* A non-type argument of integral or enumerated type must be a
13621 else if (TREE_TYPE (t)
13622 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
13623 && !TREE_CONSTANT (t))
13625 if (complain & tf_error)
13626 error ("integral expression %qE is not constant", t);
13633 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
13635 int ix, len = DECL_NTPARMS (tmpl);
13636 bool result = false;
13638 for (ix = 0; ix != len; ix++)
13640 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
13643 if (result && (complain & tf_error))
13644 error (" trying to instantiate %qD", tmpl);
13648 /* In C++0x, it's possible to have a function template whose type depends
13649 on itself recursively. This is most obvious with decltype, but can also
13650 occur with enumeration scope (c++/48969). So we need to catch infinite
13651 recursion and reject the substitution at deduction time; this function
13652 will return error_mark_node for any repeated substitution.
13654 This also catches excessive recursion such as when f<N> depends on
13655 f<N-1> across all integers, and returns error_mark_node for all the
13656 substitutions back up to the initial one.
13658 This is, of course, not reentrant. */
13661 deduction_tsubst_fntype (tree fn, tree targs)
13663 static bool excessive_deduction_depth;
13664 static int deduction_depth;
13665 struct pending_template *old_last_pend = last_pending_template;
13667 tree fntype = TREE_TYPE (fn);
13671 if (excessive_deduction_depth)
13672 return error_mark_node;
13674 tinst = build_tree_list (fn, targs);
13675 if (!push_tinst_level (tinst))
13677 excessive_deduction_depth = true;
13679 return error_mark_node;
13682 input_location = DECL_SOURCE_LOCATION (fn);
13684 push_deduction_access_scope (fn);
13685 r = tsubst (fntype, targs, tf_none, NULL_TREE);
13686 pop_deduction_access_scope (fn);
13689 if (excessive_deduction_depth)
13691 r = error_mark_node;
13692 if (deduction_depth == 0)
13693 /* Reset once we're all the way out. */
13694 excessive_deduction_depth = false;
13697 pop_tinst_level ();
13698 /* We can't free this if a pending_template entry is pointing at it. */
13699 if (last_pending_template == old_last_pend)
13704 /* Instantiate the indicated variable or function template TMPL with
13705 the template arguments in TARG_PTR. */
13708 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
13710 tree targ_ptr = orig_args;
13714 HOST_WIDE_INT saved_processing_template_decl;
13716 if (tmpl == error_mark_node)
13717 return error_mark_node;
13719 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
13721 /* If this function is a clone, handle it specially. */
13722 if (DECL_CLONED_FUNCTION_P (tmpl))
13727 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13728 DECL_CLONED_FUNCTION. */
13729 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
13730 targ_ptr, complain);
13731 if (spec == error_mark_node)
13732 return error_mark_node;
13734 /* Look for the clone. */
13735 FOR_EACH_CLONE (clone, spec)
13736 if (DECL_NAME (clone) == DECL_NAME (tmpl))
13738 /* We should always have found the clone by now. */
13739 gcc_unreachable ();
13743 /* Check to see if we already have this specialization. */
13744 gen_tmpl = most_general_template (tmpl);
13745 if (tmpl != gen_tmpl)
13746 /* The TMPL is a partial instantiation. To get a full set of
13747 arguments we must add the arguments used to perform the
13748 partial instantiation. */
13749 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
13752 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13753 but it doesn't seem to be on the hot path. */
13754 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
13756 gcc_assert (tmpl == gen_tmpl
13757 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
13759 || fndecl == NULL_TREE);
13761 if (spec != NULL_TREE)
13764 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
13766 return error_mark_node;
13768 /* We are building a FUNCTION_DECL, during which the access of its
13769 parameters and return types have to be checked. However this
13770 FUNCTION_DECL which is the desired context for access checking
13771 is not built yet. We solve this chicken-and-egg problem by
13772 deferring all checks until we have the FUNCTION_DECL. */
13773 push_deferring_access_checks (dk_deferred);
13775 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13776 (because, for example, we have encountered a non-dependent
13777 function call in the body of a template function and must now
13778 determine which of several overloaded functions will be called),
13779 within the instantiation itself we are not processing a
13781 saved_processing_template_decl = processing_template_decl;
13782 processing_template_decl = 0;
13783 /* Substitute template parameters to obtain the specialization. */
13784 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
13785 targ_ptr, complain, gen_tmpl);
13786 processing_template_decl = saved_processing_template_decl;
13787 if (fndecl == error_mark_node)
13788 return error_mark_node;
13790 /* Now we know the specialization, compute access previously
13792 push_access_scope (fndecl);
13794 /* Some typedefs referenced from within the template code need to be access
13795 checked at template instantiation time, i.e now. These types were
13796 added to the template at parsing time. Let's get those and perfom
13797 the acces checks then. */
13798 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
13799 perform_deferred_access_checks ();
13800 pop_access_scope (fndecl);
13801 pop_deferring_access_checks ();
13803 /* The DECL_TI_TEMPLATE should always be the immediate parent
13804 template, not the most general template. */
13805 DECL_TI_TEMPLATE (fndecl) = tmpl;
13807 /* If we've just instantiated the main entry point for a function,
13808 instantiate all the alternate entry points as well. We do this
13809 by cloning the instantiation of the main entry point, not by
13810 instantiating the template clones. */
13811 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
13812 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
13817 /* Wrapper for instantiate_template_1. */
13820 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
13823 timevar_push (TV_TEMPLATE_INST);
13824 ret = instantiate_template_1 (tmpl, orig_args, complain);
13825 timevar_pop (TV_TEMPLATE_INST);
13829 /* We're going to do deduction substitution on the type of TMPL, a function
13830 template. In C++11 mode, push into that access scope. In C++03 mode,
13831 disable access checking. */
13834 push_deduction_access_scope (tree tmpl)
13836 if (cxx_dialect >= cxx0x)
13837 push_access_scope (DECL_TEMPLATE_RESULT (tmpl));
13839 push_deferring_access_checks (dk_no_check);
13842 /* And pop back out. */
13845 pop_deduction_access_scope (tree tmpl)
13847 if (cxx_dialect >= cxx0x)
13848 pop_access_scope (DECL_TEMPLATE_RESULT (tmpl));
13850 pop_deferring_access_checks ();
13853 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13854 NARGS elements of the arguments that are being used when calling
13855 it. TARGS is a vector into which the deduced template arguments
13858 Return zero for success, 2 for an incomplete match that doesn't resolve
13859 all the types, and 1 for complete failure. An error message will be
13860 printed only for an incomplete match.
13862 If FN is a conversion operator, or we are trying to produce a specific
13863 specialization, RETURN_TYPE is the return type desired.
13865 The EXPLICIT_TARGS are explicit template arguments provided via a
13868 The parameter STRICT is one of:
13871 We are deducing arguments for a function call, as in
13872 [temp.deduct.call].
13875 We are deducing arguments for a conversion function, as in
13876 [temp.deduct.conv].
13879 We are deducing arguments when doing an explicit instantiation
13880 as in [temp.explicit], when determining an explicit specialization
13881 as in [temp.expl.spec], or when taking the address of a function
13882 template, as in [temp.deduct.funcaddr]. */
13885 fn_type_unification (tree fn,
13886 tree explicit_targs,
13889 unsigned int nargs,
13891 unification_kind_t strict,
13897 bool incomplete_argument_packs_p = false;
13899 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
13901 fntype = TREE_TYPE (fn);
13902 if (explicit_targs)
13906 The specified template arguments must match the template
13907 parameters in kind (i.e., type, nontype, template), and there
13908 must not be more arguments than there are parameters;
13909 otherwise type deduction fails.
13911 Nontype arguments must match the types of the corresponding
13912 nontype template parameters, or must be convertible to the
13913 types of the corresponding nontype parameters as specified in
13914 _temp.arg.nontype_, otherwise type deduction fails.
13916 All references in the function type of the function template
13917 to the corresponding template parameters are replaced by the
13918 specified template argument values. If a substitution in a
13919 template parameter or in the function type of the function
13920 template results in an invalid type, type deduction fails. */
13921 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
13922 int i, len = TREE_VEC_LENGTH (tparms);
13923 tree converted_args;
13924 bool incomplete = false;
13926 if (explicit_targs == error_mark_node)
13930 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
13931 /*require_all_args=*/false,
13932 /*use_default_args=*/false));
13933 if (converted_args == error_mark_node)
13936 /* Substitute the explicit args into the function type. This is
13937 necessary so that, for instance, explicitly declared function
13938 arguments can match null pointed constants. If we were given
13939 an incomplete set of explicit args, we must not do semantic
13940 processing during substitution as we could create partial
13942 for (i = 0; i < len; i++)
13944 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13945 bool parameter_pack = false;
13947 /* Dig out the actual parm. */
13948 if (TREE_CODE (parm) == TYPE_DECL
13949 || TREE_CODE (parm) == TEMPLATE_DECL)
13951 parm = TREE_TYPE (parm);
13952 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
13954 else if (TREE_CODE (parm) == PARM_DECL)
13956 parm = DECL_INITIAL (parm);
13957 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
13960 if (parameter_pack)
13964 template_parm_level_and_index (parm, &level, &idx);
13966 /* Mark the argument pack as "incomplete". We could
13967 still deduce more arguments during unification.
13968 We remove this mark in type_unification_real. */
13969 targ = TMPL_ARG (converted_args, level, idx);
13972 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
13973 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
13974 = ARGUMENT_PACK_ARGS (targ);
13977 /* We have some incomplete argument packs. */
13978 incomplete_argument_packs_p = true;
13982 if (incomplete_argument_packs_p)
13983 /* Any substitution is guaranteed to be incomplete if there
13984 are incomplete argument packs, because we can still deduce
13988 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
13990 processing_template_decl += incomplete;
13991 fntype = deduction_tsubst_fntype (fn, converted_args);
13992 processing_template_decl -= incomplete;
13994 if (fntype == error_mark_node)
13997 /* Place the explicitly specified arguments in TARGS. */
13998 for (i = NUM_TMPL_ARGS (converted_args); i--;)
13999 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
14002 /* Never do unification on the 'this' parameter. */
14003 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
14009 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
14010 new_args = XALLOCAVEC (tree, nargs + 1);
14011 new_args[0] = return_type;
14012 memcpy (new_args + 1, args, nargs * sizeof (tree));
14017 /* We allow incomplete unification without an error message here
14018 because the standard doesn't seem to explicitly prohibit it. Our
14019 callers must be ready to deal with unification failures in any
14021 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
14022 targs, parms, args, nargs, /*subr=*/0,
14025 /* Now that we have bindings for all of the template arguments,
14026 ensure that the arguments deduced for the template template
14027 parameters have compatible template parameter lists. We cannot
14028 check this property before we have deduced all template
14029 arguments, because the template parameter types of a template
14030 template parameter might depend on prior template parameters
14031 deduced after the template template parameter. The following
14032 ill-formed example illustrates this issue:
14034 template<typename T, template<T> class C> void f(C<5>, T);
14036 template<int N> struct X {};
14039 f(X<5>(), 5l); // error: template argument deduction fails
14042 The template parameter list of 'C' depends on the template type
14043 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14044 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14045 time that we deduce 'C'. */
14047 && !template_template_parm_bindings_ok_p
14048 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
14052 /* All is well so far. Now, check:
14056 When all template arguments have been deduced, all uses of
14057 template parameters in nondeduced contexts are replaced with
14058 the corresponding deduced argument values. If the
14059 substitution results in an invalid type, as described above,
14060 type deduction fails. */
14062 tree substed = deduction_tsubst_fntype (fn, targs);
14063 if (substed == error_mark_node)
14066 /* If we're looking for an exact match, check that what we got
14067 is indeed an exact match. It might not be if some template
14068 parameters are used in non-deduced contexts. */
14069 if (strict == DEDUCE_EXACT)
14074 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
14076 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
14077 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
14078 if (!same_type_p (args[i], TREE_VALUE (sarg)))
14086 /* Adjust types before performing type deduction, as described in
14087 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14088 sections are symmetric. PARM is the type of a function parameter
14089 or the return type of the conversion function. ARG is the type of
14090 the argument passed to the call, or the type of the value
14091 initialized with the result of the conversion function.
14092 ARG_EXPR is the original argument expression, which may be null. */
14095 maybe_adjust_types_for_deduction (unification_kind_t strict,
14109 /* Swap PARM and ARG throughout the remainder of this
14110 function; the handling is precisely symmetric since PARM
14111 will initialize ARG rather than vice versa. */
14119 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14120 too, but here handle it by stripping the reference from PARM
14121 rather than by adding it to ARG. */
14122 if (TREE_CODE (*parm) == REFERENCE_TYPE
14123 && TYPE_REF_IS_RVALUE (*parm)
14124 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14125 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14126 && TREE_CODE (*arg) == REFERENCE_TYPE
14127 && !TYPE_REF_IS_RVALUE (*arg))
14128 *parm = TREE_TYPE (*parm);
14129 /* Nothing else to do in this case. */
14133 gcc_unreachable ();
14136 if (TREE_CODE (*parm) != REFERENCE_TYPE)
14138 /* [temp.deduct.call]
14140 If P is not a reference type:
14142 --If A is an array type, the pointer type produced by the
14143 array-to-pointer standard conversion (_conv.array_) is
14144 used in place of A for type deduction; otherwise,
14146 --If A is a function type, the pointer type produced by
14147 the function-to-pointer standard conversion
14148 (_conv.func_) is used in place of A for type deduction;
14151 --If A is a cv-qualified type, the top level
14152 cv-qualifiers of A's type are ignored for type
14154 if (TREE_CODE (*arg) == ARRAY_TYPE)
14155 *arg = build_pointer_type (TREE_TYPE (*arg));
14156 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
14157 *arg = build_pointer_type (*arg);
14159 *arg = TYPE_MAIN_VARIANT (*arg);
14162 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
14163 of the form T&&, where T is a template parameter, and the argument
14164 is an lvalue, T is deduced as A& */
14165 if (TREE_CODE (*parm) == REFERENCE_TYPE
14166 && TYPE_REF_IS_RVALUE (*parm)
14167 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
14168 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
14169 && (arg_expr ? real_lvalue_p (arg_expr)
14170 /* try_one_overload doesn't provide an arg_expr, but
14171 functions are always lvalues. */
14172 : TREE_CODE (*arg) == FUNCTION_TYPE))
14173 *arg = build_reference_type (*arg);
14175 /* [temp.deduct.call]
14177 If P is a cv-qualified type, the top level cv-qualifiers
14178 of P's type are ignored for type deduction. If P is a
14179 reference type, the type referred to by P is used for
14181 *parm = TYPE_MAIN_VARIANT (*parm);
14182 if (TREE_CODE (*parm) == REFERENCE_TYPE)
14184 *parm = TREE_TYPE (*parm);
14185 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14188 /* DR 322. For conversion deduction, remove a reference type on parm
14189 too (which has been swapped into ARG). */
14190 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
14191 *arg = TREE_TYPE (*arg);
14196 /* Most parms like fn_type_unification.
14198 If SUBR is 1, we're being called recursively (to unify the
14199 arguments of a function or method parameter of a function
14203 type_unification_real (tree tparms,
14207 unsigned int xnargs,
14209 unification_kind_t strict,
14212 tree parm, arg, arg_expr;
14214 int ntparms = TREE_VEC_LENGTH (tparms);
14216 int saw_undeduced = 0;
14219 unsigned int nargs;
14222 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
14223 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
14224 gcc_assert (ntparms > 0);
14226 /* Reset the number of non-defaulted template arguments contained
14228 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
14233 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
14234 | UNIFY_ALLOW_DERIVED);
14238 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
14242 sub_strict = UNIFY_ALLOW_NONE;
14246 gcc_unreachable ();
14255 while (parms && parms != void_list_node
14258 parm = TREE_VALUE (parms);
14260 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
14261 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
14262 /* For a function parameter pack that occurs at the end of the
14263 parameter-declaration-list, the type A of each remaining
14264 argument of the call is compared with the type P of the
14265 declarator-id of the function parameter pack. */
14268 parms = TREE_CHAIN (parms);
14270 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
14271 /* For a function parameter pack that does not occur at the
14272 end of the parameter-declaration-list, the type of the
14273 parameter pack is a non-deduced context. */
14280 if (arg == error_mark_node)
14282 if (arg == unknown_type_node)
14283 /* We can't deduce anything from this, but we might get all the
14284 template args from other function args. */
14287 /* Conversions will be performed on a function argument that
14288 corresponds with a function parameter that contains only
14289 non-deducible template parameters and explicitly specified
14290 template parameters. */
14291 if (!uses_template_parms (parm))
14296 type = TREE_TYPE (arg);
14300 if (same_type_p (parm, type))
14302 if (strict != DEDUCE_EXACT
14303 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
14312 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
14313 if (type_unknown_p (arg))
14315 /* [temp.deduct.type]
14317 A template-argument can be deduced from a pointer to
14318 function or pointer to member function argument if
14319 the set of overloaded functions does not contain
14320 function templates and at most one of a set of
14321 overloaded functions provides a unique match. */
14322 if (resolve_overloaded_unification
14323 (tparms, targs, parm, arg, strict, sub_strict))
14329 arg = unlowered_expr_type (arg);
14330 if (arg == error_mark_node)
14335 int arg_strict = sub_strict;
14338 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
14341 if (arg == init_list_type_node && arg_expr)
14343 if (unify (tparms, targs, parm, arg, arg_strict))
14350 && parms != void_list_node
14351 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
14353 /* Unify the remaining arguments with the pack expansion type. */
14355 tree parmvec = make_tree_vec (1);
14357 /* Allocate a TREE_VEC and copy in all of the arguments */
14358 argvec = make_tree_vec (nargs - ia);
14359 for (i = 0; ia < nargs; ++ia, ++i)
14360 TREE_VEC_ELT (argvec, i) = args[ia];
14362 /* Copy the parameter into parmvec. */
14363 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
14364 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
14365 /*call_args_p=*/true, /*subr=*/subr))
14368 /* Advance to the end of the list of parameters. */
14369 parms = TREE_CHAIN (parms);
14372 /* Fail if we've reached the end of the parm list, and more args
14373 are present, and the parm list isn't variadic. */
14374 if (ia < nargs && parms == void_list_node)
14376 /* Fail if parms are left and they don't have default values. */
14377 if (parms && parms != void_list_node
14378 && TREE_PURPOSE (parms) == NULL_TREE)
14383 /* Check to see if we need another pass before we start clearing
14384 ARGUMENT_PACK_INCOMPLETE_P. */
14385 for (i = 0; i < ntparms; i++)
14387 tree targ = TREE_VEC_ELT (targs, i);
14388 tree tparm = TREE_VEC_ELT (tparms, i);
14390 if (targ || tparm == error_mark_node)
14392 tparm = TREE_VALUE (tparm);
14394 /* If this is an undeduced nontype parameter that depends on
14395 a type parameter, try another pass; its type may have been
14396 deduced from a later argument than the one from which
14397 this parameter can be deduced. */
14398 if (TREE_CODE (tparm) == PARM_DECL
14399 && uses_template_parms (TREE_TYPE (tparm))
14400 && !saw_undeduced++)
14404 for (i = 0; i < ntparms; i++)
14406 tree targ = TREE_VEC_ELT (targs, i);
14407 tree tparm = TREE_VEC_ELT (tparms, i);
14409 /* Clear the "incomplete" flags on all argument packs now so that
14410 substituting them into later default arguments works. */
14411 if (targ && ARGUMENT_PACK_P (targ))
14413 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
14414 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
14417 if (targ || tparm == error_mark_node)
14419 tparm = TREE_VALUE (tparm);
14421 /* Core issue #226 (C++0x) [temp.deduct]:
14423 If a template argument has not been deduced, its
14424 default template argument, if any, is used.
14426 When we are in C++98 mode, TREE_PURPOSE will either
14427 be NULL_TREE or ERROR_MARK_NODE, so we do not need
14428 to explicitly check cxx_dialect here. */
14429 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
14431 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
14432 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
14433 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
14434 arg = convert_template_argument (parm, arg, targs, tf_none,
14436 if (arg == error_mark_node)
14440 TREE_VEC_ELT (targs, i) = arg;
14441 /* The position of the first default template argument,
14442 is also the number of non-defaulted arguments in TARGS.
14444 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
14445 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
14450 /* If the type parameter is a parameter pack, then it will
14451 be deduced to an empty parameter pack. */
14452 if (template_parameter_pack_p (tparm))
14456 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
14458 arg = make_node (NONTYPE_ARGUMENT_PACK);
14459 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
14460 TREE_CONSTANT (arg) = 1;
14463 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
14465 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
14467 TREE_VEC_ELT (targs, i) = arg;
14474 #ifdef ENABLE_CHECKING
14475 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
14476 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
14482 /* Subroutine of type_unification_real. Args are like the variables
14483 at the call site. ARG is an overloaded function (or template-id);
14484 we try deducing template args from each of the overloads, and if
14485 only one succeeds, we go with that. Modifies TARGS and returns
14486 true on success. */
14489 resolve_overloaded_unification (tree tparms,
14493 unification_kind_t strict,
14496 tree tempargs = copy_node (targs);
14498 tree goodfn = NULL_TREE;
14501 if (TREE_CODE (arg) == ADDR_EXPR)
14503 arg = TREE_OPERAND (arg, 0);
14509 if (TREE_CODE (arg) == COMPONENT_REF)
14510 /* Handle `&x' where `x' is some static or non-static member
14512 arg = TREE_OPERAND (arg, 1);
14514 if (TREE_CODE (arg) == OFFSET_REF)
14515 arg = TREE_OPERAND (arg, 1);
14517 /* Strip baselink information. */
14518 if (BASELINK_P (arg))
14519 arg = BASELINK_FUNCTIONS (arg);
14521 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
14523 /* If we got some explicit template args, we need to plug them into
14524 the affected templates before we try to unify, in case the
14525 explicit args will completely resolve the templates in question. */
14528 tree expl_subargs = TREE_OPERAND (arg, 1);
14529 arg = TREE_OPERAND (arg, 0);
14531 for (; arg; arg = OVL_NEXT (arg))
14533 tree fn = OVL_CURRENT (arg);
14534 tree subargs, elem;
14536 if (TREE_CODE (fn) != TEMPLATE_DECL)
14539 ++processing_template_decl;
14540 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
14541 expl_subargs, /*check_ret=*/false);
14542 if (subargs && !any_dependent_template_arguments_p (subargs))
14544 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
14545 if (try_one_overload (tparms, targs, tempargs, parm,
14546 elem, strict, sub_strict, addr_p)
14547 && (!goodfn || !decls_match (goodfn, elem)))
14555 --processing_template_decl;
14557 /* If no templates (or more than one) are fully resolved by the
14558 explicit arguments, this template-id is a non-deduced context; it
14559 could still be OK if we deduce all template arguments for the
14560 enclosing call through other arguments. */
14564 else if (TREE_CODE (arg) != OVERLOAD
14565 && TREE_CODE (arg) != FUNCTION_DECL)
14566 /* If ARG is, for example, "(0, &f)" then its type will be unknown
14567 -- but the deduction does not succeed because the expression is
14568 not just the function on its own. */
14571 for (; arg; arg = OVL_NEXT (arg))
14572 if (try_one_overload (tparms, targs, tempargs, parm,
14573 TREE_TYPE (OVL_CURRENT (arg)),
14574 strict, sub_strict, addr_p)
14575 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
14577 goodfn = OVL_CURRENT (arg);
14581 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14582 to function or pointer to member function argument if the set of
14583 overloaded functions does not contain function templates and at most
14584 one of a set of overloaded functions provides a unique match.
14586 So if we found multiple possibilities, we return success but don't
14587 deduce anything. */
14591 int i = TREE_VEC_LENGTH (targs);
14593 if (TREE_VEC_ELT (tempargs, i))
14594 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
14602 /* Core DR 115: In contexts where deduction is done and fails, or in
14603 contexts where deduction is not done, if a template argument list is
14604 specified and it, along with any default template arguments, identifies
14605 a single function template specialization, then the template-id is an
14606 lvalue for the function template specialization. */
14609 resolve_nondeduced_context (tree orig_expr)
14611 tree expr, offset, baselink;
14614 if (!type_unknown_p (orig_expr))
14619 offset = NULL_TREE;
14620 baselink = NULL_TREE;
14622 if (TREE_CODE (expr) == ADDR_EXPR)
14624 expr = TREE_OPERAND (expr, 0);
14627 if (TREE_CODE (expr) == OFFSET_REF)
14630 expr = TREE_OPERAND (expr, 1);
14632 if (TREE_CODE (expr) == BASELINK)
14635 expr = BASELINK_FUNCTIONS (expr);
14638 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
14641 tree goodfn = NULL_TREE;
14643 /* If we got some explicit template args, we need to plug them into
14644 the affected templates before we try to unify, in case the
14645 explicit args will completely resolve the templates in question. */
14647 tree expl_subargs = TREE_OPERAND (expr, 1);
14648 tree arg = TREE_OPERAND (expr, 0);
14649 tree badfn = NULL_TREE;
14650 tree badargs = NULL_TREE;
14652 for (; arg; arg = OVL_NEXT (arg))
14654 tree fn = OVL_CURRENT (arg);
14655 tree subargs, elem;
14657 if (TREE_CODE (fn) != TEMPLATE_DECL)
14660 ++processing_template_decl;
14661 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
14662 expl_subargs, /*check_ret=*/false);
14663 if (subargs && !any_dependent_template_arguments_p (subargs))
14665 elem = instantiate_template (fn, subargs, tf_none);
14666 if (elem == error_mark_node)
14671 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
14677 --processing_template_decl;
14683 expr = build_baselink (BASELINK_BINFO (baselink),
14684 BASELINK_ACCESS_BINFO (baselink),
14685 expr, BASELINK_OPTYPE (baselink));
14689 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
14690 expr = build_offset_ref (base, expr, addr);
14693 expr = cp_build_addr_expr (expr, tf_warning_or_error);
14696 else if (good == 0 && badargs)
14697 /* There were no good options and at least one bad one, so let the
14698 user know what the problem is. */
14699 instantiate_template (badfn, badargs, tf_warning_or_error);
14704 /* Subroutine of resolve_overloaded_unification; does deduction for a single
14705 overload. Fills TARGS with any deduced arguments, or error_mark_node if
14706 different overloads deduce different arguments for a given parm.
14707 ADDR_P is true if the expression for which deduction is being
14708 performed was of the form "& fn" rather than simply "fn".
14710 Returns 1 on success. */
14713 try_one_overload (tree tparms,
14718 unification_kind_t strict,
14726 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14727 to function or pointer to member function argument if the set of
14728 overloaded functions does not contain function templates and at most
14729 one of a set of overloaded functions provides a unique match.
14731 So if this is a template, just return success. */
14733 if (uses_template_parms (arg))
14736 if (TREE_CODE (arg) == METHOD_TYPE)
14737 arg = build_ptrmemfunc_type (build_pointer_type (arg));
14739 arg = build_pointer_type (arg);
14741 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
14743 /* We don't copy orig_targs for this because if we have already deduced
14744 some template args from previous args, unify would complain when we
14745 try to deduce a template parameter for the same argument, even though
14746 there isn't really a conflict. */
14747 nargs = TREE_VEC_LENGTH (targs);
14748 tempargs = make_tree_vec (nargs);
14750 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
14753 /* First make sure we didn't deduce anything that conflicts with
14754 explicitly specified args. */
14755 for (i = nargs; i--; )
14757 tree elt = TREE_VEC_ELT (tempargs, i);
14758 tree oldelt = TREE_VEC_ELT (orig_targs, i);
14762 else if (uses_template_parms (elt))
14763 /* Since we're unifying against ourselves, we will fill in
14764 template args used in the function parm list with our own
14765 template parms. Discard them. */
14766 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
14767 else if (oldelt && !template_args_equal (oldelt, elt))
14771 for (i = nargs; i--; )
14773 tree elt = TREE_VEC_ELT (tempargs, i);
14776 TREE_VEC_ELT (targs, i) = elt;
14782 /* PARM is a template class (perhaps with unbound template
14783 parameters). ARG is a fully instantiated type. If ARG can be
14784 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
14785 TARGS are as for unify. */
14788 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
14790 tree copy_of_targs;
14792 if (!CLASSTYPE_TEMPLATE_INFO (arg)
14793 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
14794 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
14797 /* We need to make a new template argument vector for the call to
14798 unify. If we used TARGS, we'd clutter it up with the result of
14799 the attempted unification, even if this class didn't work out.
14800 We also don't want to commit ourselves to all the unifications
14801 we've already done, since unification is supposed to be done on
14802 an argument-by-argument basis. In other words, consider the
14803 following pathological case:
14805 template <int I, int J, int K>
14808 template <int I, int J>
14809 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14811 template <int I, int J, int K>
14812 void f(S<I, J, K>, S<I, I, I>);
14821 Now, by the time we consider the unification involving `s2', we
14822 already know that we must have `f<0, 0, 0>'. But, even though
14823 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14824 because there are two ways to unify base classes of S<0, 1, 2>
14825 with S<I, I, I>. If we kept the already deduced knowledge, we
14826 would reject the possibility I=1. */
14827 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
14829 /* If unification failed, we're done. */
14830 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
14831 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
14837 /* Given a template type PARM and a class type ARG, find the unique
14838 base type in ARG that is an instance of PARM. We do not examine
14839 ARG itself; only its base-classes. If there is not exactly one
14840 appropriate base class, return NULL_TREE. PARM may be the type of
14841 a partial specialization, as well as a plain template type. Used
14845 get_template_base (tree tparms, tree targs, tree parm, tree arg)
14847 tree rval = NULL_TREE;
14850 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
14852 binfo = TYPE_BINFO (complete_type (arg));
14854 /* The type could not be completed. */
14857 /* Walk in inheritance graph order. The search order is not
14858 important, and this avoids multiple walks of virtual bases. */
14859 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
14861 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
14865 /* If there is more than one satisfactory baseclass, then:
14869 If they yield more than one possible deduced A, the type
14873 if (rval && !same_type_p (r, rval))
14883 /* Returns the level of DECL, which declares a template parameter. */
14886 template_decl_level (tree decl)
14888 switch (TREE_CODE (decl))
14891 case TEMPLATE_DECL:
14892 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
14895 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
14898 gcc_unreachable ();
14903 /* Decide whether ARG can be unified with PARM, considering only the
14904 cv-qualifiers of each type, given STRICT as documented for unify.
14905 Returns nonzero iff the unification is OK on that basis. */
14908 check_cv_quals_for_unify (int strict, tree arg, tree parm)
14910 int arg_quals = cp_type_quals (arg);
14911 int parm_quals = cp_type_quals (parm);
14913 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14914 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14916 /* Although a CVR qualifier is ignored when being applied to a
14917 substituted template parameter ([8.3.2]/1 for example), that
14918 does not allow us to unify "const T" with "int&" because both
14919 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14920 It is ok when we're allowing additional CV qualifiers
14921 at the outer level [14.8.2.1]/3,1st bullet. */
14922 if ((TREE_CODE (arg) == REFERENCE_TYPE
14923 || TREE_CODE (arg) == FUNCTION_TYPE
14924 || TREE_CODE (arg) == METHOD_TYPE)
14925 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
14928 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
14929 && (parm_quals & TYPE_QUAL_RESTRICT))
14933 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
14934 && (arg_quals & parm_quals) != parm_quals)
14937 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
14938 && (parm_quals & arg_quals) != arg_quals)
14944 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14946 template_parm_level_and_index (tree parm, int* level, int* index)
14948 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14949 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14950 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14952 *index = TEMPLATE_TYPE_IDX (parm);
14953 *level = TEMPLATE_TYPE_LEVEL (parm);
14957 *index = TEMPLATE_PARM_IDX (parm);
14958 *level = TEMPLATE_PARM_LEVEL (parm);
14962 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14963 expansion at the end of PACKED_PARMS. Returns 0 if the type
14964 deduction succeeds, 1 otherwise. STRICT is the same as in
14965 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14966 call argument list. We'll need to adjust the arguments to make them
14967 types. SUBR tells us if this is from a recursive call to
14968 type_unification_real. */
14970 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
14971 tree packed_args, int strict, bool call_args_p,
14975 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
14976 tree pattern = PACK_EXPANSION_PATTERN (parm);
14977 tree pack, packs = NULL_TREE;
14978 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
14979 int len = TREE_VEC_LENGTH (packed_args);
14981 /* Determine the parameter packs we will be deducing from the
14982 pattern, and record their current deductions. */
14983 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
14984 pack; pack = TREE_CHAIN (pack))
14986 tree parm_pack = TREE_VALUE (pack);
14989 /* Determine the index and level of this parameter pack. */
14990 template_parm_level_and_index (parm_pack, &level, &idx);
14992 /* Keep track of the parameter packs and their corresponding
14994 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
14995 TREE_TYPE (packs) = make_tree_vec (len - start);
14998 /* Loop through all of the arguments that have not yet been
14999 unified and unify each with the pattern. */
15000 for (i = start; i < len; i++)
15002 tree parm = pattern;
15004 /* For each parameter pack, clear out the deduced value so that
15005 we can deduce it again. */
15006 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15009 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15011 TMPL_ARG (targs, level, idx) = NULL_TREE;
15014 /* Unify the pattern with the current argument. */
15016 tree arg = TREE_VEC_ELT (packed_args, i);
15017 tree arg_expr = NULL_TREE;
15018 int arg_strict = strict;
15019 bool skip_arg_p = false;
15025 /* This mirrors what we do in type_unification_real. */
15029 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
15030 | UNIFY_ALLOW_MORE_CV_QUAL
15031 | UNIFY_ALLOW_DERIVED);
15035 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
15039 sub_strict = UNIFY_ALLOW_NONE;
15043 gcc_unreachable ();
15048 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
15049 if (type_unknown_p (arg))
15051 /* [temp.deduct.type] A template-argument can be
15052 deduced from a pointer to function or pointer
15053 to member function argument if the set of
15054 overloaded functions does not contain function
15055 templates and at most one of a set of
15056 overloaded functions provides a unique
15059 if (resolve_overloaded_unification
15060 (tparms, targs, parm, arg,
15061 (unification_kind_t) strict,
15071 arg = unlowered_expr_type (arg);
15072 if (arg == error_mark_node)
15077 arg_strict = sub_strict;
15081 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
15082 &parm, &arg, arg_expr);
15087 /* For deduction from an init-list we need the actual list. */
15088 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
15090 if (unify (tparms, targs, parm, arg, arg_strict))
15095 /* For each parameter pack, collect the deduced value. */
15096 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15099 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15101 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
15102 TMPL_ARG (targs, level, idx);
15106 /* Verify that the results of unification with the parameter packs
15107 produce results consistent with what we've seen before, and make
15108 the deduced argument packs available. */
15109 for (pack = packs; pack; pack = TREE_CHAIN (pack))
15111 tree old_pack = TREE_VALUE (pack);
15112 tree new_args = TREE_TYPE (pack);
15113 int i, len = TREE_VEC_LENGTH (new_args);
15115 bool nondeduced_p = false;
15117 /* By default keep the original deduced argument pack.
15118 If necessary, more specific code is going to update the
15119 resulting deduced argument later down in this function. */
15120 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
15121 TMPL_ARG (targs, level, idx) = old_pack;
15123 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
15124 actually deduce anything. */
15125 for (i = 0; i < len && !nondeduced_p; ++i)
15126 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
15127 nondeduced_p = true;
15131 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
15133 /* Prepend the explicit arguments onto NEW_ARGS. */
15134 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15135 tree old_args = new_args;
15136 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
15137 int len = explicit_len + TREE_VEC_LENGTH (old_args);
15139 /* Copy the explicit arguments. */
15140 new_args = make_tree_vec (len);
15141 for (i = 0; i < explicit_len; i++)
15142 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
15144 /* Copy the deduced arguments. */
15145 for (; i < len; i++)
15146 TREE_VEC_ELT (new_args, i) =
15147 TREE_VEC_ELT (old_args, i - explicit_len);
15153 /* Build the deduced *_ARGUMENT_PACK. */
15154 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
15156 result = make_node (NONTYPE_ARGUMENT_PACK);
15157 TREE_TYPE (result) =
15158 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
15159 TREE_CONSTANT (result) = 1;
15162 result = cxx_make_type (TYPE_ARGUMENT_PACK);
15164 SET_ARGUMENT_PACK_ARGS (result, new_args);
15166 /* Note the deduced argument packs for this parameter
15168 TMPL_ARG (targs, level, idx) = result;
15170 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
15171 && (ARGUMENT_PACK_ARGS (old_pack)
15172 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
15174 /* We only had the explicitly-provided arguments before, but
15175 now we have a complete set of arguments. */
15176 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
15178 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
15179 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
15180 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
15182 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
15184 /* Inconsistent unification of this parameter pack. */
15191 /* Deduce the value of template parameters. TPARMS is the (innermost)
15192 set of template parameters to a template. TARGS is the bindings
15193 for those template parameters, as determined thus far; TARGS may
15194 include template arguments for outer levels of template parameters
15195 as well. PARM is a parameter to a template function, or a
15196 subcomponent of that parameter; ARG is the corresponding argument.
15197 This function attempts to match PARM with ARG in a manner
15198 consistent with the existing assignments in TARGS. If more values
15199 are deduced, then TARGS is updated.
15201 Returns 0 if the type deduction succeeds, 1 otherwise. The
15202 parameter STRICT is a bitwise or of the following flags:
15205 Require an exact match between PARM and ARG.
15206 UNIFY_ALLOW_MORE_CV_QUAL:
15207 Allow the deduced ARG to be more cv-qualified (by qualification
15208 conversion) than ARG.
15209 UNIFY_ALLOW_LESS_CV_QUAL:
15210 Allow the deduced ARG to be less cv-qualified than ARG.
15211 UNIFY_ALLOW_DERIVED:
15212 Allow the deduced ARG to be a template base class of ARG,
15213 or a pointer to a template base class of the type pointed to by
15215 UNIFY_ALLOW_INTEGER:
15216 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
15217 case for more information.
15218 UNIFY_ALLOW_OUTER_LEVEL:
15219 This is the outermost level of a deduction. Used to determine validity
15220 of qualification conversions. A valid qualification conversion must
15221 have const qualified pointers leading up to the inner type which
15222 requires additional CV quals, except at the outer level, where const
15223 is not required [conv.qual]. It would be normal to set this flag in
15224 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
15225 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
15226 This is the outermost level of a deduction, and PARM can be more CV
15227 qualified at this point.
15228 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
15229 This is the outermost level of a deduction, and PARM can be less CV
15230 qualified at this point. */
15233 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
15238 int strict_in = strict;
15240 /* I don't think this will do the right thing with respect to types.
15241 But the only case I've seen it in so far has been array bounds, where
15242 signedness is the only information lost, and I think that will be
15244 while (TREE_CODE (parm) == NOP_EXPR)
15245 parm = TREE_OPERAND (parm, 0);
15247 if (arg == error_mark_node)
15249 if (arg == unknown_type_node
15250 || arg == init_list_type_node)
15251 /* We can't deduce anything from this, but we might get all the
15252 template args from other function args. */
15255 /* If PARM uses template parameters, then we can't bail out here,
15256 even if ARG == PARM, since we won't record unifications for the
15257 template parameters. We might need them if we're trying to
15258 figure out which of two things is more specialized. */
15259 if (arg == parm && !uses_template_parms (parm))
15262 /* Handle init lists early, so the rest of the function can assume
15263 we're dealing with a type. */
15264 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
15268 tree orig_parm = parm;
15270 /* Replace T with std::initializer_list<T> for deduction. */
15271 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15272 && flag_deduce_init_list)
15273 parm = listify (parm);
15275 if (!is_std_init_list (parm))
15276 /* We can only deduce from an initializer list argument if the
15277 parameter is std::initializer_list; otherwise this is a
15278 non-deduced context. */
15281 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
15283 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
15285 int elt_strict = strict;
15287 if (elt == error_mark_node)
15290 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
15292 tree type = TREE_TYPE (elt);
15293 /* It should only be possible to get here for a call. */
15294 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
15295 elt_strict |= maybe_adjust_types_for_deduction
15296 (DEDUCE_CALL, &elttype, &type, elt);
15300 if (unify (tparms, targs, elttype, elt, elt_strict))
15304 /* If the std::initializer_list<T> deduction worked, replace the
15305 deduced A with std::initializer_list<A>. */
15306 if (orig_parm != parm)
15308 idx = TEMPLATE_TYPE_IDX (orig_parm);
15309 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15310 targ = listify (targ);
15311 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
15316 /* Immediately reject some pairs that won't unify because of
15317 cv-qualification mismatches. */
15318 if (TREE_CODE (arg) == TREE_CODE (parm)
15320 /* It is the elements of the array which hold the cv quals of an array
15321 type, and the elements might be template type parms. We'll check
15322 when we recurse. */
15323 && TREE_CODE (arg) != ARRAY_TYPE
15324 /* We check the cv-qualifiers when unifying with template type
15325 parameters below. We want to allow ARG `const T' to unify with
15326 PARM `T' for example, when computing which of two templates
15327 is more specialized, for example. */
15328 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
15329 && !check_cv_quals_for_unify (strict_in, arg, parm))
15332 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
15333 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
15334 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
15335 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
15336 strict &= ~UNIFY_ALLOW_DERIVED;
15337 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
15338 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
15340 switch (TREE_CODE (parm))
15342 case TYPENAME_TYPE:
15344 case UNBOUND_CLASS_TEMPLATE:
15345 /* In a type which contains a nested-name-specifier, template
15346 argument values cannot be deduced for template parameters used
15347 within the nested-name-specifier. */
15350 case TEMPLATE_TYPE_PARM:
15351 case TEMPLATE_TEMPLATE_PARM:
15352 case BOUND_TEMPLATE_TEMPLATE_PARM:
15353 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
15354 if (tparm == error_mark_node)
15357 if (TEMPLATE_TYPE_LEVEL (parm)
15358 != template_decl_level (tparm))
15359 /* The PARM is not one we're trying to unify. Just check
15360 to see if it matches ARG. */
15361 return (TREE_CODE (arg) == TREE_CODE (parm)
15362 && same_type_p (parm, arg)) ? 0 : 1;
15363 idx = TEMPLATE_TYPE_IDX (parm);
15364 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15365 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
15367 /* Check for mixed types and values. */
15368 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
15369 && TREE_CODE (tparm) != TYPE_DECL)
15370 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15371 && TREE_CODE (tparm) != TEMPLATE_DECL))
15374 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15376 /* ARG must be constructed from a template class or a template
15377 template parameter. */
15378 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
15379 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
15383 tree parmvec = TYPE_TI_ARGS (parm);
15384 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
15385 tree full_argvec = add_to_template_args (targs, argvec);
15387 = DECL_INNERMOST_TEMPLATE_PARMS
15388 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
15390 int parm_variadic_p = 0;
15392 /* The resolution to DR150 makes clear that default
15393 arguments for an N-argument may not be used to bind T
15394 to a template template parameter with fewer than N
15395 parameters. It is not safe to permit the binding of
15396 default arguments as an extension, as that may change
15397 the meaning of a conforming program. Consider:
15399 struct Dense { static const unsigned int dim = 1; };
15401 template <template <typename> class View,
15403 void operator+(float, View<Block> const&);
15405 template <typename Block,
15406 unsigned int Dim = Block::dim>
15407 struct Lvalue_proxy { operator float() const; };
15411 Lvalue_proxy<Dense> p;
15416 Here, if Lvalue_proxy is permitted to bind to View, then
15417 the global operator+ will be used; if they are not, the
15418 Lvalue_proxy will be converted to float. */
15419 if (coerce_template_parms (parm_parms,
15421 TYPE_TI_TEMPLATE (parm),
15423 /*require_all_args=*/true,
15424 /*use_default_args=*/false)
15425 == error_mark_node)
15428 /* Deduce arguments T, i from TT<T> or TT<i>.
15429 We check each element of PARMVEC and ARGVEC individually
15430 rather than the whole TREE_VEC since they can have
15431 different number of elements. */
15433 parmvec = expand_template_argument_pack (parmvec);
15434 argvec = expand_template_argument_pack (argvec);
15436 len = TREE_VEC_LENGTH (parmvec);
15438 /* Check if the parameters end in a pack, making them
15441 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
15442 parm_variadic_p = 1;
15444 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
15447 for (i = 0; i < len - parm_variadic_p; ++i)
15449 if (unify (tparms, targs,
15450 TREE_VEC_ELT (parmvec, i),
15451 TREE_VEC_ELT (argvec, i),
15456 if (parm_variadic_p
15457 && unify_pack_expansion (tparms, targs,
15460 /*call_args_p=*/false,
15464 arg = TYPE_TI_TEMPLATE (arg);
15466 /* Fall through to deduce template name. */
15469 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
15470 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
15472 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
15474 /* Simple cases: Value already set, does match or doesn't. */
15475 if (targ != NULL_TREE && template_args_equal (targ, arg))
15482 /* If PARM is `const T' and ARG is only `int', we don't have
15483 a match unless we are allowing additional qualification.
15484 If ARG is `const int' and PARM is just `T' that's OK;
15485 that binds `const int' to `T'. */
15486 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
15490 /* Consider the case where ARG is `const volatile int' and
15491 PARM is `const T'. Then, T should be `volatile int'. */
15492 arg = cp_build_qualified_type_real
15493 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
15494 if (arg == error_mark_node)
15497 /* Simple cases: Value already set, does match or doesn't. */
15498 if (targ != NULL_TREE && same_type_p (targ, arg))
15503 /* Make sure that ARG is not a variable-sized array. (Note
15504 that were talking about variable-sized arrays (like
15505 `int[n]'), rather than arrays of unknown size (like
15506 `int[]').) We'll get very confused by such a type since
15507 the bound of the array will not be computable in an
15508 instantiation. Besides, such types are not allowed in
15509 ISO C++, so we can do as we please here. */
15510 if (variably_modified_type_p (arg, NULL_TREE))
15513 /* Strip typedefs as in convert_template_argument. */
15514 arg = canonicalize_type_argument (arg, tf_none);
15517 /* If ARG is a parameter pack or an expansion, we cannot unify
15518 against it unless PARM is also a parameter pack. */
15519 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
15520 && !template_parameter_pack_p (parm))
15523 /* If the argument deduction results is a METHOD_TYPE,
15524 then there is a problem.
15525 METHOD_TYPE doesn't map to any real C++ type the result of
15526 the deduction can not be of that type. */
15527 if (TREE_CODE (arg) == METHOD_TYPE)
15530 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
15533 case TEMPLATE_PARM_INDEX:
15534 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
15535 if (tparm == error_mark_node)
15538 if (TEMPLATE_PARM_LEVEL (parm)
15539 != template_decl_level (tparm))
15540 /* The PARM is not one we're trying to unify. Just check
15541 to see if it matches ARG. */
15542 return !(TREE_CODE (arg) == TREE_CODE (parm)
15543 && cp_tree_equal (parm, arg));
15545 idx = TEMPLATE_PARM_IDX (parm);
15546 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
15549 return !cp_tree_equal (targ, arg);
15551 /* [temp.deduct.type] If, in the declaration of a function template
15552 with a non-type template-parameter, the non-type
15553 template-parameter is used in an expression in the function
15554 parameter-list and, if the corresponding template-argument is
15555 deduced, the template-argument type shall match the type of the
15556 template-parameter exactly, except that a template-argument
15557 deduced from an array bound may be of any integral type.
15558 The non-type parameter might use already deduced type parameters. */
15559 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
15560 if (!TREE_TYPE (arg))
15561 /* Template-parameter dependent expression. Just accept it for now.
15562 It will later be processed in convert_template_argument. */
15564 else if (same_type_p (TREE_TYPE (arg), tparm))
15566 else if ((strict & UNIFY_ALLOW_INTEGER)
15567 && (TREE_CODE (tparm) == INTEGER_TYPE
15568 || TREE_CODE (tparm) == BOOLEAN_TYPE))
15569 /* Convert the ARG to the type of PARM; the deduced non-type
15570 template argument must exactly match the types of the
15571 corresponding parameter. */
15572 arg = fold (build_nop (tparm, arg));
15573 else if (uses_template_parms (tparm))
15574 /* We haven't deduced the type of this parameter yet. Try again
15580 /* If ARG is a parameter pack or an expansion, we cannot unify
15581 against it unless PARM is also a parameter pack. */
15582 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
15583 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
15586 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
15591 /* A pointer-to-member constant can be unified only with
15592 another constant. */
15593 if (TREE_CODE (arg) != PTRMEM_CST)
15596 /* Just unify the class member. It would be useless (and possibly
15597 wrong, depending on the strict flags) to unify also
15598 PTRMEM_CST_CLASS, because we want to be sure that both parm and
15599 arg refer to the same variable, even if through different
15600 classes. For instance:
15602 struct A { int x; };
15605 Unification of &A::x and &B::x must succeed. */
15606 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
15607 PTRMEM_CST_MEMBER (arg), strict);
15612 if (TREE_CODE (arg) != POINTER_TYPE)
15615 /* [temp.deduct.call]
15617 A can be another pointer or pointer to member type that can
15618 be converted to the deduced A via a qualification
15619 conversion (_conv.qual_).
15621 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
15622 This will allow for additional cv-qualification of the
15623 pointed-to types if appropriate. */
15625 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
15626 /* The derived-to-base conversion only persists through one
15627 level of pointers. */
15628 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
15630 return unify (tparms, targs, TREE_TYPE (parm),
15631 TREE_TYPE (arg), strict);
15634 case REFERENCE_TYPE:
15635 if (TREE_CODE (arg) != REFERENCE_TYPE)
15637 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15638 strict & UNIFY_ALLOW_MORE_CV_QUAL);
15641 if (TREE_CODE (arg) != ARRAY_TYPE)
15643 if ((TYPE_DOMAIN (parm) == NULL_TREE)
15644 != (TYPE_DOMAIN (arg) == NULL_TREE))
15646 if (TYPE_DOMAIN (parm) != NULL_TREE)
15653 /* Our representation of array types uses "N - 1" as the
15654 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
15655 not an integer constant. We cannot unify arbitrarily
15656 complex expressions, so we eliminate the MINUS_EXPRs
15658 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
15659 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
15662 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
15663 parm_max = TREE_OPERAND (parm_max, 0);
15665 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
15666 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
15669 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
15670 trying to unify the type of a variable with the type
15671 of a template parameter. For example:
15673 template <unsigned int N>
15674 void f (char (&) [N]);
15681 Here, the type of the ARG will be "int [g(i)]", and
15682 may be a SAVE_EXPR, etc. */
15683 if (TREE_CODE (arg_max) != MINUS_EXPR)
15685 arg_max = TREE_OPERAND (arg_max, 0);
15688 /* If only one of the bounds used a MINUS_EXPR, compensate
15689 by adding one to the other bound. */
15690 if (parm_cst && !arg_cst)
15691 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
15695 else if (arg_cst && !parm_cst)
15696 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
15701 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
15704 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15705 strict & UNIFY_ALLOW_MORE_CV_QUAL);
15712 case ENUMERAL_TYPE:
15714 if (TREE_CODE (arg) != TREE_CODE (parm))
15717 /* We have already checked cv-qualification at the top of the
15719 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
15722 /* As far as unification is concerned, this wins. Later checks
15723 will invalidate it if necessary. */
15726 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
15727 /* Type INTEGER_CST can come from ordinary constant template args. */
15729 while (TREE_CODE (arg) == NOP_EXPR)
15730 arg = TREE_OPERAND (arg, 0);
15732 if (TREE_CODE (arg) != INTEGER_CST)
15734 return !tree_int_cst_equal (parm, arg);
15739 if (TREE_CODE (arg) != TREE_VEC)
15741 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
15743 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
15744 if (unify (tparms, targs,
15745 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
15753 if (TREE_CODE (arg) != TREE_CODE (parm))
15756 if (TYPE_PTRMEMFUNC_P (parm))
15758 if (!TYPE_PTRMEMFUNC_P (arg))
15761 return unify (tparms, targs,
15762 TYPE_PTRMEMFUNC_FN_TYPE (parm),
15763 TYPE_PTRMEMFUNC_FN_TYPE (arg),
15767 if (CLASSTYPE_TEMPLATE_INFO (parm))
15769 tree t = NULL_TREE;
15771 if (strict_in & UNIFY_ALLOW_DERIVED)
15773 /* First, we try to unify the PARM and ARG directly. */
15774 t = try_class_unification (tparms, targs,
15779 /* Fallback to the special case allowed in
15780 [temp.deduct.call]:
15782 If P is a class, and P has the form
15783 template-id, then A can be a derived class of
15784 the deduced A. Likewise, if P is a pointer to
15785 a class of the form template-id, A can be a
15786 pointer to a derived class pointed to by the
15788 t = get_template_base (tparms, targs, parm, arg);
15794 else if (CLASSTYPE_TEMPLATE_INFO (arg)
15795 && (CLASSTYPE_TI_TEMPLATE (parm)
15796 == CLASSTYPE_TI_TEMPLATE (arg)))
15797 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15798 Then, we should unify `int' and `U'. */
15801 /* There's no chance of unification succeeding. */
15804 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
15805 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
15807 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
15812 case FUNCTION_TYPE:
15814 unsigned int nargs;
15819 if (TREE_CODE (arg) != TREE_CODE (parm))
15822 /* CV qualifications for methods can never be deduced, they must
15823 match exactly. We need to check them explicitly here,
15824 because type_unification_real treats them as any other
15825 cv-qualified parameter. */
15826 if (TREE_CODE (parm) == METHOD_TYPE
15827 && (!check_cv_quals_for_unify
15829 class_of_this_parm (arg),
15830 class_of_this_parm (parm))))
15833 if (unify (tparms, targs, TREE_TYPE (parm),
15834 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
15837 nargs = list_length (TYPE_ARG_TYPES (arg));
15838 args = XALLOCAVEC (tree, nargs);
15839 for (a = TYPE_ARG_TYPES (arg), i = 0;
15840 a != NULL_TREE && a != void_list_node;
15841 a = TREE_CHAIN (a), ++i)
15842 args[i] = TREE_VALUE (a);
15845 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
15846 args, nargs, 1, DEDUCE_EXACT,
15851 /* Unify a pointer to member with a pointer to member function, which
15852 deduces the type of the member as a function type. */
15853 if (TYPE_PTRMEMFUNC_P (arg))
15858 /* Check top-level cv qualifiers */
15859 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
15862 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15863 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
15866 /* Determine the type of the function we are unifying against. */
15867 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
15869 build_function_type (TREE_TYPE (method_type),
15870 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
15872 /* Extract the cv-qualifiers of the member function from the
15873 implicit object parameter and place them on the function
15874 type to be restored later. */
15875 fntype = apply_memfn_quals (fntype, type_memfn_quals (method_type));
15876 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
15879 if (TREE_CODE (arg) != OFFSET_TYPE)
15881 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
15882 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
15884 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
15888 if (DECL_TEMPLATE_PARM_P (parm))
15889 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
15890 if (arg != integral_constant_value (parm))
15895 case TEMPLATE_DECL:
15896 /* Matched cases are handled by the ARG == PARM test above. */
15900 /* A non-type template parameter that is a variable should be a
15901 an integral constant, in which case, it whould have been
15902 folded into its (constant) value. So we should not be getting
15903 a variable here. */
15904 gcc_unreachable ();
15906 case TYPE_ARGUMENT_PACK:
15907 case NONTYPE_ARGUMENT_PACK:
15909 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
15910 tree packed_args = ARGUMENT_PACK_ARGS (arg);
15911 int i, len = TREE_VEC_LENGTH (packed_parms);
15912 int argslen = TREE_VEC_LENGTH (packed_args);
15913 int parm_variadic_p = 0;
15915 for (i = 0; i < len; ++i)
15917 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
15920 /* We can unify against something with a trailing
15922 parm_variadic_p = 1;
15924 /* Since there is something following the pack
15925 expansion, we cannot unify this template argument
15932 /* If we don't have enough arguments to satisfy the parameters
15933 (not counting the pack expression at the end), or we have
15934 too many arguments for a parameter list that doesn't end in
15935 a pack expression, we can't unify. */
15936 if (argslen < (len - parm_variadic_p)
15937 || (argslen > len && !parm_variadic_p))
15940 /* Unify all of the parameters that precede the (optional)
15941 pack expression. */
15942 for (i = 0; i < len - parm_variadic_p; ++i)
15944 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
15945 TREE_VEC_ELT (packed_args, i), strict))
15949 if (parm_variadic_p)
15950 return unify_pack_expansion (tparms, targs,
15951 packed_parms, packed_args,
15952 strict, /*call_args_p=*/false,
15960 case DECLTYPE_TYPE:
15961 case UNDERLYING_TYPE:
15962 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
15963 or UNDERLYING_TYPE nodes. */
15967 /* Unification fails if we hit an error node. */
15971 /* An unresolved overload is a nondeduced context. */
15972 if (type_unknown_p (parm))
15974 gcc_assert (EXPR_P (parm));
15976 /* We must be looking at an expression. This can happen with
15980 void foo(S<I>, S<I + 2>);
15982 This is a "nondeduced context":
15986 The nondeduced contexts are:
15988 --A type that is a template-id in which one or more of
15989 the template-arguments is an expression that references
15990 a template-parameter.
15992 In these cases, we assume deduction succeeded, but don't
15993 actually infer any unifications. */
15995 if (!uses_template_parms (parm)
15996 && !template_args_equal (parm, arg))
16003 /* Note that DECL can be defined in this translation unit, if
16007 mark_definable (tree decl)
16010 DECL_NOT_REALLY_EXTERN (decl) = 1;
16011 FOR_EACH_CLONE (clone, decl)
16012 DECL_NOT_REALLY_EXTERN (clone) = 1;
16015 /* Called if RESULT is explicitly instantiated, or is a member of an
16016 explicitly instantiated class. */
16019 mark_decl_instantiated (tree result, int extern_p)
16021 SET_DECL_EXPLICIT_INSTANTIATION (result);
16023 /* If this entity has already been written out, it's too late to
16024 make any modifications. */
16025 if (TREE_ASM_WRITTEN (result))
16028 if (TREE_CODE (result) != FUNCTION_DECL)
16029 /* The TREE_PUBLIC flag for function declarations will have been
16030 set correctly by tsubst. */
16031 TREE_PUBLIC (result) = 1;
16033 /* This might have been set by an earlier implicit instantiation. */
16034 DECL_COMDAT (result) = 0;
16037 DECL_NOT_REALLY_EXTERN (result) = 0;
16040 mark_definable (result);
16041 /* Always make artificials weak. */
16042 if (DECL_ARTIFICIAL (result) && flag_weak)
16043 comdat_linkage (result);
16044 /* For WIN32 we also want to put explicit instantiations in
16045 linkonce sections. */
16046 else if (TREE_PUBLIC (result))
16047 maybe_make_one_only (result);
16050 /* If EXTERN_P, then this function will not be emitted -- unless
16051 followed by an explicit instantiation, at which point its linkage
16052 will be adjusted. If !EXTERN_P, then this function will be
16053 emitted here. In neither circumstance do we want
16054 import_export_decl to adjust the linkage. */
16055 DECL_INTERFACE_KNOWN (result) = 1;
16058 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16059 important template arguments. If any are missing, we check whether
16060 they're important by using error_mark_node for substituting into any
16061 args that were used for partial ordering (the ones between ARGS and END)
16062 and seeing if it bubbles up. */
16065 check_undeduced_parms (tree targs, tree args, tree end)
16067 bool found = false;
16069 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
16070 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
16073 TREE_VEC_ELT (targs, i) = error_mark_node;
16077 for (; args != end; args = TREE_CHAIN (args))
16079 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
16080 if (substed == error_mark_node)
16087 /* Given two function templates PAT1 and PAT2, return:
16089 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
16090 -1 if PAT2 is more specialized than PAT1.
16091 0 if neither is more specialized.
16093 LEN indicates the number of parameters we should consider
16094 (defaulted parameters should not be considered).
16096 The 1998 std underspecified function template partial ordering, and
16097 DR214 addresses the issue. We take pairs of arguments, one from
16098 each of the templates, and deduce them against each other. One of
16099 the templates will be more specialized if all the *other*
16100 template's arguments deduce against its arguments and at least one
16101 of its arguments *does* *not* deduce against the other template's
16102 corresponding argument. Deduction is done as for class templates.
16103 The arguments used in deduction have reference and top level cv
16104 qualifiers removed. Iff both arguments were originally reference
16105 types *and* deduction succeeds in both directions, the template
16106 with the more cv-qualified argument wins for that pairing (if
16107 neither is more cv-qualified, they both are equal). Unlike regular
16108 deduction, after all the arguments have been deduced in this way,
16109 we do *not* verify the deduced template argument values can be
16110 substituted into non-deduced contexts.
16112 The logic can be a bit confusing here, because we look at deduce1 and
16113 targs1 to see if pat2 is at least as specialized, and vice versa; if we
16114 can find template arguments for pat1 to make arg1 look like arg2, that
16115 means that arg2 is at least as specialized as arg1. */
16118 more_specialized_fn (tree pat1, tree pat2, int len)
16120 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
16121 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
16122 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
16123 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
16124 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
16125 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
16126 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
16127 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
16128 tree origs1, origs2;
16129 bool lose1 = false;
16130 bool lose2 = false;
16132 /* Remove the this parameter from non-static member functions. If
16133 one is a non-static member function and the other is not a static
16134 member function, remove the first parameter from that function
16135 also. This situation occurs for operator functions where we
16136 locate both a member function (with this pointer) and non-member
16137 operator (with explicit first operand). */
16138 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
16140 len--; /* LEN is the number of significant arguments for DECL1 */
16141 args1 = TREE_CHAIN (args1);
16142 if (!DECL_STATIC_FUNCTION_P (decl2))
16143 args2 = TREE_CHAIN (args2);
16145 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
16147 args2 = TREE_CHAIN (args2);
16148 if (!DECL_STATIC_FUNCTION_P (decl1))
16151 args1 = TREE_CHAIN (args1);
16155 /* If only one is a conversion operator, they are unordered. */
16156 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
16159 /* Consider the return type for a conversion function */
16160 if (DECL_CONV_FN_P (decl1))
16162 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
16163 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
16167 processing_template_decl++;
16173 /* Stop when an ellipsis is seen. */
16174 && args1 != NULL_TREE && args2 != NULL_TREE)
16176 tree arg1 = TREE_VALUE (args1);
16177 tree arg2 = TREE_VALUE (args2);
16178 int deduce1, deduce2;
16182 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16183 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16185 /* When both arguments are pack expansions, we need only
16186 unify the patterns themselves. */
16187 arg1 = PACK_EXPANSION_PATTERN (arg1);
16188 arg2 = PACK_EXPANSION_PATTERN (arg2);
16190 /* This is the last comparison we need to do. */
16194 if (TREE_CODE (arg1) == REFERENCE_TYPE)
16196 arg1 = TREE_TYPE (arg1);
16197 quals1 = cp_type_quals (arg1);
16200 if (TREE_CODE (arg2) == REFERENCE_TYPE)
16202 arg2 = TREE_TYPE (arg2);
16203 quals2 = cp_type_quals (arg2);
16206 if ((quals1 < 0) != (quals2 < 0))
16208 /* Only of the args is a reference, see if we should apply
16209 array/function pointer decay to it. This is not part of
16210 DR214, but is, IMHO, consistent with the deduction rules
16211 for the function call itself, and with our earlier
16212 implementation of the underspecified partial ordering
16213 rules. (nathan). */
16216 switch (TREE_CODE (arg1))
16219 arg1 = TREE_TYPE (arg1);
16221 case FUNCTION_TYPE:
16222 arg1 = build_pointer_type (arg1);
16231 switch (TREE_CODE (arg2))
16234 arg2 = TREE_TYPE (arg2);
16236 case FUNCTION_TYPE:
16237 arg2 = build_pointer_type (arg2);
16246 arg1 = TYPE_MAIN_VARIANT (arg1);
16247 arg2 = TYPE_MAIN_VARIANT (arg2);
16249 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
16251 int i, len2 = list_length (args2);
16252 tree parmvec = make_tree_vec (1);
16253 tree argvec = make_tree_vec (len2);
16256 /* Setup the parameter vector, which contains only ARG1. */
16257 TREE_VEC_ELT (parmvec, 0) = arg1;
16259 /* Setup the argument vector, which contains the remaining
16261 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
16262 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16264 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
16265 argvec, UNIFY_ALLOW_NONE,
16266 /*call_args_p=*/false,
16269 /* We cannot deduce in the other direction, because ARG1 is
16270 a pack expansion but ARG2 is not. */
16273 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16275 int i, len1 = list_length (args1);
16276 tree parmvec = make_tree_vec (1);
16277 tree argvec = make_tree_vec (len1);
16280 /* Setup the parameter vector, which contains only ARG1. */
16281 TREE_VEC_ELT (parmvec, 0) = arg2;
16283 /* Setup the argument vector, which contains the remaining
16285 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
16286 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
16288 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
16289 argvec, UNIFY_ALLOW_NONE,
16290 /*call_args_p=*/false,
16293 /* We cannot deduce in the other direction, because ARG2 is
16294 a pack expansion but ARG1 is not.*/
16300 /* The normal case, where neither argument is a pack
16302 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
16303 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
16306 /* If we couldn't deduce arguments for tparms1 to make arg1 match
16307 arg2, then arg2 is not as specialized as arg1. */
16313 /* "If, for a given type, deduction succeeds in both directions
16314 (i.e., the types are identical after the transformations above)
16315 and if the type from the argument template is more cv-qualified
16316 than the type from the parameter template (as described above)
16317 that type is considered to be more specialized than the other. If
16318 neither type is more cv-qualified than the other then neither type
16319 is more specialized than the other." */
16321 if (deduce1 && deduce2
16322 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
16324 if ((quals1 & quals2) == quals2)
16326 if ((quals1 & quals2) == quals1)
16330 if (lose1 && lose2)
16331 /* We've failed to deduce something in either direction.
16332 These must be unordered. */
16335 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
16336 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
16337 /* We have already processed all of the arguments in our
16338 handing of the pack expansion type. */
16341 args1 = TREE_CHAIN (args1);
16342 args2 = TREE_CHAIN (args2);
16345 /* "In most cases, all template parameters must have values in order for
16346 deduction to succeed, but for partial ordering purposes a template
16347 parameter may remain without a value provided it is not used in the
16348 types being used for partial ordering."
16350 Thus, if we are missing any of the targs1 we need to substitute into
16351 origs1, then pat2 is not as specialized as pat1. This can happen when
16352 there is a nondeduced context. */
16353 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
16355 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
16358 processing_template_decl--;
16360 /* All things being equal, if the next argument is a pack expansion
16361 for one function but not for the other, prefer the
16362 non-variadic function. FIXME this is bogus; see c++/41958. */
16364 && args1 && TREE_VALUE (args1)
16365 && args2 && TREE_VALUE (args2))
16367 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
16368 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
16371 if (lose1 == lose2)
16379 /* Determine which of two partial specializations is more specialized.
16381 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
16382 to the first partial specialization. The TREE_VALUE is the
16383 innermost set of template parameters for the partial
16384 specialization. PAT2 is similar, but for the second template.
16386 Return 1 if the first partial specialization is more specialized;
16387 -1 if the second is more specialized; 0 if neither is more
16390 See [temp.class.order] for information about determining which of
16391 two templates is more specialized. */
16394 more_specialized_class (tree pat1, tree pat2)
16399 bool any_deductions = false;
16401 tmpl1 = TREE_TYPE (pat1);
16402 tmpl2 = TREE_TYPE (pat2);
16404 /* Just like what happens for functions, if we are ordering between
16405 different class template specializations, we may encounter dependent
16406 types in the arguments, and we need our dependency check functions
16407 to behave correctly. */
16408 ++processing_template_decl;
16409 targs = get_class_bindings (TREE_VALUE (pat1),
16410 CLASSTYPE_TI_ARGS (tmpl1),
16411 CLASSTYPE_TI_ARGS (tmpl2));
16415 any_deductions = true;
16418 targs = get_class_bindings (TREE_VALUE (pat2),
16419 CLASSTYPE_TI_ARGS (tmpl2),
16420 CLASSTYPE_TI_ARGS (tmpl1));
16424 any_deductions = true;
16426 --processing_template_decl;
16428 /* In the case of a tie where at least one of the class templates
16429 has a parameter pack at the end, the template with the most
16430 non-packed parameters wins. */
16433 && (template_args_variadic_p (TREE_PURPOSE (pat1))
16434 || template_args_variadic_p (TREE_PURPOSE (pat2))))
16436 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
16437 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
16438 int len1 = TREE_VEC_LENGTH (args1);
16439 int len2 = TREE_VEC_LENGTH (args2);
16441 /* We don't count the pack expansion at the end. */
16442 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
16444 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
16449 else if (len1 < len2)
16456 /* Return the template arguments that will produce the function signature
16457 DECL from the function template FN, with the explicit template
16458 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
16459 also match. Return NULL_TREE if no satisfactory arguments could be
16463 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
16465 int ntparms = DECL_NTPARMS (fn);
16466 tree targs = make_tree_vec (ntparms);
16468 tree decl_arg_types;
16470 unsigned int nargs, ix;
16473 /* Substitute the explicit template arguments into the type of DECL.
16474 The call to fn_type_unification will handle substitution into the
16476 decl_type = TREE_TYPE (decl);
16477 if (explicit_args && uses_template_parms (decl_type))
16480 tree converted_args;
16482 if (DECL_TEMPLATE_INFO (decl))
16483 tmpl = DECL_TI_TEMPLATE (decl);
16485 /* We can get here for some invalid specializations. */
16489 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16490 explicit_args, NULL_TREE,
16492 /*require_all_args=*/false,
16493 /*use_default_args=*/false);
16494 if (converted_args == error_mark_node)
16497 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
16498 if (decl_type == error_mark_node)
16502 /* Never do unification on the 'this' parameter. */
16503 decl_arg_types = skip_artificial_parms_for (decl,
16504 TYPE_ARG_TYPES (decl_type));
16506 nargs = list_length (decl_arg_types);
16507 args = XALLOCAVEC (tree, nargs);
16508 for (arg = decl_arg_types, ix = 0;
16509 arg != NULL_TREE && arg != void_list_node;
16510 arg = TREE_CHAIN (arg), ++ix)
16511 args[ix] = TREE_VALUE (arg);
16513 if (fn_type_unification (fn, explicit_args, targs,
16515 (check_rettype || DECL_CONV_FN_P (fn)
16516 ? TREE_TYPE (decl_type) : NULL_TREE),
16517 DEDUCE_EXACT, LOOKUP_NORMAL))
16523 /* Return the innermost template arguments that, when applied to a
16524 template specialization whose innermost template parameters are
16525 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
16528 For example, suppose we have:
16530 template <class T, class U> struct S {};
16531 template <class T> struct S<T*, int> {};
16533 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
16534 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
16535 int}. The resulting vector will be {double}, indicating that `T'
16536 is bound to `double'. */
16539 get_class_bindings (tree tparms, tree spec_args, tree args)
16541 int i, ntparms = TREE_VEC_LENGTH (tparms);
16543 tree innermost_deduced_args;
16545 innermost_deduced_args = make_tree_vec (ntparms);
16546 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16548 deduced_args = copy_node (args);
16549 SET_TMPL_ARGS_LEVEL (deduced_args,
16550 TMPL_ARGS_DEPTH (deduced_args),
16551 innermost_deduced_args);
16554 deduced_args = innermost_deduced_args;
16556 if (unify (tparms, deduced_args,
16557 INNERMOST_TEMPLATE_ARGS (spec_args),
16558 INNERMOST_TEMPLATE_ARGS (args),
16562 for (i = 0; i < ntparms; ++i)
16563 if (! TREE_VEC_ELT (innermost_deduced_args, i))
16566 /* Verify that nondeduced template arguments agree with the type
16567 obtained from argument deduction.
16571 struct A { typedef int X; };
16572 template <class T, class U> struct C {};
16573 template <class T> struct C<T, typename T::X> {};
16575 Then with the instantiation `C<A, int>', we can deduce that
16576 `T' is `A' but unify () does not check whether `typename T::X'
16578 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
16579 if (spec_args == error_mark_node
16580 /* We only need to check the innermost arguments; the other
16581 arguments will always agree. */
16582 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
16583 INNERMOST_TEMPLATE_ARGS (args)))
16586 /* Now that we have bindings for all of the template arguments,
16587 ensure that the arguments deduced for the template template
16588 parameters have compatible template parameter lists. See the use
16589 of template_template_parm_bindings_ok_p in fn_type_unification
16590 for more information. */
16591 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
16594 return deduced_args;
16597 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
16598 Return the TREE_LIST node with the most specialized template, if
16599 any. If there is no most specialized template, the error_mark_node
16602 Note that this function does not look at, or modify, the
16603 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
16604 returned is one of the elements of INSTANTIATIONS, callers may
16605 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
16606 and retrieve it from the value returned. */
16609 most_specialized_instantiation (tree templates)
16613 ++processing_template_decl;
16616 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
16620 if (get_bindings (TREE_VALUE (champ),
16621 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
16622 NULL_TREE, /*check_ret=*/true))
16625 if (get_bindings (TREE_VALUE (fn),
16626 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
16627 NULL_TREE, /*check_ret=*/true))
16634 /* Equally specialized, move to next function. If there
16635 is no next function, nothing's most specialized. */
16636 fn = TREE_CHAIN (fn);
16644 /* Now verify that champ is better than everything earlier in the
16645 instantiation list. */
16646 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
16647 if (get_bindings (TREE_VALUE (champ),
16648 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
16649 NULL_TREE, /*check_ret=*/true)
16650 || !get_bindings (TREE_VALUE (fn),
16651 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
16652 NULL_TREE, /*check_ret=*/true))
16658 processing_template_decl--;
16661 return error_mark_node;
16666 /* If DECL is a specialization of some template, return the most
16667 general such template. Otherwise, returns NULL_TREE.
16669 For example, given:
16671 template <class T> struct S { template <class U> void f(U); };
16673 if TMPL is `template <class U> void S<int>::f(U)' this will return
16674 the full template. This function will not trace past partial
16675 specializations, however. For example, given in addition:
16677 template <class T> struct S<T*> { template <class U> void f(U); };
16679 if TMPL is `template <class U> void S<int*>::f(U)' this will return
16680 `template <class T> template <class U> S<T*>::f(U)'. */
16683 most_general_template (tree decl)
16685 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
16686 an immediate specialization. */
16687 if (TREE_CODE (decl) == FUNCTION_DECL)
16689 if (DECL_TEMPLATE_INFO (decl)) {
16690 decl = DECL_TI_TEMPLATE (decl);
16692 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
16693 template friend. */
16694 if (TREE_CODE (decl) != TEMPLATE_DECL)
16700 /* Look for more and more general templates. */
16701 while (DECL_TEMPLATE_INFO (decl))
16703 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
16704 (See cp-tree.h for details.) */
16705 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
16708 if (CLASS_TYPE_P (TREE_TYPE (decl))
16709 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
16712 /* Stop if we run into an explicitly specialized class template. */
16713 if (!DECL_NAMESPACE_SCOPE_P (decl)
16714 && DECL_CONTEXT (decl)
16715 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
16718 decl = DECL_TI_TEMPLATE (decl);
16724 /* Return the most specialized of the class template partial
16725 specializations of TMPL which can produce TYPE, a specialization of
16726 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
16727 a _TYPE node corresponding to the partial specialization, while the
16728 TREE_PURPOSE is the set of template arguments that must be
16729 substituted into the TREE_TYPE in order to generate TYPE.
16731 If the choice of partial specialization is ambiguous, a diagnostic
16732 is issued, and the error_mark_node is returned. If there are no
16733 partial specializations of TMPL matching TYPE, then NULL_TREE is
16737 most_specialized_class (tree type, tree tmpl, tsubst_flags_t complain)
16739 tree list = NULL_TREE;
16745 tree outer_args = NULL_TREE;
16747 tmpl = most_general_template (tmpl);
16748 args = CLASSTYPE_TI_ARGS (type);
16750 /* For determining which partial specialization to use, only the
16751 innermost args are interesting. */
16752 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
16754 outer_args = strip_innermost_template_args (args, 1);
16755 args = INNERMOST_TEMPLATE_ARGS (args);
16758 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
16760 tree partial_spec_args;
16762 tree parms = TREE_VALUE (t);
16764 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
16766 ++processing_template_decl;
16772 /* Discard the outer levels of args, and then substitute in the
16773 template args from the enclosing class. */
16774 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
16775 partial_spec_args = tsubst_template_args
16776 (partial_spec_args, outer_args, tf_none, NULL_TREE);
16778 /* PARMS already refers to just the innermost parms, but the
16779 template parms in partial_spec_args had their levels lowered
16780 by tsubst, so we need to do the same for the parm list. We
16781 can't just tsubst the TREE_VEC itself, as tsubst wants to
16782 treat a TREE_VEC as an argument vector. */
16783 parms = copy_node (parms);
16784 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
16785 TREE_VEC_ELT (parms, i) =
16786 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
16790 partial_spec_args =
16791 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
16792 add_to_template_args (outer_args,
16793 partial_spec_args),
16795 /*require_all_args=*/true,
16796 /*use_default_args=*/true);
16798 --processing_template_decl;
16800 if (partial_spec_args == error_mark_node)
16801 return error_mark_node;
16803 spec_args = get_class_bindings (parms,
16809 spec_args = add_to_template_args (outer_args, spec_args);
16810 list = tree_cons (spec_args, TREE_VALUE (t), list);
16811 TREE_TYPE (list) = TREE_TYPE (t);
16818 ambiguous_p = false;
16821 t = TREE_CHAIN (t);
16822 for (; t; t = TREE_CHAIN (t))
16824 fate = more_specialized_class (champ, t);
16831 t = TREE_CHAIN (t);
16834 ambiguous_p = true;
16843 for (t = list; t && t != champ; t = TREE_CHAIN (t))
16845 fate = more_specialized_class (champ, t);
16848 ambiguous_p = true;
16856 char *spaces = NULL;
16857 if (!(complain & tf_error))
16858 return error_mark_node;
16859 error ("ambiguous class template instantiation for %q#T", type);
16860 str = ngettext ("candidate is:", "candidates are:", list_length (list));
16861 for (t = list; t; t = TREE_CHAIN (t))
16863 error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
16864 spaces = spaces ? spaces : get_spaces (str);
16867 return error_mark_node;
16873 /* Explicitly instantiate DECL. */
16876 do_decl_instantiation (tree decl, tree storage)
16878 tree result = NULL_TREE;
16881 if (!decl || decl == error_mark_node)
16882 /* An error occurred, for which grokdeclarator has already issued
16883 an appropriate message. */
16885 else if (! DECL_LANG_SPECIFIC (decl))
16887 error ("explicit instantiation of non-template %q#D", decl);
16890 else if (TREE_CODE (decl) == VAR_DECL)
16892 /* There is an asymmetry here in the way VAR_DECLs and
16893 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16894 the latter, the DECL we get back will be marked as a
16895 template instantiation, and the appropriate
16896 DECL_TEMPLATE_INFO will be set up. This does not happen for
16897 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16898 should handle VAR_DECLs as it currently handles
16900 if (!DECL_CLASS_SCOPE_P (decl))
16902 error ("%qD is not a static data member of a class template", decl);
16905 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
16906 if (!result || TREE_CODE (result) != VAR_DECL)
16908 error ("no matching template for %qD found", decl);
16911 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
16913 error ("type %qT for explicit instantiation %qD does not match "
16914 "declared type %qT", TREE_TYPE (result), decl,
16919 else if (TREE_CODE (decl) != FUNCTION_DECL)
16921 error ("explicit instantiation of %q#D", decl);
16927 /* Check for various error cases. Note that if the explicit
16928 instantiation is valid the RESULT will currently be marked as an
16929 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16930 until we get here. */
16932 if (DECL_TEMPLATE_SPECIALIZATION (result))
16934 /* DR 259 [temp.spec].
16936 Both an explicit instantiation and a declaration of an explicit
16937 specialization shall not appear in a program unless the explicit
16938 instantiation follows a declaration of the explicit specialization.
16940 For a given set of template parameters, if an explicit
16941 instantiation of a template appears after a declaration of an
16942 explicit specialization for that template, the explicit
16943 instantiation has no effect. */
16946 else if (DECL_EXPLICIT_INSTANTIATION (result))
16950 No program shall explicitly instantiate any template more
16953 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16954 the first instantiation was `extern' and the second is not,
16955 and EXTERN_P for the opposite case. */
16956 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
16957 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
16958 /* If an "extern" explicit instantiation follows an ordinary
16959 explicit instantiation, the template is instantiated. */
16963 else if (!DECL_IMPLICIT_INSTANTIATION (result))
16965 error ("no matching template for %qD found", result);
16968 else if (!DECL_TEMPLATE_INFO (result))
16970 permerror (input_location, "explicit instantiation of non-template %q#D", result);
16974 if (storage == NULL_TREE)
16976 else if (storage == ridpointers[(int) RID_EXTERN])
16978 if (!in_system_header && (cxx_dialect == cxx98))
16979 pedwarn (input_location, OPT_pedantic,
16980 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16985 error ("storage class %qD applied to template instantiation", storage);
16987 check_explicit_instantiation_namespace (result);
16988 mark_decl_instantiated (result, extern_p);
16990 instantiate_decl (result, /*defer_ok=*/1,
16991 /*expl_inst_class_mem_p=*/false);
16995 mark_class_instantiated (tree t, int extern_p)
16997 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
16998 SET_CLASSTYPE_INTERFACE_KNOWN (t);
16999 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
17000 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
17003 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
17004 rest_of_type_compilation (t, 1);
17008 /* Called from do_type_instantiation through binding_table_foreach to
17009 do recursive instantiation for the type bound in ENTRY. */
17011 bt_instantiate_type_proc (binding_entry entry, void *data)
17013 tree storage = *(tree *) data;
17015 if (MAYBE_CLASS_TYPE_P (entry->type)
17016 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
17017 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
17020 /* Called from do_type_instantiation to instantiate a member
17021 (a member function or a static member variable) of an
17022 explicitly instantiated class template. */
17024 instantiate_class_member (tree decl, int extern_p)
17026 mark_decl_instantiated (decl, extern_p);
17028 instantiate_decl (decl, /*defer_ok=*/1,
17029 /*expl_inst_class_mem_p=*/true);
17032 /* Perform an explicit instantiation of template class T. STORAGE, if
17033 non-null, is the RID for extern, inline or static. COMPLAIN is
17034 nonzero if this is called from the parser, zero if called recursively,
17035 since the standard is unclear (as detailed below). */
17038 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
17043 int previous_instantiation_extern_p = 0;
17045 if (TREE_CODE (t) == TYPE_DECL)
17048 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
17050 error ("explicit instantiation of non-template type %qT", t);
17056 if (!COMPLETE_TYPE_P (t))
17058 if (complain & tf_error)
17059 error ("explicit instantiation of %q#T before definition of template",
17064 if (storage != NULL_TREE)
17066 if (!in_system_header)
17068 if (storage == ridpointers[(int) RID_EXTERN])
17070 if (cxx_dialect == cxx98)
17071 pedwarn (input_location, OPT_pedantic,
17072 "ISO C++ 1998 forbids the use of %<extern%> on "
17073 "explicit instantiations");
17076 pedwarn (input_location, OPT_pedantic,
17077 "ISO C++ forbids the use of %qE"
17078 " on explicit instantiations", storage);
17081 if (storage == ridpointers[(int) RID_INLINE])
17083 else if (storage == ridpointers[(int) RID_EXTERN])
17085 else if (storage == ridpointers[(int) RID_STATIC])
17089 error ("storage class %qD applied to template instantiation",
17095 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
17097 /* DR 259 [temp.spec].
17099 Both an explicit instantiation and a declaration of an explicit
17100 specialization shall not appear in a program unless the explicit
17101 instantiation follows a declaration of the explicit specialization.
17103 For a given set of template parameters, if an explicit
17104 instantiation of a template appears after a declaration of an
17105 explicit specialization for that template, the explicit
17106 instantiation has no effect. */
17109 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
17113 No program shall explicitly instantiate any template more
17116 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
17117 instantiation was `extern'. If EXTERN_P then the second is.
17118 These cases are OK. */
17119 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
17121 if (!previous_instantiation_extern_p && !extern_p
17122 && (complain & tf_error))
17123 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
17125 /* If we've already instantiated the template, just return now. */
17126 if (!CLASSTYPE_INTERFACE_ONLY (t))
17130 check_explicit_instantiation_namespace (TYPE_NAME (t));
17131 mark_class_instantiated (t, extern_p);
17139 /* In contrast to implicit instantiation, where only the
17140 declarations, and not the definitions, of members are
17141 instantiated, we have here:
17145 The explicit instantiation of a class template specialization
17146 implies the instantiation of all of its members not
17147 previously explicitly specialized in the translation unit
17148 containing the explicit instantiation.
17150 Of course, we can't instantiate member template classes, since
17151 we don't have any arguments for them. Note that the standard
17152 is unclear on whether the instantiation of the members are
17153 *explicit* instantiations or not. However, the most natural
17154 interpretation is that it should be an explicit instantiation. */
17157 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
17158 if (TREE_CODE (tmp) == FUNCTION_DECL
17159 && DECL_TEMPLATE_INSTANTIATION (tmp))
17160 instantiate_class_member (tmp, extern_p);
17162 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
17163 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
17164 instantiate_class_member (tmp, extern_p);
17166 if (CLASSTYPE_NESTED_UTDS (t))
17167 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
17168 bt_instantiate_type_proc, &storage);
17172 /* Given a function DECL, which is a specialization of TMPL, modify
17173 DECL to be a re-instantiation of TMPL with the same template
17174 arguments. TMPL should be the template into which tsubst'ing
17175 should occur for DECL, not the most general template.
17177 One reason for doing this is a scenario like this:
17180 void f(const T&, int i);
17182 void g() { f(3, 7); }
17185 void f(const T& t, const int i) { }
17187 Note that when the template is first instantiated, with
17188 instantiate_template, the resulting DECL will have no name for the
17189 first parameter, and the wrong type for the second. So, when we go
17190 to instantiate the DECL, we regenerate it. */
17193 regenerate_decl_from_template (tree decl, tree tmpl)
17195 /* The arguments used to instantiate DECL, from the most general
17200 args = DECL_TI_ARGS (decl);
17201 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
17203 /* Make sure that we can see identifiers, and compute access
17205 push_access_scope (decl);
17207 if (TREE_CODE (decl) == FUNCTION_DECL)
17215 args_depth = TMPL_ARGS_DEPTH (args);
17216 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
17217 if (args_depth > parms_depth)
17218 args = get_innermost_template_args (args, parms_depth);
17220 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
17221 args, tf_error, NULL_TREE,
17222 /*defer_ok*/false);
17224 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
17227 /* Merge parameter declarations. */
17228 decl_parm = skip_artificial_parms_for (decl,
17229 DECL_ARGUMENTS (decl));
17231 = skip_artificial_parms_for (code_pattern,
17232 DECL_ARGUMENTS (code_pattern));
17233 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
17238 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
17239 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
17240 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
17242 parm_type = type_decays_to (parm_type);
17243 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
17244 TREE_TYPE (decl_parm) = parm_type;
17245 attributes = DECL_ATTRIBUTES (pattern_parm);
17246 if (DECL_ATTRIBUTES (decl_parm) != attributes)
17248 DECL_ATTRIBUTES (decl_parm) = attributes;
17249 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
17251 decl_parm = DECL_CHAIN (decl_parm);
17252 pattern_parm = DECL_CHAIN (pattern_parm);
17254 /* Merge any parameters that match with the function parameter
17256 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
17259 tree expanded_types;
17260 /* Expand the TYPE_PACK_EXPANSION that provides the types for
17261 the parameters in this function parameter pack. */
17262 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
17263 args, tf_error, NULL_TREE);
17264 len = TREE_VEC_LENGTH (expanded_types);
17265 for (i = 0; i < len; i++)
17270 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
17271 /* Rename the parameter to include the index. */
17272 DECL_NAME (decl_parm) =
17273 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
17274 parm_type = TREE_VEC_ELT (expanded_types, i);
17275 parm_type = type_decays_to (parm_type);
17276 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
17277 TREE_TYPE (decl_parm) = parm_type;
17278 attributes = DECL_ATTRIBUTES (pattern_parm);
17279 if (DECL_ATTRIBUTES (decl_parm) != attributes)
17281 DECL_ATTRIBUTES (decl_parm) = attributes;
17282 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
17284 decl_parm = DECL_CHAIN (decl_parm);
17287 /* Merge additional specifiers from the CODE_PATTERN. */
17288 if (DECL_DECLARED_INLINE_P (code_pattern)
17289 && !DECL_DECLARED_INLINE_P (decl))
17290 DECL_DECLARED_INLINE_P (decl) = 1;
17292 else if (TREE_CODE (decl) == VAR_DECL)
17294 DECL_INITIAL (decl) =
17295 tsubst_expr (DECL_INITIAL (code_pattern), args,
17296 tf_error, DECL_TI_TEMPLATE (decl),
17297 /*integral_constant_expression_p=*/false);
17298 if (VAR_HAD_UNKNOWN_BOUND (decl))
17299 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
17300 tf_error, DECL_TI_TEMPLATE (decl));
17303 gcc_unreachable ();
17305 pop_access_scope (decl);
17308 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
17309 substituted to get DECL. */
17312 template_for_substitution (tree decl)
17314 tree tmpl = DECL_TI_TEMPLATE (decl);
17316 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
17317 for the instantiation. This is not always the most general
17318 template. Consider, for example:
17321 struct S { template <class U> void f();
17322 template <> void f<int>(); };
17324 and an instantiation of S<double>::f<int>. We want TD to be the
17325 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
17326 while (/* An instantiation cannot have a definition, so we need a
17327 more general template. */
17328 DECL_TEMPLATE_INSTANTIATION (tmpl)
17329 /* We must also deal with friend templates. Given:
17331 template <class T> struct S {
17332 template <class U> friend void f() {};
17335 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
17336 so far as the language is concerned, but that's still
17337 where we get the pattern for the instantiation from. On
17338 other hand, if the definition comes outside the class, say:
17340 template <class T> struct S {
17341 template <class U> friend void f();
17343 template <class U> friend void f() {}
17345 we don't need to look any further. That's what the check for
17346 DECL_INITIAL is for. */
17347 || (TREE_CODE (decl) == FUNCTION_DECL
17348 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
17349 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
17351 /* The present template, TD, should not be a definition. If it
17352 were a definition, we should be using it! Note that we
17353 cannot restructure the loop to just keep going until we find
17354 a template with a definition, since that might go too far if
17355 a specialization was declared, but not defined. */
17356 gcc_assert (TREE_CODE (decl) != VAR_DECL
17357 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
17359 /* Fetch the more general template. */
17360 tmpl = DECL_TI_TEMPLATE (tmpl);
17366 /* Returns true if we need to instantiate this template instance even if we
17367 know we aren't going to emit it.. */
17370 always_instantiate_p (tree decl)
17372 /* We always instantiate inline functions so that we can inline them. An
17373 explicit instantiation declaration prohibits implicit instantiation of
17374 non-inline functions. With high levels of optimization, we would
17375 normally inline non-inline functions -- but we're not allowed to do
17376 that for "extern template" functions. Therefore, we check
17377 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
17378 return ((TREE_CODE (decl) == FUNCTION_DECL
17379 && DECL_DECLARED_INLINE_P (decl))
17380 /* And we need to instantiate static data members so that
17381 their initializers are available in integral constant
17383 || (TREE_CODE (decl) == VAR_DECL
17384 && decl_maybe_constant_var_p (decl)));
17387 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
17388 instantiate it now, modifying TREE_TYPE (fn). */
17391 maybe_instantiate_noexcept (tree fn)
17393 tree fntype, spec, noex, clone;
17395 if (DECL_CLONED_FUNCTION_P (fn))
17396 fn = DECL_CLONED_FUNCTION (fn);
17397 fntype = TREE_TYPE (fn);
17398 spec = TYPE_RAISES_EXCEPTIONS (fntype);
17400 if (!DEFERRED_NOEXCEPT_SPEC_P (spec))
17403 noex = TREE_PURPOSE (spec);
17405 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
17407 push_tinst_level (fn);
17408 push_access_scope (fn);
17409 input_location = DECL_SOURCE_LOCATION (fn);
17410 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
17411 DEFERRED_NOEXCEPT_ARGS (noex),
17412 tf_warning_or_error, fn, /*function_p=*/false,
17413 /*integral_constant_expression_p=*/true);
17414 pop_access_scope (fn);
17415 pop_tinst_level ();
17416 spec = build_noexcept_spec (noex, tf_warning_or_error);
17417 if (spec == error_mark_node)
17418 spec = noexcept_false_spec;
17422 /* This is an implicitly declared function, so NOEX is a list of
17423 other functions to evaluate and merge. */
17425 spec = noexcept_true_spec;
17426 for (elt = noex; elt; elt = OVL_NEXT (elt))
17428 tree fn = OVL_CURRENT (elt);
17430 maybe_instantiate_noexcept (fn);
17431 subspec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
17432 spec = merge_exception_specifiers (spec, subspec, NULL_TREE);
17436 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
17438 FOR_EACH_CLONE (clone, fn)
17440 if (TREE_TYPE (clone) == fntype)
17441 TREE_TYPE (clone) = TREE_TYPE (fn);
17443 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
17447 /* Produce the definition of D, a _DECL generated from a template. If
17448 DEFER_OK is nonzero, then we don't have to actually do the
17449 instantiation now; we just have to do it sometime. Normally it is
17450 an error if this is an explicit instantiation but D is undefined.
17451 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
17452 explicitly instantiated class template. */
17455 instantiate_decl (tree d, int defer_ok,
17456 bool expl_inst_class_mem_p)
17458 tree tmpl = DECL_TI_TEMPLATE (d);
17465 bool pattern_defined;
17467 location_t saved_loc = input_location;
17470 /* This function should only be used to instantiate templates for
17471 functions and static member variables. */
17472 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
17473 || TREE_CODE (d) == VAR_DECL);
17475 /* Variables are never deferred; if instantiation is required, they
17476 are instantiated right away. That allows for better code in the
17477 case that an expression refers to the value of the variable --
17478 if the variable has a constant value the referring expression can
17479 take advantage of that fact. */
17480 if (TREE_CODE (d) == VAR_DECL
17481 || DECL_DECLARED_CONSTEXPR_P (d))
17484 /* Don't instantiate cloned functions. Instead, instantiate the
17485 functions they cloned. */
17486 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
17487 d = DECL_CLONED_FUNCTION (d);
17489 if (DECL_TEMPLATE_INSTANTIATED (d)
17490 || DECL_TEMPLATE_SPECIALIZATION (d))
17491 /* D has already been instantiated or explicitly specialized, so
17492 there's nothing for us to do here.
17494 It might seem reasonable to check whether or not D is an explicit
17495 instantiation, and, if so, stop here. But when an explicit
17496 instantiation is deferred until the end of the compilation,
17497 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
17498 the instantiation. */
17501 /* Check to see whether we know that this template will be
17502 instantiated in some other file, as with "extern template"
17504 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
17506 /* In general, we do not instantiate such templates. */
17507 if (external_p && !always_instantiate_p (d))
17510 gen_tmpl = most_general_template (tmpl);
17511 gen_args = DECL_TI_ARGS (d);
17513 if (tmpl != gen_tmpl)
17514 /* We should already have the extra args. */
17515 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
17516 == TMPL_ARGS_DEPTH (gen_args));
17517 /* And what's in the hash table should match D. */
17518 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
17519 || spec == NULL_TREE);
17521 /* This needs to happen before any tsubsting. */
17522 if (! push_tinst_level (d))
17525 timevar_push (TV_TEMPLATE_INST);
17527 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
17528 for the instantiation. */
17529 td = template_for_substitution (d);
17530 code_pattern = DECL_TEMPLATE_RESULT (td);
17532 /* We should never be trying to instantiate a member of a class
17533 template or partial specialization. */
17534 gcc_assert (d != code_pattern);
17536 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
17537 || DECL_TEMPLATE_SPECIALIZATION (td))
17538 /* In the case of a friend template whose definition is provided
17539 outside the class, we may have too many arguments. Drop the
17540 ones we don't need. The same is true for specializations. */
17541 args = get_innermost_template_args
17542 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
17546 if (TREE_CODE (d) == FUNCTION_DECL)
17547 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
17548 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern));
17550 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
17552 /* We may be in the middle of deferred access check. Disable it now. */
17553 push_deferring_access_checks (dk_no_deferred);
17555 /* Unless an explicit instantiation directive has already determined
17556 the linkage of D, remember that a definition is available for
17558 if (pattern_defined
17559 && !DECL_INTERFACE_KNOWN (d)
17560 && !DECL_NOT_REALLY_EXTERN (d))
17561 mark_definable (d);
17563 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
17564 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
17565 input_location = DECL_SOURCE_LOCATION (d);
17567 /* If D is a member of an explicitly instantiated class template,
17568 and no definition is available, treat it like an implicit
17570 if (!pattern_defined && expl_inst_class_mem_p
17571 && DECL_EXPLICIT_INSTANTIATION (d))
17573 /* Leave linkage flags alone on instantiations with anonymous
17575 if (TREE_PUBLIC (d))
17577 DECL_NOT_REALLY_EXTERN (d) = 0;
17578 DECL_INTERFACE_KNOWN (d) = 0;
17580 SET_DECL_IMPLICIT_INSTANTIATION (d);
17583 if (TREE_CODE (d) == FUNCTION_DECL)
17584 maybe_instantiate_noexcept (d);
17586 /* Recheck the substitutions to obtain any warning messages
17587 about ignoring cv qualifiers. Don't do this for artificial decls,
17588 as it breaks the context-sensitive substitution for lambda op(). */
17589 if (!defer_ok && !DECL_ARTIFICIAL (d))
17591 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
17592 tree type = TREE_TYPE (gen);
17594 /* Make sure that we can see identifiers, and compute access
17595 correctly. D is already the target FUNCTION_DECL with the
17597 push_access_scope (d);
17599 if (TREE_CODE (gen) == FUNCTION_DECL)
17601 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
17602 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
17603 d, /*defer_ok*/true);
17604 /* Don't simply tsubst the function type, as that will give
17605 duplicate warnings about poor parameter qualifications.
17606 The function arguments are the same as the decl_arguments
17607 without the top level cv qualifiers. */
17608 type = TREE_TYPE (type);
17610 tsubst (type, gen_args, tf_warning_or_error, d);
17612 pop_access_scope (d);
17615 /* Defer all other templates, unless we have been explicitly
17616 forbidden from doing so. */
17617 if (/* If there is no definition, we cannot instantiate the
17620 /* If it's OK to postpone instantiation, do so. */
17622 /* If this is a static data member that will be defined
17623 elsewhere, we don't want to instantiate the entire data
17624 member, but we do want to instantiate the initializer so that
17625 we can substitute that elsewhere. */
17626 || (external_p && TREE_CODE (d) == VAR_DECL))
17628 /* The definition of the static data member is now required so
17629 we must substitute the initializer. */
17630 if (TREE_CODE (d) == VAR_DECL
17631 && !DECL_INITIAL (d)
17632 && DECL_INITIAL (code_pattern))
17636 bool const_init = false;
17638 ns = decl_namespace_context (d);
17639 push_nested_namespace (ns);
17640 push_nested_class (DECL_CONTEXT (d));
17641 init = tsubst_expr (DECL_INITIAL (code_pattern),
17643 tf_warning_or_error, NULL_TREE,
17644 /*integral_constant_expression_p=*/false);
17645 /* Make sure the initializer is still constant, in case of
17646 circular dependency (template/instantiate6.C). */
17648 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
17649 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
17650 /*asmspec_tree=*/NULL_TREE,
17651 LOOKUP_ONLYCONVERTING);
17652 pop_nested_class ();
17653 pop_nested_namespace (ns);
17656 /* We restore the source position here because it's used by
17657 add_pending_template. */
17658 input_location = saved_loc;
17660 if (at_eof && !pattern_defined
17661 && DECL_EXPLICIT_INSTANTIATION (d)
17662 && DECL_NOT_REALLY_EXTERN (d))
17665 The definition of a non-exported function template, a
17666 non-exported member function template, or a non-exported
17667 member function or static data member of a class template
17668 shall be present in every translation unit in which it is
17669 explicitly instantiated. */
17670 permerror (input_location, "explicit instantiation of %qD "
17671 "but no definition available", d);
17673 /* If we're in unevaluated context, we just wanted to get the
17674 constant value; this isn't an odr use, so don't queue
17675 a full instantiation. */
17676 if (cp_unevaluated_operand != 0)
17678 /* ??? Historically, we have instantiated inline functions, even
17679 when marked as "extern template". */
17680 if (!(external_p && TREE_CODE (d) == VAR_DECL))
17681 add_pending_template (d);
17684 /* Tell the repository that D is available in this translation unit
17685 -- and see if it is supposed to be instantiated here. */
17686 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
17688 /* In a PCH file, despite the fact that the repository hasn't
17689 requested instantiation in the PCH it is still possible that
17690 an instantiation will be required in a file that includes the
17693 add_pending_template (d);
17694 /* Instantiate inline functions so that the inliner can do its
17695 job, even though we'll not be emitting a copy of this
17697 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
17701 need_push = !cfun || !global_bindings_p ();
17703 push_to_top_level ();
17705 /* Mark D as instantiated so that recursive calls to
17706 instantiate_decl do not try to instantiate it again. */
17707 DECL_TEMPLATE_INSTANTIATED (d) = 1;
17709 /* Regenerate the declaration in case the template has been modified
17710 by a subsequent redeclaration. */
17711 regenerate_decl_from_template (d, td);
17713 /* We already set the file and line above. Reset them now in case
17714 they changed as a result of calling regenerate_decl_from_template. */
17715 input_location = DECL_SOURCE_LOCATION (d);
17717 if (TREE_CODE (d) == VAR_DECL)
17720 bool const_init = false;
17722 /* Clear out DECL_RTL; whatever was there before may not be right
17723 since we've reset the type of the declaration. */
17724 SET_DECL_RTL (d, NULL);
17725 DECL_IN_AGGR_P (d) = 0;
17727 /* The initializer is placed in DECL_INITIAL by
17728 regenerate_decl_from_template so we don't need to
17729 push/pop_access_scope again here. Pull it out so that
17730 cp_finish_decl can process it. */
17731 init = DECL_INITIAL (d);
17732 DECL_INITIAL (d) = NULL_TREE;
17733 DECL_INITIALIZED_P (d) = 0;
17735 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
17736 initializer. That function will defer actual emission until
17737 we have a chance to determine linkage. */
17738 DECL_EXTERNAL (d) = 0;
17740 /* Enter the scope of D so that access-checking works correctly. */
17741 push_nested_class (DECL_CONTEXT (d));
17742 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
17743 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
17744 pop_nested_class ();
17746 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
17747 synthesize_method (d);
17748 else if (TREE_CODE (d) == FUNCTION_DECL)
17750 htab_t saved_local_specializations;
17755 /* Save away the current list, in case we are instantiating one
17756 template from within the body of another. */
17757 saved_local_specializations = local_specializations;
17759 /* Set up the list of local specializations. */
17760 local_specializations = htab_create (37,
17761 hash_local_specialization,
17762 eq_local_specializations,
17765 /* Set up context. */
17766 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
17768 /* Create substitution entries for the parameters. */
17769 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
17770 tmpl_parm = DECL_ARGUMENTS (subst_decl);
17771 spec_parm = DECL_ARGUMENTS (d);
17772 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
17774 register_local_specialization (spec_parm, tmpl_parm);
17775 spec_parm = skip_artificial_parms_for (d, spec_parm);
17776 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
17778 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17780 register_local_specialization (spec_parm, tmpl_parm);
17781 tmpl_parm = DECL_CHAIN (tmpl_parm);
17782 spec_parm = DECL_CHAIN (spec_parm);
17784 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
17786 /* Register the (value) argument pack as a specialization of
17787 TMPL_PARM, then move on. */
17788 tree argpack = make_fnparm_pack (spec_parm);
17789 register_local_specialization (argpack, tmpl_parm);
17790 tmpl_parm = DECL_CHAIN (tmpl_parm);
17791 spec_parm = NULL_TREE;
17793 gcc_assert (!spec_parm);
17795 /* Substitute into the body of the function. */
17796 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
17797 tf_warning_or_error, tmpl,
17798 /*integral_constant_expression_p=*/false);
17800 /* Set the current input_location to the end of the function
17801 so that finish_function knows where we are. */
17802 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
17804 /* We don't need the local specializations any more. */
17805 htab_delete (local_specializations);
17806 local_specializations = saved_local_specializations;
17808 /* Finish the function. */
17809 d = finish_function (0);
17810 expand_or_defer_fn (d);
17813 /* We're not deferring instantiation any more. */
17814 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
17817 pop_from_top_level ();
17820 input_location = saved_loc;
17821 pop_deferring_access_checks ();
17822 pop_tinst_level ();
17824 timevar_pop (TV_TEMPLATE_INST);
17829 /* Run through the list of templates that we wish we could
17830 instantiate, and instantiate any we can. RETRIES is the
17831 number of times we retry pending template instantiation. */
17834 instantiate_pending_templates (int retries)
17837 location_t saved_loc = input_location;
17839 /* Instantiating templates may trigger vtable generation. This in turn
17840 may require further template instantiations. We place a limit here
17841 to avoid infinite loop. */
17842 if (pending_templates && retries >= max_tinst_depth)
17844 tree decl = pending_templates->tinst->decl;
17846 error ("template instantiation depth exceeds maximum of %d"
17847 " instantiating %q+D, possibly from virtual table generation"
17848 " (use -ftemplate-depth= to increase the maximum)",
17849 max_tinst_depth, decl);
17850 if (TREE_CODE (decl) == FUNCTION_DECL)
17851 /* Pretend that we defined it. */
17852 DECL_INITIAL (decl) = error_mark_node;
17858 struct pending_template **t = &pending_templates;
17859 struct pending_template *last = NULL;
17863 tree instantiation = reopen_tinst_level ((*t)->tinst);
17864 bool complete = false;
17866 if (TYPE_P (instantiation))
17870 if (!COMPLETE_TYPE_P (instantiation))
17872 instantiate_class_template (instantiation);
17873 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
17874 for (fn = TYPE_METHODS (instantiation);
17876 fn = TREE_CHAIN (fn))
17877 if (! DECL_ARTIFICIAL (fn))
17878 instantiate_decl (fn,
17880 /*expl_inst_class_mem_p=*/false);
17881 if (COMPLETE_TYPE_P (instantiation))
17885 complete = COMPLETE_TYPE_P (instantiation);
17889 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
17890 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
17893 = instantiate_decl (instantiation,
17895 /*expl_inst_class_mem_p=*/false);
17896 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
17900 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
17901 || DECL_TEMPLATE_INSTANTIATED (instantiation));
17905 /* If INSTANTIATION has been instantiated, then we don't
17906 need to consider it again in the future. */
17914 current_tinst_level = NULL;
17916 last_pending_template = last;
17918 while (reconsider);
17920 input_location = saved_loc;
17923 /* Substitute ARGVEC into T, which is a list of initializers for
17924 either base class or a non-static data member. The TREE_PURPOSEs
17925 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17926 instantiate_decl. */
17929 tsubst_initializer_list (tree t, tree argvec)
17931 tree inits = NULL_TREE;
17933 for (; t; t = TREE_CHAIN (t))
17937 tree expanded_bases = NULL_TREE;
17938 tree expanded_arguments = NULL_TREE;
17941 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
17946 /* Expand the base class expansion type into separate base
17948 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
17949 tf_warning_or_error,
17951 if (expanded_bases == error_mark_node)
17954 /* We'll be building separate TREE_LISTs of arguments for
17956 len = TREE_VEC_LENGTH (expanded_bases);
17957 expanded_arguments = make_tree_vec (len);
17958 for (i = 0; i < len; i++)
17959 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
17961 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17962 expand each argument in the TREE_VALUE of t. */
17963 expr = make_node (EXPR_PACK_EXPANSION);
17964 PACK_EXPANSION_PARAMETER_PACKS (expr) =
17965 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
17967 if (TREE_VALUE (t) == void_type_node)
17968 /* VOID_TYPE_NODE is used to indicate
17969 value-initialization. */
17971 for (i = 0; i < len; i++)
17972 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
17976 /* Substitute parameter packs into each argument in the
17978 in_base_initializer = 1;
17979 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
17981 tree expanded_exprs;
17983 /* Expand the argument. */
17984 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
17986 = tsubst_pack_expansion (expr, argvec,
17987 tf_warning_or_error,
17989 if (expanded_exprs == error_mark_node)
17992 /* Prepend each of the expanded expressions to the
17993 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17994 for (i = 0; i < len; i++)
17996 TREE_VEC_ELT (expanded_arguments, i) =
17997 tree_cons (NULL_TREE,
17998 TREE_VEC_ELT (expanded_exprs, i),
17999 TREE_VEC_ELT (expanded_arguments, i));
18002 in_base_initializer = 0;
18004 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18005 since we built them backwards. */
18006 for (i = 0; i < len; i++)
18008 TREE_VEC_ELT (expanded_arguments, i) =
18009 nreverse (TREE_VEC_ELT (expanded_arguments, i));
18014 for (i = 0; i < len; ++i)
18016 if (expanded_bases)
18018 decl = TREE_VEC_ELT (expanded_bases, i);
18019 decl = expand_member_init (decl);
18020 init = TREE_VEC_ELT (expanded_arguments, i);
18024 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
18025 tf_warning_or_error, NULL_TREE);
18027 decl = expand_member_init (decl);
18028 if (decl && !DECL_P (decl))
18029 in_base_initializer = 1;
18031 init = TREE_VALUE (t);
18032 if (init != void_type_node)
18033 init = tsubst_expr (init, argvec,
18034 tf_warning_or_error, NULL_TREE,
18035 /*integral_constant_expression_p=*/false);
18036 in_base_initializer = 0;
18041 init = build_tree_list (decl, init);
18042 TREE_CHAIN (init) = inits;
18050 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18053 set_current_access_from_decl (tree decl)
18055 if (TREE_PRIVATE (decl))
18056 current_access_specifier = access_private_node;
18057 else if (TREE_PROTECTED (decl))
18058 current_access_specifier = access_protected_node;
18060 current_access_specifier = access_public_node;
18063 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18064 is the instantiation (which should have been created with
18065 start_enum) and ARGS are the template arguments to use. */
18068 tsubst_enum (tree tag, tree newtag, tree args)
18072 if (SCOPED_ENUM_P (newtag))
18073 begin_scope (sk_scoped_enum, newtag);
18075 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
18080 decl = TREE_VALUE (e);
18081 /* Note that in a template enum, the TREE_VALUE is the
18082 CONST_DECL, not the corresponding INTEGER_CST. */
18083 value = tsubst_expr (DECL_INITIAL (decl),
18084 args, tf_warning_or_error, NULL_TREE,
18085 /*integral_constant_expression_p=*/true);
18087 /* Give this enumeration constant the correct access. */
18088 set_current_access_from_decl (decl);
18090 /* Actually build the enumerator itself. */
18092 (DECL_NAME (decl), value, newtag, DECL_SOURCE_LOCATION (decl));
18095 if (SCOPED_ENUM_P (newtag))
18098 finish_enum_value_list (newtag);
18099 finish_enum (newtag);
18101 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
18102 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
18105 /* DECL is a FUNCTION_DECL that is a template specialization. Return
18106 its type -- but without substituting the innermost set of template
18107 arguments. So, innermost set of template parameters will appear in
18111 get_mostly_instantiated_function_type (tree decl)
18119 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
18120 targs = DECL_TI_ARGS (decl);
18121 tparms = DECL_TEMPLATE_PARMS (tmpl);
18122 parm_depth = TMPL_PARMS_DEPTH (tparms);
18124 /* There should be as many levels of arguments as there are levels
18126 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
18128 fn_type = TREE_TYPE (tmpl);
18130 if (parm_depth == 1)
18131 /* No substitution is necessary. */
18138 /* Replace the innermost level of the TARGS with NULL_TREEs to
18139 let tsubst know not to substitute for those parameters. */
18140 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
18141 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
18142 SET_TMPL_ARGS_LEVEL (partial_args, i,
18143 TMPL_ARGS_LEVEL (targs, i));
18144 SET_TMPL_ARGS_LEVEL (partial_args,
18145 TMPL_ARGS_DEPTH (targs),
18146 make_tree_vec (DECL_NTPARMS (tmpl)));
18148 /* Make sure that we can see identifiers, and compute access
18150 push_access_scope (decl);
18152 ++processing_template_decl;
18153 /* Now, do the (partial) substitution to figure out the
18154 appropriate function type. */
18155 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
18156 --processing_template_decl;
18158 /* Substitute into the template parameters to obtain the real
18159 innermost set of parameters. This step is important if the
18160 innermost set of template parameters contains value
18161 parameters whose types depend on outer template parameters. */
18162 TREE_VEC_LENGTH (partial_args)--;
18163 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
18165 pop_access_scope (decl);
18171 /* Return truthvalue if we're processing a template different from
18172 the last one involved in diagnostics. */
18174 problematic_instantiation_changed (void)
18176 return last_template_error_tick != tinst_level_tick;
18179 /* Remember current template involved in diagnostics. */
18181 record_last_problematic_instantiation (void)
18183 last_template_error_tick = tinst_level_tick;
18186 struct tinst_level *
18187 current_instantiation (void)
18189 return current_tinst_level;
18192 /* [temp.param] Check that template non-type parm TYPE is of an allowable
18193 type. Return zero for ok, nonzero for disallowed. Issue error and
18194 warning messages under control of COMPLAIN. */
18197 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
18199 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
18201 else if (POINTER_TYPE_P (type))
18203 else if (TYPE_PTR_TO_MEMBER_P (type))
18205 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
18207 else if (TREE_CODE (type) == TYPENAME_TYPE)
18209 else if (TREE_CODE (type) == DECLTYPE_TYPE)
18212 if (complain & tf_error)
18213 error ("%q#T is not a valid type for a template constant parameter", type);
18217 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
18218 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
18221 dependent_type_p_r (tree type)
18227 A type is dependent if it is:
18229 -- a template parameter. Template template parameters are types
18230 for us (since TYPE_P holds true for them) so we handle
18232 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18233 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
18235 /* -- a qualified-id with a nested-name-specifier which contains a
18236 class-name that names a dependent type or whose unqualified-id
18237 names a dependent type. */
18238 if (TREE_CODE (type) == TYPENAME_TYPE)
18240 /* -- a cv-qualified type where the cv-unqualified type is
18242 type = TYPE_MAIN_VARIANT (type);
18243 /* -- a compound type constructed from any dependent type. */
18244 if (TYPE_PTR_TO_MEMBER_P (type))
18245 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
18246 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
18248 else if (TREE_CODE (type) == POINTER_TYPE
18249 || TREE_CODE (type) == REFERENCE_TYPE)
18250 return dependent_type_p (TREE_TYPE (type));
18251 else if (TREE_CODE (type) == FUNCTION_TYPE
18252 || TREE_CODE (type) == METHOD_TYPE)
18256 if (dependent_type_p (TREE_TYPE (type)))
18258 for (arg_type = TYPE_ARG_TYPES (type);
18260 arg_type = TREE_CHAIN (arg_type))
18261 if (dependent_type_p (TREE_VALUE (arg_type)))
18265 /* -- an array type constructed from any dependent type or whose
18266 size is specified by a constant expression that is
18269 We checked for type- and value-dependence of the bounds in
18270 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
18271 if (TREE_CODE (type) == ARRAY_TYPE)
18273 if (TYPE_DOMAIN (type)
18274 && dependent_type_p (TYPE_DOMAIN (type)))
18276 return dependent_type_p (TREE_TYPE (type));
18279 /* -- a template-id in which either the template name is a template
18281 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18283 /* ... or any of the template arguments is a dependent type or
18284 an expression that is type-dependent or value-dependent. */
18285 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
18286 && (any_dependent_template_arguments_p
18287 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
18290 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
18291 dependent; if the argument of the `typeof' expression is not
18292 type-dependent, then it should already been have resolved. */
18293 if (TREE_CODE (type) == TYPEOF_TYPE
18294 || TREE_CODE (type) == DECLTYPE_TYPE
18295 || TREE_CODE (type) == UNDERLYING_TYPE)
18298 /* A template argument pack is dependent if any of its packed
18300 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
18302 tree args = ARGUMENT_PACK_ARGS (type);
18303 int i, len = TREE_VEC_LENGTH (args);
18304 for (i = 0; i < len; ++i)
18305 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
18309 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
18310 be template parameters. */
18311 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
18314 /* The standard does not specifically mention types that are local
18315 to template functions or local classes, but they should be
18316 considered dependent too. For example:
18318 template <int I> void f() {
18323 The size of `E' cannot be known until the value of `I' has been
18324 determined. Therefore, `E' must be considered dependent. */
18325 scope = TYPE_CONTEXT (type);
18326 if (scope && TYPE_P (scope))
18327 return dependent_type_p (scope);
18328 /* Don't use type_dependent_expression_p here, as it can lead
18329 to infinite recursion trying to determine whether a lambda
18330 nested in a lambda is dependent (c++/47687). */
18331 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
18332 && DECL_LANG_SPECIFIC (scope)
18333 && DECL_TEMPLATE_INFO (scope)
18334 && (any_dependent_template_arguments_p
18335 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
18338 /* Other types are non-dependent. */
18342 /* Returns TRUE if TYPE is dependent, in the sense of
18343 [temp.dep.type]. Note that a NULL type is considered dependent. */
18346 dependent_type_p (tree type)
18348 /* If there are no template parameters in scope, then there can't be
18349 any dependent types. */
18350 if (!processing_template_decl)
18352 /* If we are not processing a template, then nobody should be
18353 providing us with a dependent type. */
18355 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
18359 /* If the type is NULL, we have not computed a type for the entity
18360 in question; in that case, the type is dependent. */
18364 /* Erroneous types can be considered non-dependent. */
18365 if (type == error_mark_node)
18368 /* If we have not already computed the appropriate value for TYPE,
18370 if (!TYPE_DEPENDENT_P_VALID (type))
18372 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
18373 TYPE_DEPENDENT_P_VALID (type) = 1;
18376 return TYPE_DEPENDENT_P (type);
18379 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
18380 lookup. In other words, a dependent type that is not the current
18384 dependent_scope_p (tree scope)
18386 return (scope && TYPE_P (scope) && dependent_type_p (scope)
18387 && !currently_open_class (scope));
18390 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
18391 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
18394 /* Note that this predicate is not appropriate for general expressions;
18395 only constant expressions (that satisfy potential_constant_expression)
18396 can be tested for value dependence.
18398 We should really also have a predicate for "instantiation-dependent".
18400 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
18401 (what about instantiation-dependent constant-expressions?)
18402 is_late_template_attribute: defer if instantiation-dependent.
18403 compute_array_index_type: proceed if constant and not t- or v-dependent
18404 if instantiation-dependent, need to remember full expression
18405 uses_template_parms: FIXME - need to audit callers
18406 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
18407 dependent_type_p [array_type]: dependent if index type is dependent
18409 static_assert - instantiation-dependent */
18412 value_dependent_expression_p (tree expression)
18414 if (!processing_template_decl)
18417 /* A name declared with a dependent type. */
18418 if (DECL_P (expression) && type_dependent_expression_p (expression))
18421 switch (TREE_CODE (expression))
18423 case IDENTIFIER_NODE:
18424 /* A name that has not been looked up -- must be dependent. */
18427 case TEMPLATE_PARM_INDEX:
18428 /* A non-type template parm. */
18432 /* A non-type template parm. */
18433 if (DECL_TEMPLATE_PARM_P (expression))
18435 return value_dependent_expression_p (DECL_INITIAL (expression));
18438 /* A constant with literal type and is initialized
18439 with an expression that is value-dependent. */
18440 if (DECL_INITIAL (expression)
18441 && decl_constant_var_p (expression)
18442 && value_dependent_expression_p (DECL_INITIAL (expression)))
18446 case DYNAMIC_CAST_EXPR:
18447 case STATIC_CAST_EXPR:
18448 case CONST_CAST_EXPR:
18449 case REINTERPRET_CAST_EXPR:
18451 /* These expressions are value-dependent if the type to which
18452 the cast occurs is dependent or the expression being casted
18453 is value-dependent. */
18455 tree type = TREE_TYPE (expression);
18457 if (dependent_type_p (type))
18460 /* A functional cast has a list of operands. */
18461 expression = TREE_OPERAND (expression, 0);
18464 /* If there are no operands, it must be an expression such
18465 as "int()". This should not happen for aggregate types
18466 because it would form non-constant expressions. */
18467 gcc_assert (cxx_dialect >= cxx0x
18468 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
18473 if (TREE_CODE (expression) == TREE_LIST)
18474 return any_value_dependent_elements_p (expression);
18476 return value_dependent_expression_p (expression);
18482 /* A `sizeof' expression is value-dependent if the operand is
18483 type-dependent or is a pack expansion. */
18484 expression = TREE_OPERAND (expression, 0);
18485 if (PACK_EXPANSION_P (expression))
18487 else if (TYPE_P (expression))
18488 return dependent_type_p (expression);
18489 return type_dependent_expression_p (expression);
18491 case AT_ENCODE_EXPR:
18492 /* An 'encode' expression is value-dependent if the operand is
18494 expression = TREE_OPERAND (expression, 0);
18495 return dependent_type_p (expression);
18497 case NOEXCEPT_EXPR:
18498 expression = TREE_OPERAND (expression, 0);
18499 return type_dependent_expression_p (expression);
18503 tree name = TREE_OPERAND (expression, 1);
18504 return value_dependent_expression_p (name);
18507 case COMPONENT_REF:
18508 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
18509 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
18511 case NONTYPE_ARGUMENT_PACK:
18512 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
18513 is value-dependent. */
18515 tree values = ARGUMENT_PACK_ARGS (expression);
18516 int i, len = TREE_VEC_LENGTH (values);
18518 for (i = 0; i < len; ++i)
18519 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
18527 tree type2 = TRAIT_EXPR_TYPE2 (expression);
18528 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
18529 || (type2 ? dependent_type_p (type2) : false));
18533 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
18534 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
18537 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
18538 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
18542 tree op = TREE_OPERAND (expression, 0);
18543 return (value_dependent_expression_p (op)
18544 || has_value_dependent_address (op));
18549 tree fn = get_callee_fndecl (expression);
18551 if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
18553 nargs = call_expr_nargs (expression);
18554 for (i = 0; i < nargs; ++i)
18556 tree op = CALL_EXPR_ARG (expression, i);
18557 /* In a call to a constexpr member function, look through the
18558 implicit ADDR_EXPR on the object argument so that it doesn't
18559 cause the call to be considered value-dependent. We also
18560 look through it in potential_constant_expression. */
18561 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
18562 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
18563 && TREE_CODE (op) == ADDR_EXPR)
18564 op = TREE_OPERAND (op, 0);
18565 if (value_dependent_expression_p (op))
18571 case TEMPLATE_ID_EXPR:
18572 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
18574 return type_dependent_expression_p (expression);
18580 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
18581 if (value_dependent_expression_p (val))
18587 /* A constant expression is value-dependent if any subexpression is
18588 value-dependent. */
18589 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
18591 case tcc_reference:
18593 case tcc_comparison:
18595 case tcc_expression:
18598 int i, len = cp_tree_operand_length (expression);
18600 for (i = 0; i < len; i++)
18602 tree t = TREE_OPERAND (expression, i);
18604 /* In some cases, some of the operands may be missing.l
18605 (For example, in the case of PREDECREMENT_EXPR, the
18606 amount to increment by may be missing.) That doesn't
18607 make the expression dependent. */
18608 if (t && value_dependent_expression_p (t))
18619 /* The expression is not value-dependent. */
18623 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
18624 [temp.dep.expr]. Note that an expression with no type is
18625 considered dependent. Other parts of the compiler arrange for an
18626 expression with type-dependent subexpressions to have no type, so
18627 this function doesn't have to be fully recursive. */
18630 type_dependent_expression_p (tree expression)
18632 if (!processing_template_decl)
18635 if (expression == error_mark_node)
18638 /* An unresolved name is always dependent. */
18639 if (TREE_CODE (expression) == IDENTIFIER_NODE
18640 || TREE_CODE (expression) == USING_DECL)
18643 /* Some expression forms are never type-dependent. */
18644 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
18645 || TREE_CODE (expression) == SIZEOF_EXPR
18646 || TREE_CODE (expression) == ALIGNOF_EXPR
18647 || TREE_CODE (expression) == AT_ENCODE_EXPR
18648 || TREE_CODE (expression) == NOEXCEPT_EXPR
18649 || TREE_CODE (expression) == TRAIT_EXPR
18650 || TREE_CODE (expression) == TYPEID_EXPR
18651 || TREE_CODE (expression) == DELETE_EXPR
18652 || TREE_CODE (expression) == VEC_DELETE_EXPR
18653 || TREE_CODE (expression) == THROW_EXPR)
18656 /* The types of these expressions depends only on the type to which
18657 the cast occurs. */
18658 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
18659 || TREE_CODE (expression) == STATIC_CAST_EXPR
18660 || TREE_CODE (expression) == CONST_CAST_EXPR
18661 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
18662 || TREE_CODE (expression) == CAST_EXPR)
18663 return dependent_type_p (TREE_TYPE (expression));
18665 /* The types of these expressions depends only on the type created
18666 by the expression. */
18667 if (TREE_CODE (expression) == NEW_EXPR
18668 || TREE_CODE (expression) == VEC_NEW_EXPR)
18670 /* For NEW_EXPR tree nodes created inside a template, either
18671 the object type itself or a TREE_LIST may appear as the
18673 tree type = TREE_OPERAND (expression, 1);
18674 if (TREE_CODE (type) == TREE_LIST)
18675 /* This is an array type. We need to check array dimensions
18677 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
18678 || value_dependent_expression_p
18679 (TREE_OPERAND (TREE_VALUE (type), 1));
18681 return dependent_type_p (type);
18684 if (TREE_CODE (expression) == SCOPE_REF)
18686 tree scope = TREE_OPERAND (expression, 0);
18687 tree name = TREE_OPERAND (expression, 1);
18689 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
18690 contains an identifier associated by name lookup with one or more
18691 declarations declared with a dependent type, or...a
18692 nested-name-specifier or qualified-id that names a member of an
18693 unknown specialization. */
18694 return (type_dependent_expression_p (name)
18695 || dependent_scope_p (scope));
18698 if (TREE_CODE (expression) == FUNCTION_DECL
18699 && DECL_LANG_SPECIFIC (expression)
18700 && DECL_TEMPLATE_INFO (expression)
18701 && (any_dependent_template_arguments_p
18702 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
18705 if (TREE_CODE (expression) == TEMPLATE_DECL
18706 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
18709 if (TREE_CODE (expression) == STMT_EXPR)
18710 expression = stmt_expr_value_expr (expression);
18712 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
18717 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
18719 if (type_dependent_expression_p (elt))
18725 /* A static data member of the current instantiation with incomplete
18726 array type is type-dependent, as the definition and specializations
18727 can have different bounds. */
18728 if (TREE_CODE (expression) == VAR_DECL
18729 && DECL_CLASS_SCOPE_P (expression)
18730 && dependent_type_p (DECL_CONTEXT (expression))
18731 && VAR_HAD_UNKNOWN_BOUND (expression))
18734 if (TREE_TYPE (expression) == unknown_type_node)
18736 if (TREE_CODE (expression) == ADDR_EXPR)
18737 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
18738 if (TREE_CODE (expression) == COMPONENT_REF
18739 || TREE_CODE (expression) == OFFSET_REF)
18741 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
18743 expression = TREE_OPERAND (expression, 1);
18744 if (TREE_CODE (expression) == IDENTIFIER_NODE)
18747 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
18748 if (TREE_CODE (expression) == SCOPE_REF)
18751 if (TREE_CODE (expression) == BASELINK)
18752 expression = BASELINK_FUNCTIONS (expression);
18754 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
18756 if (any_dependent_template_arguments_p
18757 (TREE_OPERAND (expression, 1)))
18759 expression = TREE_OPERAND (expression, 0);
18761 gcc_assert (TREE_CODE (expression) == OVERLOAD
18762 || TREE_CODE (expression) == FUNCTION_DECL);
18766 if (type_dependent_expression_p (OVL_CURRENT (expression)))
18768 expression = OVL_NEXT (expression);
18773 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
18775 return (dependent_type_p (TREE_TYPE (expression)));
18778 /* Like type_dependent_expression_p, but it also works while not processing
18779 a template definition, i.e. during substitution or mangling. */
18782 type_dependent_expression_p_push (tree expr)
18785 ++processing_template_decl;
18786 b = type_dependent_expression_p (expr);
18787 --processing_template_decl;
18791 /* Returns TRUE if ARGS contains a type-dependent expression. */
18794 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
18799 FOR_EACH_VEC_ELT (tree, args, i, arg)
18801 if (type_dependent_expression_p (arg))
18807 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
18808 expressions) contains any type-dependent expressions. */
18811 any_type_dependent_elements_p (const_tree list)
18813 for (; list; list = TREE_CHAIN (list))
18814 if (value_dependent_expression_p (TREE_VALUE (list)))
18820 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
18821 expressions) contains any value-dependent expressions. */
18824 any_value_dependent_elements_p (const_tree list)
18826 for (; list; list = TREE_CHAIN (list))
18827 if (value_dependent_expression_p (TREE_VALUE (list)))
18833 /* Returns TRUE if the ARG (a template argument) is dependent. */
18836 dependent_template_arg_p (tree arg)
18838 if (!processing_template_decl)
18841 /* Assume a template argument that was wrongly written by the user
18842 is dependent. This is consistent with what
18843 any_dependent_template_arguments_p [that calls this function]
18845 if (arg == error_mark_node)
18848 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
18849 arg = ARGUMENT_PACK_SELECT_ARG (arg);
18851 if (TREE_CODE (arg) == TEMPLATE_DECL
18852 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18853 return dependent_template_p (arg);
18854 else if (ARGUMENT_PACK_P (arg))
18856 tree args = ARGUMENT_PACK_ARGS (arg);
18857 int i, len = TREE_VEC_LENGTH (args);
18858 for (i = 0; i < len; ++i)
18860 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
18866 else if (TYPE_P (arg))
18867 return dependent_type_p (arg);
18869 return (type_dependent_expression_p (arg)
18870 || value_dependent_expression_p (arg));
18873 /* Returns true if ARGS (a collection of template arguments) contains
18874 any types that require structural equality testing. */
18877 any_template_arguments_need_structural_equality_p (tree args)
18884 if (args == error_mark_node)
18887 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18889 tree level = TMPL_ARGS_LEVEL (args, i + 1);
18890 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18892 tree arg = TREE_VEC_ELT (level, j);
18893 tree packed_args = NULL_TREE;
18896 if (ARGUMENT_PACK_P (arg))
18898 /* Look inside the argument pack. */
18899 packed_args = ARGUMENT_PACK_ARGS (arg);
18900 len = TREE_VEC_LENGTH (packed_args);
18903 for (k = 0; k < len; ++k)
18906 arg = TREE_VEC_ELT (packed_args, k);
18908 if (error_operand_p (arg))
18910 else if (TREE_CODE (arg) == TEMPLATE_DECL
18911 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
18913 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
18915 else if (!TYPE_P (arg) && TREE_TYPE (arg)
18916 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
18925 /* Returns true if ARGS (a collection of template arguments) contains
18926 any dependent arguments. */
18929 any_dependent_template_arguments_p (const_tree args)
18936 if (args == error_mark_node)
18939 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
18941 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
18942 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
18943 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
18950 /* Returns TRUE if the template TMPL is dependent. */
18953 dependent_template_p (tree tmpl)
18955 if (TREE_CODE (tmpl) == OVERLOAD)
18959 if (dependent_template_p (OVL_CURRENT (tmpl)))
18961 tmpl = OVL_NEXT (tmpl);
18966 /* Template template parameters are dependent. */
18967 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
18968 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
18970 /* So are names that have not been looked up. */
18971 if (TREE_CODE (tmpl) == SCOPE_REF
18972 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
18974 /* So are member templates of dependent classes. */
18975 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
18976 return dependent_type_p (DECL_CONTEXT (tmpl));
18980 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18983 dependent_template_id_p (tree tmpl, tree args)
18985 return (dependent_template_p (tmpl)
18986 || any_dependent_template_arguments_p (args));
18989 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18993 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
18997 if (!processing_template_decl)
19000 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
19002 tree decl = TREE_VEC_ELT (declv, i);
19003 tree init = TREE_VEC_ELT (initv, i);
19004 tree cond = TREE_VEC_ELT (condv, i);
19005 tree incr = TREE_VEC_ELT (incrv, i);
19007 if (type_dependent_expression_p (decl))
19010 if (init && type_dependent_expression_p (init))
19013 if (type_dependent_expression_p (cond))
19016 if (COMPARISON_CLASS_P (cond)
19017 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
19018 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
19021 if (TREE_CODE (incr) == MODOP_EXPR)
19023 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
19024 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
19027 else if (type_dependent_expression_p (incr))
19029 else if (TREE_CODE (incr) == MODIFY_EXPR)
19031 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
19033 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
19035 tree t = TREE_OPERAND (incr, 1);
19036 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
19037 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
19046 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
19047 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
19048 no such TYPE can be found. Note that this function peers inside
19049 uninstantiated templates and therefore should be used only in
19050 extremely limited situations. ONLY_CURRENT_P restricts this
19051 peering to the currently open classes hierarchy (which is required
19052 when comparing types). */
19055 resolve_typename_type (tree type, bool only_current_p)
19064 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
19066 scope = TYPE_CONTEXT (type);
19067 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
19068 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
19069 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
19070 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
19071 identifier of the TYPENAME_TYPE anymore.
19072 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
19073 TYPENAME_TYPE instead, we avoid messing up with a possible
19074 typedef variant case. */
19075 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
19077 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
19078 it first before we can figure out what NAME refers to. */
19079 if (TREE_CODE (scope) == TYPENAME_TYPE)
19080 scope = resolve_typename_type (scope, only_current_p);
19081 /* If we don't know what SCOPE refers to, then we cannot resolve the
19083 if (TREE_CODE (scope) == TYPENAME_TYPE)
19085 /* If the SCOPE is a template type parameter, we have no way of
19086 resolving the name. */
19087 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
19089 /* If the SCOPE is not the current instantiation, there's no reason
19090 to look inside it. */
19091 if (only_current_p && !currently_open_class (scope))
19093 /* If this is a typedef, we don't want to look inside (c++/11987). */
19094 if (typedef_variant_p (type))
19096 /* If SCOPE isn't the template itself, it will not have a valid
19097 TYPE_FIELDS list. */
19098 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
19099 /* scope is either the template itself or a compatible instantiation
19100 like X<T>, so look up the name in the original template. */
19101 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
19103 /* scope is a partial instantiation, so we can't do the lookup or we
19104 will lose the template arguments. */
19106 /* Enter the SCOPE so that name lookup will be resolved as if we
19107 were in the class definition. In particular, SCOPE will no
19108 longer be considered a dependent type. */
19109 pushed_scope = push_scope (scope);
19110 /* Look up the declaration. */
19111 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
19113 result = NULL_TREE;
19115 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
19116 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
19119 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
19120 && TREE_CODE (decl) == TYPE_DECL)
19122 result = TREE_TYPE (decl);
19123 if (result == error_mark_node)
19124 result = NULL_TREE;
19126 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
19127 && DECL_CLASS_TEMPLATE_P (decl))
19131 /* Obtain the template and the arguments. */
19132 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
19133 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
19134 /* Instantiate the template. */
19135 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
19136 /*entering_scope=*/0,
19137 tf_error | tf_user);
19138 if (result == error_mark_node)
19139 result = NULL_TREE;
19142 /* Leave the SCOPE. */
19144 pop_scope (pushed_scope);
19146 /* If we failed to resolve it, return the original typename. */
19150 /* If lookup found a typename type, resolve that too. */
19151 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
19153 /* Ill-formed programs can cause infinite recursion here, so we
19154 must catch that. */
19155 TYPENAME_IS_RESOLVING_P (type) = 1;
19156 result = resolve_typename_type (result, only_current_p);
19157 TYPENAME_IS_RESOLVING_P (type) = 0;
19160 /* Qualify the resulting type. */
19161 quals = cp_type_quals (type);
19163 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
19168 /* EXPR is an expression which is not type-dependent. Return a proxy
19169 for EXPR that can be used to compute the types of larger
19170 expressions containing EXPR. */
19173 build_non_dependent_expr (tree expr)
19177 #ifdef ENABLE_CHECKING
19178 /* Try to get a constant value for all non-type-dependent expressions in
19179 order to expose bugs in *_dependent_expression_p and constexpr. */
19180 if (cxx_dialect >= cxx0x)
19181 maybe_constant_value (fold_non_dependent_expr_sfinae (expr, tf_none));
19184 /* Preserve OVERLOADs; the functions must be available to resolve
19187 if (TREE_CODE (inner_expr) == STMT_EXPR)
19188 inner_expr = stmt_expr_value_expr (inner_expr);
19189 if (TREE_CODE (inner_expr) == ADDR_EXPR)
19190 inner_expr = TREE_OPERAND (inner_expr, 0);
19191 if (TREE_CODE (inner_expr) == COMPONENT_REF)
19192 inner_expr = TREE_OPERAND (inner_expr, 1);
19193 if (is_overloaded_fn (inner_expr)
19194 || TREE_CODE (inner_expr) == OFFSET_REF)
19196 /* There is no need to return a proxy for a variable. */
19197 if (TREE_CODE (expr) == VAR_DECL)
19199 /* Preserve string constants; conversions from string constants to
19200 "char *" are allowed, even though normally a "const char *"
19201 cannot be used to initialize a "char *". */
19202 if (TREE_CODE (expr) == STRING_CST)
19204 /* Preserve arithmetic constants, as an optimization -- there is no
19205 reason to create a new node. */
19206 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
19208 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
19209 There is at least one place where we want to know that a
19210 particular expression is a throw-expression: when checking a ?:
19211 expression, there are special rules if the second or third
19212 argument is a throw-expression. */
19213 if (TREE_CODE (expr) == THROW_EXPR)
19216 if (TREE_CODE (expr) == COND_EXPR)
19217 return build3 (COND_EXPR,
19219 TREE_OPERAND (expr, 0),
19220 (TREE_OPERAND (expr, 1)
19221 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
19222 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
19223 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
19224 if (TREE_CODE (expr) == COMPOUND_EXPR
19225 && !COMPOUND_EXPR_OVERLOADED (expr))
19226 return build2 (COMPOUND_EXPR,
19228 TREE_OPERAND (expr, 0),
19229 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
19231 /* If the type is unknown, it can't really be non-dependent */
19232 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
19234 /* Otherwise, build a NON_DEPENDENT_EXPR. */
19235 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
19238 /* ARGS is a vector of expressions as arguments to a function call.
19239 Replace the arguments with equivalent non-dependent expressions.
19240 This modifies ARGS in place. */
19243 make_args_non_dependent (VEC(tree,gc) *args)
19248 FOR_EACH_VEC_ELT (tree, args, ix, arg)
19250 tree newarg = build_non_dependent_expr (arg);
19252 VEC_replace (tree, args, ix, newarg);
19256 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
19257 with a level one deeper than the actual template parms. */
19262 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
19263 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
19264 TYPE_DECL, get_identifier ("auto"), au);
19265 TYPE_STUB_DECL (au) = TYPE_NAME (au);
19266 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
19267 (0, processing_template_decl + 1, processing_template_decl + 1,
19268 0, TYPE_NAME (au), NULL_TREE);
19269 TYPE_CANONICAL (au) = canonical_type_parameter (au);
19270 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
19271 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
19276 /* Given type ARG, return std::initializer_list<ARG>. */
19281 tree std_init_list = namespace_binding
19282 (get_identifier ("initializer_list"), std_node);
19284 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
19286 error ("deducing from brace-enclosed initializer list requires "
19287 "#include <initializer_list>");
19288 return error_mark_node;
19290 argvec = make_tree_vec (1);
19291 TREE_VEC_ELT (argvec, 0) = arg;
19292 return lookup_template_class (std_init_list, argvec, NULL_TREE,
19293 NULL_TREE, 0, tf_warning_or_error);
19296 /* Replace auto in TYPE with std::initializer_list<auto>. */
19299 listify_autos (tree type, tree auto_node)
19301 tree init_auto = listify (auto_node);
19302 tree argvec = make_tree_vec (1);
19303 TREE_VEC_ELT (argvec, 0) = init_auto;
19304 if (processing_template_decl)
19305 argvec = add_to_template_args (current_template_args (), argvec);
19306 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
19309 /* walk_tree helper for do_auto_deduction. */
19312 contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
19315 /* Is this a variable with the type we're looking for? */
19317 && TREE_TYPE (*tp) == type)
19323 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
19324 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
19327 do_auto_deduction (tree type, tree init, tree auto_node)
19329 tree parms, tparms, targs;
19334 if (processing_template_decl
19335 && (TREE_TYPE (init) == NULL_TREE
19336 || BRACE_ENCLOSED_INITIALIZER_P (init)))
19337 /* Not enough information to try this yet. */
19340 /* The name of the object being declared shall not appear in the
19341 initializer expression. */
19342 decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
19345 error ("variable %q#D with %<auto%> type used in its own "
19346 "initializer", decl);
19347 return error_mark_node;
19350 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
19351 with either a new invented type template parameter U or, if the
19352 initializer is a braced-init-list (8.5.4), with
19353 std::initializer_list<U>. */
19354 if (BRACE_ENCLOSED_INITIALIZER_P (init))
19355 type = listify_autos (type, auto_node);
19357 init = resolve_nondeduced_context (init);
19359 parms = build_tree_list (NULL_TREE, type);
19361 tparms = make_tree_vec (1);
19362 targs = make_tree_vec (1);
19363 TREE_VEC_ELT (tparms, 0)
19364 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
19365 val = type_unification_real (tparms, targs, parms, args, 1, 0,
19366 DEDUCE_CALL, LOOKUP_NORMAL);
19369 if (processing_template_decl)
19370 /* Try again at instantiation time. */
19372 if (type && type != error_mark_node)
19373 /* If type is error_mark_node a diagnostic must have been
19374 emitted by now. Also, having a mention to '<type error>'
19375 in the diagnostic is not really useful to the user. */
19376 error ("unable to deduce %qT from %qE", type, init);
19377 return error_mark_node;
19380 /* If the list of declarators contains more than one declarator, the type
19381 of each declared variable is determined as described above. If the
19382 type deduced for the template parameter U is not the same in each
19383 deduction, the program is ill-formed. */
19384 if (TREE_TYPE (auto_node)
19385 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
19387 error ("inconsistent deduction for %qT: %qT and then %qT",
19388 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
19389 return error_mark_node;
19391 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
19393 if (processing_template_decl)
19394 targs = add_to_template_args (current_template_args (), targs);
19395 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
19398 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
19402 splice_late_return_type (tree type, tree late_return_type)
19406 if (late_return_type == NULL_TREE)
19408 argvec = make_tree_vec (1);
19409 TREE_VEC_ELT (argvec, 0) = late_return_type;
19410 if (processing_template_parmlist)
19411 /* For a late-specified return type in a template type-parameter, we
19412 need to add a dummy argument level for its parmlist. */
19413 argvec = add_to_template_args
19414 (make_tree_vec (processing_template_parmlist), argvec);
19415 if (current_template_parms)
19416 argvec = add_to_template_args (current_template_args (), argvec);
19417 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
19420 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
19423 is_auto (const_tree type)
19425 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19426 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
19432 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
19433 appear as a type-specifier for the declaration in question, we don't
19434 have to look through the whole type. */
19437 type_uses_auto (tree type)
19439 enum tree_code code;
19440 if (is_auto (type))
19443 code = TREE_CODE (type);
19445 if (code == POINTER_TYPE || code == REFERENCE_TYPE
19446 || code == OFFSET_TYPE || code == FUNCTION_TYPE
19447 || code == METHOD_TYPE || code == ARRAY_TYPE)
19448 return type_uses_auto (TREE_TYPE (type));
19450 if (TYPE_PTRMEMFUNC_P (type))
19451 return type_uses_auto (TREE_TYPE (TREE_TYPE
19452 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
19457 /* For a given template T, return the vector of typedefs referenced
19458 in T for which access check is needed at T instantiation time.
19459 T is either a FUNCTION_DECL or a RECORD_TYPE.
19460 Those typedefs were added to T by the function
19461 append_type_to_template_for_access_check. */
19463 VEC(qualified_typedef_usage_t,gc)*
19464 get_types_needing_access_check (tree t)
19467 VEC(qualified_typedef_usage_t,gc) *result = NULL;
19469 if (!t || t == error_mark_node)
19472 if (!(ti = get_template_info (t)))
19475 if (CLASS_TYPE_P (t)
19476 || TREE_CODE (t) == FUNCTION_DECL)
19478 if (!TI_TEMPLATE (ti))
19481 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
19487 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
19488 tied to T. That list of typedefs will be access checked at
19489 T instantiation time.
19490 T is either a FUNCTION_DECL or a RECORD_TYPE.
19491 TYPE_DECL is a TYPE_DECL node representing a typedef.
19492 SCOPE is the scope through which TYPE_DECL is accessed.
19493 LOCATION is the location of the usage point of TYPE_DECL.
19495 This function is a subroutine of
19496 append_type_to_template_for_access_check. */
19499 append_type_to_template_for_access_check_1 (tree t,
19502 location_t location)
19504 qualified_typedef_usage_t typedef_usage;
19507 if (!t || t == error_mark_node)
19510 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
19511 || CLASS_TYPE_P (t))
19513 && TREE_CODE (type_decl) == TYPE_DECL
19516 if (!(ti = get_template_info (t)))
19519 gcc_assert (TI_TEMPLATE (ti));
19521 typedef_usage.typedef_decl = type_decl;
19522 typedef_usage.context = scope;
19523 typedef_usage.locus = location;
19525 VEC_safe_push (qualified_typedef_usage_t, gc,
19526 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
19530 /* Append TYPE_DECL to the template TEMPL.
19531 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
19532 At TEMPL instanciation time, TYPE_DECL will be checked to see
19533 if it can be accessed through SCOPE.
19534 LOCATION is the location of the usage point of TYPE_DECL.
19536 e.g. consider the following code snippet:
19543 template<class U> struct S
19545 C::myint mi; // <-- usage point of the typedef C::myint
19550 At S<char> instantiation time, we need to check the access of C::myint
19551 In other words, we need to check the access of the myint typedef through
19552 the C scope. For that purpose, this function will add the myint typedef
19553 and the scope C through which its being accessed to a list of typedefs
19554 tied to the template S. That list will be walked at template instantiation
19555 time and access check performed on each typedefs it contains.
19556 Note that this particular code snippet should yield an error because
19557 myint is private to C. */
19560 append_type_to_template_for_access_check (tree templ,
19563 location_t location)
19565 qualified_typedef_usage_t *iter;
19568 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
19570 /* Make sure we don't append the type to the template twice. */
19571 FOR_EACH_VEC_ELT (qualified_typedef_usage_t,
19572 get_types_needing_access_check (templ),
19574 if (iter->typedef_decl == type_decl && scope == iter->context)
19577 append_type_to_template_for_access_check_1 (templ, type_decl,
19581 /* Set up the hash tables for template instantiations. */
19584 init_template_processing (void)
19586 decl_specializations = htab_create_ggc (37,
19587 hash_specialization,
19588 eq_specializations,
19590 type_specializations = htab_create_ggc (37,
19591 hash_specialization,
19592 eq_specializations,
19596 /* Print stats about the template hash tables for -fstats. */
19599 print_template_statistics (void)
19601 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
19602 "%f collisions\n", (long) htab_size (decl_specializations),
19603 (long) htab_elements (decl_specializations),
19604 htab_collisions (decl_specializations));
19605 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
19606 "%f collisions\n", (long) htab_size (type_specializations),
19607 (long) htab_elements (type_specializations),
19608 htab_collisions (type_specializations));
19611 #include "gt-cp-pt.h"