1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
47 #include "tree-iterator.h"
50 /* The type of functions taking a tree, and some additional data, and
52 typedef int (*tree_fn_t) (tree, void*);
54 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
56 were not yet available, or because we were putting off doing the work. */
57 struct GTY (()) pending_template {
58 struct pending_template *next;
59 struct tinst_level *tinst;
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static GTY(()) tree saved_trees;
69 static VEC(int,heap) *inline_parm_levels;
71 static GTY(()) struct tinst_level *current_tinst_level;
73 static GTY(()) tree saved_access_scope;
75 /* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78 static tree cur_stmt_expr;
80 /* A map from local variable declarations in the body of the template
81 presently being instantiated to the corresponding instantiated
83 static htab_t local_specializations;
85 typedef struct GTY(()) spec_entry
92 static GTY ((param_is (spec_entry)))
93 htab_t decl_specializations;
95 static GTY ((param_is (spec_entry)))
96 htab_t type_specializations;
98 /* Contains canonical template parameter types. The vector is indexed by
99 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
100 TREE_LIST, whose TREE_VALUEs contain the canonical template
101 parameters of various types and levels. */
102 static GTY(()) VEC(tree,gc) *canonical_template_parms;
104 #define UNIFY_ALLOW_NONE 0
105 #define UNIFY_ALLOW_MORE_CV_QUAL 1
106 #define UNIFY_ALLOW_LESS_CV_QUAL 2
107 #define UNIFY_ALLOW_DERIVED 4
108 #define UNIFY_ALLOW_INTEGER 8
109 #define UNIFY_ALLOW_OUTER_LEVEL 16
110 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
111 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
113 static void push_access_scope (tree);
114 static void pop_access_scope (tree);
115 static bool resolve_overloaded_unification (tree, tree, tree, tree,
116 unification_kind_t, int);
117 static int try_one_overload (tree, tree, tree, tree, tree,
118 unification_kind_t, int, bool);
119 static int unify (tree, tree, tree, tree, int);
120 static void add_pending_template (tree);
121 static int push_tinst_level (tree);
122 static void pop_tinst_level (void);
123 static tree reopen_tinst_level (struct tinst_level *);
124 static tree tsubst_initializer_list (tree, tree);
125 static tree get_class_bindings (tree, tree, tree);
126 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
128 static void tsubst_enum (tree, tree, tree);
129 static tree add_to_template_args (tree, tree);
130 static tree add_outermost_template_args (tree, tree);
131 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
132 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
134 static int type_unification_real (tree, tree, tree, const tree *,
135 unsigned int, int, unification_kind_t, int);
136 static void note_template_header (int);
137 static tree convert_nontype_argument_function (tree, tree);
138 static tree convert_nontype_argument (tree, tree);
139 static tree convert_template_argument (tree, tree, tree,
140 tsubst_flags_t, int, tree);
141 static int for_each_template_parm (tree, tree_fn_t, void*,
142 struct pointer_set_t*, bool);
143 static tree expand_template_argument_pack (tree);
144 static tree build_template_parm_index (int, int, int, tree, tree);
145 static bool inline_needs_template_parms (tree);
146 static void push_inline_template_parms_recursive (tree, int);
147 static tree retrieve_local_specialization (tree);
148 static void register_local_specialization (tree, tree);
149 static hashval_t hash_specialization (const void *p);
150 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
151 static int mark_template_parm (tree, void *);
152 static int template_parm_this_level_p (tree, void *);
153 static tree tsubst_friend_function (tree, tree);
154 static tree tsubst_friend_class (tree, tree);
155 static int can_complete_type_without_circularity (tree);
156 static tree get_bindings (tree, tree, tree, bool);
157 static int template_decl_level (tree);
158 static int check_cv_quals_for_unify (int, tree, tree);
159 static void template_parm_level_and_index (tree, int*, int*);
160 static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
161 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
162 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
163 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
164 static void regenerate_decl_from_template (tree, tree);
165 static tree most_specialized_class (tree, tree);
166 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
167 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
168 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
169 static bool check_specialization_scope (void);
170 static tree process_partial_specialization (tree);
171 static void set_current_access_from_decl (tree);
172 static tree get_template_base (tree, tree, tree, tree);
173 static tree try_class_unification (tree, tree, tree, tree);
174 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
176 static bool template_template_parm_bindings_ok_p (tree, tree);
177 static int template_args_equal (tree, tree);
178 static void tsubst_default_arguments (tree);
179 static tree for_each_template_parm_r (tree *, int *, void *);
180 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
181 static void copy_default_args_to_explicit_spec (tree);
182 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
183 static int eq_local_specializations (const void *, const void *);
184 static bool dependent_template_arg_p (tree);
185 static bool any_template_arguments_need_structural_equality_p (tree);
186 static bool dependent_type_p_r (tree);
187 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
188 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_decl (tree, tree, tsubst_flags_t);
191 static void perform_typedefs_access_check (tree tmpl, tree targs);
192 static void append_type_to_template_for_access_check_1 (tree, tree, tree);
193 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
194 static tree listify (tree);
195 static tree listify_autos (tree, tree);
197 /* Make the current scope suitable for access checking when we are
198 processing T. T can be FUNCTION_DECL for instantiated function
199 template, or VAR_DECL for static member variable (need by
200 instantiate_decl). */
203 push_access_scope (tree t)
205 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
206 || TREE_CODE (t) == VAR_DECL);
208 if (DECL_FRIEND_CONTEXT (t))
209 push_nested_class (DECL_FRIEND_CONTEXT (t));
210 else if (DECL_CLASS_SCOPE_P (t))
211 push_nested_class (DECL_CONTEXT (t));
213 push_to_top_level ();
215 if (TREE_CODE (t) == FUNCTION_DECL)
217 saved_access_scope = tree_cons
218 (NULL_TREE, current_function_decl, saved_access_scope);
219 current_function_decl = t;
223 /* Restore the scope set up by push_access_scope. T is the node we
227 pop_access_scope (tree t)
229 if (TREE_CODE (t) == FUNCTION_DECL)
231 current_function_decl = TREE_VALUE (saved_access_scope);
232 saved_access_scope = TREE_CHAIN (saved_access_scope);
235 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
238 pop_from_top_level ();
241 /* Do any processing required when DECL (a member template
242 declaration) is finished. Returns the TEMPLATE_DECL corresponding
243 to DECL, unless it is a specialization, in which case the DECL
244 itself is returned. */
247 finish_member_template_decl (tree decl)
249 if (decl == error_mark_node)
250 return error_mark_node;
252 gcc_assert (DECL_P (decl));
254 if (TREE_CODE (decl) == TYPE_DECL)
258 type = TREE_TYPE (decl);
259 if (type == error_mark_node)
260 return error_mark_node;
261 if (MAYBE_CLASS_TYPE_P (type)
262 && CLASSTYPE_TEMPLATE_INFO (type)
263 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
265 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
266 check_member_template (tmpl);
271 else if (TREE_CODE (decl) == FIELD_DECL)
272 error ("data member %qD cannot be a member template", decl);
273 else if (DECL_TEMPLATE_INFO (decl))
275 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
277 check_member_template (DECL_TI_TEMPLATE (decl));
278 return DECL_TI_TEMPLATE (decl);
284 error ("invalid member template declaration %qD", decl);
286 return error_mark_node;
289 /* Return the template info node corresponding to T, whatever T is. */
292 get_template_info (const_tree t)
294 tree tinfo = NULL_TREE;
296 if (!t || t == error_mark_node)
299 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
300 tinfo = DECL_TEMPLATE_INFO (t);
302 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
305 if (TAGGED_TYPE_P (t))
306 tinfo = TYPE_TEMPLATE_INFO (t);
311 /* Returns the template nesting level of the indicated class TYPE.
321 A<T>::B<U> has depth two, while A<T> has depth one.
322 Both A<T>::B<int> and A<int>::B<U> have depth one, if
323 they are instantiations, not specializations.
325 This function is guaranteed to return 0 if passed NULL_TREE so
326 that, for example, `template_class_depth (current_class_type)' is
330 template_class_depth (tree type)
335 type && TREE_CODE (type) != NAMESPACE_DECL;
336 type = (TREE_CODE (type) == FUNCTION_DECL)
337 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
339 tree tinfo = get_template_info (type);
341 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
342 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
349 /* Subroutine of maybe_begin_member_template_processing.
350 Returns true if processing DECL needs us to push template parms. */
353 inline_needs_template_parms (tree decl)
355 if (! DECL_TEMPLATE_INFO (decl))
358 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
359 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
362 /* Subroutine of maybe_begin_member_template_processing.
363 Push the template parms in PARMS, starting from LEVELS steps into the
364 chain, and ending at the beginning, since template parms are listed
368 push_inline_template_parms_recursive (tree parmlist, int levels)
370 tree parms = TREE_VALUE (parmlist);
374 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
376 ++processing_template_decl;
377 current_template_parms
378 = tree_cons (size_int (processing_template_decl),
379 parms, current_template_parms);
380 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
382 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
384 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
386 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
388 if (parm == error_mark_node)
391 gcc_assert (DECL_P (parm));
393 switch (TREE_CODE (parm))
402 /* Make a CONST_DECL as is done in process_template_parm.
403 It is ugly that we recreate this here; the original
404 version built in process_template_parm is no longer
406 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
407 CONST_DECL, DECL_NAME (parm),
409 DECL_ARTIFICIAL (decl) = 1;
410 TREE_CONSTANT (decl) = 1;
411 TREE_READONLY (decl) = 1;
412 DECL_INITIAL (decl) = DECL_INITIAL (parm);
413 SET_DECL_TEMPLATE_PARM_P (decl);
424 /* Restore the template parameter context for a member template or
425 a friend template defined in a class definition. */
428 maybe_begin_member_template_processing (tree decl)
433 if (inline_needs_template_parms (decl))
435 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
436 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
438 if (DECL_TEMPLATE_SPECIALIZATION (decl))
441 parms = TREE_CHAIN (parms);
444 push_inline_template_parms_recursive (parms, levels);
447 /* Remember how many levels of template parameters we pushed so that
448 we can pop them later. */
449 VEC_safe_push (int, heap, inline_parm_levels, levels);
452 /* Undo the effects of maybe_begin_member_template_processing. */
455 maybe_end_member_template_processing (void)
460 if (VEC_length (int, inline_parm_levels) == 0)
463 last = VEC_pop (int, inline_parm_levels);
464 for (i = 0; i < last; ++i)
466 --processing_template_decl;
467 current_template_parms = TREE_CHAIN (current_template_parms);
472 /* Return a new template argument vector which contains all of ARGS,
473 but has as its innermost set of arguments the EXTRA_ARGS. */
476 add_to_template_args (tree args, tree extra_args)
483 extra_depth = TMPL_ARGS_DEPTH (extra_args);
484 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
486 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
487 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
489 for (j = 1; j <= extra_depth; ++j, ++i)
490 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
495 /* Like add_to_template_args, but only the outermost ARGS are added to
496 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
497 (EXTRA_ARGS) levels are added. This function is used to combine
498 the template arguments from a partial instantiation with the
499 template arguments used to attain the full instantiation from the
500 partial instantiation. */
503 add_outermost_template_args (tree args, tree extra_args)
507 /* If there are more levels of EXTRA_ARGS than there are ARGS,
508 something very fishy is going on. */
509 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
511 /* If *all* the new arguments will be the EXTRA_ARGS, just return
513 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
516 /* For the moment, we make ARGS look like it contains fewer levels. */
517 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
519 new_args = add_to_template_args (args, extra_args);
521 /* Now, we restore ARGS to its full dimensions. */
522 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
527 /* Return the N levels of innermost template arguments from the ARGS. */
530 get_innermost_template_args (tree args, int n)
538 /* If N is 1, just return the innermost set of template arguments. */
540 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
542 /* If we're not removing anything, just return the arguments we were
544 extra_levels = TMPL_ARGS_DEPTH (args) - n;
545 gcc_assert (extra_levels >= 0);
546 if (extra_levels == 0)
549 /* Make a new set of arguments, not containing the outer arguments. */
550 new_args = make_tree_vec (n);
551 for (i = 1; i <= n; ++i)
552 SET_TMPL_ARGS_LEVEL (new_args, i,
553 TMPL_ARGS_LEVEL (args, i + extra_levels));
558 /* The inverse of get_innermost_template_args: Return all but the innermost
559 EXTRA_LEVELS levels of template arguments from the ARGS. */
562 strip_innermost_template_args (tree args, int extra_levels)
565 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
570 /* If N is 1, just return the outermost set of template arguments. */
572 return TMPL_ARGS_LEVEL (args, 1);
574 /* If we're not removing anything, just return the arguments we were
576 gcc_assert (extra_levels >= 0);
577 if (extra_levels == 0)
580 /* Make a new set of arguments, not containing the inner arguments. */
581 new_args = make_tree_vec (n);
582 for (i = 1; i <= n; ++i)
583 SET_TMPL_ARGS_LEVEL (new_args, i,
584 TMPL_ARGS_LEVEL (args, i));
589 /* We've got a template header coming up; push to a new level for storing
593 begin_template_parm_list (void)
595 /* We use a non-tag-transparent scope here, which causes pushtag to
596 put tags in this scope, rather than in the enclosing class or
597 namespace scope. This is the right thing, since we want
598 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
599 global template class, push_template_decl handles putting the
600 TEMPLATE_DECL into top-level scope. For a nested template class,
603 template <class T> struct S1 {
604 template <class T> struct S2 {};
607 pushtag contains special code to call pushdecl_with_scope on the
608 TEMPLATE_DECL for S2. */
609 begin_scope (sk_template_parms, NULL);
610 ++processing_template_decl;
611 ++processing_template_parmlist;
612 note_template_header (0);
615 /* This routine is called when a specialization is declared. If it is
616 invalid to declare a specialization here, an error is reported and
617 false is returned, otherwise this routine will return true. */
620 check_specialization_scope (void)
622 tree scope = current_scope ();
626 An explicit specialization shall be declared in the namespace of
627 which the template is a member, or, for member templates, in the
628 namespace of which the enclosing class or enclosing class
629 template is a member. An explicit specialization of a member
630 function, member class or static data member of a class template
631 shall be declared in the namespace of which the class template
633 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
635 error ("explicit specialization in non-namespace scope %qD", scope);
641 In an explicit specialization declaration for a member of a class
642 template or a member template that appears in namespace scope,
643 the member template and some of its enclosing class templates may
644 remain unspecialized, except that the declaration shall not
645 explicitly specialize a class member template if its enclosing
646 class templates are not explicitly specialized as well. */
647 if (current_template_parms)
649 error ("enclosing class templates are not explicitly specialized");
656 /* We've just seen template <>. */
659 begin_specialization (void)
661 begin_scope (sk_template_spec, NULL);
662 note_template_header (1);
663 return check_specialization_scope ();
666 /* Called at then end of processing a declaration preceded by
670 end_specialization (void)
673 reset_specialization ();
676 /* Any template <>'s that we have seen thus far are not referring to a
677 function specialization. */
680 reset_specialization (void)
682 processing_specialization = 0;
683 template_header_count = 0;
686 /* We've just seen a template header. If SPECIALIZATION is nonzero,
687 it was of the form template <>. */
690 note_template_header (int specialization)
692 processing_specialization = specialization;
693 template_header_count++;
696 /* We're beginning an explicit instantiation. */
699 begin_explicit_instantiation (void)
701 gcc_assert (!processing_explicit_instantiation);
702 processing_explicit_instantiation = true;
707 end_explicit_instantiation (void)
709 gcc_assert (processing_explicit_instantiation);
710 processing_explicit_instantiation = false;
713 /* An explicit specialization or partial specialization TMPL is being
714 declared. Check that the namespace in which the specialization is
715 occurring is permissible. Returns false iff it is invalid to
716 specialize TMPL in the current namespace. */
719 check_specialization_namespace (tree tmpl)
721 tree tpl_ns = decl_namespace_context (tmpl);
725 An explicit specialization shall be declared in the namespace of
726 which the template is a member, or, for member templates, in the
727 namespace of which the enclosing class or enclosing class
728 template is a member. An explicit specialization of a member
729 function, member class or static data member of a class template
730 shall be declared in the namespace of which the class template is
732 if (is_associated_namespace (current_namespace, tpl_ns))
733 /* Same or super-using namespace. */
737 permerror (input_location, "specialization of %qD in different namespace", tmpl);
738 permerror (input_location, " from definition of %q+#D", tmpl);
743 /* SPEC is an explicit instantiation. Check that it is valid to
744 perform this explicit instantiation in the current namespace. */
747 check_explicit_instantiation_namespace (tree spec)
751 /* DR 275: An explicit instantiation shall appear in an enclosing
752 namespace of its template. */
753 ns = decl_namespace_context (spec);
754 if (!is_ancestor (current_namespace, ns))
755 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
756 "(which does not enclose namespace %qD)",
757 spec, current_namespace, ns);
760 /* The TYPE is being declared. If it is a template type, that means it
761 is a partial specialization. Do appropriate error-checking. */
764 maybe_process_partial_specialization (tree type)
768 if (type == error_mark_node)
769 return error_mark_node;
771 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
773 error ("name of class shadows template template parameter %qD",
775 return error_mark_node;
778 context = TYPE_CONTEXT (type);
780 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
782 /* This is for ordinary explicit specialization and partial
783 specialization of a template class such as:
785 template <> class C<int>;
789 template <class T> class C<T*>;
791 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
793 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
794 && !COMPLETE_TYPE_P (type))
796 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
797 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
798 if (processing_template_decl)
800 if (push_template_decl (TYPE_MAIN_DECL (type))
802 return error_mark_node;
805 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
806 error ("specialization of %qT after instantiation", type);
808 else if (CLASS_TYPE_P (type)
809 && !CLASSTYPE_USE_TEMPLATE (type)
810 && CLASSTYPE_TEMPLATE_INFO (type)
811 && context && CLASS_TYPE_P (context)
812 && CLASSTYPE_TEMPLATE_INFO (context))
814 /* This is for an explicit specialization of member class
815 template according to [temp.expl.spec/18]:
817 template <> template <class U> class C<int>::D;
819 The context `C<int>' must be an implicit instantiation.
820 Otherwise this is just a member class template declared
823 template <> class C<int> { template <class U> class D; };
824 template <> template <class U> class C<int>::D;
826 In the first case, `C<int>::D' is a specialization of `C<T>::D'
827 while in the second case, `C<int>::D' is a primary template
828 and `C<T>::D' may not exist. */
830 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
831 && !COMPLETE_TYPE_P (type))
834 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
836 if (current_namespace
837 != decl_namespace_context (tmpl))
839 permerror (input_location, "specializing %q#T in different namespace", type);
840 permerror (input_location, " from definition of %q+#D", tmpl);
843 /* Check for invalid specialization after instantiation:
845 template <> template <> class C<int>::D<int>;
846 template <> template <class U> class C<int>::D; */
848 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
849 t; t = TREE_CHAIN (t))
851 tree inst = TREE_VALUE (t);
852 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
854 /* We already have a full specialization of this partial
855 instantiation. Reassign it to the new member
856 specialization template. */
860 elt.tmpl = most_general_template (tmpl);
861 elt.args = CLASSTYPE_TI_ARGS (inst);
864 htab_remove_elt (type_specializations, &elt);
867 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
869 slot = (spec_entry **)
870 htab_find_slot (type_specializations, &elt, INSERT);
871 *slot = GGC_NEW (spec_entry);
874 else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
875 /* But if we've had an implicit instantiation, that's a
876 problem ([temp.expl.spec]/6). */
877 error ("specialization %qT after instantiation %qT",
881 /* Mark TYPE as a specialization. And as a result, we only
882 have one level of template argument for the innermost
884 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
885 CLASSTYPE_TI_ARGS (type)
886 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
889 else if (processing_specialization)
891 error ("explicit specialization of non-template %qT", type);
892 return error_mark_node;
898 /* Returns nonzero if we can optimize the retrieval of specializations
899 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
900 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
903 optimize_specialization_lookup_p (tree tmpl)
905 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
906 && DECL_CLASS_SCOPE_P (tmpl)
907 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
909 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
910 /* The optimized lookup depends on the fact that the
911 template arguments for the member function template apply
912 purely to the containing class, which is not true if the
913 containing class is an explicit or partial
915 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
916 && !DECL_MEMBER_TEMPLATE_P (tmpl)
917 && !DECL_CONV_FN_P (tmpl)
918 /* It is possible to have a template that is not a member
919 template and is not a member of a template class:
921 template <typename T>
922 struct S { friend A::f(); };
924 Here, the friend function is a template, but the context does
925 not have template information. The optimized lookup relies
926 on having ARGS be the template arguments for both the class
927 and the function template. */
928 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
931 /* Retrieve the specialization (in the sense of [temp.spec] - a
932 specialization is either an instantiation or an explicit
933 specialization) of TMPL for the given template ARGS. If there is
934 no such specialization, return NULL_TREE. The ARGS are a vector of
935 arguments, or a vector of vectors of arguments, in the case of
936 templates with more than one level of parameters.
938 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
939 then we search for a partial specialization matching ARGS. This
940 parameter is ignored if TMPL is not a class template. */
943 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
945 if (args == error_mark_node)
948 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
950 /* There should be as many levels of arguments as there are
951 levels of parameters. */
952 gcc_assert (TMPL_ARGS_DEPTH (args)
953 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
955 if (optimize_specialization_lookup_p (tmpl))
958 tree class_specialization;
959 VEC(tree,gc) *methods;
963 /* The template arguments actually apply to the containing
964 class. Find the class specialization with those
966 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
968 = retrieve_specialization (class_template, args, 0);
969 if (!class_specialization)
971 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
972 for the specialization. */
973 idx = class_method_index_for_fn (class_specialization, tmpl);
976 /* Iterate through the methods with the indicated name, looking
977 for the one that has an instance of TMPL. */
978 methods = CLASSTYPE_METHOD_VEC (class_specialization);
979 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
981 tree fn = OVL_CURRENT (fns);
982 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
983 /* using-declarations can add base methods to the method vec,
984 and we don't want those here. */
985 && DECL_CONTEXT (fn) == class_specialization)
994 htab_t specializations;
998 elt.spec = NULL_TREE;
1000 if (DECL_CLASS_TEMPLATE_P (tmpl))
1001 specializations = type_specializations;
1003 specializations = decl_specializations;
1006 hash = hash_specialization (&elt);
1007 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1015 /* Like retrieve_specialization, but for local declarations. */
1018 retrieve_local_specialization (tree tmpl)
1022 if (local_specializations == NULL)
1025 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1026 htab_hash_pointer (tmpl));
1027 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
1030 /* Returns nonzero iff DECL is a specialization of TMPL. */
1033 is_specialization_of (tree decl, tree tmpl)
1037 if (TREE_CODE (decl) == FUNCTION_DECL)
1041 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1047 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1049 for (t = TREE_TYPE (decl);
1051 t = CLASSTYPE_USE_TEMPLATE (t)
1052 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1053 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1060 /* Returns nonzero iff DECL is a specialization of friend declaration
1061 FRIEND_DECL according to [temp.friend]. */
1064 is_specialization_of_friend (tree decl, tree friend_decl)
1066 bool need_template = true;
1069 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1070 || TREE_CODE (decl) == TYPE_DECL);
1072 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1073 of a template class, we want to check if DECL is a specialization
1075 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1076 && DECL_TEMPLATE_INFO (friend_decl)
1077 && !DECL_USE_TEMPLATE (friend_decl))
1079 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1080 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1081 need_template = false;
1083 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1084 && !PRIMARY_TEMPLATE_P (friend_decl))
1085 need_template = false;
1087 /* There is nothing to do if this is not a template friend. */
1088 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1091 if (is_specialization_of (decl, friend_decl))
1095 A member of a class template may be declared to be a friend of a
1096 non-template class. In this case, the corresponding member of
1097 every specialization of the class template is a friend of the
1098 class granting friendship.
1100 For example, given a template friend declaration
1102 template <class T> friend void A<T>::f();
1104 the member function below is considered a friend
1106 template <> struct A<int> {
1110 For this type of template friend, TEMPLATE_DEPTH below will be
1111 nonzero. To determine if DECL is a friend of FRIEND, we first
1112 check if the enclosing class is a specialization of another. */
1114 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
1116 && DECL_CLASS_SCOPE_P (decl)
1117 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1118 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1120 /* Next, we check the members themselves. In order to handle
1121 a few tricky cases, such as when FRIEND_DECL's are
1123 template <class T> friend void A<T>::g(T t);
1124 template <class T> template <T t> friend void A<T>::h();
1128 void A<int>::g(int);
1129 template <int> void A<int>::h();
1131 we need to figure out ARGS, the template arguments from
1132 the context of DECL. This is required for template substitution
1133 of `T' in the function parameter of `g' and template parameter
1134 of `h' in the above examples. Here ARGS corresponds to `int'. */
1136 tree context = DECL_CONTEXT (decl);
1137 tree args = NULL_TREE;
1138 int current_depth = 0;
1140 while (current_depth < template_depth)
1142 if (CLASSTYPE_TEMPLATE_INFO (context))
1144 if (current_depth == 0)
1145 args = TYPE_TI_ARGS (context);
1147 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1150 context = TYPE_CONTEXT (context);
1153 if (TREE_CODE (decl) == FUNCTION_DECL)
1158 tree friend_args_type;
1159 tree decl_args_type;
1161 /* Make sure that both DECL and FRIEND_DECL are templates or
1163 is_template = DECL_TEMPLATE_INFO (decl)
1164 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1165 if (need_template ^ is_template)
1167 else if (is_template)
1169 /* If both are templates, check template parameter list. */
1171 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1173 if (!comp_template_parms
1174 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1178 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1181 decl_type = TREE_TYPE (decl);
1183 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1184 tf_none, NULL_TREE);
1185 if (friend_type == error_mark_node)
1188 /* Check if return types match. */
1189 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1192 /* Check if function parameter types match, ignoring the
1193 `this' parameter. */
1194 friend_args_type = TYPE_ARG_TYPES (friend_type);
1195 decl_args_type = TYPE_ARG_TYPES (decl_type);
1196 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1197 friend_args_type = TREE_CHAIN (friend_args_type);
1198 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1199 decl_args_type = TREE_CHAIN (decl_args_type);
1201 return compparms (decl_args_type, friend_args_type);
1205 /* DECL is a TYPE_DECL */
1207 tree decl_type = TREE_TYPE (decl);
1209 /* Make sure that both DECL and FRIEND_DECL are templates or
1212 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1213 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1215 if (need_template ^ is_template)
1217 else if (is_template)
1220 /* If both are templates, check the name of the two
1221 TEMPLATE_DECL's first because is_friend didn't. */
1222 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1223 != DECL_NAME (friend_decl))
1226 /* Now check template parameter list. */
1228 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1230 return comp_template_parms
1231 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1235 return (DECL_NAME (decl)
1236 == DECL_NAME (friend_decl));
1242 /* Register the specialization SPEC as a specialization of TMPL with
1243 the indicated ARGS. IS_FRIEND indicates whether the specialization
1244 is actually just a friend declaration. Returns SPEC, or an
1245 equivalent prior declaration, if available. */
1248 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1252 spec_entry **slot = NULL;
1255 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
1257 if (TREE_CODE (spec) == FUNCTION_DECL
1258 && uses_template_parms (DECL_TI_ARGS (spec)))
1259 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1260 register it; we want the corresponding TEMPLATE_DECL instead.
1261 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1262 the more obvious `uses_template_parms (spec)' to avoid problems
1263 with default function arguments. In particular, given
1264 something like this:
1266 template <class T> void f(T t1, T t = T())
1268 the default argument expression is not substituted for in an
1269 instantiation unless and until it is actually needed. */
1272 if (optimize_specialization_lookup_p (tmpl))
1273 /* We don't put these specializations in the hash table, but we might
1274 want to give an error about a mismatch. */
1275 fn = retrieve_specialization (tmpl, args, 0);
1283 hash = hash_specialization (&elt);
1285 slot = (spec_entry **)
1286 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1293 /* We can sometimes try to re-register a specialization that we've
1294 already got. In particular, regenerate_decl_from_template calls
1295 duplicate_decls which will update the specialization list. But,
1296 we'll still get called again here anyhow. It's more convenient
1297 to simply allow this than to try to prevent it. */
1300 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1302 if (DECL_TEMPLATE_INSTANTIATION (fn))
1304 if (DECL_ODR_USED (fn)
1305 || DECL_EXPLICIT_INSTANTIATION (fn))
1307 error ("specialization of %qD after instantiation",
1309 return error_mark_node;
1314 /* This situation should occur only if the first
1315 specialization is an implicit instantiation, the
1316 second is an explicit specialization, and the
1317 implicit instantiation has not yet been used. That
1318 situation can occur if we have implicitly
1319 instantiated a member function and then specialized
1322 We can also wind up here if a friend declaration that
1323 looked like an instantiation turns out to be a
1326 template <class T> void foo(T);
1327 class S { friend void foo<>(int) };
1328 template <> void foo(int);
1330 We transform the existing DECL in place so that any
1331 pointers to it become pointers to the updated
1334 If there was a definition for the template, but not
1335 for the specialization, we want this to look as if
1336 there were no definition, and vice versa. */
1337 DECL_INITIAL (fn) = NULL_TREE;
1338 duplicate_decls (spec, fn, is_friend);
1339 /* The call to duplicate_decls will have applied
1342 An explicit specialization of a function template
1343 is inline only if it is explicitly declared to be,
1344 and independently of whether its function template
1347 to the primary function; now copy the inline bits to
1348 the various clones. */
1349 FOR_EACH_CLONE (clone, fn)
1351 DECL_DECLARED_INLINE_P (clone)
1352 = DECL_DECLARED_INLINE_P (fn);
1353 DECL_SOURCE_LOCATION (clone)
1354 = DECL_SOURCE_LOCATION (fn);
1356 check_specialization_namespace (fn);
1361 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1363 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1364 /* Dup decl failed, but this is a new definition. Set the
1365 line number so any errors match this new
1367 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1373 return duplicate_decls (spec, fn, is_friend);
1375 /* A specialization must be declared in the same namespace as the
1376 template it is specializing. */
1377 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1378 && !check_specialization_namespace (tmpl))
1379 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1381 if (!optimize_specialization_lookup_p (tmpl))
1383 gcc_assert (tmpl && args && spec);
1384 *slot = GGC_NEW (spec_entry);
1386 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1387 && PRIMARY_TEMPLATE_P (tmpl)
1388 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1389 /* TMPL is a forward declaration of a template function; keep a list
1390 of all specializations in case we need to reassign them to a friend
1391 template later in tsubst_friend_function. */
1392 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1393 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1399 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1400 TMPL and ARGS members, ignores SPEC. */
1403 eq_specializations (const void *p1, const void *p2)
1405 const spec_entry *e1 = (const spec_entry *)p1;
1406 const spec_entry *e2 = (const spec_entry *)p2;
1408 return (e1->tmpl == e2->tmpl
1409 && comp_template_args (e1->args, e2->args));
1412 /* Returns a hash for a template TMPL and template arguments ARGS. */
1415 hash_tmpl_and_args (tree tmpl, tree args)
1417 hashval_t val = DECL_UID (tmpl);
1418 return iterative_hash_template_arg (args, val);
1421 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1425 hash_specialization (const void *p)
1427 const spec_entry *e = (const spec_entry *)p;
1428 return hash_tmpl_and_args (e->tmpl, e->args);
1431 /* Recursively calculate a hash value for a template argument ARG, for use
1432 in the hash tables of template specializations. */
1435 iterative_hash_template_arg (tree arg, hashval_t val)
1437 unsigned HOST_WIDE_INT i;
1438 enum tree_code code;
1441 if (arg == NULL_TREE)
1442 return iterative_hash_object (arg, val);
1447 code = TREE_CODE (arg);
1448 tclass = TREE_CODE_CLASS (code);
1450 val = iterative_hash_object (code, val);
1457 case IDENTIFIER_NODE:
1458 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1462 int i, len = TREE_VEC_LENGTH (arg);
1463 for (i = 0; i < len; ++i)
1464 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1468 case TYPE_PACK_EXPANSION:
1469 case EXPR_PACK_EXPANSION:
1470 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1472 case ARGUMENT_PACK_SELECT:
1473 /* We can get one of these when re-hashing a previous entry in the middle
1474 of substituting into a pack expansion. Just look through it... */
1475 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1476 /* ...and fall through. */
1477 case TYPE_ARGUMENT_PACK:
1478 case NONTYPE_ARGUMENT_PACK:
1479 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1482 for (; arg; arg = TREE_CHAIN (arg))
1483 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1487 for (; arg; arg = OVL_CHAIN (arg))
1488 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1494 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1496 val = iterative_hash_template_arg (field, val);
1497 val = iterative_hash_template_arg (value, val);
1503 if (!DECL_ARTIFICIAL (arg))
1504 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1505 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1508 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1511 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1512 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1514 case TEMPLATE_PARM_INDEX:
1515 val = iterative_hash_template_arg
1516 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1517 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1518 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1521 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1522 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1523 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1526 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1528 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1532 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1533 code = TREE_CODE (TREE_OPERAND (arg, 1));
1534 val = iterative_hash_object (code, val);
1535 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1541 if (TYPE_CANONICAL (arg))
1542 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1544 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1545 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1546 /* Otherwise just compare the types during lookup. */
1549 case tcc_declaration:
1551 return iterative_hash_expr (arg, val);
1554 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1556 unsigned n = TREE_OPERAND_LENGTH (arg);
1557 for (i = 0; i < n; ++i)
1558 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1567 /* Unregister the specialization SPEC as a specialization of TMPL.
1568 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1569 if the SPEC was listed as a specialization of TMPL.
1571 Note that SPEC has been ggc_freed, so we can't look inside it. */
1574 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1579 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1580 elt.args = TI_ARGS (tinfo);
1581 elt.spec = NULL_TREE;
1583 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1586 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1587 gcc_assert (new_spec != NULL_TREE);
1588 (*slot)->spec = new_spec;
1595 /* Compare an entry in the local specializations hash table P1 (which
1596 is really a pointer to a TREE_LIST) with P2 (which is really a
1600 eq_local_specializations (const void *p1, const void *p2)
1602 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
1605 /* Hash P1, an entry in the local specializations table. */
1608 hash_local_specialization (const void* p1)
1610 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
1613 /* Like register_specialization, but for local declarations. We are
1614 registering SPEC, an instantiation of TMPL. */
1617 register_local_specialization (tree spec, tree tmpl)
1621 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1622 htab_hash_pointer (tmpl), INSERT);
1623 *slot = build_tree_list (spec, tmpl);
1626 /* TYPE is a class type. Returns true if TYPE is an explicitly
1627 specialized class. */
1630 explicit_class_specialization_p (tree type)
1632 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1634 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1637 /* Print the list of candidate FNS in an error message. */
1640 print_candidates (tree fns)
1645 const char *str = "candidates are:";
1647 if (is_overloaded_fn (fns))
1649 for (f = fns; f; f = OVL_NEXT (f))
1651 error ("%s %+#D", str, OVL_CURRENT (f));
1655 else for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1657 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1658 error ("%s %+#D", str, OVL_CURRENT (f));
1663 /* Returns the template (one of the functions given by TEMPLATE_ID)
1664 which can be specialized to match the indicated DECL with the
1665 explicit template args given in TEMPLATE_ID. The DECL may be
1666 NULL_TREE if none is available. In that case, the functions in
1667 TEMPLATE_ID are non-members.
1669 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1670 specialization of a member template.
1672 The TEMPLATE_COUNT is the number of references to qualifying
1673 template classes that appeared in the name of the function. See
1674 check_explicit_specialization for a more accurate description.
1676 TSK indicates what kind of template declaration (if any) is being
1677 declared. TSK_TEMPLATE indicates that the declaration given by
1678 DECL, though a FUNCTION_DECL, has template parameters, and is
1679 therefore a template function.
1681 The template args (those explicitly specified and those deduced)
1682 are output in a newly created vector *TARGS_OUT.
1684 If it is impossible to determine the result, an error message is
1685 issued. The error_mark_node is returned to indicate failure. */
1688 determine_specialization (tree template_id,
1691 int need_member_template,
1697 tree explicit_targs;
1698 tree candidates = NULL_TREE;
1699 /* A TREE_LIST of templates of which DECL may be a specialization.
1700 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1701 corresponding TREE_PURPOSE is the set of template arguments that,
1702 when used to instantiate the template, would produce a function
1703 with the signature of DECL. */
1704 tree templates = NULL_TREE;
1706 struct cp_binding_level *b;
1708 *targs_out = NULL_TREE;
1710 if (template_id == error_mark_node || decl == error_mark_node)
1711 return error_mark_node;
1713 fns = TREE_OPERAND (template_id, 0);
1714 explicit_targs = TREE_OPERAND (template_id, 1);
1716 if (fns == error_mark_node)
1717 return error_mark_node;
1719 /* Check for baselinks. */
1720 if (BASELINK_P (fns))
1721 fns = BASELINK_FUNCTIONS (fns);
1723 if (!is_overloaded_fn (fns))
1725 error ("%qD is not a function template", fns);
1726 return error_mark_node;
1729 /* Count the number of template headers specified for this
1732 for (b = current_binding_level;
1733 b->kind == sk_template_parms;
1737 for (; fns; fns = OVL_NEXT (fns))
1739 tree fn = OVL_CURRENT (fns);
1741 if (TREE_CODE (fn) == TEMPLATE_DECL)
1743 tree decl_arg_types;
1746 /* In case of explicit specialization, we need to check if
1747 the number of template headers appearing in the specialization
1748 is correct. This is usually done in check_explicit_specialization,
1749 but the check done there cannot be exhaustive when specializing
1750 member functions. Consider the following code:
1752 template <> void A<int>::f(int);
1753 template <> template <> void A<int>::f(int);
1755 Assuming that A<int> is not itself an explicit specialization
1756 already, the first line specializes "f" which is a non-template
1757 member function, whilst the second line specializes "f" which
1758 is a template member function. So both lines are syntactically
1759 correct, and check_explicit_specialization does not reject
1762 Here, we can do better, as we are matching the specialization
1763 against the declarations. We count the number of template
1764 headers, and we check if they match TEMPLATE_COUNT + 1
1765 (TEMPLATE_COUNT is the number of qualifying template classes,
1766 plus there must be another header for the member template
1769 Notice that if header_count is zero, this is not a
1770 specialization but rather a template instantiation, so there
1771 is no check we can perform here. */
1772 if (header_count && header_count != template_count + 1)
1775 /* Check that the number of template arguments at the
1776 innermost level for DECL is the same as for FN. */
1777 if (current_binding_level->kind == sk_template_parms
1778 && !current_binding_level->explicit_spec_p
1779 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1780 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1781 (current_template_parms))))
1784 /* DECL might be a specialization of FN. */
1785 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1786 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1788 /* For a non-static member function, we need to make sure
1789 that the const qualification is the same. Since
1790 get_bindings does not try to merge the "this" parameter,
1791 we must do the comparison explicitly. */
1792 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1793 && !same_type_p (TREE_VALUE (fn_arg_types),
1794 TREE_VALUE (decl_arg_types)))
1797 /* Skip the "this" parameter and, for constructors of
1798 classes with virtual bases, the VTT parameter. A
1799 full specialization of a constructor will have a VTT
1800 parameter, but a template never will. */
1802 = skip_artificial_parms_for (decl, decl_arg_types);
1804 = skip_artificial_parms_for (fn, fn_arg_types);
1806 /* Check that the number of function parameters matches.
1808 template <class T> void f(int i = 0);
1809 template <> void f<int>();
1810 The specialization f<int> is invalid but is not caught
1811 by get_bindings below. */
1812 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1815 /* Function templates cannot be specializations; there are
1816 no partial specializations of functions. Therefore, if
1817 the type of DECL does not match FN, there is no
1819 if (tsk == tsk_template)
1821 if (compparms (fn_arg_types, decl_arg_types))
1822 candidates = tree_cons (NULL_TREE, fn, candidates);
1826 /* See whether this function might be a specialization of this
1828 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1831 /* We cannot deduce template arguments that when used to
1832 specialize TMPL will produce DECL. */
1835 /* Save this template, and the arguments deduced. */
1836 templates = tree_cons (targs, fn, templates);
1838 else if (need_member_template)
1839 /* FN is an ordinary member function, and we need a
1840 specialization of a member template. */
1842 else if (TREE_CODE (fn) != FUNCTION_DECL)
1843 /* We can get IDENTIFIER_NODEs here in certain erroneous
1846 else if (!DECL_FUNCTION_MEMBER_P (fn))
1847 /* This is just an ordinary non-member function. Nothing can
1848 be a specialization of that. */
1850 else if (DECL_ARTIFICIAL (fn))
1851 /* Cannot specialize functions that are created implicitly. */
1855 tree decl_arg_types;
1857 /* This is an ordinary member function. However, since
1858 we're here, we can assume it's enclosing class is a
1859 template class. For example,
1861 template <typename T> struct S { void f(); };
1862 template <> void S<int>::f() {}
1864 Here, S<int>::f is a non-template, but S<int> is a
1865 template class. If FN has the same type as DECL, we
1866 might be in business. */
1868 if (!DECL_TEMPLATE_INFO (fn))
1869 /* Its enclosing class is an explicit specialization
1870 of a template class. This is not a candidate. */
1873 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1874 TREE_TYPE (TREE_TYPE (fn))))
1875 /* The return types differ. */
1878 /* Adjust the type of DECL in case FN is a static member. */
1879 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1880 if (DECL_STATIC_FUNCTION_P (fn)
1881 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1882 decl_arg_types = TREE_CHAIN (decl_arg_types);
1884 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1887 candidates = tree_cons (NULL_TREE, fn, candidates);
1891 if (templates && TREE_CHAIN (templates))
1897 It is possible for a specialization with a given function
1898 signature to be instantiated from more than one function
1899 template. In such cases, explicit specification of the
1900 template arguments must be used to uniquely identify the
1901 function template specialization being specialized.
1903 Note that here, there's no suggestion that we're supposed to
1904 determine which of the candidate templates is most
1905 specialized. However, we, also have:
1909 Partial ordering of overloaded function template
1910 declarations is used in the following contexts to select
1911 the function template to which a function template
1912 specialization refers:
1914 -- when an explicit specialization refers to a function
1917 So, we do use the partial ordering rules, at least for now.
1918 This extension can only serve to make invalid programs valid,
1919 so it's safe. And, there is strong anecdotal evidence that
1920 the committee intended the partial ordering rules to apply;
1921 the EDG front end has that behavior, and John Spicer claims
1922 that the committee simply forgot to delete the wording in
1923 [temp.expl.spec]. */
1924 tree tmpl = most_specialized_instantiation (templates);
1925 if (tmpl != error_mark_node)
1928 TREE_CHAIN (templates) = NULL_TREE;
1932 if (templates == NULL_TREE && candidates == NULL_TREE)
1934 error ("template-id %qD for %q+D does not match any template "
1935 "declaration", template_id, decl);
1936 return error_mark_node;
1938 else if ((templates && TREE_CHAIN (templates))
1939 || (candidates && TREE_CHAIN (candidates))
1940 || (templates && candidates))
1942 error ("ambiguous template specialization %qD for %q+D",
1944 chainon (candidates, templates);
1945 print_candidates (candidates);
1946 return error_mark_node;
1949 /* We have one, and exactly one, match. */
1952 tree fn = TREE_VALUE (candidates);
1953 *targs_out = copy_node (DECL_TI_ARGS (fn));
1954 /* DECL is a re-declaration or partial instantiation of a template
1956 if (TREE_CODE (fn) == TEMPLATE_DECL)
1958 /* It was a specialization of an ordinary member function in a
1960 return DECL_TI_TEMPLATE (fn);
1963 /* It was a specialization of a template. */
1964 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1965 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1967 *targs_out = copy_node (targs);
1968 SET_TMPL_ARGS_LEVEL (*targs_out,
1969 TMPL_ARGS_DEPTH (*targs_out),
1970 TREE_PURPOSE (templates));
1973 *targs_out = TREE_PURPOSE (templates);
1974 return TREE_VALUE (templates);
1977 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1978 but with the default argument values filled in from those in the
1982 copy_default_args_to_explicit_spec_1 (tree spec_types,
1985 tree new_spec_types;
1990 if (spec_types == void_list_node)
1991 return void_list_node;
1993 /* Substitute into the rest of the list. */
1995 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1996 TREE_CHAIN (tmpl_types));
1998 /* Add the default argument for this parameter. */
1999 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2000 TREE_VALUE (spec_types),
2004 /* DECL is an explicit specialization. Replicate default arguments
2005 from the template it specializes. (That way, code like:
2007 template <class T> void f(T = 3);
2008 template <> void f(double);
2011 works, as required.) An alternative approach would be to look up
2012 the correct default arguments at the call-site, but this approach
2013 is consistent with how implicit instantiations are handled. */
2016 copy_default_args_to_explicit_spec (tree decl)
2021 tree new_spec_types;
2025 tree object_type = NULL_TREE;
2026 tree in_charge = NULL_TREE;
2027 tree vtt = NULL_TREE;
2029 /* See if there's anything we need to do. */
2030 tmpl = DECL_TI_TEMPLATE (decl);
2031 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2032 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2033 if (TREE_PURPOSE (t))
2038 old_type = TREE_TYPE (decl);
2039 spec_types = TYPE_ARG_TYPES (old_type);
2041 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2043 /* Remove the this pointer, but remember the object's type for
2045 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2046 spec_types = TREE_CHAIN (spec_types);
2047 tmpl_types = TREE_CHAIN (tmpl_types);
2049 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2051 /* DECL may contain more parameters than TMPL due to the extra
2052 in-charge parameter in constructors and destructors. */
2053 in_charge = spec_types;
2054 spec_types = TREE_CHAIN (spec_types);
2056 if (DECL_HAS_VTT_PARM_P (decl))
2059 spec_types = TREE_CHAIN (spec_types);
2063 /* Compute the merged default arguments. */
2065 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2067 /* Compute the new FUNCTION_TYPE. */
2071 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2076 /* Put the in-charge parameter back. */
2077 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2078 TREE_VALUE (in_charge),
2081 new_type = build_method_type_directly (object_type,
2082 TREE_TYPE (old_type),
2086 new_type = build_function_type (TREE_TYPE (old_type),
2088 new_type = cp_build_type_attribute_variant (new_type,
2089 TYPE_ATTRIBUTES (old_type));
2090 new_type = build_exception_variant (new_type,
2091 TYPE_RAISES_EXCEPTIONS (old_type));
2092 TREE_TYPE (decl) = new_type;
2095 /* Check to see if the function just declared, as indicated in
2096 DECLARATOR, and in DECL, is a specialization of a function
2097 template. We may also discover that the declaration is an explicit
2098 instantiation at this point.
2100 Returns DECL, or an equivalent declaration that should be used
2101 instead if all goes well. Issues an error message if something is
2102 amiss. Returns error_mark_node if the error is not easily
2105 FLAGS is a bitmask consisting of the following flags:
2107 2: The function has a definition.
2108 4: The function is a friend.
2110 The TEMPLATE_COUNT is the number of references to qualifying
2111 template classes that appeared in the name of the function. For
2114 template <class T> struct S { void f(); };
2117 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2118 classes are not counted in the TEMPLATE_COUNT, so that in
2120 template <class T> struct S {};
2121 template <> struct S<int> { void f(); }
2122 template <> void S<int>::f();
2124 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2125 invalid; there should be no template <>.)
2127 If the function is a specialization, it is marked as such via
2128 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2129 is set up correctly, and it is added to the list of specializations
2130 for that template. */
2133 check_explicit_specialization (tree declarator,
2138 int have_def = flags & 2;
2139 int is_friend = flags & 4;
2140 int specialization = 0;
2141 int explicit_instantiation = 0;
2142 int member_specialization = 0;
2143 tree ctype = DECL_CLASS_CONTEXT (decl);
2144 tree dname = DECL_NAME (decl);
2149 if (!processing_specialization)
2152 tsk = tsk_excessive_parms;
2155 tsk = current_tmpl_spec_kind (template_count);
2160 if (processing_specialization)
2163 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2165 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2168 /* This could be something like:
2170 template <class T> void f(T);
2171 class S { friend void f<>(int); } */
2175 /* This case handles bogus declarations like template <>
2176 template <class T> void f<int>(); */
2178 error ("template-id %qD in declaration of primary template",
2185 case tsk_invalid_member_spec:
2186 /* The error has already been reported in
2187 check_specialization_scope. */
2188 return error_mark_node;
2190 case tsk_invalid_expl_inst:
2191 error ("template parameter list used in explicit instantiation");
2197 error ("definition provided for explicit instantiation");
2199 explicit_instantiation = 1;
2202 case tsk_excessive_parms:
2203 case tsk_insufficient_parms:
2204 if (tsk == tsk_excessive_parms)
2205 error ("too many template parameter lists in declaration of %qD",
2207 else if (template_header_count)
2208 error("too few template parameter lists in declaration of %qD", decl);
2210 error("explicit specialization of %qD must be introduced by "
2211 "%<template <>%>", decl);
2215 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2217 member_specialization = 1;
2223 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2225 /* This case handles bogus declarations like template <>
2226 template <class T> void f<int>(); */
2228 if (uses_template_parms (declarator))
2229 error ("function template partial specialization %qD "
2230 "is not allowed", declarator);
2232 error ("template-id %qD in declaration of primary template",
2237 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2238 /* This is a specialization of a member template, without
2239 specialization the containing class. Something like:
2241 template <class T> struct S {
2242 template <class U> void f (U);
2244 template <> template <class U> void S<int>::f(U) {}
2246 That's a specialization -- but of the entire template. */
2254 if (specialization || member_specialization)
2256 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2257 for (; t; t = TREE_CHAIN (t))
2258 if (TREE_PURPOSE (t))
2260 permerror (input_location,
2261 "default argument specified in explicit specialization");
2266 if (specialization || member_specialization || explicit_instantiation)
2268 tree tmpl = NULL_TREE;
2269 tree targs = NULL_TREE;
2271 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2272 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2276 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2281 /* If there is no class context, the explicit instantiation
2282 must be at namespace scope. */
2283 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2285 /* Find the namespace binding, using the declaration
2287 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2289 if (fns == error_mark_node || !is_overloaded_fn (fns))
2291 error ("%qD is not a template function", dname);
2292 fns = error_mark_node;
2296 tree fn = OVL_CURRENT (fns);
2297 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
2298 CP_DECL_CONTEXT (fn)))
2299 error ("%qD is not declared in %qD",
2300 decl, current_namespace);
2304 declarator = lookup_template_function (fns, NULL_TREE);
2307 if (declarator == error_mark_node)
2308 return error_mark_node;
2310 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2312 if (!explicit_instantiation)
2313 /* A specialization in class scope. This is invalid,
2314 but the error will already have been flagged by
2315 check_specialization_scope. */
2316 return error_mark_node;
2319 /* It's not valid to write an explicit instantiation in
2322 class C { template void f(); }
2324 This case is caught by the parser. However, on
2327 template class C { void f(); };
2329 (which is invalid) we can get here. The error will be
2336 else if (ctype != NULL_TREE
2337 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2340 /* Find the list of functions in ctype that have the same
2341 name as the declared function. */
2342 tree name = TREE_OPERAND (declarator, 0);
2343 tree fns = NULL_TREE;
2346 if (constructor_name_p (name, ctype))
2348 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2350 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2351 : !CLASSTYPE_DESTRUCTORS (ctype))
2353 /* From [temp.expl.spec]:
2355 If such an explicit specialization for the member
2356 of a class template names an implicitly-declared
2357 special member function (clause _special_), the
2358 program is ill-formed.
2360 Similar language is found in [temp.explicit]. */
2361 error ("specialization of implicitly-declared special member function");
2362 return error_mark_node;
2365 name = is_constructor ? ctor_identifier : dtor_identifier;
2368 if (!DECL_CONV_FN_P (decl))
2370 idx = lookup_fnfields_1 (ctype, name);
2372 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2376 VEC(tree,gc) *methods;
2379 /* For a type-conversion operator, we cannot do a
2380 name-based lookup. We might be looking for `operator
2381 int' which will be a specialization of `operator T'.
2382 So, we find *all* the conversion operators, and then
2383 select from them. */
2386 methods = CLASSTYPE_METHOD_VEC (ctype);
2388 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2389 VEC_iterate (tree, methods, idx, ovl);
2392 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2393 /* There are no more conversion functions. */
2396 /* Glue all these conversion functions together
2397 with those we already have. */
2398 for (; ovl; ovl = OVL_NEXT (ovl))
2399 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2403 if (fns == NULL_TREE)
2405 error ("no member function %qD declared in %qT", name, ctype);
2406 return error_mark_node;
2409 TREE_OPERAND (declarator, 0) = fns;
2412 /* Figure out what exactly is being specialized at this point.
2413 Note that for an explicit instantiation, even one for a
2414 member function, we cannot tell apriori whether the
2415 instantiation is for a member template, or just a member
2416 function of a template class. Even if a member template is
2417 being instantiated, the member template arguments may be
2418 elided if they can be deduced from the rest of the
2420 tmpl = determine_specialization (declarator, decl,
2422 member_specialization,
2426 if (!tmpl || tmpl == error_mark_node)
2427 /* We couldn't figure out what this declaration was
2429 return error_mark_node;
2432 tree gen_tmpl = most_general_template (tmpl);
2434 if (explicit_instantiation)
2436 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2437 is done by do_decl_instantiation later. */
2439 int arg_depth = TMPL_ARGS_DEPTH (targs);
2440 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2442 if (arg_depth > parm_depth)
2444 /* If TMPL is not the most general template (for
2445 example, if TMPL is a friend template that is
2446 injected into namespace scope), then there will
2447 be too many levels of TARGS. Remove some of them
2452 new_targs = make_tree_vec (parm_depth);
2453 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2454 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2455 = TREE_VEC_ELT (targs, i);
2459 return instantiate_template (tmpl, targs, tf_error);
2462 /* If we thought that the DECL was a member function, but it
2463 turns out to be specializing a static member function,
2464 make DECL a static member function as well. */
2465 if (DECL_STATIC_FUNCTION_P (tmpl)
2466 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2467 revert_static_member_fn (decl);
2469 /* If this is a specialization of a member template of a
2470 template class, we want to return the TEMPLATE_DECL, not
2471 the specialization of it. */
2472 if (tsk == tsk_template)
2474 tree result = DECL_TEMPLATE_RESULT (tmpl);
2475 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2476 DECL_INITIAL (result) = NULL_TREE;
2480 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2481 DECL_SOURCE_LOCATION (result)
2482 = DECL_SOURCE_LOCATION (decl);
2483 /* We want to use the argument list specified in the
2484 definition, not in the original declaration. */
2485 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2486 for (parm = DECL_ARGUMENTS (result); parm;
2487 parm = TREE_CHAIN (parm))
2488 DECL_CONTEXT (parm) = result;
2490 return register_specialization (tmpl, gen_tmpl, targs,
2494 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2495 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2497 /* Inherit default function arguments from the template
2498 DECL is specializing. */
2499 copy_default_args_to_explicit_spec (decl);
2501 /* This specialization has the same protection as the
2502 template it specializes. */
2503 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2504 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2506 /* 7.1.1-1 [dcl.stc]
2508 A storage-class-specifier shall not be specified in an
2509 explicit specialization...
2511 The parser rejects these, so unless action is taken here,
2512 explicit function specializations will always appear with
2515 The action recommended by the C++ CWG in response to C++
2516 defect report 605 is to make the storage class and linkage
2517 of the explicit specialization match the templated function:
2519 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2521 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2523 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2524 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2526 /* This specialization has the same linkage and visibility as
2527 the function template it specializes. */
2528 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
2529 if (! TREE_PUBLIC (decl))
2531 DECL_INTERFACE_KNOWN (decl) = 1;
2532 DECL_NOT_REALLY_EXTERN (decl) = 1;
2534 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2535 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2537 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2538 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2542 /* If DECL is a friend declaration, declared using an
2543 unqualified name, the namespace associated with DECL may
2544 have been set incorrectly. For example, in:
2546 template <typename T> void f(T);
2548 struct S { friend void f<int>(int); }
2551 we will have set the DECL_CONTEXT for the friend
2552 declaration to N, rather than to the global namespace. */
2553 if (DECL_NAMESPACE_SCOPE_P (decl))
2554 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2556 if (is_friend && !have_def)
2557 /* This is not really a declaration of a specialization.
2558 It's just the name of an instantiation. But, it's not
2559 a request for an instantiation, either. */
2560 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2561 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2562 /* This is indeed a specialization. In case of constructors
2563 and destructors, we need in-charge and not-in-charge
2564 versions in V3 ABI. */
2565 clone_function_decl (decl, /*update_method_vec_p=*/0);
2567 /* Register this specialization so that we can find it
2569 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
2576 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2577 parameters. These are represented in the same format used for
2578 DECL_TEMPLATE_PARMS. */
2581 comp_template_parms (const_tree parms1, const_tree parms2)
2586 if (parms1 == parms2)
2589 for (p1 = parms1, p2 = parms2;
2590 p1 != NULL_TREE && p2 != NULL_TREE;
2591 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2593 tree t1 = TREE_VALUE (p1);
2594 tree t2 = TREE_VALUE (p2);
2597 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2598 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2600 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2603 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2605 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2606 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2608 /* If either of the template parameters are invalid, assume
2609 they match for the sake of error recovery. */
2610 if (parm1 == error_mark_node || parm2 == error_mark_node)
2613 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2616 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2617 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2618 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
2620 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2625 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2626 /* One set of parameters has more parameters lists than the
2633 /* Determine whether PARM is a parameter pack. */
2636 template_parameter_pack_p (const_tree parm)
2638 /* Determine if we have a non-type template parameter pack. */
2639 if (TREE_CODE (parm) == PARM_DECL)
2640 return (DECL_TEMPLATE_PARM_P (parm)
2641 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2643 /* If this is a list of template parameters, we could get a
2644 TYPE_DECL or a TEMPLATE_DECL. */
2645 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2646 parm = TREE_TYPE (parm);
2648 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2649 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2650 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2653 /* Determine if T is a function parameter pack. */
2656 function_parameter_pack_p (const_tree t)
2658 if (t && TREE_CODE (t) == PARM_DECL)
2659 return FUNCTION_PARAMETER_PACK_P (t);
2663 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2664 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2667 get_function_template_decl (const_tree primary_func_tmpl_inst)
2669 if (! primary_func_tmpl_inst
2670 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
2671 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
2674 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
2677 /* Return true iff the function parameter PARAM_DECL was expanded
2678 from the function parameter pack PACK. */
2681 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
2683 if (DECL_ARTIFICIAL (param_decl)
2684 || !function_parameter_pack_p (pack))
2687 gcc_assert (DECL_NAME (param_decl) && DECL_NAME (pack));
2689 /* The parameter pack and its pack arguments have the same
2691 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
2694 /* Determine whether ARGS describes a variadic template args list,
2695 i.e., one that is terminated by a template argument pack. */
2698 template_args_variadic_p (tree args)
2703 if (args == NULL_TREE)
2706 args = INNERMOST_TEMPLATE_ARGS (args);
2707 nargs = TREE_VEC_LENGTH (args);
2712 last_parm = TREE_VEC_ELT (args, nargs - 1);
2714 return ARGUMENT_PACK_P (last_parm);
2717 /* Generate a new name for the parameter pack name NAME (an
2718 IDENTIFIER_NODE) that incorporates its */
2721 make_ith_pack_parameter_name (tree name, int i)
2723 /* Munge the name to include the parameter index. */
2724 #define NUMBUF_LEN 128
2725 char numbuf[NUMBUF_LEN];
2729 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2730 newname_len = IDENTIFIER_LENGTH (name)
2731 + strlen (numbuf) + 2;
2732 newname = (char*)alloca (newname_len);
2733 snprintf (newname, newname_len,
2734 "%s#%i", IDENTIFIER_POINTER (name), i);
2735 return get_identifier (newname);
2738 /* Return true if T is a primary function
2739 or class template instantiation. */
2742 primary_template_instantiation_p (const_tree t)
2747 if (TREE_CODE (t) == FUNCTION_DECL)
2748 return DECL_LANG_SPECIFIC (t)
2749 && DECL_TEMPLATE_INSTANTIATION (t)
2750 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2751 else if (CLASS_TYPE_P (t))
2752 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2753 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2757 /* Return true if PARM is a template template parameter. */
2760 template_template_parameter_p (const_tree parm)
2762 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2765 /* Return the template parameters of T if T is a
2766 primary template instantiation, NULL otherwise. */
2769 get_primary_template_innermost_parameters (const_tree t)
2771 tree parms = NULL, template_info = NULL;
2773 if ((template_info = get_template_info (t))
2774 && primary_template_instantiation_p (t))
2775 parms = INNERMOST_TEMPLATE_PARMS
2776 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2781 /* Returns the template arguments of T if T is a template instantiation,
2785 get_template_innermost_arguments (const_tree t)
2787 tree args = NULL, template_info = NULL;
2789 if ((template_info = get_template_info (t))
2790 && TI_ARGS (template_info))
2791 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2796 /* Return the argument pack elements of T if T is a template argument pack,
2800 get_template_argument_pack_elems (const_tree t)
2802 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2803 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2806 return ARGUMENT_PACK_ARGS (t);
2809 /* Structure used to track the progress of find_parameter_packs_r. */
2810 struct find_parameter_pack_data
2812 /* TREE_LIST that will contain all of the parameter packs found by
2814 tree* parameter_packs;
2816 /* Set of AST nodes that have been visited by the traversal. */
2817 struct pointer_set_t *visited;
2820 /* Identifies all of the argument packs that occur in a template
2821 argument and appends them to the TREE_LIST inside DATA, which is a
2822 find_parameter_pack_data structure. This is a subroutine of
2823 make_pack_expansion and uses_parameter_packs. */
2825 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2828 struct find_parameter_pack_data* ppd =
2829 (struct find_parameter_pack_data*)data;
2830 bool parameter_pack_p = false;
2832 /* Identify whether this is a parameter pack or not. */
2833 switch (TREE_CODE (t))
2835 case TEMPLATE_PARM_INDEX:
2836 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2837 parameter_pack_p = true;
2840 case TEMPLATE_TYPE_PARM:
2841 case TEMPLATE_TEMPLATE_PARM:
2842 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2843 parameter_pack_p = true;
2847 if (FUNCTION_PARAMETER_PACK_P (t))
2849 /* We don't want to walk into the type of a PARM_DECL,
2850 because we don't want to see the type parameter pack. */
2852 parameter_pack_p = true;
2857 /* Not a parameter pack. */
2861 if (parameter_pack_p)
2863 /* Add this parameter pack to the list. */
2864 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
2868 cp_walk_tree (&TYPE_CONTEXT (t),
2869 &find_parameter_packs_r, ppd, ppd->visited);
2871 /* This switch statement will return immediately if we don't find a
2873 switch (TREE_CODE (t))
2875 case TEMPLATE_PARM_INDEX:
2878 case BOUND_TEMPLATE_TEMPLATE_PARM:
2879 /* Check the template itself. */
2880 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
2881 &find_parameter_packs_r, ppd, ppd->visited);
2882 /* Check the template arguments. */
2883 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
2888 case TEMPLATE_TYPE_PARM:
2889 case TEMPLATE_TEMPLATE_PARM:
2896 if (TYPE_PTRMEMFUNC_P (t))
2902 if (TYPE_TEMPLATE_INFO (t))
2903 cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
2904 &find_parameter_packs_r, ppd, ppd->visited);
2910 cp_walk_tree (&TREE_TYPE (t),
2911 &find_parameter_packs_r, ppd, ppd->visited);
2915 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
2920 case TYPE_PACK_EXPANSION:
2921 case EXPR_PACK_EXPANSION:
2926 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
2931 case IDENTIFIER_NODE:
2932 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
2944 /* Determines if the expression or type T uses any parameter packs. */
2946 uses_parameter_packs (tree t)
2948 tree parameter_packs = NULL_TREE;
2949 struct find_parameter_pack_data ppd;
2950 ppd.parameter_packs = ¶meter_packs;
2951 ppd.visited = pointer_set_create ();
2952 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
2953 pointer_set_destroy (ppd.visited);
2954 return parameter_packs != NULL_TREE;
2957 /* Turn ARG, which may be an expression, type, or a TREE_LIST
2958 representation a base-class initializer into a parameter pack
2959 expansion. If all goes well, the resulting node will be an
2960 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2963 make_pack_expansion (tree arg)
2966 tree parameter_packs = NULL_TREE;
2967 bool for_types = false;
2968 struct find_parameter_pack_data ppd;
2970 if (!arg || arg == error_mark_node)
2973 if (TREE_CODE (arg) == TREE_LIST)
2975 /* The only time we will see a TREE_LIST here is for a base
2976 class initializer. In this case, the TREE_PURPOSE will be a
2977 _TYPE node (representing the base class expansion we're
2978 initializing) and the TREE_VALUE will be a TREE_LIST
2979 containing the initialization arguments.
2981 The resulting expansion looks somewhat different from most
2982 expansions. Rather than returning just one _EXPANSION, we
2983 return a TREE_LIST whose TREE_PURPOSE is a
2984 TYPE_PACK_EXPANSION containing the bases that will be
2985 initialized. The TREE_VALUE will be identical to the
2986 original TREE_VALUE, which is a list of arguments that will
2987 be passed to each base. We do not introduce any new pack
2988 expansion nodes into the TREE_VALUE (although it is possible
2989 that some already exist), because the TREE_PURPOSE and
2990 TREE_VALUE all need to be expanded together with the same
2991 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2992 resulting TREE_PURPOSE will mention the parameter packs in
2993 both the bases and the arguments to the bases. */
2996 tree parameter_packs = NULL_TREE;
2998 /* Determine which parameter packs will be used by the base
3000 ppd.visited = pointer_set_create ();
3001 ppd.parameter_packs = ¶meter_packs;
3002 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3005 if (parameter_packs == NULL_TREE)
3007 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3008 pointer_set_destroy (ppd.visited);
3009 return error_mark_node;
3012 if (TREE_VALUE (arg) != void_type_node)
3014 /* Collect the sets of parameter packs used in each of the
3015 initialization arguments. */
3016 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3018 /* Determine which parameter packs will be expanded in this
3020 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3025 pointer_set_destroy (ppd.visited);
3027 /* Create the pack expansion type for the base type. */
3028 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3029 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3030 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3032 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3033 they will rarely be compared to anything. */
3034 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3036 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3039 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3042 /* Build the PACK_EXPANSION_* node. */
3044 ? cxx_make_type (TYPE_PACK_EXPANSION)
3045 : make_node (EXPR_PACK_EXPANSION);
3046 SET_PACK_EXPANSION_PATTERN (result, arg);
3047 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3049 /* Propagate type and const-expression information. */
3050 TREE_TYPE (result) = TREE_TYPE (arg);
3051 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3054 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3055 they will rarely be compared to anything. */
3056 SET_TYPE_STRUCTURAL_EQUALITY (result);
3058 /* Determine which parameter packs will be expanded. */
3059 ppd.parameter_packs = ¶meter_packs;
3060 ppd.visited = pointer_set_create ();
3061 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3062 pointer_set_destroy (ppd.visited);
3064 /* Make sure we found some parameter packs. */
3065 if (parameter_packs == NULL_TREE)
3068 error ("expansion pattern %<%T%> contains no argument packs", arg);
3070 error ("expansion pattern %<%E%> contains no argument packs", arg);
3071 return error_mark_node;
3073 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3078 /* Checks T for any "bare" parameter packs, which have not yet been
3079 expanded, and issues an error if any are found. This operation can
3080 only be done on full expressions or types (e.g., an expression
3081 statement, "if" condition, etc.), because we could have expressions like:
3083 foo(f(g(h(args)))...)
3085 where "args" is a parameter pack. check_for_bare_parameter_packs
3086 should not be called for the subexpressions args, h(args),
3087 g(h(args)), or f(g(h(args))), because we would produce erroneous
3090 Returns TRUE and emits an error if there were bare parameter packs,
3091 returns FALSE otherwise. */
3093 check_for_bare_parameter_packs (tree t)
3095 tree parameter_packs = NULL_TREE;
3096 struct find_parameter_pack_data ppd;
3098 if (!processing_template_decl || !t || t == error_mark_node)
3101 if (TREE_CODE (t) == TYPE_DECL)
3104 ppd.parameter_packs = ¶meter_packs;
3105 ppd.visited = pointer_set_create ();
3106 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3107 pointer_set_destroy (ppd.visited);
3109 if (parameter_packs)
3111 error ("parameter packs not expanded with %<...%>:");
3112 while (parameter_packs)
3114 tree pack = TREE_VALUE (parameter_packs);
3115 tree name = NULL_TREE;
3117 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3118 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3119 name = TYPE_NAME (pack);
3120 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3121 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3123 name = DECL_NAME (pack);
3126 inform (input_location, " %qD", name);
3128 inform (input_location, " <anonymous>");
3130 parameter_packs = TREE_CHAIN (parameter_packs);
3139 /* Expand any parameter packs that occur in the template arguments in
3142 expand_template_argument_pack (tree args)
3144 tree result_args = NULL_TREE;
3145 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3146 int num_result_args = -1;
3148 /* First, determine if we need to expand anything, and the number of
3149 slots we'll need. */
3150 for (in_arg = 0; in_arg < nargs; ++in_arg)
3152 tree arg = TREE_VEC_ELT (args, in_arg);
3153 if (arg == NULL_TREE)
3155 if (ARGUMENT_PACK_P (arg))
3157 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3158 if (num_result_args < 0)
3159 num_result_args = in_arg + num_packed;
3161 num_result_args += num_packed;
3165 if (num_result_args >= 0)
3170 /* If no expansion is necessary, we're done. */
3171 if (num_result_args < 0)
3174 /* Expand arguments. */
3175 result_args = make_tree_vec (num_result_args);
3176 for (in_arg = 0; in_arg < nargs; ++in_arg)
3178 tree arg = TREE_VEC_ELT (args, in_arg);
3179 if (ARGUMENT_PACK_P (arg))
3181 tree packed = ARGUMENT_PACK_ARGS (arg);
3182 int i, num_packed = TREE_VEC_LENGTH (packed);
3183 for (i = 0; i < num_packed; ++i, ++out_arg)
3184 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3188 TREE_VEC_ELT (result_args, out_arg) = arg;
3196 /* Checks if DECL shadows a template parameter.
3198 [temp.local]: A template-parameter shall not be redeclared within its
3199 scope (including nested scopes).
3201 Emits an error and returns TRUE if the DECL shadows a parameter,
3202 returns FALSE otherwise. */
3205 check_template_shadow (tree decl)
3209 /* If we're not in a template, we can't possibly shadow a template
3211 if (!current_template_parms)
3214 /* Figure out what we're shadowing. */
3215 if (TREE_CODE (decl) == OVERLOAD)
3216 decl = OVL_CURRENT (decl);
3217 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3219 /* If there's no previous binding for this name, we're not shadowing
3220 anything, let alone a template parameter. */
3224 /* If we're not shadowing a template parameter, we're done. Note
3225 that OLDDECL might be an OVERLOAD (or perhaps even an
3226 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3228 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3231 /* We check for decl != olddecl to avoid bogus errors for using a
3232 name inside a class. We check TPFI to avoid duplicate errors for
3233 inline member templates. */
3235 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
3238 error ("declaration of %q+#D", decl);
3239 error (" shadows template parm %q+#D", olddecl);
3243 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3244 ORIG_LEVEL, DECL, and TYPE. */
3247 build_template_parm_index (int index,
3253 tree t = make_node (TEMPLATE_PARM_INDEX);
3254 TEMPLATE_PARM_IDX (t) = index;
3255 TEMPLATE_PARM_LEVEL (t) = level;
3256 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3257 TEMPLATE_PARM_DECL (t) = decl;
3258 TREE_TYPE (t) = type;
3259 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3260 TREE_READONLY (t) = TREE_READONLY (decl);
3265 /* Find the canonical type parameter for the given template type
3266 parameter. Returns the canonical type parameter, which may be TYPE
3267 if no such parameter existed. */
3269 canonical_type_parameter (tree type)
3272 int idx = TEMPLATE_TYPE_IDX (type);
3273 if (!canonical_template_parms)
3274 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3276 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3277 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3279 list = VEC_index (tree, canonical_template_parms, idx);
3280 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3281 list = TREE_CHAIN (list);
3284 return TREE_VALUE (list);
3287 VEC_replace(tree, canonical_template_parms, idx,
3288 tree_cons (NULL_TREE, type,
3289 VEC_index (tree, canonical_template_parms, idx)));
3294 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3295 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3296 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3297 new one is created. */
3300 reduce_template_parm_level (tree index, tree type, int levels, tree args,
3301 tsubst_flags_t complain)
3303 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3304 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
3305 != TEMPLATE_PARM_LEVEL (index) - levels))
3307 tree orig_decl = TEMPLATE_PARM_DECL (index);
3310 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3311 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3312 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3313 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3314 DECL_ARTIFICIAL (decl) = 1;
3315 SET_DECL_TEMPLATE_PARM_P (decl);
3317 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
3318 TEMPLATE_PARM_LEVEL (index) - levels,
3319 TEMPLATE_PARM_ORIG_LEVEL (index),
3321 TEMPLATE_PARM_DESCENDANTS (index) = t;
3322 TEMPLATE_PARM_PARAMETER_PACK (t)
3323 = TEMPLATE_PARM_PARAMETER_PACK (index);
3325 /* Template template parameters need this. */
3326 if (TREE_CODE (decl) == TEMPLATE_DECL)
3327 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3328 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3332 return TEMPLATE_PARM_DESCENDANTS (index);
3335 /* Process information from new template parameter PARM and append it to the
3336 LIST being built. This new parameter is a non-type parameter iff
3337 IS_NON_TYPE is true. This new parameter is a parameter
3338 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3342 process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
3343 bool is_parameter_pack)
3350 gcc_assert (TREE_CODE (parm) == TREE_LIST);
3351 defval = TREE_PURPOSE (parm);
3355 tree p = tree_last (list);
3357 if (p && TREE_VALUE (p) != error_mark_node)
3360 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3361 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3363 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3373 parm = TREE_VALUE (parm);
3375 SET_DECL_TEMPLATE_PARM_P (parm);
3377 if (TREE_TYPE (parm) == error_mark_node)
3379 err_parm_list = build_tree_list (defval, parm);
3380 TREE_VALUE (err_parm_list) = error_mark_node;
3381 return chainon (list, err_parm_list);
3387 The top-level cv-qualifiers on the template-parameter are
3388 ignored when determining its type. */
3389 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3390 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
3392 err_parm_list = build_tree_list (defval, parm);
3393 TREE_VALUE (err_parm_list) = error_mark_node;
3394 return chainon (list, err_parm_list);
3397 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3399 /* This template parameter is not a parameter pack, but it
3400 should be. Complain about "bare" parameter packs. */
3401 check_for_bare_parameter_packs (TREE_TYPE (parm));
3403 /* Recover by calling this a parameter pack. */
3404 is_parameter_pack = true;
3408 /* A template parameter is not modifiable. */
3409 TREE_CONSTANT (parm) = 1;
3410 TREE_READONLY (parm) = 1;
3411 decl = build_decl (parm_loc,
3412 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
3413 TREE_CONSTANT (decl) = 1;
3414 TREE_READONLY (decl) = 1;
3415 DECL_INITIAL (parm) = DECL_INITIAL (decl)
3416 = build_template_parm_index (idx, processing_template_decl,
3417 processing_template_decl,
3418 decl, TREE_TYPE (parm));
3420 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3421 = is_parameter_pack;
3426 parm = TREE_VALUE (TREE_VALUE (parm));
3428 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3430 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
3431 /* This is for distinguishing between real templates and template
3432 template parameters */
3433 TREE_TYPE (parm) = t;
3434 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3439 t = cxx_make_type (TEMPLATE_TYPE_PARM);
3440 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3441 decl = build_decl (parm_loc,
3442 TYPE_DECL, parm, t);
3445 TYPE_NAME (t) = decl;
3446 TYPE_STUB_DECL (t) = decl;
3448 TEMPLATE_TYPE_PARM_INDEX (t)
3449 = build_template_parm_index (idx, processing_template_decl,
3450 processing_template_decl,
3451 decl, TREE_TYPE (parm));
3452 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
3453 TYPE_CANONICAL (t) = canonical_type_parameter (t);
3455 DECL_ARTIFICIAL (decl) = 1;
3456 SET_DECL_TEMPLATE_PARM_P (decl);
3458 parm = build_tree_list (defval, parm);
3459 return chainon (list, parm);
3462 /* The end of a template parameter list has been reached. Process the
3463 tree list into a parameter vector, converting each parameter into a more
3464 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3468 end_template_parm_list (tree parms)
3472 tree saved_parmlist = make_tree_vec (list_length (parms));
3474 current_template_parms
3475 = tree_cons (size_int (processing_template_decl),
3476 saved_parmlist, current_template_parms);
3478 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3480 next = TREE_CHAIN (parm);
3481 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3482 TREE_CHAIN (parm) = NULL_TREE;
3485 --processing_template_parmlist;
3487 return saved_parmlist;
3490 /* end_template_decl is called after a template declaration is seen. */
3493 end_template_decl (void)
3495 reset_specialization ();
3497 if (! processing_template_decl)
3500 /* This matches the pushlevel in begin_template_parm_list. */
3503 --processing_template_decl;
3504 current_template_parms = TREE_CHAIN (current_template_parms);
3507 /* Within the declaration of a template, return all levels of template
3508 parameters that apply. The template parameters are represented as
3509 a TREE_VEC, in the form documented in cp-tree.h for template
3513 current_template_args (void)
3516 tree args = NULL_TREE;
3517 int length = TMPL_PARMS_DEPTH (current_template_parms);
3520 /* If there is only one level of template parameters, we do not
3521 create a TREE_VEC of TREE_VECs. Instead, we return a single
3522 TREE_VEC containing the arguments. */
3524 args = make_tree_vec (length);
3526 for (header = current_template_parms; header; header = TREE_CHAIN (header))
3528 tree a = copy_node (TREE_VALUE (header));
3531 TREE_TYPE (a) = NULL_TREE;
3532 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
3534 tree t = TREE_VEC_ELT (a, i);
3536 /* T will be a list if we are called from within a
3537 begin/end_template_parm_list pair, but a vector directly
3538 if within a begin/end_member_template_processing pair. */
3539 if (TREE_CODE (t) == TREE_LIST)
3543 if (!error_operand_p (t))
3545 if (TREE_CODE (t) == TYPE_DECL
3546 || TREE_CODE (t) == TEMPLATE_DECL)
3550 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3552 /* Turn this argument into a TYPE_ARGUMENT_PACK
3553 with a single element, which expands T. */
3554 tree vec = make_tree_vec (1);
3555 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3557 t = cxx_make_type (TYPE_ARGUMENT_PACK);
3558 SET_ARGUMENT_PACK_ARGS (t, vec);
3563 t = DECL_INITIAL (t);
3565 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3567 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3568 with a single element, which expands T. */
3569 tree vec = make_tree_vec (1);
3570 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3571 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3573 t = make_node (NONTYPE_ARGUMENT_PACK);
3574 SET_ARGUMENT_PACK_ARGS (t, vec);
3575 TREE_TYPE (t) = type;
3578 TREE_VEC_ELT (a, i) = t;
3584 TREE_VEC_ELT (args, --l) = a;
3592 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3593 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3594 a member template. Used by push_template_decl below. */
3597 build_template_decl (tree decl, tree parms, bool member_template_p)
3599 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3600 DECL_TEMPLATE_PARMS (tmpl) = parms;
3601 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
3602 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
3607 struct template_parm_data
3609 /* The level of the template parameters we are currently
3613 /* The index of the specialization argument we are currently
3617 /* An array whose size is the number of template parameters. The
3618 elements are nonzero if the parameter has been used in any one
3619 of the arguments processed so far. */
3622 /* An array whose size is the number of template arguments. The
3623 elements are nonzero if the argument makes use of template
3624 parameters of this level. */
3625 int* arg_uses_template_parms;
3628 /* Subroutine of push_template_decl used to see if each template
3629 parameter in a partial specialization is used in the explicit
3630 argument list. If T is of the LEVEL given in DATA (which is
3631 treated as a template_parm_data*), then DATA->PARMS is marked
3635 mark_template_parm (tree t, void* data)
3639 struct template_parm_data* tpd = (struct template_parm_data*) data;
3641 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3643 level = TEMPLATE_PARM_LEVEL (t);
3644 idx = TEMPLATE_PARM_IDX (t);
3648 level = TEMPLATE_TYPE_LEVEL (t);
3649 idx = TEMPLATE_TYPE_IDX (t);
3652 if (level == tpd->level)
3654 tpd->parms[idx] = 1;
3655 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3658 /* Return zero so that for_each_template_parm will continue the
3659 traversal of the tree; we want to mark *every* template parm. */
3663 /* Process the partial specialization DECL. */
3666 process_partial_specialization (tree decl)
3668 tree type = TREE_TYPE (decl);
3669 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3670 tree specargs = CLASSTYPE_TI_ARGS (type);
3671 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
3672 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
3674 int nargs = TREE_VEC_LENGTH (inner_args);
3677 int did_error_intro = 0;
3678 struct template_parm_data tpd;
3679 struct template_parm_data tpd2;
3681 gcc_assert (current_template_parms);
3683 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3684 ntparms = TREE_VEC_LENGTH (inner_parms);
3686 /* We check that each of the template parameters given in the
3687 partial specialization is used in the argument list to the
3688 specialization. For example:
3690 template <class T> struct S;
3691 template <class T> struct S<T*>;
3693 The second declaration is OK because `T*' uses the template
3694 parameter T, whereas
3696 template <class T> struct S<int>;
3698 is no good. Even trickier is:
3709 The S2<T> declaration is actually invalid; it is a
3710 full-specialization. Of course,
3713 struct S2<T (*)(U)>;
3715 or some such would have been OK. */
3716 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
3717 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
3718 memset (tpd.parms, 0, sizeof (int) * ntparms);
3720 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
3721 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
3722 for (i = 0; i < nargs; ++i)
3724 tpd.current_arg = i;
3725 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3726 &mark_template_parm,
3729 /*include_nondeduced_p=*/false);
3731 for (i = 0; i < ntparms; ++i)
3732 if (tpd.parms[i] == 0)
3734 /* One of the template parms was not used in the
3736 if (!did_error_intro)
3738 error ("template parameters not used in partial specialization:");
3739 did_error_intro = 1;
3742 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
3745 /* [temp.class.spec]
3747 The argument list of the specialization shall not be identical to
3748 the implicit argument list of the primary template. */
3749 if (comp_template_args
3751 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3753 error ("partial specialization %qT does not specialize any template arguments", type);
3755 /* [temp.class.spec]
3757 A partially specialized non-type argument expression shall not
3758 involve template parameters of the partial specialization except
3759 when the argument expression is a simple identifier.
3761 The type of a template parameter corresponding to a specialized
3762 non-type argument shall not be dependent on a parameter of the
3765 Also, we verify that pack expansions only occur at the
3766 end of the argument list. */
3767 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
3769 for (i = 0; i < nargs; ++i)
3771 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
3772 tree arg = TREE_VEC_ELT (inner_args, i);
3773 tree packed_args = NULL_TREE;
3776 if (ARGUMENT_PACK_P (arg))
3778 /* Extract the arguments from the argument pack. We'll be
3779 iterating over these in the following loop. */
3780 packed_args = ARGUMENT_PACK_ARGS (arg);
3781 len = TREE_VEC_LENGTH (packed_args);
3784 for (j = 0; j < len; j++)
3787 /* Get the Jth argument in the parameter pack. */
3788 arg = TREE_VEC_ELT (packed_args, j);
3790 if (PACK_EXPANSION_P (arg))
3792 /* Pack expansions must come at the end of the
3794 if ((packed_args && j < len - 1)
3795 || (!packed_args && i < nargs - 1))
3797 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3798 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3800 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3803 TREE_VEC_ELT (packed_args, j) = error_mark_node;
3807 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3808 /* We only care about the pattern. */
3809 arg = PACK_EXPANSION_PATTERN (arg);
3811 if (/* These first two lines are the `non-type' bit. */
3813 && TREE_CODE (arg) != TEMPLATE_DECL
3814 /* This next line is the `argument expression is not just a
3815 simple identifier' condition and also the `specialized
3816 non-type argument' bit. */
3817 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3819 if ((!packed_args && tpd.arg_uses_template_parms[i])
3820 || (packed_args && uses_template_parms (arg)))
3821 error ("template argument %qE involves template parameter(s)",
3825 /* Look at the corresponding template parameter,
3826 marking which template parameters its type depends
3828 tree type = TREE_TYPE (parm);
3832 /* We haven't yet initialized TPD2. Do so now. */
3833 tpd2.arg_uses_template_parms
3834 = (int *) alloca (sizeof (int) * nargs);
3835 /* The number of parameters here is the number in the
3836 main template, which, as checked in the assertion
3838 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3840 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3843 /* Mark the template parameters. But this time, we're
3844 looking for the template parameters of the main
3845 template, not in the specialization. */
3846 tpd2.current_arg = i;
3847 tpd2.arg_uses_template_parms[i] = 0;
3848 memset (tpd2.parms, 0, sizeof (int) * nargs);
3849 for_each_template_parm (type,
3850 &mark_template_parm,
3853 /*include_nondeduced_p=*/false);
3855 if (tpd2.arg_uses_template_parms [i])
3857 /* The type depended on some template parameters.
3858 If they are fully specialized in the
3859 specialization, that's OK. */
3861 for (j = 0; j < nargs; ++j)
3862 if (tpd2.parms[j] != 0
3863 && tpd.arg_uses_template_parms [j])
3865 error ("type %qT of template argument %qE depends "
3866 "on template parameter(s)",
3877 /* We should only get here once. */
3878 gcc_assert (!COMPLETE_TYPE_P (type));
3880 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
3881 = tree_cons (specargs, inner_parms,
3882 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
3883 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3887 /* Check that a template declaration's use of default arguments and
3888 parameter packs is not invalid. Here, PARMS are the template
3889 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3890 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3894 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3895 declaration (but not a definition); 1 indicates a declaration, 2
3896 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3897 emitted for extraneous default arguments.
3899 Returns TRUE if there were no errors found, FALSE otherwise. */
3902 check_default_tmpl_args (tree decl, tree parms, int is_primary,
3903 int is_partial, int is_friend_decl)
3906 int last_level_to_check;
3908 bool no_errors = true;
3912 A default template-argument shall not be specified in a
3913 function template declaration or a function template definition, nor
3914 in the template-parameter-list of the definition of a member of a
3917 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
3918 /* You can't have a function template declaration in a local
3919 scope, nor you can you define a member of a class template in a
3923 if (current_class_type
3924 && !TYPE_BEING_DEFINED (current_class_type)
3925 && DECL_LANG_SPECIFIC (decl)
3926 && DECL_DECLARES_FUNCTION_P (decl)
3927 /* If this is either a friend defined in the scope of the class
3928 or a member function. */
3929 && (DECL_FUNCTION_MEMBER_P (decl)
3930 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3931 : DECL_FRIEND_CONTEXT (decl)
3932 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3934 /* And, if it was a member function, it really was defined in
3935 the scope of the class. */
3936 && (!DECL_FUNCTION_MEMBER_P (decl)
3937 || DECL_INITIALIZED_IN_CLASS_P (decl)))
3938 /* We already checked these parameters when the template was
3939 declared, so there's no need to do it again now. This function
3940 was defined in class scope, but we're processing it's body now
3941 that the class is complete. */
3944 /* Core issue 226 (C++0x only): the following only applies to class
3946 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
3950 If a template-parameter has a default template-argument, all
3951 subsequent template-parameters shall have a default
3952 template-argument supplied. */
3953 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3955 tree inner_parms = TREE_VALUE (parm_level);
3956 int ntparms = TREE_VEC_LENGTH (inner_parms);
3957 int seen_def_arg_p = 0;
3960 for (i = 0; i < ntparms; ++i)
3962 tree parm = TREE_VEC_ELT (inner_parms, i);
3964 if (parm == error_mark_node)
3967 if (TREE_PURPOSE (parm))
3969 else if (seen_def_arg_p
3970 && !template_parameter_pack_p (TREE_VALUE (parm)))
3972 error ("no default argument for %qD", TREE_VALUE (parm));
3973 /* For better subsequent error-recovery, we indicate that
3974 there should have been a default argument. */
3975 TREE_PURPOSE (parm) = error_mark_node;
3981 /* Don't complain about an enclosing partial
3983 && parm_level == parms
3984 && TREE_CODE (decl) == TYPE_DECL
3986 && template_parameter_pack_p (TREE_VALUE (parm)))
3988 /* A primary class template can only have one
3989 parameter pack, at the end of the template
3992 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
3993 error ("parameter pack %qE must be at the end of the"
3994 " template parameter list", TREE_VALUE (parm));
3996 error ("parameter pack %qT must be at the end of the"
3997 " template parameter list",
3998 TREE_TYPE (TREE_VALUE (parm)));
4000 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4008 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
4012 /* For an ordinary class template, default template arguments are
4013 allowed at the innermost level, e.g.:
4014 template <class T = int>
4016 but, in a partial specialization, they're not allowed even
4017 there, as we have in [temp.class.spec]:
4019 The template parameter list of a specialization shall not
4020 contain default template argument values.
4022 So, for a partial specialization, or for a function template
4023 (in C++98/C++03), we look at all of them. */
4026 /* But, for a primary class template that is not a partial
4027 specialization we look at all template parameters except the
4029 parms = TREE_CHAIN (parms);
4031 /* Figure out what error message to issue. */
4032 if (is_friend_decl == 2)
4033 msg = "default template arguments may not be used in function template friend re-declaration";
4034 else if (is_friend_decl)
4035 msg = "default template arguments may not be used in function template friend declarations";
4036 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
4037 msg = ("default template arguments may not be used in function templates "
4038 "without -std=c++0x or -std=gnu++0x");
4039 else if (is_partial)
4040 msg = "default template arguments may not be used in partial specializations";
4042 msg = "default argument for template parameter for class enclosing %qD";
4044 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4045 /* If we're inside a class definition, there's no need to
4046 examine the parameters to the class itself. On the one
4047 hand, they will be checked when the class is defined, and,
4048 on the other, default arguments are valid in things like:
4049 template <class T = double>
4050 struct S { template <class U> void f(U); };
4051 Here the default argument for `S' has no bearing on the
4052 declaration of `f'. */
4053 last_level_to_check = template_class_depth (current_class_type) + 1;
4055 /* Check everything. */
4056 last_level_to_check = 0;
4058 for (parm_level = parms;
4059 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
4060 parm_level = TREE_CHAIN (parm_level))
4062 tree inner_parms = TREE_VALUE (parm_level);
4066 ntparms = TREE_VEC_LENGTH (inner_parms);
4067 for (i = 0; i < ntparms; ++i)
4069 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4072 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4077 if (is_friend_decl == 2)
4084 /* Clear out the default argument so that we are not
4086 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4090 /* At this point, if we're still interested in issuing messages,
4091 they must apply to classes surrounding the object declared. */
4093 msg = "default argument for template parameter for class enclosing %qD";
4099 /* Worker for push_template_decl_real, called via
4100 for_each_template_parm. DATA is really an int, indicating the
4101 level of the parameters we are interested in. If T is a template
4102 parameter of that level, return nonzero. */
4105 template_parm_this_level_p (tree t, void* data)
4107 int this_level = *(int *)data;
4110 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4111 level = TEMPLATE_PARM_LEVEL (t);
4113 level = TEMPLATE_TYPE_LEVEL (t);
4114 return level == this_level;
4117 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4118 parameters given by current_template_args, or reuses a
4119 previously existing one, if appropriate. Returns the DECL, or an
4120 equivalent one, if it is replaced via a call to duplicate_decls.
4122 If IS_FRIEND is true, DECL is a friend declaration. */
4125 push_template_decl_real (tree decl, bool is_friend)
4133 int new_template_p = 0;
4134 /* True if the template is a member template, in the sense of
4136 bool member_template_p = false;
4138 if (decl == error_mark_node || !current_template_parms)
4139 return error_mark_node;
4141 /* See if this is a partial specialization. */
4142 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
4143 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
4144 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
4146 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4150 /* For a friend, we want the context of the friend function, not
4151 the type of which it is a friend. */
4152 ctx = DECL_CONTEXT (decl);
4153 else if (CP_DECL_CONTEXT (decl)
4154 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4155 /* In the case of a virtual function, we want the class in which
4157 ctx = CP_DECL_CONTEXT (decl);
4159 /* Otherwise, if we're currently defining some class, the DECL
4160 is assumed to be a member of the class. */
4161 ctx = current_scope ();
4163 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4166 if (!DECL_CONTEXT (decl))
4167 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
4169 /* See if this is a primary template. */
4170 if (is_friend && ctx)
4171 /* A friend template that specifies a class context, i.e.
4172 template <typename T> friend void A<T>::f();
4176 primary = template_parm_scope_p ();
4180 if (DECL_CLASS_SCOPE_P (decl))
4181 member_template_p = true;
4182 if (TREE_CODE (decl) == TYPE_DECL
4183 && ANON_AGGRNAME_P (DECL_NAME (decl)))
4185 error ("template class without a name");
4186 return error_mark_node;
4188 else if (TREE_CODE (decl) == FUNCTION_DECL)
4190 if (DECL_DESTRUCTOR_P (decl))
4194 A destructor shall not be a member template. */
4195 error ("destructor %qD declared as member template", decl);
4196 return error_mark_node;
4198 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4199 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4200 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4201 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4202 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4203 == void_list_node)))
4205 /* [basic.stc.dynamic.allocation]
4207 An allocation function can be a function
4208 template. ... Template allocation functions shall
4209 have two or more parameters. */
4210 error ("invalid template declaration of %qD", decl);
4211 return error_mark_node;
4214 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4215 && CLASS_TYPE_P (TREE_TYPE (decl)))
4219 error ("template declaration of %q#D", decl);
4220 return error_mark_node;
4224 /* Check to see that the rules regarding the use of default
4225 arguments are not being violated. */
4226 check_default_tmpl_args (decl, current_template_parms,
4227 primary, is_partial, /*is_friend_decl=*/0);
4229 /* Ensure that there are no parameter packs in the type of this
4230 declaration that have not been expanded. */
4231 if (TREE_CODE (decl) == FUNCTION_DECL)
4233 /* Check each of the arguments individually to see if there are
4234 any bare parameter packs. */
4235 tree type = TREE_TYPE (decl);
4236 tree arg = DECL_ARGUMENTS (decl);
4237 tree argtype = TYPE_ARG_TYPES (type);
4239 while (arg && argtype)
4241 if (!FUNCTION_PARAMETER_PACK_P (arg)
4242 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
4244 /* This is a PARM_DECL that contains unexpanded parameter
4245 packs. We have already complained about this in the
4246 check_for_bare_parameter_packs call, so just replace
4247 these types with ERROR_MARK_NODE. */
4248 TREE_TYPE (arg) = error_mark_node;
4249 TREE_VALUE (argtype) = error_mark_node;
4252 arg = TREE_CHAIN (arg);
4253 argtype = TREE_CHAIN (argtype);
4256 /* Check for bare parameter packs in the return type and the
4257 exception specifiers. */
4258 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
4259 /* Errors were already issued, set return type to int
4260 as the frontend doesn't expect error_mark_node as
4262 TREE_TYPE (type) = integer_type_node;
4263 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4264 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
4266 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
4268 TREE_TYPE (decl) = error_mark_node;
4269 return error_mark_node;
4273 return process_partial_specialization (decl);
4275 args = current_template_args ();
4278 || TREE_CODE (ctx) == FUNCTION_DECL
4279 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
4280 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
4282 if (DECL_LANG_SPECIFIC (decl)
4283 && DECL_TEMPLATE_INFO (decl)
4284 && DECL_TI_TEMPLATE (decl))
4285 tmpl = DECL_TI_TEMPLATE (decl);
4286 /* If DECL is a TYPE_DECL for a class-template, then there won't
4287 be DECL_LANG_SPECIFIC. The information equivalent to
4288 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4289 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4290 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4291 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4293 /* Since a template declaration already existed for this
4294 class-type, we must be redeclaring it here. Make sure
4295 that the redeclaration is valid. */
4296 redeclare_class_template (TREE_TYPE (decl),
4297 current_template_parms);
4298 /* We don't need to create a new TEMPLATE_DECL; just use the
4299 one we already had. */
4300 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4304 tmpl = build_template_decl (decl, current_template_parms,
4308 if (DECL_LANG_SPECIFIC (decl)
4309 && DECL_TEMPLATE_SPECIALIZATION (decl))
4311 /* A specialization of a member template of a template
4313 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4314 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4315 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4321 tree a, t, current, parms;
4323 tree tinfo = get_template_info (decl);
4327 error ("template definition of non-template %q#D", decl);
4328 return error_mark_node;
4331 tmpl = TI_TEMPLATE (tinfo);
4333 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
4334 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
4335 && DECL_TEMPLATE_SPECIALIZATION (decl)
4336 && DECL_MEMBER_TEMPLATE_P (tmpl))
4340 /* The declaration is a specialization of a member
4341 template, declared outside the class. Therefore, the
4342 innermost template arguments will be NULL, so we
4343 replace them with the arguments determined by the
4344 earlier call to check_explicit_specialization. */
4345 args = DECL_TI_ARGS (decl);
4348 = build_template_decl (decl, current_template_parms,
4350 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4351 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4352 DECL_TI_TEMPLATE (decl) = new_tmpl;
4353 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
4354 DECL_TEMPLATE_INFO (new_tmpl)
4355 = tree_cons (tmpl, args, NULL_TREE);
4357 register_specialization (new_tmpl,
4358 most_general_template (tmpl),
4364 /* Make sure the template headers we got make sense. */
4366 parms = DECL_TEMPLATE_PARMS (tmpl);
4367 i = TMPL_PARMS_DEPTH (parms);
4368 if (TMPL_ARGS_DEPTH (args) != i)
4370 error ("expected %d levels of template parms for %q#D, got %d",
4371 i, decl, TMPL_ARGS_DEPTH (args));
4374 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4376 a = TMPL_ARGS_LEVEL (args, i);
4377 t = INNERMOST_TEMPLATE_PARMS (parms);
4379 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4381 if (current == decl)
4382 error ("got %d template parameters for %q#D",
4383 TREE_VEC_LENGTH (a), decl);
4385 error ("got %d template parameters for %q#T",
4386 TREE_VEC_LENGTH (a), current);
4387 error (" but %d required", TREE_VEC_LENGTH (t));
4388 return error_mark_node;
4391 if (current == decl)
4394 current = (TYPE_P (current)
4395 ? TYPE_CONTEXT (current)
4396 : DECL_CONTEXT (current));
4399 /* Check that the parms are used in the appropriate qualifying scopes
4400 in the declarator. */
4401 if (!comp_template_args
4403 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4406 template arguments to %qD do not match original template %qD",
4407 decl, DECL_TEMPLATE_RESULT (tmpl));
4408 if (!uses_template_parms (TI_ARGS (tinfo)))
4409 inform (input_location, "use template<> for an explicit specialization");
4410 /* Avoid crash in import_export_decl. */
4411 DECL_INTERFACE_KNOWN (decl) = 1;
4412 return error_mark_node;
4416 DECL_TEMPLATE_RESULT (tmpl) = decl;
4417 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4419 /* Push template declarations for global functions and types. Note
4420 that we do not try to push a global template friend declared in a
4421 template class; such a thing may well depend on the template
4422 parameters of the class. */
4423 if (new_template_p && !ctx
4424 && !(is_friend && template_class_depth (current_class_type) > 0))
4426 tmpl = pushdecl_namespace_level (tmpl, is_friend);
4427 if (tmpl == error_mark_node)
4428 return error_mark_node;
4430 /* Hide template friend classes that haven't been declared yet. */
4431 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4433 DECL_ANTICIPATED (tmpl) = 1;
4434 DECL_FRIEND_P (tmpl) = 1;
4440 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4443 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4444 if (DECL_CONV_FN_P (tmpl))
4446 int depth = TMPL_PARMS_DEPTH (parms);
4448 /* It is a conversion operator. See if the type converted to
4449 depends on innermost template operands. */
4451 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4453 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4456 /* Give template template parms a DECL_CONTEXT of the template
4457 for which they are a parameter. */
4458 parms = INNERMOST_TEMPLATE_PARMS (parms);
4459 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4461 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4462 if (TREE_CODE (parm) == TEMPLATE_DECL)
4463 DECL_CONTEXT (parm) = tmpl;
4467 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4468 back to its most general template. If TMPL is a specialization,
4469 ARGS may only have the innermost set of arguments. Add the missing
4470 argument levels if necessary. */
4471 if (DECL_TEMPLATE_INFO (tmpl))
4472 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4474 info = tree_cons (tmpl, args, NULL_TREE);
4476 if (DECL_IMPLICIT_TYPEDEF_P (decl))
4477 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
4478 else if (DECL_LANG_SPECIFIC (decl))
4479 DECL_TEMPLATE_INFO (decl) = info;
4481 return DECL_TEMPLATE_RESULT (tmpl);
4485 push_template_decl (tree decl)
4487 return push_template_decl_real (decl, false);
4490 /* Called when a class template TYPE is redeclared with the indicated
4491 template PARMS, e.g.:
4493 template <class T> struct S;
4494 template <class T> struct S {}; */
4497 redeclare_class_template (tree type, tree parms)
4503 if (!TYPE_TEMPLATE_INFO (type))
4505 error ("%qT is not a template type", type);
4509 tmpl = TYPE_TI_TEMPLATE (type);
4510 if (!PRIMARY_TEMPLATE_P (tmpl))
4511 /* The type is nested in some template class. Nothing to worry
4512 about here; there are no new template parameters for the nested
4518 error ("template specifiers not specified in declaration of %qD",
4523 parms = INNERMOST_TEMPLATE_PARMS (parms);
4524 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4526 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4528 error ("redeclared with %d template parameter(s)",
4529 TREE_VEC_LENGTH (parms));
4530 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
4531 tmpl, TREE_VEC_LENGTH (tmpl_parms));
4535 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4542 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4543 || TREE_VEC_ELT (parms, i) == error_mark_node)
4546 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4547 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4548 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4549 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
4551 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4553 if (tmpl_parm != error_mark_node
4554 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4555 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4556 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4557 || (TREE_CODE (tmpl_parm) != PARM_DECL
4558 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4559 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4560 || (TREE_CODE (tmpl_parm) == PARM_DECL
4561 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4562 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
4564 error ("template parameter %q+#D", tmpl_parm);
4565 error ("redeclared here as %q#D", parm);
4569 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4571 /* We have in [temp.param]:
4573 A template-parameter may not be given default arguments
4574 by two different declarations in the same scope. */
4575 error_at (input_location, "redefinition of default argument for %q#D", parm);
4576 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4577 "original definition appeared here");
4581 if (parm_default != NULL_TREE)
4582 /* Update the previous template parameters (which are the ones
4583 that will really count) with the new default value. */
4584 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
4585 else if (tmpl_default != NULL_TREE)
4586 /* Update the new parameters, too; they'll be used as the
4587 parameters for any members. */
4588 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
4594 /* Simplify EXPR if it is a non-dependent expression. Returns the
4595 (possibly simplified) expression. */
4598 fold_non_dependent_expr (tree expr)
4600 if (expr == NULL_TREE)
4603 /* If we're in a template, but EXPR isn't value dependent, simplify
4604 it. We're supposed to treat:
4606 template <typename T> void f(T[1 + 1]);
4607 template <typename T> void f(T[2]);
4609 as two declarations of the same function, for example. */
4610 if (processing_template_decl
4611 && !type_dependent_expression_p (expr)
4612 && !value_dependent_expression_p (expr))
4614 HOST_WIDE_INT saved_processing_template_decl;
4616 saved_processing_template_decl = processing_template_decl;
4617 processing_template_decl = 0;
4618 expr = tsubst_copy_and_build (expr,
4621 /*in_decl=*/NULL_TREE,
4622 /*function_p=*/false,
4623 /*integral_constant_expression_p=*/true);
4624 processing_template_decl = saved_processing_template_decl;
4629 /* EXPR is an expression which is used in a constant-expression context.
4630 For instance, it could be a VAR_DECL with a constant initializer.
4631 Extract the innermost constant expression.
4633 This is basically a more powerful version of
4634 integral_constant_value, which can be used also in templates where
4635 initializers can maintain a syntactic rather than semantic form
4636 (even if they are non-dependent, for access-checking purposes). */
4639 fold_decl_constant_value (tree expr)
4641 tree const_expr = expr;
4644 expr = fold_non_dependent_expr (const_expr);
4645 const_expr = integral_constant_value (expr);
4647 while (expr != const_expr);
4652 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4653 must be a function or a pointer-to-function type, as specified
4654 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4655 and check that the resulting function has external linkage. */
4658 convert_nontype_argument_function (tree type, tree expr)
4663 fn = instantiate_type (type, fns, tf_none);
4664 if (fn == error_mark_node)
4665 return error_mark_node;
4668 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4669 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
4670 if (TREE_CODE (fn_no_ptr) == BASELINK)
4671 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4673 /* [temp.arg.nontype]/1
4675 A template-argument for a non-type, non-template template-parameter
4678 -- the address of an object or function with external linkage. */
4679 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4681 error ("%qE is not a valid template argument for type %qT "
4682 "because function %qD has not external linkage",
4683 expr, type, fn_no_ptr);
4690 /* Subroutine of convert_nontype_argument.
4691 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4692 Emit an error otherwise. */
4695 check_valid_ptrmem_cst_expr (tree type, tree expr)
4698 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
4700 error ("%qE is not a valid template argument for type %qT",
4702 error ("it must be a pointer-to-member of the form `&X::Y'");
4706 /* Attempt to convert the non-type template parameter EXPR to the
4707 indicated TYPE. If the conversion is successful, return the
4708 converted value. If the conversion is unsuccessful, return
4709 NULL_TREE if we issued an error message, or error_mark_node if we
4710 did not. We issue error messages for out-and-out bad template
4711 parameters, but not simply because the conversion failed, since we
4712 might be just trying to do argument deduction. Both TYPE and EXPR
4713 must be non-dependent.
4715 The conversion follows the special rules described in
4716 [temp.arg.nontype], and it is much more strict than an implicit
4719 This function is called twice for each template argument (see
4720 lookup_template_class for a more accurate description of this
4721 problem). This means that we need to handle expressions which
4722 are not valid in a C++ source, but can be created from the
4723 first call (for instance, casts to perform conversions). These
4724 hacks can go away after we fix the double coercion problem. */
4727 convert_nontype_argument (tree type, tree expr)
4731 /* Detect immediately string literals as invalid non-type argument.
4732 This special-case is not needed for correctness (we would easily
4733 catch this later), but only to provide better diagnostic for this
4734 common user mistake. As suggested by DR 100, we do not mention
4735 linkage issues in the diagnostic as this is not the point. */
4736 if (TREE_CODE (expr) == STRING_CST)
4738 error ("%qE is not a valid template argument for type %qT "
4739 "because string literals can never be used in this context",
4744 /* If we are in a template, EXPR may be non-dependent, but still
4745 have a syntactic, rather than semantic, form. For example, EXPR
4746 might be a SCOPE_REF, rather than the VAR_DECL to which the
4747 SCOPE_REF refers. Preserving the qualifying scope is necessary
4748 so that access checking can be performed when the template is
4749 instantiated -- but here we need the resolved form so that we can
4750 convert the argument. */
4751 expr = fold_non_dependent_expr (expr);
4752 if (error_operand_p (expr))
4753 return error_mark_node;
4754 expr_type = TREE_TYPE (expr);
4756 /* HACK: Due to double coercion, we can get a
4757 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4758 which is the tree that we built on the first call (see
4759 below when coercing to reference to object or to reference to
4760 function). We just strip everything and get to the arg.
4761 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4763 if (TREE_CODE (expr) == NOP_EXPR)
4765 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
4767 /* ??? Maybe we could use convert_from_reference here, but we
4768 would need to relax its constraints because the NOP_EXPR
4769 could actually change the type to something more cv-qualified,
4770 and this is not folded by convert_from_reference. */
4771 tree addr = TREE_OPERAND (expr, 0);
4772 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4773 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4774 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4775 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4776 (TREE_TYPE (expr_type),
4777 TREE_TYPE (TREE_TYPE (addr))));
4779 expr = TREE_OPERAND (addr, 0);
4780 expr_type = TREE_TYPE (expr);
4783 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4784 parameter is a pointer to object, through decay and
4785 qualification conversion. Let's strip everything. */
4786 else if (TYPE_PTROBV_P (type))
4789 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4790 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4791 /* Skip the ADDR_EXPR only if it is part of the decay for
4792 an array. Otherwise, it is part of the original argument
4793 in the source code. */
4794 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4795 expr = TREE_OPERAND (expr, 0);
4796 expr_type = TREE_TYPE (expr);
4800 /* [temp.arg.nontype]/5, bullet 1
4802 For a non-type template-parameter of integral or enumeration type,
4803 integral promotions (_conv.prom_) and integral conversions
4804 (_conv.integral_) are applied. */
4805 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4807 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
4808 return error_mark_node;
4810 expr = fold_decl_constant_value (expr);
4811 /* Notice that there are constant expressions like '4 % 0' which
4812 do not fold into integer constants. */
4813 if (TREE_CODE (expr) != INTEGER_CST)
4815 error ("%qE is not a valid template argument for type %qT "
4816 "because it is a non-constant expression", expr, type);
4820 /* At this point, an implicit conversion does what we want,
4821 because we already know that the expression is of integral
4823 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4824 if (expr == error_mark_node)
4825 return error_mark_node;
4827 /* Conversion was allowed: fold it to a bare integer constant. */
4830 /* [temp.arg.nontype]/5, bullet 2
4832 For a non-type template-parameter of type pointer to object,
4833 qualification conversions (_conv.qual_) and the array-to-pointer
4834 conversion (_conv.array_) are applied. */
4835 else if (TYPE_PTROBV_P (type))
4837 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
4839 A template-argument for a non-type, non-template template-parameter
4840 shall be one of: [...]
4842 -- the name of a non-type template-parameter;
4843 -- the address of an object or function with external linkage, [...]
4844 expressed as "& id-expression" where the & is optional if the name
4845 refers to a function or array, or if the corresponding
4846 template-parameter is a reference.
4848 Here, we do not care about functions, as they are invalid anyway
4849 for a parameter of type pointer-to-object. */
4851 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4852 /* Non-type template parameters are OK. */
4854 else if (TREE_CODE (expr) != ADDR_EXPR
4855 && TREE_CODE (expr_type) != ARRAY_TYPE)
4857 if (TREE_CODE (expr) == VAR_DECL)
4859 error ("%qD is not a valid template argument "
4860 "because %qD is a variable, not the address of "
4865 /* Other values, like integer constants, might be valid
4866 non-type arguments of some other type. */
4867 return error_mark_node;
4873 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4874 ? TREE_OPERAND (expr, 0) : expr);
4875 if (TREE_CODE (decl) != VAR_DECL)
4877 error ("%qE is not a valid template argument of type %qT "
4878 "because %qE is not a variable",
4882 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4884 error ("%qE is not a valid template argument of type %qT "
4885 "because %qD does not have external linkage",
4891 expr = decay_conversion (expr);
4892 if (expr == error_mark_node)
4893 return error_mark_node;
4895 expr = perform_qualification_conversions (type, expr);
4896 if (expr == error_mark_node)
4897 return error_mark_node;
4899 /* [temp.arg.nontype]/5, bullet 3
4901 For a non-type template-parameter of type reference to object, no
4902 conversions apply. The type referred to by the reference may be more
4903 cv-qualified than the (otherwise identical) type of the
4904 template-argument. The template-parameter is bound directly to the
4905 template-argument, which must be an lvalue. */
4906 else if (TYPE_REF_OBJ_P (type))
4908 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4910 return error_mark_node;
4912 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4914 error ("%qE is not a valid template argument for type %qT "
4915 "because of conflicts in cv-qualification", expr, type);
4919 if (!real_lvalue_p (expr))
4921 error ("%qE is not a valid template argument for type %qT "
4922 "because it is not an lvalue", expr, type);
4926 /* [temp.arg.nontype]/1
4928 A template-argument for a non-type, non-template template-parameter
4929 shall be one of: [...]
4931 -- the address of an object or function with external linkage. */
4932 if (TREE_CODE (expr) == INDIRECT_REF
4933 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
4935 expr = TREE_OPERAND (expr, 0);
4938 error ("%q#D is not a valid template argument for type %qT "
4939 "because a reference variable does not have a constant "
4940 "address", expr, type);
4947 error ("%qE is not a valid template argument for type %qT "
4948 "because it is not an object with external linkage",
4953 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4955 error ("%qE is not a valid template argument for type %qT "
4956 "because object %qD has not external linkage",
4961 expr = build_nop (type, build_address (expr));
4963 /* [temp.arg.nontype]/5, bullet 4
4965 For a non-type template-parameter of type pointer to function, only
4966 the function-to-pointer conversion (_conv.func_) is applied. If the
4967 template-argument represents a set of overloaded functions (or a
4968 pointer to such), the matching function is selected from the set
4970 else if (TYPE_PTRFN_P (type))
4972 /* If the argument is a template-id, we might not have enough
4973 context information to decay the pointer. */
4974 if (!type_unknown_p (expr_type))
4976 expr = decay_conversion (expr);
4977 if (expr == error_mark_node)
4978 return error_mark_node;
4981 expr = convert_nontype_argument_function (type, expr);
4982 if (!expr || expr == error_mark_node)
4985 if (TREE_CODE (expr) != ADDR_EXPR)
4987 error ("%qE is not a valid template argument for type %qT", expr, type);
4988 error ("it must be the address of a function with external linkage");
4992 /* [temp.arg.nontype]/5, bullet 5
4994 For a non-type template-parameter of type reference to function, no
4995 conversions apply. If the template-argument represents a set of
4996 overloaded functions, the matching function is selected from the set
4998 else if (TYPE_REFFN_P (type))
5000 if (TREE_CODE (expr) == ADDR_EXPR)
5002 error ("%qE is not a valid template argument for type %qT "
5003 "because it is a pointer", expr, type);
5004 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
5008 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
5009 if (!expr || expr == error_mark_node)
5012 expr = build_nop (type, build_address (expr));
5014 /* [temp.arg.nontype]/5, bullet 6
5016 For a non-type template-parameter of type pointer to member function,
5017 no conversions apply. If the template-argument represents a set of
5018 overloaded member functions, the matching member function is selected
5019 from the set (_over.over_). */
5020 else if (TYPE_PTRMEMFUNC_P (type))
5022 expr = instantiate_type (type, expr, tf_none);
5023 if (expr == error_mark_node)
5024 return error_mark_node;
5026 /* [temp.arg.nontype] bullet 1 says the pointer to member
5027 expression must be a pointer-to-member constant. */
5028 if (!check_valid_ptrmem_cst_expr (type, expr))
5029 return error_mark_node;
5031 /* There is no way to disable standard conversions in
5032 resolve_address_of_overloaded_function (called by
5033 instantiate_type). It is possible that the call succeeded by
5034 converting &B::I to &D::I (where B is a base of D), so we need
5035 to reject this conversion here.
5037 Actually, even if there was a way to disable standard conversions,
5038 it would still be better to reject them here so that we can
5039 provide a superior diagnostic. */
5040 if (!same_type_p (TREE_TYPE (expr), type))
5042 /* Make sure we are just one standard conversion off. */
5043 gcc_assert (can_convert (type, TREE_TYPE (expr)));
5044 error ("%qE is not a valid template argument for type %qT "
5045 "because it is of type %qT", expr, type,
5047 inform (input_location, "standard conversions are not allowed in this context");
5051 /* [temp.arg.nontype]/5, bullet 7
5053 For a non-type template-parameter of type pointer to data member,
5054 qualification conversions (_conv.qual_) are applied. */
5055 else if (TYPE_PTRMEM_P (type))
5057 /* [temp.arg.nontype] bullet 1 says the pointer to member
5058 expression must be a pointer-to-member constant. */
5059 if (!check_valid_ptrmem_cst_expr (type, expr))
5060 return error_mark_node;
5062 expr = perform_qualification_conversions (type, expr);
5063 if (expr == error_mark_node)
5066 /* A template non-type parameter must be one of the above. */
5070 /* Sanity check: did we actually convert the argument to the
5072 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
5076 /* Subroutine of coerce_template_template_parms, which returns 1 if
5077 PARM_PARM and ARG_PARM match using the rule for the template
5078 parameters of template template parameters. Both PARM and ARG are
5079 template parameters; the rest of the arguments are the same as for
5080 coerce_template_template_parms.
5083 coerce_template_template_parm (tree parm,
5085 tsubst_flags_t complain,
5089 if (arg == NULL_TREE || arg == error_mark_node
5090 || parm == NULL_TREE || parm == error_mark_node)
5093 if (TREE_CODE (arg) != TREE_CODE (parm))
5096 switch (TREE_CODE (parm))
5099 /* We encounter instantiations of templates like
5100 template <template <template <class> class> class TT>
5103 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5104 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5106 if (!coerce_template_template_parms
5107 (parmparm, argparm, complain, in_decl, outer_args))
5113 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5114 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5115 /* Argument is a parameter pack but parameter is not. */
5120 /* The tsubst call is used to handle cases such as
5122 template <int> class C {};
5123 template <class T, template <T> class TT> class D {};
5126 i.e. the parameter list of TT depends on earlier parameters. */
5127 if (!uses_template_parms (TREE_TYPE (arg))
5129 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5133 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5134 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5135 /* Argument is a parameter pack but parameter is not. */
5148 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5149 template template parameters. Both PARM_PARMS and ARG_PARMS are
5150 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5153 Consider the example:
5154 template <class T> class A;
5155 template<template <class U> class TT> class B;
5157 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5158 the parameters to A, and OUTER_ARGS contains A. */
5161 coerce_template_template_parms (tree parm_parms,
5163 tsubst_flags_t complain,
5167 int nparms, nargs, i;
5171 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5172 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
5174 nparms = TREE_VEC_LENGTH (parm_parms);
5175 nargs = TREE_VEC_LENGTH (arg_parms);
5177 /* Determine whether we have a parameter pack at the end of the
5178 template template parameter's template parameter list. */
5179 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5181 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5183 if (parm == error_mark_node)
5186 switch (TREE_CODE (parm))
5190 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5195 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5205 && !(variadic_p && nargs >= nparms - 1))
5208 /* Check all of the template parameters except the parameter pack at
5209 the end (if any). */
5210 for (i = 0; i < nparms - variadic_p; ++i)
5212 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5213 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5216 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5217 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5219 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5227 /* Check each of the template parameters in the template
5228 argument against the template parameter pack at the end of
5229 the template template parameter. */
5230 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5233 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5235 for (; i < nargs; ++i)
5237 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5240 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5242 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5251 /* Verifies that the deduced template arguments (in TARGS) for the
5252 template template parameters (in TPARMS) represent valid bindings,
5253 by comparing the template parameter list of each template argument
5254 to the template parameter list of its corresponding template
5255 template parameter, in accordance with DR150. This
5256 routine can only be called after all template arguments have been
5257 deduced. It will return TRUE if all of the template template
5258 parameter bindings are okay, FALSE otherwise. */
5260 template_template_parm_bindings_ok_p (tree tparms, tree targs)
5262 int i, ntparms = TREE_VEC_LENGTH (tparms);
5265 /* We're dealing with template parms in this process. */
5266 ++processing_template_decl;
5268 targs = INNERMOST_TEMPLATE_ARGS (targs);
5270 for (i = 0; i < ntparms; ++i)
5272 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5273 tree targ = TREE_VEC_ELT (targs, i);
5275 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5277 tree packed_args = NULL_TREE;
5280 if (ARGUMENT_PACK_P (targ))
5282 /* Look inside the argument pack. */
5283 packed_args = ARGUMENT_PACK_ARGS (targ);
5284 len = TREE_VEC_LENGTH (packed_args);
5287 for (idx = 0; idx < len; ++idx)
5289 tree targ_parms = NULL_TREE;
5292 /* Extract the next argument from the argument
5294 targ = TREE_VEC_ELT (packed_args, idx);
5296 if (PACK_EXPANSION_P (targ))
5297 /* Look at the pattern of the pack expansion. */
5298 targ = PACK_EXPANSION_PATTERN (targ);
5300 /* Extract the template parameters from the template
5302 if (TREE_CODE (targ) == TEMPLATE_DECL)
5303 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5304 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5305 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5307 /* Verify that we can coerce the template template
5308 parameters from the template argument to the template
5309 parameter. This requires an exact match. */
5311 && !coerce_template_template_parms
5312 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5327 --processing_template_decl;
5331 /* Convert the indicated template ARG as necessary to match the
5332 indicated template PARM. Returns the converted ARG, or
5333 error_mark_node if the conversion was unsuccessful. Error and
5334 warning messages are issued under control of COMPLAIN. This
5335 conversion is for the Ith parameter in the parameter list. ARGS is
5336 the full set of template arguments deduced so far. */
5339 convert_template_argument (tree parm,
5342 tsubst_flags_t complain,
5348 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
5350 if (TREE_CODE (arg) == TREE_LIST
5351 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
5353 /* The template argument was the name of some
5354 member function. That's usually
5355 invalid, but static members are OK. In any
5356 case, grab the underlying fields/functions
5357 and issue an error later if required. */
5358 orig_arg = TREE_VALUE (arg);
5359 TREE_TYPE (arg) = unknown_type_node;
5364 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5365 requires_type = (TREE_CODE (parm) == TYPE_DECL
5366 || requires_tmpl_type);
5368 /* When determining whether an argument pack expansion is a template,
5369 look at the pattern. */
5370 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5371 arg = PACK_EXPANSION_PATTERN (arg);
5374 ((TREE_CODE (arg) == TEMPLATE_DECL
5375 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5376 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5377 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
5380 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5381 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
5382 arg = TYPE_STUB_DECL (arg);
5384 is_type = TYPE_P (arg) || is_tmpl_type;
5386 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5387 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5389 permerror (input_location, "to refer to a type member of a template parameter, "
5390 "use %<typename %E%>", orig_arg);
5392 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5393 TREE_OPERAND (arg, 1),
5395 complain & tf_error);
5399 if (is_type != requires_type)
5403 if (complain & tf_error)
5405 error ("type/value mismatch at argument %d in template "
5406 "parameter list for %qD",
5409 error (" expected a constant of type %qT, got %qT",
5411 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
5412 else if (requires_tmpl_type)
5413 error (" expected a class template, got %qE", orig_arg);
5415 error (" expected a type, got %qE", orig_arg);
5418 return error_mark_node;
5420 if (is_tmpl_type ^ requires_tmpl_type)
5422 if (in_decl && (complain & tf_error))
5424 error ("type/value mismatch at argument %d in template "
5425 "parameter list for %qD",
5428 error (" expected a type, got %qT", DECL_NAME (arg));
5430 error (" expected a class template, got %qT", orig_arg);
5432 return error_mark_node;
5437 if (requires_tmpl_type)
5439 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5440 /* The number of argument required is not known yet.
5441 Just accept it for now. */
5442 val = TREE_TYPE (arg);
5445 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5448 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
5450 if (coerce_template_template_parms (parmparm, argparm,
5456 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5458 if (val != error_mark_node)
5460 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5461 val = TREE_TYPE (val);
5462 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
5463 && DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
5465 val = TREE_TYPE (arg);
5466 val = make_pack_expansion (val);
5472 if (in_decl && (complain & tf_error))
5474 error ("type/value mismatch at argument %d in "
5475 "template parameter list for %qD",
5477 error (" expected a template of type %qD, got %qT",
5481 val = error_mark_node;
5487 /* We only form one instance of each template specialization.
5488 Therefore, if we use a non-canonical variant (i.e., a
5489 typedef), any future messages referring to the type will use
5490 the typedef, which is confusing if those future uses do not
5491 themselves also use the typedef. */
5493 val = strip_typedefs (val);
5497 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
5499 if (invalid_nontype_parm_type_p (t, complain))
5500 return error_mark_node;
5502 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5504 if (same_type_p (t, TREE_TYPE (orig_arg)))
5508 /* Not sure if this is reachable, but it doesn't hurt
5510 error ("type mismatch in nontype parameter pack");
5511 val = error_mark_node;
5514 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
5515 /* We used to call digest_init here. However, digest_init
5516 will report errors, which we don't want when complain
5517 is zero. More importantly, digest_init will try too
5518 hard to convert things: for example, `0' should not be
5519 converted to pointer type at this point according to
5520 the standard. Accepting this is not merely an
5521 extension, since deciding whether or not these
5522 conversions can occur is part of determining which
5523 function template to call, or whether a given explicit
5524 argument specification is valid. */
5525 val = convert_nontype_argument (t, orig_arg);
5529 if (val == NULL_TREE)
5530 val = error_mark_node;
5531 else if (val == error_mark_node && (complain & tf_error))
5532 error ("could not convert template argument %qE to %qT", orig_arg, t);
5538 /* Coerces the remaining template arguments in INNER_ARGS (from
5539 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5540 Returns the coerced argument pack. PARM_IDX is the position of this
5541 parameter in the template parameter list. ARGS is the original
5542 template argument list. */
5544 coerce_template_parameter_pack (tree parms,
5552 tsubst_flags_t complain)
5554 tree parm = TREE_VEC_ELT (parms, parm_idx);
5555 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5558 tree packed_types = NULL_TREE;
5560 if (arg_idx > nargs)
5563 packed_args = make_tree_vec (nargs - arg_idx);
5565 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5566 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5568 /* When the template parameter is a non-type template
5569 parameter pack whose type uses parameter packs, we need
5570 to look at each of the template arguments
5571 separately. Build a vector of the types for these
5572 non-type template parameters in PACKED_TYPES. */
5574 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5575 packed_types = tsubst_pack_expansion (expansion, args,
5578 if (packed_types == error_mark_node)
5579 return error_mark_node;
5581 /* Check that we have the right number of arguments. */
5583 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5584 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5587 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5588 error ("wrong number of template arguments (%d, should be %d)",
5589 nargs, needed_parms);
5590 return error_mark_node;
5593 /* If we aren't able to check the actual arguments now
5594 (because they haven't been expanded yet), we can at least
5595 verify that all of the types used for the non-type
5596 template parameter pack are, in fact, valid for non-type
5597 template parameters. */
5599 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5601 int j, len = TREE_VEC_LENGTH (packed_types);
5602 for (j = 0; j < len; ++j)
5604 tree t = TREE_VEC_ELT (packed_types, j);
5605 if (invalid_nontype_parm_type_p (t, complain))
5606 return error_mark_node;
5611 /* Convert the remaining arguments, which will be a part of the
5612 parameter pack "parm". */
5613 for (; arg_idx < nargs; ++arg_idx)
5615 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5616 tree actual_parm = TREE_VALUE (parm);
5618 if (packed_types && !PACK_EXPANSION_P (arg))
5620 /* When we have a vector of types (corresponding to the
5621 non-type template parameter pack that uses parameter
5622 packs in its type, as mention above), and the
5623 argument is not an expansion (which expands to a
5624 currently unknown number of arguments), clone the
5625 parm and give it the next type in PACKED_TYPES. */
5626 actual_parm = copy_node (actual_parm);
5627 TREE_TYPE (actual_parm) =
5628 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5631 if (arg != error_mark_node)
5632 arg = convert_template_argument (actual_parm,
5633 arg, new_args, complain, parm_idx,
5635 if (arg == error_mark_node)
5637 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5640 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5641 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5642 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
5645 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
5646 TREE_TYPE (argument_pack)
5647 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
5648 TREE_CONSTANT (argument_pack) = 1;
5651 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5652 return argument_pack;
5655 /* Convert all template arguments to their appropriate types, and
5656 return a vector containing the innermost resulting template
5657 arguments. If any error occurs, return error_mark_node. Error and
5658 warning messages are issued under control of COMPLAIN.
5660 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5661 for arguments not specified in ARGS. Otherwise, if
5662 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5663 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5664 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5668 coerce_template_parms (tree parms,
5671 tsubst_flags_t complain,
5672 bool require_all_args,
5673 bool use_default_args)
5675 int nparms, nargs, parm_idx, arg_idx, lost = 0;
5678 tree new_inner_args;
5679 int saved_unevaluated_operand;
5680 int saved_inhibit_evaluation_warnings;
5682 /* When used as a boolean value, indicates whether this is a
5683 variadic template parameter list. Since it's an int, we can also
5684 subtract it from nparms to get the number of non-variadic
5688 nparms = TREE_VEC_LENGTH (parms);
5690 /* Determine if there are any parameter packs. */
5691 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5693 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5694 if (template_parameter_pack_p (tparm))
5698 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5699 /* If there are 0 or 1 parameter packs, we need to expand any argument
5700 packs so that we can deduce a parameter pack from some non-packed args
5701 followed by an argument pack, as in variadic85.C. If there are more
5702 than that, we need to leave argument packs intact so the arguments are
5703 assigned to the right parameter packs. This should only happen when
5704 dealing with a nested class inside a partial specialization of a class
5705 template, as in variadic92.C. */
5706 if (variadic_p <= 1)
5707 inner_args = expand_template_argument_pack (inner_args);
5709 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5710 if ((nargs > nparms && !variadic_p)
5711 || (nargs < nparms - variadic_p
5713 && (!use_default_args
5714 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5715 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
5717 if (complain & tf_error)
5719 const char *or_more = "";
5722 or_more = " or more";
5726 error ("wrong number of template arguments (%d, should be %d%s)",
5727 nargs, nparms, or_more);
5730 error ("provided for %q+D", in_decl);
5733 return error_mark_node;
5736 /* We need to evaluate the template arguments, even though this
5737 template-id may be nested within a "sizeof". */
5738 saved_unevaluated_operand = cp_unevaluated_operand;
5739 cp_unevaluated_operand = 0;
5740 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5741 c_inhibit_evaluation_warnings = 0;
5742 new_inner_args = make_tree_vec (nparms);
5743 new_args = add_outermost_template_args (args, new_inner_args);
5744 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
5749 /* Get the Ith template parameter. */
5750 parm = TREE_VEC_ELT (parms, parm_idx);
5752 if (parm == error_mark_node)
5754 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
5758 /* Calculate the next argument. */
5759 if (arg_idx < nargs)
5760 arg = TREE_VEC_ELT (inner_args, arg_idx);
5764 if (template_parameter_pack_p (TREE_VALUE (parm))
5765 && !(arg && ARGUMENT_PACK_P (arg)))
5767 /* All remaining arguments will be placed in the
5768 template parameter pack PARM. */
5769 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5770 inner_args, arg_idx,
5774 /* Store this argument. */
5775 if (arg == error_mark_node)
5777 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5779 /* We are done with all of the arguments. */
5786 if (PACK_EXPANSION_P (arg))
5788 if (complain & tf_error)
5790 /* FIXME this restriction was removed by N2555; see
5792 /* If ARG is a pack expansion, but PARM is not a
5793 template parameter pack (if it were, we would have
5794 handled it above), we're trying to expand into a
5795 fixed-length argument list. */
5796 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5797 sorry ("cannot expand %<%E%> into a fixed-length "
5798 "argument list", arg);
5800 sorry ("cannot expand %<%T%> into a fixed-length "
5801 "argument list", arg);
5803 return error_mark_node;
5806 else if (require_all_args)
5807 /* There must be a default arg in this case. */
5808 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5813 if (arg == error_mark_node)
5815 if (complain & tf_error)
5816 error ("template argument %d is invalid", arg_idx + 1);
5819 /* This only occurs if there was an error in the template
5820 parameter list itself (which we would already have
5821 reported) that we are trying to recover from, e.g., a class
5822 template with a parameter list such as
5823 template<typename..., typename>. */
5824 return error_mark_node;
5826 arg = convert_template_argument (TREE_VALUE (parm),
5827 arg, new_args, complain,
5830 if (arg == error_mark_node)
5832 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
5834 cp_unevaluated_operand = saved_unevaluated_operand;
5835 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
5838 return error_mark_node;
5840 return new_inner_args;
5843 /* Returns 1 if template args OT and NT are equivalent. */
5846 template_args_equal (tree ot, tree nt)
5851 if (TREE_CODE (nt) == TREE_VEC)
5852 /* For member templates */
5853 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5854 else if (PACK_EXPANSION_P (ot))
5855 return PACK_EXPANSION_P (nt)
5856 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5857 PACK_EXPANSION_PATTERN (nt));
5858 else if (ARGUMENT_PACK_P (ot))
5863 if (!ARGUMENT_PACK_P (nt))
5866 opack = ARGUMENT_PACK_ARGS (ot);
5867 npack = ARGUMENT_PACK_ARGS (nt);
5868 len = TREE_VEC_LENGTH (opack);
5869 if (TREE_VEC_LENGTH (npack) != len)
5871 for (i = 0; i < len; ++i)
5872 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5873 TREE_VEC_ELT (npack, i)))
5877 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
5879 /* We get here probably because we are in the middle of substituting
5880 into the pattern of a pack expansion. In that case the
5881 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
5882 interested in. So we want to use the initial pack argument for
5884 ot = ARGUMENT_PACK_SELECT_FROM_PACK (ot);
5885 if (nt && TREE_CODE (nt) == ARGUMENT_PACK_SELECT)
5886 nt = ARGUMENT_PACK_SELECT_FROM_PACK (nt);
5887 return template_args_equal (ot, nt);
5889 else if (TYPE_P (nt))
5890 return TYPE_P (ot) && same_type_p (ot, nt);
5891 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5894 return cp_tree_equal (ot, nt);
5897 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
5898 of template arguments. Returns 0 otherwise. */
5901 comp_template_args (tree oldargs, tree newargs)
5905 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5908 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5910 tree nt = TREE_VEC_ELT (newargs, i);
5911 tree ot = TREE_VEC_ELT (oldargs, i);
5913 if (! template_args_equal (ot, nt))
5920 add_pending_template (tree d)
5922 tree ti = (TYPE_P (d)
5923 ? CLASSTYPE_TEMPLATE_INFO (d)
5924 : DECL_TEMPLATE_INFO (d));
5925 struct pending_template *pt;
5928 if (TI_PENDING_TEMPLATE_FLAG (ti))
5931 /* We are called both from instantiate_decl, where we've already had a
5932 tinst_level pushed, and instantiate_template, where we haven't.
5934 level = !current_tinst_level || current_tinst_level->decl != d;
5937 push_tinst_level (d);
5939 pt = GGC_NEW (struct pending_template);
5941 pt->tinst = current_tinst_level;
5942 if (last_pending_template)
5943 last_pending_template->next = pt;
5945 pending_templates = pt;
5947 last_pending_template = pt;
5949 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5956 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5957 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5958 documentation for TEMPLATE_ID_EXPR. */
5961 lookup_template_function (tree fns, tree arglist)
5965 if (fns == error_mark_node || arglist == error_mark_node)
5966 return error_mark_node;
5968 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
5969 gcc_assert (fns && (is_overloaded_fn (fns)
5970 || TREE_CODE (fns) == IDENTIFIER_NODE));
5972 if (BASELINK_P (fns))
5974 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5976 BASELINK_FUNCTIONS (fns),
5981 type = TREE_TYPE (fns);
5982 if (TREE_CODE (fns) == OVERLOAD || !type)
5983 type = unknown_type_node;
5985 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
5988 /* Within the scope of a template class S<T>, the name S gets bound
5989 (in build_self_reference) to a TYPE_DECL for the class, not a
5990 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5991 or one of its enclosing classes, and that type is a template,
5992 return the associated TEMPLATE_DECL. Otherwise, the original
5993 DECL is returned. */
5996 maybe_get_template_decl_from_type_decl (tree decl)
5998 return (decl != NULL_TREE
5999 && TREE_CODE (decl) == TYPE_DECL
6000 && DECL_ARTIFICIAL (decl)
6001 && CLASS_TYPE_P (TREE_TYPE (decl))
6002 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
6003 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
6006 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6007 parameters, find the desired type.
6009 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6011 IN_DECL, if non-NULL, is the template declaration we are trying to
6014 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6015 the class we are looking up.
6017 Issue error and warning messages under control of COMPLAIN.
6019 If the template class is really a local class in a template
6020 function, then the FUNCTION_CONTEXT is the function in which it is
6023 ??? Note that this function is currently called *twice* for each
6024 template-id: the first time from the parser, while creating the
6025 incomplete type (finish_template_type), and the second type during the
6026 real instantiation (instantiate_template_class). This is surely something
6027 that we want to avoid. It also causes some problems with argument
6028 coercion (see convert_nontype_argument for more information on this). */
6031 lookup_template_class (tree d1,
6036 tsubst_flags_t complain)
6038 tree templ = NULL_TREE, parmlist;
6045 timevar_push (TV_NAME_LOOKUP);
6047 if (TREE_CODE (d1) == IDENTIFIER_NODE)
6049 tree value = innermost_non_namespace_value (d1);
6050 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
6055 push_decl_namespace (context);
6056 templ = lookup_name (d1);
6057 templ = maybe_get_template_decl_from_type_decl (templ);
6059 pop_decl_namespace ();
6062 context = DECL_CONTEXT (templ);
6064 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
6066 tree type = TREE_TYPE (d1);
6068 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6069 an implicit typename for the second A. Deal with it. */
6070 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
6071 type = TREE_TYPE (type);
6073 if (CLASSTYPE_TEMPLATE_INFO (type))
6075 templ = CLASSTYPE_TI_TEMPLATE (type);
6076 d1 = DECL_NAME (templ);
6079 else if (TREE_CODE (d1) == ENUMERAL_TYPE
6080 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
6082 templ = TYPE_TI_TEMPLATE (d1);
6083 d1 = DECL_NAME (templ);
6085 else if (TREE_CODE (d1) == TEMPLATE_DECL
6086 && DECL_TEMPLATE_RESULT (d1)
6087 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
6090 d1 = DECL_NAME (templ);
6091 context = DECL_CONTEXT (templ);
6094 /* Issue an error message if we didn't find a template. */
6097 if (complain & tf_error)
6098 error ("%qT is not a template", d1);
6099 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6102 if (TREE_CODE (templ) != TEMPLATE_DECL
6103 /* Make sure it's a user visible template, if it was named by
6105 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
6106 && !PRIMARY_TEMPLATE_P (templ)))
6108 if (complain & tf_error)
6110 error ("non-template type %qT used as a template", d1);
6112 error ("for template declaration %q+D", in_decl);
6114 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6117 complain &= ~tf_user;
6119 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
6121 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6122 template arguments */
6128 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6130 /* Consider an example where a template template parameter declared as
6132 template <class T, class U = std::allocator<T> > class TT
6134 The template parameter level of T and U are one level larger than
6135 of TT. To proper process the default argument of U, say when an
6136 instantiation `TT<int>' is seen, we need to build the full
6137 arguments containing {int} as the innermost level. Outer levels,
6138 available when not appearing as default template argument, can be
6139 obtained from the arguments of the enclosing template.
6141 Suppose that TT is later substituted with std::vector. The above
6142 instantiation is `TT<int, std::allocator<T> >' with TT at
6143 level 1, and T at level 2, while the template arguments at level 1
6144 becomes {std::vector} and the inner level 2 is {int}. */
6146 outer = DECL_CONTEXT (templ);
6148 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6149 else if (current_template_parms)
6150 /* This is an argument of the current template, so we haven't set
6151 DECL_CONTEXT yet. */
6152 outer = current_template_args ();
6155 arglist = add_to_template_args (outer, arglist);
6157 arglist2 = coerce_template_parms (parmlist, arglist, templ,
6159 /*require_all_args=*/true,
6160 /*use_default_args=*/true);
6161 if (arglist2 == error_mark_node
6162 || (!uses_template_parms (arglist2)
6163 && check_instantiated_args (templ, arglist2, complain)))
6164 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6166 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
6167 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
6171 tree template_type = TREE_TYPE (templ);
6174 tree found = NULL_TREE;
6177 int is_partial_instantiation;
6179 gen_tmpl = most_general_template (templ);
6180 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
6181 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6182 arg_depth = TMPL_ARGS_DEPTH (arglist);
6184 if (arg_depth == 1 && parm_depth > 1)
6186 /* We've been given an incomplete set of template arguments.
6189 template <class T> struct S1 {
6190 template <class U> struct S2 {};
6191 template <class U> struct S2<U*> {};
6194 we will be called with an ARGLIST of `U*', but the
6195 TEMPLATE will be `template <class T> template
6196 <class U> struct S1<T>::S2'. We must fill in the missing
6199 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
6201 arg_depth = TMPL_ARGS_DEPTH (arglist);
6204 /* Now we should have enough arguments. */
6205 gcc_assert (parm_depth == arg_depth);
6207 /* From here on, we're only interested in the most general
6210 /* Calculate the BOUND_ARGS. These will be the args that are
6211 actually tsubst'd into the definition to create the
6215 /* We have multiple levels of arguments to coerce, at once. */
6217 int saved_depth = TMPL_ARGS_DEPTH (arglist);
6219 tree bound_args = make_tree_vec (parm_depth);
6221 for (i = saved_depth,
6222 t = DECL_TEMPLATE_PARMS (gen_tmpl);
6223 i > 0 && t != NULL_TREE;
6224 --i, t = TREE_CHAIN (t))
6226 tree a = coerce_template_parms (TREE_VALUE (t),
6229 /*require_all_args=*/true,
6230 /*use_default_args=*/true);
6232 /* Don't process further if one of the levels fails. */
6233 if (a == error_mark_node)
6235 /* Restore the ARGLIST to its full size. */
6236 TREE_VEC_LENGTH (arglist) = saved_depth;
6237 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6240 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6242 /* We temporarily reduce the length of the ARGLIST so
6243 that coerce_template_parms will see only the arguments
6244 corresponding to the template parameters it is
6246 TREE_VEC_LENGTH (arglist)--;
6249 /* Restore the ARGLIST to its full size. */
6250 TREE_VEC_LENGTH (arglist) = saved_depth;
6252 arglist = bound_args;
6256 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
6257 INNERMOST_TEMPLATE_ARGS (arglist),
6260 /*require_all_args=*/true,
6261 /*use_default_args=*/true);
6263 if (arglist == error_mark_node)
6264 /* We were unable to bind the arguments. */
6265 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6267 /* In the scope of a template class, explicit references to the
6268 template class refer to the type of the template, not any
6269 instantiation of it. For example, in:
6271 template <class T> class C { void f(C<T>); }
6273 the `C<T>' is just the same as `C'. Outside of the
6274 class, however, such a reference is an instantiation. */
6276 || !PRIMARY_TEMPLATE_P (gen_tmpl)
6277 || currently_open_class (template_type))
6278 /* comp_template_args is expensive, check it last. */
6279 && comp_template_args (TYPE_TI_ARGS (template_type),
6281 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
6283 /* If we already have this specialization, return it. */
6284 elt.tmpl = gen_tmpl;
6286 hash = hash_specialization (&elt);
6287 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6291 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
6293 /* This type is a "partial instantiation" if any of the template
6294 arguments still involve template parameters. Note that we set
6295 IS_PARTIAL_INSTANTIATION for partial specializations as
6297 is_partial_instantiation = uses_template_parms (arglist);
6299 /* If the deduced arguments are invalid, then the binding
6301 if (!is_partial_instantiation
6302 && check_instantiated_args (gen_tmpl,
6303 INNERMOST_TEMPLATE_ARGS (arglist),
6305 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6307 if (!is_partial_instantiation
6308 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6309 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
6310 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
6312 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6313 DECL_NAME (gen_tmpl),
6314 /*tag_scope=*/ts_global);
6315 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
6318 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
6321 context = global_namespace;
6323 /* Create the type. */
6324 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6326 if (!is_partial_instantiation)
6328 set_current_access_from_decl (TYPE_NAME (template_type));
6329 t = start_enum (TYPE_IDENTIFIER (template_type),
6330 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6331 arglist, complain, in_decl),
6332 SCOPED_ENUM_P (template_type));
6336 /* We don't want to call start_enum for this type, since
6337 the values for the enumeration constants may involve
6338 template parameters. And, no one should be interested
6339 in the enumeration constants for such a type. */
6340 t = cxx_make_type (ENUMERAL_TYPE);
6341 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6346 t = make_class_type (TREE_CODE (template_type));
6347 CLASSTYPE_DECLARED_CLASS (t)
6348 = CLASSTYPE_DECLARED_CLASS (template_type);
6349 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
6350 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
6352 /* A local class. Make sure the decl gets registered properly. */
6353 if (context == current_function_decl)
6354 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
6356 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6357 /* This instantiation is another name for the primary
6358 template type. Set the TYPE_CANONICAL field
6360 TYPE_CANONICAL (t) = template_type;
6361 else if (any_template_arguments_need_structural_equality_p (arglist))
6362 /* Some of the template arguments require structural
6363 equality testing, so this template class requires
6364 structural equality testing. */
6365 SET_TYPE_STRUCTURAL_EQUALITY (t);
6368 /* If we called start_enum or pushtag above, this information
6369 will already be set up. */
6372 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6374 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
6375 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
6376 DECL_SOURCE_LOCATION (type_decl)
6377 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
6380 type_decl = TYPE_NAME (t);
6382 TREE_PRIVATE (type_decl)
6383 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6384 TREE_PROTECTED (type_decl)
6385 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
6386 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6388 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6389 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6392 /* Set up the template information. We have to figure out which
6393 template is the immediate parent if this is a full
6395 if (parm_depth == 1 || is_partial_instantiation
6396 || !PRIMARY_TEMPLATE_P (gen_tmpl))
6397 /* This case is easy; there are no member templates involved. */
6401 /* This is a full instantiation of a member template. Find
6402 the partial instantiation of which this is an instance. */
6404 /* Temporarily reduce by one the number of levels in the ARGLIST
6405 so as to avoid comparing the last set of arguments. */
6406 TREE_VEC_LENGTH (arglist)--;
6407 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6408 TREE_VEC_LENGTH (arglist)++;
6409 found = CLASSTYPE_TI_TEMPLATE (found);
6412 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
6415 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6416 &elt, hash, INSERT);
6417 *slot = GGC_NEW (spec_entry);
6420 /* Note this use of the partial instantiation so we can check it
6421 later in maybe_process_partial_specialization. */
6422 DECL_TEMPLATE_INSTANTIATIONS (templ)
6423 = tree_cons (arglist, t,
6424 DECL_TEMPLATE_INSTANTIATIONS (templ));
6426 if (TREE_CODE (t) == ENUMERAL_TYPE
6427 && !is_partial_instantiation)
6428 /* Now that the type has been registered on the instantiations
6429 list, we set up the enumerators. Because the enumeration
6430 constants may involve the enumeration type itself, we make
6431 sure to register the type first, and then create the
6432 constants. That way, doing tsubst_expr for the enumeration
6433 constants won't result in recursive calls here; we'll find
6434 the instantiation and exit above. */
6435 tsubst_enum (template_type, t, arglist);
6437 if (is_partial_instantiation)
6438 /* If the type makes use of template parameters, the
6439 code that generates debugging information will crash. */
6440 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
6442 /* Possibly limit visibility based on template args. */
6443 TREE_PUBLIC (type_decl) = 1;
6444 determine_visibility (type_decl);
6446 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6448 timevar_pop (TV_NAME_LOOKUP);
6455 /* True when we should also visit template parameters that occur in
6456 non-deduced contexts. */
6457 bool include_nondeduced_p;
6458 struct pointer_set_t *visited;
6461 /* Called from for_each_template_parm via walk_tree. */
6464 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
6467 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6468 tree_fn_t fn = pfd->fn;
6469 void *data = pfd->data;
6472 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6473 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6474 pfd->include_nondeduced_p))
6475 return error_mark_node;
6477 switch (TREE_CODE (t))
6480 if (TYPE_PTRMEMFUNC_P (t))
6486 if (!TYPE_TEMPLATE_INFO (t))
6488 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
6489 fn, data, pfd->visited,
6490 pfd->include_nondeduced_p))
6491 return error_mark_node;
6495 if (for_each_template_parm (TYPE_MIN_VALUE (t),
6496 fn, data, pfd->visited,
6497 pfd->include_nondeduced_p)
6498 || for_each_template_parm (TYPE_MAX_VALUE (t),
6499 fn, data, pfd->visited,
6500 pfd->include_nondeduced_p))
6501 return error_mark_node;
6505 /* Since we're not going to walk subtrees, we have to do this
6507 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
6508 pfd->visited, pfd->include_nondeduced_p))
6509 return error_mark_node;
6513 /* Check the return type. */
6514 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6515 pfd->include_nondeduced_p))
6516 return error_mark_node;
6518 /* Check the parameter types. Since default arguments are not
6519 instantiated until they are needed, the TYPE_ARG_TYPES may
6520 contain expressions that involve template parameters. But,
6521 no-one should be looking at them yet. And, once they're
6522 instantiated, they don't contain template parameters, so
6523 there's no point in looking at them then, either. */
6527 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
6528 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
6529 pfd->visited, pfd->include_nondeduced_p))
6530 return error_mark_node;
6532 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6533 want walk_tree walking into them itself. */
6539 if (pfd->include_nondeduced_p
6540 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6542 pfd->include_nondeduced_p))
6543 return error_mark_node;
6548 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
6549 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
6550 pfd->visited, pfd->include_nondeduced_p))
6551 return error_mark_node;
6556 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6557 && for_each_template_parm (DECL_INITIAL (t), fn, data,
6558 pfd->visited, pfd->include_nondeduced_p))
6559 return error_mark_node;
6560 if (DECL_CONTEXT (t)
6561 && pfd->include_nondeduced_p
6562 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
6563 pfd->visited, pfd->include_nondeduced_p))
6564 return error_mark_node;
6567 case BOUND_TEMPLATE_TEMPLATE_PARM:
6568 /* Record template parameters such as `T' inside `TT<T>'. */
6569 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6570 pfd->include_nondeduced_p))
6571 return error_mark_node;
6574 case TEMPLATE_TEMPLATE_PARM:
6575 case TEMPLATE_TYPE_PARM:
6576 case TEMPLATE_PARM_INDEX:
6577 if (fn && (*fn)(t, data))
6578 return error_mark_node;
6580 return error_mark_node;
6584 /* A template template parameter is encountered. */
6585 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
6586 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6587 pfd->include_nondeduced_p))
6588 return error_mark_node;
6590 /* Already substituted template template parameter */
6596 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
6598 pfd->include_nondeduced_p))
6599 return error_mark_node;
6603 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
6604 && pfd->include_nondeduced_p
6605 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
6606 (TREE_TYPE (t)), fn, data,
6607 pfd->visited, pfd->include_nondeduced_p))
6608 return error_mark_node;
6613 /* If there's no type, then this thing must be some expression
6614 involving template parameters. */
6615 if (!fn && !TREE_TYPE (t))
6616 return error_mark_node;
6621 case REINTERPRET_CAST_EXPR:
6622 case CONST_CAST_EXPR:
6623 case STATIC_CAST_EXPR:
6624 case DYNAMIC_CAST_EXPR:
6628 case PSEUDO_DTOR_EXPR:
6630 return error_mark_node;
6637 /* We didn't find any template parameters we liked. */
6641 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6642 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
6643 call FN with the parameter and the DATA.
6644 If FN returns nonzero, the iteration is terminated, and
6645 for_each_template_parm returns 1. Otherwise, the iteration
6646 continues. If FN never returns a nonzero value, the value
6647 returned by for_each_template_parm is 0. If FN is NULL, it is
6648 considered to be the function which always returns 1.
6650 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6651 parameters that occur in non-deduced contexts. When false, only
6652 visits those template parameters that can be deduced. */
6655 for_each_template_parm (tree t, tree_fn_t fn, void* data,
6656 struct pointer_set_t *visited,
6657 bool include_nondeduced_p)
6659 struct pair_fn_data pfd;
6665 pfd.include_nondeduced_p = include_nondeduced_p;
6667 /* Walk the tree. (Conceptually, we would like to walk without
6668 duplicates, but for_each_template_parm_r recursively calls
6669 for_each_template_parm, so we would need to reorganize a fair
6670 bit to use walk_tree_without_duplicates, so we keep our own
6673 pfd.visited = visited;
6675 pfd.visited = pointer_set_create ();
6676 result = cp_walk_tree (&t,
6677 for_each_template_parm_r,
6679 pfd.visited) != NULL_TREE;
6684 pointer_set_destroy (pfd.visited);
6691 /* Returns true if T depends on any template parameter. */
6694 uses_template_parms (tree t)
6697 int saved_processing_template_decl;
6699 saved_processing_template_decl = processing_template_decl;
6700 if (!saved_processing_template_decl)
6701 processing_template_decl = 1;
6703 dependent_p = dependent_type_p (t);
6704 else if (TREE_CODE (t) == TREE_VEC)
6705 dependent_p = any_dependent_template_arguments_p (t);
6706 else if (TREE_CODE (t) == TREE_LIST)
6707 dependent_p = (uses_template_parms (TREE_VALUE (t))
6708 || uses_template_parms (TREE_CHAIN (t)));
6709 else if (TREE_CODE (t) == TYPE_DECL)
6710 dependent_p = dependent_type_p (TREE_TYPE (t));
6713 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6714 || TREE_CODE (t) == OVERLOAD
6715 || TREE_CODE (t) == BASELINK
6716 || TREE_CODE (t) == IDENTIFIER_NODE
6717 || TREE_CODE (t) == TRAIT_EXPR
6718 || TREE_CODE (t) == CONSTRUCTOR
6719 || CONSTANT_CLASS_P (t))
6720 dependent_p = (type_dependent_expression_p (t)
6721 || value_dependent_expression_p (t));
6724 gcc_assert (t == error_mark_node);
6725 dependent_p = false;
6728 processing_template_decl = saved_processing_template_decl;
6733 /* Returns true if T depends on any template parameter with level LEVEL. */
6736 uses_template_parms_level (tree t, int level)
6738 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6739 /*include_nondeduced_p=*/true);
6742 static int tinst_depth;
6743 extern int max_tinst_depth;
6744 #ifdef GATHER_STATISTICS
6747 static int tinst_level_tick;
6748 static int last_template_error_tick;
6750 /* We're starting to instantiate D; record the template instantiation context
6751 for diagnostics and to restore it later. */
6754 push_tinst_level (tree d)
6756 struct tinst_level *new_level;
6758 if (tinst_depth >= max_tinst_depth)
6760 /* If the instantiation in question still has unbound template parms,
6761 we don't really care if we can't instantiate it, so just return.
6762 This happens with base instantiation for implicit `typename'. */
6763 if (uses_template_parms (d))
6766 last_template_error_tick = tinst_level_tick;
6767 error ("template instantiation depth exceeds maximum of %d (use "
6768 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
6769 max_tinst_depth, d);
6771 print_instantiation_context ();
6776 new_level = GGC_NEW (struct tinst_level);
6777 new_level->decl = d;
6778 new_level->locus = input_location;
6779 new_level->in_system_header_p = in_system_header;
6780 new_level->next = current_tinst_level;
6781 current_tinst_level = new_level;
6784 #ifdef GATHER_STATISTICS
6785 if (tinst_depth > depth_reached)
6786 depth_reached = tinst_depth;
6793 /* We're done instantiating this template; return to the instantiation
6797 pop_tinst_level (void)
6799 /* Restore the filename and line number stashed away when we started
6800 this instantiation. */
6801 input_location = current_tinst_level->locus;
6802 current_tinst_level = current_tinst_level->next;
6807 /* We're instantiating a deferred template; restore the template
6808 instantiation context in which the instantiation was requested, which
6809 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
6812 reopen_tinst_level (struct tinst_level *level)
6814 struct tinst_level *t;
6817 for (t = level; t; t = t->next)
6820 current_tinst_level = level;
6825 /* Returns the TINST_LEVEL which gives the original instantiation
6828 struct tinst_level *
6829 outermost_tinst_level (void)
6831 struct tinst_level *level = current_tinst_level;
6834 level = level->next;
6838 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
6841 parameter_of_template_p (tree parm, tree templ)
6846 if (!parm || !templ)
6849 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6850 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6852 parms = DECL_TEMPLATE_PARMS (templ);
6853 parms = INNERMOST_TEMPLATE_PARMS (parms);
6855 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6856 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6862 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6863 vector of template arguments, as for tsubst.
6865 Returns an appropriate tsubst'd friend declaration. */
6868 tsubst_friend_function (tree decl, tree args)
6872 if (TREE_CODE (decl) == FUNCTION_DECL
6873 && DECL_TEMPLATE_INSTANTIATION (decl)
6874 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6875 /* This was a friend declared with an explicit template
6876 argument list, e.g.:
6880 to indicate that f was a template instantiation, not a new
6881 function declaration. Now, we have to figure out what
6882 instantiation of what template. */
6884 tree template_id, arglist, fns;
6887 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
6889 /* Friend functions are looked up in the containing namespace scope.
6890 We must enter that scope, to avoid finding member functions of the
6891 current class with same name. */
6892 push_nested_namespace (ns);
6893 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
6894 tf_warning_or_error, NULL_TREE,
6895 /*integral_constant_expression_p=*/false);
6896 pop_nested_namespace (ns);
6897 arglist = tsubst (DECL_TI_ARGS (decl), args,
6898 tf_warning_or_error, NULL_TREE);
6899 template_id = lookup_template_function (fns, arglist);
6901 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6902 tmpl = determine_specialization (template_id, new_friend,
6904 /*need_member_template=*/0,
6905 TREE_VEC_LENGTH (args),
6907 return instantiate_template (tmpl, new_args, tf_error);
6910 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
6912 /* The NEW_FRIEND will look like an instantiation, to the
6913 compiler, but is not an instantiation from the point of view of
6914 the language. For example, we might have had:
6916 template <class T> struct S {
6917 template <class U> friend void f(T, U);
6920 Then, in S<int>, template <class U> void f(int, U) is not an
6921 instantiation of anything. */
6922 if (new_friend == error_mark_node)
6923 return error_mark_node;
6925 DECL_USE_TEMPLATE (new_friend) = 0;
6926 if (TREE_CODE (decl) == TEMPLATE_DECL)
6928 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6929 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6930 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6933 /* The mangled name for the NEW_FRIEND is incorrect. The function
6934 is not a template instantiation and should not be mangled like
6935 one. Therefore, we forget the mangling here; we'll recompute it
6936 later if we need it. */
6937 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6939 SET_DECL_RTL (new_friend, NULL_RTX);
6940 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
6943 if (DECL_NAMESPACE_SCOPE_P (new_friend))
6946 tree new_friend_template_info;
6947 tree new_friend_result_template_info;
6949 int new_friend_is_defn;
6951 /* We must save some information from NEW_FRIEND before calling
6952 duplicate decls since that function will free NEW_FRIEND if
6954 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
6955 new_friend_is_defn =
6956 (DECL_INITIAL (DECL_TEMPLATE_RESULT
6957 (template_for_substitution (new_friend)))
6959 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
6961 /* This declaration is a `primary' template. */
6962 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
6964 new_friend_result_template_info
6965 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
6968 new_friend_result_template_info = NULL_TREE;
6970 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6971 if (new_friend_is_defn)
6972 DECL_INITIAL (new_friend) = error_mark_node;
6974 /* Inside pushdecl_namespace_level, we will push into the
6975 current namespace. However, the friend function should go
6976 into the namespace of the template. */
6977 ns = decl_namespace_context (new_friend);
6978 push_nested_namespace (ns);
6979 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
6980 pop_nested_namespace (ns);
6982 if (old_decl == error_mark_node)
6983 return error_mark_node;
6985 if (old_decl != new_friend)
6987 /* This new friend declaration matched an existing
6988 declaration. For example, given:
6990 template <class T> void f(T);
6991 template <class U> class C {
6992 template <class T> friend void f(T) {}
6995 the friend declaration actually provides the definition
6996 of `f', once C has been instantiated for some type. So,
6997 old_decl will be the out-of-class template declaration,
6998 while new_friend is the in-class definition.
7000 But, if `f' was called before this point, the
7001 instantiation of `f' will have DECL_TI_ARGS corresponding
7002 to `T' but not to `U', references to which might appear
7003 in the definition of `f'. Previously, the most general
7004 template for an instantiation of `f' was the out-of-class
7005 version; now it is the in-class version. Therefore, we
7006 run through all specialization of `f', adding to their
7007 DECL_TI_ARGS appropriately. In particular, they need a
7008 new set of outer arguments, corresponding to the
7009 arguments for this class instantiation.
7011 The same situation can arise with something like this:
7014 template <class T> class C {
7018 when `C<int>' is instantiated. Now, `f(int)' is defined
7021 if (!new_friend_is_defn)
7022 /* On the other hand, if the in-class declaration does
7023 *not* provide a definition, then we don't want to alter
7024 existing definitions. We can just leave everything
7029 tree new_template = TI_TEMPLATE (new_friend_template_info);
7030 tree new_args = TI_ARGS (new_friend_template_info);
7032 /* Overwrite whatever template info was there before, if
7033 any, with the new template information pertaining to
7035 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
7037 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
7038 /* We should have called reregister_specialization in
7040 gcc_assert (retrieve_specialization (new_template,
7047 /* Indicate that the old function template is a partial
7049 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
7050 = new_friend_result_template_info;
7052 gcc_assert (new_template
7053 == most_general_template (new_template));
7054 gcc_assert (new_template != old_decl);
7056 /* Reassign any specializations already in the hash table
7057 to the new more general template, and add the
7058 additional template args. */
7059 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
7063 tree spec = TREE_VALUE (t);
7066 elt.tmpl = old_decl;
7067 elt.args = DECL_TI_ARGS (spec);
7068 elt.spec = NULL_TREE;
7070 htab_remove_elt (decl_specializations, &elt);
7073 = add_outermost_template_args (new_args,
7074 DECL_TI_ARGS (spec));
7076 register_specialization
7077 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
7080 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
7084 /* The information from NEW_FRIEND has been merged into OLD_DECL
7085 by duplicate_decls. */
7086 new_friend = old_decl;
7091 tree context = DECL_CONTEXT (new_friend);
7095 template <class T> class C {
7096 template <class U> friend void C1<U>::f (); // case 1
7097 friend void C2<T>::f (); // case 2
7099 we only need to make sure CONTEXT is a complete type for
7100 case 2. To distinguish between the two cases, we note that
7101 CONTEXT of case 1 remains dependent type after tsubst while
7102 this isn't true for case 2. */
7103 ++processing_template_decl;
7104 dependent_p = dependent_type_p (context);
7105 --processing_template_decl;
7108 && !complete_type_or_else (context, NULL_TREE))
7109 return error_mark_node;
7111 if (COMPLETE_TYPE_P (context))
7113 /* Check to see that the declaration is really present, and,
7114 possibly obtain an improved declaration. */
7115 tree fn = check_classfn (context,
7116 new_friend, NULL_TREE);
7126 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7127 template arguments, as for tsubst.
7129 Returns an appropriate tsubst'd friend type or error_mark_node on
7133 tsubst_friend_class (tree friend_tmpl, tree args)
7139 context = DECL_CONTEXT (friend_tmpl);
7143 if (TREE_CODE (context) == NAMESPACE_DECL)
7144 push_nested_namespace (context);
7146 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
7149 /* Look for a class template declaration. We look for hidden names
7150 because two friend declarations of the same template are the
7151 same. For example, in:
7154 template <typename> friend class F;
7156 template <typename> struct B {
7157 template <typename> friend class F;
7160 both F templates are the same. */
7161 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7162 /*block_p=*/true, 0,
7163 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
7165 /* But, if we don't find one, it might be because we're in a
7166 situation like this:
7174 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7175 for `S<int>', not the TEMPLATE_DECL. */
7176 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7178 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
7179 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
7182 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
7184 /* The friend template has already been declared. Just
7185 check to see that the declarations match, and install any new
7186 default parameters. We must tsubst the default parameters,
7187 of course. We only need the innermost template parameters
7188 because that is all that redeclare_class_template will look
7190 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7191 > TMPL_ARGS_DEPTH (args))
7194 location_t saved_input_location;
7195 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
7196 args, tf_warning_or_error);
7198 saved_input_location = input_location;
7199 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
7200 redeclare_class_template (TREE_TYPE (tmpl), parms);
7201 input_location = saved_input_location;
7205 friend_type = TREE_TYPE (tmpl);
7209 /* The friend template has not already been declared. In this
7210 case, the instantiation of the template class will cause the
7211 injection of this template into the global scope. */
7212 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
7213 if (tmpl == error_mark_node)
7214 return error_mark_node;
7216 /* The new TMPL is not an instantiation of anything, so we
7217 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7218 the new type because that is supposed to be the corresponding
7219 template decl, i.e., TMPL. */
7220 DECL_USE_TEMPLATE (tmpl) = 0;
7221 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7222 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
7223 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7224 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
7226 /* Inject this template into the global scope. */
7227 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
7232 if (TREE_CODE (context) == NAMESPACE_DECL)
7233 pop_nested_namespace (context);
7235 pop_nested_class ();
7241 /* Returns zero if TYPE cannot be completed later due to circularity.
7242 Otherwise returns one. */
7245 can_complete_type_without_circularity (tree type)
7247 if (type == NULL_TREE || type == error_mark_node)
7249 else if (COMPLETE_TYPE_P (type))
7251 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7252 return can_complete_type_without_circularity (TREE_TYPE (type));
7253 else if (CLASS_TYPE_P (type)
7254 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
7260 /* Apply any attributes which had to be deferred until instantiation
7261 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7262 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7265 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7266 tree args, tsubst_flags_t complain, tree in_decl)
7268 tree last_dep = NULL_TREE;
7272 for (t = attributes; t; t = TREE_CHAIN (t))
7273 if (ATTR_IS_DEPENDENT (t))
7276 attributes = copy_list (attributes);
7280 if (DECL_P (*decl_p))
7282 if (TREE_TYPE (*decl_p) == error_mark_node)
7284 p = &DECL_ATTRIBUTES (*decl_p);
7287 p = &TYPE_ATTRIBUTES (*decl_p);
7291 tree late_attrs = NULL_TREE;
7292 tree *q = &late_attrs;
7294 for (*p = attributes; *p; )
7297 if (ATTR_IS_DEPENDENT (t))
7299 *p = TREE_CHAIN (t);
7300 TREE_CHAIN (t) = NULL_TREE;
7301 /* If the first attribute argument is an identifier, don't
7302 pass it through tsubst. Attributes like mode, format,
7303 cleanup and several target specific attributes expect it
7306 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7307 && TREE_VALUE (TREE_VALUE (t))
7308 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7309 == IDENTIFIER_NODE))
7312 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7314 /*integral_constant_expression_p=*/false);
7315 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7317 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7322 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7323 /*integral_constant_expression_p=*/false);
7325 q = &TREE_CHAIN (t);
7328 p = &TREE_CHAIN (t);
7331 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7335 /* Perform (or defer) access check for typedefs that were referenced
7336 from within the template TMPL code.
7337 This is a subroutine of instantiate_template and instantiate_class_template.
7338 TMPL is the template to consider and TARGS is the list of arguments of
7342 perform_typedefs_access_check (tree tmpl, tree targs)
7347 || (!CLASS_TYPE_P (tmpl)
7348 && TREE_CODE (tmpl) != FUNCTION_DECL))
7351 for (t = get_types_needing_access_check (tmpl); t; t = TREE_CHAIN (t))
7353 tree type_decl = TREE_PURPOSE (t);
7354 tree type_scope = TREE_VALUE (t);
7356 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7359 if (uses_template_parms (type_decl))
7360 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7361 if (uses_template_parms (type_scope))
7362 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7364 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7365 type_decl, type_decl);
7370 instantiate_class_template (tree type)
7372 tree templ, args, pattern, t, member;
7376 unsigned int saved_maximum_field_alignment;
7378 if (type == error_mark_node)
7379 return error_mark_node;
7381 if (TYPE_BEING_DEFINED (type)
7382 || COMPLETE_TYPE_P (type)
7383 || uses_template_parms (type))
7386 /* Figure out which template is being instantiated. */
7387 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7388 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
7390 /* Determine what specialization of the original template to
7392 t = most_specialized_class (type, templ);
7393 if (t == error_mark_node)
7395 TYPE_BEING_DEFINED (type) = 1;
7396 return error_mark_node;
7400 /* This TYPE is actually an instantiation of a partial
7401 specialization. We replace the innermost set of ARGS with
7402 the arguments appropriate for substitution. For example,
7405 template <class T> struct S {};
7406 template <class T> struct S<T*> {};
7408 and supposing that we are instantiating S<int*>, ARGS will
7409 presently be {int*} -- but we need {int}. */
7410 pattern = TREE_TYPE (t);
7411 args = TREE_PURPOSE (t);
7415 pattern = TREE_TYPE (templ);
7416 args = CLASSTYPE_TI_ARGS (type);
7419 /* If the template we're instantiating is incomplete, then clearly
7420 there's nothing we can do. */
7421 if (!COMPLETE_TYPE_P (pattern))
7424 /* If we've recursively instantiated too many templates, stop. */
7425 if (! push_tinst_level (type))
7428 /* Now we're really doing the instantiation. Mark the type as in
7429 the process of being defined. */
7430 TYPE_BEING_DEFINED (type) = 1;
7432 /* We may be in the middle of deferred access check. Disable
7434 push_deferring_access_checks (dk_no_deferred);
7436 push_to_top_level ();
7437 /* Use #pragma pack from the template context. */
7438 saved_maximum_field_alignment = maximum_field_alignment;
7439 maximum_field_alignment = TYPE_PRECISION (pattern);
7441 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
7443 /* Set the input location to the most specialized template definition.
7444 This is needed if tsubsting causes an error. */
7445 typedecl = TYPE_MAIN_DECL (pattern);
7446 input_location = DECL_SOURCE_LOCATION (typedecl);
7448 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
7449 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7450 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
7451 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
7452 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7453 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
7454 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7455 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
7456 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7457 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
7458 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7459 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
7460 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
7461 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
7462 if (ANON_AGGR_TYPE_P (pattern))
7463 SET_ANON_AGGR_TYPE_P (type);
7464 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7466 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7467 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7470 pbinfo = TYPE_BINFO (pattern);
7472 /* We should never instantiate a nested class before its enclosing
7473 class; we need to look up the nested class by name before we can
7474 instantiate it, and that lookup should instantiate the enclosing
7476 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7477 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7478 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
7480 base_list = NULL_TREE;
7481 if (BINFO_N_BASE_BINFOS (pbinfo))
7484 tree context = TYPE_CONTEXT (type);
7488 /* We must enter the scope containing the type, as that is where
7489 the accessibility of types named in dependent bases are
7491 pushed_scope = push_scope (context ? context : global_namespace);
7493 /* Substitute into each of the bases to determine the actual
7495 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
7498 tree access = BINFO_BASE_ACCESS (pbinfo, i);
7499 tree expanded_bases = NULL_TREE;
7502 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7505 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7506 args, tf_error, NULL_TREE);
7507 if (expanded_bases == error_mark_node)
7510 len = TREE_VEC_LENGTH (expanded_bases);
7513 for (idx = 0; idx < len; idx++)
7516 /* Extract the already-expanded base class. */
7517 base = TREE_VEC_ELT (expanded_bases, idx);
7519 /* Substitute to figure out the base class. */
7520 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7523 if (base == error_mark_node)
7526 base_list = tree_cons (access, base, base_list);
7527 if (BINFO_VIRTUAL_P (pbase_binfo))
7528 TREE_TYPE (base_list) = integer_type_node;
7532 /* The list is now in reverse order; correct that. */
7533 base_list = nreverse (base_list);
7536 pop_scope (pushed_scope);
7538 /* Now call xref_basetypes to set up all the base-class
7540 xref_basetypes (type, base_list);
7542 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7543 (int) ATTR_FLAG_TYPE_IN_PLACE,
7544 args, tf_error, NULL_TREE);
7546 /* Now that our base classes are set up, enter the scope of the
7547 class, so that name lookups into base classes, etc. will work
7548 correctly. This is precisely analogous to what we do in
7549 begin_class_definition when defining an ordinary non-template
7550 class, except we also need to push the enclosing classes. */
7551 push_nested_class (type);
7553 /* Now members are processed in the order of declaration. */
7554 for (member = CLASSTYPE_DECL_LIST (pattern);
7555 member; member = TREE_CHAIN (member))
7557 tree t = TREE_VALUE (member);
7559 if (TREE_PURPOSE (member))
7563 /* Build new CLASSTYPE_NESTED_UTDS. */
7566 bool class_template_p;
7568 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7569 && TYPE_LANG_SPECIFIC (t)
7570 && CLASSTYPE_IS_TEMPLATE (t));
7571 /* If the member is a class template, then -- even after
7572 substitution -- there may be dependent types in the
7573 template argument list for the class. We increment
7574 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7575 that function will assume that no types are dependent
7576 when outside of a template. */
7577 if (class_template_p)
7578 ++processing_template_decl;
7579 newtag = tsubst (t, args, tf_error, NULL_TREE);
7580 if (class_template_p)
7581 --processing_template_decl;
7582 if (newtag == error_mark_node)
7585 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7587 tree name = TYPE_IDENTIFIER (t);
7589 if (class_template_p)
7590 /* Unfortunately, lookup_template_class sets
7591 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
7592 instantiation (i.e., for the type of a member
7593 template class nested within a template class.)
7594 This behavior is required for
7595 maybe_process_partial_specialization to work
7596 correctly, but is not accurate in this case;
7597 the TAG is not an instantiation of anything.
7598 (The corresponding TEMPLATE_DECL is an
7599 instantiation, but the TYPE is not.) */
7600 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7602 /* Now, we call pushtag to put this NEWTAG into the scope of
7603 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7604 pushtag calling push_template_decl. We don't have to do
7605 this for enums because it will already have been done in
7608 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
7609 pushtag (name, newtag, /*tag_scope=*/ts_current);
7612 else if (TREE_CODE (t) == FUNCTION_DECL
7613 || DECL_FUNCTION_TEMPLATE_P (t))
7615 /* Build new TYPE_METHODS. */
7618 if (TREE_CODE (t) == TEMPLATE_DECL)
7619 ++processing_template_decl;
7620 r = tsubst (t, args, tf_error, NULL_TREE);
7621 if (TREE_CODE (t) == TEMPLATE_DECL)
7622 --processing_template_decl;
7623 set_current_access_from_decl (r);
7624 finish_member_declaration (r);
7628 /* Build new TYPE_FIELDS. */
7629 if (TREE_CODE (t) == STATIC_ASSERT)
7632 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7633 tf_warning_or_error, NULL_TREE,
7634 /*integral_constant_expression_p=*/true);
7635 finish_static_assert (condition,
7636 STATIC_ASSERT_MESSAGE (t),
7637 STATIC_ASSERT_SOURCE_LOCATION (t),
7640 else if (TREE_CODE (t) != CONST_DECL)
7644 /* The file and line for this declaration, to
7645 assist in error message reporting. Since we
7646 called push_tinst_level above, we don't need to
7648 input_location = DECL_SOURCE_LOCATION (t);
7650 if (TREE_CODE (t) == TEMPLATE_DECL)
7651 ++processing_template_decl;
7652 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
7653 if (TREE_CODE (t) == TEMPLATE_DECL)
7654 --processing_template_decl;
7655 if (TREE_CODE (r) == VAR_DECL)
7659 [t]he initialization (and any associated
7660 side-effects) of a static data member does
7661 not occur unless the static data member is
7662 itself used in a way that requires the
7663 definition of the static data member to
7666 Therefore, we do not substitute into the
7667 initialized for the static data member here. */
7668 finish_static_data_member_decl
7671 /*init_const_expr_p=*/false,
7672 /*asmspec_tree=*/NULL_TREE,
7674 if (DECL_INITIALIZED_IN_CLASS_P (r))
7675 check_static_variable_definition (r, TREE_TYPE (r));
7677 else if (TREE_CODE (r) == FIELD_DECL)
7679 /* Determine whether R has a valid type and can be
7680 completed later. If R is invalid, then it is
7681 replaced by error_mark_node so that it will not be
7682 added to TYPE_FIELDS. */
7683 tree rtype = TREE_TYPE (r);
7684 if (can_complete_type_without_circularity (rtype))
7685 complete_type (rtype);
7687 if (!COMPLETE_TYPE_P (rtype))
7689 cxx_incomplete_type_error (r, rtype);
7690 r = error_mark_node;
7694 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7695 such a thing will already have been added to the field
7696 list by tsubst_enum in finish_member_declaration in the
7697 CLASSTYPE_NESTED_UTDS case above. */
7698 if (!(TREE_CODE (r) == TYPE_DECL
7699 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
7700 && DECL_ARTIFICIAL (r)))
7702 set_current_access_from_decl (r);
7703 finish_member_declaration (r);
7710 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7712 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7714 tree friend_type = t;
7715 bool adjust_processing_template_decl = false;
7717 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7719 /* template <class T> friend class C; */
7720 friend_type = tsubst_friend_class (friend_type, args);
7721 adjust_processing_template_decl = true;
7723 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7725 /* template <class T> friend class C::D; */
7726 friend_type = tsubst (friend_type, args,
7727 tf_warning_or_error, NULL_TREE);
7728 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7729 friend_type = TREE_TYPE (friend_type);
7730 adjust_processing_template_decl = true;
7732 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7734 /* This could be either
7738 when dependent_type_p is false or
7740 template <class U> friend class T::C;
7743 friend_type = tsubst (friend_type, args,
7744 tf_warning_or_error, NULL_TREE);
7745 /* Bump processing_template_decl for correct
7746 dependent_type_p calculation. */
7747 ++processing_template_decl;
7748 if (dependent_type_p (friend_type))
7749 adjust_processing_template_decl = true;
7750 --processing_template_decl;
7752 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7753 && hidden_name_p (TYPE_NAME (friend_type)))
7757 where C hasn't been declared yet. Let's lookup name
7758 from namespace scope directly, bypassing any name that
7759 come from dependent base class. */
7760 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7762 /* The call to xref_tag_from_type does injection for friend
7764 push_nested_namespace (ns);
7766 xref_tag_from_type (friend_type, NULL_TREE,
7767 /*tag_scope=*/ts_current);
7768 pop_nested_namespace (ns);
7770 else if (uses_template_parms (friend_type))
7771 /* friend class C<T>; */
7772 friend_type = tsubst (friend_type, args,
7773 tf_warning_or_error, NULL_TREE);
7778 where C is already declared or
7780 friend class C<int>;
7782 We don't have to do anything in these cases. */
7784 if (adjust_processing_template_decl)
7785 /* Trick make_friend_class into realizing that the friend
7786 we're adding is a template, not an ordinary class. It's
7787 important that we use make_friend_class since it will
7788 perform some error-checking and output cross-reference
7790 ++processing_template_decl;
7792 if (friend_type != error_mark_node)
7793 make_friend_class (type, friend_type, /*complain=*/false);
7795 if (adjust_processing_template_decl)
7796 --processing_template_decl;
7800 /* Build new DECL_FRIENDLIST. */
7803 /* The file and line for this declaration, to
7804 assist in error message reporting. Since we
7805 called push_tinst_level above, we don't need to
7807 input_location = DECL_SOURCE_LOCATION (t);
7809 if (TREE_CODE (t) == TEMPLATE_DECL)
7811 ++processing_template_decl;
7812 push_deferring_access_checks (dk_no_check);
7815 r = tsubst_friend_function (t, args);
7816 add_friend (type, r, /*complain=*/false);
7817 if (TREE_CODE (t) == TEMPLATE_DECL)
7819 pop_deferring_access_checks ();
7820 --processing_template_decl;
7826 /* Set the file and line number information to whatever is given for
7827 the class itself. This puts error messages involving generated
7828 implicit functions at a predictable point, and the same point
7829 that would be used for non-template classes. */
7830 input_location = DECL_SOURCE_LOCATION (typedecl);
7832 unreverse_member_declarations (type);
7833 finish_struct_1 (type);
7834 TYPE_BEING_DEFINED (type) = 0;
7836 /* Now that the class is complete, instantiate default arguments for
7837 any member functions. We don't do this earlier because the
7838 default arguments may reference members of the class. */
7839 if (!PRIMARY_TEMPLATE_P (templ))
7840 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
7841 if (TREE_CODE (t) == FUNCTION_DECL
7842 /* Implicitly generated member functions will not have template
7843 information; they are not instantiations, but instead are
7844 created "fresh" for each instantiation. */
7845 && DECL_TEMPLATE_INFO (t))
7846 tsubst_default_arguments (t);
7848 /* Some typedefs referenced from within the template code need to be access
7849 checked at template instantiation time, i.e now. These types were
7850 added to the template at parsing time. Let's get those and perform
7851 the access checks then. */
7852 perform_typedefs_access_check (pattern, args);
7853 perform_deferred_access_checks ();
7854 pop_nested_class ();
7855 maximum_field_alignment = saved_maximum_field_alignment;
7856 pop_from_top_level ();
7857 pop_deferring_access_checks ();
7860 /* The vtable for a template class can be emitted in any translation
7861 unit in which the class is instantiated. When there is no key
7862 method, however, finish_struct_1 will already have added TYPE to
7863 the keyed_classes list. */
7864 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
7865 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7871 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7877 else if (TYPE_P (t))
7878 r = tsubst (t, args, complain, in_decl);
7881 r = tsubst_expr (t, args, complain, in_decl,
7882 /*integral_constant_expression_p=*/true);
7883 r = fold_non_dependent_expr (r);
7888 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
7889 NONTYPE_ARGUMENT_PACK. */
7892 make_fnparm_pack (tree spec_parm)
7894 /* Collect all of the extra "packed" parameters into an
7898 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
7899 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
7900 int i, len = list_length (spec_parm);
7902 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
7903 parmvec = make_tree_vec (len);
7904 parmtypevec = make_tree_vec (len);
7905 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
7907 TREE_VEC_ELT (parmvec, i) = spec_parm;
7908 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
7911 /* Build the argument packs. */
7912 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
7913 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
7914 TREE_TYPE (argpack) = argtypepack;
7919 /* Substitute ARGS into T, which is an pack expansion
7920 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7921 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7922 (if only a partial substitution could be performed) or
7923 ERROR_MARK_NODE if there was an error. */
7925 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
7929 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
7930 tree first_arg_pack; int i, len = -1;
7933 bool very_local_specializations = false;
7935 gcc_assert (PACK_EXPANSION_P (t));
7936 pattern = PACK_EXPANSION_PATTERN (t);
7938 /* Determine the argument packs that will instantiate the parameter
7939 packs used in the expansion expression. While we're at it,
7940 compute the number of arguments to be expanded and make sure it
7942 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
7943 pack = TREE_CHAIN (pack))
7945 tree parm_pack = TREE_VALUE (pack);
7946 tree arg_pack = NULL_TREE;
7947 tree orig_arg = NULL_TREE;
7949 if (TREE_CODE (parm_pack) == PARM_DECL)
7951 arg_pack = retrieve_local_specialization (parm_pack);
7952 if (arg_pack == NULL_TREE)
7954 /* This can happen for a parameter name used later in a function
7955 declaration (such as in a late-specified return type). Just
7956 make a dummy decl, since it's only used for its type. */
7957 gcc_assert (cp_unevaluated_operand != 0);
7958 arg_pack = tsubst_decl (parm_pack, args, complain);
7959 arg_pack = make_fnparm_pack (arg_pack);
7964 int level, idx, levels;
7965 template_parm_level_and_index (parm_pack, &level, &idx);
7967 levels = TMPL_ARGS_DEPTH (args);
7968 if (level <= levels)
7969 arg_pack = TMPL_ARG (args, level, idx);
7972 orig_arg = arg_pack;
7973 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7974 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7976 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7977 /* This can only happen if we forget to expand an argument
7978 pack somewhere else. Just return an error, silently. */
7980 result = make_tree_vec (1);
7981 TREE_VEC_ELT (result, 0) = error_mark_node;
7986 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
7987 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
7989 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
7990 tree pattern = PACK_EXPANSION_PATTERN (expansion);
7991 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
7992 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
7993 /* The argument pack that the parameter maps to is just an
7994 expansion of the parameter itself, such as one would
7995 find in the implicit typedef of a class inside the
7996 class itself. Consider this parameter "unsubstituted",
7997 so that we will maintain the outer pack expansion. */
7998 arg_pack = NULL_TREE;
8004 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
8006 /* It's all-or-nothing with incomplete argument packs. */
8007 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8008 return error_mark_node;
8010 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
8016 first_arg_pack = arg_pack;
8018 else if (len != my_len)
8021 /* We got explicit args for some packs but not others;
8022 do nothing now and try again after deduction. */
8024 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
8025 error ("mismatched argument pack lengths while expanding "
8029 error ("mismatched argument pack lengths while expanding "
8032 return error_mark_node;
8035 /* Keep track of the parameter packs and their corresponding
8037 packs = tree_cons (parm_pack, arg_pack, packs);
8038 TREE_TYPE (packs) = orig_arg;
8041 /* We can't substitute for this parameter pack. */
8042 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
8044 unsubstituted_packs);
8047 /* We cannot expand this expansion expression, because we don't have
8048 all of the argument packs we need. Substitute into the pattern
8049 and return a PACK_EXPANSION_*. The caller will need to deal with
8051 if (unsubstituted_packs)
8054 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8055 new_pat = tsubst_expr (pattern, args, complain, in_decl,
8056 /*integral_constant_expression_p=*/false);
8058 new_pat = tsubst (pattern, args, complain, in_decl);
8059 return make_pack_expansion (new_pat);
8062 /* We could not find any argument packs that work. */
8064 return error_mark_node;
8066 if (!local_specializations)
8068 /* We're in a late-specified return type, so we don't have a local
8069 specializations table. Create one for doing this expansion. */
8070 very_local_specializations = true;
8071 local_specializations = htab_create (37,
8072 hash_local_specialization,
8073 eq_local_specializations,
8077 /* For each argument in each argument pack, substitute into the
8079 result = make_tree_vec (len + incomplete);
8080 for (i = 0; i < len + incomplete; ++i)
8082 /* For parameter pack, change the substitution of the parameter
8083 pack to the ith argument in its argument pack, then expand
8085 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8087 tree parm = TREE_PURPOSE (pack);
8089 if (TREE_CODE (parm) == PARM_DECL)
8091 /* Select the Ith argument from the pack. */
8092 tree arg = make_node (ARGUMENT_PACK_SELECT);
8093 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
8094 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
8096 register_local_specialization (arg, parm);
8102 template_parm_level_and_index (parm, &level, &idx);
8106 /* Select the Ith argument from the pack. */
8107 value = make_node (ARGUMENT_PACK_SELECT);
8108 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
8109 ARGUMENT_PACK_SELECT_INDEX (value) = i;
8112 /* Update the corresponding argument. */
8113 TMPL_ARG (args, level, idx) = value;
8117 /* Substitute into the PATTERN with the altered arguments. */
8118 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
8119 TREE_VEC_ELT (result, i) =
8120 tsubst_expr (pattern, args, complain, in_decl,
8121 /*integral_constant_expression_p=*/false);
8123 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
8126 /* When we have incomplete argument packs, the last "expanded"
8127 result is itself a pack expansion, which allows us
8128 to deduce more arguments. */
8129 TREE_VEC_ELT (result, i) =
8130 make_pack_expansion (TREE_VEC_ELT (result, i));
8132 if (TREE_VEC_ELT (result, i) == error_mark_node)
8134 result = error_mark_node;
8139 /* Update ARGS to restore the substitution from parameter packs to
8140 their argument packs. */
8141 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8143 tree parm = TREE_PURPOSE (pack);
8145 if (TREE_CODE (parm) == PARM_DECL)
8146 register_local_specialization (TREE_TYPE (pack), parm);
8150 template_parm_level_and_index (parm, &level, &idx);
8152 /* Update the corresponding argument. */
8153 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8154 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8157 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8161 if (very_local_specializations)
8163 htab_delete (local_specializations);
8164 local_specializations = NULL;
8170 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8171 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8172 parameter packs; all parms generated from a function parameter pack will
8173 have the same DECL_PARM_INDEX. */
8176 get_pattern_parm (tree parm, tree tmpl)
8178 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
8181 if (DECL_ARTIFICIAL (parm))
8183 for (patparm = DECL_ARGUMENTS (pattern);
8184 patparm; patparm = TREE_CHAIN (patparm))
8185 if (DECL_ARTIFICIAL (patparm)
8186 && DECL_NAME (parm) == DECL_NAME (patparm))
8191 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
8192 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
8193 gcc_assert (DECL_PARM_INDEX (patparm)
8194 == DECL_PARM_INDEX (parm));
8200 /* Substitute ARGS into the vector or list of template arguments T. */
8203 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8206 int len = TREE_VEC_LENGTH (t);
8207 int need_new = 0, i, expanded_len_adjust = 0, out;
8208 tree *elts = (tree *) alloca (len * sizeof (tree));
8210 for (i = 0; i < len; i++)
8212 tree orig_arg = TREE_VEC_ELT (t, i);
8215 if (TREE_CODE (orig_arg) == TREE_VEC)
8216 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
8217 else if (PACK_EXPANSION_P (orig_arg))
8219 /* Substitute into an expansion expression. */
8220 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8222 if (TREE_CODE (new_arg) == TREE_VEC)
8223 /* Add to the expanded length adjustment the number of
8224 expanded arguments. We subtract one from this
8225 measurement, because the argument pack expression
8226 itself is already counted as 1 in
8227 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8228 the argument pack is empty. */
8229 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8231 else if (ARGUMENT_PACK_P (orig_arg))
8233 /* Substitute into each of the arguments. */
8234 new_arg = TYPE_P (orig_arg)
8235 ? cxx_make_type (TREE_CODE (orig_arg))
8236 : make_node (TREE_CODE (orig_arg));
8238 SET_ARGUMENT_PACK_ARGS (
8240 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8241 args, complain, in_decl));
8243 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8244 new_arg = error_mark_node;
8246 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8247 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8249 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8251 if (TREE_TYPE (new_arg) == error_mark_node)
8252 new_arg = error_mark_node;
8256 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
8258 if (new_arg == error_mark_node)
8259 return error_mark_node;
8262 if (new_arg != orig_arg)
8269 /* Make space for the expanded arguments coming from template
8271 t = make_tree_vec (len + expanded_len_adjust);
8272 for (i = 0, out = 0; i < len; i++)
8274 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8275 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8276 && TREE_CODE (elts[i]) == TREE_VEC)
8280 /* Now expand the template argument pack "in place". */
8281 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8282 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8286 TREE_VEC_ELT (t, out) = elts[i];
8294 /* Return the result of substituting ARGS into the template parameters
8295 given by PARMS. If there are m levels of ARGS and m + n levels of
8296 PARMS, then the result will contain n levels of PARMS. For
8297 example, if PARMS is `template <class T> template <class U>
8298 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8299 result will be `template <int*, double, class V>'. */
8302 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
8307 /* When substituting into a template, we must set
8308 PROCESSING_TEMPLATE_DECL as the template parameters may be
8309 dependent if they are based on one-another, and the dependency
8310 predicates are short-circuit outside of templates. */
8311 ++processing_template_decl;
8313 for (new_parms = &r;
8314 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8315 new_parms = &(TREE_CHAIN (*new_parms)),
8316 parms = TREE_CHAIN (parms))
8319 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8322 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8328 if (parms == error_mark_node)
8331 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8333 if (tuple == error_mark_node)
8336 default_value = TREE_PURPOSE (tuple);
8337 parm_decl = TREE_VALUE (tuple);
8339 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
8340 if (TREE_CODE (parm_decl) == PARM_DECL
8341 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8342 parm_decl = error_mark_node;
8343 default_value = tsubst_template_arg (default_value, args,
8344 complain, NULL_TREE);
8346 tuple = build_tree_list (default_value, parm_decl);
8347 TREE_VEC_ELT (new_vec, i) = tuple;
8351 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
8352 - TMPL_ARGS_DEPTH (args)),
8353 new_vec, NULL_TREE);
8356 --processing_template_decl;
8361 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8362 type T. If T is not an aggregate or enumeration type, it is
8363 handled as if by tsubst. IN_DECL is as for tsubst. If
8364 ENTERING_SCOPE is nonzero, T is the context for a template which
8365 we are presently tsubst'ing. Return the substituted value. */
8368 tsubst_aggr_type (tree t,
8370 tsubst_flags_t complain,
8377 switch (TREE_CODE (t))
8380 if (TYPE_PTRMEMFUNC_P (t))
8381 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
8383 /* Else fall through. */
8386 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
8391 int saved_unevaluated_operand;
8392 int saved_inhibit_evaluation_warnings;
8394 /* In "sizeof(X<I>)" we need to evaluate "I". */
8395 saved_unevaluated_operand = cp_unevaluated_operand;
8396 cp_unevaluated_operand = 0;
8397 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8398 c_inhibit_evaluation_warnings = 0;
8400 /* First, determine the context for the type we are looking
8402 context = TYPE_CONTEXT (t);
8405 context = tsubst_aggr_type (context, args, complain,
8406 in_decl, /*entering_scope=*/1);
8407 /* If context is a nested class inside a class template,
8408 it may still need to be instantiated (c++/33959). */
8409 if (TYPE_P (context))
8410 context = complete_type (context);
8413 /* Then, figure out what arguments are appropriate for the
8414 type we are trying to find. For example, given:
8416 template <class T> struct S;
8417 template <class T, class U> void f(T, U) { S<U> su; }
8419 and supposing that we are instantiating f<int, double>,
8420 then our ARGS will be {int, double}, but, when looking up
8421 S we only want {double}. */
8422 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8424 if (argvec == error_mark_node)
8425 r = error_mark_node;
8428 r = lookup_template_class (t, argvec, in_decl, context,
8429 entering_scope, complain);
8430 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8433 cp_unevaluated_operand = saved_unevaluated_operand;
8434 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8439 /* This is not a template type, so there's nothing to do. */
8443 return tsubst (t, args, complain, in_decl);
8447 /* Substitute into the default argument ARG (a default argument for
8448 FN), which has the indicated TYPE. */
8451 tsubst_default_argument (tree fn, tree type, tree arg)
8453 tree saved_class_ptr = NULL_TREE;
8454 tree saved_class_ref = NULL_TREE;
8456 /* This default argument came from a template. Instantiate the
8457 default argument here, not in tsubst. In the case of
8466 we must be careful to do name lookup in the scope of S<T>,
8467 rather than in the current class. */
8468 push_access_scope (fn);
8469 /* The "this" pointer is not valid in a default argument. */
8472 saved_class_ptr = current_class_ptr;
8473 cp_function_chain->x_current_class_ptr = NULL_TREE;
8474 saved_class_ref = current_class_ref;
8475 cp_function_chain->x_current_class_ref = NULL_TREE;
8478 push_deferring_access_checks(dk_no_deferred);
8479 /* The default argument expression may cause implicitly defined
8480 member functions to be synthesized, which will result in garbage
8481 collection. We must treat this situation as if we were within
8482 the body of function so as to avoid collecting live data on the
8485 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
8486 tf_warning_or_error, NULL_TREE,
8487 /*integral_constant_expression_p=*/false);
8489 pop_deferring_access_checks();
8491 /* Restore the "this" pointer. */
8494 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8495 cp_function_chain->x_current_class_ref = saved_class_ref;
8498 /* Make sure the default argument is reasonable. */
8499 arg = check_default_argument (type, arg);
8501 pop_access_scope (fn);
8506 /* Substitute into all the default arguments for FN. */
8509 tsubst_default_arguments (tree fn)
8514 tmpl_args = DECL_TI_ARGS (fn);
8516 /* If this function is not yet instantiated, we certainly don't need
8517 its default arguments. */
8518 if (uses_template_parms (tmpl_args))
8521 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8523 arg = TREE_CHAIN (arg))
8524 if (TREE_PURPOSE (arg))
8525 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
8527 TREE_PURPOSE (arg));
8530 /* Substitute the ARGS into the T, which is a _DECL. Return the
8531 result of the substitution. Issue error and warning messages under
8532 control of COMPLAIN. */
8535 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8537 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8538 location_t saved_loc;
8543 /* Set the filename and linenumber to improve error-reporting. */
8544 saved_loc = input_location;
8545 input_location = DECL_SOURCE_LOCATION (t);
8547 switch (TREE_CODE (t))
8551 /* We can get here when processing a member function template,
8552 member class template, or template template parameter. */
8553 tree decl = DECL_TEMPLATE_RESULT (t);
8558 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8560 /* Template template parameter is treated here. */
8561 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8562 if (new_type == error_mark_node)
8563 RETURN (error_mark_node);
8566 TREE_CHAIN (r) = NULL_TREE;
8567 TREE_TYPE (r) = new_type;
8568 DECL_TEMPLATE_RESULT (r)
8569 = build_decl (DECL_SOURCE_LOCATION (decl),
8570 TYPE_DECL, DECL_NAME (decl), new_type);
8571 DECL_TEMPLATE_PARMS (r)
8572 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8574 TYPE_NAME (new_type) = r;
8578 /* We might already have an instance of this template.
8579 The ARGS are for the surrounding class type, so the
8580 full args contain the tsubst'd args for the context,
8581 plus the innermost args from the template decl. */
8582 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
8583 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8584 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8585 /* Because this is a template, the arguments will still be
8586 dependent, even after substitution. If
8587 PROCESSING_TEMPLATE_DECL is not set, the dependency
8588 predicates will short-circuit. */
8589 ++processing_template_decl;
8590 full_args = tsubst_template_args (tmpl_args, args,
8592 --processing_template_decl;
8593 if (full_args == error_mark_node)
8594 RETURN (error_mark_node);
8596 /* If this is a default template template argument,
8597 tsubst might not have changed anything. */
8598 if (full_args == tmpl_args)
8601 hash = hash_tmpl_and_args (t, full_args);
8602 spec = retrieve_specialization (t, full_args, hash);
8603 if (spec != NULL_TREE)
8609 /* Make a new template decl. It will be similar to the
8610 original, but will record the current template arguments.
8611 We also create a new function declaration, which is just
8612 like the old one, but points to this new template, rather
8613 than the old one. */
8615 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
8616 TREE_CHAIN (r) = NULL_TREE;
8618 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
8620 if (TREE_CODE (decl) == TYPE_DECL)
8623 ++processing_template_decl;
8624 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8625 --processing_template_decl;
8626 if (new_type == error_mark_node)
8627 RETURN (error_mark_node);
8629 TREE_TYPE (r) = new_type;
8630 CLASSTYPE_TI_TEMPLATE (new_type) = r;
8631 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
8632 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8633 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
8638 ++processing_template_decl;
8639 new_decl = tsubst (decl, args, complain, in_decl);
8640 --processing_template_decl;
8641 if (new_decl == error_mark_node)
8642 RETURN (error_mark_node);
8644 DECL_TEMPLATE_RESULT (r) = new_decl;
8645 DECL_TI_TEMPLATE (new_decl) = r;
8646 TREE_TYPE (r) = TREE_TYPE (new_decl);
8647 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
8648 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
8651 SET_DECL_IMPLICIT_INSTANTIATION (r);
8652 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8653 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
8655 /* The template parameters for this new template are all the
8656 template parameters for the old template, except the
8657 outermost level of parameters. */
8658 DECL_TEMPLATE_PARMS (r)
8659 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8662 if (PRIMARY_TEMPLATE_P (t))
8663 DECL_PRIMARY_TEMPLATE (r) = r;
8665 if (TREE_CODE (decl) != TYPE_DECL)
8666 /* Record this non-type partial instantiation. */
8667 register_specialization (r, t,
8668 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
8676 tree argvec = NULL_TREE;
8684 /* Nobody should be tsubst'ing into non-template functions. */
8685 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
8687 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8692 /* If T is not dependent, just return it. We have to
8693 increment PROCESSING_TEMPLATE_DECL because
8694 value_dependent_expression_p assumes that nothing is
8695 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8696 ++processing_template_decl;
8697 dependent_p = value_dependent_expression_p (t);
8698 --processing_template_decl;
8702 /* Calculate the most general template of which R is a
8703 specialization, and the complete set of arguments used to
8705 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
8706 argvec = tsubst_template_args (DECL_TI_ARGS
8707 (DECL_TEMPLATE_RESULT (gen_tmpl)),
8708 args, complain, in_decl);
8710 /* Check to see if we already have this specialization. */
8711 hash = hash_tmpl_and_args (gen_tmpl, argvec);
8712 spec = retrieve_specialization (gen_tmpl, argvec, hash);
8720 /* We can see more levels of arguments than parameters if
8721 there was a specialization of a member template, like
8724 template <class T> struct S { template <class U> void f(); }
8725 template <> template <class U> void S<int>::f(U);
8727 Here, we'll be substituting into the specialization,
8728 because that's where we can find the code we actually
8729 want to generate, but we'll have enough arguments for
8730 the most general template.
8732 We also deal with the peculiar case:
8734 template <class T> struct S {
8735 template <class U> friend void f();
8737 template <class U> void f() {}
8739 template void f<double>();
8741 Here, the ARGS for the instantiation of will be {int,
8742 double}. But, we only need as many ARGS as there are
8743 levels of template parameters in CODE_PATTERN. We are
8744 careful not to get fooled into reducing the ARGS in
8747 template <class T> struct S { template <class U> void f(U); }
8748 template <class T> template <> void S<T>::f(int) {}
8750 which we can spot because the pattern will be a
8751 specialization in this case. */
8752 args_depth = TMPL_ARGS_DEPTH (args);
8754 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
8755 if (args_depth > parms_depth
8756 && !DECL_TEMPLATE_SPECIALIZATION (t))
8757 args = get_innermost_template_args (args, parms_depth);
8761 /* This special case arises when we have something like this:
8763 template <class T> struct S {
8764 friend void f<int>(int, double);
8767 Here, the DECL_TI_TEMPLATE for the friend declaration
8768 will be an IDENTIFIER_NODE. We are being called from
8769 tsubst_friend_function, and we want only to create a
8770 new decl (R) with appropriate types so that we can call
8771 determine_specialization. */
8772 gen_tmpl = NULL_TREE;
8775 if (DECL_CLASS_SCOPE_P (t))
8777 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8781 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8782 complain, t, /*entering_scope=*/1);
8787 ctx = DECL_CONTEXT (t);
8789 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8790 if (type == error_mark_node)
8791 RETURN (error_mark_node);
8793 /* We do NOT check for matching decls pushed separately at this
8794 point, as they may not represent instantiations of this
8795 template, and in any case are considered separate under the
8798 DECL_USE_TEMPLATE (r) = 0;
8799 TREE_TYPE (r) = type;
8800 /* Clear out the mangled name and RTL for the instantiation. */
8801 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8802 SET_DECL_RTL (r, NULL_RTX);
8803 /* Leave DECL_INITIAL set on deleted instantiations. */
8804 if (!DECL_DELETED_FN (r))
8805 DECL_INITIAL (r) = NULL_TREE;
8806 DECL_CONTEXT (r) = ctx;
8808 if (member && DECL_CONV_FN_P (r))
8809 /* Type-conversion operator. Reconstruct the name, in
8810 case it's the name of one of the template's parameters. */
8811 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
8813 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
8815 DECL_RESULT (r) = NULL_TREE;
8817 TREE_STATIC (r) = 0;
8818 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8819 DECL_EXTERNAL (r) = 1;
8820 /* If this is an instantiation of a function with internal
8821 linkage, we already know what object file linkage will be
8822 assigned to the instantiation. */
8823 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
8824 DECL_DEFER_OUTPUT (r) = 0;
8825 TREE_CHAIN (r) = NULL_TREE;
8826 DECL_PENDING_INLINE_INFO (r) = 0;
8827 DECL_PENDING_INLINE_P (r) = 0;
8828 DECL_SAVED_TREE (r) = NULL_TREE;
8829 DECL_STRUCT_FUNCTION (r) = NULL;
8831 /* We'll re-clone as appropriate in instantiate_template. */
8832 DECL_CLONED_FUNCTION (r) = NULL_TREE;
8834 /* If we aren't complaining now, return on error before we register
8835 the specialization so that we'll complain eventually. */
8836 if ((complain & tf_error) == 0
8837 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
8838 && !grok_op_properties (r, /*complain=*/false))
8839 RETURN (error_mark_node);
8841 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8842 this in the special friend case mentioned above where
8843 GEN_TMPL is NULL. */
8846 DECL_TEMPLATE_INFO (r)
8847 = tree_cons (gen_tmpl, argvec, NULL_TREE);
8848 SET_DECL_IMPLICIT_INSTANTIATION (r);
8849 register_specialization (r, gen_tmpl, argvec, false, hash);
8851 /* We're not supposed to instantiate default arguments
8852 until they are called, for a template. But, for a
8855 template <class T> void f ()
8856 { extern void g(int i = T()); }
8858 we should do the substitution when the template is
8859 instantiated. We handle the member function case in
8860 instantiate_class_template since the default arguments
8861 might refer to other members of the class. */
8863 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8864 && !uses_template_parms (argvec))
8865 tsubst_default_arguments (r);
8868 DECL_TEMPLATE_INFO (r) = NULL_TREE;
8870 /* Copy the list of befriending classes. */
8871 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8873 friends = &TREE_CHAIN (*friends))
8875 *friends = copy_node (*friends);
8876 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
8881 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
8883 maybe_retrofit_in_chrg (r);
8884 if (DECL_CONSTRUCTOR_P (r))
8885 grok_ctor_properties (ctx, r);
8886 /* If this is an instantiation of a member template, clone it.
8887 If it isn't, that'll be handled by
8888 clone_constructors_and_destructors. */
8889 if (PRIMARY_TEMPLATE_P (gen_tmpl))
8890 clone_function_decl (r, /*update_method_vec_p=*/0);
8892 else if ((complain & tf_error) != 0
8893 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
8894 && !grok_op_properties (r, /*complain=*/true))
8895 RETURN (error_mark_node);
8897 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
8898 SET_DECL_FRIEND_CONTEXT (r,
8899 tsubst (DECL_FRIEND_CONTEXT (t),
8900 args, complain, in_decl));
8902 /* Possibly limit visibility based on template args. */
8903 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
8904 if (DECL_VISIBILITY_SPECIFIED (t))
8906 DECL_VISIBILITY_SPECIFIED (r) = 0;
8908 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8910 determine_visibility (r);
8911 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
8912 && !processing_template_decl)
8913 defaulted_late_check (r);
8915 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8916 args, complain, in_decl);
8922 tree type = NULL_TREE;
8924 tree expanded_types = NULL_TREE;
8925 tree prev_r = NULL_TREE;
8926 tree first_r = NULL_TREE;
8928 if (FUNCTION_PARAMETER_PACK_P (t))
8930 /* If there is a local specialization that isn't a
8931 parameter pack, it means that we're doing a "simple"
8932 substitution from inside tsubst_pack_expansion. Just
8933 return the local specialization (which will be a single
8935 tree spec = retrieve_local_specialization (t);
8937 && TREE_CODE (spec) == PARM_DECL
8938 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
8941 /* Expand the TYPE_PACK_EXPANSION that provides the types for
8942 the parameters in this function parameter pack. */
8943 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
8945 if (TREE_CODE (expanded_types) == TREE_VEC)
8947 len = TREE_VEC_LENGTH (expanded_types);
8949 /* Zero-length parameter packs are boring. Just substitute
8952 RETURN (tsubst (TREE_CHAIN (t), args, complain,
8957 /* All we did was update the type. Make a note of that. */
8958 type = expanded_types;
8959 expanded_types = NULL_TREE;
8963 /* Loop through all of the parameter's we'll build. When T is
8964 a function parameter pack, LEN is the number of expanded
8965 types in EXPANDED_TYPES; otherwise, LEN is 1. */
8967 for (i = 0; i < len; ++i)
8971 if (DECL_TEMPLATE_PARM_P (t))
8972 SET_DECL_TEMPLATE_PARM_P (r);
8974 /* An argument of a function parameter pack is not a parameter
8976 FUNCTION_PARAMETER_PACK_P (r) = false;
8979 /* We're on the Ith parameter of the function parameter
8982 /* Get the Ith type. */
8983 type = TREE_VEC_ELT (expanded_types, i);
8986 /* Rename the parameter to include the index. */
8988 make_ith_pack_parameter_name (DECL_NAME (r), i);
8991 /* We're dealing with a normal parameter. */
8992 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8994 type = type_decays_to (type);
8995 TREE_TYPE (r) = type;
8996 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8998 if (DECL_INITIAL (r))
9000 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
9001 DECL_INITIAL (r) = TREE_TYPE (r);
9003 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
9007 DECL_CONTEXT (r) = NULL_TREE;
9009 if (!DECL_TEMPLATE_PARM_P (r))
9010 DECL_ARG_TYPE (r) = type_passed_as (type);
9012 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9013 args, complain, in_decl);
9015 /* Keep track of the first new parameter we
9016 generate. That's what will be returned to the
9021 /* Build a proper chain of parameters when substituting
9022 into a function parameter pack. */
9024 TREE_CHAIN (prev_r) = r;
9028 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
9029 complain, TREE_CHAIN (t));
9031 /* FIRST_R contains the start of the chain we've built. */
9041 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9042 if (type == error_mark_node)
9043 RETURN (error_mark_node);
9044 TREE_TYPE (r) = type;
9045 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9047 /* DECL_INITIAL gives the number of bits in a bit-field. */
9049 = tsubst_expr (DECL_INITIAL (t), args,
9051 /*integral_constant_expression_p=*/true);
9052 /* We don't have to set DECL_CONTEXT here; it is set by
9053 finish_member_declaration. */
9054 TREE_CHAIN (r) = NULL_TREE;
9055 if (VOID_TYPE_P (type))
9056 error ("instantiation of %q+D as type %qT", r, type);
9058 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
9059 args, complain, in_decl);
9064 /* We reach here only for member using decls. */
9065 if (DECL_DEPENDENT_P (t))
9067 r = do_class_using_decl
9068 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
9069 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
9071 r = error_mark_node;
9074 TREE_PROTECTED (r) = TREE_PROTECTED (t);
9075 TREE_PRIVATE (r) = TREE_PRIVATE (t);
9081 TREE_CHAIN (r) = NULL_TREE;
9088 tree argvec = NULL_TREE;
9089 tree gen_tmpl = NULL_TREE;
9091 tree tmpl = NULL_TREE;
9093 tree type = NULL_TREE;
9096 if (TREE_CODE (t) == TYPE_DECL
9097 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9099 /* If this is the canonical decl, we don't have to
9100 mess with instantiations, and often we can't (for
9101 typename, template type parms and such). Note that
9102 TYPE_NAME is not correct for the above test if
9103 we've copied the type for a typedef. */
9104 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9105 if (type == error_mark_node)
9106 RETURN (error_mark_node);
9107 r = TYPE_NAME (type);
9111 /* Check to see if we already have the specialization we
9114 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
9116 /* T is a static data member or namespace-scope entity.
9117 We have to substitute into namespace-scope variables
9118 (even though such entities are never templates) because
9121 template <class T> void f() { extern T t; }
9123 where the entity referenced is not known until
9124 instantiation time. */
9126 ctx = DECL_CONTEXT (t);
9127 if (DECL_CLASS_SCOPE_P (t))
9129 ctx = tsubst_aggr_type (ctx, args,
9131 in_decl, /*entering_scope=*/1);
9132 /* If CTX is unchanged, then T is in fact the
9133 specialization we want. That situation occurs when
9134 referencing a static data member within in its own
9135 class. We can use pointer equality, rather than
9136 same_type_p, because DECL_CONTEXT is always
9138 if (ctx == DECL_CONTEXT (t))
9144 tmpl = DECL_TI_TEMPLATE (t);
9145 gen_tmpl = most_general_template (tmpl);
9146 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
9147 hash = hash_tmpl_and_args (gen_tmpl, argvec);
9148 spec = retrieve_specialization (gen_tmpl, argvec, hash);
9153 /* A local variable. */
9155 /* Subsequent calls to pushdecl will fill this in. */
9157 spec = retrieve_local_specialization (t);
9159 /* If we already have the specialization we need, there is
9160 nothing more to do. */
9167 /* Create a new node for the specialization we need. */
9169 if (type == NULL_TREE)
9170 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9171 if (TREE_CODE (r) == VAR_DECL)
9173 /* Even if the original location is out of scope, the
9174 newly substituted one is not. */
9175 DECL_DEAD_FOR_LOCAL (r) = 0;
9176 DECL_INITIALIZED_P (r) = 0;
9177 DECL_TEMPLATE_INSTANTIATED (r) = 0;
9178 if (type == error_mark_node)
9179 RETURN (error_mark_node);
9180 if (TREE_CODE (type) == FUNCTION_TYPE)
9182 /* It may seem that this case cannot occur, since:
9187 declares a function, not a variable. However:
9190 template <typename T> void g() { T t; }
9191 template void g<f>();
9193 is an attempt to declare a variable with function
9195 error ("variable %qD has function type",
9196 /* R is not yet sufficiently initialized, so we
9197 just use its name. */
9199 RETURN (error_mark_node);
9201 type = complete_type (type);
9202 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9203 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
9204 type = check_var_type (DECL_NAME (r), type);
9206 if (DECL_HAS_VALUE_EXPR_P (t))
9208 tree ve = DECL_VALUE_EXPR (t);
9209 ve = tsubst_expr (ve, args, complain, in_decl,
9210 /*constant_expression_p=*/false);
9211 SET_DECL_VALUE_EXPR (r, ve);
9214 else if (DECL_SELF_REFERENCE_P (t))
9215 SET_DECL_SELF_REFERENCE_P (r);
9216 TREE_TYPE (r) = type;
9217 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
9218 DECL_CONTEXT (r) = ctx;
9219 /* Clear out the mangled name and RTL for the instantiation. */
9220 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
9221 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9222 SET_DECL_RTL (r, NULL_RTX);
9223 /* The initializer must not be expanded until it is required;
9225 DECL_INITIAL (r) = NULL_TREE;
9226 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9227 SET_DECL_RTL (r, NULL_RTX);
9228 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
9229 if (TREE_CODE (r) == VAR_DECL)
9231 /* Possibly limit visibility based on template args. */
9232 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
9233 if (DECL_VISIBILITY_SPECIFIED (t))
9235 DECL_VISIBILITY_SPECIFIED (r) = 0;
9237 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9239 determine_visibility (r);
9241 /* Preserve a typedef that names a type. */
9242 else if (TREE_CODE (r) == TYPE_DECL
9243 && DECL_ORIGINAL_TYPE (t)
9244 && type != error_mark_node)
9246 DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t),
9247 args, complain, in_decl);
9248 TREE_TYPE (r) = type = build_variant_type_copy (type);
9249 TYPE_NAME (type) = r;
9254 /* A static data member declaration is always marked
9255 external when it is declared in-class, even if an
9256 initializer is present. We mimic the non-template
9258 DECL_EXTERNAL (r) = 1;
9260 register_specialization (r, gen_tmpl, argvec, false, hash);
9261 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
9262 SET_DECL_IMPLICIT_INSTANTIATION (r);
9264 else if (cp_unevaluated_operand)
9266 /* We're substituting this var in a decltype outside of its
9267 scope, such as for a lambda return type. Don't add it to
9268 local_specializations, do perform auto deduction. */
9269 tree auto_node = type_uses_auto (type);
9271 = tsubst_expr (DECL_INITIAL (t), args, complain, in_decl,
9272 /*constant_expression_p=*/false);
9274 if (auto_node && init && describable_type (init))
9276 type = do_auto_deduction (type, init, auto_node);
9277 TREE_TYPE (r) = type;
9281 register_local_specialization (r, t);
9283 TREE_CHAIN (r) = NULL_TREE;
9285 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9286 (int) ATTR_FLAG_TYPE_IN_PLACE,
9287 args, complain, in_decl);
9298 /* Restore the file and line information. */
9299 input_location = saved_loc;
9304 /* Substitute into the ARG_TYPES of a function type. */
9307 tsubst_arg_types (tree arg_types,
9309 tsubst_flags_t complain,
9312 tree remaining_arg_types;
9313 tree type = NULL_TREE;
9315 tree expanded_args = NULL_TREE;
9318 if (!arg_types || arg_types == void_list_node)
9321 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
9322 args, complain, in_decl);
9323 if (remaining_arg_types == error_mark_node)
9324 return error_mark_node;
9326 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
9328 /* For a pack expansion, perform substitution on the
9329 entire expression. Later on, we'll handle the arguments
9331 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9332 args, complain, in_decl);
9334 if (TREE_CODE (expanded_args) == TREE_VEC)
9335 /* So that we'll spin through the parameters, one by one. */
9336 i = TREE_VEC_LENGTH (expanded_args);
9339 /* We only partially substituted into the parameter
9340 pack. Our type is TYPE_PACK_EXPANSION. */
9341 type = expanded_args;
9342 expanded_args = NULL_TREE;
9350 type = TREE_VEC_ELT (expanded_args, i);
9352 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
9354 if (type == error_mark_node)
9355 return error_mark_node;
9356 if (VOID_TYPE_P (type))
9358 if (complain & tf_error)
9360 error ("invalid parameter type %qT", type);
9362 error ("in declaration %q+D", in_decl);
9364 return error_mark_node;
9367 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9368 top-level qualifiers as required. */
9369 type = TYPE_MAIN_VARIANT (type_decays_to (type));
9371 /* We do not substitute into default arguments here. The standard
9372 mandates that they be instantiated only when needed, which is
9373 done in build_over_call. */
9374 default_arg = TREE_PURPOSE (arg_types);
9376 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9378 /* We've instantiated a template before its default arguments
9379 have been parsed. This can happen for a nested template
9380 class, and is not an error unless we require the default
9381 argument in a call of this function. */
9382 remaining_arg_types =
9383 tree_cons (default_arg, type, remaining_arg_types);
9384 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9385 remaining_arg_types);
9388 remaining_arg_types =
9389 hash_tree_cons (default_arg, type, remaining_arg_types);
9392 return remaining_arg_types;
9395 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9396 *not* handle the exception-specification for FNTYPE, because the
9397 initial substitution of explicitly provided template parameters
9398 during argument deduction forbids substitution into the
9399 exception-specification:
9403 All references in the function type of the function template to the
9404 corresponding template parameters are replaced by the specified tem-
9405 plate argument values. If a substitution in a template parameter or
9406 in the function type of the function template results in an invalid
9407 type, type deduction fails. [Note: The equivalent substitution in
9408 exception specifications is done only when the function is instanti-
9409 ated, at which point a program is ill-formed if the substitution
9410 results in an invalid type.] */
9413 tsubst_function_type (tree t,
9415 tsubst_flags_t complain,
9422 /* The TYPE_CONTEXT is not used for function/method types. */
9423 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
9425 /* Substitute the return type. */
9426 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9427 if (return_type == error_mark_node)
9428 return error_mark_node;
9429 /* The standard does not presently indicate that creation of a
9430 function type with an invalid return type is a deduction failure.
9431 However, that is clearly analogous to creating an array of "void"
9432 or a reference to a reference. This is core issue #486. */
9433 if (TREE_CODE (return_type) == ARRAY_TYPE
9434 || TREE_CODE (return_type) == FUNCTION_TYPE)
9436 if (complain & tf_error)
9438 if (TREE_CODE (return_type) == ARRAY_TYPE)
9439 error ("function returning an array");
9441 error ("function returning a function");
9443 return error_mark_node;
9446 /* Substitute the argument types. */
9447 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
9449 if (arg_types == error_mark_node)
9450 return error_mark_node;
9452 /* Construct a new type node and return it. */
9453 if (TREE_CODE (t) == FUNCTION_TYPE)
9454 fntype = build_function_type (return_type, arg_types);
9457 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9458 if (! MAYBE_CLASS_TYPE_P (r))
9462 Type deduction may fail for any of the following
9465 -- Attempting to create "pointer to member of T" when T
9466 is not a class type. */
9467 if (complain & tf_error)
9468 error ("creating pointer to member function of non-class type %qT",
9470 return error_mark_node;
9473 fntype = build_method_type_directly (r, return_type,
9474 TREE_CHAIN (arg_types));
9476 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
9477 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
9482 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9483 ARGS into that specification, and return the substituted
9484 specification. If there is no specification, return NULL_TREE. */
9487 tsubst_exception_specification (tree fntype,
9489 tsubst_flags_t complain,
9495 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9496 new_specs = NULL_TREE;
9499 if (! TREE_VALUE (specs))
9506 tree expanded_specs = NULL_TREE;
9508 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9510 /* Expand the pack expansion type. */
9511 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9515 if (expanded_specs == error_mark_node)
9516 return error_mark_node;
9517 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9518 len = TREE_VEC_LENGTH (expanded_specs);
9521 /* We're substituting into a member template, so
9522 we got a TYPE_PACK_EXPANSION back. Add that
9523 expansion and move on. */
9524 gcc_assert (TREE_CODE (expanded_specs)
9525 == TYPE_PACK_EXPANSION);
9526 new_specs = add_exception_specifier (new_specs,
9529 specs = TREE_CHAIN (specs);
9534 for (i = 0; i < len; ++i)
9537 spec = TREE_VEC_ELT (expanded_specs, i);
9539 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9540 if (spec == error_mark_node)
9542 new_specs = add_exception_specifier (new_specs, spec,
9546 specs = TREE_CHAIN (specs);
9552 /* Take the tree structure T and replace template parameters used
9553 therein with the argument vector ARGS. IN_DECL is an associated
9554 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
9555 Issue error and warning messages under control of COMPLAIN. Note
9556 that we must be relatively non-tolerant of extensions here, in
9557 order to preserve conformance; if we allow substitutions that
9558 should not be allowed, we may allow argument deductions that should
9559 not succeed, and therefore report ambiguous overload situations
9560 where there are none. In theory, we could allow the substitution,
9561 but indicate that it should have failed, and allow our caller to
9562 make sure that the right thing happens, but we don't try to do this
9565 This function is used for dealing with types, decls and the like;
9566 for expressions, use tsubst_expr or tsubst_copy. */
9569 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
9573 if (t == NULL_TREE || t == error_mark_node
9574 || t == integer_type_node
9575 || t == void_type_node
9576 || t == char_type_node
9577 || t == unknown_type_node
9578 || TREE_CODE (t) == NAMESPACE_DECL)
9582 return tsubst_decl (t, args, complain);
9584 if (args == NULL_TREE)
9587 if (TREE_CODE (t) == IDENTIFIER_NODE)
9588 type = IDENTIFIER_TYPE_VALUE (t);
9590 type = TREE_TYPE (t);
9592 gcc_assert (type != unknown_type_node);
9594 /* Reuse typedefs. We need to do this to handle dependent attributes,
9595 such as attribute aligned. */
9598 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
9600 tree decl = TYPE_NAME (t);
9602 if (DECL_CLASS_SCOPE_P (decl)
9603 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9604 && uses_template_parms (DECL_CONTEXT (decl)))
9606 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9607 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
9608 r = retrieve_specialization (tmpl, gen_args, 0);
9610 else if (DECL_FUNCTION_SCOPE_P (decl)
9611 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9612 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
9613 r = retrieve_local_specialization (decl);
9615 /* The typedef is from a non-template context. */
9621 r = cp_build_qualified_type_real
9622 (r, cp_type_quals (t) | cp_type_quals (r),
9623 complain | tf_ignore_bad_quals);
9626 /* Else we must be instantiating the typedef, so fall through. */
9630 && TREE_CODE (t) != TYPENAME_TYPE
9631 && TREE_CODE (t) != IDENTIFIER_NODE
9632 && TREE_CODE (t) != FUNCTION_TYPE
9633 && TREE_CODE (t) != METHOD_TYPE)
9634 type = tsubst (type, args, complain, in_decl);
9635 if (type == error_mark_node)
9636 return error_mark_node;
9638 switch (TREE_CODE (t))
9643 return tsubst_aggr_type (t, args, complain, in_decl,
9644 /*entering_scope=*/0);
9647 case IDENTIFIER_NODE:
9659 if (t == integer_type_node)
9662 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9663 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9667 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
9669 max = tsubst_expr (omax, args, complain, in_decl,
9670 /*integral_constant_expression_p=*/false);
9672 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9674 if (TREE_CODE (max) == NOP_EXPR
9675 && TREE_SIDE_EFFECTS (omax)
9676 && !TREE_TYPE (max))
9677 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
9679 max = fold_decl_constant_value (max);
9681 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9682 with TREE_SIDE_EFFECTS that indicates this is not an integral
9683 constant expression. */
9684 if (processing_template_decl
9685 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
9687 gcc_assert (TREE_CODE (max) == NOP_EXPR);
9688 TREE_SIDE_EFFECTS (max) = 1;
9691 if (TREE_CODE (max) != INTEGER_CST
9692 && !at_function_scope_p ()
9693 && !TREE_SIDE_EFFECTS (max)
9694 && !value_dependent_expression_p (max))
9696 if (complain & tf_error)
9697 error ("array bound is not an integer constant");
9698 return error_mark_node;
9703 Type deduction may fail for any of the following
9706 Attempting to create an array with a size that is
9707 zero or negative. */
9708 if (integer_zerop (max) && !(complain & tf_error))
9709 /* We must fail if performing argument deduction (as
9710 indicated by the state of complain), so that
9711 another substitution can be found. */
9712 return error_mark_node;
9713 else if (TREE_CODE (max) == INTEGER_CST
9714 && INT_CST_LT (max, integer_zero_node))
9716 if (complain & tf_error)
9717 error ("creating array with negative size (%qE)", max);
9719 return error_mark_node;
9722 return compute_array_index_type (NULL_TREE, max);
9725 case TEMPLATE_TYPE_PARM:
9726 case TEMPLATE_TEMPLATE_PARM:
9727 case BOUND_TEMPLATE_TEMPLATE_PARM:
9728 case TEMPLATE_PARM_INDEX:
9733 tree arg = NULL_TREE;
9737 gcc_assert (TREE_VEC_LENGTH (args) > 0);
9738 template_parm_level_and_index (t, &level, &idx);
9740 levels = TMPL_ARGS_DEPTH (args);
9741 if (level <= levels)
9743 arg = TMPL_ARG (args, level, idx);
9745 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9746 /* See through ARGUMENT_PACK_SELECT arguments. */
9747 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9750 if (arg == error_mark_node)
9751 return error_mark_node;
9752 else if (arg != NULL_TREE)
9754 if (ARGUMENT_PACK_P (arg))
9755 /* If ARG is an argument pack, we don't actually want to
9756 perform a substitution here, because substitutions
9757 for argument packs are only done
9758 element-by-element. We can get to this point when
9759 substituting the type of a non-type template
9760 parameter pack, when that type actually contains
9761 template parameter packs from an outer template, e.g.,
9763 template<typename... Types> struct A {
9764 template<Types... Values> struct B { };
9768 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
9771 gcc_assert (TYPE_P (arg));
9773 /* cv-quals from the template are discarded when
9774 substituting in a function or reference type. */
9775 if (TREE_CODE (arg) == FUNCTION_TYPE
9776 || TREE_CODE (arg) == METHOD_TYPE
9777 || TREE_CODE (arg) == REFERENCE_TYPE)
9778 quals = cp_type_quals (arg);
9780 quals = cp_type_quals (arg) | cp_type_quals (t);
9782 return cp_build_qualified_type_real
9783 (arg, quals, complain | tf_ignore_bad_quals);
9785 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9787 /* We are processing a type constructed from a
9788 template template parameter. */
9789 tree argvec = tsubst (TYPE_TI_ARGS (t),
9790 args, complain, in_decl);
9791 if (argvec == error_mark_node)
9792 return error_mark_node;
9794 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9795 are resolving nested-types in the signature of a
9796 member function templates. Otherwise ARG is a
9797 TEMPLATE_DECL and is the real template to be
9799 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9800 arg = TYPE_NAME (arg);
9802 r = lookup_template_class (arg,
9805 /*entering_scope=*/0,
9807 return cp_build_qualified_type_real
9808 (r, TYPE_QUALS (t), complain);
9811 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9816 /* This can happen during the attempted tsubst'ing in
9817 unify. This means that we don't yet have any information
9818 about the template parameter in question. */
9821 /* If we get here, we must have been looking at a parm for a
9822 more deeply nested template. Make a new version of this
9823 template parameter, but with a lower level. */
9824 switch (TREE_CODE (t))
9826 case TEMPLATE_TYPE_PARM:
9827 case TEMPLATE_TEMPLATE_PARM:
9828 case BOUND_TEMPLATE_TEMPLATE_PARM:
9829 if (cp_type_quals (t))
9831 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
9832 r = cp_build_qualified_type_real
9833 (r, cp_type_quals (t),
9834 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9835 ? tf_ignore_bad_quals : 0));
9840 TEMPLATE_TYPE_PARM_INDEX (r)
9841 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
9842 r, levels, args, complain);
9843 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9844 TYPE_MAIN_VARIANT (r) = r;
9845 TYPE_POINTER_TO (r) = NULL_TREE;
9846 TYPE_REFERENCE_TO (r) = NULL_TREE;
9848 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9849 /* We have reduced the level of the template
9850 template parameter, but not the levels of its
9851 template parameters, so canonical_type_parameter
9852 will not be able to find the canonical template
9853 template parameter for this level. Thus, we
9854 require structural equality checking to compare
9855 TEMPLATE_TEMPLATE_PARMs. */
9856 SET_TYPE_STRUCTURAL_EQUALITY (r);
9857 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9858 SET_TYPE_STRUCTURAL_EQUALITY (r);
9860 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9862 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9864 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
9866 if (argvec == error_mark_node)
9867 return error_mark_node;
9869 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9870 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
9875 case TEMPLATE_PARM_INDEX:
9876 r = reduce_template_parm_level (t, type, levels, args, complain);
9888 tree purpose, value, chain;
9890 if (t == void_list_node)
9893 purpose = TREE_PURPOSE (t);
9896 purpose = tsubst (purpose, args, complain, in_decl);
9897 if (purpose == error_mark_node)
9898 return error_mark_node;
9900 value = TREE_VALUE (t);
9903 value = tsubst (value, args, complain, in_decl);
9904 if (value == error_mark_node)
9905 return error_mark_node;
9907 chain = TREE_CHAIN (t);
9908 if (chain && chain != void_type_node)
9910 chain = tsubst (chain, args, complain, in_decl);
9911 if (chain == error_mark_node)
9912 return error_mark_node;
9914 if (purpose == TREE_PURPOSE (t)
9915 && value == TREE_VALUE (t)
9916 && chain == TREE_CHAIN (t))
9918 return hash_tree_cons (purpose, value, chain);
9922 /* We should never be tsubsting a binfo. */
9926 /* A vector of template arguments. */
9928 return tsubst_template_args (t, args, complain, in_decl);
9931 case REFERENCE_TYPE:
9933 enum tree_code code;
9935 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
9938 code = TREE_CODE (t);
9943 Type deduction may fail for any of the following
9946 -- Attempting to create a pointer to reference type.
9947 -- Attempting to create a reference to a reference type or
9948 a reference to void.
9950 Core issue 106 says that creating a reference to a reference
9951 during instantiation is no longer a cause for failure. We
9952 only enforce this check in strict C++98 mode. */
9953 if ((TREE_CODE (type) == REFERENCE_TYPE
9954 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
9955 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
9957 static location_t last_loc;
9959 /* We keep track of the last time we issued this error
9960 message to avoid spewing a ton of messages during a
9961 single bad template instantiation. */
9962 if (complain & tf_error
9963 && last_loc != input_location)
9965 if (TREE_CODE (type) == VOID_TYPE)
9966 error ("forming reference to void");
9968 error ("forming %s to reference type %qT",
9969 (code == POINTER_TYPE) ? "pointer" : "reference",
9971 last_loc = input_location;
9974 return error_mark_node;
9976 else if (code == POINTER_TYPE)
9978 r = build_pointer_type (type);
9979 if (TREE_CODE (type) == METHOD_TYPE)
9980 r = build_ptrmemfunc_type (r);
9982 else if (TREE_CODE (type) == REFERENCE_TYPE)
9983 /* In C++0x, during template argument substitution, when there is an
9984 attempt to create a reference to a reference type, reference
9985 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
9987 "If a template-argument for a template-parameter T names a type
9988 that is a reference to a type A, an attempt to create the type
9989 'lvalue reference to cv T' creates the type 'lvalue reference to
9990 A,' while an attempt to create the type type rvalue reference to
9991 cv T' creates the type T"
9993 r = cp_build_reference_type
9995 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
9997 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
9998 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
10000 if (r != error_mark_node)
10001 /* Will this ever be needed for TYPE_..._TO values? */
10008 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
10009 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
10013 Type deduction may fail for any of the following
10016 -- Attempting to create "pointer to member of T" when T
10017 is not a class type. */
10018 if (complain & tf_error)
10019 error ("creating pointer to member of non-class type %qT", r);
10020 return error_mark_node;
10022 if (TREE_CODE (type) == REFERENCE_TYPE)
10024 if (complain & tf_error)
10025 error ("creating pointer to member reference type %qT", type);
10026 return error_mark_node;
10028 if (TREE_CODE (type) == VOID_TYPE)
10030 if (complain & tf_error)
10031 error ("creating pointer to member of type void");
10032 return error_mark_node;
10034 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10035 if (TREE_CODE (type) == FUNCTION_TYPE)
10037 /* The type of the implicit object parameter gets its
10038 cv-qualifiers from the FUNCTION_TYPE. */
10040 tree method_type = build_memfn_type (type, r, cp_type_quals (type));
10041 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
10042 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
10046 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
10050 case FUNCTION_TYPE:
10055 fntype = tsubst_function_type (t, args, complain, in_decl);
10056 if (fntype == error_mark_node)
10057 return error_mark_node;
10059 /* Substitute the exception specification. */
10060 specs = tsubst_exception_specification (t, args, complain,
10062 if (specs == error_mark_node)
10063 return error_mark_node;
10065 fntype = build_exception_variant (fntype, specs);
10070 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
10071 if (domain == error_mark_node)
10072 return error_mark_node;
10074 /* As an optimization, we avoid regenerating the array type if
10075 it will obviously be the same as T. */
10076 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
10079 /* These checks should match the ones in grokdeclarator.
10083 The deduction may fail for any of the following reasons:
10085 -- Attempting to create an array with an element type that
10086 is void, a function type, or a reference type, or [DR337]
10087 an abstract class type. */
10088 if (TREE_CODE (type) == VOID_TYPE
10089 || TREE_CODE (type) == FUNCTION_TYPE
10090 || TREE_CODE (type) == REFERENCE_TYPE)
10092 if (complain & tf_error)
10093 error ("creating array of %qT", type);
10094 return error_mark_node;
10096 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
10098 if (complain & tf_error)
10099 error ("creating array of %qT, which is an abstract class type",
10101 return error_mark_node;
10104 r = build_cplus_array_type (type, domain);
10106 if (TYPE_USER_ALIGN (t))
10108 TYPE_ALIGN (r) = TYPE_ALIGN (t);
10109 TYPE_USER_ALIGN (r) = 1;
10118 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10119 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10121 if (e1 == error_mark_node || e2 == error_mark_node)
10122 return error_mark_node;
10124 return fold_build2_loc (input_location,
10125 TREE_CODE (t), TREE_TYPE (t), e1, e2);
10131 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10132 if (e == error_mark_node)
10133 return error_mark_node;
10135 return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
10138 case TYPENAME_TYPE:
10140 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10141 in_decl, /*entering_scope=*/1);
10142 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
10143 complain, in_decl);
10145 if (ctx == error_mark_node || f == error_mark_node)
10146 return error_mark_node;
10148 if (!MAYBE_CLASS_TYPE_P (ctx))
10150 if (complain & tf_error)
10151 error ("%qT is not a class, struct, or union type", ctx);
10152 return error_mark_node;
10154 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
10156 /* Normally, make_typename_type does not require that the CTX
10157 have complete type in order to allow things like:
10159 template <class T> struct S { typename S<T>::X Y; };
10161 But, such constructs have already been resolved by this
10162 point, so here CTX really should have complete type, unless
10163 it's a partial instantiation. */
10164 if (!(complain & tf_no_class_instantiations))
10165 ctx = complete_type (ctx);
10166 if (!COMPLETE_TYPE_P (ctx))
10168 if (complain & tf_error)
10169 cxx_incomplete_type_error (NULL_TREE, ctx);
10170 return error_mark_node;
10174 f = make_typename_type (ctx, f, typename_type,
10175 (complain & tf_error) | tf_keep_type_decl);
10176 if (f == error_mark_node)
10178 if (TREE_CODE (f) == TYPE_DECL)
10180 complain |= tf_ignore_bad_quals;
10184 if (TREE_CODE (f) != TYPENAME_TYPE)
10186 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
10187 error ("%qT resolves to %qT, which is not an enumeration type",
10189 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
10190 error ("%qT resolves to %qT, which is is not a class type",
10194 return cp_build_qualified_type_real
10195 (f, cp_type_quals (f) | cp_type_quals (t), complain);
10198 case UNBOUND_CLASS_TEMPLATE:
10200 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10201 in_decl, /*entering_scope=*/1);
10202 tree name = TYPE_IDENTIFIER (t);
10203 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
10205 if (ctx == error_mark_node || name == error_mark_node)
10206 return error_mark_node;
10209 parm_list = tsubst_template_parms (parm_list, args, complain);
10210 return make_unbound_class_template (ctx, name, parm_list, complain);
10216 gcc_unreachable ();
10220 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10221 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10222 /*integral_constant_expression_p=*/false);
10223 if (e1 == error_mark_node || e2 == error_mark_node)
10224 return error_mark_node;
10226 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
10231 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
10232 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10233 if (e1 == error_mark_node || e2 == error_mark_node)
10234 return error_mark_node;
10236 return build_qualified_name (/*type=*/NULL_TREE,
10237 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
10244 ++cp_unevaluated_operand;
10245 ++c_inhibit_evaluation_warnings;
10247 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
10249 /*integral_constant_expression_p=*/false);
10251 --cp_unevaluated_operand;
10252 --c_inhibit_evaluation_warnings;
10254 type = finish_typeof (type);
10255 return cp_build_qualified_type_real (type,
10257 | cp_type_quals (type),
10261 case DECLTYPE_TYPE:
10265 ++cp_unevaluated_operand;
10266 ++c_inhibit_evaluation_warnings;
10268 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10270 /*integral_constant_expression_p=*/false);
10272 --cp_unevaluated_operand;
10273 --c_inhibit_evaluation_warnings;
10275 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
10276 type = lambda_capture_field_type (type);
10277 else if (DECLTYPE_FOR_LAMBDA_RETURN (t))
10278 type = lambda_return_type (type);
10280 type = finish_decltype_type
10281 (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10282 return cp_build_qualified_type_real (type,
10284 | cp_type_quals (type),
10288 case TYPE_ARGUMENT_PACK:
10289 case NONTYPE_ARGUMENT_PACK:
10291 tree r = TYPE_P (t)
10292 ? cxx_make_type (TREE_CODE (t))
10293 : make_node (TREE_CODE (t));
10295 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10299 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10301 /* For template nontype argument packs, also substitute into
10303 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
10304 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10311 sorry ("use of %qs in template",
10312 tree_code_name [(int) TREE_CODE (t)]);
10313 return error_mark_node;
10317 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10318 type of the expression on the left-hand side of the "." or "->"
10322 tsubst_baselink (tree baselink, tree object_type,
10323 tree args, tsubst_flags_t complain, tree in_decl)
10326 tree qualifying_scope;
10329 tree template_args = 0;
10330 bool template_id_p = false;
10332 /* A baselink indicates a function from a base class. Both the
10333 BASELINK_ACCESS_BINFO and the base class referenced may
10334 indicate bases of the template class, rather than the
10335 instantiated class. In addition, lookups that were not
10336 ambiguous before may be ambiguous now. Therefore, we perform
10337 the lookup again. */
10338 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
10339 qualifying_scope = tsubst (qualifying_scope, args,
10340 complain, in_decl);
10341 fns = BASELINK_FUNCTIONS (baselink);
10342 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
10343 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10345 template_id_p = true;
10346 template_args = TREE_OPERAND (fns, 1);
10347 fns = TREE_OPERAND (fns, 0);
10349 template_args = tsubst_template_args (template_args, args,
10350 complain, in_decl);
10352 name = DECL_NAME (get_first_fn (fns));
10353 if (IDENTIFIER_TYPENAME_P (name))
10354 name = mangle_conv_op_name_for_type (optype);
10355 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
10357 /* If lookup found a single function, mark it as used at this
10358 point. (If it lookup found multiple functions the one selected
10359 later by overload resolution will be marked as used at that
10361 if (BASELINK_P (baselink))
10362 fns = BASELINK_FUNCTIONS (baselink);
10363 if (!template_id_p && !really_overloaded_fn (fns))
10364 mark_used (OVL_CURRENT (fns));
10366 /* Add back the template arguments, if present. */
10367 if (BASELINK_P (baselink) && template_id_p)
10368 BASELINK_FUNCTIONS (baselink)
10369 = build_nt (TEMPLATE_ID_EXPR,
10370 BASELINK_FUNCTIONS (baselink),
10372 /* Update the conversion operator type. */
10373 BASELINK_OPTYPE (baselink) = optype;
10376 object_type = current_class_type;
10377 return adjust_result_of_qualified_name_lookup (baselink,
10382 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10383 true if the qualified-id will be a postfix-expression in-and-of
10384 itself; false if more of the postfix-expression follows the
10385 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10389 tsubst_qualified_id (tree qualified_id, tree args,
10390 tsubst_flags_t complain, tree in_decl,
10391 bool done, bool address_p)
10397 tree template_args;
10399 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
10401 /* Figure out what name to look up. */
10402 name = TREE_OPERAND (qualified_id, 1);
10403 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10405 is_template = true;
10406 template_args = TREE_OPERAND (name, 1);
10408 template_args = tsubst_template_args (template_args, args,
10409 complain, in_decl);
10410 name = TREE_OPERAND (name, 0);
10414 is_template = false;
10415 template_args = NULL_TREE;
10418 /* Substitute into the qualifying scope. When there are no ARGS, we
10419 are just trying to simplify a non-dependent expression. In that
10420 case the qualifying scope may be dependent, and, in any case,
10421 substituting will not help. */
10422 scope = TREE_OPERAND (qualified_id, 0);
10425 scope = tsubst (scope, args, complain, in_decl);
10426 expr = tsubst_copy (name, args, complain, in_decl);
10431 if (dependent_type_p (scope))
10433 tree type = NULL_TREE;
10434 if (DECL_P (expr) && !dependent_scope_p (scope))
10435 type = TREE_TYPE (expr);
10436 return build_qualified_name (type, scope, expr,
10437 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10440 if (!BASELINK_P (name) && !DECL_P (expr))
10442 if (TREE_CODE (expr) == BIT_NOT_EXPR)
10444 /* A BIT_NOT_EXPR is used to represent a destructor. */
10445 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10447 error ("qualifying type %qT does not match destructor name ~%qT",
10448 scope, TREE_OPERAND (expr, 0));
10449 expr = error_mark_node;
10452 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10453 /*is_type_p=*/0, false);
10456 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
10457 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10458 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10460 if (complain & tf_error)
10462 error ("dependent-name %qE is parsed as a non-type, but "
10463 "instantiation yields a type", qualified_id);
10464 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
10466 return error_mark_node;
10472 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10474 /* Remember that there was a reference to this entity. */
10478 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10480 if (complain & tf_error)
10481 qualified_name_lookup_error (scope,
10482 TREE_OPERAND (qualified_id, 1),
10483 expr, input_location);
10484 return error_mark_node;
10488 expr = lookup_template_function (expr, template_args);
10490 if (expr == error_mark_node && complain & tf_error)
10491 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
10492 expr, input_location);
10493 else if (TYPE_P (scope))
10495 expr = (adjust_result_of_qualified_name_lookup
10496 (expr, scope, current_class_type));
10497 expr = (finish_qualified_id_expr
10498 (scope, expr, done, address_p,
10499 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10500 /*template_arg_p=*/false));
10503 /* Expressions do not generally have reference type. */
10504 if (TREE_CODE (expr) != SCOPE_REF
10505 /* However, if we're about to form a pointer-to-member, we just
10506 want the referenced member referenced. */
10507 && TREE_CODE (expr) != OFFSET_REF)
10508 expr = convert_from_reference (expr);
10513 /* Like tsubst, but deals with expressions. This function just replaces
10514 template parms; to finish processing the resultant expression, use
10518 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10520 enum tree_code code;
10523 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
10526 code = TREE_CODE (t);
10531 r = retrieve_local_specialization (t);
10536 /* This can happen for a parameter name used later in a function
10537 declaration (such as in a late-specified return type). Just
10538 make a dummy decl, since it's only used for its type. */
10539 gcc_assert (cp_unevaluated_operand != 0);
10540 /* We copy T because want to tsubst the PARM_DECL only,
10541 not the following PARM_DECLs that are chained to T. */
10543 r = tsubst_decl (c, args, complain);
10544 /* Give it the template pattern as its context; its true context
10545 hasn't been instantiated yet and this is good enough for
10547 DECL_CONTEXT (r) = DECL_CONTEXT (t);
10550 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10551 r = ARGUMENT_PACK_SELECT_ARG (r);
10560 if (DECL_TEMPLATE_PARM_P (t))
10561 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
10562 /* There is no need to substitute into namespace-scope
10564 if (DECL_NAMESPACE_SCOPE_P (t))
10566 /* If ARGS is NULL, then T is known to be non-dependent. */
10567 if (args == NULL_TREE)
10568 return integral_constant_value (t);
10570 /* Unfortunately, we cannot just call lookup_name here.
10573 template <int I> int f() {
10575 struct S { void g() { E e = a; } };
10578 When we instantiate f<7>::S::g(), say, lookup_name is not
10579 clever enough to find f<7>::a. */
10581 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
10582 /*entering_scope=*/0);
10584 for (v = TYPE_VALUES (enum_type);
10586 v = TREE_CHAIN (v))
10587 if (TREE_PURPOSE (v) == DECL_NAME (t))
10588 return TREE_VALUE (v);
10590 /* We didn't find the name. That should never happen; if
10591 name-lookup found it during preliminary parsing, we
10592 should find it again here during instantiation. */
10593 gcc_unreachable ();
10598 if (DECL_CONTEXT (t))
10602 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
10603 /*entering_scope=*/1);
10604 if (ctx != DECL_CONTEXT (t))
10606 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10609 if (complain & tf_error)
10610 error ("using invalid field %qD", t);
10611 return error_mark_node;
10620 case FUNCTION_DECL:
10621 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10622 || local_variable_p (t))
10623 t = tsubst (t, args, complain, in_decl);
10628 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
10630 case TEMPLATE_DECL:
10631 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10632 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
10633 args, complain, in_decl);
10634 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
10635 return tsubst (t, args, complain, in_decl);
10636 else if (DECL_CLASS_SCOPE_P (t)
10637 && uses_template_parms (DECL_CONTEXT (t)))
10639 /* Template template argument like the following example need
10642 template <template <class> class TT> struct C {};
10643 template <class T> struct D {
10644 template <class U> struct E {};
10649 We are processing the template argument `E' in #1 for
10650 the template instantiation #2. Originally, `E' is a
10651 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10652 have to substitute this with one having context `D<int>'. */
10654 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10655 return lookup_field (context, DECL_NAME(t), 0, false);
10658 /* Ordinary template template argument. */
10662 case REINTERPRET_CAST_EXPR:
10663 case CONST_CAST_EXPR:
10664 case STATIC_CAST_EXPR:
10665 case DYNAMIC_CAST_EXPR:
10668 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10669 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10672 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10674 /* We only want to compute the number of arguments. */
10675 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10676 complain, in_decl);
10679 if (TREE_CODE (expanded) == TREE_VEC)
10680 len = TREE_VEC_LENGTH (expanded);
10682 if (expanded == error_mark_node)
10683 return error_mark_node;
10684 else if (PACK_EXPANSION_P (expanded)
10685 || (TREE_CODE (expanded) == TREE_VEC
10687 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10689 if (TREE_CODE (expanded) == TREE_VEC)
10690 expanded = TREE_VEC_ELT (expanded, len - 1);
10692 if (TYPE_P (expanded))
10693 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10694 complain & tf_error);
10696 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10697 complain & tf_error);
10700 return build_int_cst (size_type_node, len);
10706 case TRUTH_NOT_EXPR:
10709 case UNARY_PLUS_EXPR: /* Unary + */
10714 case REALPART_EXPR:
10715 case IMAGPART_EXPR:
10717 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10718 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
10720 case COMPONENT_REF:
10725 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10726 name = TREE_OPERAND (t, 1);
10727 if (TREE_CODE (name) == BIT_NOT_EXPR)
10729 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10730 complain, in_decl);
10731 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10733 else if (TREE_CODE (name) == SCOPE_REF
10734 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10736 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10737 complain, in_decl);
10738 name = TREE_OPERAND (name, 1);
10739 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10740 complain, in_decl);
10741 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10742 name = build_qualified_name (/*type=*/NULL_TREE,
10744 /*template_p=*/false);
10746 else if (TREE_CODE (name) == BASELINK)
10747 name = tsubst_baselink (name,
10748 non_reference (TREE_TYPE (object)),
10752 name = tsubst_copy (name, args, complain, in_decl);
10753 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
10759 case TRUNC_DIV_EXPR:
10760 case CEIL_DIV_EXPR:
10761 case FLOOR_DIV_EXPR:
10762 case ROUND_DIV_EXPR:
10763 case EXACT_DIV_EXPR:
10767 case TRUNC_MOD_EXPR:
10768 case FLOOR_MOD_EXPR:
10769 case TRUTH_ANDIF_EXPR:
10770 case TRUTH_ORIF_EXPR:
10771 case TRUTH_AND_EXPR:
10772 case TRUTH_OR_EXPR:
10785 case COMPOUND_EXPR:
10788 case PREDECREMENT_EXPR:
10789 case PREINCREMENT_EXPR:
10790 case POSTDECREMENT_EXPR:
10791 case POSTINCREMENT_EXPR:
10793 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10794 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10797 return build_qualified_name (/*type=*/NULL_TREE,
10798 tsubst_copy (TREE_OPERAND (t, 0),
10799 args, complain, in_decl),
10800 tsubst_copy (TREE_OPERAND (t, 1),
10801 args, complain, in_decl),
10802 QUALIFIED_NAME_IS_TEMPLATE (t));
10807 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10808 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10809 NULL_TREE, NULL_TREE);
10813 int n = VL_EXP_OPERAND_LENGTH (t);
10814 tree result = build_vl_exp (CALL_EXPR, n);
10816 for (i = 0; i < n; i++)
10817 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10818 complain, in_decl);
10824 case PSEUDO_DTOR_EXPR:
10827 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10828 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10829 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10830 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
10837 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10838 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10839 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
10840 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10847 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10848 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
10849 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10850 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10854 case TEMPLATE_ID_EXPR:
10856 /* Substituted template arguments */
10857 tree fn = TREE_OPERAND (t, 0);
10858 tree targs = TREE_OPERAND (t, 1);
10860 fn = tsubst_copy (fn, args, complain, in_decl);
10862 targs = tsubst_template_args (targs, args, complain, in_decl);
10864 return lookup_template_function (fn, targs);
10869 tree purpose, value, chain;
10871 if (t == void_list_node)
10874 purpose = TREE_PURPOSE (t);
10876 purpose = tsubst_copy (purpose, args, complain, in_decl);
10877 value = TREE_VALUE (t);
10879 value = tsubst_copy (value, args, complain, in_decl);
10880 chain = TREE_CHAIN (t);
10881 if (chain && chain != void_type_node)
10882 chain = tsubst_copy (chain, args, complain, in_decl);
10883 if (purpose == TREE_PURPOSE (t)
10884 && value == TREE_VALUE (t)
10885 && chain == TREE_CHAIN (t))
10887 return tree_cons (purpose, value, chain);
10892 case ENUMERAL_TYPE:
10894 case TEMPLATE_TYPE_PARM:
10895 case TEMPLATE_TEMPLATE_PARM:
10896 case BOUND_TEMPLATE_TEMPLATE_PARM:
10897 case TEMPLATE_PARM_INDEX:
10899 case REFERENCE_TYPE:
10901 case FUNCTION_TYPE:
10904 case TYPENAME_TYPE:
10905 case UNBOUND_CLASS_TEMPLATE:
10907 case DECLTYPE_TYPE:
10909 return tsubst (t, args, complain, in_decl);
10911 case IDENTIFIER_NODE:
10912 if (IDENTIFIER_TYPENAME_P (t))
10914 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
10915 return mangle_conv_op_name_for_type (new_type);
10921 /* This is handled by tsubst_copy_and_build. */
10922 gcc_unreachable ();
10925 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
10927 tsubst (TREE_TYPE (t), args, complain, in_decl));
10929 case CLEANUP_POINT_EXPR:
10930 /* We shouldn't have built any of these during initial template
10931 generation. Instead, they should be built during instantiation
10932 in response to the saved STMT_IS_FULL_EXPR_P setting. */
10933 gcc_unreachable ();
10936 mark_used (TREE_OPERAND (t, 1));
10939 case EXPR_PACK_EXPANSION:
10940 error ("invalid use of pack expansion expression");
10941 return error_mark_node;
10943 case NONTYPE_ARGUMENT_PACK:
10944 error ("use %<...%> to expand argument pack");
10945 return error_mark_node;
10952 /* Like tsubst_copy, but specifically for OpenMP clauses. */
10955 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
10958 tree new_clauses = NULL, nc, oc;
10960 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
10962 nc = copy_node (oc);
10963 OMP_CLAUSE_CHAIN (nc) = new_clauses;
10966 switch (OMP_CLAUSE_CODE (nc))
10968 case OMP_CLAUSE_LASTPRIVATE:
10969 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
10971 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
10972 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
10973 in_decl, /*integral_constant_expression_p=*/false);
10974 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
10975 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
10978 case OMP_CLAUSE_PRIVATE:
10979 case OMP_CLAUSE_SHARED:
10980 case OMP_CLAUSE_FIRSTPRIVATE:
10981 case OMP_CLAUSE_REDUCTION:
10982 case OMP_CLAUSE_COPYIN:
10983 case OMP_CLAUSE_COPYPRIVATE:
10984 case OMP_CLAUSE_IF:
10985 case OMP_CLAUSE_NUM_THREADS:
10986 case OMP_CLAUSE_SCHEDULE:
10987 case OMP_CLAUSE_COLLAPSE:
10988 OMP_CLAUSE_OPERAND (nc, 0)
10989 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
10990 in_decl, /*integral_constant_expression_p=*/false);
10992 case OMP_CLAUSE_NOWAIT:
10993 case OMP_CLAUSE_ORDERED:
10994 case OMP_CLAUSE_DEFAULT:
10995 case OMP_CLAUSE_UNTIED:
10998 gcc_unreachable ();
11002 return finish_omp_clauses (nreverse (new_clauses));
11005 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11008 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
11011 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11013 tree purpose, value, chain;
11018 if (TREE_CODE (t) != TREE_LIST)
11019 return tsubst_copy_and_build (t, args, complain, in_decl,
11020 /*function_p=*/false,
11021 /*integral_constant_expression_p=*/false);
11023 if (t == void_list_node)
11026 purpose = TREE_PURPOSE (t);
11028 purpose = RECUR (purpose);
11029 value = TREE_VALUE (t);
11030 if (value && TREE_CODE (value) != LABEL_DECL)
11031 value = RECUR (value);
11032 chain = TREE_CHAIN (t);
11033 if (chain && chain != void_type_node)
11034 chain = RECUR (chain);
11035 return tree_cons (purpose, value, chain);
11039 /* Substitute one OMP_FOR iterator. */
11042 tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
11043 tree condv, tree incrv, tree *clauses,
11044 tree args, tsubst_flags_t complain, tree in_decl,
11045 bool integral_constant_expression_p)
11047 #define RECUR(NODE) \
11048 tsubst_expr ((NODE), args, complain, in_decl, \
11049 integral_constant_expression_p)
11050 tree decl, init, cond, incr, auto_node;
11052 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
11053 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
11054 decl = RECUR (TREE_OPERAND (init, 0));
11055 init = TREE_OPERAND (init, 1);
11056 auto_node = type_uses_auto (TREE_TYPE (decl));
11057 if (auto_node && init)
11059 tree init_expr = init;
11060 if (TREE_CODE (init_expr) == DECL_EXPR)
11061 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
11062 init_expr = RECUR (init_expr);
11064 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
11066 gcc_assert (!type_dependent_expression_p (decl));
11068 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
11070 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
11071 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11072 if (TREE_CODE (incr) == MODIFY_EXPR)
11073 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
11074 RECUR (TREE_OPERAND (incr, 1)),
11077 incr = RECUR (incr);
11078 TREE_VEC_ELT (declv, i) = decl;
11079 TREE_VEC_ELT (initv, i) = init;
11080 TREE_VEC_ELT (condv, i) = cond;
11081 TREE_VEC_ELT (incrv, i) = incr;
11085 if (init && TREE_CODE (init) != DECL_EXPR)
11088 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
11090 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
11091 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
11092 && OMP_CLAUSE_DECL (c) == decl)
11094 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
11095 && OMP_CLAUSE_DECL (c) == decl)
11096 error ("iteration variable %qD should not be firstprivate", decl);
11097 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
11098 && OMP_CLAUSE_DECL (c) == decl)
11099 error ("iteration variable %qD should not be reduction", decl);
11103 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
11104 OMP_CLAUSE_DECL (c) = decl;
11105 c = finish_omp_clauses (c);
11108 OMP_CLAUSE_CHAIN (c) = *clauses;
11113 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
11114 if (COMPARISON_CLASS_P (cond))
11115 cond = build2 (TREE_CODE (cond), boolean_type_node,
11116 RECUR (TREE_OPERAND (cond, 0)),
11117 RECUR (TREE_OPERAND (cond, 1)));
11119 cond = RECUR (cond);
11120 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
11121 switch (TREE_CODE (incr))
11123 case PREINCREMENT_EXPR:
11124 case PREDECREMENT_EXPR:
11125 case POSTINCREMENT_EXPR:
11126 case POSTDECREMENT_EXPR:
11127 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
11128 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
11131 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11132 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11134 tree rhs = TREE_OPERAND (incr, 1);
11135 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11136 RECUR (TREE_OPERAND (incr, 0)),
11137 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11138 RECUR (TREE_OPERAND (rhs, 0)),
11139 RECUR (TREE_OPERAND (rhs, 1))));
11142 incr = RECUR (incr);
11145 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
11146 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
11148 tree lhs = RECUR (TREE_OPERAND (incr, 0));
11149 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
11150 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
11151 TREE_TYPE (decl), lhs,
11152 RECUR (TREE_OPERAND (incr, 2))));
11154 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
11155 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
11156 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
11158 tree rhs = TREE_OPERAND (incr, 2);
11159 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
11160 RECUR (TREE_OPERAND (incr, 0)),
11161 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
11162 RECUR (TREE_OPERAND (rhs, 0)),
11163 RECUR (TREE_OPERAND (rhs, 1))));
11166 incr = RECUR (incr);
11169 incr = RECUR (incr);
11173 TREE_VEC_ELT (declv, i) = decl;
11174 TREE_VEC_ELT (initv, i) = init;
11175 TREE_VEC_ELT (condv, i) = cond;
11176 TREE_VEC_ELT (incrv, i) = incr;
11180 /* Like tsubst_copy for expressions, etc. but also does semantic
11184 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
11185 bool integral_constant_expression_p)
11187 #define RECUR(NODE) \
11188 tsubst_expr ((NODE), args, complain, in_decl, \
11189 integral_constant_expression_p)
11193 if (t == NULL_TREE || t == error_mark_node)
11196 if (EXPR_HAS_LOCATION (t))
11197 input_location = EXPR_LOCATION (t);
11198 if (STATEMENT_CODE_P (TREE_CODE (t)))
11199 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
11201 switch (TREE_CODE (t))
11203 case STATEMENT_LIST:
11205 tree_stmt_iterator i;
11206 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
11207 RECUR (tsi_stmt (i));
11211 case CTOR_INITIALIZER:
11212 finish_mem_initializers (tsubst_initializer_list
11213 (TREE_OPERAND (t, 0), args));
11217 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
11221 tmp = RECUR (EXPR_STMT_EXPR (t));
11222 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11223 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11225 finish_expr_stmt (tmp);
11229 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
11237 decl = DECL_EXPR_DECL (t);
11238 if (TREE_CODE (decl) == LABEL_DECL)
11239 finish_label_decl (DECL_NAME (decl));
11240 else if (TREE_CODE (decl) == USING_DECL)
11242 tree scope = USING_DECL_SCOPE (decl);
11243 tree name = DECL_NAME (decl);
11246 scope = RECUR (scope);
11247 decl = lookup_qualified_name (scope, name,
11248 /*is_type_p=*/false,
11249 /*complain=*/false);
11250 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
11251 qualified_name_lookup_error (scope, name, decl, input_location);
11253 do_local_using_decl (decl, scope, name);
11257 init = DECL_INITIAL (decl);
11258 decl = tsubst (decl, args, complain, in_decl);
11259 if (decl != error_mark_node)
11261 /* By marking the declaration as instantiated, we avoid
11262 trying to instantiate it. Since instantiate_decl can't
11263 handle local variables, and since we've already done
11264 all that needs to be done, that's the right thing to
11266 if (TREE_CODE (decl) == VAR_DECL)
11267 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11268 if (TREE_CODE (decl) == VAR_DECL
11269 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11270 /* Anonymous aggregates are a special case. */
11271 finish_anon_union (decl);
11274 maybe_push_decl (decl);
11275 if (TREE_CODE (decl) == VAR_DECL
11276 && DECL_PRETTY_FUNCTION_P (decl))
11278 /* For __PRETTY_FUNCTION__ we have to adjust the
11280 const char *const name
11281 = cxx_printable_name (current_function_decl, 2);
11282 init = cp_fname_init (name, &TREE_TYPE (decl));
11286 tree t = RECUR (init);
11289 /* If we had an initializer but it
11290 instantiated to nothing,
11291 value-initialize the object. This will
11292 only occur when the initializer was a
11293 pack expansion where the parameter packs
11294 used in that expansion were of length
11296 init = build_value_init (TREE_TYPE (decl));
11301 cp_finish_decl (decl, init, false, NULL_TREE, 0);
11306 /* A DECL_EXPR can also be used as an expression, in the condition
11307 clause of an if/for/while construct. */
11312 stmt = begin_for_stmt ();
11313 RECUR (FOR_INIT_STMT (t));
11314 finish_for_init_stmt (stmt);
11315 tmp = RECUR (FOR_COND (t));
11316 finish_for_cond (tmp, stmt);
11317 tmp = RECUR (FOR_EXPR (t));
11318 finish_for_expr (tmp, stmt);
11319 RECUR (FOR_BODY (t));
11320 finish_for_stmt (stmt);
11324 stmt = begin_while_stmt ();
11325 tmp = RECUR (WHILE_COND (t));
11326 finish_while_stmt_cond (tmp, stmt);
11327 RECUR (WHILE_BODY (t));
11328 finish_while_stmt (stmt);
11332 stmt = begin_do_stmt ();
11333 RECUR (DO_BODY (t));
11334 finish_do_body (stmt);
11335 tmp = RECUR (DO_COND (t));
11336 finish_do_stmt (tmp, stmt);
11340 stmt = begin_if_stmt ();
11341 tmp = RECUR (IF_COND (t));
11342 finish_if_stmt_cond (tmp, stmt);
11343 RECUR (THEN_CLAUSE (t));
11344 finish_then_clause (stmt);
11346 if (ELSE_CLAUSE (t))
11348 begin_else_clause (stmt);
11349 RECUR (ELSE_CLAUSE (t));
11350 finish_else_clause (stmt);
11353 finish_if_stmt (stmt);
11357 if (BIND_EXPR_BODY_BLOCK (t))
11358 stmt = begin_function_body ();
11360 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11361 ? BCS_TRY_BLOCK : 0);
11363 RECUR (BIND_EXPR_BODY (t));
11365 if (BIND_EXPR_BODY_BLOCK (t))
11366 finish_function_body (stmt);
11368 finish_compound_stmt (stmt);
11372 finish_break_stmt ();
11375 case CONTINUE_STMT:
11376 finish_continue_stmt ();
11380 stmt = begin_switch_stmt ();
11381 tmp = RECUR (SWITCH_STMT_COND (t));
11382 finish_switch_cond (tmp, stmt);
11383 RECUR (SWITCH_STMT_BODY (t));
11384 finish_switch_stmt (stmt);
11387 case CASE_LABEL_EXPR:
11388 finish_case_label (EXPR_LOCATION (t),
11389 RECUR (CASE_LOW (t)),
11390 RECUR (CASE_HIGH (t)));
11395 tree decl = LABEL_EXPR_LABEL (t);
11398 label = finish_label_stmt (DECL_NAME (decl));
11399 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11400 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11405 tmp = GOTO_DESTINATION (t);
11406 if (TREE_CODE (tmp) != LABEL_DECL)
11407 /* Computed goto's must be tsubst'd into. On the other hand,
11408 non-computed gotos must not be; the identifier in question
11409 will have no binding. */
11412 tmp = DECL_NAME (tmp);
11413 finish_goto_stmt (tmp);
11417 tmp = finish_asm_stmt
11418 (ASM_VOLATILE_P (t),
11419 RECUR (ASM_STRING (t)),
11420 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11421 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11422 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl),
11423 tsubst_copy_asm_operands (ASM_LABELS (t), args, complain, in_decl));
11425 tree asm_expr = tmp;
11426 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11427 asm_expr = TREE_OPERAND (asm_expr, 0);
11428 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11435 stmt = begin_try_block ();
11436 RECUR (TRY_STMTS (t));
11437 finish_cleanup_try_block (stmt);
11438 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
11442 tree compound_stmt = NULL_TREE;
11444 if (FN_TRY_BLOCK_P (t))
11445 stmt = begin_function_try_block (&compound_stmt);
11447 stmt = begin_try_block ();
11449 RECUR (TRY_STMTS (t));
11451 if (FN_TRY_BLOCK_P (t))
11452 finish_function_try_block (stmt);
11454 finish_try_block (stmt);
11456 RECUR (TRY_HANDLERS (t));
11457 if (FN_TRY_BLOCK_P (t))
11458 finish_function_handler_sequence (stmt, compound_stmt);
11460 finish_handler_sequence (stmt);
11466 tree decl = HANDLER_PARMS (t);
11470 decl = tsubst (decl, args, complain, in_decl);
11471 /* Prevent instantiate_decl from trying to instantiate
11472 this variable. We've already done all that needs to be
11474 if (decl != error_mark_node)
11475 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
11477 stmt = begin_handler ();
11478 finish_handler_parms (decl, stmt);
11479 RECUR (HANDLER_BODY (t));
11480 finish_handler (stmt);
11485 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
11488 case STATIC_ASSERT:
11491 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11494 /*integral_constant_expression_p=*/true);
11495 finish_static_assert (condition,
11496 STATIC_ASSERT_MESSAGE (t),
11497 STATIC_ASSERT_SOURCE_LOCATION (t),
11498 /*member_p=*/false);
11503 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11504 args, complain, in_decl);
11505 stmt = begin_omp_parallel ();
11506 RECUR (OMP_PARALLEL_BODY (t));
11507 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11508 = OMP_PARALLEL_COMBINED (t);
11512 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11513 args, complain, in_decl);
11514 stmt = begin_omp_task ();
11515 RECUR (OMP_TASK_BODY (t));
11516 finish_omp_task (tmp, stmt);
11521 tree clauses, body, pre_body;
11522 tree declv, initv, condv, incrv;
11525 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11526 args, complain, in_decl);
11527 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11528 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11529 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11530 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11532 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11533 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11534 &clauses, args, complain, in_decl,
11535 integral_constant_expression_p);
11537 stmt = begin_omp_structured_block ();
11539 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11540 if (TREE_VEC_ELT (initv, i) == NULL
11541 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11542 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11543 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11545 tree init = RECUR (TREE_VEC_ELT (initv, i));
11546 gcc_assert (init == TREE_VEC_ELT (declv, i));
11547 TREE_VEC_ELT (initv, i) = NULL_TREE;
11551 tree decl_expr = TREE_VEC_ELT (initv, i);
11552 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11553 gcc_assert (init != NULL);
11554 TREE_VEC_ELT (initv, i) = RECUR (init);
11555 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11557 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11560 pre_body = push_stmt_list ();
11561 RECUR (OMP_FOR_PRE_BODY (t));
11562 pre_body = pop_stmt_list (pre_body);
11564 body = push_stmt_list ();
11565 RECUR (OMP_FOR_BODY (t));
11566 body = pop_stmt_list (body);
11568 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11569 body, pre_body, clauses);
11571 add_stmt (finish_omp_structured_block (stmt));
11577 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11578 stmt = push_stmt_list ();
11579 RECUR (OMP_BODY (t));
11580 stmt = pop_stmt_list (stmt);
11583 OMP_BODY (t) = stmt;
11584 OMP_CLAUSES (t) = tmp;
11592 stmt = push_stmt_list ();
11593 RECUR (OMP_BODY (t));
11594 stmt = pop_stmt_list (stmt);
11597 OMP_BODY (t) = stmt;
11602 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11604 tree op1 = TREE_OPERAND (t, 1);
11605 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11606 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11607 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11611 case EXPR_PACK_EXPANSION:
11612 error ("invalid use of pack expansion expression");
11613 return error_mark_node;
11615 case NONTYPE_ARGUMENT_PACK:
11616 error ("use %<...%> to expand argument pack");
11617 return error_mark_node;
11620 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
11622 return tsubst_copy_and_build (t, args, complain, in_decl,
11623 /*function_p=*/false,
11624 integral_constant_expression_p);
11631 /* T is a postfix-expression that is not being used in a function
11632 call. Return the substituted version of T. */
11635 tsubst_non_call_postfix_expression (tree t, tree args,
11636 tsubst_flags_t complain,
11639 if (TREE_CODE (t) == SCOPE_REF)
11640 t = tsubst_qualified_id (t, args, complain, in_decl,
11641 /*done=*/false, /*address_p=*/false);
11643 t = tsubst_copy_and_build (t, args, complain, in_decl,
11644 /*function_p=*/false,
11645 /*integral_constant_expression_p=*/false);
11650 /* Like tsubst but deals with expressions and performs semantic
11651 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
11654 tsubst_copy_and_build (tree t,
11656 tsubst_flags_t complain,
11659 bool integral_constant_expression_p)
11661 #define RECUR(NODE) \
11662 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11663 /*function_p=*/false, \
11664 integral_constant_expression_p)
11668 if (t == NULL_TREE || t == error_mark_node)
11671 switch (TREE_CODE (t))
11675 /* Fall through. */
11676 case IDENTIFIER_NODE:
11680 bool non_integral_constant_expression_p;
11681 const char *error_msg;
11683 if (IDENTIFIER_TYPENAME_P (t))
11685 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11686 t = mangle_conv_op_name_for_type (new_type);
11689 /* Look up the name. */
11690 decl = lookup_name (t);
11692 /* By convention, expressions use ERROR_MARK_NODE to indicate
11693 failure, not NULL_TREE. */
11694 if (decl == NULL_TREE)
11695 decl = error_mark_node;
11697 decl = finish_id_expression (t, decl, NULL_TREE,
11699 integral_constant_expression_p,
11700 /*allow_non_integral_constant_expression_p=*/false,
11701 &non_integral_constant_expression_p,
11702 /*template_p=*/false,
11704 /*address_p=*/false,
11705 /*template_arg_p=*/false,
11710 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11711 decl = unqualified_name_lookup_error (decl);
11715 case TEMPLATE_ID_EXPR:
11718 tree templ = RECUR (TREE_OPERAND (t, 0));
11719 tree targs = TREE_OPERAND (t, 1);
11722 targs = tsubst_template_args (targs, args, complain, in_decl);
11724 if (TREE_CODE (templ) == COMPONENT_REF)
11726 object = TREE_OPERAND (templ, 0);
11727 templ = TREE_OPERAND (templ, 1);
11730 object = NULL_TREE;
11731 templ = lookup_template_function (templ, targs);
11734 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11735 object, templ, NULL_TREE);
11737 return baselink_for_fns (templ);
11742 tree r = RECUR (TREE_OPERAND (t, 0));
11744 if (REFERENCE_REF_P (t))
11746 /* A type conversion to reference type will be enclosed in
11747 such an indirect ref, but the substitution of the cast
11748 will have also added such an indirect ref. */
11749 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11750 r = convert_from_reference (r);
11753 r = build_x_indirect_ref (r, "unary *", complain);
11759 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11760 RECUR (TREE_OPERAND (t, 0)));
11763 case REINTERPRET_CAST_EXPR:
11764 case CONST_CAST_EXPR:
11765 case DYNAMIC_CAST_EXPR:
11766 case STATIC_CAST_EXPR:
11771 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11772 if (integral_constant_expression_p
11773 && !cast_valid_in_integral_constant_expression_p (type))
11775 if (complain & tf_error)
11776 error ("a cast to a type other than an integral or "
11777 "enumeration type cannot appear in a constant-expression");
11778 return error_mark_node;
11781 op = RECUR (TREE_OPERAND (t, 0));
11783 switch (TREE_CODE (t))
11786 return build_functional_cast (type, op, complain);
11787 case REINTERPRET_CAST_EXPR:
11788 return build_reinterpret_cast (type, op, complain);
11789 case CONST_CAST_EXPR:
11790 return build_const_cast (type, op, complain);
11791 case DYNAMIC_CAST_EXPR:
11792 return build_dynamic_cast (type, op, complain);
11793 case STATIC_CAST_EXPR:
11794 return build_static_cast (type, op, complain);
11796 gcc_unreachable ();
11800 case POSTDECREMENT_EXPR:
11801 case POSTINCREMENT_EXPR:
11802 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11803 args, complain, in_decl);
11804 return build_x_unary_op (TREE_CODE (t), op1, complain);
11806 case PREDECREMENT_EXPR:
11807 case PREINCREMENT_EXPR:
11811 case TRUTH_NOT_EXPR:
11812 case UNARY_PLUS_EXPR: /* Unary + */
11813 case REALPART_EXPR:
11814 case IMAGPART_EXPR:
11815 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11819 op1 = TREE_OPERAND (t, 0);
11820 if (TREE_CODE (op1) == SCOPE_REF)
11821 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
11822 /*done=*/true, /*address_p=*/true);
11824 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
11826 if (TREE_CODE (op1) == LABEL_DECL)
11827 return finish_label_address_expr (DECL_NAME (op1),
11828 EXPR_LOCATION (op1));
11829 return build_x_unary_op (ADDR_EXPR, op1, complain);
11834 case TRUNC_DIV_EXPR:
11835 case CEIL_DIV_EXPR:
11836 case FLOOR_DIV_EXPR:
11837 case ROUND_DIV_EXPR:
11838 case EXACT_DIV_EXPR:
11842 case TRUNC_MOD_EXPR:
11843 case FLOOR_MOD_EXPR:
11844 case TRUTH_ANDIF_EXPR:
11845 case TRUTH_ORIF_EXPR:
11846 case TRUTH_AND_EXPR:
11847 case TRUTH_OR_EXPR:
11862 return build_x_binary_op
11864 RECUR (TREE_OPERAND (t, 0)),
11865 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11867 : TREE_CODE (TREE_OPERAND (t, 0))),
11868 RECUR (TREE_OPERAND (t, 1)),
11869 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11871 : TREE_CODE (TREE_OPERAND (t, 1))),
11872 /*overloaded_p=*/NULL,
11876 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
11877 /*address_p=*/false);
11879 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11880 args, complain, in_decl);
11881 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
11884 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
11885 return tsubst_copy (t, args, complain, in_decl);
11889 op1 = TREE_OPERAND (t, 0);
11892 /* When there are no ARGS, we are trying to evaluate a
11893 non-dependent expression from the parser. Trying to do
11894 the substitutions may not work. */
11896 op1 = TREE_TYPE (op1);
11900 ++cp_unevaluated_operand;
11901 ++c_inhibit_evaluation_warnings;
11902 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
11903 /*function_p=*/false,
11904 /*integral_constant_expression_p=*/false);
11905 --cp_unevaluated_operand;
11906 --c_inhibit_evaluation_warnings;
11909 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
11910 complain & tf_error);
11912 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
11913 complain & tf_error);
11917 tree r = build_x_modify_expr
11918 (RECUR (TREE_OPERAND (t, 0)),
11919 TREE_CODE (TREE_OPERAND (t, 1)),
11920 RECUR (TREE_OPERAND (t, 2)),
11922 /* TREE_NO_WARNING must be set if either the expression was
11923 parenthesized or it uses an operator such as >>= rather
11924 than plain assignment. In the former case, it was already
11925 set and must be copied. In the latter case,
11926 build_x_modify_expr sets it and it must not be reset
11928 if (TREE_NO_WARNING (t))
11929 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
11934 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11935 args, complain, in_decl);
11936 /* Remember that there was a reference to this entity. */
11939 return build_x_arrow (op1);
11943 tree placement = RECUR (TREE_OPERAND (t, 0));
11944 tree init = RECUR (TREE_OPERAND (t, 3));
11945 VEC(tree,gc) *placement_vec;
11946 VEC(tree,gc) *init_vec;
11949 if (placement == NULL_TREE)
11950 placement_vec = NULL;
11953 placement_vec = make_tree_vector ();
11954 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
11955 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
11958 /* If there was an initializer in the original tree, but it
11959 instantiated to an empty list, then we should pass a
11960 non-NULL empty vector to tell build_new that it was an
11961 empty initializer() rather than no initializer. This can
11962 only happen when the initializer is a pack expansion whose
11963 parameter packs are of length zero. */
11964 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
11968 init_vec = make_tree_vector ();
11969 if (init == void_zero_node)
11970 gcc_assert (init_vec != NULL);
11973 for (; init != NULL_TREE; init = TREE_CHAIN (init))
11974 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
11978 ret = build_new (&placement_vec,
11979 RECUR (TREE_OPERAND (t, 1)),
11980 RECUR (TREE_OPERAND (t, 2)),
11982 NEW_EXPR_USE_GLOBAL (t),
11985 if (placement_vec != NULL)
11986 release_tree_vector (placement_vec);
11987 if (init_vec != NULL)
11988 release_tree_vector (init_vec);
11994 return delete_sanity
11995 (RECUR (TREE_OPERAND (t, 0)),
11996 RECUR (TREE_OPERAND (t, 1)),
11997 DELETE_EXPR_USE_VEC (t),
11998 DELETE_EXPR_USE_GLOBAL (t));
12000 case COMPOUND_EXPR:
12001 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
12002 RECUR (TREE_OPERAND (t, 1)),
12008 VEC(tree,gc) *call_args;
12009 unsigned int nargs, i;
12014 function = CALL_EXPR_FN (t);
12015 /* When we parsed the expression, we determined whether or
12016 not Koenig lookup should be performed. */
12017 koenig_p = KOENIG_LOOKUP_P (t);
12018 if (TREE_CODE (function) == SCOPE_REF)
12020 qualified_p = true;
12021 function = tsubst_qualified_id (function, args, complain, in_decl,
12023 /*address_p=*/false);
12027 if (TREE_CODE (function) == COMPONENT_REF)
12029 tree op = TREE_OPERAND (function, 1);
12031 qualified_p = (TREE_CODE (op) == SCOPE_REF
12032 || (BASELINK_P (op)
12033 && BASELINK_QUALIFIED_P (op)));
12036 qualified_p = false;
12038 function = tsubst_copy_and_build (function, args, complain,
12041 integral_constant_expression_p);
12043 if (BASELINK_P (function))
12044 qualified_p = true;
12047 nargs = call_expr_nargs (t);
12048 call_args = make_tree_vector ();
12049 for (i = 0; i < nargs; ++i)
12051 tree arg = CALL_EXPR_ARG (t, i);
12053 if (!PACK_EXPANSION_P (arg))
12054 VEC_safe_push (tree, gc, call_args,
12055 RECUR (CALL_EXPR_ARG (t, i)));
12058 /* Expand the pack expansion and push each entry onto
12060 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
12061 if (TREE_CODE (arg) == TREE_VEC)
12063 unsigned int len, j;
12065 len = TREE_VEC_LENGTH (arg);
12066 for (j = 0; j < len; ++j)
12068 tree value = TREE_VEC_ELT (arg, j);
12069 if (value != NULL_TREE)
12070 value = convert_from_reference (value);
12071 VEC_safe_push (tree, gc, call_args, value);
12076 /* A partial substitution. Add one entry. */
12077 VEC_safe_push (tree, gc, call_args, arg);
12082 /* We do not perform argument-dependent lookup if normal
12083 lookup finds a non-function, in accordance with the
12084 expected resolution of DR 218. */
12086 && ((is_overloaded_fn (function)
12087 /* If lookup found a member function, the Koenig lookup is
12088 not appropriate, even if an unqualified-name was used
12089 to denote the function. */
12090 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
12091 || TREE_CODE (function) == IDENTIFIER_NODE)
12092 /* Only do this when substitution turns a dependent call
12093 into a non-dependent call. */
12094 && type_dependent_expression_p_push (t)
12095 && !any_type_dependent_arguments_p (call_args))
12096 function = perform_koenig_lookup (function, call_args);
12098 if (TREE_CODE (function) == IDENTIFIER_NODE)
12100 unqualified_name_lookup_error (function);
12101 release_tree_vector (call_args);
12102 return error_mark_node;
12105 /* Remember that there was a reference to this entity. */
12106 if (DECL_P (function))
12107 mark_used (function);
12109 if (TREE_CODE (function) == OFFSET_REF)
12110 ret = build_offset_ref_call_from_tree (function, &call_args);
12111 else if (TREE_CODE (function) == COMPONENT_REF)
12113 if (!BASELINK_P (TREE_OPERAND (function, 1)))
12114 ret = finish_call_expr (function, &call_args,
12115 /*disallow_virtual=*/false,
12116 /*koenig_p=*/false,
12119 ret = (build_new_method_call
12120 (TREE_OPERAND (function, 0),
12121 TREE_OPERAND (function, 1),
12122 &call_args, NULL_TREE,
12123 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
12128 ret = finish_call_expr (function, &call_args,
12129 /*disallow_virtual=*/qualified_p,
12133 release_tree_vector (call_args);
12139 return build_x_conditional_expr
12140 (RECUR (TREE_OPERAND (t, 0)),
12141 RECUR (TREE_OPERAND (t, 1)),
12142 RECUR (TREE_OPERAND (t, 2)),
12145 case PSEUDO_DTOR_EXPR:
12146 return finish_pseudo_destructor_expr
12147 (RECUR (TREE_OPERAND (t, 0)),
12148 RECUR (TREE_OPERAND (t, 1)),
12149 RECUR (TREE_OPERAND (t, 2)));
12153 tree purpose, value, chain;
12155 if (t == void_list_node)
12158 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
12159 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
12161 /* We have pack expansions, so expand those and
12162 create a new list out of it. */
12163 tree purposevec = NULL_TREE;
12164 tree valuevec = NULL_TREE;
12168 /* Expand the argument expressions. */
12169 if (TREE_PURPOSE (t))
12170 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
12171 complain, in_decl);
12172 if (TREE_VALUE (t))
12173 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
12174 complain, in_decl);
12176 /* Build the rest of the list. */
12177 chain = TREE_CHAIN (t);
12178 if (chain && chain != void_type_node)
12179 chain = RECUR (chain);
12181 /* Determine the number of arguments. */
12182 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
12184 len = TREE_VEC_LENGTH (purposevec);
12185 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
12187 else if (TREE_CODE (valuevec) == TREE_VEC)
12188 len = TREE_VEC_LENGTH (valuevec);
12191 /* Since we only performed a partial substitution into
12192 the argument pack, we only return a single list
12194 if (purposevec == TREE_PURPOSE (t)
12195 && valuevec == TREE_VALUE (t)
12196 && chain == TREE_CHAIN (t))
12199 return tree_cons (purposevec, valuevec, chain);
12202 /* Convert the argument vectors into a TREE_LIST */
12206 /* Grab the Ith values. */
12208 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
12211 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12214 /* Build the list (backwards). */
12215 chain = tree_cons (purpose, value, chain);
12221 purpose = TREE_PURPOSE (t);
12223 purpose = RECUR (purpose);
12224 value = TREE_VALUE (t);
12226 value = RECUR (value);
12227 chain = TREE_CHAIN (t);
12228 if (chain && chain != void_type_node)
12229 chain = RECUR (chain);
12230 if (purpose == TREE_PURPOSE (t)
12231 && value == TREE_VALUE (t)
12232 && chain == TREE_CHAIN (t))
12234 return tree_cons (purpose, value, chain);
12237 case COMPONENT_REF:
12243 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12244 args, complain, in_decl);
12245 /* Remember that there was a reference to this entity. */
12246 if (DECL_P (object))
12247 mark_used (object);
12248 object_type = TREE_TYPE (object);
12250 member = TREE_OPERAND (t, 1);
12251 if (BASELINK_P (member))
12252 member = tsubst_baselink (member,
12253 non_reference (TREE_TYPE (object)),
12254 args, complain, in_decl);
12256 member = tsubst_copy (member, args, complain, in_decl);
12257 if (member == error_mark_node)
12258 return error_mark_node;
12260 if (object_type && !CLASS_TYPE_P (object_type))
12262 if (SCALAR_TYPE_P (object_type))
12264 tree s = NULL_TREE;
12265 tree dtor = member;
12267 if (TREE_CODE (dtor) == SCOPE_REF)
12269 s = TREE_OPERAND (dtor, 0);
12270 dtor = TREE_OPERAND (dtor, 1);
12272 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12274 dtor = TREE_OPERAND (dtor, 0);
12276 return finish_pseudo_destructor_expr (object, s, dtor);
12280 else if (TREE_CODE (member) == SCOPE_REF
12281 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12286 /* Lookup the template functions now that we know what the
12288 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12289 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
12290 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12291 /*is_type_p=*/false,
12292 /*complain=*/false);
12293 if (BASELINK_P (member))
12295 BASELINK_FUNCTIONS (member)
12296 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12298 member = (adjust_result_of_qualified_name_lookup
12299 (member, BINFO_TYPE (BASELINK_BINFO (member)),
12304 qualified_name_lookup_error (object_type, tmpl, member,
12306 return error_mark_node;
12309 else if (TREE_CODE (member) == SCOPE_REF
12310 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12311 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12313 if (complain & tf_error)
12315 if (TYPE_P (TREE_OPERAND (member, 0)))
12316 error ("%qT is not a class or namespace",
12317 TREE_OPERAND (member, 0));
12319 error ("%qD is not a class or namespace",
12320 TREE_OPERAND (member, 0));
12322 return error_mark_node;
12324 else if (TREE_CODE (member) == FIELD_DECL)
12325 return finish_non_static_data_member (member, object, NULL_TREE);
12327 return finish_class_member_access_expr (object, member,
12328 /*template_p=*/false,
12334 (RECUR (TREE_OPERAND (t, 0)));
12338 VEC(constructor_elt,gc) *n;
12339 constructor_elt *ce;
12340 unsigned HOST_WIDE_INT idx;
12341 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12342 bool process_index_p;
12344 bool need_copy_p = false;
12347 if (type == error_mark_node)
12348 return error_mark_node;
12350 /* digest_init will do the wrong thing if we let it. */
12351 if (type && TYPE_PTRMEMFUNC_P (type))
12354 /* We do not want to process the index of aggregate
12355 initializers as they are identifier nodes which will be
12356 looked up by digest_init. */
12357 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
12359 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
12360 newlen = VEC_length (constructor_elt, n);
12361 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
12363 if (ce->index && process_index_p)
12364 ce->index = RECUR (ce->index);
12366 if (PACK_EXPANSION_P (ce->value))
12368 /* Substitute into the pack expansion. */
12369 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12372 if (ce->value == error_mark_node)
12374 else if (TREE_VEC_LENGTH (ce->value) == 1)
12375 /* Just move the argument into place. */
12376 ce->value = TREE_VEC_ELT (ce->value, 0);
12379 /* Update the length of the final CONSTRUCTOR
12380 arguments vector, and note that we will need to
12382 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12383 need_copy_p = true;
12387 ce->value = RECUR (ce->value);
12392 VEC(constructor_elt,gc) *old_n = n;
12394 n = VEC_alloc (constructor_elt, gc, newlen);
12395 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
12398 if (TREE_CODE (ce->value) == TREE_VEC)
12400 int i, len = TREE_VEC_LENGTH (ce->value);
12401 for (i = 0; i < len; ++i)
12402 CONSTRUCTOR_APPEND_ELT (n, 0,
12403 TREE_VEC_ELT (ce->value, i));
12406 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12410 r = build_constructor (init_list_type_node, n);
12411 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
12413 if (TREE_HAS_CONSTRUCTOR (t))
12414 return finish_compound_literal (type, r);
12421 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
12422 if (TYPE_P (operand_0))
12423 return get_typeid (operand_0);
12424 return build_typeid (operand_0);
12434 tree r = tsubst_copy (t, args, complain, in_decl);
12436 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12437 /* If the original type was a reference, we'll be wrapped in
12438 the appropriate INDIRECT_REF. */
12439 r = convert_from_reference (r);
12444 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
12445 tsubst_copy (TREE_TYPE (t), args, complain,
12448 case OFFSETOF_EXPR:
12449 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
12453 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12454 complain, in_decl);
12456 tree type2 = TRAIT_EXPR_TYPE2 (t);
12458 type2 = tsubst_copy (type2, args, complain, in_decl);
12460 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12465 tree old_stmt_expr = cur_stmt_expr;
12466 tree stmt_expr = begin_stmt_expr ();
12468 cur_stmt_expr = stmt_expr;
12469 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12470 integral_constant_expression_p);
12471 stmt_expr = finish_stmt_expr (stmt_expr, false);
12472 cur_stmt_expr = old_stmt_expr;
12478 t = tsubst_copy (t, args, complain, in_decl);
12479 /* As in finish_id_expression, we resolve enumeration constants
12480 to their underlying values. */
12481 if (TREE_CODE (t) == CONST_DECL)
12483 used_types_insert (TREE_TYPE (t));
12484 return DECL_INITIAL (t);
12490 tree r = build_lambda_expr ();
12492 tree type = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
12493 TREE_TYPE (r) = type;
12494 CLASSTYPE_LAMBDA_EXPR (type) = r;
12496 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
12497 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
12498 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
12499 LAMBDA_EXPR_DISCRIMINATOR (r)
12500 = (LAMBDA_EXPR_DISCRIMINATOR (t));
12501 LAMBDA_EXPR_CAPTURE_LIST (r)
12502 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t));
12503 LAMBDA_EXPR_THIS_CAPTURE (r)
12504 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t));
12505 LAMBDA_EXPR_EXTRA_SCOPE (r)
12506 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t));
12508 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
12509 determine_visibility (TYPE_NAME (type));
12510 /* Now that we know visibility, instantiate the type so we have a
12511 declaration of the op() for later calls to lambda_function. */
12512 complete_type (type);
12514 type = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
12516 apply_lambda_return_type (r, type);
12518 return build_lambda_object (r);
12522 /* Handle Objective-C++ constructs, if appropriate. */
12525 = objcp_tsubst_copy_and_build (t, args, complain,
12526 in_decl, /*function_p=*/false);
12530 return tsubst_copy (t, args, complain, in_decl);
12536 /* Verify that the instantiated ARGS are valid. For type arguments,
12537 make sure that the type's linkage is ok. For non-type arguments,
12538 make sure they are constants if they are integral or enumerations.
12539 Emit an error under control of COMPLAIN, and return TRUE on error. */
12542 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
12544 if (ARGUMENT_PACK_P (t))
12546 tree vec = ARGUMENT_PACK_ARGS (t);
12547 int len = TREE_VEC_LENGTH (vec);
12548 bool result = false;
12551 for (i = 0; i < len; ++i)
12552 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12556 else if (TYPE_P (t))
12558 /* [basic.link]: A name with no linkage (notably, the name
12559 of a class or enumeration declared in a local scope)
12560 shall not be used to declare an entity with linkage.
12561 This implies that names with no linkage cannot be used as
12564 DR 757 relaxes this restriction for C++0x. */
12565 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
12566 : no_linkage_check (t, /*relaxed_p=*/false));
12570 /* DR 488 makes use of a type with no linkage cause
12571 type deduction to fail. */
12572 if (complain & tf_error)
12574 if (TYPE_ANONYMOUS_P (nt))
12575 error ("%qT is/uses anonymous type", t);
12577 error ("template argument for %qD uses local type %qT",
12582 /* In order to avoid all sorts of complications, we do not
12583 allow variably-modified types as template arguments. */
12584 else if (variably_modified_type_p (t, NULL_TREE))
12586 if (complain & tf_error)
12587 error ("%qT is a variably modified type", t);
12591 /* A non-type argument of integral or enumerated type must be a
12593 else if (TREE_TYPE (t)
12594 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12595 && !TREE_CONSTANT (t))
12597 if (complain & tf_error)
12598 error ("integral expression %qE is not constant", t);
12605 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12607 int ix, len = DECL_NTPARMS (tmpl);
12608 bool result = false;
12610 for (ix = 0; ix != len; ix++)
12612 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12615 if (result && (complain & tf_error))
12616 error (" trying to instantiate %qD", tmpl);
12620 /* Instantiate the indicated variable or function template TMPL with
12621 the template arguments in TARG_PTR. */
12624 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
12626 tree targ_ptr = orig_args;
12630 HOST_WIDE_INT saved_processing_template_decl;
12632 if (tmpl == error_mark_node)
12633 return error_mark_node;
12635 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
12637 /* If this function is a clone, handle it specially. */
12638 if (DECL_CLONED_FUNCTION_P (tmpl))
12643 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12644 DECL_CLONED_FUNCTION. */
12645 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
12646 targ_ptr, complain);
12647 if (spec == error_mark_node)
12648 return error_mark_node;
12650 /* Look for the clone. */
12651 FOR_EACH_CLONE (clone, spec)
12652 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12654 /* We should always have found the clone by now. */
12655 gcc_unreachable ();
12659 /* Check to see if we already have this specialization. */
12660 gen_tmpl = most_general_template (tmpl);
12661 if (tmpl != gen_tmpl)
12662 /* The TMPL is a partial instantiation. To get a full set of
12663 arguments we must add the arguments used to perform the
12664 partial instantiation. */
12665 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12668 /* It would be nice to avoid hashing here and then again in tsubst_decl,
12669 but it doesn't seem to be on the hot path. */
12670 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
12672 gcc_assert (tmpl == gen_tmpl
12673 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
12675 || fndecl == NULL_TREE);
12677 if (spec != NULL_TREE)
12680 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
12682 return error_mark_node;
12684 /* We are building a FUNCTION_DECL, during which the access of its
12685 parameters and return types have to be checked. However this
12686 FUNCTION_DECL which is the desired context for access checking
12687 is not built yet. We solve this chicken-and-egg problem by
12688 deferring all checks until we have the FUNCTION_DECL. */
12689 push_deferring_access_checks (dk_deferred);
12691 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12692 (because, for example, we have encountered a non-dependent
12693 function call in the body of a template function and must now
12694 determine which of several overloaded functions will be called),
12695 within the instantiation itself we are not processing a
12697 saved_processing_template_decl = processing_template_decl;
12698 processing_template_decl = 0;
12699 /* Substitute template parameters to obtain the specialization. */
12700 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
12701 targ_ptr, complain, gen_tmpl);
12702 processing_template_decl = saved_processing_template_decl;
12703 if (fndecl == error_mark_node)
12704 return error_mark_node;
12706 /* Now we know the specialization, compute access previously
12708 push_access_scope (fndecl);
12710 /* Some typedefs referenced from within the template code need to be access
12711 checked at template instantiation time, i.e now. These types were
12712 added to the template at parsing time. Let's get those and perfom
12713 the acces checks then. */
12714 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
12715 perform_deferred_access_checks ();
12716 pop_access_scope (fndecl);
12717 pop_deferring_access_checks ();
12719 /* The DECL_TI_TEMPLATE should always be the immediate parent
12720 template, not the most general template. */
12721 DECL_TI_TEMPLATE (fndecl) = tmpl;
12723 /* If we've just instantiated the main entry point for a function,
12724 instantiate all the alternate entry points as well. We do this
12725 by cloning the instantiation of the main entry point, not by
12726 instantiating the template clones. */
12727 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
12728 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
12733 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
12734 NARGS elements of the arguments that are being used when calling
12735 it. TARGS is a vector into which the deduced template arguments
12738 Return zero for success, 2 for an incomplete match that doesn't resolve
12739 all the types, and 1 for complete failure. An error message will be
12740 printed only for an incomplete match.
12742 If FN is a conversion operator, or we are trying to produce a specific
12743 specialization, RETURN_TYPE is the return type desired.
12745 The EXPLICIT_TARGS are explicit template arguments provided via a
12748 The parameter STRICT is one of:
12751 We are deducing arguments for a function call, as in
12752 [temp.deduct.call].
12755 We are deducing arguments for a conversion function, as in
12756 [temp.deduct.conv].
12759 We are deducing arguments when doing an explicit instantiation
12760 as in [temp.explicit], when determining an explicit specialization
12761 as in [temp.expl.spec], or when taking the address of a function
12762 template, as in [temp.deduct.funcaddr]. */
12765 fn_type_unification (tree fn,
12766 tree explicit_targs,
12769 unsigned int nargs,
12771 unification_kind_t strict,
12777 bool incomplete_argument_packs_p = false;
12779 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
12781 fntype = TREE_TYPE (fn);
12782 if (explicit_targs)
12786 The specified template arguments must match the template
12787 parameters in kind (i.e., type, nontype, template), and there
12788 must not be more arguments than there are parameters;
12789 otherwise type deduction fails.
12791 Nontype arguments must match the types of the corresponding
12792 nontype template parameters, or must be convertible to the
12793 types of the corresponding nontype parameters as specified in
12794 _temp.arg.nontype_, otherwise type deduction fails.
12796 All references in the function type of the function template
12797 to the corresponding template parameters are replaced by the
12798 specified template argument values. If a substitution in a
12799 template parameter or in the function type of the function
12800 template results in an invalid type, type deduction fails. */
12801 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12802 int i, len = TREE_VEC_LENGTH (tparms);
12803 tree converted_args;
12804 bool incomplete = false;
12806 if (explicit_targs == error_mark_node)
12810 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
12811 /*require_all_args=*/false,
12812 /*use_default_args=*/false));
12813 if (converted_args == error_mark_node)
12816 /* Substitute the explicit args into the function type. This is
12817 necessary so that, for instance, explicitly declared function
12818 arguments can match null pointed constants. If we were given
12819 an incomplete set of explicit args, we must not do semantic
12820 processing during substitution as we could create partial
12822 for (i = 0; i < len; i++)
12824 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12825 bool parameter_pack = false;
12827 /* Dig out the actual parm. */
12828 if (TREE_CODE (parm) == TYPE_DECL
12829 || TREE_CODE (parm) == TEMPLATE_DECL)
12831 parm = TREE_TYPE (parm);
12832 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12834 else if (TREE_CODE (parm) == PARM_DECL)
12836 parm = DECL_INITIAL (parm);
12837 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12840 if (parameter_pack)
12844 template_parm_level_and_index (parm, &level, &idx);
12846 /* Mark the argument pack as "incomplete". We could
12847 still deduce more arguments during unification. */
12848 targ = TMPL_ARG (converted_args, level, idx);
12851 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12852 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12853 = ARGUMENT_PACK_ARGS (targ);
12856 /* We have some incomplete argument packs. */
12857 incomplete_argument_packs_p = true;
12861 if (incomplete_argument_packs_p)
12862 /* Any substitution is guaranteed to be incomplete if there
12863 are incomplete argument packs, because we can still deduce
12867 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
12869 processing_template_decl += incomplete;
12870 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
12871 processing_template_decl -= incomplete;
12873 if (fntype == error_mark_node)
12876 /* Place the explicitly specified arguments in TARGS. */
12877 for (i = NUM_TMPL_ARGS (converted_args); i--;)
12878 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
12881 /* Never do unification on the 'this' parameter. */
12882 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
12888 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
12889 new_args = XALLOCAVEC (tree, nargs + 1);
12890 new_args[0] = return_type;
12891 memcpy (new_args + 1, args, nargs * sizeof (tree));
12896 /* We allow incomplete unification without an error message here
12897 because the standard doesn't seem to explicitly prohibit it. Our
12898 callers must be ready to deal with unification failures in any
12900 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
12901 targs, parms, args, nargs, /*subr=*/0,
12904 if (result == 0 && incomplete_argument_packs_p)
12906 int i, len = NUM_TMPL_ARGS (targs);
12908 /* Clear the "incomplete" flags on all argument packs. */
12909 for (i = 0; i < len; i++)
12911 tree arg = TREE_VEC_ELT (targs, i);
12912 if (ARGUMENT_PACK_P (arg))
12914 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
12915 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
12920 /* Now that we have bindings for all of the template arguments,
12921 ensure that the arguments deduced for the template template
12922 parameters have compatible template parameter lists. We cannot
12923 check this property before we have deduced all template
12924 arguments, because the template parameter types of a template
12925 template parameter might depend on prior template parameters
12926 deduced after the template template parameter. The following
12927 ill-formed example illustrates this issue:
12929 template<typename T, template<T> class C> void f(C<5>, T);
12931 template<int N> struct X {};
12934 f(X<5>(), 5l); // error: template argument deduction fails
12937 The template parameter list of 'C' depends on the template type
12938 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
12939 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
12940 time that we deduce 'C'. */
12942 && !template_template_parm_bindings_ok_p
12943 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
12947 /* All is well so far. Now, check:
12951 When all template arguments have been deduced, all uses of
12952 template parameters in nondeduced contexts are replaced with
12953 the corresponding deduced argument values. If the
12954 substitution results in an invalid type, as described above,
12955 type deduction fails. */
12957 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
12958 if (substed == error_mark_node)
12961 /* If we're looking for an exact match, check that what we got
12962 is indeed an exact match. It might not be if some template
12963 parameters are used in non-deduced contexts. */
12964 if (strict == DEDUCE_EXACT)
12969 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
12971 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
12972 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
12973 if (!same_type_p (args[i], TREE_VALUE (sarg)))
12981 /* Adjust types before performing type deduction, as described in
12982 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
12983 sections are symmetric. PARM is the type of a function parameter
12984 or the return type of the conversion function. ARG is the type of
12985 the argument passed to the call, or the type of the value
12986 initialized with the result of the conversion function.
12987 ARG_EXPR is the original argument expression, which may be null. */
12990 maybe_adjust_types_for_deduction (unification_kind_t strict,
13004 /* Swap PARM and ARG throughout the remainder of this
13005 function; the handling is precisely symmetric since PARM
13006 will initialize ARG rather than vice versa. */
13014 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13015 too, but here handle it by stripping the reference from PARM
13016 rather than by adding it to ARG. */
13017 if (TREE_CODE (*parm) == REFERENCE_TYPE
13018 && TYPE_REF_IS_RVALUE (*parm)
13019 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13020 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13021 && TREE_CODE (*arg) == REFERENCE_TYPE
13022 && !TYPE_REF_IS_RVALUE (*arg))
13023 *parm = TREE_TYPE (*parm);
13024 /* Nothing else to do in this case. */
13028 gcc_unreachable ();
13031 if (TREE_CODE (*parm) != REFERENCE_TYPE)
13033 /* [temp.deduct.call]
13035 If P is not a reference type:
13037 --If A is an array type, the pointer type produced by the
13038 array-to-pointer standard conversion (_conv.array_) is
13039 used in place of A for type deduction; otherwise,
13041 --If A is a function type, the pointer type produced by
13042 the function-to-pointer standard conversion
13043 (_conv.func_) is used in place of A for type deduction;
13046 --If A is a cv-qualified type, the top level
13047 cv-qualifiers of A's type are ignored for type
13049 if (TREE_CODE (*arg) == ARRAY_TYPE)
13050 *arg = build_pointer_type (TREE_TYPE (*arg));
13051 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
13052 *arg = build_pointer_type (*arg);
13054 *arg = TYPE_MAIN_VARIANT (*arg);
13057 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13058 of the form T&&, where T is a template parameter, and the argument
13059 is an lvalue, T is deduced as A& */
13060 if (TREE_CODE (*parm) == REFERENCE_TYPE
13061 && TYPE_REF_IS_RVALUE (*parm)
13062 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
13063 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
13064 && arg_expr && real_lvalue_p (arg_expr))
13065 *arg = build_reference_type (*arg);
13067 /* [temp.deduct.call]
13069 If P is a cv-qualified type, the top level cv-qualifiers
13070 of P's type are ignored for type deduction. If P is a
13071 reference type, the type referred to by P is used for
13073 *parm = TYPE_MAIN_VARIANT (*parm);
13074 if (TREE_CODE (*parm) == REFERENCE_TYPE)
13076 *parm = TREE_TYPE (*parm);
13077 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13080 /* DR 322. For conversion deduction, remove a reference type on parm
13081 too (which has been swapped into ARG). */
13082 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
13083 *arg = TREE_TYPE (*arg);
13088 /* Most parms like fn_type_unification.
13090 If SUBR is 1, we're being called recursively (to unify the
13091 arguments of a function or method parameter of a function
13095 type_unification_real (tree tparms,
13099 unsigned int xnargs,
13101 unification_kind_t strict,
13104 tree parm, arg, arg_expr;
13106 int ntparms = TREE_VEC_LENGTH (tparms);
13108 int saw_undeduced = 0;
13111 unsigned int nargs;
13114 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
13115 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
13116 gcc_assert (ntparms > 0);
13121 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
13122 | UNIFY_ALLOW_DERIVED);
13126 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13130 sub_strict = UNIFY_ALLOW_NONE;
13134 gcc_unreachable ();
13143 while (parms && parms != void_list_node
13146 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13149 parm = TREE_VALUE (parms);
13150 parms = TREE_CHAIN (parms);
13155 if (arg == error_mark_node)
13157 if (arg == unknown_type_node)
13158 /* We can't deduce anything from this, but we might get all the
13159 template args from other function args. */
13162 /* Conversions will be performed on a function argument that
13163 corresponds with a function parameter that contains only
13164 non-deducible template parameters and explicitly specified
13165 template parameters. */
13166 if (!uses_template_parms (parm))
13171 type = TREE_TYPE (arg);
13175 if (same_type_p (parm, type))
13177 if (strict != DEDUCE_EXACT
13178 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
13187 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13188 if (type_unknown_p (arg))
13190 /* [temp.deduct.type]
13192 A template-argument can be deduced from a pointer to
13193 function or pointer to member function argument if
13194 the set of overloaded functions does not contain
13195 function templates and at most one of a set of
13196 overloaded functions provides a unique match. */
13197 if (resolve_overloaded_unification
13198 (tparms, targs, parm, arg, strict, sub_strict))
13204 arg = unlowered_expr_type (arg);
13205 if (arg == error_mark_node)
13210 int arg_strict = sub_strict;
13213 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
13216 if (arg == init_list_type_node && arg_expr)
13218 if (unify (tparms, targs, parm, arg, arg_strict))
13225 && parms != void_list_node
13226 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
13228 /* Unify the remaining arguments with the pack expansion type. */
13230 tree parmvec = make_tree_vec (1);
13232 /* Allocate a TREE_VEC and copy in all of the arguments */
13233 argvec = make_tree_vec (nargs - ia);
13234 for (i = 0; ia < nargs; ++ia, ++i)
13235 TREE_VEC_ELT (argvec, i) = args[ia];
13237 /* Copy the parameter into parmvec. */
13238 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
13239 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
13240 /*call_args_p=*/true, /*subr=*/subr))
13243 /* Advance to the end of the list of parameters. */
13244 parms = TREE_CHAIN (parms);
13247 /* Fail if we've reached the end of the parm list, and more args
13248 are present, and the parm list isn't variadic. */
13249 if (ia < nargs && parms == void_list_node)
13251 /* Fail if parms are left and they don't have default values. */
13252 if (parms && parms != void_list_node
13253 && TREE_PURPOSE (parms) == NULL_TREE)
13257 for (i = 0; i < ntparms; i++)
13258 if (!TREE_VEC_ELT (targs, i))
13262 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
13265 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13267 /* If this is an undeduced nontype parameter that depends on
13268 a type parameter, try another pass; its type may have been
13269 deduced from a later argument than the one from which
13270 this parameter can be deduced. */
13271 if (TREE_CODE (tparm) == PARM_DECL
13272 && uses_template_parms (TREE_TYPE (tparm))
13273 && !saw_undeduced++)
13276 /* Core issue #226 (C++0x) [temp.deduct]:
13278 If a template argument has not been deduced, its
13279 default template argument, if any, is used.
13281 When we are in C++98 mode, TREE_PURPOSE will either
13282 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13283 to explicitly check cxx_dialect here. */
13284 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13286 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
13287 tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
13288 arg = tsubst_template_arg (arg, targs, tf_none, NULL_TREE);
13289 arg = convert_template_argument (parm, arg, targs, tf_none,
13291 if (arg == error_mark_node)
13295 TREE_VEC_ELT (targs, i) = arg;
13300 /* If the type parameter is a parameter pack, then it will
13301 be deduced to an empty parameter pack. */
13302 if (template_parameter_pack_p (tparm))
13306 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13308 arg = make_node (NONTYPE_ARGUMENT_PACK);
13309 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13310 TREE_CONSTANT (arg) = 1;
13313 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
13315 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13317 TREE_VEC_ELT (targs, i) = arg;
13327 /* Subroutine of type_unification_real. Args are like the variables
13328 at the call site. ARG is an overloaded function (or template-id);
13329 we try deducing template args from each of the overloads, and if
13330 only one succeeds, we go with that. Modifies TARGS and returns
13331 true on success. */
13334 resolve_overloaded_unification (tree tparms,
13338 unification_kind_t strict,
13341 tree tempargs = copy_node (targs);
13343 tree goodfn = NULL_TREE;
13346 if (TREE_CODE (arg) == ADDR_EXPR)
13348 arg = TREE_OPERAND (arg, 0);
13354 if (TREE_CODE (arg) == COMPONENT_REF)
13355 /* Handle `&x' where `x' is some static or non-static member
13357 arg = TREE_OPERAND (arg, 1);
13359 if (TREE_CODE (arg) == OFFSET_REF)
13360 arg = TREE_OPERAND (arg, 1);
13362 /* Strip baselink information. */
13363 if (BASELINK_P (arg))
13364 arg = BASELINK_FUNCTIONS (arg);
13366 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13368 /* If we got some explicit template args, we need to plug them into
13369 the affected templates before we try to unify, in case the
13370 explicit args will completely resolve the templates in question. */
13372 tree expl_subargs = TREE_OPERAND (arg, 1);
13373 arg = TREE_OPERAND (arg, 0);
13375 for (; arg; arg = OVL_NEXT (arg))
13377 tree fn = OVL_CURRENT (arg);
13378 tree subargs, elem;
13380 if (TREE_CODE (fn) != TEMPLATE_DECL)
13383 ++processing_template_decl;
13384 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13385 expl_subargs, /*check_ret=*/false);
13388 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
13389 if (try_one_overload (tparms, targs, tempargs, parm,
13390 elem, strict, sub_strict, addr_p)
13391 && (!goodfn || !decls_match (goodfn, elem)))
13397 --processing_template_decl;
13400 else if (TREE_CODE (arg) != OVERLOAD
13401 && TREE_CODE (arg) != FUNCTION_DECL)
13402 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13403 -- but the deduction does not succeed because the expression is
13404 not just the function on its own. */
13407 for (; arg; arg = OVL_NEXT (arg))
13408 if (try_one_overload (tparms, targs, tempargs, parm,
13409 TREE_TYPE (OVL_CURRENT (arg)),
13410 strict, sub_strict, addr_p)
13411 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13413 goodfn = OVL_CURRENT (arg);
13417 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13418 to function or pointer to member function argument if the set of
13419 overloaded functions does not contain function templates and at most
13420 one of a set of overloaded functions provides a unique match.
13422 So if we found multiple possibilities, we return success but don't
13423 deduce anything. */
13427 int i = TREE_VEC_LENGTH (targs);
13429 if (TREE_VEC_ELT (tempargs, i))
13430 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13438 /* Core DR 115: In contexts where deduction is done and fails, or in
13439 contexts where deduction is not done, if a template argument list is
13440 specified and it, along with any default template arguments, identifies
13441 a single function template specialization, then the template-id is an
13442 lvalue for the function template specialization. */
13445 resolve_nondeduced_context (tree orig_expr)
13447 tree expr, offset, baselink;
13450 if (!type_unknown_p (orig_expr))
13455 offset = NULL_TREE;
13456 baselink = NULL_TREE;
13458 if (TREE_CODE (expr) == ADDR_EXPR)
13460 expr = TREE_OPERAND (expr, 0);
13463 if (TREE_CODE (expr) == OFFSET_REF)
13466 expr = TREE_OPERAND (expr, 1);
13468 if (TREE_CODE (expr) == BASELINK)
13471 expr = BASELINK_FUNCTIONS (expr);
13474 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
13477 tree goodfn = NULL_TREE;
13479 /* If we got some explicit template args, we need to plug them into
13480 the affected templates before we try to unify, in case the
13481 explicit args will completely resolve the templates in question. */
13483 tree expl_subargs = TREE_OPERAND (expr, 1);
13484 tree arg = TREE_OPERAND (expr, 0);
13485 tree badfn = NULL_TREE;
13486 tree badargs = NULL_TREE;
13488 for (; arg; arg = OVL_NEXT (arg))
13490 tree fn = OVL_CURRENT (arg);
13491 tree subargs, elem;
13493 if (TREE_CODE (fn) != TEMPLATE_DECL)
13496 ++processing_template_decl;
13497 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13498 expl_subargs, /*check_ret=*/false);
13499 if (subargs && !any_dependent_template_arguments_p (subargs))
13501 elem = instantiate_template (fn, subargs, tf_none);
13502 if (elem == error_mark_node)
13507 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
13513 --processing_template_decl;
13519 expr = build_baselink (BASELINK_BINFO (baselink),
13520 BASELINK_ACCESS_BINFO (baselink),
13521 expr, BASELINK_OPTYPE (baselink));
13523 expr = build2 (OFFSET_REF, TREE_TYPE (expr),
13524 TREE_OPERAND (offset, 0), expr);
13526 expr = build_address (expr);
13529 else if (good == 0 && badargs)
13530 /* There were no good options and at least one bad one, so let the
13531 user know what the problem is. */
13532 instantiate_template (badfn, badargs, tf_warning_or_error);
13537 /* Subroutine of resolve_overloaded_unification; does deduction for a single
13538 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13539 different overloads deduce different arguments for a given parm.
13540 ADDR_P is true if the expression for which deduction is being
13541 performed was of the form "& fn" rather than simply "fn".
13543 Returns 1 on success. */
13546 try_one_overload (tree tparms,
13551 unification_kind_t strict,
13559 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13560 to function or pointer to member function argument if the set of
13561 overloaded functions does not contain function templates and at most
13562 one of a set of overloaded functions provides a unique match.
13564 So if this is a template, just return success. */
13566 if (uses_template_parms (arg))
13569 if (TREE_CODE (arg) == METHOD_TYPE)
13570 arg = build_ptrmemfunc_type (build_pointer_type (arg));
13572 arg = build_pointer_type (arg);
13574 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
13576 /* We don't copy orig_targs for this because if we have already deduced
13577 some template args from previous args, unify would complain when we
13578 try to deduce a template parameter for the same argument, even though
13579 there isn't really a conflict. */
13580 nargs = TREE_VEC_LENGTH (targs);
13581 tempargs = make_tree_vec (nargs);
13583 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
13586 /* First make sure we didn't deduce anything that conflicts with
13587 explicitly specified args. */
13588 for (i = nargs; i--; )
13590 tree elt = TREE_VEC_ELT (tempargs, i);
13591 tree oldelt = TREE_VEC_ELT (orig_targs, i);
13595 else if (uses_template_parms (elt))
13596 /* Since we're unifying against ourselves, we will fill in
13597 template args used in the function parm list with our own
13598 template parms. Discard them. */
13599 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
13600 else if (oldelt && !template_args_equal (oldelt, elt))
13604 for (i = nargs; i--; )
13606 tree elt = TREE_VEC_ELT (tempargs, i);
13609 TREE_VEC_ELT (targs, i) = elt;
13615 /* PARM is a template class (perhaps with unbound template
13616 parameters). ARG is a fully instantiated type. If ARG can be
13617 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13618 TARGS are as for unify. */
13621 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
13623 tree copy_of_targs;
13625 if (!CLASSTYPE_TEMPLATE_INFO (arg)
13626 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
13627 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
13630 /* We need to make a new template argument vector for the call to
13631 unify. If we used TARGS, we'd clutter it up with the result of
13632 the attempted unification, even if this class didn't work out.
13633 We also don't want to commit ourselves to all the unifications
13634 we've already done, since unification is supposed to be done on
13635 an argument-by-argument basis. In other words, consider the
13636 following pathological case:
13638 template <int I, int J, int K>
13641 template <int I, int J>
13642 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
13644 template <int I, int J, int K>
13645 void f(S<I, J, K>, S<I, I, I>);
13654 Now, by the time we consider the unification involving `s2', we
13655 already know that we must have `f<0, 0, 0>'. But, even though
13656 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
13657 because there are two ways to unify base classes of S<0, 1, 2>
13658 with S<I, I, I>. If we kept the already deduced knowledge, we
13659 would reject the possibility I=1. */
13660 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
13662 /* If unification failed, we're done. */
13663 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
13664 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
13670 /* Given a template type PARM and a class type ARG, find the unique
13671 base type in ARG that is an instance of PARM. We do not examine
13672 ARG itself; only its base-classes. If there is not exactly one
13673 appropriate base class, return NULL_TREE. PARM may be the type of
13674 a partial specialization, as well as a plain template type. Used
13678 get_template_base (tree tparms, tree targs, tree parm, tree arg)
13680 tree rval = NULL_TREE;
13683 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
13685 binfo = TYPE_BINFO (complete_type (arg));
13687 /* The type could not be completed. */
13690 /* Walk in inheritance graph order. The search order is not
13691 important, and this avoids multiple walks of virtual bases. */
13692 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
13694 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
13698 /* If there is more than one satisfactory baseclass, then:
13702 If they yield more than one possible deduced A, the type
13706 if (rval && !same_type_p (r, rval))
13716 /* Returns the level of DECL, which declares a template parameter. */
13719 template_decl_level (tree decl)
13721 switch (TREE_CODE (decl))
13724 case TEMPLATE_DECL:
13725 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
13728 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
13731 gcc_unreachable ();
13736 /* Decide whether ARG can be unified with PARM, considering only the
13737 cv-qualifiers of each type, given STRICT as documented for unify.
13738 Returns nonzero iff the unification is OK on that basis. */
13741 check_cv_quals_for_unify (int strict, tree arg, tree parm)
13743 int arg_quals = cp_type_quals (arg);
13744 int parm_quals = cp_type_quals (parm);
13746 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13747 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13749 /* Although a CVR qualifier is ignored when being applied to a
13750 substituted template parameter ([8.3.2]/1 for example), that
13751 does not apply during deduction [14.8.2.4]/1, (even though
13752 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13753 this). Except when we're allowing additional CV qualifiers
13754 at the outer level [14.8.2.1]/3,1st bullet. */
13755 if ((TREE_CODE (arg) == REFERENCE_TYPE
13756 || TREE_CODE (arg) == FUNCTION_TYPE
13757 || TREE_CODE (arg) == METHOD_TYPE)
13758 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
13761 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
13762 && (parm_quals & TYPE_QUAL_RESTRICT))
13766 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
13767 && (arg_quals & parm_quals) != parm_quals)
13770 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
13771 && (parm_quals & arg_quals) != arg_quals)
13777 /* Determines the LEVEL and INDEX for the template parameter PARM. */
13779 template_parm_level_and_index (tree parm, int* level, int* index)
13781 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13782 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13783 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13785 *index = TEMPLATE_TYPE_IDX (parm);
13786 *level = TEMPLATE_TYPE_LEVEL (parm);
13790 *index = TEMPLATE_PARM_IDX (parm);
13791 *level = TEMPLATE_PARM_LEVEL (parm);
13795 /* Unifies the remaining arguments in PACKED_ARGS with the pack
13796 expansion at the end of PACKED_PARMS. Returns 0 if the type
13797 deduction succeeds, 1 otherwise. STRICT is the same as in
13798 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13799 call argument list. We'll need to adjust the arguments to make them
13800 types. SUBR tells us if this is from a recursive call to
13801 type_unification_real. */
13803 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
13804 tree packed_args, int strict, bool call_args_p,
13808 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
13809 tree pattern = PACK_EXPANSION_PATTERN (parm);
13810 tree pack, packs = NULL_TREE;
13811 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
13812 int len = TREE_VEC_LENGTH (packed_args);
13814 /* Determine the parameter packs we will be deducing from the
13815 pattern, and record their current deductions. */
13816 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
13817 pack; pack = TREE_CHAIN (pack))
13819 tree parm_pack = TREE_VALUE (pack);
13822 /* Determine the index and level of this parameter pack. */
13823 template_parm_level_and_index (parm_pack, &level, &idx);
13825 /* Keep track of the parameter packs and their corresponding
13827 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
13828 TREE_TYPE (packs) = make_tree_vec (len - start);
13831 /* Loop through all of the arguments that have not yet been
13832 unified and unify each with the pattern. */
13833 for (i = start; i < len; i++)
13835 tree parm = pattern;
13837 /* For each parameter pack, clear out the deduced value so that
13838 we can deduce it again. */
13839 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13842 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13844 TMPL_ARG (targs, level, idx) = NULL_TREE;
13847 /* Unify the pattern with the current argument. */
13849 tree arg = TREE_VEC_ELT (packed_args, i);
13850 tree arg_expr = NULL_TREE;
13851 int arg_strict = strict;
13852 bool skip_arg_p = false;
13858 /* This mirrors what we do in type_unification_real. */
13862 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
13863 | UNIFY_ALLOW_MORE_CV_QUAL
13864 | UNIFY_ALLOW_DERIVED);
13868 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13872 sub_strict = UNIFY_ALLOW_NONE;
13876 gcc_unreachable ();
13881 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13882 if (type_unknown_p (arg))
13884 /* [temp.deduct.type] A template-argument can be
13885 deduced from a pointer to function or pointer
13886 to member function argument if the set of
13887 overloaded functions does not contain function
13888 templates and at most one of a set of
13889 overloaded functions provides a unique
13892 if (resolve_overloaded_unification
13893 (tparms, targs, parm, arg,
13894 (unification_kind_t) strict,
13904 arg = unlowered_expr_type (arg);
13905 if (arg == error_mark_node)
13910 arg_strict = sub_strict;
13914 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
13915 &parm, &arg, arg_expr);
13920 if (unify (tparms, targs, parm, arg, arg_strict))
13925 /* For each parameter pack, collect the deduced value. */
13926 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13929 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13931 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
13932 TMPL_ARG (targs, level, idx);
13936 /* Verify that the results of unification with the parameter packs
13937 produce results consistent with what we've seen before, and make
13938 the deduced argument packs available. */
13939 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13941 tree old_pack = TREE_VALUE (pack);
13942 tree new_args = TREE_TYPE (pack);
13943 int i, len = TREE_VEC_LENGTH (new_args);
13944 bool nondeduced_p = false;
13946 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
13947 actually deduce anything. */
13948 for (i = 0; i < len && !nondeduced_p; ++i)
13949 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
13950 nondeduced_p = true;
13954 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
13956 /* Prepend the explicit arguments onto NEW_ARGS. */
13957 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13958 tree old_args = new_args;
13959 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
13960 int len = explicit_len + TREE_VEC_LENGTH (old_args);
13962 /* Copy the explicit arguments. */
13963 new_args = make_tree_vec (len);
13964 for (i = 0; i < explicit_len; i++)
13965 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
13967 /* Copy the deduced arguments. */
13968 for (; i < len; i++)
13969 TREE_VEC_ELT (new_args, i) =
13970 TREE_VEC_ELT (old_args, i - explicit_len);
13978 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13980 /* Build the deduced *_ARGUMENT_PACK. */
13981 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
13983 result = make_node (NONTYPE_ARGUMENT_PACK);
13984 TREE_TYPE (result) =
13985 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
13986 TREE_CONSTANT (result) = 1;
13989 result = cxx_make_type (TYPE_ARGUMENT_PACK);
13991 SET_ARGUMENT_PACK_ARGS (result, new_args);
13993 /* Note the deduced argument packs for this parameter
13995 TMPL_ARG (targs, level, idx) = result;
13997 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
13998 && (ARGUMENT_PACK_ARGS (old_pack)
13999 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
14001 /* We only had the explicitly-provided arguments before, but
14002 now we have a complete set of arguments. */
14004 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
14005 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14007 /* Keep the original deduced argument pack. */
14008 TMPL_ARG (targs, level, idx) = old_pack;
14010 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
14011 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
14012 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
14014 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
14016 /* Inconsistent unification of this parameter pack. */
14022 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
14024 /* Keep the original deduced argument pack. */
14025 TMPL_ARG (targs, level, idx) = old_pack;
14032 /* Deduce the value of template parameters. TPARMS is the (innermost)
14033 set of template parameters to a template. TARGS is the bindings
14034 for those template parameters, as determined thus far; TARGS may
14035 include template arguments for outer levels of template parameters
14036 as well. PARM is a parameter to a template function, or a
14037 subcomponent of that parameter; ARG is the corresponding argument.
14038 This function attempts to match PARM with ARG in a manner
14039 consistent with the existing assignments in TARGS. If more values
14040 are deduced, then TARGS is updated.
14042 Returns 0 if the type deduction succeeds, 1 otherwise. The
14043 parameter STRICT is a bitwise or of the following flags:
14046 Require an exact match between PARM and ARG.
14047 UNIFY_ALLOW_MORE_CV_QUAL:
14048 Allow the deduced ARG to be more cv-qualified (by qualification
14049 conversion) than ARG.
14050 UNIFY_ALLOW_LESS_CV_QUAL:
14051 Allow the deduced ARG to be less cv-qualified than ARG.
14052 UNIFY_ALLOW_DERIVED:
14053 Allow the deduced ARG to be a template base class of ARG,
14054 or a pointer to a template base class of the type pointed to by
14056 UNIFY_ALLOW_INTEGER:
14057 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14058 case for more information.
14059 UNIFY_ALLOW_OUTER_LEVEL:
14060 This is the outermost level of a deduction. Used to determine validity
14061 of qualification conversions. A valid qualification conversion must
14062 have const qualified pointers leading up to the inner type which
14063 requires additional CV quals, except at the outer level, where const
14064 is not required [conv.qual]. It would be normal to set this flag in
14065 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14066 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14067 This is the outermost level of a deduction, and PARM can be more CV
14068 qualified at this point.
14069 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14070 This is the outermost level of a deduction, and PARM can be less CV
14071 qualified at this point. */
14074 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
14079 int strict_in = strict;
14081 /* I don't think this will do the right thing with respect to types.
14082 But the only case I've seen it in so far has been array bounds, where
14083 signedness is the only information lost, and I think that will be
14085 while (TREE_CODE (parm) == NOP_EXPR)
14086 parm = TREE_OPERAND (parm, 0);
14088 if (arg == error_mark_node)
14090 if (arg == unknown_type_node
14091 || arg == init_list_type_node)
14092 /* We can't deduce anything from this, but we might get all the
14093 template args from other function args. */
14096 /* If PARM uses template parameters, then we can't bail out here,
14097 even if ARG == PARM, since we won't record unifications for the
14098 template parameters. We might need them if we're trying to
14099 figure out which of two things is more specialized. */
14100 if (arg == parm && !uses_template_parms (parm))
14103 /* Handle init lists early, so the rest of the function can assume
14104 we're dealing with a type. */
14105 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
14109 tree orig_parm = parm;
14111 /* Replace T with std::initializer_list<T> for deduction. */
14112 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14113 && flag_deduce_init_list)
14114 parm = listify (parm);
14116 if (!is_std_init_list (parm))
14117 /* We can only deduce from an initializer list argument if the
14118 parameter is std::initializer_list; otherwise this is a
14119 non-deduced context. */
14122 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
14124 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
14126 int elt_strict = strict;
14127 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
14129 tree type = TREE_TYPE (elt);
14130 /* It should only be possible to get here for a call. */
14131 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
14132 elt_strict |= maybe_adjust_types_for_deduction
14133 (DEDUCE_CALL, &elttype, &type, elt);
14137 if (unify (tparms, targs, elttype, elt, elt_strict))
14141 /* If the std::initializer_list<T> deduction worked, replace the
14142 deduced A with std::initializer_list<A>. */
14143 if (orig_parm != parm)
14145 idx = TEMPLATE_TYPE_IDX (orig_parm);
14146 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14147 targ = listify (targ);
14148 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
14153 /* Immediately reject some pairs that won't unify because of
14154 cv-qualification mismatches. */
14155 if (TREE_CODE (arg) == TREE_CODE (parm)
14157 /* It is the elements of the array which hold the cv quals of an array
14158 type, and the elements might be template type parms. We'll check
14159 when we recurse. */
14160 && TREE_CODE (arg) != ARRAY_TYPE
14161 /* We check the cv-qualifiers when unifying with template type
14162 parameters below. We want to allow ARG `const T' to unify with
14163 PARM `T' for example, when computing which of two templates
14164 is more specialized, for example. */
14165 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
14166 && !check_cv_quals_for_unify (strict_in, arg, parm))
14169 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
14170 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
14171 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
14172 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
14173 strict &= ~UNIFY_ALLOW_DERIVED;
14174 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
14175 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
14177 switch (TREE_CODE (parm))
14179 case TYPENAME_TYPE:
14181 case UNBOUND_CLASS_TEMPLATE:
14182 /* In a type which contains a nested-name-specifier, template
14183 argument values cannot be deduced for template parameters used
14184 within the nested-name-specifier. */
14187 case TEMPLATE_TYPE_PARM:
14188 case TEMPLATE_TEMPLATE_PARM:
14189 case BOUND_TEMPLATE_TEMPLATE_PARM:
14190 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14191 if (tparm == error_mark_node)
14194 if (TEMPLATE_TYPE_LEVEL (parm)
14195 != template_decl_level (tparm))
14196 /* The PARM is not one we're trying to unify. Just check
14197 to see if it matches ARG. */
14198 return (TREE_CODE (arg) == TREE_CODE (parm)
14199 && same_type_p (parm, arg)) ? 0 : 1;
14200 idx = TEMPLATE_TYPE_IDX (parm);
14201 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14202 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
14204 /* Check for mixed types and values. */
14205 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
14206 && TREE_CODE (tparm) != TYPE_DECL)
14207 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14208 && TREE_CODE (tparm) != TEMPLATE_DECL))
14211 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14213 /* ARG must be constructed from a template class or a template
14214 template parameter. */
14215 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
14216 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
14220 tree parmvec = TYPE_TI_ARGS (parm);
14221 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
14223 = DECL_INNERMOST_TEMPLATE_PARMS
14224 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
14226 int parm_variadic_p = 0;
14228 /* The resolution to DR150 makes clear that default
14229 arguments for an N-argument may not be used to bind T
14230 to a template template parameter with fewer than N
14231 parameters. It is not safe to permit the binding of
14232 default arguments as an extension, as that may change
14233 the meaning of a conforming program. Consider:
14235 struct Dense { static const unsigned int dim = 1; };
14237 template <template <typename> class View,
14239 void operator+(float, View<Block> const&);
14241 template <typename Block,
14242 unsigned int Dim = Block::dim>
14243 struct Lvalue_proxy { operator float() const; };
14247 Lvalue_proxy<Dense> p;
14252 Here, if Lvalue_proxy is permitted to bind to View, then
14253 the global operator+ will be used; if they are not, the
14254 Lvalue_proxy will be converted to float. */
14255 if (coerce_template_parms (parm_parms,
14257 TYPE_TI_TEMPLATE (parm),
14259 /*require_all_args=*/true,
14260 /*use_default_args=*/false)
14261 == error_mark_node)
14264 /* Deduce arguments T, i from TT<T> or TT<i>.
14265 We check each element of PARMVEC and ARGVEC individually
14266 rather than the whole TREE_VEC since they can have
14267 different number of elements. */
14269 parmvec = expand_template_argument_pack (parmvec);
14270 argvec = expand_template_argument_pack (argvec);
14272 len = TREE_VEC_LENGTH (parmvec);
14274 /* Check if the parameters end in a pack, making them
14277 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
14278 parm_variadic_p = 1;
14280 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
14283 for (i = 0; i < len - parm_variadic_p; ++i)
14285 if (unify (tparms, targs,
14286 TREE_VEC_ELT (parmvec, i),
14287 TREE_VEC_ELT (argvec, i),
14292 if (parm_variadic_p
14293 && unify_pack_expansion (tparms, targs,
14296 /*call_args_p=*/false,
14300 arg = TYPE_TI_TEMPLATE (arg);
14302 /* Fall through to deduce template name. */
14305 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
14306 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
14308 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14310 /* Simple cases: Value already set, does match or doesn't. */
14311 if (targ != NULL_TREE && template_args_equal (targ, arg))
14318 /* If PARM is `const T' and ARG is only `int', we don't have
14319 a match unless we are allowing additional qualification.
14320 If ARG is `const int' and PARM is just `T' that's OK;
14321 that binds `const int' to `T'. */
14322 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
14326 /* Consider the case where ARG is `const volatile int' and
14327 PARM is `const T'. Then, T should be `volatile int'. */
14328 arg = cp_build_qualified_type_real
14329 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
14330 if (arg == error_mark_node)
14333 /* Simple cases: Value already set, does match or doesn't. */
14334 if (targ != NULL_TREE && same_type_p (targ, arg))
14339 /* Make sure that ARG is not a variable-sized array. (Note
14340 that were talking about variable-sized arrays (like
14341 `int[n]'), rather than arrays of unknown size (like
14342 `int[]').) We'll get very confused by such a type since
14343 the bound of the array will not be computable in an
14344 instantiation. Besides, such types are not allowed in
14345 ISO C++, so we can do as we please here. */
14346 if (variably_modified_type_p (arg, NULL_TREE))
14349 /* Strip typedefs as in convert_template_argument. */
14350 arg = strip_typedefs (arg);
14353 /* If ARG is a parameter pack or an expansion, we cannot unify
14354 against it unless PARM is also a parameter pack. */
14355 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14356 && !template_parameter_pack_p (parm))
14359 /* If the argument deduction results is a METHOD_TYPE,
14360 then there is a problem.
14361 METHOD_TYPE doesn't map to any real C++ type the result of
14362 the deduction can not be of that type. */
14363 if (TREE_CODE (arg) == METHOD_TYPE)
14366 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14369 case TEMPLATE_PARM_INDEX:
14370 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
14371 if (tparm == error_mark_node)
14374 if (TEMPLATE_PARM_LEVEL (parm)
14375 != template_decl_level (tparm))
14376 /* The PARM is not one we're trying to unify. Just check
14377 to see if it matches ARG. */
14378 return !(TREE_CODE (arg) == TREE_CODE (parm)
14379 && cp_tree_equal (parm, arg));
14381 idx = TEMPLATE_PARM_IDX (parm);
14382 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
14385 return !cp_tree_equal (targ, arg);
14387 /* [temp.deduct.type] If, in the declaration of a function template
14388 with a non-type template-parameter, the non-type
14389 template-parameter is used in an expression in the function
14390 parameter-list and, if the corresponding template-argument is
14391 deduced, the template-argument type shall match the type of the
14392 template-parameter exactly, except that a template-argument
14393 deduced from an array bound may be of any integral type.
14394 The non-type parameter might use already deduced type parameters. */
14395 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
14396 if (!TREE_TYPE (arg))
14397 /* Template-parameter dependent expression. Just accept it for now.
14398 It will later be processed in convert_template_argument. */
14400 else if (same_type_p (TREE_TYPE (arg), tparm))
14402 else if ((strict & UNIFY_ALLOW_INTEGER)
14403 && (TREE_CODE (tparm) == INTEGER_TYPE
14404 || TREE_CODE (tparm) == BOOLEAN_TYPE))
14405 /* Convert the ARG to the type of PARM; the deduced non-type
14406 template argument must exactly match the types of the
14407 corresponding parameter. */
14408 arg = fold (build_nop (tparm, arg));
14409 else if (uses_template_parms (tparm))
14410 /* We haven't deduced the type of this parameter yet. Try again
14416 /* If ARG is a parameter pack or an expansion, we cannot unify
14417 against it unless PARM is also a parameter pack. */
14418 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14419 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14422 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
14427 /* A pointer-to-member constant can be unified only with
14428 another constant. */
14429 if (TREE_CODE (arg) != PTRMEM_CST)
14432 /* Just unify the class member. It would be useless (and possibly
14433 wrong, depending on the strict flags) to unify also
14434 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14435 arg refer to the same variable, even if through different
14436 classes. For instance:
14438 struct A { int x; };
14441 Unification of &A::x and &B::x must succeed. */
14442 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
14443 PTRMEM_CST_MEMBER (arg), strict);
14448 if (TREE_CODE (arg) != POINTER_TYPE)
14451 /* [temp.deduct.call]
14453 A can be another pointer or pointer to member type that can
14454 be converted to the deduced A via a qualification
14455 conversion (_conv.qual_).
14457 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14458 This will allow for additional cv-qualification of the
14459 pointed-to types if appropriate. */
14461 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
14462 /* The derived-to-base conversion only persists through one
14463 level of pointers. */
14464 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
14466 return unify (tparms, targs, TREE_TYPE (parm),
14467 TREE_TYPE (arg), strict);
14470 case REFERENCE_TYPE:
14471 if (TREE_CODE (arg) != REFERENCE_TYPE)
14473 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14474 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14477 if (TREE_CODE (arg) != ARRAY_TYPE)
14479 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14480 != (TYPE_DOMAIN (arg) == NULL_TREE))
14482 if (TYPE_DOMAIN (parm) != NULL_TREE)
14489 /* Our representation of array types uses "N - 1" as the
14490 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14491 not an integer constant. We cannot unify arbitrarily
14492 complex expressions, so we eliminate the MINUS_EXPRs
14494 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14495 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14498 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
14499 parm_max = TREE_OPERAND (parm_max, 0);
14501 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14502 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14505 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14506 trying to unify the type of a variable with the type
14507 of a template parameter. For example:
14509 template <unsigned int N>
14510 void f (char (&) [N]);
14517 Here, the type of the ARG will be "int [g(i)]", and
14518 may be a SAVE_EXPR, etc. */
14519 if (TREE_CODE (arg_max) != MINUS_EXPR)
14521 arg_max = TREE_OPERAND (arg_max, 0);
14524 /* If only one of the bounds used a MINUS_EXPR, compensate
14525 by adding one to the other bound. */
14526 if (parm_cst && !arg_cst)
14527 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
14531 else if (arg_cst && !parm_cst)
14532 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
14537 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
14540 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14541 strict & UNIFY_ALLOW_MORE_CV_QUAL);
14548 case ENUMERAL_TYPE:
14550 if (TREE_CODE (arg) != TREE_CODE (parm))
14553 /* We have already checked cv-qualification at the top of the
14555 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
14558 /* As far as unification is concerned, this wins. Later checks
14559 will invalidate it if necessary. */
14562 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
14563 /* Type INTEGER_CST can come from ordinary constant template args. */
14565 while (TREE_CODE (arg) == NOP_EXPR)
14566 arg = TREE_OPERAND (arg, 0);
14568 if (TREE_CODE (arg) != INTEGER_CST)
14570 return !tree_int_cst_equal (parm, arg);
14575 if (TREE_CODE (arg) != TREE_VEC)
14577 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
14579 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
14580 if (unify (tparms, targs,
14581 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
14589 if (TREE_CODE (arg) != TREE_CODE (parm))
14592 if (TYPE_PTRMEMFUNC_P (parm))
14594 if (!TYPE_PTRMEMFUNC_P (arg))
14597 return unify (tparms, targs,
14598 TYPE_PTRMEMFUNC_FN_TYPE (parm),
14599 TYPE_PTRMEMFUNC_FN_TYPE (arg),
14603 if (CLASSTYPE_TEMPLATE_INFO (parm))
14605 tree t = NULL_TREE;
14607 if (strict_in & UNIFY_ALLOW_DERIVED)
14609 /* First, we try to unify the PARM and ARG directly. */
14610 t = try_class_unification (tparms, targs,
14615 /* Fallback to the special case allowed in
14616 [temp.deduct.call]:
14618 If P is a class, and P has the form
14619 template-id, then A can be a derived class of
14620 the deduced A. Likewise, if P is a pointer to
14621 a class of the form template-id, A can be a
14622 pointer to a derived class pointed to by the
14624 t = get_template_base (tparms, targs, parm, arg);
14630 else if (CLASSTYPE_TEMPLATE_INFO (arg)
14631 && (CLASSTYPE_TI_TEMPLATE (parm)
14632 == CLASSTYPE_TI_TEMPLATE (arg)))
14633 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14634 Then, we should unify `int' and `U'. */
14637 /* There's no chance of unification succeeding. */
14640 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
14641 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
14643 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
14648 case FUNCTION_TYPE:
14650 unsigned int nargs;
14655 if (TREE_CODE (arg) != TREE_CODE (parm))
14658 /* CV qualifications for methods can never be deduced, they must
14659 match exactly. We need to check them explicitly here,
14660 because type_unification_real treats them as any other
14661 cv-qualified parameter. */
14662 if (TREE_CODE (parm) == METHOD_TYPE
14663 && (!check_cv_quals_for_unify
14665 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
14666 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
14669 if (unify (tparms, targs, TREE_TYPE (parm),
14670 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
14673 nargs = list_length (TYPE_ARG_TYPES (arg));
14674 args = XALLOCAVEC (tree, nargs);
14675 for (a = TYPE_ARG_TYPES (arg), i = 0;
14676 a != NULL_TREE && a != void_list_node;
14677 a = TREE_CHAIN (a), ++i)
14678 args[i] = TREE_VALUE (a);
14681 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
14682 args, nargs, 1, DEDUCE_EXACT,
14687 /* Unify a pointer to member with a pointer to member function, which
14688 deduces the type of the member as a function type. */
14689 if (TYPE_PTRMEMFUNC_P (arg))
14693 cp_cv_quals cv_quals;
14695 /* Check top-level cv qualifiers */
14696 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
14699 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14700 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
14703 /* Determine the type of the function we are unifying against. */
14704 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
14706 build_function_type (TREE_TYPE (method_type),
14707 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
14709 /* Extract the cv-qualifiers of the member function from the
14710 implicit object parameter and place them on the function
14711 type to be restored later. */
14713 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
14714 fntype = build_qualified_type (fntype, cv_quals);
14715 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
14718 if (TREE_CODE (arg) != OFFSET_TYPE)
14720 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14721 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
14723 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
14727 if (DECL_TEMPLATE_PARM_P (parm))
14728 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
14729 if (arg != integral_constant_value (parm))
14734 case TEMPLATE_DECL:
14735 /* Matched cases are handled by the ARG == PARM test above. */
14738 case TYPE_ARGUMENT_PACK:
14739 case NONTYPE_ARGUMENT_PACK:
14741 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
14742 tree packed_args = ARGUMENT_PACK_ARGS (arg);
14743 int i, len = TREE_VEC_LENGTH (packed_parms);
14744 int argslen = TREE_VEC_LENGTH (packed_args);
14745 int parm_variadic_p = 0;
14747 for (i = 0; i < len; ++i)
14749 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
14752 /* We can unify against something with a trailing
14754 parm_variadic_p = 1;
14756 /* Since there is something following the pack
14757 expansion, we cannot unify this template argument
14764 /* If we don't have enough arguments to satisfy the parameters
14765 (not counting the pack expression at the end), or we have
14766 too many arguments for a parameter list that doesn't end in
14767 a pack expression, we can't unify. */
14768 if (argslen < (len - parm_variadic_p)
14769 || (argslen > len && !parm_variadic_p))
14772 /* Unify all of the parameters that precede the (optional)
14773 pack expression. */
14774 for (i = 0; i < len - parm_variadic_p; ++i)
14776 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
14777 TREE_VEC_ELT (packed_args, i), strict))
14781 if (parm_variadic_p)
14782 return unify_pack_expansion (tparms, targs,
14783 packed_parms, packed_args,
14784 strict, /*call_args_p=*/false,
14792 case DECLTYPE_TYPE:
14793 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14798 /* Unification fails if we hit an error node. */
14802 gcc_assert (EXPR_P (parm));
14804 /* We must be looking at an expression. This can happen with
14808 void foo(S<I>, S<I + 2>);
14810 This is a "nondeduced context":
14814 The nondeduced contexts are:
14816 --A type that is a template-id in which one or more of
14817 the template-arguments is an expression that references
14818 a template-parameter.
14820 In these cases, we assume deduction succeeded, but don't
14821 actually infer any unifications. */
14823 if (!uses_template_parms (parm)
14824 && !template_args_equal (parm, arg))
14831 /* Note that DECL can be defined in this translation unit, if
14835 mark_definable (tree decl)
14838 DECL_NOT_REALLY_EXTERN (decl) = 1;
14839 FOR_EACH_CLONE (clone, decl)
14840 DECL_NOT_REALLY_EXTERN (clone) = 1;
14843 /* Called if RESULT is explicitly instantiated, or is a member of an
14844 explicitly instantiated class. */
14847 mark_decl_instantiated (tree result, int extern_p)
14849 SET_DECL_EXPLICIT_INSTANTIATION (result);
14851 /* If this entity has already been written out, it's too late to
14852 make any modifications. */
14853 if (TREE_ASM_WRITTEN (result))
14856 if (TREE_CODE (result) != FUNCTION_DECL)
14857 /* The TREE_PUBLIC flag for function declarations will have been
14858 set correctly by tsubst. */
14859 TREE_PUBLIC (result) = 1;
14861 /* This might have been set by an earlier implicit instantiation. */
14862 DECL_COMDAT (result) = 0;
14865 DECL_NOT_REALLY_EXTERN (result) = 0;
14868 mark_definable (result);
14869 /* Always make artificials weak. */
14870 if (DECL_ARTIFICIAL (result) && flag_weak)
14871 comdat_linkage (result);
14872 /* For WIN32 we also want to put explicit instantiations in
14873 linkonce sections. */
14874 else if (TREE_PUBLIC (result))
14875 maybe_make_one_only (result);
14878 /* If EXTERN_P, then this function will not be emitted -- unless
14879 followed by an explicit instantiation, at which point its linkage
14880 will be adjusted. If !EXTERN_P, then this function will be
14881 emitted here. In neither circumstance do we want
14882 import_export_decl to adjust the linkage. */
14883 DECL_INTERFACE_KNOWN (result) = 1;
14886 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
14887 important template arguments. If any are missing, we check whether
14888 they're important by using error_mark_node for substituting into any
14889 args that were used for partial ordering (the ones between ARGS and END)
14890 and seeing if it bubbles up. */
14893 check_undeduced_parms (tree targs, tree args, tree end)
14895 bool found = false;
14897 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
14898 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
14901 TREE_VEC_ELT (targs, i) = error_mark_node;
14905 for (; args != end; args = TREE_CHAIN (args))
14907 tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
14908 if (substed == error_mark_node)
14915 /* Given two function templates PAT1 and PAT2, return:
14917 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
14918 -1 if PAT2 is more specialized than PAT1.
14919 0 if neither is more specialized.
14921 LEN indicates the number of parameters we should consider
14922 (defaulted parameters should not be considered).
14924 The 1998 std underspecified function template partial ordering, and
14925 DR214 addresses the issue. We take pairs of arguments, one from
14926 each of the templates, and deduce them against each other. One of
14927 the templates will be more specialized if all the *other*
14928 template's arguments deduce against its arguments and at least one
14929 of its arguments *does* *not* deduce against the other template's
14930 corresponding argument. Deduction is done as for class templates.
14931 The arguments used in deduction have reference and top level cv
14932 qualifiers removed. Iff both arguments were originally reference
14933 types *and* deduction succeeds in both directions, the template
14934 with the more cv-qualified argument wins for that pairing (if
14935 neither is more cv-qualified, they both are equal). Unlike regular
14936 deduction, after all the arguments have been deduced in this way,
14937 we do *not* verify the deduced template argument values can be
14938 substituted into non-deduced contexts.
14940 The logic can be a bit confusing here, because we look at deduce1 and
14941 targs1 to see if pat2 is at least as specialized, and vice versa; if we
14942 can find template arguments for pat1 to make arg1 look like arg2, that
14943 means that arg2 is at least as specialized as arg1. */
14946 more_specialized_fn (tree pat1, tree pat2, int len)
14948 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
14949 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
14950 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
14951 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
14952 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
14953 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
14954 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
14955 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
14956 tree origs1, origs2;
14957 bool lose1 = false;
14958 bool lose2 = false;
14960 /* Remove the this parameter from non-static member functions. If
14961 one is a non-static member function and the other is not a static
14962 member function, remove the first parameter from that function
14963 also. This situation occurs for operator functions where we
14964 locate both a member function (with this pointer) and non-member
14965 operator (with explicit first operand). */
14966 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
14968 len--; /* LEN is the number of significant arguments for DECL1 */
14969 args1 = TREE_CHAIN (args1);
14970 if (!DECL_STATIC_FUNCTION_P (decl2))
14971 args2 = TREE_CHAIN (args2);
14973 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
14975 args2 = TREE_CHAIN (args2);
14976 if (!DECL_STATIC_FUNCTION_P (decl1))
14979 args1 = TREE_CHAIN (args1);
14983 /* If only one is a conversion operator, they are unordered. */
14984 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
14987 /* Consider the return type for a conversion function */
14988 if (DECL_CONV_FN_P (decl1))
14990 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
14991 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
14995 processing_template_decl++;
15001 /* Stop when an ellipsis is seen. */
15002 && args1 != NULL_TREE && args2 != NULL_TREE)
15004 tree arg1 = TREE_VALUE (args1);
15005 tree arg2 = TREE_VALUE (args2);
15006 int deduce1, deduce2;
15010 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15011 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15013 /* When both arguments are pack expansions, we need only
15014 unify the patterns themselves. */
15015 arg1 = PACK_EXPANSION_PATTERN (arg1);
15016 arg2 = PACK_EXPANSION_PATTERN (arg2);
15018 /* This is the last comparison we need to do. */
15022 if (TREE_CODE (arg1) == REFERENCE_TYPE)
15024 arg1 = TREE_TYPE (arg1);
15025 quals1 = cp_type_quals (arg1);
15028 if (TREE_CODE (arg2) == REFERENCE_TYPE)
15030 arg2 = TREE_TYPE (arg2);
15031 quals2 = cp_type_quals (arg2);
15034 if ((quals1 < 0) != (quals2 < 0))
15036 /* Only of the args is a reference, see if we should apply
15037 array/function pointer decay to it. This is not part of
15038 DR214, but is, IMHO, consistent with the deduction rules
15039 for the function call itself, and with our earlier
15040 implementation of the underspecified partial ordering
15041 rules. (nathan). */
15044 switch (TREE_CODE (arg1))
15047 arg1 = TREE_TYPE (arg1);
15049 case FUNCTION_TYPE:
15050 arg1 = build_pointer_type (arg1);
15059 switch (TREE_CODE (arg2))
15062 arg2 = TREE_TYPE (arg2);
15064 case FUNCTION_TYPE:
15065 arg2 = build_pointer_type (arg2);
15074 arg1 = TYPE_MAIN_VARIANT (arg1);
15075 arg2 = TYPE_MAIN_VARIANT (arg2);
15077 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
15079 int i, len2 = list_length (args2);
15080 tree parmvec = make_tree_vec (1);
15081 tree argvec = make_tree_vec (len2);
15084 /* Setup the parameter vector, which contains only ARG1. */
15085 TREE_VEC_ELT (parmvec, 0) = arg1;
15087 /* Setup the argument vector, which contains the remaining
15089 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
15090 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15092 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
15093 argvec, UNIFY_ALLOW_NONE,
15094 /*call_args_p=*/false,
15097 /* We cannot deduce in the other direction, because ARG1 is
15098 a pack expansion but ARG2 is not. */
15101 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15103 int i, len1 = list_length (args1);
15104 tree parmvec = make_tree_vec (1);
15105 tree argvec = make_tree_vec (len1);
15108 /* Setup the parameter vector, which contains only ARG1. */
15109 TREE_VEC_ELT (parmvec, 0) = arg2;
15111 /* Setup the argument vector, which contains the remaining
15113 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
15114 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
15116 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
15117 argvec, UNIFY_ALLOW_NONE,
15118 /*call_args_p=*/false,
15121 /* We cannot deduce in the other direction, because ARG2 is
15122 a pack expansion but ARG1 is not.*/
15128 /* The normal case, where neither argument is a pack
15130 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
15131 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
15134 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15135 arg2, then arg2 is not as specialized as arg1. */
15141 /* "If, for a given type, deduction succeeds in both directions
15142 (i.e., the types are identical after the transformations above)
15143 and if the type from the argument template is more cv-qualified
15144 than the type from the parameter template (as described above)
15145 that type is considered to be more specialized than the other. If
15146 neither type is more cv-qualified than the other then neither type
15147 is more specialized than the other."
15149 We check same_type_p explicitly because deduction can also succeed
15150 in both directions when there is a nondeduced context. */
15151 if (deduce1 && deduce2
15152 && quals1 != quals2 && quals1 >= 0 && quals2 >= 0
15153 && same_type_p (arg1, arg2))
15155 if ((quals1 & quals2) == quals2)
15157 if ((quals1 & quals2) == quals1)
15161 if (lose1 && lose2)
15162 /* We've failed to deduce something in either direction.
15163 These must be unordered. */
15166 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
15167 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
15168 /* We have already processed all of the arguments in our
15169 handing of the pack expansion type. */
15172 args1 = TREE_CHAIN (args1);
15173 args2 = TREE_CHAIN (args2);
15176 /* "In most cases, all template parameters must have values in order for
15177 deduction to succeed, but for partial ordering purposes a template
15178 parameter may remain without a value provided it is not used in the
15179 types being used for partial ordering."
15181 Thus, if we are missing any of the targs1 we need to substitute into
15182 origs1, then pat2 is not as specialized as pat1. This can happen when
15183 there is a nondeduced context. */
15184 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
15186 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
15189 processing_template_decl--;
15191 /* All things being equal, if the next argument is a pack expansion
15192 for one function but not for the other, prefer the
15193 non-variadic function. FIXME this is bogus; see c++/41958. */
15195 && args1 && TREE_VALUE (args1)
15196 && args2 && TREE_VALUE (args2))
15198 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
15199 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
15202 if (lose1 == lose2)
15210 /* Determine which of two partial specializations is more specialized.
15212 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15213 to the first partial specialization. The TREE_VALUE is the
15214 innermost set of template parameters for the partial
15215 specialization. PAT2 is similar, but for the second template.
15217 Return 1 if the first partial specialization is more specialized;
15218 -1 if the second is more specialized; 0 if neither is more
15221 See [temp.class.order] for information about determining which of
15222 two templates is more specialized. */
15225 more_specialized_class (tree pat1, tree pat2)
15230 bool any_deductions = false;
15232 tmpl1 = TREE_TYPE (pat1);
15233 tmpl2 = TREE_TYPE (pat2);
15235 /* Just like what happens for functions, if we are ordering between
15236 different class template specializations, we may encounter dependent
15237 types in the arguments, and we need our dependency check functions
15238 to behave correctly. */
15239 ++processing_template_decl;
15240 targs = get_class_bindings (TREE_VALUE (pat1),
15241 CLASSTYPE_TI_ARGS (tmpl1),
15242 CLASSTYPE_TI_ARGS (tmpl2));
15246 any_deductions = true;
15249 targs = get_class_bindings (TREE_VALUE (pat2),
15250 CLASSTYPE_TI_ARGS (tmpl2),
15251 CLASSTYPE_TI_ARGS (tmpl1));
15255 any_deductions = true;
15257 --processing_template_decl;
15259 /* In the case of a tie where at least one of the class templates
15260 has a parameter pack at the end, the template with the most
15261 non-packed parameters wins. */
15264 && (template_args_variadic_p (TREE_PURPOSE (pat1))
15265 || template_args_variadic_p (TREE_PURPOSE (pat2))))
15267 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
15268 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
15269 int len1 = TREE_VEC_LENGTH (args1);
15270 int len2 = TREE_VEC_LENGTH (args2);
15272 /* We don't count the pack expansion at the end. */
15273 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
15275 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
15280 else if (len1 < len2)
15287 /* Return the template arguments that will produce the function signature
15288 DECL from the function template FN, with the explicit template
15289 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15290 also match. Return NULL_TREE if no satisfactory arguments could be
15294 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
15296 int ntparms = DECL_NTPARMS (fn);
15297 tree targs = make_tree_vec (ntparms);
15299 tree decl_arg_types;
15301 unsigned int nargs, ix;
15304 /* Substitute the explicit template arguments into the type of DECL.
15305 The call to fn_type_unification will handle substitution into the
15307 decl_type = TREE_TYPE (decl);
15308 if (explicit_args && uses_template_parms (decl_type))
15311 tree converted_args;
15313 if (DECL_TEMPLATE_INFO (decl))
15314 tmpl = DECL_TI_TEMPLATE (decl);
15316 /* We can get here for some invalid specializations. */
15320 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
15321 explicit_args, NULL_TREE,
15323 /*require_all_args=*/false,
15324 /*use_default_args=*/false);
15325 if (converted_args == error_mark_node)
15328 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
15329 if (decl_type == error_mark_node)
15333 /* Never do unification on the 'this' parameter. */
15334 decl_arg_types = skip_artificial_parms_for (decl,
15335 TYPE_ARG_TYPES (decl_type));
15337 nargs = list_length (decl_arg_types);
15338 args = XALLOCAVEC (tree, nargs);
15339 for (arg = decl_arg_types, ix = 0;
15340 arg != NULL_TREE && arg != void_list_node;
15341 arg = TREE_CHAIN (arg), ++ix)
15342 args[ix] = TREE_VALUE (arg);
15344 if (fn_type_unification (fn, explicit_args, targs,
15346 (check_rettype || DECL_CONV_FN_P (fn)
15347 ? TREE_TYPE (decl_type) : NULL_TREE),
15348 DEDUCE_EXACT, LOOKUP_NORMAL))
15354 /* Return the innermost template arguments that, when applied to a
15355 template specialization whose innermost template parameters are
15356 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15359 For example, suppose we have:
15361 template <class T, class U> struct S {};
15362 template <class T> struct S<T*, int> {};
15364 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15365 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15366 int}. The resulting vector will be {double}, indicating that `T'
15367 is bound to `double'. */
15370 get_class_bindings (tree tparms, tree spec_args, tree args)
15372 int i, ntparms = TREE_VEC_LENGTH (tparms);
15374 tree innermost_deduced_args;
15376 innermost_deduced_args = make_tree_vec (ntparms);
15377 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15379 deduced_args = copy_node (args);
15380 SET_TMPL_ARGS_LEVEL (deduced_args,
15381 TMPL_ARGS_DEPTH (deduced_args),
15382 innermost_deduced_args);
15385 deduced_args = innermost_deduced_args;
15387 if (unify (tparms, deduced_args,
15388 INNERMOST_TEMPLATE_ARGS (spec_args),
15389 INNERMOST_TEMPLATE_ARGS (args),
15393 for (i = 0; i < ntparms; ++i)
15394 if (! TREE_VEC_ELT (innermost_deduced_args, i))
15397 /* Verify that nondeduced template arguments agree with the type
15398 obtained from argument deduction.
15402 struct A { typedef int X; };
15403 template <class T, class U> struct C {};
15404 template <class T> struct C<T, typename T::X> {};
15406 Then with the instantiation `C<A, int>', we can deduce that
15407 `T' is `A' but unify () does not check whether `typename T::X'
15409 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
15410 if (spec_args == error_mark_node
15411 /* We only need to check the innermost arguments; the other
15412 arguments will always agree. */
15413 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
15414 INNERMOST_TEMPLATE_ARGS (args)))
15417 /* Now that we have bindings for all of the template arguments,
15418 ensure that the arguments deduced for the template template
15419 parameters have compatible template parameter lists. See the use
15420 of template_template_parm_bindings_ok_p in fn_type_unification
15421 for more information. */
15422 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
15425 return deduced_args;
15428 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15429 Return the TREE_LIST node with the most specialized template, if
15430 any. If there is no most specialized template, the error_mark_node
15433 Note that this function does not look at, or modify, the
15434 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15435 returned is one of the elements of INSTANTIATIONS, callers may
15436 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15437 and retrieve it from the value returned. */
15440 most_specialized_instantiation (tree templates)
15444 ++processing_template_decl;
15447 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
15451 if (get_bindings (TREE_VALUE (champ),
15452 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15453 NULL_TREE, /*check_ret=*/false))
15456 if (get_bindings (TREE_VALUE (fn),
15457 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15458 NULL_TREE, /*check_ret=*/false))
15465 /* Equally specialized, move to next function. If there
15466 is no next function, nothing's most specialized. */
15467 fn = TREE_CHAIN (fn);
15475 /* Now verify that champ is better than everything earlier in the
15476 instantiation list. */
15477 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
15478 if (get_bindings (TREE_VALUE (champ),
15479 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15480 NULL_TREE, /*check_ret=*/false)
15481 || !get_bindings (TREE_VALUE (fn),
15482 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15483 NULL_TREE, /*check_ret=*/false))
15489 processing_template_decl--;
15492 return error_mark_node;
15497 /* If DECL is a specialization of some template, return the most
15498 general such template. Otherwise, returns NULL_TREE.
15500 For example, given:
15502 template <class T> struct S { template <class U> void f(U); };
15504 if TMPL is `template <class U> void S<int>::f(U)' this will return
15505 the full template. This function will not trace past partial
15506 specializations, however. For example, given in addition:
15508 template <class T> struct S<T*> { template <class U> void f(U); };
15510 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15511 `template <class T> template <class U> S<T*>::f(U)'. */
15514 most_general_template (tree decl)
15516 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15517 an immediate specialization. */
15518 if (TREE_CODE (decl) == FUNCTION_DECL)
15520 if (DECL_TEMPLATE_INFO (decl)) {
15521 decl = DECL_TI_TEMPLATE (decl);
15523 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15524 template friend. */
15525 if (TREE_CODE (decl) != TEMPLATE_DECL)
15531 /* Look for more and more general templates. */
15532 while (DECL_TEMPLATE_INFO (decl))
15534 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15535 (See cp-tree.h for details.) */
15536 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
15539 if (CLASS_TYPE_P (TREE_TYPE (decl))
15540 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
15543 /* Stop if we run into an explicitly specialized class template. */
15544 if (!DECL_NAMESPACE_SCOPE_P (decl)
15545 && DECL_CONTEXT (decl)
15546 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
15549 decl = DECL_TI_TEMPLATE (decl);
15555 /* Return the most specialized of the class template partial
15556 specializations of TMPL which can produce TYPE, a specialization of
15557 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15558 a _TYPE node corresponding to the partial specialization, while the
15559 TREE_PURPOSE is the set of template arguments that must be
15560 substituted into the TREE_TYPE in order to generate TYPE.
15562 If the choice of partial specialization is ambiguous, a diagnostic
15563 is issued, and the error_mark_node is returned. If there are no
15564 partial specializations of TMPL matching TYPE, then NULL_TREE is
15568 most_specialized_class (tree type, tree tmpl)
15570 tree list = NULL_TREE;
15576 tree outer_args = NULL_TREE;
15578 tmpl = most_general_template (tmpl);
15579 args = CLASSTYPE_TI_ARGS (type);
15581 /* For determining which partial specialization to use, only the
15582 innermost args are interesting. */
15583 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15585 outer_args = strip_innermost_template_args (args, 1);
15586 args = INNERMOST_TEMPLATE_ARGS (args);
15589 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
15591 tree partial_spec_args;
15593 tree parms = TREE_VALUE (t);
15595 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
15600 ++processing_template_decl;
15602 /* Discard the outer levels of args, and then substitute in the
15603 template args from the enclosing class. */
15604 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
15605 partial_spec_args = tsubst_template_args
15606 (partial_spec_args, outer_args, tf_none, NULL_TREE);
15608 /* PARMS already refers to just the innermost parms, but the
15609 template parms in partial_spec_args had their levels lowered
15610 by tsubst, so we need to do the same for the parm list. We
15611 can't just tsubst the TREE_VEC itself, as tsubst wants to
15612 treat a TREE_VEC as an argument vector. */
15613 parms = copy_node (parms);
15614 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
15615 TREE_VEC_ELT (parms, i) =
15616 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
15618 --processing_template_decl;
15620 spec_args = get_class_bindings (parms,
15626 spec_args = add_to_template_args (outer_args, spec_args);
15627 list = tree_cons (spec_args, TREE_VALUE (t), list);
15628 TREE_TYPE (list) = TREE_TYPE (t);
15635 ambiguous_p = false;
15638 t = TREE_CHAIN (t);
15639 for (; t; t = TREE_CHAIN (t))
15641 fate = more_specialized_class (champ, t);
15648 t = TREE_CHAIN (t);
15651 ambiguous_p = true;
15660 for (t = list; t && t != champ; t = TREE_CHAIN (t))
15662 fate = more_specialized_class (champ, t);
15665 ambiguous_p = true;
15672 const char *str = "candidates are:";
15673 error ("ambiguous class template instantiation for %q#T", type);
15674 for (t = list; t; t = TREE_CHAIN (t))
15676 error ("%s %+#T", str, TREE_TYPE (t));
15679 return error_mark_node;
15685 /* Explicitly instantiate DECL. */
15688 do_decl_instantiation (tree decl, tree storage)
15690 tree result = NULL_TREE;
15693 if (!decl || decl == error_mark_node)
15694 /* An error occurred, for which grokdeclarator has already issued
15695 an appropriate message. */
15697 else if (! DECL_LANG_SPECIFIC (decl))
15699 error ("explicit instantiation of non-template %q#D", decl);
15702 else if (TREE_CODE (decl) == VAR_DECL)
15704 /* There is an asymmetry here in the way VAR_DECLs and
15705 FUNCTION_DECLs are handled by grokdeclarator. In the case of
15706 the latter, the DECL we get back will be marked as a
15707 template instantiation, and the appropriate
15708 DECL_TEMPLATE_INFO will be set up. This does not happen for
15709 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
15710 should handle VAR_DECLs as it currently handles
15712 if (!DECL_CLASS_SCOPE_P (decl))
15714 error ("%qD is not a static data member of a class template", decl);
15717 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
15718 if (!result || TREE_CODE (result) != VAR_DECL)
15720 error ("no matching template for %qD found", decl);
15723 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
15725 error ("type %qT for explicit instantiation %qD does not match "
15726 "declared type %qT", TREE_TYPE (result), decl,
15731 else if (TREE_CODE (decl) != FUNCTION_DECL)
15733 error ("explicit instantiation of %q#D", decl);
15739 /* Check for various error cases. Note that if the explicit
15740 instantiation is valid the RESULT will currently be marked as an
15741 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
15742 until we get here. */
15744 if (DECL_TEMPLATE_SPECIALIZATION (result))
15746 /* DR 259 [temp.spec].
15748 Both an explicit instantiation and a declaration of an explicit
15749 specialization shall not appear in a program unless the explicit
15750 instantiation follows a declaration of the explicit specialization.
15752 For a given set of template parameters, if an explicit
15753 instantiation of a template appears after a declaration of an
15754 explicit specialization for that template, the explicit
15755 instantiation has no effect. */
15758 else if (DECL_EXPLICIT_INSTANTIATION (result))
15762 No program shall explicitly instantiate any template more
15765 We check DECL_NOT_REALLY_EXTERN so as not to complain when
15766 the first instantiation was `extern' and the second is not,
15767 and EXTERN_P for the opposite case. */
15768 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
15769 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
15770 /* If an "extern" explicit instantiation follows an ordinary
15771 explicit instantiation, the template is instantiated. */
15775 else if (!DECL_IMPLICIT_INSTANTIATION (result))
15777 error ("no matching template for %qD found", result);
15780 else if (!DECL_TEMPLATE_INFO (result))
15782 permerror (input_location, "explicit instantiation of non-template %q#D", result);
15786 if (storage == NULL_TREE)
15788 else if (storage == ridpointers[(int) RID_EXTERN])
15790 if (!in_system_header && (cxx_dialect == cxx98))
15791 pedwarn (input_location, OPT_pedantic,
15792 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
15797 error ("storage class %qD applied to template instantiation", storage);
15799 check_explicit_instantiation_namespace (result);
15800 mark_decl_instantiated (result, extern_p);
15802 instantiate_decl (result, /*defer_ok=*/1,
15803 /*expl_inst_class_mem_p=*/false);
15807 mark_class_instantiated (tree t, int extern_p)
15809 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
15810 SET_CLASSTYPE_INTERFACE_KNOWN (t);
15811 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
15812 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
15815 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
15816 rest_of_type_compilation (t, 1);
15820 /* Called from do_type_instantiation through binding_table_foreach to
15821 do recursive instantiation for the type bound in ENTRY. */
15823 bt_instantiate_type_proc (binding_entry entry, void *data)
15825 tree storage = *(tree *) data;
15827 if (MAYBE_CLASS_TYPE_P (entry->type)
15828 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
15829 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
15832 /* Called from do_type_instantiation to instantiate a member
15833 (a member function or a static member variable) of an
15834 explicitly instantiated class template. */
15836 instantiate_class_member (tree decl, int extern_p)
15838 mark_decl_instantiated (decl, extern_p);
15840 instantiate_decl (decl, /*defer_ok=*/1,
15841 /*expl_inst_class_mem_p=*/true);
15844 /* Perform an explicit instantiation of template class T. STORAGE, if
15845 non-null, is the RID for extern, inline or static. COMPLAIN is
15846 nonzero if this is called from the parser, zero if called recursively,
15847 since the standard is unclear (as detailed below). */
15850 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
15855 int previous_instantiation_extern_p = 0;
15857 if (TREE_CODE (t) == TYPE_DECL)
15860 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
15862 error ("explicit instantiation of non-template type %qT", t);
15868 if (!COMPLETE_TYPE_P (t))
15870 if (complain & tf_error)
15871 error ("explicit instantiation of %q#T before definition of template",
15876 if (storage != NULL_TREE)
15878 if (!in_system_header)
15880 if (storage == ridpointers[(int) RID_EXTERN])
15882 if (cxx_dialect == cxx98)
15883 pedwarn (input_location, OPT_pedantic,
15884 "ISO C++ 1998 forbids the use of %<extern%> on "
15885 "explicit instantiations");
15888 pedwarn (input_location, OPT_pedantic,
15889 "ISO C++ forbids the use of %qE"
15890 " on explicit instantiations", storage);
15893 if (storage == ridpointers[(int) RID_INLINE])
15895 else if (storage == ridpointers[(int) RID_EXTERN])
15897 else if (storage == ridpointers[(int) RID_STATIC])
15901 error ("storage class %qD applied to template instantiation",
15907 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
15909 /* DR 259 [temp.spec].
15911 Both an explicit instantiation and a declaration of an explicit
15912 specialization shall not appear in a program unless the explicit
15913 instantiation follows a declaration of the explicit specialization.
15915 For a given set of template parameters, if an explicit
15916 instantiation of a template appears after a declaration of an
15917 explicit specialization for that template, the explicit
15918 instantiation has no effect. */
15921 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
15925 No program shall explicitly instantiate any template more
15928 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
15929 instantiation was `extern'. If EXTERN_P then the second is.
15930 These cases are OK. */
15931 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
15933 if (!previous_instantiation_extern_p && !extern_p
15934 && (complain & tf_error))
15935 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
15937 /* If we've already instantiated the template, just return now. */
15938 if (!CLASSTYPE_INTERFACE_ONLY (t))
15942 check_explicit_instantiation_namespace (TYPE_NAME (t));
15943 mark_class_instantiated (t, extern_p);
15951 /* In contrast to implicit instantiation, where only the
15952 declarations, and not the definitions, of members are
15953 instantiated, we have here:
15957 The explicit instantiation of a class template specialization
15958 implies the instantiation of all of its members not
15959 previously explicitly specialized in the translation unit
15960 containing the explicit instantiation.
15962 Of course, we can't instantiate member template classes, since
15963 we don't have any arguments for them. Note that the standard
15964 is unclear on whether the instantiation of the members are
15965 *explicit* instantiations or not. However, the most natural
15966 interpretation is that it should be an explicit instantiation. */
15969 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
15970 if (TREE_CODE (tmp) == FUNCTION_DECL
15971 && DECL_TEMPLATE_INSTANTIATION (tmp))
15972 instantiate_class_member (tmp, extern_p);
15974 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
15975 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
15976 instantiate_class_member (tmp, extern_p);
15978 if (CLASSTYPE_NESTED_UTDS (t))
15979 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
15980 bt_instantiate_type_proc, &storage);
15984 /* Given a function DECL, which is a specialization of TMPL, modify
15985 DECL to be a re-instantiation of TMPL with the same template
15986 arguments. TMPL should be the template into which tsubst'ing
15987 should occur for DECL, not the most general template.
15989 One reason for doing this is a scenario like this:
15992 void f(const T&, int i);
15994 void g() { f(3, 7); }
15997 void f(const T& t, const int i) { }
15999 Note that when the template is first instantiated, with
16000 instantiate_template, the resulting DECL will have no name for the
16001 first parameter, and the wrong type for the second. So, when we go
16002 to instantiate the DECL, we regenerate it. */
16005 regenerate_decl_from_template (tree decl, tree tmpl)
16007 /* The arguments used to instantiate DECL, from the most general
16012 args = DECL_TI_ARGS (decl);
16013 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
16015 /* Make sure that we can see identifiers, and compute access
16017 push_access_scope (decl);
16019 if (TREE_CODE (decl) == FUNCTION_DECL)
16027 args_depth = TMPL_ARGS_DEPTH (args);
16028 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
16029 if (args_depth > parms_depth)
16030 args = get_innermost_template_args (args, parms_depth);
16032 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
16033 args, tf_error, NULL_TREE);
16035 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
16038 /* Merge parameter declarations. */
16039 decl_parm = skip_artificial_parms_for (decl,
16040 DECL_ARGUMENTS (decl));
16042 = skip_artificial_parms_for (code_pattern,
16043 DECL_ARGUMENTS (code_pattern));
16044 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
16049 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16050 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
16051 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
16053 parm_type = type_decays_to (parm_type);
16054 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16055 TREE_TYPE (decl_parm) = parm_type;
16056 attributes = DECL_ATTRIBUTES (pattern_parm);
16057 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16059 DECL_ATTRIBUTES (decl_parm) = attributes;
16060 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16062 decl_parm = TREE_CHAIN (decl_parm);
16063 pattern_parm = TREE_CHAIN (pattern_parm);
16065 /* Merge any parameters that match with the function parameter
16067 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
16070 tree expanded_types;
16071 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16072 the parameters in this function parameter pack. */
16073 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
16074 args, tf_error, NULL_TREE);
16075 len = TREE_VEC_LENGTH (expanded_types);
16076 for (i = 0; i < len; i++)
16081 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
16082 /* Rename the parameter to include the index. */
16083 DECL_NAME (decl_parm) =
16084 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
16085 parm_type = TREE_VEC_ELT (expanded_types, i);
16086 parm_type = type_decays_to (parm_type);
16087 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
16088 TREE_TYPE (decl_parm) = parm_type;
16089 attributes = DECL_ATTRIBUTES (pattern_parm);
16090 if (DECL_ATTRIBUTES (decl_parm) != attributes)
16092 DECL_ATTRIBUTES (decl_parm) = attributes;
16093 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
16095 decl_parm = TREE_CHAIN (decl_parm);
16098 /* Merge additional specifiers from the CODE_PATTERN. */
16099 if (DECL_DECLARED_INLINE_P (code_pattern)
16100 && !DECL_DECLARED_INLINE_P (decl))
16101 DECL_DECLARED_INLINE_P (decl) = 1;
16103 else if (TREE_CODE (decl) == VAR_DECL)
16104 DECL_INITIAL (decl) =
16105 tsubst_expr (DECL_INITIAL (code_pattern), args,
16106 tf_error, DECL_TI_TEMPLATE (decl),
16107 /*integral_constant_expression_p=*/false);
16109 gcc_unreachable ();
16111 pop_access_scope (decl);
16114 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16115 substituted to get DECL. */
16118 template_for_substitution (tree decl)
16120 tree tmpl = DECL_TI_TEMPLATE (decl);
16122 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16123 for the instantiation. This is not always the most general
16124 template. Consider, for example:
16127 struct S { template <class U> void f();
16128 template <> void f<int>(); };
16130 and an instantiation of S<double>::f<int>. We want TD to be the
16131 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16132 while (/* An instantiation cannot have a definition, so we need a
16133 more general template. */
16134 DECL_TEMPLATE_INSTANTIATION (tmpl)
16135 /* We must also deal with friend templates. Given:
16137 template <class T> struct S {
16138 template <class U> friend void f() {};
16141 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16142 so far as the language is concerned, but that's still
16143 where we get the pattern for the instantiation from. On
16144 other hand, if the definition comes outside the class, say:
16146 template <class T> struct S {
16147 template <class U> friend void f();
16149 template <class U> friend void f() {}
16151 we don't need to look any further. That's what the check for
16152 DECL_INITIAL is for. */
16153 || (TREE_CODE (decl) == FUNCTION_DECL
16154 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
16155 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
16157 /* The present template, TD, should not be a definition. If it
16158 were a definition, we should be using it! Note that we
16159 cannot restructure the loop to just keep going until we find
16160 a template with a definition, since that might go too far if
16161 a specialization was declared, but not defined. */
16162 gcc_assert (TREE_CODE (decl) != VAR_DECL
16163 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
16165 /* Fetch the more general template. */
16166 tmpl = DECL_TI_TEMPLATE (tmpl);
16172 /* Returns true if we need to instantiate this template instance even if we
16173 know we aren't going to emit it.. */
16176 always_instantiate_p (tree decl)
16178 /* We always instantiate inline functions so that we can inline them. An
16179 explicit instantiation declaration prohibits implicit instantiation of
16180 non-inline functions. With high levels of optimization, we would
16181 normally inline non-inline functions -- but we're not allowed to do
16182 that for "extern template" functions. Therefore, we check
16183 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16184 return ((TREE_CODE (decl) == FUNCTION_DECL
16185 && DECL_DECLARED_INLINE_P (decl))
16186 /* And we need to instantiate static data members so that
16187 their initializers are available in integral constant
16189 || (TREE_CODE (decl) == VAR_DECL
16190 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
16193 /* Produce the definition of D, a _DECL generated from a template. If
16194 DEFER_OK is nonzero, then we don't have to actually do the
16195 instantiation now; we just have to do it sometime. Normally it is
16196 an error if this is an explicit instantiation but D is undefined.
16197 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16198 explicitly instantiated class template. */
16201 instantiate_decl (tree d, int defer_ok,
16202 bool expl_inst_class_mem_p)
16204 tree tmpl = DECL_TI_TEMPLATE (d);
16211 bool pattern_defined;
16213 location_t saved_loc = input_location;
16216 /* This function should only be used to instantiate templates for
16217 functions and static member variables. */
16218 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
16219 || TREE_CODE (d) == VAR_DECL);
16221 /* Variables are never deferred; if instantiation is required, they
16222 are instantiated right away. That allows for better code in the
16223 case that an expression refers to the value of the variable --
16224 if the variable has a constant value the referring expression can
16225 take advantage of that fact. */
16226 if (TREE_CODE (d) == VAR_DECL)
16229 /* Don't instantiate cloned functions. Instead, instantiate the
16230 functions they cloned. */
16231 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
16232 d = DECL_CLONED_FUNCTION (d);
16234 if (DECL_TEMPLATE_INSTANTIATED (d)
16235 || DECL_TEMPLATE_SPECIALIZATION (d))
16236 /* D has already been instantiated or explicitly specialized, so
16237 there's nothing for us to do here.
16239 It might seem reasonable to check whether or not D is an explicit
16240 instantiation, and, if so, stop here. But when an explicit
16241 instantiation is deferred until the end of the compilation,
16242 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16243 the instantiation. */
16246 /* Check to see whether we know that this template will be
16247 instantiated in some other file, as with "extern template"
16249 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
16251 /* In general, we do not instantiate such templates. */
16252 if (external_p && !always_instantiate_p (d))
16255 gen_tmpl = most_general_template (tmpl);
16256 gen_args = DECL_TI_ARGS (d);
16258 if (tmpl != gen_tmpl)
16259 /* We should already have the extra args. */
16260 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
16261 == TMPL_ARGS_DEPTH (gen_args));
16262 /* And what's in the hash table should match D. */
16263 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
16264 || spec == NULL_TREE);
16266 /* This needs to happen before any tsubsting. */
16267 if (! push_tinst_level (d))
16270 timevar_push (TV_PARSE);
16272 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16273 for the instantiation. */
16274 td = template_for_substitution (d);
16275 code_pattern = DECL_TEMPLATE_RESULT (td);
16277 /* We should never be trying to instantiate a member of a class
16278 template or partial specialization. */
16279 gcc_assert (d != code_pattern);
16281 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
16282 || DECL_TEMPLATE_SPECIALIZATION (td))
16283 /* In the case of a friend template whose definition is provided
16284 outside the class, we may have too many arguments. Drop the
16285 ones we don't need. The same is true for specializations. */
16286 args = get_innermost_template_args
16287 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
16291 if (TREE_CODE (d) == FUNCTION_DECL)
16292 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
16294 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
16296 /* We may be in the middle of deferred access check. Disable it now. */
16297 push_deferring_access_checks (dk_no_deferred);
16299 /* Unless an explicit instantiation directive has already determined
16300 the linkage of D, remember that a definition is available for
16302 if (pattern_defined
16303 && !DECL_INTERFACE_KNOWN (d)
16304 && !DECL_NOT_REALLY_EXTERN (d))
16305 mark_definable (d);
16307 input_location = DECL_SOURCE_LOCATION (d);
16309 /* If D is a member of an explicitly instantiated class template,
16310 and no definition is available, treat it like an implicit
16312 if (!pattern_defined && expl_inst_class_mem_p
16313 && DECL_EXPLICIT_INSTANTIATION (d))
16315 DECL_NOT_REALLY_EXTERN (d) = 0;
16316 DECL_INTERFACE_KNOWN (d) = 0;
16317 SET_DECL_IMPLICIT_INSTANTIATION (d);
16320 /* Recheck the substitutions to obtain any warning messages
16321 about ignoring cv qualifiers. Don't do this for artificial decls,
16322 as it breaks the context-sensitive substitution for lambda op(). */
16323 if (!defer_ok && !DECL_ARTIFICIAL (d))
16325 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
16326 tree type = TREE_TYPE (gen);
16328 /* Make sure that we can see identifiers, and compute access
16329 correctly. D is already the target FUNCTION_DECL with the
16331 push_access_scope (d);
16333 if (TREE_CODE (gen) == FUNCTION_DECL)
16335 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
16336 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
16338 /* Don't simply tsubst the function type, as that will give
16339 duplicate warnings about poor parameter qualifications.
16340 The function arguments are the same as the decl_arguments
16341 without the top level cv qualifiers. */
16342 type = TREE_TYPE (type);
16344 tsubst (type, gen_args, tf_warning_or_error, d);
16346 pop_access_scope (d);
16349 /* Defer all other templates, unless we have been explicitly
16350 forbidden from doing so. */
16351 if (/* If there is no definition, we cannot instantiate the
16354 /* If it's OK to postpone instantiation, do so. */
16356 /* If this is a static data member that will be defined
16357 elsewhere, we don't want to instantiate the entire data
16358 member, but we do want to instantiate the initializer so that
16359 we can substitute that elsewhere. */
16360 || (external_p && TREE_CODE (d) == VAR_DECL))
16362 /* The definition of the static data member is now required so
16363 we must substitute the initializer. */
16364 if (TREE_CODE (d) == VAR_DECL
16365 && !DECL_INITIAL (d)
16366 && DECL_INITIAL (code_pattern))
16371 ns = decl_namespace_context (d);
16372 push_nested_namespace (ns);
16373 push_nested_class (DECL_CONTEXT (d));
16374 init = tsubst_expr (DECL_INITIAL (code_pattern),
16376 tf_warning_or_error, NULL_TREE,
16377 /*integral_constant_expression_p=*/false);
16378 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
16379 /*asmspec_tree=*/NULL_TREE,
16380 LOOKUP_ONLYCONVERTING);
16381 pop_nested_class ();
16382 pop_nested_namespace (ns);
16385 /* We restore the source position here because it's used by
16386 add_pending_template. */
16387 input_location = saved_loc;
16389 if (at_eof && !pattern_defined
16390 && DECL_EXPLICIT_INSTANTIATION (d)
16391 && DECL_NOT_REALLY_EXTERN (d))
16394 The definition of a non-exported function template, a
16395 non-exported member function template, or a non-exported
16396 member function or static data member of a class template
16397 shall be present in every translation unit in which it is
16398 explicitly instantiated. */
16399 permerror (input_location, "explicit instantiation of %qD "
16400 "but no definition available", d);
16402 /* ??? Historically, we have instantiated inline functions, even
16403 when marked as "extern template". */
16404 if (!(external_p && TREE_CODE (d) == VAR_DECL))
16405 add_pending_template (d);
16408 /* Tell the repository that D is available in this translation unit
16409 -- and see if it is supposed to be instantiated here. */
16410 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
16412 /* In a PCH file, despite the fact that the repository hasn't
16413 requested instantiation in the PCH it is still possible that
16414 an instantiation will be required in a file that includes the
16417 add_pending_template (d);
16418 /* Instantiate inline functions so that the inliner can do its
16419 job, even though we'll not be emitting a copy of this
16421 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
16425 need_push = !cfun || !global_bindings_p ();
16427 push_to_top_level ();
16429 /* Mark D as instantiated so that recursive calls to
16430 instantiate_decl do not try to instantiate it again. */
16431 DECL_TEMPLATE_INSTANTIATED (d) = 1;
16433 /* Regenerate the declaration in case the template has been modified
16434 by a subsequent redeclaration. */
16435 regenerate_decl_from_template (d, td);
16437 /* We already set the file and line above. Reset them now in case
16438 they changed as a result of calling regenerate_decl_from_template. */
16439 input_location = DECL_SOURCE_LOCATION (d);
16441 if (TREE_CODE (d) == VAR_DECL)
16445 /* Clear out DECL_RTL; whatever was there before may not be right
16446 since we've reset the type of the declaration. */
16447 SET_DECL_RTL (d, NULL_RTX);
16448 DECL_IN_AGGR_P (d) = 0;
16450 /* The initializer is placed in DECL_INITIAL by
16451 regenerate_decl_from_template. Pull it out so that
16452 cp_finish_decl can process it. */
16453 init = DECL_INITIAL (d);
16454 DECL_INITIAL (d) = NULL_TREE;
16455 DECL_INITIALIZED_P (d) = 0;
16457 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16458 initializer. That function will defer actual emission until
16459 we have a chance to determine linkage. */
16460 DECL_EXTERNAL (d) = 0;
16462 /* Enter the scope of D so that access-checking works correctly. */
16463 push_nested_class (DECL_CONTEXT (d));
16464 cp_finish_decl (d, init, false, NULL_TREE, 0);
16465 pop_nested_class ();
16467 else if (TREE_CODE (d) == FUNCTION_DECL)
16469 htab_t saved_local_specializations;
16474 /* Save away the current list, in case we are instantiating one
16475 template from within the body of another. */
16476 saved_local_specializations = local_specializations;
16478 /* Set up the list of local specializations. */
16479 local_specializations = htab_create (37,
16480 hash_local_specialization,
16481 eq_local_specializations,
16484 /* Set up context. */
16485 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
16487 /* Create substitution entries for the parameters. */
16488 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16489 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16490 spec_parm = DECL_ARGUMENTS (d);
16491 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
16493 register_local_specialization (spec_parm, tmpl_parm);
16494 spec_parm = skip_artificial_parms_for (d, spec_parm);
16495 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
16497 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16499 register_local_specialization (spec_parm, tmpl_parm);
16500 tmpl_parm = TREE_CHAIN (tmpl_parm);
16501 spec_parm = TREE_CHAIN (spec_parm);
16503 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16505 /* Register the (value) argument pack as a specialization of
16506 TMPL_PARM, then move on. */
16507 tree argpack = make_fnparm_pack (spec_parm);
16508 register_local_specialization (argpack, tmpl_parm);
16509 tmpl_parm = TREE_CHAIN (tmpl_parm);
16510 spec_parm = NULL_TREE;
16512 gcc_assert (!spec_parm);
16514 /* Substitute into the body of the function. */
16515 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
16516 tf_warning_or_error, tmpl,
16517 /*integral_constant_expression_p=*/false);
16519 /* Set the current input_location to the end of the function
16520 so that finish_function knows where we are. */
16521 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
16523 /* We don't need the local specializations any more. */
16524 htab_delete (local_specializations);
16525 local_specializations = saved_local_specializations;
16527 /* Finish the function. */
16528 d = finish_function (0);
16529 expand_or_defer_fn (d);
16532 /* We're not deferring instantiation any more. */
16533 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
16536 pop_from_top_level ();
16539 input_location = saved_loc;
16540 pop_deferring_access_checks ();
16541 pop_tinst_level ();
16543 timevar_pop (TV_PARSE);
16548 /* Run through the list of templates that we wish we could
16549 instantiate, and instantiate any we can. RETRIES is the
16550 number of times we retry pending template instantiation. */
16553 instantiate_pending_templates (int retries)
16556 location_t saved_loc = input_location;
16558 /* Instantiating templates may trigger vtable generation. This in turn
16559 may require further template instantiations. We place a limit here
16560 to avoid infinite loop. */
16561 if (pending_templates && retries >= max_tinst_depth)
16563 tree decl = pending_templates->tinst->decl;
16565 error ("template instantiation depth exceeds maximum of %d"
16566 " instantiating %q+D, possibly from virtual table generation"
16567 " (use -ftemplate-depth-NN to increase the maximum)",
16568 max_tinst_depth, decl);
16569 if (TREE_CODE (decl) == FUNCTION_DECL)
16570 /* Pretend that we defined it. */
16571 DECL_INITIAL (decl) = error_mark_node;
16577 struct pending_template **t = &pending_templates;
16578 struct pending_template *last = NULL;
16582 tree instantiation = reopen_tinst_level ((*t)->tinst);
16583 bool complete = false;
16585 if (TYPE_P (instantiation))
16589 if (!COMPLETE_TYPE_P (instantiation))
16591 instantiate_class_template (instantiation);
16592 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
16593 for (fn = TYPE_METHODS (instantiation);
16595 fn = TREE_CHAIN (fn))
16596 if (! DECL_ARTIFICIAL (fn))
16597 instantiate_decl (fn,
16599 /*expl_inst_class_mem_p=*/false);
16600 if (COMPLETE_TYPE_P (instantiation))
16604 complete = COMPLETE_TYPE_P (instantiation);
16608 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
16609 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
16612 = instantiate_decl (instantiation,
16614 /*expl_inst_class_mem_p=*/false);
16615 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
16619 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
16620 || DECL_TEMPLATE_INSTANTIATED (instantiation));
16624 /* If INSTANTIATION has been instantiated, then we don't
16625 need to consider it again in the future. */
16633 current_tinst_level = NULL;
16635 last_pending_template = last;
16637 while (reconsider);
16639 input_location = saved_loc;
16642 /* Substitute ARGVEC into T, which is a list of initializers for
16643 either base class or a non-static data member. The TREE_PURPOSEs
16644 are DECLs, and the TREE_VALUEs are the initializer values. Used by
16645 instantiate_decl. */
16648 tsubst_initializer_list (tree t, tree argvec)
16650 tree inits = NULL_TREE;
16652 for (; t; t = TREE_CHAIN (t))
16656 tree expanded_bases = NULL_TREE;
16657 tree expanded_arguments = NULL_TREE;
16660 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
16665 /* Expand the base class expansion type into separate base
16667 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
16668 tf_warning_or_error,
16670 if (expanded_bases == error_mark_node)
16673 /* We'll be building separate TREE_LISTs of arguments for
16675 len = TREE_VEC_LENGTH (expanded_bases);
16676 expanded_arguments = make_tree_vec (len);
16677 for (i = 0; i < len; i++)
16678 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
16680 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
16681 expand each argument in the TREE_VALUE of t. */
16682 expr = make_node (EXPR_PACK_EXPANSION);
16683 PACK_EXPANSION_PARAMETER_PACKS (expr) =
16684 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
16686 if (TREE_VALUE (t) == void_type_node)
16687 /* VOID_TYPE_NODE is used to indicate
16688 value-initialization. */
16690 for (i = 0; i < len; i++)
16691 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
16695 /* Substitute parameter packs into each argument in the
16697 in_base_initializer = 1;
16698 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
16700 tree expanded_exprs;
16702 /* Expand the argument. */
16703 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
16705 = tsubst_pack_expansion (expr, argvec,
16706 tf_warning_or_error,
16708 if (expanded_exprs == error_mark_node)
16711 /* Prepend each of the expanded expressions to the
16712 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
16713 for (i = 0; i < len; i++)
16715 TREE_VEC_ELT (expanded_arguments, i) =
16716 tree_cons (NULL_TREE,
16717 TREE_VEC_ELT (expanded_exprs, i),
16718 TREE_VEC_ELT (expanded_arguments, i));
16721 in_base_initializer = 0;
16723 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
16724 since we built them backwards. */
16725 for (i = 0; i < len; i++)
16727 TREE_VEC_ELT (expanded_arguments, i) =
16728 nreverse (TREE_VEC_ELT (expanded_arguments, i));
16733 for (i = 0; i < len; ++i)
16735 if (expanded_bases)
16737 decl = TREE_VEC_ELT (expanded_bases, i);
16738 decl = expand_member_init (decl);
16739 init = TREE_VEC_ELT (expanded_arguments, i);
16743 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
16744 tf_warning_or_error, NULL_TREE);
16746 decl = expand_member_init (decl);
16747 if (decl && !DECL_P (decl))
16748 in_base_initializer = 1;
16750 init = tsubst_expr (TREE_VALUE (t), argvec,
16751 tf_warning_or_error, NULL_TREE,
16752 /*integral_constant_expression_p=*/false);
16753 in_base_initializer = 0;
16758 init = build_tree_list (decl, init);
16759 TREE_CHAIN (init) = inits;
16767 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
16770 set_current_access_from_decl (tree decl)
16772 if (TREE_PRIVATE (decl))
16773 current_access_specifier = access_private_node;
16774 else if (TREE_PROTECTED (decl))
16775 current_access_specifier = access_protected_node;
16777 current_access_specifier = access_public_node;
16780 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
16781 is the instantiation (which should have been created with
16782 start_enum) and ARGS are the template arguments to use. */
16785 tsubst_enum (tree tag, tree newtag, tree args)
16789 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
16794 decl = TREE_VALUE (e);
16795 /* Note that in a template enum, the TREE_VALUE is the
16796 CONST_DECL, not the corresponding INTEGER_CST. */
16797 value = tsubst_expr (DECL_INITIAL (decl),
16798 args, tf_warning_or_error, NULL_TREE,
16799 /*integral_constant_expression_p=*/true);
16801 /* Give this enumeration constant the correct access. */
16802 set_current_access_from_decl (decl);
16804 /* Actually build the enumerator itself. */
16805 build_enumerator (DECL_NAME (decl), value, newtag);
16808 finish_enum (newtag);
16809 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
16810 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
16813 /* DECL is a FUNCTION_DECL that is a template specialization. Return
16814 its type -- but without substituting the innermost set of template
16815 arguments. So, innermost set of template parameters will appear in
16819 get_mostly_instantiated_function_type (tree decl)
16827 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16828 targs = DECL_TI_ARGS (decl);
16829 tparms = DECL_TEMPLATE_PARMS (tmpl);
16830 parm_depth = TMPL_PARMS_DEPTH (tparms);
16832 /* There should be as many levels of arguments as there are levels
16834 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
16836 fn_type = TREE_TYPE (tmpl);
16838 if (parm_depth == 1)
16839 /* No substitution is necessary. */
16843 int i, save_access_control;
16846 /* Replace the innermost level of the TARGS with NULL_TREEs to
16847 let tsubst know not to substitute for those parameters. */
16848 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
16849 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
16850 SET_TMPL_ARGS_LEVEL (partial_args, i,
16851 TMPL_ARGS_LEVEL (targs, i));
16852 SET_TMPL_ARGS_LEVEL (partial_args,
16853 TMPL_ARGS_DEPTH (targs),
16854 make_tree_vec (DECL_NTPARMS (tmpl)));
16856 /* Disable access control as this function is used only during
16858 save_access_control = flag_access_control;
16859 flag_access_control = 0;
16861 ++processing_template_decl;
16862 /* Now, do the (partial) substitution to figure out the
16863 appropriate function type. */
16864 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
16865 --processing_template_decl;
16867 /* Substitute into the template parameters to obtain the real
16868 innermost set of parameters. This step is important if the
16869 innermost set of template parameters contains value
16870 parameters whose types depend on outer template parameters. */
16871 TREE_VEC_LENGTH (partial_args)--;
16872 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
16874 flag_access_control = save_access_control;
16880 /* Return truthvalue if we're processing a template different from
16881 the last one involved in diagnostics. */
16883 problematic_instantiation_changed (void)
16885 return last_template_error_tick != tinst_level_tick;
16888 /* Remember current template involved in diagnostics. */
16890 record_last_problematic_instantiation (void)
16892 last_template_error_tick = tinst_level_tick;
16895 struct tinst_level *
16896 current_instantiation (void)
16898 return current_tinst_level;
16901 /* [temp.param] Check that template non-type parm TYPE is of an allowable
16902 type. Return zero for ok, nonzero for disallowed. Issue error and
16903 warning messages under control of COMPLAIN. */
16906 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
16908 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
16910 else if (POINTER_TYPE_P (type))
16912 else if (TYPE_PTR_TO_MEMBER_P (type))
16914 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16916 else if (TREE_CODE (type) == TYPENAME_TYPE)
16919 if (complain & tf_error)
16920 error ("%q#T is not a valid type for a template constant parameter", type);
16924 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
16925 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
16928 dependent_type_p_r (tree type)
16934 A type is dependent if it is:
16936 -- a template parameter. Template template parameters are types
16937 for us (since TYPE_P holds true for them) so we handle
16939 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
16940 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
16942 /* -- a qualified-id with a nested-name-specifier which contains a
16943 class-name that names a dependent type or whose unqualified-id
16944 names a dependent type. */
16945 if (TREE_CODE (type) == TYPENAME_TYPE)
16947 /* -- a cv-qualified type where the cv-unqualified type is
16949 type = TYPE_MAIN_VARIANT (type);
16950 /* -- a compound type constructed from any dependent type. */
16951 if (TYPE_PTR_TO_MEMBER_P (type))
16952 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
16953 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
16955 else if (TREE_CODE (type) == POINTER_TYPE
16956 || TREE_CODE (type) == REFERENCE_TYPE)
16957 return dependent_type_p (TREE_TYPE (type));
16958 else if (TREE_CODE (type) == FUNCTION_TYPE
16959 || TREE_CODE (type) == METHOD_TYPE)
16963 if (dependent_type_p (TREE_TYPE (type)))
16965 for (arg_type = TYPE_ARG_TYPES (type);
16967 arg_type = TREE_CHAIN (arg_type))
16968 if (dependent_type_p (TREE_VALUE (arg_type)))
16972 /* -- an array type constructed from any dependent type or whose
16973 size is specified by a constant expression that is
16974 value-dependent. */
16975 if (TREE_CODE (type) == ARRAY_TYPE)
16977 if (TYPE_DOMAIN (type)
16978 && dependent_type_p (TYPE_DOMAIN (type)))
16980 return dependent_type_p (TREE_TYPE (type));
16982 else if (TREE_CODE (type) == INTEGER_TYPE
16983 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
16985 /* If this is the TYPE_DOMAIN of an array type, consider it
16986 dependent. We already checked for value-dependence in
16987 compute_array_index_type. */
16988 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
16991 /* -- a template-id in which either the template name is a template
16993 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
16995 /* ... or any of the template arguments is a dependent type or
16996 an expression that is type-dependent or value-dependent. */
16997 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
16998 && (any_dependent_template_arguments_p
16999 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
17002 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17003 argument of the `typeof' expression is not type-dependent, then
17004 it should already been have resolved. */
17005 if (TREE_CODE (type) == TYPEOF_TYPE
17006 || TREE_CODE (type) == DECLTYPE_TYPE)
17009 /* A template argument pack is dependent if any of its packed
17011 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
17013 tree args = ARGUMENT_PACK_ARGS (type);
17014 int i, len = TREE_VEC_LENGTH (args);
17015 for (i = 0; i < len; ++i)
17016 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17020 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17021 be template parameters. */
17022 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
17025 /* The standard does not specifically mention types that are local
17026 to template functions or local classes, but they should be
17027 considered dependent too. For example:
17029 template <int I> void f() {
17034 The size of `E' cannot be known until the value of `I' has been
17035 determined. Therefore, `E' must be considered dependent. */
17036 scope = TYPE_CONTEXT (type);
17037 if (scope && TYPE_P (scope))
17038 return dependent_type_p (scope);
17039 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
17040 return type_dependent_expression_p (scope);
17042 /* Other types are non-dependent. */
17046 /* Returns TRUE if TYPE is dependent, in the sense of
17047 [temp.dep.type]. */
17050 dependent_type_p (tree type)
17052 /* If there are no template parameters in scope, then there can't be
17053 any dependent types. */
17054 if (!processing_template_decl)
17056 /* If we are not processing a template, then nobody should be
17057 providing us with a dependent type. */
17059 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
17063 /* If the type is NULL, we have not computed a type for the entity
17064 in question; in that case, the type is dependent. */
17068 /* Erroneous types can be considered non-dependent. */
17069 if (type == error_mark_node)
17072 /* If we have not already computed the appropriate value for TYPE,
17074 if (!TYPE_DEPENDENT_P_VALID (type))
17076 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
17077 TYPE_DEPENDENT_P_VALID (type) = 1;
17080 return TYPE_DEPENDENT_P (type);
17083 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17084 lookup. In other words, a dependent type that is not the current
17088 dependent_scope_p (tree scope)
17090 return (scope && TYPE_P (scope) && dependent_type_p (scope)
17091 && !currently_open_class (scope));
17094 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
17097 dependent_scope_ref_p (tree expression, bool criterion (tree))
17102 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
17104 if (!TYPE_P (TREE_OPERAND (expression, 0)))
17107 scope = TREE_OPERAND (expression, 0);
17108 name = TREE_OPERAND (expression, 1);
17112 An id-expression is type-dependent if it contains a
17113 nested-name-specifier that contains a class-name that names a
17115 /* The suggested resolution to Core Issue 224 implies that if the
17116 qualifying type is the current class, then we must peek
17119 && currently_open_class (scope)
17120 && !criterion (name))
17122 if (dependent_type_p (scope))
17128 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17129 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17133 value_dependent_expression_p (tree expression)
17135 if (!processing_template_decl)
17138 /* A name declared with a dependent type. */
17139 if (DECL_P (expression) && type_dependent_expression_p (expression))
17142 switch (TREE_CODE (expression))
17144 case IDENTIFIER_NODE:
17145 /* A name that has not been looked up -- must be dependent. */
17148 case TEMPLATE_PARM_INDEX:
17149 /* A non-type template parm. */
17153 /* A non-type template parm. */
17154 if (DECL_TEMPLATE_PARM_P (expression))
17156 return value_dependent_expression_p (DECL_INITIAL (expression));
17159 /* A constant with integral or enumeration type and is initialized
17160 with an expression that is value-dependent. */
17161 if (DECL_INITIAL (expression)
17162 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
17163 && value_dependent_expression_p (DECL_INITIAL (expression)))
17167 case DYNAMIC_CAST_EXPR:
17168 case STATIC_CAST_EXPR:
17169 case CONST_CAST_EXPR:
17170 case REINTERPRET_CAST_EXPR:
17172 /* These expressions are value-dependent if the type to which
17173 the cast occurs is dependent or the expression being casted
17174 is value-dependent. */
17176 tree type = TREE_TYPE (expression);
17178 if (dependent_type_p (type))
17181 /* A functional cast has a list of operands. */
17182 expression = TREE_OPERAND (expression, 0);
17185 /* If there are no operands, it must be an expression such
17186 as "int()". This should not happen for aggregate types
17187 because it would form non-constant expressions. */
17188 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
17193 if (TREE_CODE (expression) == TREE_LIST)
17194 return any_value_dependent_elements_p (expression);
17196 return value_dependent_expression_p (expression);
17201 /* A `sizeof' expression is value-dependent if the operand is
17202 type-dependent or is a pack expansion. */
17203 expression = TREE_OPERAND (expression, 0);
17204 if (PACK_EXPANSION_P (expression))
17206 else if (TYPE_P (expression))
17207 return dependent_type_p (expression);
17208 return type_dependent_expression_p (expression);
17211 return dependent_scope_ref_p (expression, value_dependent_expression_p);
17213 case COMPONENT_REF:
17214 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
17215 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
17218 /* A CALL_EXPR may appear in a constant expression if it is a
17219 call to a builtin function, e.g., __builtin_constant_p. All
17220 such calls are value-dependent. */
17223 case NONTYPE_ARGUMENT_PACK:
17224 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17225 is value-dependent. */
17227 tree values = ARGUMENT_PACK_ARGS (expression);
17228 int i, len = TREE_VEC_LENGTH (values);
17230 for (i = 0; i < len; ++i)
17231 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
17239 tree type2 = TRAIT_EXPR_TYPE2 (expression);
17240 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
17241 || (type2 ? dependent_type_p (type2) : false));
17245 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
17246 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
17249 /* A constant expression is value-dependent if any subexpression is
17250 value-dependent. */
17251 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
17253 case tcc_reference:
17255 return (value_dependent_expression_p
17256 (TREE_OPERAND (expression, 0)));
17258 case tcc_comparison:
17260 return ((value_dependent_expression_p
17261 (TREE_OPERAND (expression, 0)))
17262 || (value_dependent_expression_p
17263 (TREE_OPERAND (expression, 1))));
17265 case tcc_expression:
17269 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
17270 /* In some cases, some of the operands may be missing.
17271 (For example, in the case of PREDECREMENT_EXPR, the
17272 amount to increment by may be missing.) That doesn't
17273 make the expression dependent. */
17274 if (TREE_OPERAND (expression, i)
17275 && (value_dependent_expression_p
17276 (TREE_OPERAND (expression, i))))
17286 /* The expression is not value-dependent. */
17290 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17291 [temp.dep.expr]. */
17294 type_dependent_expression_p (tree expression)
17296 if (!processing_template_decl)
17299 if (expression == error_mark_node)
17302 /* An unresolved name is always dependent. */
17303 if (TREE_CODE (expression) == IDENTIFIER_NODE
17304 || TREE_CODE (expression) == USING_DECL)
17307 /* Some expression forms are never type-dependent. */
17308 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
17309 || TREE_CODE (expression) == SIZEOF_EXPR
17310 || TREE_CODE (expression) == ALIGNOF_EXPR
17311 || TREE_CODE (expression) == TRAIT_EXPR
17312 || TREE_CODE (expression) == TYPEID_EXPR
17313 || TREE_CODE (expression) == DELETE_EXPR
17314 || TREE_CODE (expression) == VEC_DELETE_EXPR
17315 || TREE_CODE (expression) == THROW_EXPR)
17318 /* The types of these expressions depends only on the type to which
17319 the cast occurs. */
17320 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
17321 || TREE_CODE (expression) == STATIC_CAST_EXPR
17322 || TREE_CODE (expression) == CONST_CAST_EXPR
17323 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
17324 || TREE_CODE (expression) == CAST_EXPR)
17325 return dependent_type_p (TREE_TYPE (expression));
17327 /* The types of these expressions depends only on the type created
17328 by the expression. */
17329 if (TREE_CODE (expression) == NEW_EXPR
17330 || TREE_CODE (expression) == VEC_NEW_EXPR)
17332 /* For NEW_EXPR tree nodes created inside a template, either
17333 the object type itself or a TREE_LIST may appear as the
17335 tree type = TREE_OPERAND (expression, 1);
17336 if (TREE_CODE (type) == TREE_LIST)
17337 /* This is an array type. We need to check array dimensions
17339 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
17340 || value_dependent_expression_p
17341 (TREE_OPERAND (TREE_VALUE (type), 1));
17343 return dependent_type_p (type);
17346 if (TREE_CODE (expression) == SCOPE_REF
17347 && dependent_scope_ref_p (expression,
17348 type_dependent_expression_p))
17351 if (TREE_CODE (expression) == FUNCTION_DECL
17352 && DECL_LANG_SPECIFIC (expression)
17353 && DECL_TEMPLATE_INFO (expression)
17354 && (any_dependent_template_arguments_p
17355 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
17358 if (TREE_CODE (expression) == TEMPLATE_DECL
17359 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
17362 if (TREE_CODE (expression) == STMT_EXPR)
17363 expression = stmt_expr_value_expr (expression);
17365 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
17370 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
17372 if (type_dependent_expression_p (elt))
17378 if (TREE_TYPE (expression) == unknown_type_node)
17380 if (TREE_CODE (expression) == ADDR_EXPR)
17381 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
17382 if (TREE_CODE (expression) == COMPONENT_REF
17383 || TREE_CODE (expression) == OFFSET_REF)
17385 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
17387 expression = TREE_OPERAND (expression, 1);
17388 if (TREE_CODE (expression) == IDENTIFIER_NODE)
17391 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17392 if (TREE_CODE (expression) == SCOPE_REF)
17395 if (TREE_CODE (expression) == BASELINK)
17396 expression = BASELINK_FUNCTIONS (expression);
17398 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
17400 if (any_dependent_template_arguments_p
17401 (TREE_OPERAND (expression, 1)))
17403 expression = TREE_OPERAND (expression, 0);
17405 gcc_assert (TREE_CODE (expression) == OVERLOAD
17406 || TREE_CODE (expression) == FUNCTION_DECL);
17410 if (type_dependent_expression_p (OVL_CURRENT (expression)))
17412 expression = OVL_NEXT (expression);
17417 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
17419 return (dependent_type_p (TREE_TYPE (expression)));
17422 /* Like type_dependent_expression_p, but it also works while not processing
17423 a template definition, i.e. during substitution or mangling. */
17426 type_dependent_expression_p_push (tree expr)
17429 ++processing_template_decl;
17430 b = type_dependent_expression_p (expr);
17431 --processing_template_decl;
17435 /* Returns TRUE if ARGS contains a type-dependent expression. */
17438 any_type_dependent_arguments_p (const VEC(tree,gc) *args)
17443 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
17445 if (type_dependent_expression_p (arg))
17451 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17452 expressions) contains any value-dependent expressions. */
17455 any_value_dependent_elements_p (const_tree list)
17457 for (; list; list = TREE_CHAIN (list))
17458 if (value_dependent_expression_p (TREE_VALUE (list)))
17464 /* Returns TRUE if the ARG (a template argument) is dependent. */
17467 dependent_template_arg_p (tree arg)
17469 if (!processing_template_decl)
17472 if (TREE_CODE (arg) == TEMPLATE_DECL
17473 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17474 return dependent_template_p (arg);
17475 else if (ARGUMENT_PACK_P (arg))
17477 tree args = ARGUMENT_PACK_ARGS (arg);
17478 int i, len = TREE_VEC_LENGTH (args);
17479 for (i = 0; i < len; ++i)
17481 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17487 else if (TYPE_P (arg))
17488 return dependent_type_p (arg);
17490 return (type_dependent_expression_p (arg)
17491 || value_dependent_expression_p (arg));
17494 /* Returns true if ARGS (a collection of template arguments) contains
17495 any types that require structural equality testing. */
17498 any_template_arguments_need_structural_equality_p (tree args)
17505 if (args == error_mark_node)
17508 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17510 tree level = TMPL_ARGS_LEVEL (args, i + 1);
17511 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17513 tree arg = TREE_VEC_ELT (level, j);
17514 tree packed_args = NULL_TREE;
17517 if (ARGUMENT_PACK_P (arg))
17519 /* Look inside the argument pack. */
17520 packed_args = ARGUMENT_PACK_ARGS (arg);
17521 len = TREE_VEC_LENGTH (packed_args);
17524 for (k = 0; k < len; ++k)
17527 arg = TREE_VEC_ELT (packed_args, k);
17529 if (error_operand_p (arg))
17531 else if (TREE_CODE (arg) == TEMPLATE_DECL
17532 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17534 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
17536 else if (!TYPE_P (arg) && TREE_TYPE (arg)
17537 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
17546 /* Returns true if ARGS (a collection of template arguments) contains
17547 any dependent arguments. */
17550 any_dependent_template_arguments_p (const_tree args)
17557 if (args == error_mark_node)
17560 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17562 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
17563 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17564 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
17571 /* Returns TRUE if the template TMPL is dependent. */
17574 dependent_template_p (tree tmpl)
17576 if (TREE_CODE (tmpl) == OVERLOAD)
17580 if (dependent_template_p (OVL_FUNCTION (tmpl)))
17582 tmpl = OVL_CHAIN (tmpl);
17587 /* Template template parameters are dependent. */
17588 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
17589 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
17591 /* So are names that have not been looked up. */
17592 if (TREE_CODE (tmpl) == SCOPE_REF
17593 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
17595 /* So are member templates of dependent classes. */
17596 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
17597 return dependent_type_p (DECL_CONTEXT (tmpl));
17601 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17604 dependent_template_id_p (tree tmpl, tree args)
17606 return (dependent_template_p (tmpl)
17607 || any_dependent_template_arguments_p (args));
17610 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17614 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
17618 if (!processing_template_decl)
17621 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
17623 tree decl = TREE_VEC_ELT (declv, i);
17624 tree init = TREE_VEC_ELT (initv, i);
17625 tree cond = TREE_VEC_ELT (condv, i);
17626 tree incr = TREE_VEC_ELT (incrv, i);
17628 if (type_dependent_expression_p (decl))
17631 if (init && type_dependent_expression_p (init))
17634 if (type_dependent_expression_p (cond))
17637 if (COMPARISON_CLASS_P (cond)
17638 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
17639 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
17642 if (TREE_CODE (incr) == MODOP_EXPR)
17644 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
17645 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
17648 else if (type_dependent_expression_p (incr))
17650 else if (TREE_CODE (incr) == MODIFY_EXPR)
17652 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
17654 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
17656 tree t = TREE_OPERAND (incr, 1);
17657 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
17658 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
17667 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
17668 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
17669 no such TYPE can be found. Note that this function peers inside
17670 uninstantiated templates and therefore should be used only in
17671 extremely limited situations. ONLY_CURRENT_P restricts this
17672 peering to the currently open classes hierarchy (which is required
17673 when comparing types). */
17676 resolve_typename_type (tree type, bool only_current_p)
17685 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
17687 scope = TYPE_CONTEXT (type);
17688 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
17689 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
17690 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
17691 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
17692 identifier of the TYPENAME_TYPE anymore.
17693 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
17694 TYPENAME_TYPE instead, we avoid messing up with a possible
17695 typedef variant case. */
17696 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
17698 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
17699 it first before we can figure out what NAME refers to. */
17700 if (TREE_CODE (scope) == TYPENAME_TYPE)
17701 scope = resolve_typename_type (scope, only_current_p);
17702 /* If we don't know what SCOPE refers to, then we cannot resolve the
17704 if (TREE_CODE (scope) == TYPENAME_TYPE)
17706 /* If the SCOPE is a template type parameter, we have no way of
17707 resolving the name. */
17708 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
17710 /* If the SCOPE is not the current instantiation, there's no reason
17711 to look inside it. */
17712 if (only_current_p && !currently_open_class (scope))
17714 /* If this is a typedef, we don't want to look inside (c++/11987). */
17715 if (typedef_variant_p (type))
17717 /* If SCOPE isn't the template itself, it will not have a valid
17718 TYPE_FIELDS list. */
17719 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
17720 /* scope is either the template itself or a compatible instantiation
17721 like X<T>, so look up the name in the original template. */
17722 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
17724 /* scope is a partial instantiation, so we can't do the lookup or we
17725 will lose the template arguments. */
17727 /* Enter the SCOPE so that name lookup will be resolved as if we
17728 were in the class definition. In particular, SCOPE will no
17729 longer be considered a dependent type. */
17730 pushed_scope = push_scope (scope);
17731 /* Look up the declaration. */
17732 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
17734 result = NULL_TREE;
17736 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
17737 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
17740 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
17741 && TREE_CODE (decl) == TYPE_DECL)
17743 result = TREE_TYPE (decl);
17744 if (result == error_mark_node)
17745 result = NULL_TREE;
17747 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
17748 && DECL_CLASS_TEMPLATE_P (decl))
17752 /* Obtain the template and the arguments. */
17753 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
17754 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
17755 /* Instantiate the template. */
17756 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
17757 /*entering_scope=*/0,
17758 tf_error | tf_user);
17759 if (result == error_mark_node)
17760 result = NULL_TREE;
17763 /* Leave the SCOPE. */
17765 pop_scope (pushed_scope);
17767 /* If we failed to resolve it, return the original typename. */
17771 /* If lookup found a typename type, resolve that too. */
17772 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
17774 /* Ill-formed programs can cause infinite recursion here, so we
17775 must catch that. */
17776 TYPENAME_IS_RESOLVING_P (type) = 1;
17777 result = resolve_typename_type (result, only_current_p);
17778 TYPENAME_IS_RESOLVING_P (type) = 0;
17781 /* Qualify the resulting type. */
17782 quals = cp_type_quals (type);
17784 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
17789 /* EXPR is an expression which is not type-dependent. Return a proxy
17790 for EXPR that can be used to compute the types of larger
17791 expressions containing EXPR. */
17794 build_non_dependent_expr (tree expr)
17798 /* Preserve null pointer constants so that the type of things like
17799 "p == 0" where "p" is a pointer can be determined. */
17800 if (null_ptr_cst_p (expr))
17802 /* Preserve OVERLOADs; the functions must be available to resolve
17805 if (TREE_CODE (inner_expr) == STMT_EXPR)
17806 inner_expr = stmt_expr_value_expr (inner_expr);
17807 if (TREE_CODE (inner_expr) == ADDR_EXPR)
17808 inner_expr = TREE_OPERAND (inner_expr, 0);
17809 if (TREE_CODE (inner_expr) == COMPONENT_REF)
17810 inner_expr = TREE_OPERAND (inner_expr, 1);
17811 if (is_overloaded_fn (inner_expr)
17812 || TREE_CODE (inner_expr) == OFFSET_REF)
17814 /* There is no need to return a proxy for a variable. */
17815 if (TREE_CODE (expr) == VAR_DECL)
17817 /* Preserve string constants; conversions from string constants to
17818 "char *" are allowed, even though normally a "const char *"
17819 cannot be used to initialize a "char *". */
17820 if (TREE_CODE (expr) == STRING_CST)
17822 /* Preserve arithmetic constants, as an optimization -- there is no
17823 reason to create a new node. */
17824 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
17826 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
17827 There is at least one place where we want to know that a
17828 particular expression is a throw-expression: when checking a ?:
17829 expression, there are special rules if the second or third
17830 argument is a throw-expression. */
17831 if (TREE_CODE (expr) == THROW_EXPR)
17834 if (TREE_CODE (expr) == COND_EXPR)
17835 return build3 (COND_EXPR,
17837 TREE_OPERAND (expr, 0),
17838 (TREE_OPERAND (expr, 1)
17839 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
17840 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
17841 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
17842 if (TREE_CODE (expr) == COMPOUND_EXPR
17843 && !COMPOUND_EXPR_OVERLOADED (expr))
17844 return build2 (COMPOUND_EXPR,
17846 TREE_OPERAND (expr, 0),
17847 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
17849 /* If the type is unknown, it can't really be non-dependent */
17850 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
17852 /* Otherwise, build a NON_DEPENDENT_EXPR.
17854 REFERENCE_TYPEs are not stripped for expressions in templates
17855 because doing so would play havoc with mangling. Consider, for
17858 template <typename T> void f<T& g>() { g(); }
17860 In the body of "f", the expression for "g" will have
17861 REFERENCE_TYPE, even though the standard says that it should
17862 not. The reason is that we must preserve the syntactic form of
17863 the expression so that mangling (say) "f<g>" inside the body of
17864 "f" works out correctly. Therefore, the REFERENCE_TYPE is
17866 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
17869 /* ARGS is a vector of expressions as arguments to a function call.
17870 Replace the arguments with equivalent non-dependent expressions.
17871 This modifies ARGS in place. */
17874 make_args_non_dependent (VEC(tree,gc) *args)
17879 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
17881 tree newarg = build_non_dependent_expr (arg);
17883 VEC_replace (tree, args, ix, newarg);
17887 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
17888 with a level one deeper than the actual template parms. */
17893 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
17894 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
17895 TYPE_DECL, get_identifier ("auto"), au);
17896 TYPE_STUB_DECL (au) = TYPE_NAME (au);
17897 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
17898 (0, processing_template_decl + 1, processing_template_decl + 1,
17899 TYPE_NAME (au), NULL_TREE);
17900 TYPE_CANONICAL (au) = canonical_type_parameter (au);
17901 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
17902 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
17907 /* Given type ARG, return std::initializer_list<ARG>. */
17912 tree std_init_list = namespace_binding
17913 (get_identifier ("initializer_list"), std_node);
17915 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
17917 error ("deducing from brace-enclosed initializer list requires "
17918 "#include <initializer_list>");
17919 return error_mark_node;
17921 argvec = make_tree_vec (1);
17922 TREE_VEC_ELT (argvec, 0) = arg;
17923 return lookup_template_class (std_init_list, argvec, NULL_TREE,
17924 NULL_TREE, 0, tf_warning_or_error);
17927 /* Replace auto in TYPE with std::initializer_list<auto>. */
17930 listify_autos (tree type, tree auto_node)
17932 tree init_auto = listify (auto_node);
17933 tree argvec = make_tree_vec (1);
17934 TREE_VEC_ELT (argvec, 0) = init_auto;
17935 if (processing_template_decl)
17936 argvec = add_to_template_args (current_template_args (), argvec);
17937 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17940 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
17941 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
17944 do_auto_deduction (tree type, tree init, tree auto_node)
17946 tree parms, tparms, targs;
17950 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
17951 with either a new invented type template parameter U or, if the
17952 initializer is a braced-init-list (8.5.4), with
17953 std::initializer_list<U>. */
17954 if (BRACE_ENCLOSED_INITIALIZER_P (init))
17955 type = listify_autos (type, auto_node);
17957 parms = build_tree_list (NULL_TREE, type);
17959 tparms = make_tree_vec (1);
17960 targs = make_tree_vec (1);
17961 TREE_VEC_ELT (tparms, 0)
17962 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
17963 val = type_unification_real (tparms, targs, parms, args, 1, 0,
17964 DEDUCE_CALL, LOOKUP_NORMAL);
17967 error ("unable to deduce %qT from %qE", type, init);
17968 return error_mark_node;
17971 /* If the list of declarators contains more than one declarator, the type
17972 of each declared variable is determined as described above. If the
17973 type deduced for the template parameter U is not the same in each
17974 deduction, the program is ill-formed. */
17975 if (TREE_TYPE (auto_node)
17976 && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
17978 error ("inconsistent deduction for %qT: %qT and then %qT",
17979 auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
17980 return error_mark_node;
17982 TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
17984 if (processing_template_decl)
17985 targs = add_to_template_args (current_template_args (), targs);
17986 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
17989 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
17993 splice_late_return_type (tree type, tree late_return_type)
17997 if (late_return_type == NULL_TREE)
17999 argvec = make_tree_vec (1);
18000 TREE_VEC_ELT (argvec, 0) = late_return_type;
18001 if (processing_template_decl)
18002 argvec = add_to_template_args (current_template_args (), argvec);
18003 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
18006 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18009 is_auto (const_tree type)
18011 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18012 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
18018 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18019 appear as a type-specifier for the declaration in question, we don't
18020 have to look through the whole type. */
18023 type_uses_auto (tree type)
18025 enum tree_code code;
18026 if (is_auto (type))
18029 code = TREE_CODE (type);
18031 if (code == POINTER_TYPE || code == REFERENCE_TYPE
18032 || code == OFFSET_TYPE || code == FUNCTION_TYPE
18033 || code == METHOD_TYPE || code == ARRAY_TYPE)
18034 return type_uses_auto (TREE_TYPE (type));
18036 if (TYPE_PTRMEMFUNC_P (type))
18037 return type_uses_auto (TREE_TYPE (TREE_TYPE
18038 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
18043 /* For a given template T, return the list of typedefs referenced
18044 in T for which access check is needed at T instantiation time.
18045 T is either a FUNCTION_DECL or a RECORD_TYPE.
18046 Those typedefs were added to T by the function
18047 append_type_to_template_for_access_check. */
18050 get_types_needing_access_check (tree t)
18052 tree ti, result = NULL_TREE;
18054 if (!t || t == error_mark_node)
18057 if (!(ti = get_template_info (t)))
18060 if (CLASS_TYPE_P (t)
18061 || TREE_CODE (t) == FUNCTION_DECL)
18063 if (!TI_TEMPLATE (ti))
18066 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
18072 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18073 tied to T. That list of typedefs will be access checked at
18074 T instantiation time.
18075 T is either a FUNCTION_DECL or a RECORD_TYPE.
18076 TYPE_DECL is a TYPE_DECL node representing a typedef.
18077 SCOPE is the scope through which TYPE_DECL is accessed.
18079 This function is a subroutine of
18080 append_type_to_template_for_access_check. */
18083 append_type_to_template_for_access_check_1 (tree t,
18089 if (!t || t == error_mark_node)
18092 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
18093 || CLASS_TYPE_P (t))
18095 && TREE_CODE (type_decl) == TYPE_DECL
18098 if (!(ti = get_template_info (t)))
18101 gcc_assert (TI_TEMPLATE (ti));
18103 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti) =
18104 tree_cons (type_decl, scope, TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti));
18107 /* Append TYPE_DECL to the template TEMPL.
18108 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18109 At TEMPL instanciation time, TYPE_DECL will be checked to see
18110 if it can be accessed through SCOPE.
18112 e.g. consider the following code snippet:
18119 template<class U> struct S
18126 At S<char> instantiation time, we need to check the access of C::myint
18127 In other words, we need to check the access of the myint typedef through
18128 the C scope. For that purpose, this function will add the myint typedef
18129 and the scope C through which its being accessed to a list of typedefs
18130 tied to the template S. That list will be walked at template instantiation
18131 time and access check performed on each typedefs it contains.
18132 Note that this particular code snippet should yield an error because
18133 myint is private to C. */
18136 append_type_to_template_for_access_check (tree templ,
18142 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
18144 /* Make sure we don't append the type to the template twice. */
18145 for (node = get_types_needing_access_check (templ);
18147 node = TREE_CHAIN (node))
18149 tree decl = TREE_PURPOSE (node);
18150 tree type_scope = TREE_VALUE (node);
18152 if (decl == type_decl && type_scope == scope)
18156 append_type_to_template_for_access_check_1 (templ, type_decl, scope);
18159 /* Set up the hash tables for template instantiations. */
18162 init_template_processing (void)
18164 decl_specializations = htab_create_ggc (37,
18165 hash_specialization,
18166 eq_specializations,
18168 type_specializations = htab_create_ggc (37,
18169 hash_specialization,
18170 eq_specializations,
18174 #include "gt-cp-pt.h"