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 Free Software Foundation, Inc.
4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 Rewritten by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
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 The TREE_PURPOSE of each entry is either a DECL (for a function or
58 static data member), or a TYPE (for a class) indicating what we are
59 hoping to instantiate. The TREE_VALUE is not used. */
60 static GTY(()) tree pending_templates;
61 static GTY(()) tree last_pending_template;
63 int processing_template_parmlist;
64 static int template_header_count;
66 static GTY(()) tree saved_trees;
67 static VEC(int,heap) *inline_parm_levels;
69 static GTY(()) tree current_tinst_level;
71 static GTY(()) tree saved_access_scope;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
81 static htab_t local_specializations;
83 #define UNIFY_ALLOW_NONE 0
84 #define UNIFY_ALLOW_MORE_CV_QUAL 1
85 #define UNIFY_ALLOW_LESS_CV_QUAL 2
86 #define UNIFY_ALLOW_DERIVED 4
87 #define UNIFY_ALLOW_INTEGER 8
88 #define UNIFY_ALLOW_OUTER_LEVEL 16
89 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
90 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
92 static void push_access_scope (tree);
93 static void pop_access_scope (tree);
94 static bool resolve_overloaded_unification (tree, tree, tree, tree,
95 unification_kind_t, int);
96 static int try_one_overload (tree, tree, tree, tree, tree,
97 unification_kind_t, int, bool);
98 static int unify (tree, tree, tree, tree, int);
99 static void add_pending_template (tree);
100 static int push_tinst_level (tree);
101 static void pop_tinst_level (void);
102 static void reopen_tinst_level (tree);
103 static tree classtype_mangled_name (tree);
104 static char* mangle_class_name_for_template (const char *, tree, tree);
105 static tree tsubst_initializer_list (tree, tree);
106 static tree get_class_bindings (tree, tree, tree);
107 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
109 static void tsubst_enum (tree, tree, tree);
110 static tree add_to_template_args (tree, tree);
111 static tree add_outermost_template_args (tree, tree);
112 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
113 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
114 static int type_unification_real (tree, tree, tree, tree,
115 int, unification_kind_t, int);
116 static void note_template_header (int);
117 static tree convert_nontype_argument_function (tree, tree);
118 static tree convert_nontype_argument (tree, tree);
119 static tree convert_template_argument (tree, tree, tree,
120 tsubst_flags_t, int, tree);
121 static int for_each_template_parm (tree, tree_fn_t, void*,
122 struct pointer_set_t*);
123 static tree build_template_parm_index (int, int, int, tree, tree);
124 static int inline_needs_template_parms (tree);
125 static void push_inline_template_parms_recursive (tree, int);
126 static tree retrieve_local_specialization (tree);
127 static void register_local_specialization (tree, tree);
128 static tree reduce_template_parm_level (tree, tree, int);
129 static int mark_template_parm (tree, void *);
130 static int template_parm_this_level_p (tree, void *);
131 static tree tsubst_friend_function (tree, tree);
132 static tree tsubst_friend_class (tree, tree);
133 static int can_complete_type_without_circularity (tree);
134 static tree get_bindings (tree, tree, tree, bool);
135 static int template_decl_level (tree);
136 static int check_cv_quals_for_unify (int, tree, tree);
137 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
138 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
139 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
140 static void regenerate_decl_from_template (tree, tree);
141 static tree most_specialized_class (tree, tree);
142 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
143 static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
144 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
145 static bool check_specialization_scope (void);
146 static tree process_partial_specialization (tree);
147 static void set_current_access_from_decl (tree);
148 static void check_default_tmpl_args (tree, tree, int, int);
149 static tree get_template_base (tree, tree, tree, tree);
150 static tree try_class_unification (tree, tree, tree, tree);
151 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
153 static int template_args_equal (tree, tree);
154 static void tsubst_default_arguments (tree);
155 static tree for_each_template_parm_r (tree *, int *, void *);
156 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
157 static void copy_default_args_to_explicit_spec (tree);
158 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
159 static int eq_local_specializations (const void *, const void *);
160 static bool dependent_type_p_r (tree);
161 static tree tsubst (tree, tree, tsubst_flags_t, tree);
162 static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
163 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
165 /* Make the current scope suitable for access checking when we are
166 processing T. T can be FUNCTION_DECL for instantiated function
167 template, or VAR_DECL for static member variable (need by
168 instantiate_decl). */
171 push_access_scope (tree t)
173 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
174 || TREE_CODE (t) == VAR_DECL);
176 if (DECL_FRIEND_CONTEXT (t))
177 push_nested_class (DECL_FRIEND_CONTEXT (t));
178 else if (DECL_CLASS_SCOPE_P (t))
179 push_nested_class (DECL_CONTEXT (t));
181 push_to_top_level ();
183 if (TREE_CODE (t) == FUNCTION_DECL)
185 saved_access_scope = tree_cons
186 (NULL_TREE, current_function_decl, saved_access_scope);
187 current_function_decl = t;
191 /* Restore the scope set up by push_access_scope. T is the node we
195 pop_access_scope (tree t)
197 if (TREE_CODE (t) == FUNCTION_DECL)
199 current_function_decl = TREE_VALUE (saved_access_scope);
200 saved_access_scope = TREE_CHAIN (saved_access_scope);
203 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
206 pop_from_top_level ();
209 /* Do any processing required when DECL (a member template
210 declaration) is finished. Returns the TEMPLATE_DECL corresponding
211 to DECL, unless it is a specialization, in which case the DECL
212 itself is returned. */
215 finish_member_template_decl (tree decl)
217 if (decl == error_mark_node)
218 return error_mark_node;
220 gcc_assert (DECL_P (decl));
222 if (TREE_CODE (decl) == TYPE_DECL)
226 type = TREE_TYPE (decl);
227 if (IS_AGGR_TYPE (type)
228 && CLASSTYPE_TEMPLATE_INFO (type)
229 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
231 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
232 check_member_template (tmpl);
237 else if (TREE_CODE (decl) == FIELD_DECL)
238 error ("data member %qD cannot be a member template", decl);
239 else if (DECL_TEMPLATE_INFO (decl))
241 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
243 check_member_template (DECL_TI_TEMPLATE (decl));
244 return DECL_TI_TEMPLATE (decl);
250 error ("invalid member template declaration %qD", decl);
252 return error_mark_node;
255 /* Returns the template nesting level of the indicated class TYPE.
265 A<T>::B<U> has depth two, while A<T> has depth one.
266 Both A<T>::B<int> and A<int>::B<U> have depth one, if
267 they are instantiations, not specializations.
269 This function is guaranteed to return 0 if passed NULL_TREE so
270 that, for example, `template_class_depth (current_class_type)' is
274 template_class_depth (tree type)
279 type && TREE_CODE (type) != NAMESPACE_DECL;
280 type = (TREE_CODE (type) == FUNCTION_DECL)
281 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
283 if (TREE_CODE (type) != FUNCTION_DECL)
285 if (CLASSTYPE_TEMPLATE_INFO (type)
286 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
287 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
292 if (DECL_TEMPLATE_INFO (type)
293 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
294 && uses_template_parms (DECL_TI_ARGS (type)))
302 /* Returns 1 if processing DECL as part of do_pending_inlines
303 needs us to push template parms. */
306 inline_needs_template_parms (tree decl)
308 if (! DECL_TEMPLATE_INFO (decl))
311 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
312 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
315 /* Subroutine of maybe_begin_member_template_processing.
316 Push the template parms in PARMS, starting from LEVELS steps into the
317 chain, and ending at the beginning, since template parms are listed
321 push_inline_template_parms_recursive (tree parmlist, int levels)
323 tree parms = TREE_VALUE (parmlist);
327 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
329 ++processing_template_decl;
330 current_template_parms
331 = tree_cons (size_int (processing_template_decl),
332 parms, current_template_parms);
333 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
335 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
337 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
339 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
341 if (parm == error_mark_node)
344 gcc_assert (DECL_P (parm));
346 switch (TREE_CODE (parm))
355 /* Make a CONST_DECL as is done in process_template_parm.
356 It is ugly that we recreate this here; the original
357 version built in process_template_parm is no longer
359 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
361 DECL_ARTIFICIAL (decl) = 1;
362 TREE_CONSTANT (decl) = 1;
363 TREE_INVARIANT (decl) = 1;
364 TREE_READONLY (decl) = 1;
365 DECL_INITIAL (decl) = DECL_INITIAL (parm);
366 SET_DECL_TEMPLATE_PARM_P (decl);
377 /* Restore the template parameter context for a member template or
378 a friend template defined in a class definition. */
381 maybe_begin_member_template_processing (tree decl)
386 if (inline_needs_template_parms (decl))
388 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
389 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
391 if (DECL_TEMPLATE_SPECIALIZATION (decl))
394 parms = TREE_CHAIN (parms);
397 push_inline_template_parms_recursive (parms, levels);
400 /* Remember how many levels of template parameters we pushed so that
401 we can pop them later. */
402 VEC_safe_push (int, heap, inline_parm_levels, levels);
405 /* Undo the effects of maybe_begin_member_template_processing. */
408 maybe_end_member_template_processing (void)
413 if (VEC_length (int, inline_parm_levels) == 0)
416 last = VEC_pop (int, inline_parm_levels);
417 for (i = 0; i < last; ++i)
419 --processing_template_decl;
420 current_template_parms = TREE_CHAIN (current_template_parms);
425 /* Return a new template argument vector which contains all of ARGS,
426 but has as its innermost set of arguments the EXTRA_ARGS. */
429 add_to_template_args (tree args, tree extra_args)
436 extra_depth = TMPL_ARGS_DEPTH (extra_args);
437 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
439 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
440 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
442 for (j = 1; j <= extra_depth; ++j, ++i)
443 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
448 /* Like add_to_template_args, but only the outermost ARGS are added to
449 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
450 (EXTRA_ARGS) levels are added. This function is used to combine
451 the template arguments from a partial instantiation with the
452 template arguments used to attain the full instantiation from the
453 partial instantiation. */
456 add_outermost_template_args (tree args, tree extra_args)
460 /* If there are more levels of EXTRA_ARGS than there are ARGS,
461 something very fishy is going on. */
462 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
464 /* If *all* the new arguments will be the EXTRA_ARGS, just return
466 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
469 /* For the moment, we make ARGS look like it contains fewer levels. */
470 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
472 new_args = add_to_template_args (args, extra_args);
474 /* Now, we restore ARGS to its full dimensions. */
475 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
480 /* Return the N levels of innermost template arguments from the ARGS. */
483 get_innermost_template_args (tree args, int n)
491 /* If N is 1, just return the innermost set of template arguments. */
493 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
495 /* If we're not removing anything, just return the arguments we were
497 extra_levels = TMPL_ARGS_DEPTH (args) - n;
498 gcc_assert (extra_levels >= 0);
499 if (extra_levels == 0)
502 /* Make a new set of arguments, not containing the outer arguments. */
503 new_args = make_tree_vec (n);
504 for (i = 1; i <= n; ++i)
505 SET_TMPL_ARGS_LEVEL (new_args, i,
506 TMPL_ARGS_LEVEL (args, i + extra_levels));
511 /* We've got a template header coming up; push to a new level for storing
515 begin_template_parm_list (void)
517 /* We use a non-tag-transparent scope here, which causes pushtag to
518 put tags in this scope, rather than in the enclosing class or
519 namespace scope. This is the right thing, since we want
520 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
521 global template class, push_template_decl handles putting the
522 TEMPLATE_DECL into top-level scope. For a nested template class,
525 template <class T> struct S1 {
526 template <class T> struct S2 {};
529 pushtag contains special code to call pushdecl_with_scope on the
530 TEMPLATE_DECL for S2. */
531 begin_scope (sk_template_parms, NULL);
532 ++processing_template_decl;
533 ++processing_template_parmlist;
534 note_template_header (0);
537 /* This routine is called when a specialization is declared. If it is
538 invalid to declare a specialization here, an error is reported and
539 false is returned, otherwise this routine will return true. */
542 check_specialization_scope (void)
544 tree scope = current_scope ();
548 An explicit specialization shall be declared in the namespace of
549 which the template is a member, or, for member templates, in the
550 namespace of which the enclosing class or enclosing class
551 template is a member. An explicit specialization of a member
552 function, member class or static data member of a class template
553 shall be declared in the namespace of which the class template
555 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
557 error ("explicit specialization in non-namespace scope %qD", scope);
563 In an explicit specialization declaration for a member of a class
564 template or a member template that appears in namespace scope,
565 the member template and some of its enclosing class templates may
566 remain unspecialized, except that the declaration shall not
567 explicitly specialize a class member template if its enclosing
568 class templates are not explicitly specialized as well. */
569 if (current_template_parms)
571 error ("enclosing class templates are not explicitly specialized");
578 /* We've just seen template <>. */
581 begin_specialization (void)
583 begin_scope (sk_template_spec, NULL);
584 note_template_header (1);
585 return check_specialization_scope ();
588 /* Called at then end of processing a declaration preceded by
592 end_specialization (void)
595 reset_specialization ();
598 /* Any template <>'s that we have seen thus far are not referring to a
599 function specialization. */
602 reset_specialization (void)
604 processing_specialization = 0;
605 template_header_count = 0;
608 /* We've just seen a template header. If SPECIALIZATION is nonzero,
609 it was of the form template <>. */
612 note_template_header (int specialization)
614 processing_specialization = specialization;
615 template_header_count++;
618 /* We're beginning an explicit instantiation. */
621 begin_explicit_instantiation (void)
623 gcc_assert (!processing_explicit_instantiation);
624 processing_explicit_instantiation = true;
629 end_explicit_instantiation (void)
631 gcc_assert (processing_explicit_instantiation);
632 processing_explicit_instantiation = false;
635 /* An explicit specialization or partial specialization TMPL is being
636 declared. Check that the namespace in which the specialization is
637 occurring is permissible. Returns false iff it is invalid to
638 specialize TMPL in the current namespace. */
641 check_specialization_namespace (tree tmpl)
643 tree tpl_ns = decl_namespace_context (tmpl);
647 An explicit specialization shall be declared in the namespace of
648 which the template is a member, or, for member templates, in the
649 namespace of which the enclosing class or enclosing class
650 template is a member. An explicit specialization of a member
651 function, member class or static data member of a class template
652 shall be declared in the namespace of which the class template is
654 if (is_associated_namespace (current_namespace, tpl_ns))
655 /* Same or super-using namespace. */
659 pedwarn ("specialization of %qD in different namespace", tmpl);
660 pedwarn (" from definition of %q+#D", tmpl);
665 /* SPEC is an explicit instantiation. Check that it is valid to
666 perform this explicit instantiation in the current namespace. */
669 check_explicit_instantiation_namespace (tree spec)
673 /* DR 275: An explicit instantiation shall appear in an enclosing
674 namespace of its template. */
675 ns = decl_namespace_context (spec);
676 if (!is_ancestor (current_namespace, ns))
677 pedwarn ("explicit instantiation of %qD in namespace %qD "
678 "(which does not enclose namespace %qD)",
679 spec, current_namespace, ns);
682 /* The TYPE is being declared. If it is a template type, that means it
683 is a partial specialization. Do appropriate error-checking. */
686 maybe_process_partial_specialization (tree type)
690 if (type == error_mark_node)
691 return error_mark_node;
693 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
695 error ("name of class shadows template template parameter %qD",
697 return error_mark_node;
700 context = TYPE_CONTEXT (type);
702 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
704 /* This is for ordinary explicit specialization and partial
705 specialization of a template class such as:
707 template <> class C<int>;
711 template <class T> class C<T*>;
713 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
715 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
716 && !COMPLETE_TYPE_P (type))
718 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
719 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
720 if (processing_template_decl)
721 push_template_decl (TYPE_MAIN_DECL (type));
723 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
724 error ("specialization of %qT after instantiation", type);
726 else if (CLASS_TYPE_P (type)
727 && !CLASSTYPE_USE_TEMPLATE (type)
728 && CLASSTYPE_TEMPLATE_INFO (type)
729 && context && CLASS_TYPE_P (context)
730 && CLASSTYPE_TEMPLATE_INFO (context))
732 /* This is for an explicit specialization of member class
733 template according to [temp.expl.spec/18]:
735 template <> template <class U> class C<int>::D;
737 The context `C<int>' must be an implicit instantiation.
738 Otherwise this is just a member class template declared
741 template <> class C<int> { template <class U> class D; };
742 template <> template <class U> class C<int>::D;
744 In the first case, `C<int>::D' is a specialization of `C<T>::D'
745 while in the second case, `C<int>::D' is a primary template
746 and `C<T>::D' may not exist. */
748 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
749 && !COMPLETE_TYPE_P (type))
753 if (current_namespace
754 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
756 pedwarn ("specializing %q#T in different namespace", type);
757 pedwarn (" from definition of %q+#D",
758 CLASSTYPE_TI_TEMPLATE (type));
761 /* Check for invalid specialization after instantiation:
763 template <> template <> class C<int>::D<int>;
764 template <> template <class U> class C<int>::D; */
766 for (t = DECL_TEMPLATE_INSTANTIATIONS
767 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
768 t; t = TREE_CHAIN (t))
769 if (TREE_VALUE (t) != type
770 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
771 error ("specialization %qT after instantiation %qT",
772 type, TREE_VALUE (t));
774 /* Mark TYPE as a specialization. And as a result, we only
775 have one level of template argument for the innermost
777 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
778 CLASSTYPE_TI_ARGS (type)
779 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
782 else if (processing_specialization)
784 error ("explicit specialization of non-template %qT", type);
785 return error_mark_node;
791 /* Returns nonzero if we can optimize the retrieval of specializations
792 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
793 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
796 optimize_specialization_lookup_p (tree tmpl)
798 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
799 && DECL_CLASS_SCOPE_P (tmpl)
800 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
802 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
803 /* The optimized lookup depends on the fact that the
804 template arguments for the member function template apply
805 purely to the containing class, which is not true if the
806 containing class is an explicit or partial
808 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
809 && !DECL_MEMBER_TEMPLATE_P (tmpl)
810 && !DECL_CONV_FN_P (tmpl)
811 /* It is possible to have a template that is not a member
812 template and is not a member of a template class:
814 template <typename T>
815 struct S { friend A::f(); };
817 Here, the friend function is a template, but the context does
818 not have template information. The optimized lookup relies
819 on having ARGS be the template arguments for both the class
820 and the function template. */
821 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
824 /* Retrieve the specialization (in the sense of [temp.spec] - a
825 specialization is either an instantiation or an explicit
826 specialization) of TMPL for the given template ARGS. If there is
827 no such specialization, return NULL_TREE. The ARGS are a vector of
828 arguments, or a vector of vectors of arguments, in the case of
829 templates with more than one level of parameters.
831 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
832 then we search for a partial specialization matching ARGS. This
833 parameter is ignored if TMPL is not a class template. */
836 retrieve_specialization (tree tmpl, tree args,
837 bool class_specializations_p)
839 if (args == error_mark_node)
842 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
844 /* There should be as many levels of arguments as there are
845 levels of parameters. */
846 gcc_assert (TMPL_ARGS_DEPTH (args)
847 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
849 if (optimize_specialization_lookup_p (tmpl))
852 tree class_specialization;
853 VEC(tree,gc) *methods;
857 /* The template arguments actually apply to the containing
858 class. Find the class specialization with those
860 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
862 = retrieve_specialization (class_template, args,
863 /*class_specializations_p=*/false);
864 if (!class_specialization)
866 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
867 for the specialization. */
868 idx = class_method_index_for_fn (class_specialization, tmpl);
871 /* Iterate through the methods with the indicated name, looking
872 for the one that has an instance of TMPL. */
873 methods = CLASSTYPE_METHOD_VEC (class_specialization);
874 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
876 tree fn = OVL_CURRENT (fns);
877 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
887 /* Class templates store their instantiations on the
888 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
889 DECL_TEMPLATE_SPECIALIZATIONS list. */
890 if (!class_specializations_p
891 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
892 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
894 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
896 /* Iterate through the list until we find a matching template. */
897 while (*sp != NULL_TREE)
901 if (comp_template_args (TREE_PURPOSE (spec), args))
903 /* Use the move-to-front heuristic to speed up future
907 *sp = TREE_CHAIN (*sp);
908 TREE_CHAIN (spec) = *head;
911 return TREE_VALUE (spec);
913 sp = &TREE_CHAIN (spec);
920 /* Like retrieve_specialization, but for local declarations. */
923 retrieve_local_specialization (tree tmpl)
925 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
926 htab_hash_pointer (tmpl));
927 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
930 /* Returns nonzero iff DECL is a specialization of TMPL. */
933 is_specialization_of (tree decl, tree tmpl)
937 if (TREE_CODE (decl) == FUNCTION_DECL)
941 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
947 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
949 for (t = TREE_TYPE (decl);
951 t = CLASSTYPE_USE_TEMPLATE (t)
952 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
953 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
960 /* Returns nonzero iff DECL is a specialization of friend declaration
961 FRIEND according to [temp.friend]. */
964 is_specialization_of_friend (tree decl, tree friend)
966 bool need_template = true;
969 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
970 || TREE_CODE (decl) == TYPE_DECL);
972 /* For [temp.friend/6] when FRIEND is an ordinary member function
973 of a template class, we want to check if DECL is a specialization
975 if (TREE_CODE (friend) == FUNCTION_DECL
976 && DECL_TEMPLATE_INFO (friend)
977 && !DECL_USE_TEMPLATE (friend))
979 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
980 friend = DECL_TI_TEMPLATE (friend);
981 need_template = false;
983 else if (TREE_CODE (friend) == TEMPLATE_DECL
984 && !PRIMARY_TEMPLATE_P (friend))
985 need_template = false;
987 /* There is nothing to do if this is not a template friend. */
988 if (TREE_CODE (friend) != TEMPLATE_DECL)
991 if (is_specialization_of (decl, friend))
995 A member of a class template may be declared to be a friend of a
996 non-template class. In this case, the corresponding member of
997 every specialization of the class template is a friend of the
998 class granting friendship.
1000 For example, given a template friend declaration
1002 template <class T> friend void A<T>::f();
1004 the member function below is considered a friend
1006 template <> struct A<int> {
1010 For this type of template friend, TEMPLATE_DEPTH below will be
1011 nonzero. To determine if DECL is a friend of FRIEND, we first
1012 check if the enclosing class is a specialization of another. */
1014 template_depth = template_class_depth (DECL_CONTEXT (friend));
1016 && DECL_CLASS_SCOPE_P (decl)
1017 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1018 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1020 /* Next, we check the members themselves. In order to handle
1021 a few tricky cases, such as when FRIEND's are
1023 template <class T> friend void A<T>::g(T t);
1024 template <class T> template <T t> friend void A<T>::h();
1028 void A<int>::g(int);
1029 template <int> void A<int>::h();
1031 we need to figure out ARGS, the template arguments from
1032 the context of DECL. This is required for template substitution
1033 of `T' in the function parameter of `g' and template parameter
1034 of `h' in the above examples. Here ARGS corresponds to `int'. */
1036 tree context = DECL_CONTEXT (decl);
1037 tree args = NULL_TREE;
1038 int current_depth = 0;
1040 while (current_depth < template_depth)
1042 if (CLASSTYPE_TEMPLATE_INFO (context))
1044 if (current_depth == 0)
1045 args = TYPE_TI_ARGS (context);
1047 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1050 context = TYPE_CONTEXT (context);
1053 if (TREE_CODE (decl) == FUNCTION_DECL)
1058 tree friend_args_type;
1059 tree decl_args_type;
1061 /* Make sure that both DECL and FRIEND are templates or
1063 is_template = DECL_TEMPLATE_INFO (decl)
1064 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1065 if (need_template ^ is_template)
1067 else if (is_template)
1069 /* If both are templates, check template parameter list. */
1071 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1073 if (!comp_template_parms
1074 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1078 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1081 decl_type = TREE_TYPE (decl);
1083 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1084 tf_none, NULL_TREE);
1085 if (friend_type == error_mark_node)
1088 /* Check if return types match. */
1089 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1092 /* Check if function parameter types match, ignoring the
1093 `this' parameter. */
1094 friend_args_type = TYPE_ARG_TYPES (friend_type);
1095 decl_args_type = TYPE_ARG_TYPES (decl_type);
1096 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1097 friend_args_type = TREE_CHAIN (friend_args_type);
1098 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1099 decl_args_type = TREE_CHAIN (decl_args_type);
1101 return compparms (decl_args_type, friend_args_type);
1105 /* DECL is a TYPE_DECL */
1107 tree decl_type = TREE_TYPE (decl);
1109 /* Make sure that both DECL and FRIEND are templates or
1112 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1113 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1115 if (need_template ^ is_template)
1117 else if (is_template)
1120 /* If both are templates, check the name of the two
1121 TEMPLATE_DECL's first because is_friend didn't. */
1122 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1123 != DECL_NAME (friend))
1126 /* Now check template parameter list. */
1128 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1130 return comp_template_parms
1131 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1135 return (DECL_NAME (decl)
1136 == DECL_NAME (friend));
1142 /* Register the specialization SPEC as a specialization of TMPL with
1143 the indicated ARGS. IS_FRIEND indicates whether the specialization
1144 is actually just a friend declaration. Returns SPEC, or an
1145 equivalent prior declaration, if available. */
1148 register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
1152 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
1154 if (TREE_CODE (spec) == FUNCTION_DECL
1155 && uses_template_parms (DECL_TI_ARGS (spec)))
1156 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1157 register it; we want the corresponding TEMPLATE_DECL instead.
1158 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1159 the more obvious `uses_template_parms (spec)' to avoid problems
1160 with default function arguments. In particular, given
1161 something like this:
1163 template <class T> void f(T t1, T t = T())
1165 the default argument expression is not substituted for in an
1166 instantiation unless and until it is actually needed. */
1169 fn = retrieve_specialization (tmpl, args,
1170 /*class_specializations_p=*/false);
1171 /* We can sometimes try to re-register a specialization that we've
1172 already got. In particular, regenerate_decl_from_template calls
1173 duplicate_decls which will update the specialization list. But,
1174 we'll still get called again here anyhow. It's more convenient
1175 to simply allow this than to try to prevent it. */
1178 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1180 if (DECL_TEMPLATE_INSTANTIATION (fn))
1183 || DECL_EXPLICIT_INSTANTIATION (fn))
1185 error ("specialization of %qD after instantiation",
1187 return error_mark_node;
1192 /* This situation should occur only if the first
1193 specialization is an implicit instantiation, the
1194 second is an explicit specialization, and the
1195 implicit instantiation has not yet been used. That
1196 situation can occur if we have implicitly
1197 instantiated a member function and then specialized
1200 We can also wind up here if a friend declaration that
1201 looked like an instantiation turns out to be a
1204 template <class T> void foo(T);
1205 class S { friend void foo<>(int) };
1206 template <> void foo(int);
1208 We transform the existing DECL in place so that any
1209 pointers to it become pointers to the updated
1212 If there was a definition for the template, but not
1213 for the specialization, we want this to look as if
1214 there were no definition, and vice versa. */
1215 DECL_INITIAL (fn) = NULL_TREE;
1216 duplicate_decls (spec, fn, is_friend);
1217 /* The call to duplicate_decls will have applied
1220 An explicit specialization of a function template
1221 is inline only if it is explicitly declared to be,
1222 and independently of whether its function template
1225 to the primary function; now copy the inline bits to
1226 the various clones. */
1227 FOR_EACH_CLONE (clone, fn)
1229 DECL_DECLARED_INLINE_P (clone)
1230 = DECL_DECLARED_INLINE_P (fn);
1234 check_specialization_namespace (fn);
1239 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1241 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1242 /* Dup decl failed, but this is a new definition. Set the
1243 line number so any errors match this new
1245 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1251 /* A specialization must be declared in the same namespace as the
1252 template it is specializing. */
1253 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1254 && !check_specialization_namespace (tmpl))
1255 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
1257 if (!optimize_specialization_lookup_p (tmpl))
1258 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1259 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
1264 /* Unregister the specialization SPEC as a specialization of TMPL.
1265 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1266 if the SPEC was listed as a specialization of TMPL. */
1269 reregister_specialization (tree spec, tree tmpl, tree new_spec)
1273 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1275 s = &TREE_CHAIN (*s))
1276 if (TREE_VALUE (*s) == spec)
1279 *s = TREE_CHAIN (*s);
1281 TREE_VALUE (*s) = new_spec;
1288 /* Compare an entry in the local specializations hash table P1 (which
1289 is really a pointer to a TREE_LIST) with P2 (which is really a
1293 eq_local_specializations (const void *p1, const void *p2)
1295 return TREE_VALUE ((tree) p1) == (tree) p2;
1298 /* Hash P1, an entry in the local specializations table. */
1301 hash_local_specialization (const void* p1)
1303 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1306 /* Like register_specialization, but for local declarations. We are
1307 registering SPEC, an instantiation of TMPL. */
1310 register_local_specialization (tree spec, tree tmpl)
1314 slot = htab_find_slot_with_hash (local_specializations, tmpl,
1315 htab_hash_pointer (tmpl), INSERT);
1316 *slot = build_tree_list (spec, tmpl);
1319 /* TYPE is a class type. Returns true if TYPE is an explicitly
1320 specialized class. */
1323 explicit_class_specialization_p (tree type)
1325 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1327 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1330 /* Print the list of candidate FNS in an error message. */
1333 print_candidates (tree fns)
1337 const char *str = "candidates are:";
1339 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1343 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
1344 error ("%s %+#D", str, OVL_CURRENT (f));
1349 /* Returns the template (one of the functions given by TEMPLATE_ID)
1350 which can be specialized to match the indicated DECL with the
1351 explicit template args given in TEMPLATE_ID. The DECL may be
1352 NULL_TREE if none is available. In that case, the functions in
1353 TEMPLATE_ID are non-members.
1355 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1356 specialization of a member template.
1358 The TEMPLATE_COUNT is the number of references to qualifying
1359 template classes that appeared in the name of the function. See
1360 check_explicit_specialization for a more accurate description.
1362 TSK indicates what kind of template declaration (if any) is being
1363 declared. TSK_TEMPLATE indicates that the declaration given by
1364 DECL, though a FUNCTION_DECL, has template parameters, and is
1365 therefore a template function.
1367 The template args (those explicitly specified and those deduced)
1368 are output in a newly created vector *TARGS_OUT.
1370 If it is impossible to determine the result, an error message is
1371 issued. The error_mark_node is returned to indicate failure. */
1374 determine_specialization (tree template_id,
1377 int need_member_template,
1383 tree explicit_targs;
1384 tree candidates = NULL_TREE;
1385 /* A TREE_LIST of templates of which DECL may be a specialization.
1386 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1387 corresponding TREE_PURPOSE is the set of template arguments that,
1388 when used to instantiate the template, would produce a function
1389 with the signature of DECL. */
1390 tree templates = NULL_TREE;
1392 struct cp_binding_level *b;
1394 *targs_out = NULL_TREE;
1396 if (template_id == error_mark_node || decl == error_mark_node)
1397 return error_mark_node;
1399 fns = TREE_OPERAND (template_id, 0);
1400 explicit_targs = TREE_OPERAND (template_id, 1);
1402 if (fns == error_mark_node)
1403 return error_mark_node;
1405 /* Check for baselinks. */
1406 if (BASELINK_P (fns))
1407 fns = BASELINK_FUNCTIONS (fns);
1409 if (!is_overloaded_fn (fns))
1411 error ("%qD is not a function template", fns);
1412 return error_mark_node;
1415 /* Count the number of template headers specified for this
1418 for (b = current_binding_level;
1419 b->kind == sk_template_parms;
1423 for (; fns; fns = OVL_NEXT (fns))
1425 tree fn = OVL_CURRENT (fns);
1427 if (TREE_CODE (fn) == TEMPLATE_DECL)
1429 tree decl_arg_types;
1432 /* In case of explicit specialization, we need to check if
1433 the number of template headers appearing in the specialization
1434 is correct. This is usually done in check_explicit_specialization,
1435 but the check done there cannot be exhaustive when specializing
1436 member functions. Consider the following code:
1438 template <> void A<int>::f(int);
1439 template <> template <> void A<int>::f(int);
1441 Assuming that A<int> is not itself an explicit specialization
1442 already, the first line specializes "f" which is a non-template
1443 member function, whilst the second line specializes "f" which
1444 is a template member function. So both lines are syntactically
1445 correct, and check_explicit_specialization does not reject
1448 Here, we can do better, as we are matching the specialization
1449 against the declarations. We count the number of template
1450 headers, and we check if they match TEMPLATE_COUNT + 1
1451 (TEMPLATE_COUNT is the number of qualifying template classes,
1452 plus there must be another header for the member template
1455 Notice that if header_count is zero, this is not a
1456 specialization but rather a template instantiation, so there
1457 is no check we can perform here. */
1458 if (header_count && header_count != template_count + 1)
1461 /* Check that the number of template arguments at the
1462 innermost level for DECL is the same as for FN. */
1463 if (current_binding_level->kind == sk_template_parms
1464 && !current_binding_level->explicit_spec_p
1465 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
1466 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1467 (current_template_parms))))
1470 /* DECL might be a specialization of FN. */
1471 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1472 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1474 /* For a non-static member function, we need to make sure
1475 that the const qualification is the same. Since
1476 get_bindings does not try to merge the "this" parameter,
1477 we must do the comparison explicitly. */
1478 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1479 && !same_type_p (TREE_VALUE (fn_arg_types),
1480 TREE_VALUE (decl_arg_types)))
1483 /* Skip the "this" parameter and, for constructors of
1484 classes with virtual bases, the VTT parameter. A
1485 full specialization of a constructor will have a VTT
1486 parameter, but a template never will. */
1488 = skip_artificial_parms_for (decl, decl_arg_types);
1490 = skip_artificial_parms_for (fn, fn_arg_types);
1492 /* Check that the number of function parameters matches.
1494 template <class T> void f(int i = 0);
1495 template <> void f<int>();
1496 The specialization f<int> is invalid but is not caught
1497 by get_bindings below. */
1498 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1501 /* Function templates cannot be specializations; there are
1502 no partial specializations of functions. Therefore, if
1503 the type of DECL does not match FN, there is no
1505 if (tsk == tsk_template)
1507 if (compparms (fn_arg_types, decl_arg_types))
1508 candidates = tree_cons (NULL_TREE, fn, candidates);
1512 /* See whether this function might be a specialization of this
1514 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
1517 /* We cannot deduce template arguments that when used to
1518 specialize TMPL will produce DECL. */
1521 /* Save this template, and the arguments deduced. */
1522 templates = tree_cons (targs, fn, templates);
1524 else if (need_member_template)
1525 /* FN is an ordinary member function, and we need a
1526 specialization of a member template. */
1528 else if (TREE_CODE (fn) != FUNCTION_DECL)
1529 /* We can get IDENTIFIER_NODEs here in certain erroneous
1532 else if (!DECL_FUNCTION_MEMBER_P (fn))
1533 /* This is just an ordinary non-member function. Nothing can
1534 be a specialization of that. */
1536 else if (DECL_ARTIFICIAL (fn))
1537 /* Cannot specialize functions that are created implicitly. */
1541 tree decl_arg_types;
1543 /* This is an ordinary member function. However, since
1544 we're here, we can assume it's enclosing class is a
1545 template class. For example,
1547 template <typename T> struct S { void f(); };
1548 template <> void S<int>::f() {}
1550 Here, S<int>::f is a non-template, but S<int> is a
1551 template class. If FN has the same type as DECL, we
1552 might be in business. */
1554 if (!DECL_TEMPLATE_INFO (fn))
1555 /* Its enclosing class is an explicit specialization
1556 of a template class. This is not a candidate. */
1559 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1560 TREE_TYPE (TREE_TYPE (fn))))
1561 /* The return types differ. */
1564 /* Adjust the type of DECL in case FN is a static member. */
1565 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1566 if (DECL_STATIC_FUNCTION_P (fn)
1567 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1568 decl_arg_types = TREE_CHAIN (decl_arg_types);
1570 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1573 candidates = tree_cons (NULL_TREE, fn, candidates);
1577 if (templates && TREE_CHAIN (templates))
1583 It is possible for a specialization with a given function
1584 signature to be instantiated from more than one function
1585 template. In such cases, explicit specification of the
1586 template arguments must be used to uniquely identify the
1587 function template specialization being specialized.
1589 Note that here, there's no suggestion that we're supposed to
1590 determine which of the candidate templates is most
1591 specialized. However, we, also have:
1595 Partial ordering of overloaded function template
1596 declarations is used in the following contexts to select
1597 the function template to which a function template
1598 specialization refers:
1600 -- when an explicit specialization refers to a function
1603 So, we do use the partial ordering rules, at least for now.
1604 This extension can only serve to make invalid programs valid,
1605 so it's safe. And, there is strong anecdotal evidence that
1606 the committee intended the partial ordering rules to apply;
1607 the EDG front-end has that behavior, and John Spicer claims
1608 that the committee simply forgot to delete the wording in
1609 [temp.expl.spec]. */
1610 tree tmpl = most_specialized_instantiation (templates);
1611 if (tmpl != error_mark_node)
1614 TREE_CHAIN (templates) = NULL_TREE;
1618 if (templates == NULL_TREE && candidates == NULL_TREE)
1620 error ("template-id %qD for %q+D does not match any template "
1621 "declaration", template_id, decl);
1622 return error_mark_node;
1624 else if ((templates && TREE_CHAIN (templates))
1625 || (candidates && TREE_CHAIN (candidates))
1626 || (templates && candidates))
1628 error ("ambiguous template specialization %qD for %q+D",
1630 chainon (candidates, templates);
1631 print_candidates (candidates);
1632 return error_mark_node;
1635 /* We have one, and exactly one, match. */
1638 tree fn = TREE_VALUE (candidates);
1639 /* DECL is a re-declaration of a template function. */
1640 if (TREE_CODE (fn) == TEMPLATE_DECL)
1642 /* It was a specialization of an ordinary member function in a
1644 *targs_out = copy_node (DECL_TI_ARGS (fn));
1645 return DECL_TI_TEMPLATE (fn);
1648 /* It was a specialization of a template. */
1649 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
1650 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1652 *targs_out = copy_node (targs);
1653 SET_TMPL_ARGS_LEVEL (*targs_out,
1654 TMPL_ARGS_DEPTH (*targs_out),
1655 TREE_PURPOSE (templates));
1658 *targs_out = TREE_PURPOSE (templates);
1659 return TREE_VALUE (templates);
1662 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1663 but with the default argument values filled in from those in the
1667 copy_default_args_to_explicit_spec_1 (tree spec_types,
1670 tree new_spec_types;
1675 if (spec_types == void_list_node)
1676 return void_list_node;
1678 /* Substitute into the rest of the list. */
1680 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1681 TREE_CHAIN (tmpl_types));
1683 /* Add the default argument for this parameter. */
1684 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1685 TREE_VALUE (spec_types),
1689 /* DECL is an explicit specialization. Replicate default arguments
1690 from the template it specializes. (That way, code like:
1692 template <class T> void f(T = 3);
1693 template <> void f(double);
1696 works, as required.) An alternative approach would be to look up
1697 the correct default arguments at the call-site, but this approach
1698 is consistent with how implicit instantiations are handled. */
1701 copy_default_args_to_explicit_spec (tree decl)
1706 tree new_spec_types;
1710 tree object_type = NULL_TREE;
1711 tree in_charge = NULL_TREE;
1712 tree vtt = NULL_TREE;
1714 /* See if there's anything we need to do. */
1715 tmpl = DECL_TI_TEMPLATE (decl);
1716 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1717 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1718 if (TREE_PURPOSE (t))
1723 old_type = TREE_TYPE (decl);
1724 spec_types = TYPE_ARG_TYPES (old_type);
1726 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1728 /* Remove the this pointer, but remember the object's type for
1730 object_type = TREE_TYPE (TREE_VALUE (spec_types));
1731 spec_types = TREE_CHAIN (spec_types);
1732 tmpl_types = TREE_CHAIN (tmpl_types);
1734 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
1736 /* DECL may contain more parameters than TMPL due to the extra
1737 in-charge parameter in constructors and destructors. */
1738 in_charge = spec_types;
1739 spec_types = TREE_CHAIN (spec_types);
1741 if (DECL_HAS_VTT_PARM_P (decl))
1744 spec_types = TREE_CHAIN (spec_types);
1748 /* Compute the merged default arguments. */
1750 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1752 /* Compute the new FUNCTION_TYPE. */
1756 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1761 /* Put the in-charge parameter back. */
1762 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1763 TREE_VALUE (in_charge),
1766 new_type = build_method_type_directly (object_type,
1767 TREE_TYPE (old_type),
1771 new_type = build_function_type (TREE_TYPE (old_type),
1773 new_type = cp_build_type_attribute_variant (new_type,
1774 TYPE_ATTRIBUTES (old_type));
1775 new_type = build_exception_variant (new_type,
1776 TYPE_RAISES_EXCEPTIONS (old_type));
1777 TREE_TYPE (decl) = new_type;
1780 /* Check to see if the function just declared, as indicated in
1781 DECLARATOR, and in DECL, is a specialization of a function
1782 template. We may also discover that the declaration is an explicit
1783 instantiation at this point.
1785 Returns DECL, or an equivalent declaration that should be used
1786 instead if all goes well. Issues an error message if something is
1787 amiss. Returns error_mark_node if the error is not easily
1790 FLAGS is a bitmask consisting of the following flags:
1792 2: The function has a definition.
1793 4: The function is a friend.
1795 The TEMPLATE_COUNT is the number of references to qualifying
1796 template classes that appeared in the name of the function. For
1799 template <class T> struct S { void f(); };
1802 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1803 classes are not counted in the TEMPLATE_COUNT, so that in
1805 template <class T> struct S {};
1806 template <> struct S<int> { void f(); }
1807 template <> void S<int>::f();
1809 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1810 invalid; there should be no template <>.)
1812 If the function is a specialization, it is marked as such via
1813 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1814 is set up correctly, and it is added to the list of specializations
1815 for that template. */
1818 check_explicit_specialization (tree declarator,
1823 int have_def = flags & 2;
1824 int is_friend = flags & 4;
1825 int specialization = 0;
1826 int explicit_instantiation = 0;
1827 int member_specialization = 0;
1828 tree ctype = DECL_CLASS_CONTEXT (decl);
1829 tree dname = DECL_NAME (decl);
1834 if (!processing_specialization)
1837 tsk = tsk_excessive_parms;
1840 tsk = current_tmpl_spec_kind (template_count);
1845 if (processing_specialization)
1848 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1850 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1853 /* This could be something like:
1855 template <class T> void f(T);
1856 class S { friend void f<>(int); } */
1860 /* This case handles bogus declarations like template <>
1861 template <class T> void f<int>(); */
1863 error ("template-id %qD in declaration of primary template",
1870 case tsk_invalid_member_spec:
1871 /* The error has already been reported in
1872 check_specialization_scope. */
1873 return error_mark_node;
1875 case tsk_invalid_expl_inst:
1876 error ("template parameter list used in explicit instantiation");
1882 error ("definition provided for explicit instantiation");
1884 explicit_instantiation = 1;
1887 case tsk_excessive_parms:
1888 case tsk_insufficient_parms:
1889 if (tsk == tsk_excessive_parms)
1890 error ("too many template parameter lists in declaration of %qD",
1892 else if (template_header_count)
1893 error("too few template parameter lists in declaration of %qD", decl);
1895 error("explicit specialization of %qD must be introduced by "
1896 "%<template <>%>", decl);
1900 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1902 member_specialization = 1;
1908 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1910 /* This case handles bogus declarations like template <>
1911 template <class T> void f<int>(); */
1913 if (uses_template_parms (declarator))
1914 error ("function template partial specialization %qD "
1915 "is not allowed", declarator);
1917 error ("template-id %qD in declaration of primary template",
1922 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1923 /* This is a specialization of a member template, without
1924 specialization the containing class. Something like:
1926 template <class T> struct S {
1927 template <class U> void f (U);
1929 template <> template <class U> void S<int>::f(U) {}
1931 That's a specialization -- but of the entire template. */
1939 if (specialization || member_specialization)
1941 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1942 for (; t; t = TREE_CHAIN (t))
1943 if (TREE_PURPOSE (t))
1946 ("default argument specified in explicit specialization");
1951 if (specialization || member_specialization || explicit_instantiation)
1953 tree tmpl = NULL_TREE;
1954 tree targs = NULL_TREE;
1956 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1957 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1961 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
1966 /* If there is no class context, the explicit instantiation
1967 must be at namespace scope. */
1968 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
1970 /* Find the namespace binding, using the declaration
1972 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
1974 if (!fns || !is_overloaded_fn (fns))
1976 error ("%qD is not a template function", dname);
1977 fns = error_mark_node;
1981 tree fn = OVL_CURRENT (fns);
1982 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
1983 CP_DECL_CONTEXT (fn)))
1984 error ("%qD is not declared in %qD",
1985 decl, current_namespace);
1989 declarator = lookup_template_function (fns, NULL_TREE);
1992 if (declarator == error_mark_node)
1993 return error_mark_node;
1995 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1997 if (!explicit_instantiation)
1998 /* A specialization in class scope. This is invalid,
1999 but the error will already have been flagged by
2000 check_specialization_scope. */
2001 return error_mark_node;
2004 /* It's not valid to write an explicit instantiation in
2007 class C { template void f(); }
2009 This case is caught by the parser. However, on
2012 template class C { void f(); };
2014 (which is invalid) we can get here. The error will be
2021 else if (ctype != NULL_TREE
2022 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2025 /* Find the list of functions in ctype that have the same
2026 name as the declared function. */
2027 tree name = TREE_OPERAND (declarator, 0);
2028 tree fns = NULL_TREE;
2031 if (constructor_name_p (name, ctype))
2033 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2035 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
2036 : !CLASSTYPE_DESTRUCTORS (ctype))
2038 /* From [temp.expl.spec]:
2040 If such an explicit specialization for the member
2041 of a class template names an implicitly-declared
2042 special member function (clause _special_), the
2043 program is ill-formed.
2045 Similar language is found in [temp.explicit]. */
2046 error ("specialization of implicitly-declared special member function");
2047 return error_mark_node;
2050 name = is_constructor ? ctor_identifier : dtor_identifier;
2053 if (!DECL_CONV_FN_P (decl))
2055 idx = lookup_fnfields_1 (ctype, name);
2057 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
2061 VEC(tree,gc) *methods;
2064 /* For a type-conversion operator, we cannot do a
2065 name-based lookup. We might be looking for `operator
2066 int' which will be a specialization of `operator T'.
2067 So, we find *all* the conversion operators, and then
2068 select from them. */
2071 methods = CLASSTYPE_METHOD_VEC (ctype);
2073 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2074 VEC_iterate (tree, methods, idx, ovl);
2077 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2078 /* There are no more conversion functions. */
2081 /* Glue all these conversion functions together
2082 with those we already have. */
2083 for (; ovl; ovl = OVL_NEXT (ovl))
2084 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2088 if (fns == NULL_TREE)
2090 error ("no member function %qD declared in %qT", name, ctype);
2091 return error_mark_node;
2094 TREE_OPERAND (declarator, 0) = fns;
2097 /* Figure out what exactly is being specialized at this point.
2098 Note that for an explicit instantiation, even one for a
2099 member function, we cannot tell apriori whether the
2100 instantiation is for a member template, or just a member
2101 function of a template class. Even if a member template is
2102 being instantiated, the member template arguments may be
2103 elided if they can be deduced from the rest of the
2105 tmpl = determine_specialization (declarator, decl,
2107 member_specialization,
2111 if (!tmpl || tmpl == error_mark_node)
2112 /* We couldn't figure out what this declaration was
2114 return error_mark_node;
2117 tree gen_tmpl = most_general_template (tmpl);
2119 if (explicit_instantiation)
2121 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2122 is done by do_decl_instantiation later. */
2124 int arg_depth = TMPL_ARGS_DEPTH (targs);
2125 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2127 if (arg_depth > parm_depth)
2129 /* If TMPL is not the most general template (for
2130 example, if TMPL is a friend template that is
2131 injected into namespace scope), then there will
2132 be too many levels of TARGS. Remove some of them
2137 new_targs = make_tree_vec (parm_depth);
2138 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2139 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2140 = TREE_VEC_ELT (targs, i);
2144 return instantiate_template (tmpl, targs, tf_error);
2147 /* If we thought that the DECL was a member function, but it
2148 turns out to be specializing a static member function,
2149 make DECL a static member function as well. */
2150 if (DECL_STATIC_FUNCTION_P (tmpl)
2151 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2152 revert_static_member_fn (decl);
2154 /* If this is a specialization of a member template of a
2155 template class, we want to return the TEMPLATE_DECL, not
2156 the specialization of it. */
2157 if (tsk == tsk_template)
2159 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2160 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
2163 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2164 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2165 = DECL_SOURCE_LOCATION (decl);
2166 /* We want to use the argument list specified in the
2167 definition, not in the original declaration. */
2168 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2169 = DECL_ARGUMENTS (decl);
2174 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2175 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
2177 /* Inherit default function arguments from the template
2178 DECL is specializing. */
2179 copy_default_args_to_explicit_spec (decl);
2181 /* This specialization has the same protection as the
2182 template it specializes. */
2183 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2184 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
2186 /* If DECL is a friend declaration, declared using an
2187 unqualified name, the namespace associated with DECL may
2188 have been set incorrectly. For example, in:
2190 template <typename T> void f(T);
2192 struct S { friend void f<int>(int); }
2195 we will have set the DECL_CONTEXT for the friend
2196 declaration to N, rather than to the global namespace. */
2197 if (DECL_NAMESPACE_SCOPE_P (decl))
2198 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
2200 if (is_friend && !have_def)
2201 /* This is not really a declaration of a specialization.
2202 It's just the name of an instantiation. But, it's not
2203 a request for an instantiation, either. */
2204 SET_DECL_IMPLICIT_INSTANTIATION (decl);
2205 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2206 /* This is indeed a specialization. In case of constructors
2207 and destructors, we need in-charge and not-in-charge
2208 versions in V3 ABI. */
2209 clone_function_decl (decl, /*update_method_vec_p=*/0);
2211 /* Register this specialization so that we can find it
2213 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
2220 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2221 parameters. These are represented in the same format used for
2222 DECL_TEMPLATE_PARMS. */
2225 comp_template_parms (tree parms1, tree parms2)
2230 if (parms1 == parms2)
2233 for (p1 = parms1, p2 = parms2;
2234 p1 != NULL_TREE && p2 != NULL_TREE;
2235 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2237 tree t1 = TREE_VALUE (p1);
2238 tree t2 = TREE_VALUE (p2);
2241 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2242 gcc_assert (TREE_CODE (t2) == TREE_VEC);
2244 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2247 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
2249 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2250 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
2252 /* If either of the template parameters are invalid, assume
2253 they match for the sake of error recovery. */
2254 if (parm1 == error_mark_node || parm2 == error_mark_node)
2257 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2260 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2262 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
2267 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2268 /* One set of parameters has more parameters lists than the
2275 /* Complain if DECL shadows a template parameter.
2277 [temp.local]: A template-parameter shall not be redeclared within its
2278 scope (including nested scopes). */
2281 check_template_shadow (tree decl)
2285 /* If we're not in a template, we can't possibly shadow a template
2287 if (!current_template_parms)
2290 /* Figure out what we're shadowing. */
2291 if (TREE_CODE (decl) == OVERLOAD)
2292 decl = OVL_CURRENT (decl);
2293 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
2295 /* If there's no previous binding for this name, we're not shadowing
2296 anything, let alone a template parameter. */
2300 /* If we're not shadowing a template parameter, we're done. Note
2301 that OLDDECL might be an OVERLOAD (or perhaps even an
2302 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2304 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
2307 /* We check for decl != olddecl to avoid bogus errors for using a
2308 name inside a class. We check TPFI to avoid duplicate errors for
2309 inline member templates. */
2311 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2314 error ("declaration of %q+#D", decl);
2315 error (" shadows template parm %q+#D", olddecl);
2318 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2319 ORIG_LEVEL, DECL, and TYPE. */
2322 build_template_parm_index (int index,
2328 tree t = make_node (TEMPLATE_PARM_INDEX);
2329 TEMPLATE_PARM_IDX (t) = index;
2330 TEMPLATE_PARM_LEVEL (t) = level;
2331 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2332 TEMPLATE_PARM_DECL (t) = decl;
2333 TREE_TYPE (t) = type;
2334 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
2335 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
2336 TREE_READONLY (t) = TREE_READONLY (decl);
2341 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2342 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2343 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2344 new one is created. */
2347 reduce_template_parm_level (tree index, tree type, int levels)
2349 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2350 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
2351 != TEMPLATE_PARM_LEVEL (index) - levels))
2353 tree orig_decl = TEMPLATE_PARM_DECL (index);
2356 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2357 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
2358 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
2359 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2360 DECL_ARTIFICIAL (decl) = 1;
2361 SET_DECL_TEMPLATE_PARM_P (decl);
2363 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
2364 TEMPLATE_PARM_LEVEL (index) - levels,
2365 TEMPLATE_PARM_ORIG_LEVEL (index),
2367 TEMPLATE_PARM_DESCENDANTS (index) = t;
2369 /* Template template parameters need this. */
2370 if (TREE_CODE (decl) != CONST_DECL)
2371 DECL_TEMPLATE_PARMS (decl)
2372 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
2375 return TEMPLATE_PARM_DESCENDANTS (index);
2378 /* Process information from new template parameter PARM and append it to the
2379 LIST being built. This new parameter is a non-type parameter iff
2380 IS_NON_TYPE is true. */
2383 process_template_parm (tree list, tree parm, bool is_non_type)
2390 gcc_assert (TREE_CODE (parm) == TREE_LIST);
2391 defval = TREE_PURPOSE (parm);
2395 tree p = tree_last (list);
2397 if (p && TREE_VALUE (p) != error_mark_node)
2400 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2401 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2403 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2413 parm = TREE_VALUE (parm);
2415 SET_DECL_TEMPLATE_PARM_P (parm);
2417 if (TREE_TYPE (parm) == error_mark_node)
2419 err_parm_list = build_tree_list (defval, parm);
2420 TREE_VALUE (err_parm_list) = error_mark_node;
2421 return chainon (list, err_parm_list);
2427 The top-level cv-qualifiers on the template-parameter are
2428 ignored when determining its type. */
2429 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2430 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
2432 err_parm_list = build_tree_list (defval, parm);
2433 TREE_VALUE (err_parm_list) = error_mark_node;
2434 return chainon (list, err_parm_list);
2438 /* A template parameter is not modifiable. */
2439 TREE_CONSTANT (parm) = 1;
2440 TREE_INVARIANT (parm) = 1;
2441 TREE_READONLY (parm) = 1;
2442 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
2443 TREE_CONSTANT (decl) = 1;
2444 TREE_INVARIANT (decl) = 1;
2445 TREE_READONLY (decl) = 1;
2446 DECL_INITIAL (parm) = DECL_INITIAL (decl)
2447 = build_template_parm_index (idx, processing_template_decl,
2448 processing_template_decl,
2449 decl, TREE_TYPE (parm));
2454 parm = TREE_VALUE (TREE_VALUE (parm));
2456 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2458 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
2459 /* This is for distinguishing between real templates and template
2460 template parameters */
2461 TREE_TYPE (parm) = t;
2462 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2467 t = make_aggr_type (TEMPLATE_TYPE_PARM);
2468 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2469 decl = build_decl (TYPE_DECL, parm, t);
2472 TYPE_NAME (t) = decl;
2473 TYPE_STUB_DECL (t) = decl;
2475 TEMPLATE_TYPE_PARM_INDEX (t)
2476 = build_template_parm_index (idx, processing_template_decl,
2477 processing_template_decl,
2478 decl, TREE_TYPE (parm));
2480 DECL_ARTIFICIAL (decl) = 1;
2481 SET_DECL_TEMPLATE_PARM_P (decl);
2483 parm = build_tree_list (defval, parm);
2484 return chainon (list, parm);
2487 /* The end of a template parameter list has been reached. Process the
2488 tree list into a parameter vector, converting each parameter into a more
2489 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2493 end_template_parm_list (tree parms)
2497 tree saved_parmlist = make_tree_vec (list_length (parms));
2499 current_template_parms
2500 = tree_cons (size_int (processing_template_decl),
2501 saved_parmlist, current_template_parms);
2503 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2505 next = TREE_CHAIN (parm);
2506 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2507 TREE_CHAIN (parm) = NULL_TREE;
2510 --processing_template_parmlist;
2512 return saved_parmlist;
2515 /* end_template_decl is called after a template declaration is seen. */
2518 end_template_decl (void)
2520 reset_specialization ();
2522 if (! processing_template_decl)
2525 /* This matches the pushlevel in begin_template_parm_list. */
2528 --processing_template_decl;
2529 current_template_parms = TREE_CHAIN (current_template_parms);
2532 /* Given a template argument vector containing the template PARMS.
2533 The innermost PARMS are given first. */
2536 current_template_args (void)
2539 tree args = NULL_TREE;
2540 int length = TMPL_PARMS_DEPTH (current_template_parms);
2543 /* If there is only one level of template parameters, we do not
2544 create a TREE_VEC of TREE_VECs. Instead, we return a single
2545 TREE_VEC containing the arguments. */
2547 args = make_tree_vec (length);
2549 for (header = current_template_parms; header; header = TREE_CHAIN (header))
2551 tree a = copy_node (TREE_VALUE (header));
2554 TREE_TYPE (a) = NULL_TREE;
2555 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
2557 tree t = TREE_VEC_ELT (a, i);
2559 /* T will be a list if we are called from within a
2560 begin/end_template_parm_list pair, but a vector directly
2561 if within a begin/end_member_template_processing pair. */
2562 if (TREE_CODE (t) == TREE_LIST)
2566 if (t != error_mark_node)
2568 if (TREE_CODE (t) == TYPE_DECL
2569 || TREE_CODE (t) == TEMPLATE_DECL)
2572 t = DECL_INITIAL (t);
2575 TREE_VEC_ELT (a, i) = t;
2580 TREE_VEC_ELT (args, --l) = a;
2588 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2589 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
2590 a member template. Used by push_template_decl below. */
2593 build_template_decl (tree decl, tree parms, bool member_template_p)
2595 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2596 DECL_TEMPLATE_PARMS (tmpl) = parms;
2597 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
2598 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
2599 if (DECL_LANG_SPECIFIC (decl))
2601 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2602 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
2603 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
2604 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
2605 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2606 if (DECL_OVERLOADED_OPERATOR_P (decl))
2607 SET_OVERLOADED_OPERATOR_CODE (tmpl,
2608 DECL_OVERLOADED_OPERATOR_P (decl));
2614 struct template_parm_data
2616 /* The level of the template parameters we are currently
2620 /* The index of the specialization argument we are currently
2624 /* An array whose size is the number of template parameters. The
2625 elements are nonzero if the parameter has been used in any one
2626 of the arguments processed so far. */
2629 /* An array whose size is the number of template arguments. The
2630 elements are nonzero if the argument makes use of template
2631 parameters of this level. */
2632 int* arg_uses_template_parms;
2635 /* Subroutine of push_template_decl used to see if each template
2636 parameter in a partial specialization is used in the explicit
2637 argument list. If T is of the LEVEL given in DATA (which is
2638 treated as a template_parm_data*), then DATA->PARMS is marked
2642 mark_template_parm (tree t, void* data)
2646 struct template_parm_data* tpd = (struct template_parm_data*) data;
2648 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2650 level = TEMPLATE_PARM_LEVEL (t);
2651 idx = TEMPLATE_PARM_IDX (t);
2655 level = TEMPLATE_TYPE_LEVEL (t);
2656 idx = TEMPLATE_TYPE_IDX (t);
2659 if (level == tpd->level)
2661 tpd->parms[idx] = 1;
2662 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2665 /* Return zero so that for_each_template_parm will continue the
2666 traversal of the tree; we want to mark *every* template parm. */
2670 /* Process the partial specialization DECL. */
2673 process_partial_specialization (tree decl)
2675 tree type = TREE_TYPE (decl);
2676 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2677 tree specargs = CLASSTYPE_TI_ARGS (type);
2678 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
2679 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2680 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2681 int nargs = TREE_VEC_LENGTH (inner_args);
2682 int ntparms = TREE_VEC_LENGTH (inner_parms);
2684 int did_error_intro = 0;
2685 struct template_parm_data tpd;
2686 struct template_parm_data tpd2;
2688 /* We check that each of the template parameters given in the
2689 partial specialization is used in the argument list to the
2690 specialization. For example:
2692 template <class T> struct S;
2693 template <class T> struct S<T*>;
2695 The second declaration is OK because `T*' uses the template
2696 parameter T, whereas
2698 template <class T> struct S<int>;
2700 is no good. Even trickier is:
2711 The S2<T> declaration is actually invalid; it is a
2712 full-specialization. Of course,
2715 struct S2<T (*)(U)>;
2717 or some such would have been OK. */
2718 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2719 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
2720 memset (tpd.parms, 0, sizeof (int) * ntparms);
2722 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
2723 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
2724 for (i = 0; i < nargs; ++i)
2726 tpd.current_arg = i;
2727 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2728 &mark_template_parm,
2732 for (i = 0; i < ntparms; ++i)
2733 if (tpd.parms[i] == 0)
2735 /* One of the template parms was not used in the
2737 if (!did_error_intro)
2739 error ("template parameters not used in partial specialization:");
2740 did_error_intro = 1;
2743 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2746 /* [temp.class.spec]
2748 The argument list of the specialization shall not be identical to
2749 the implicit argument list of the primary template. */
2750 if (comp_template_args
2752 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2754 error ("partial specialization %qT does not specialize any template arguments", type);
2756 /* [temp.class.spec]
2758 A partially specialized non-type argument expression shall not
2759 involve template parameters of the partial specialization except
2760 when the argument expression is a simple identifier.
2762 The type of a template parameter corresponding to a specialized
2763 non-type argument shall not be dependent on a parameter of the
2765 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
2767 for (i = 0; i < nargs; ++i)
2769 tree arg = TREE_VEC_ELT (inner_args, i);
2770 if (/* These first two lines are the `non-type' bit. */
2772 && TREE_CODE (arg) != TEMPLATE_DECL
2773 /* This next line is the `argument expression is not just a
2774 simple identifier' condition and also the `specialized
2775 non-type argument' bit. */
2776 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2778 if (tpd.arg_uses_template_parms[i])
2779 error ("template argument %qE involves template parameter(s)", arg);
2782 /* Look at the corresponding template parameter,
2783 marking which template parameters its type depends
2786 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2791 /* We haven't yet initialized TPD2. Do so now. */
2792 tpd2.arg_uses_template_parms
2793 = (int *) alloca (sizeof (int) * nargs);
2794 /* The number of parameters here is the number in the
2795 main template, which, as checked in the assertion
2797 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
2799 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2802 /* Mark the template parameters. But this time, we're
2803 looking for the template parameters of the main
2804 template, not in the specialization. */
2805 tpd2.current_arg = i;
2806 tpd2.arg_uses_template_parms[i] = 0;
2807 memset (tpd2.parms, 0, sizeof (int) * nargs);
2808 for_each_template_parm (type,
2809 &mark_template_parm,
2813 if (tpd2.arg_uses_template_parms [i])
2815 /* The type depended on some template parameters.
2816 If they are fully specialized in the
2817 specialization, that's OK. */
2819 for (j = 0; j < nargs; ++j)
2820 if (tpd2.parms[j] != 0
2821 && tpd.arg_uses_template_parms [j])
2823 error ("type %qT of template argument %qE depends "
2824 "on template parameter(s)",
2834 if (retrieve_specialization (maintmpl, specargs,
2835 /*class_specializations_p=*/true))
2836 /* We've already got this specialization. */
2839 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
2840 = tree_cons (specargs, inner_parms,
2841 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2842 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2846 /* Check that a template declaration's use of default arguments is not
2847 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2848 nonzero if DECL is the thing declared by a primary template.
2849 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2852 check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
2855 int last_level_to_check;
2860 A default template-argument shall not be specified in a
2861 function template declaration or a function template definition, nor
2862 in the template-parameter-list of the definition of a member of a
2865 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
2866 /* You can't have a function template declaration in a local
2867 scope, nor you can you define a member of a class template in a
2871 if (current_class_type
2872 && !TYPE_BEING_DEFINED (current_class_type)
2873 && DECL_LANG_SPECIFIC (decl)
2874 /* If this is either a friend defined in the scope of the class
2875 or a member function. */
2876 && (DECL_FUNCTION_MEMBER_P (decl)
2877 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2878 : DECL_FRIEND_CONTEXT (decl)
2879 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2881 /* And, if it was a member function, it really was defined in
2882 the scope of the class. */
2883 && (!DECL_FUNCTION_MEMBER_P (decl)
2884 || DECL_INITIALIZED_IN_CLASS_P (decl)))
2885 /* We already checked these parameters when the template was
2886 declared, so there's no need to do it again now. This function
2887 was defined in class scope, but we're processing it's body now
2888 that the class is complete. */
2893 If a template-parameter has a default template-argument, all
2894 subsequent template-parameters shall have a default
2895 template-argument supplied. */
2896 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2898 tree inner_parms = TREE_VALUE (parm_level);
2899 int ntparms = TREE_VEC_LENGTH (inner_parms);
2900 int seen_def_arg_p = 0;
2903 for (i = 0; i < ntparms; ++i)
2905 tree parm = TREE_VEC_ELT (inner_parms, i);
2907 if (parm == error_mark_node)
2910 if (TREE_PURPOSE (parm))
2912 else if (seen_def_arg_p)
2914 error ("no default argument for %qD", TREE_VALUE (parm));
2915 /* For better subsequent error-recovery, we indicate that
2916 there should have been a default argument. */
2917 TREE_PURPOSE (parm) = error_mark_node;
2922 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2923 /* For an ordinary class template, default template arguments are
2924 allowed at the innermost level, e.g.:
2925 template <class T = int>
2927 but, in a partial specialization, they're not allowed even
2928 there, as we have in [temp.class.spec]:
2930 The template parameter list of a specialization shall not
2931 contain default template argument values.
2933 So, for a partial specialization, or for a function template,
2934 we look at all of them. */
2937 /* But, for a primary class template that is not a partial
2938 specialization we look at all template parameters except the
2940 parms = TREE_CHAIN (parms);
2942 /* Figure out what error message to issue. */
2943 if (TREE_CODE (decl) == FUNCTION_DECL)
2944 msg = "default template arguments may not be used in function templates";
2945 else if (is_partial)
2946 msg = "default template arguments may not be used in partial specializations";
2948 msg = "default argument for template parameter for class enclosing %qD";
2950 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2951 /* If we're inside a class definition, there's no need to
2952 examine the parameters to the class itself. On the one
2953 hand, they will be checked when the class is defined, and,
2954 on the other, default arguments are valid in things like:
2955 template <class T = double>
2956 struct S { template <class U> void f(U); };
2957 Here the default argument for `S' has no bearing on the
2958 declaration of `f'. */
2959 last_level_to_check = template_class_depth (current_class_type) + 1;
2961 /* Check everything. */
2962 last_level_to_check = 0;
2964 for (parm_level = parms;
2965 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2966 parm_level = TREE_CHAIN (parm_level))
2968 tree inner_parms = TREE_VALUE (parm_level);
2972 ntparms = TREE_VEC_LENGTH (inner_parms);
2973 for (i = 0; i < ntparms; ++i)
2975 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
2978 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2986 /* Clear out the default argument so that we are not
2988 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2992 /* At this point, if we're still interested in issuing messages,
2993 they must apply to classes surrounding the object declared. */
2995 msg = "default argument for template parameter for class enclosing %qD";
2999 /* Worker for push_template_decl_real, called via
3000 for_each_template_parm. DATA is really an int, indicating the
3001 level of the parameters we are interested in. If T is a template
3002 parameter of that level, return nonzero. */
3005 template_parm_this_level_p (tree t, void* data)
3007 int this_level = *(int *)data;
3010 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3011 level = TEMPLATE_PARM_LEVEL (t);
3013 level = TEMPLATE_TYPE_LEVEL (t);
3014 return level == this_level;
3017 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3018 parameters given by current_template_args, or reuses a
3019 previously existing one, if appropriate. Returns the DECL, or an
3020 equivalent one, if it is replaced via a call to duplicate_decls.
3022 If IS_FRIEND is true, DECL is a friend declaration. */
3025 push_template_decl_real (tree decl, bool is_friend)
3033 int new_template_p = 0;
3034 /* True if the template is a member template, in the sense of
3036 bool member_template_p = false;
3038 if (decl == error_mark_node)
3041 /* See if this is a partial specialization. */
3042 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
3043 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3044 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
3046 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3050 /* For a friend, we want the context of the friend function, not
3051 the type of which it is a friend. */
3052 ctx = DECL_CONTEXT (decl);
3053 else if (CP_DECL_CONTEXT (decl)
3054 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
3055 /* In the case of a virtual function, we want the class in which
3057 ctx = CP_DECL_CONTEXT (decl);
3059 /* Otherwise, if we're currently defining some class, the DECL
3060 is assumed to be a member of the class. */
3061 ctx = current_scope ();
3063 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3066 if (!DECL_CONTEXT (decl))
3067 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
3069 /* See if this is a primary template. */
3070 if (is_friend && ctx)
3071 /* A friend template that specifies a class context, i.e.
3072 template <typename T> friend void A<T>::f();
3076 primary = template_parm_scope_p ();
3080 if (DECL_CLASS_SCOPE_P (decl))
3081 member_template_p = true;
3082 if (TREE_CODE (decl) == TYPE_DECL
3083 && ANON_AGGRNAME_P (DECL_NAME (decl)))
3084 error ("template class without a name");
3085 else if (TREE_CODE (decl) == FUNCTION_DECL)
3087 if (DECL_DESTRUCTOR_P (decl))
3091 A destructor shall not be a member template. */
3092 error ("destructor %qD declared as member template", decl);
3093 return error_mark_node;
3095 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3096 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3097 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3098 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3099 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3100 == void_list_node)))
3102 /* [basic.stc.dynamic.allocation]
3104 An allocation function can be a function
3105 template. ... Template allocation functions shall
3106 have two or more parameters. */
3107 error ("invalid template declaration of %qD", decl);
3108 return error_mark_node;
3111 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3112 && CLASS_TYPE_P (TREE_TYPE (decl)))
3116 error ("template declaration of %q#D", decl);
3117 return error_mark_node;
3121 /* Check to see that the rules regarding the use of default
3122 arguments are not being violated. */
3123 check_default_tmpl_args (decl, current_template_parms,
3124 primary, is_partial);
3127 return process_partial_specialization (decl);
3129 args = current_template_args ();
3132 || TREE_CODE (ctx) == FUNCTION_DECL
3133 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
3134 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
3136 if (DECL_LANG_SPECIFIC (decl)
3137 && DECL_TEMPLATE_INFO (decl)
3138 && DECL_TI_TEMPLATE (decl))
3139 tmpl = DECL_TI_TEMPLATE (decl);
3140 /* If DECL is a TYPE_DECL for a class-template, then there won't
3141 be DECL_LANG_SPECIFIC. The information equivalent to
3142 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3143 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3144 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3145 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3147 /* Since a template declaration already existed for this
3148 class-type, we must be redeclaring it here. Make sure
3149 that the redeclaration is valid. */
3150 redeclare_class_template (TREE_TYPE (decl),
3151 current_template_parms);
3152 /* We don't need to create a new TEMPLATE_DECL; just use the
3153 one we already had. */
3154 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3158 tmpl = build_template_decl (decl, current_template_parms,
3162 if (DECL_LANG_SPECIFIC (decl)
3163 && DECL_TEMPLATE_SPECIALIZATION (decl))
3165 /* A specialization of a member template of a template
3167 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3168 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3169 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3175 tree a, t, current, parms;
3178 if (TREE_CODE (decl) == TYPE_DECL)
3180 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3181 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3182 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3183 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3184 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3187 error ("%qD does not declare a template type", decl);
3191 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
3193 error ("template definition of non-template %q#D", decl);
3197 tmpl = DECL_TI_TEMPLATE (decl);
3199 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3200 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
3201 && DECL_TEMPLATE_SPECIALIZATION (decl)
3202 && DECL_MEMBER_TEMPLATE_P (tmpl))
3206 /* The declaration is a specialization of a member
3207 template, declared outside the class. Therefore, the
3208 innermost template arguments will be NULL, so we
3209 replace them with the arguments determined by the
3210 earlier call to check_explicit_specialization. */
3211 args = DECL_TI_ARGS (decl);
3214 = build_template_decl (decl, current_template_parms,
3216 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3217 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3218 DECL_TI_TEMPLATE (decl) = new_tmpl;
3219 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
3220 DECL_TEMPLATE_INFO (new_tmpl)
3221 = tree_cons (tmpl, args, NULL_TREE);
3223 register_specialization (new_tmpl,
3224 most_general_template (tmpl),
3230 /* Make sure the template headers we got make sense. */
3232 parms = DECL_TEMPLATE_PARMS (tmpl);
3233 i = TMPL_PARMS_DEPTH (parms);
3234 if (TMPL_ARGS_DEPTH (args) != i)
3236 error ("expected %d levels of template parms for %q#D, got %d",
3237 i, decl, TMPL_ARGS_DEPTH (args));
3240 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3242 a = TMPL_ARGS_LEVEL (args, i);
3243 t = INNERMOST_TEMPLATE_PARMS (parms);
3245 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3247 if (current == decl)
3248 error ("got %d template parameters for %q#D",
3249 TREE_VEC_LENGTH (a), decl);
3251 error ("got %d template parameters for %q#T",
3252 TREE_VEC_LENGTH (a), current);
3253 error (" but %d required", TREE_VEC_LENGTH (t));
3254 return error_mark_node;
3257 /* Perhaps we should also check that the parms are used in the
3258 appropriate qualifying scopes in the declarator? */
3260 if (current == decl)
3263 current = TYPE_CONTEXT (current);
3267 DECL_TEMPLATE_RESULT (tmpl) = decl;
3268 TREE_TYPE (tmpl) = TREE_TYPE (decl);
3270 /* Push template declarations for global functions and types. Note
3271 that we do not try to push a global template friend declared in a
3272 template class; such a thing may well depend on the template
3273 parameters of the class. */
3274 if (new_template_p && !ctx
3275 && !(is_friend && template_class_depth (current_class_type) > 0))
3277 tmpl = pushdecl_namespace_level (tmpl, is_friend);
3278 if (tmpl == error_mark_node)
3279 return error_mark_node;
3281 /* Hide template friend classes that haven't been declared yet. */
3282 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3284 DECL_ANTICIPATED (tmpl) = 1;
3285 DECL_FRIEND_P (tmpl) = 1;
3291 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3292 if (DECL_CONV_FN_P (tmpl))
3294 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3296 /* It is a conversion operator. See if the type converted to
3297 depends on innermost template operands. */
3299 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3301 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3305 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3306 back to its most general template. If TMPL is a specialization,
3307 ARGS may only have the innermost set of arguments. Add the missing
3308 argument levels if necessary. */
3309 if (DECL_TEMPLATE_INFO (tmpl))
3310 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3312 info = tree_cons (tmpl, args, NULL_TREE);
3314 if (DECL_IMPLICIT_TYPEDEF_P (decl))
3316 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3317 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
3318 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3319 /* Don't change the name if we've already set it up. */
3320 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
3321 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
3323 else if (DECL_LANG_SPECIFIC (decl))
3324 DECL_TEMPLATE_INFO (decl) = info;
3326 return DECL_TEMPLATE_RESULT (tmpl);
3330 push_template_decl (tree decl)
3332 return push_template_decl_real (decl, false);
3335 /* Called when a class template TYPE is redeclared with the indicated
3336 template PARMS, e.g.:
3338 template <class T> struct S;
3339 template <class T> struct S {}; */
3342 redeclare_class_template (tree type, tree parms)
3348 if (!TYPE_TEMPLATE_INFO (type))
3350 error ("%qT is not a template type", type);
3354 tmpl = TYPE_TI_TEMPLATE (type);
3355 if (!PRIMARY_TEMPLATE_P (tmpl))
3356 /* The type is nested in some template class. Nothing to worry
3357 about here; there are no new template parameters for the nested
3363 error ("template specifiers not specified in declaration of %qD",
3368 parms = INNERMOST_TEMPLATE_PARMS (parms);
3369 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3371 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3373 error ("previous declaration %q+D", tmpl);
3374 error ("used %d template parameter(s) instead of %d",
3375 TREE_VEC_LENGTH (tmpl_parms),
3376 TREE_VEC_LENGTH (parms));
3380 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3387 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
3388 || TREE_VEC_ELT (parms, i) == error_mark_node)
3391 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3392 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3393 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3394 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
3396 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3398 if (tmpl_parm != error_mark_node
3399 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3400 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3401 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
3403 error ("template parameter %q+#D", tmpl_parm);
3404 error ("redeclared here as %q#D", parm);
3408 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3410 /* We have in [temp.param]:
3412 A template-parameter may not be given default arguments
3413 by two different declarations in the same scope. */
3414 error ("redefinition of default argument for %q#D", parm);
3415 error ("%J original definition appeared here", tmpl_parm);
3419 if (parm_default != NULL_TREE)
3420 /* Update the previous template parameters (which are the ones
3421 that will really count) with the new default value. */
3422 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
3423 else if (tmpl_default != NULL_TREE)
3424 /* Update the new parameters, too; they'll be used as the
3425 parameters for any members. */
3426 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
3432 /* Simplify EXPR if it is a non-dependent expression. Returns the
3433 (possibly simplified) expression. */
3436 fold_non_dependent_expr (tree expr)
3438 if (expr == NULL_TREE)
3441 /* If we're in a template, but EXPR isn't value dependent, simplify
3442 it. We're supposed to treat:
3444 template <typename T> void f(T[1 + 1]);
3445 template <typename T> void f(T[2]);
3447 as two declarations of the same function, for example. */
3448 if (processing_template_decl
3449 && !type_dependent_expression_p (expr)
3450 && !value_dependent_expression_p (expr))
3452 HOST_WIDE_INT saved_processing_template_decl;
3454 saved_processing_template_decl = processing_template_decl;
3455 processing_template_decl = 0;
3456 expr = tsubst_copy_and_build (expr,
3459 /*in_decl=*/NULL_TREE,
3460 /*function_p=*/false,
3461 /*integral_constant_expression_p=*/true);
3462 processing_template_decl = saved_processing_template_decl;
3467 /* EXPR is an expression which is used in a constant-expression context.
3468 For instance, it could be a VAR_DECL with a constant initializer.
3469 Extract the innest constant expression.
3471 This is basically a more powerful version of
3472 integral_constant_value, which can be used also in templates where
3473 initializers can maintain a syntactic rather than semantic form
3474 (even if they are non-dependent, for access-checking purposes). */
3477 fold_decl_constant_value (tree expr)
3479 tree const_expr = expr;
3482 expr = fold_non_dependent_expr (const_expr);
3483 const_expr = integral_constant_value (expr);
3485 while (expr != const_expr);
3490 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3491 must be a function or a pointer-to-function type, as specified
3492 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3493 and check that the resulting function has external linkage. */
3496 convert_nontype_argument_function (tree type, tree expr)
3501 fn = instantiate_type (type, fns, tf_none);
3502 if (fn == error_mark_node)
3503 return error_mark_node;
3506 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3507 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
3508 if (TREE_CODE (fn_no_ptr) == BASELINK)
3509 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
3511 /* [temp.arg.nontype]/1
3513 A template-argument for a non-type, non-template template-parameter
3516 -- the address of an object or function with external linkage. */
3517 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3519 error ("%qE is not a valid template argument for type %qT "
3520 "because function %qD has not external linkage",
3521 expr, type, fn_no_ptr);
3528 /* Attempt to convert the non-type template parameter EXPR to the
3529 indicated TYPE. If the conversion is successful, return the
3530 converted value. If the conversion is unsuccessful, return
3531 NULL_TREE if we issued an error message, or error_mark_node if we
3532 did not. We issue error messages for out-and-out bad template
3533 parameters, but not simply because the conversion failed, since we
3534 might be just trying to do argument deduction. Both TYPE and EXPR
3535 must be non-dependent.
3537 The conversion follows the special rules described in
3538 [temp.arg.nontype], and it is much more strict than an implicit
3541 This function is called twice for each template argument (see
3542 lookup_template_class for a more accurate description of this
3543 problem). This means that we need to handle expressions which
3544 are not valid in a C++ source, but can be created from the
3545 first call (for instance, casts to perform conversions). These
3546 hacks can go away after we fix the double coercion problem. */
3549 convert_nontype_argument (tree type, tree expr)
3553 /* Detect immediately string literals as invalid non-type argument.
3554 This special-case is not needed for correctness (we would easily
3555 catch this later), but only to provide better diagnostic for this
3556 common user mistake. As suggested by DR 100, we do not mention
3557 linkage issues in the diagnostic as this is not the point. */
3558 if (TREE_CODE (expr) == STRING_CST)
3560 error ("%qE is not a valid template argument for type %qT "
3561 "because string literals can never be used in this context",
3566 /* If we are in a template, EXPR may be non-dependent, but still
3567 have a syntactic, rather than semantic, form. For example, EXPR
3568 might be a SCOPE_REF, rather than the VAR_DECL to which the
3569 SCOPE_REF refers. Preserving the qualifying scope is necessary
3570 so that access checking can be performed when the template is
3571 instantiated -- but here we need the resolved form so that we can
3572 convert the argument. */
3573 expr = fold_non_dependent_expr (expr);
3574 if (error_operand_p (expr))
3575 return error_mark_node;
3576 expr_type = TREE_TYPE (expr);
3578 /* HACK: Due to double coercion, we can get a
3579 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3580 which is the tree that we built on the first call (see
3581 below when coercing to reference to object or to reference to
3582 function). We just strip everything and get to the arg.
3583 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3585 if (TREE_CODE (expr) == NOP_EXPR)
3587 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
3589 /* ??? Maybe we could use convert_from_reference here, but we
3590 would need to relax its constraints because the NOP_EXPR
3591 could actually change the type to something more cv-qualified,
3592 and this is not folded by convert_from_reference. */
3593 tree addr = TREE_OPERAND (expr, 0);
3594 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3595 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3596 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3597 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3598 (TREE_TYPE (expr_type),
3599 TREE_TYPE (TREE_TYPE (addr))));
3601 expr = TREE_OPERAND (addr, 0);
3602 expr_type = TREE_TYPE (expr);
3605 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3606 parameter is a pointer to object, through decay and
3607 qualification conversion. Let's strip everything. */
3608 else if (TYPE_PTROBV_P (type))
3611 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3612 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3613 /* Skip the ADDR_EXPR only if it is part of the decay for
3614 an array. Otherwise, it is part of the original argument
3615 in the source code. */
3616 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3617 expr = TREE_OPERAND (expr, 0);
3618 expr_type = TREE_TYPE (expr);
3622 /* [temp.arg.nontype]/5, bullet 1
3624 For a non-type template-parameter of integral or enumeration type,
3625 integral promotions (_conv.prom_) and integral conversions
3626 (_conv.integral_) are applied. */
3627 if (INTEGRAL_TYPE_P (type))
3629 if (!INTEGRAL_TYPE_P (expr_type))
3630 return error_mark_node;
3632 expr = fold_decl_constant_value (expr);
3633 /* Notice that there are constant expressions like '4 % 0' which
3634 do not fold into integer constants. */
3635 if (TREE_CODE (expr) != INTEGER_CST)
3637 error ("%qE is not a valid template argument for type %qT "
3638 "because it is a non-constant expression", expr, type);
3642 /* At this point, an implicit conversion does what we want,
3643 because we already know that the expression is of integral
3645 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3646 if (expr == error_mark_node)
3647 return error_mark_node;
3649 /* Conversion was allowed: fold it to a bare integer constant. */
3652 /* [temp.arg.nontype]/5, bullet 2
3654 For a non-type template-parameter of type pointer to object,
3655 qualification conversions (_conv.qual_) and the array-to-pointer
3656 conversion (_conv.array_) are applied. */
3657 else if (TYPE_PTROBV_P (type))
3659 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
3661 A template-argument for a non-type, non-template template-parameter
3662 shall be one of: [...]
3664 -- the name of a non-type template-parameter;
3665 -- the address of an object or function with external linkage, [...]
3666 expressed as "& id-expression" where the & is optional if the name
3667 refers to a function or array, or if the corresponding
3668 template-parameter is a reference.
3670 Here, we do not care about functions, as they are invalid anyway
3671 for a parameter of type pointer-to-object. */
3673 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
3674 /* Non-type template parameters are OK. */
3676 else if (TREE_CODE (expr) != ADDR_EXPR
3677 && TREE_CODE (expr_type) != ARRAY_TYPE)
3679 if (TREE_CODE (expr) == VAR_DECL)
3681 error ("%qD is not a valid template argument "
3682 "because %qD is a variable, not the address of "
3687 /* Other values, like integer constants, might be valid
3688 non-type arguments of some other type. */
3689 return error_mark_node;
3695 decl = ((TREE_CODE (expr) == ADDR_EXPR)
3696 ? TREE_OPERAND (expr, 0) : expr);
3697 if (TREE_CODE (decl) != VAR_DECL)
3699 error ("%qE is not a valid template argument of type %qT "
3700 "because %qE is not a variable",
3704 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
3706 error ("%qE is not a valid template argument of type %qT "
3707 "because %qD does not have external linkage",
3713 expr = decay_conversion (expr);
3714 if (expr == error_mark_node)
3715 return error_mark_node;
3717 expr = perform_qualification_conversions (type, expr);
3718 if (expr == error_mark_node)
3719 return error_mark_node;
3721 /* [temp.arg.nontype]/5, bullet 3
3723 For a non-type template-parameter of type reference to object, no
3724 conversions apply. The type referred to by the reference may be more
3725 cv-qualified than the (otherwise identical) type of the
3726 template-argument. The template-parameter is bound directly to the
3727 template-argument, which must be an lvalue. */
3728 else if (TYPE_REF_OBJ_P (type))
3730 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3732 return error_mark_node;
3734 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3736 error ("%qE is not a valid template argument for type %qT "
3737 "because of conflicts in cv-qualification", expr, type);
3741 if (!real_lvalue_p (expr))
3743 error ("%qE is not a valid template argument for type %qT "
3744 "because it is not an lvalue", expr, type);
3748 /* [temp.arg.nontype]/1
3750 A template-argument for a non-type, non-template template-parameter
3751 shall be one of: [...]
3753 -- the address of an object or function with external linkage. */
3754 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3756 error ("%qE is not a valid template argument for type %qT "
3757 "because object %qD has not external linkage",
3762 expr = build_nop (type, build_address (expr));
3764 /* [temp.arg.nontype]/5, bullet 4
3766 For a non-type template-parameter of type pointer to function, only
3767 the function-to-pointer conversion (_conv.func_) is applied. If the
3768 template-argument represents a set of overloaded functions (or a
3769 pointer to such), the matching function is selected from the set
3771 else if (TYPE_PTRFN_P (type))
3773 /* If the argument is a template-id, we might not have enough
3774 context information to decay the pointer. */
3775 if (!type_unknown_p (expr_type))
3777 expr = decay_conversion (expr);
3778 if (expr == error_mark_node)
3779 return error_mark_node;
3782 expr = convert_nontype_argument_function (type, expr);
3783 if (!expr || expr == error_mark_node)
3786 /* [temp.arg.nontype]/5, bullet 5
3788 For a non-type template-parameter of type reference to function, no
3789 conversions apply. If the template-argument represents a set of
3790 overloaded functions, the matching function is selected from the set
3792 else if (TYPE_REFFN_P (type))
3794 if (TREE_CODE (expr) == ADDR_EXPR)
3796 error ("%qE is not a valid template argument for type %qT "
3797 "because it is a pointer", expr, type);
3798 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3802 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3803 if (!expr || expr == error_mark_node)
3806 expr = build_nop (type, build_address (expr));
3808 /* [temp.arg.nontype]/5, bullet 6
3810 For a non-type template-parameter of type pointer to member function,
3811 no conversions apply. If the template-argument represents a set of
3812 overloaded member functions, the matching member function is selected
3813 from the set (_over.over_). */
3814 else if (TYPE_PTRMEMFUNC_P (type))
3816 expr = instantiate_type (type, expr, tf_none);
3817 if (expr == error_mark_node)
3818 return error_mark_node;
3820 /* There is no way to disable standard conversions in
3821 resolve_address_of_overloaded_function (called by
3822 instantiate_type). It is possible that the call succeeded by
3823 converting &B::I to &D::I (where B is a base of D), so we need
3824 to reject this conversion here.
3826 Actually, even if there was a way to disable standard conversions,
3827 it would still be better to reject them here so that we can
3828 provide a superior diagnostic. */
3829 if (!same_type_p (TREE_TYPE (expr), type))
3831 /* Make sure we are just one standard conversion off. */
3832 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3833 error ("%qE is not a valid template argument for type %qT "
3834 "because it is of type %qT", expr, type,
3836 inform ("standard conversions are not allowed in this context");
3840 /* [temp.arg.nontype]/5, bullet 7
3842 For a non-type template-parameter of type pointer to data member,
3843 qualification conversions (_conv.qual_) are applied. */
3844 else if (TYPE_PTRMEM_P (type))
3846 expr = perform_qualification_conversions (type, expr);
3847 if (expr == error_mark_node)
3850 /* A template non-type parameter must be one of the above. */
3854 /* Sanity check: did we actually convert the argument to the
3856 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3861 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3862 template template parameters. Both PARM_PARMS and ARG_PARMS are
3863 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3866 Consider the example:
3867 template <class T> class A;
3868 template<template <class U> class TT> class B;
3870 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3871 the parameters to A, and OUTER_ARGS contains A. */
3874 coerce_template_template_parms (tree parm_parms,
3876 tsubst_flags_t complain,
3880 int nparms, nargs, i;
3883 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3884 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
3886 nparms = TREE_VEC_LENGTH (parm_parms);
3887 nargs = TREE_VEC_LENGTH (arg_parms);
3889 if (nargs != nparms)
3892 for (i = 0; i < nparms; ++i)
3894 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
3895 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
3898 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3899 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3901 if (arg == NULL_TREE || arg == error_mark_node
3902 || parm == NULL_TREE || parm == error_mark_node)
3905 if (TREE_CODE (arg) != TREE_CODE (parm))
3908 switch (TREE_CODE (parm))
3914 /* We encounter instantiations of templates like
3915 template <template <template <class> class> class TT>
3918 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3919 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3921 if (!coerce_template_template_parms
3922 (parmparm, argparm, complain, in_decl, outer_args))
3928 /* The tsubst call is used to handle cases such as
3930 template <int> class C {};
3931 template <class T, template <T> class TT> class D {};
3934 i.e. the parameter list of TT depends on earlier parameters. */
3935 if (!dependent_type_p (TREE_TYPE (arg))
3937 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3949 /* Convert the indicated template ARG as necessary to match the
3950 indicated template PARM. Returns the converted ARG, or
3951 error_mark_node if the conversion was unsuccessful. Error and
3952 warning messages are issued under control of COMPLAIN. This
3953 conversion is for the Ith parameter in the parameter list. ARGS is
3954 the full set of template arguments deduced so far. */
3957 convert_template_argument (tree parm,
3960 tsubst_flags_t complain,
3965 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3967 if (TREE_CODE (arg) == TREE_LIST
3968 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
3970 /* The template argument was the name of some
3971 member function. That's usually
3972 invalid, but static members are OK. In any
3973 case, grab the underlying fields/functions
3974 and issue an error later if required. */
3975 arg = TREE_VALUE (arg);
3976 TREE_TYPE (arg) = unknown_type_node;
3979 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3980 requires_type = (TREE_CODE (parm) == TYPE_DECL
3981 || requires_tmpl_type);
3983 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3984 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3985 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3986 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
3989 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3990 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
3991 arg = TYPE_STUB_DECL (arg);
3993 is_type = TYPE_P (arg) || is_tmpl_type;
3995 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3996 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3998 pedwarn ("to refer to a type member of a template parameter, "
3999 "use %<typename %E%>", arg);
4001 arg = make_typename_type (TREE_OPERAND (arg, 0),
4002 TREE_OPERAND (arg, 1),
4004 complain & tf_error);
4007 if (is_type != requires_type)
4011 if (complain & tf_error)
4013 error ("type/value mismatch at argument %d in template "
4014 "parameter list for %qD",
4017 error (" expected a constant of type %qT, got %qT",
4019 (is_tmpl_type ? DECL_NAME (arg) : arg));
4020 else if (requires_tmpl_type)
4021 error (" expected a class template, got %qE", arg);
4023 error (" expected a type, got %qE", arg);
4026 return error_mark_node;
4028 if (is_tmpl_type ^ requires_tmpl_type)
4030 if (in_decl && (complain & tf_error))
4032 error ("type/value mismatch at argument %d in template "
4033 "parameter list for %qD",
4036 error (" expected a type, got %qT", DECL_NAME (arg));
4038 error (" expected a class template, got %qT", arg);
4040 return error_mark_node;
4045 if (requires_tmpl_type)
4047 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
4048 /* The number of argument required is not known yet.
4049 Just accept it for now. */
4050 val = TREE_TYPE (arg);
4053 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4054 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4056 if (coerce_template_template_parms (parmparm, argparm,
4062 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4064 if (val != error_mark_node
4065 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
4066 val = TREE_TYPE (val);
4070 if (in_decl && (complain & tf_error))
4072 error ("type/value mismatch at argument %d in "
4073 "template parameter list for %qD",
4075 error (" expected a template of type %qD, got %qD",
4079 val = error_mark_node;
4085 /* We only form one instance of each template specialization.
4086 Therefore, if we use a non-canonical variant (i.e., a
4087 typedef), any future messages referring to the type will use
4088 the typedef, which is confusing if those future uses do not
4089 themselves also use the typedef. */
4091 val = canonical_type_variant (val);
4095 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
4097 if (invalid_nontype_parm_type_p (t, complain))
4098 return error_mark_node;
4100 if (!uses_template_parms (arg) && !uses_template_parms (t))
4101 /* We used to call digest_init here. However, digest_init
4102 will report errors, which we don't want when complain
4103 is zero. More importantly, digest_init will try too
4104 hard to convert things: for example, `0' should not be
4105 converted to pointer type at this point according to
4106 the standard. Accepting this is not merely an
4107 extension, since deciding whether or not these
4108 conversions can occur is part of determining which
4109 function template to call, or whether a given explicit
4110 argument specification is valid. */
4111 val = convert_nontype_argument (t, arg);
4115 if (val == NULL_TREE)
4116 val = error_mark_node;
4117 else if (val == error_mark_node && (complain & tf_error))
4118 error ("could not convert template argument %qE to %qT", arg, t);
4124 /* Convert all template arguments to their appropriate types, and
4125 return a vector containing the innermost resulting template
4126 arguments. If any error occurs, return error_mark_node. Error and
4127 warning messages are issued under control of COMPLAIN.
4129 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4130 for arguments not specified in ARGS. Otherwise, if
4131 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4132 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4133 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4137 coerce_template_parms (tree parms,
4140 tsubst_flags_t complain,
4141 bool require_all_args,
4142 bool use_default_args)
4144 int nparms, nargs, i, lost = 0;
4147 tree new_inner_args;
4148 bool saved_skip_evaluation;
4150 inner_args = INNERMOST_TEMPLATE_ARGS (args);
4151 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
4152 nparms = TREE_VEC_LENGTH (parms);
4157 && (!use_default_args
4158 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
4159 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
4161 if (complain & tf_error)
4163 error ("wrong number of template arguments (%d, should be %d)",
4167 error ("provided for %q+D", in_decl);
4170 return error_mark_node;
4173 /* We need to evaluate the template arguments, even though this
4174 template-id may be nested within a "sizeof". */
4175 saved_skip_evaluation = skip_evaluation;
4176 skip_evaluation = false;
4177 new_inner_args = make_tree_vec (nparms);
4178 new_args = add_outermost_template_args (args, new_inner_args);
4179 for (i = 0; i < nparms; i++)
4184 /* Get the Ith template parameter. */
4185 parm = TREE_VEC_ELT (parms, i);
4187 if (parm == error_mark_node)
4189 TREE_VEC_ELT (new_inner_args, i) = error_mark_node;
4193 /* Calculate the Ith argument. */
4195 arg = TREE_VEC_ELT (inner_args, i);
4196 else if (require_all_args)
4197 /* There must be a default arg in this case. */
4198 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4204 if (arg == error_mark_node)
4206 if (complain & tf_error)
4207 error ("template argument %d is invalid", i + 1);
4210 arg = convert_template_argument (TREE_VALUE (parm),
4211 arg, new_args, complain, i,
4214 if (arg == error_mark_node)
4216 TREE_VEC_ELT (new_inner_args, i) = arg;
4218 skip_evaluation = saved_skip_evaluation;
4221 return error_mark_node;
4223 return new_inner_args;
4226 /* Returns 1 if template args OT and NT are equivalent. */
4229 template_args_equal (tree ot, tree nt)
4234 if (TREE_CODE (nt) == TREE_VEC)
4235 /* For member templates */
4236 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4237 else if (TYPE_P (nt))
4238 return TYPE_P (ot) && same_type_p (ot, nt);
4239 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4242 return cp_tree_equal (ot, nt);
4245 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
4246 of template arguments. Returns 0 otherwise. */
4249 comp_template_args (tree oldargs, tree newargs)
4253 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4256 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4258 tree nt = TREE_VEC_ELT (newargs, i);
4259 tree ot = TREE_VEC_ELT (oldargs, i);
4261 if (! template_args_equal (ot, nt))
4267 /* Given class template name and parameter list, produce a user-friendly name
4268 for the instantiation. */
4271 mangle_class_name_for_template (const char* name, tree parms, tree arglist)
4273 static struct obstack scratch_obstack;
4274 static char *scratch_firstobj;
4277 if (!scratch_firstobj)
4278 gcc_obstack_init (&scratch_obstack);
4280 obstack_free (&scratch_obstack, scratch_firstobj);
4281 scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1);
4283 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4284 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4288 nparms = TREE_VEC_LENGTH (parms);
4289 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
4290 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
4291 for (i = 0; i < nparms; i++)
4296 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4297 arg = TREE_VEC_ELT (arglist, i);
4299 if (parm == error_mark_node)
4305 if (TREE_CODE (parm) == TYPE_DECL)
4307 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4310 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4312 if (TREE_CODE (arg) == TEMPLATE_DECL)
4314 /* Already substituted with real template. Just output
4315 the template name here */
4316 tree context = DECL_CONTEXT (arg);
4319 /* The template may be defined in a namespace, or
4320 may be a member template. */
4321 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
4322 || CLASS_TYPE_P (context));
4323 cat (decl_as_string (DECL_CONTEXT (arg),
4324 TFF_PLAIN_IDENTIFIER));
4327 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4330 /* Output the parameter declaration. */
4331 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
4335 gcc_assert (TREE_CODE (parm) == PARM_DECL);
4337 /* No need to check arglist against parmlist here; we did that
4338 in coerce_template_parms, called from lookup_template_class. */
4339 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
4342 char *bufp = obstack_next_free (&scratch_obstack);
4344 while (bufp[offset - 1] == ' ')
4346 obstack_blank_fast (&scratch_obstack, offset);
4348 /* B<C<char> >, not B<C<char>> */
4349 if (bufp[offset - 1] == '>')
4354 return (char *) obstack_base (&scratch_obstack);
4358 classtype_mangled_name (tree t)
4360 if (CLASSTYPE_TEMPLATE_INFO (t)
4361 /* Specializations have already had their names set up in
4362 lookup_template_class. */
4363 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4365 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4367 /* For non-primary templates, the template parameters are
4368 implicit from their surrounding context. */
4369 if (PRIMARY_TEMPLATE_P (tmpl))
4371 tree name = DECL_NAME (tmpl);
4372 char *mangled_name = mangle_class_name_for_template
4373 (IDENTIFIER_POINTER (name),
4374 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4375 CLASSTYPE_TI_ARGS (t));
4376 tree id = get_identifier (mangled_name);
4377 IDENTIFIER_TEMPLATE (id) = name;
4382 return TYPE_IDENTIFIER (t);
4386 add_pending_template (tree d)
4388 tree ti = (TYPE_P (d)
4389 ? CLASSTYPE_TEMPLATE_INFO (d)
4390 : DECL_TEMPLATE_INFO (d));
4394 if (TI_PENDING_TEMPLATE_FLAG (ti))
4397 /* We are called both from instantiate_decl, where we've already had a
4398 tinst_level pushed, and instantiate_template, where we haven't.
4400 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4403 push_tinst_level (d);
4405 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4406 if (last_pending_template)
4407 TREE_CHAIN (last_pending_template) = pt;
4409 pending_templates = pt;
4411 last_pending_template = pt;
4413 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
4420 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4421 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4422 documentation for TEMPLATE_ID_EXPR. */
4425 lookup_template_function (tree fns, tree arglist)
4429 if (fns == error_mark_node || arglist == error_mark_node)
4430 return error_mark_node;
4432 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4433 gcc_assert (fns && (is_overloaded_fn (fns)
4434 || TREE_CODE (fns) == IDENTIFIER_NODE));
4436 if (BASELINK_P (fns))
4438 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4440 BASELINK_FUNCTIONS (fns),
4445 type = TREE_TYPE (fns);
4446 if (TREE_CODE (fns) == OVERLOAD || !type)
4447 type = unknown_type_node;
4449 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
4452 /* Within the scope of a template class S<T>, the name S gets bound
4453 (in build_self_reference) to a TYPE_DECL for the class, not a
4454 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4455 or one of its enclosing classes, and that type is a template,
4456 return the associated TEMPLATE_DECL. Otherwise, the original
4457 DECL is returned. */
4460 maybe_get_template_decl_from_type_decl (tree decl)
4462 return (decl != NULL_TREE
4463 && TREE_CODE (decl) == TYPE_DECL
4464 && DECL_ARTIFICIAL (decl)
4465 && CLASS_TYPE_P (TREE_TYPE (decl))
4466 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
4467 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4470 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4471 parameters, find the desired type.
4473 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4475 IN_DECL, if non-NULL, is the template declaration we are trying to
4478 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4479 the class we are looking up.
4481 Issue error and warning messages under control of COMPLAIN.
4483 If the template class is really a local class in a template
4484 function, then the FUNCTION_CONTEXT is the function in which it is
4487 ??? Note that this function is currently called *twice* for each
4488 template-id: the first time from the parser, while creating the
4489 incomplete type (finish_template_type), and the second type during the
4490 real instantiation (instantiate_template_class). This is surely something
4491 that we want to avoid. It also causes some problems with argument
4492 coercion (see convert_nontype_argument for more information on this). */
4495 lookup_template_class (tree d1,
4500 tsubst_flags_t complain)
4502 tree template = NULL_TREE, parmlist;
4505 timevar_push (TV_NAME_LOOKUP);
4507 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4509 tree value = innermost_non_namespace_value (d1);
4510 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4515 push_decl_namespace (context);
4516 template = lookup_name (d1);
4517 template = maybe_get_template_decl_from_type_decl (template);
4519 pop_decl_namespace ();
4522 context = DECL_CONTEXT (template);
4524 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4526 tree type = TREE_TYPE (d1);
4528 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4529 an implicit typename for the second A. Deal with it. */
4530 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4531 type = TREE_TYPE (type);
4533 if (CLASSTYPE_TEMPLATE_INFO (type))
4535 template = CLASSTYPE_TI_TEMPLATE (type);
4536 d1 = DECL_NAME (template);
4539 else if (TREE_CODE (d1) == ENUMERAL_TYPE
4540 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
4542 template = TYPE_TI_TEMPLATE (d1);
4543 d1 = DECL_NAME (template);
4545 else if (TREE_CODE (d1) == TEMPLATE_DECL
4546 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
4549 d1 = DECL_NAME (template);
4550 context = DECL_CONTEXT (template);
4553 /* Issue an error message if we didn't find a template. */
4556 if (complain & tf_error)
4557 error ("%qT is not a template", d1);
4558 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4561 if (TREE_CODE (template) != TEMPLATE_DECL
4562 /* Make sure it's a user visible template, if it was named by
4564 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4565 && !PRIMARY_TEMPLATE_P (template)))
4567 if (complain & tf_error)
4569 error ("non-template type %qT used as a template", d1);
4571 error ("for template declaration %q+D", in_decl);
4573 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4576 complain &= ~tf_user;
4578 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4580 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4581 template arguments */
4586 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4588 /* Consider an example where a template template parameter declared as
4590 template <class T, class U = std::allocator<T> > class TT
4592 The template parameter level of T and U are one level larger than
4593 of TT. To proper process the default argument of U, say when an
4594 instantiation `TT<int>' is seen, we need to build the full
4595 arguments containing {int} as the innermost level. Outer levels,
4596 available when not appearing as default template argument, can be
4597 obtained from `current_template_args ()'.
4599 Suppose that TT is later substituted with std::vector. The above
4600 instantiation is `TT<int, std::allocator<T> >' with TT at
4601 level 1, and T at level 2, while the template arguments at level 1
4602 becomes {std::vector} and the inner level 2 is {int}. */
4604 if (current_template_parms)
4605 arglist = add_to_template_args (current_template_args (), arglist);
4607 arglist2 = coerce_template_parms (parmlist, arglist, template,
4609 /*require_all_args=*/true,
4610 /*use_default_args=*/true);
4611 if (arglist2 == error_mark_node
4612 || (!uses_template_parms (arglist2)
4613 && check_instantiated_args (template, arglist2, complain)))
4614 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4616 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
4617 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
4621 tree template_type = TREE_TYPE (template);
4624 tree found = NULL_TREE;
4627 int is_partial_instantiation;
4629 gen_tmpl = most_general_template (template);
4630 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
4631 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4632 arg_depth = TMPL_ARGS_DEPTH (arglist);
4634 if (arg_depth == 1 && parm_depth > 1)
4636 /* We've been given an incomplete set of template arguments.
4639 template <class T> struct S1 {
4640 template <class U> struct S2 {};
4641 template <class U> struct S2<U*> {};
4644 we will be called with an ARGLIST of `U*', but the
4645 TEMPLATE will be `template <class T> template
4646 <class U> struct S1<T>::S2'. We must fill in the missing
4649 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4651 arg_depth = TMPL_ARGS_DEPTH (arglist);
4654 /* Now we should have enough arguments. */
4655 gcc_assert (parm_depth == arg_depth);
4657 /* From here on, we're only interested in the most general
4659 template = gen_tmpl;
4661 /* Calculate the BOUND_ARGS. These will be the args that are
4662 actually tsubst'd into the definition to create the
4666 /* We have multiple levels of arguments to coerce, at once. */
4668 int saved_depth = TMPL_ARGS_DEPTH (arglist);
4670 tree bound_args = make_tree_vec (parm_depth);
4672 for (i = saved_depth,
4673 t = DECL_TEMPLATE_PARMS (template);
4674 i > 0 && t != NULL_TREE;
4675 --i, t = TREE_CHAIN (t))
4677 tree a = coerce_template_parms (TREE_VALUE (t),
4680 /*require_all_args=*/true,
4681 /*use_default_args=*/true);
4683 /* Don't process further if one of the levels fails. */
4684 if (a == error_mark_node)
4686 /* Restore the ARGLIST to its full size. */
4687 TREE_VEC_LENGTH (arglist) = saved_depth;
4688 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4691 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4693 /* We temporarily reduce the length of the ARGLIST so
4694 that coerce_template_parms will see only the arguments
4695 corresponding to the template parameters it is
4697 TREE_VEC_LENGTH (arglist)--;
4700 /* Restore the ARGLIST to its full size. */
4701 TREE_VEC_LENGTH (arglist) = saved_depth;
4703 arglist = bound_args;
4707 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
4708 INNERMOST_TEMPLATE_ARGS (arglist),
4711 /*require_all_args=*/true,
4712 /*use_default_args=*/true);
4714 if (arglist == error_mark_node)
4715 /* We were unable to bind the arguments. */
4716 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4718 /* In the scope of a template class, explicit references to the
4719 template class refer to the type of the template, not any
4720 instantiation of it. For example, in:
4722 template <class T> class C { void f(C<T>); }
4724 the `C<T>' is just the same as `C'. Outside of the
4725 class, however, such a reference is an instantiation. */
4726 if (comp_template_args (TYPE_TI_ARGS (template_type),
4729 found = template_type;
4731 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
4735 for (ctx = current_class_type;
4736 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4738 ? TYPE_CONTEXT (ctx)
4739 : DECL_CONTEXT (ctx)))
4740 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4743 /* We're not in the scope of the class, so the
4744 TEMPLATE_TYPE is not the type we want after all. */
4750 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4752 /* If we already have this specialization, return it. */
4753 found = retrieve_specialization (template, arglist,
4754 /*class_specializations_p=*/false);
4756 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4758 /* This type is a "partial instantiation" if any of the template
4759 arguments still involve template parameters. Note that we set
4760 IS_PARTIAL_INSTANTIATION for partial specializations as
4762 is_partial_instantiation = uses_template_parms (arglist);
4764 /* If the deduced arguments are invalid, then the binding
4766 if (!is_partial_instantiation
4767 && check_instantiated_args (template,
4768 INNERMOST_TEMPLATE_ARGS (arglist),
4770 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4772 if (!is_partial_instantiation
4773 && !PRIMARY_TEMPLATE_P (template)
4774 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4776 found = xref_tag_from_type (TREE_TYPE (template),
4777 DECL_NAME (template),
4778 /*tag_scope=*/ts_global);
4779 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
4782 context = tsubst (DECL_CONTEXT (template), arglist,
4785 context = global_namespace;
4787 /* Create the type. */
4788 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4790 if (!is_partial_instantiation)
4792 set_current_access_from_decl (TYPE_NAME (template_type));
4793 t = start_enum (TYPE_IDENTIFIER (template_type));
4796 /* We don't want to call start_enum for this type, since
4797 the values for the enumeration constants may involve
4798 template parameters. And, no one should be interested
4799 in the enumeration constants for such a type. */
4800 t = make_node (ENUMERAL_TYPE);
4804 t = make_aggr_type (TREE_CODE (template_type));
4805 CLASSTYPE_DECLARED_CLASS (t)
4806 = CLASSTYPE_DECLARED_CLASS (template_type);
4807 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
4808 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
4810 /* A local class. Make sure the decl gets registered properly. */
4811 if (context == current_function_decl)
4812 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
4815 /* If we called start_enum or pushtag above, this information
4816 will already be set up. */
4819 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
4821 type_decl = create_implicit_typedef (DECL_NAME (template), t);
4822 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
4823 TYPE_STUB_DECL (t) = type_decl;
4824 DECL_SOURCE_LOCATION (type_decl)
4825 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
4828 type_decl = TYPE_NAME (t);
4830 TREE_PRIVATE (type_decl)
4831 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4832 TREE_PROTECTED (type_decl)
4833 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
4834 DECL_IN_SYSTEM_HEADER (type_decl)
4835 = DECL_IN_SYSTEM_HEADER (template);
4836 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4838 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4839 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4842 /* Set up the template information. We have to figure out which
4843 template is the immediate parent if this is a full
4845 if (parm_depth == 1 || is_partial_instantiation
4846 || !PRIMARY_TEMPLATE_P (template))
4847 /* This case is easy; there are no member templates involved. */
4851 /* This is a full instantiation of a member template. Look
4852 for a partial instantiation of which this is an instance. */
4854 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4855 found; found = TREE_CHAIN (found))
4858 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4860 /* We only want partial instantiations, here, not
4861 specializations or full instantiations. */
4862 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4863 || !uses_template_parms (TREE_VALUE (found)))
4866 /* Temporarily reduce by one the number of levels in the
4867 ARGLIST and in FOUND so as to avoid comparing the
4868 last set of arguments. */
4869 TREE_VEC_LENGTH (arglist)--;
4870 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4872 /* See if the arguments match. If they do, then TMPL is
4873 the partial instantiation we want. */
4874 success = comp_template_args (TREE_PURPOSE (found), arglist);
4876 /* Restore the argument vectors to their full size. */
4877 TREE_VEC_LENGTH (arglist)++;
4878 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4889 /* There was no partial instantiation. This happens
4890 where C<T> is a member template of A<T> and it's used
4893 template <typename T> struct B { A<T>::C<int> m; };
4896 Create the partial instantiation.
4898 TREE_VEC_LENGTH (arglist)--;
4899 found = tsubst (template, arglist, complain, NULL_TREE);
4900 TREE_VEC_LENGTH (arglist)++;
4904 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4905 DECL_TEMPLATE_INSTANTIATIONS (template)
4906 = tree_cons (arglist, t,
4907 DECL_TEMPLATE_INSTANTIATIONS (template));
4909 if (TREE_CODE (t) == ENUMERAL_TYPE
4910 && !is_partial_instantiation)
4911 /* Now that the type has been registered on the instantiations
4912 list, we set up the enumerators. Because the enumeration
4913 constants may involve the enumeration type itself, we make
4914 sure to register the type first, and then create the
4915 constants. That way, doing tsubst_expr for the enumeration
4916 constants won't result in recursive calls here; we'll find
4917 the instantiation and exit above. */
4918 tsubst_enum (template_type, t, arglist);
4920 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4922 if (TREE_CODE (t) != ENUMERAL_TYPE)
4923 DECL_NAME (type_decl) = classtype_mangled_name (t);
4924 if (is_partial_instantiation)
4925 /* If the type makes use of template parameters, the
4926 code that generates debugging information will crash. */
4927 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
4929 /* Possibly limit visibility based on template args. */
4930 TREE_PUBLIC (type_decl) = 1;
4931 determine_visibility (type_decl);
4933 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4935 timevar_pop (TV_NAME_LOOKUP);
4942 struct pointer_set_t *visited;
4945 /* Called from for_each_template_parm via walk_tree. */
4948 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
4951 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4952 tree_fn_t fn = pfd->fn;
4953 void *data = pfd->data;
4956 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
4957 return error_mark_node;
4959 switch (TREE_CODE (t))
4962 if (TYPE_PTRMEMFUNC_P (t))
4968 if (!TYPE_TEMPLATE_INFO (t))
4970 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4971 fn, data, pfd->visited))
4972 return error_mark_node;
4976 /* Since we're not going to walk subtrees, we have to do this
4978 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4980 return error_mark_node;
4984 /* Check the return type. */
4985 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
4986 return error_mark_node;
4988 /* Check the parameter types. Since default arguments are not
4989 instantiated until they are needed, the TYPE_ARG_TYPES may
4990 contain expressions that involve template parameters. But,
4991 no-one should be looking at them yet. And, once they're
4992 instantiated, they don't contain template parameters, so
4993 there's no point in looking at them then, either. */
4997 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4998 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
5000 return error_mark_node;
5002 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5003 want walk_tree walking into them itself. */
5009 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
5011 return error_mark_node;
5016 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
5017 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
5019 return error_mark_node;
5024 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
5025 && for_each_template_parm (DECL_INITIAL (t), fn, data,
5027 return error_mark_node;
5028 if (DECL_CONTEXT (t)
5029 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
5031 return error_mark_node;
5034 case BOUND_TEMPLATE_TEMPLATE_PARM:
5035 /* Record template parameters such as `T' inside `TT<T>'. */
5036 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
5037 return error_mark_node;
5040 case TEMPLATE_TEMPLATE_PARM:
5041 case TEMPLATE_TYPE_PARM:
5042 case TEMPLATE_PARM_INDEX:
5043 if (fn && (*fn)(t, data))
5044 return error_mark_node;
5046 return error_mark_node;
5050 /* A template template parameter is encountered. */
5051 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
5052 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
5053 return error_mark_node;
5055 /* Already substituted template template parameter */
5061 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
5062 data, pfd->visited))
5063 return error_mark_node;
5067 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
5068 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5069 (TREE_TYPE (t)), fn, data,
5071 return error_mark_node;
5076 /* If there's no type, then this thing must be some expression
5077 involving template parameters. */
5078 if (!fn && !TREE_TYPE (t))
5079 return error_mark_node;
5084 case REINTERPRET_CAST_EXPR:
5085 case CONST_CAST_EXPR:
5086 case STATIC_CAST_EXPR:
5087 case DYNAMIC_CAST_EXPR:
5091 case PSEUDO_DTOR_EXPR:
5093 return error_mark_node;
5097 /* If we do not handle this case specially, we end up walking
5098 the BINFO hierarchy, which is circular, and therefore
5099 confuses walk_tree. */
5101 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
5103 return error_mark_node;
5110 /* We didn't find any template parameters we liked. */
5114 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5115 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5116 call FN with the parameter and the DATA.
5117 If FN returns nonzero, the iteration is terminated, and
5118 for_each_template_parm returns 1. Otherwise, the iteration
5119 continues. If FN never returns a nonzero value, the value
5120 returned by for_each_template_parm is 0. If FN is NULL, it is
5121 considered to be the function which always returns 1. */
5124 for_each_template_parm (tree t, tree_fn_t fn, void* data,
5125 struct pointer_set_t *visited)
5127 struct pair_fn_data pfd;
5134 /* Walk the tree. (Conceptually, we would like to walk without
5135 duplicates, but for_each_template_parm_r recursively calls
5136 for_each_template_parm, so we would need to reorganize a fair
5137 bit to use walk_tree_without_duplicates, so we keep our own
5140 pfd.visited = visited;
5142 pfd.visited = pointer_set_create ();
5143 result = walk_tree (&t,
5144 for_each_template_parm_r,
5146 pfd.visited) != NULL_TREE;
5151 pointer_set_destroy (pfd.visited);
5158 /* Returns true if T depends on any template parameter. */
5161 uses_template_parms (tree t)
5164 int saved_processing_template_decl;
5166 saved_processing_template_decl = processing_template_decl;
5167 if (!saved_processing_template_decl)
5168 processing_template_decl = 1;
5170 dependent_p = dependent_type_p (t);
5171 else if (TREE_CODE (t) == TREE_VEC)
5172 dependent_p = any_dependent_template_arguments_p (t);
5173 else if (TREE_CODE (t) == TREE_LIST)
5174 dependent_p = (uses_template_parms (TREE_VALUE (t))
5175 || uses_template_parms (TREE_CHAIN (t)));
5176 else if (TREE_CODE (t) == TYPE_DECL)
5177 dependent_p = dependent_type_p (TREE_TYPE (t));
5180 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5181 || TREE_CODE (t) == OVERLOAD
5182 || TREE_CODE (t) == BASELINK
5183 || TREE_CODE (t) == IDENTIFIER_NODE
5184 || CONSTANT_CLASS_P (t))
5185 dependent_p = (type_dependent_expression_p (t)
5186 || value_dependent_expression_p (t));
5189 gcc_assert (t == error_mark_node);
5190 dependent_p = false;
5193 processing_template_decl = saved_processing_template_decl;
5198 /* Returns true if T depends on any template parameter with level LEVEL. */
5201 uses_template_parms_level (tree t, int level)
5203 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5206 static int tinst_depth;
5207 extern int max_tinst_depth;
5208 #ifdef GATHER_STATISTICS
5211 static int tinst_level_tick;
5212 static int last_template_error_tick;
5214 /* We're starting to instantiate D; record the template instantiation context
5215 for diagnostics and to restore it later. */
5218 push_tinst_level (tree d)
5222 if (tinst_depth >= max_tinst_depth)
5224 /* If the instantiation in question still has unbound template parms,
5225 we don't really care if we can't instantiate it, so just return.
5226 This happens with base instantiation for implicit `typename'. */
5227 if (uses_template_parms (d))
5230 last_template_error_tick = tinst_level_tick;
5231 error ("template instantiation depth exceeds maximum of %d (use "
5232 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5233 max_tinst_depth, d);
5235 print_instantiation_context ();
5240 new = make_node (TINST_LEVEL);
5241 TINST_DECL (new) = d;
5242 TINST_LOCATION (new) = input_location;
5243 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
5244 TREE_CHAIN (new) = current_tinst_level;
5245 current_tinst_level = new;
5248 #ifdef GATHER_STATISTICS
5249 if (tinst_depth > depth_reached)
5250 depth_reached = tinst_depth;
5257 /* We're done instantiating this template; return to the instantiation
5261 pop_tinst_level (void)
5263 tree old = current_tinst_level;
5265 /* Restore the filename and line number stashed away when we started
5266 this instantiation. */
5267 input_location = TINST_LOCATION (old);
5268 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
5269 current_tinst_level = TREE_CHAIN (old);
5274 /* We're instantiating a deferred template; restore the template
5275 instantiation context in which the instantiation was requested, which
5276 is one step out from LEVEL. */
5279 reopen_tinst_level (tree level)
5284 for (t = level; t; t = TREE_CHAIN (t))
5287 current_tinst_level = level;
5291 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5292 vector of template arguments, as for tsubst.
5294 Returns an appropriate tsubst'd friend declaration. */
5297 tsubst_friend_function (tree decl, tree args)
5301 if (TREE_CODE (decl) == FUNCTION_DECL
5302 && DECL_TEMPLATE_INSTANTIATION (decl)
5303 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5304 /* This was a friend declared with an explicit template
5305 argument list, e.g.:
5309 to indicate that f was a template instantiation, not a new
5310 function declaration. Now, we have to figure out what
5311 instantiation of what template. */
5313 tree template_id, arglist, fns;
5316 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
5318 /* Friend functions are looked up in the containing namespace scope.
5319 We must enter that scope, to avoid finding member functions of the
5320 current cless with same name. */
5321 push_nested_namespace (ns);
5322 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
5323 tf_warning_or_error, NULL_TREE,
5324 /*integral_constant_expression_p=*/false);
5325 pop_nested_namespace (ns);
5326 arglist = tsubst (DECL_TI_ARGS (decl), args,
5327 tf_warning_or_error, NULL_TREE);
5328 template_id = lookup_template_function (fns, arglist);
5330 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5331 tmpl = determine_specialization (template_id, new_friend,
5333 /*need_member_template=*/0,
5334 TREE_VEC_LENGTH (args),
5336 return instantiate_template (tmpl, new_args, tf_error);
5339 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
5341 /* The NEW_FRIEND will look like an instantiation, to the
5342 compiler, but is not an instantiation from the point of view of
5343 the language. For example, we might have had:
5345 template <class T> struct S {
5346 template <class U> friend void f(T, U);
5349 Then, in S<int>, template <class U> void f(int, U) is not an
5350 instantiation of anything. */
5351 if (new_friend == error_mark_node)
5352 return error_mark_node;
5354 DECL_USE_TEMPLATE (new_friend) = 0;
5355 if (TREE_CODE (decl) == TEMPLATE_DECL)
5357 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5358 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5359 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5362 /* The mangled name for the NEW_FRIEND is incorrect. The function
5363 is not a template instantiation and should not be mangled like
5364 one. Therefore, we forget the mangling here; we'll recompute it
5365 later if we need it. */
5366 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5368 SET_DECL_RTL (new_friend, NULL_RTX);
5369 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
5372 if (DECL_NAMESPACE_SCOPE_P (new_friend))
5375 tree new_friend_template_info;
5376 tree new_friend_result_template_info;
5378 int new_friend_is_defn;
5380 /* We must save some information from NEW_FRIEND before calling
5381 duplicate decls since that function will free NEW_FRIEND if
5383 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5384 new_friend_is_defn =
5385 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5386 (template_for_substitution (new_friend)))
5388 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
5390 /* This declaration is a `primary' template. */
5391 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
5393 new_friend_result_template_info
5394 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
5397 new_friend_result_template_info = NULL_TREE;
5399 /* Make the init_value nonzero so pushdecl knows this is a defn. */
5400 if (new_friend_is_defn)
5401 DECL_INITIAL (new_friend) = error_mark_node;
5403 /* Inside pushdecl_namespace_level, we will push into the
5404 current namespace. However, the friend function should go
5405 into the namespace of the template. */
5406 ns = decl_namespace_context (new_friend);
5407 push_nested_namespace (ns);
5408 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
5409 pop_nested_namespace (ns);
5411 if (old_decl == error_mark_node)
5412 return error_mark_node;
5414 if (old_decl != new_friend)
5416 /* This new friend declaration matched an existing
5417 declaration. For example, given:
5419 template <class T> void f(T);
5420 template <class U> class C {
5421 template <class T> friend void f(T) {}
5424 the friend declaration actually provides the definition
5425 of `f', once C has been instantiated for some type. So,
5426 old_decl will be the out-of-class template declaration,
5427 while new_friend is the in-class definition.
5429 But, if `f' was called before this point, the
5430 instantiation of `f' will have DECL_TI_ARGS corresponding
5431 to `T' but not to `U', references to which might appear
5432 in the definition of `f'. Previously, the most general
5433 template for an instantiation of `f' was the out-of-class
5434 version; now it is the in-class version. Therefore, we
5435 run through all specialization of `f', adding to their
5436 DECL_TI_ARGS appropriately. In particular, they need a
5437 new set of outer arguments, corresponding to the
5438 arguments for this class instantiation.
5440 The same situation can arise with something like this:
5443 template <class T> class C {
5447 when `C<int>' is instantiated. Now, `f(int)' is defined
5450 if (!new_friend_is_defn)
5451 /* On the other hand, if the in-class declaration does
5452 *not* provide a definition, then we don't want to alter
5453 existing definitions. We can just leave everything
5458 /* Overwrite whatever template info was there before, if
5459 any, with the new template information pertaining to
5461 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5463 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
5464 reregister_specialization (new_friend,
5465 most_general_template (old_decl),
5470 tree new_friend_args;
5472 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
5473 = new_friend_result_template_info;
5475 new_friend_args = TI_ARGS (new_friend_template_info);
5476 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
5480 tree spec = TREE_VALUE (t);
5483 = add_outermost_template_args (new_friend_args,
5484 DECL_TI_ARGS (spec));
5487 /* Now, since specializations are always supposed to
5488 hang off of the most general template, we must move
5490 t = most_general_template (old_decl);
5493 DECL_TEMPLATE_SPECIALIZATIONS (t)
5494 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5495 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5496 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5501 /* The information from NEW_FRIEND has been merged into OLD_DECL
5502 by duplicate_decls. */
5503 new_friend = old_decl;
5508 tree context = DECL_CONTEXT (new_friend);
5512 template <class T> class C {
5513 template <class U> friend void C1<U>::f (); // case 1
5514 friend void C2<T>::f (); // case 2
5516 we only need to make sure CONTEXT is a complete type for
5517 case 2. To distinguish between the two cases, we note that
5518 CONTEXT of case 1 remains dependent type after tsubst while
5519 this isn't true for case 2. */
5520 ++processing_template_decl;
5521 dependent_p = dependent_type_p (context);
5522 --processing_template_decl;
5525 && !complete_type_or_else (context, NULL_TREE))
5526 return error_mark_node;
5528 if (COMPLETE_TYPE_P (context))
5530 /* Check to see that the declaration is really present, and,
5531 possibly obtain an improved declaration. */
5532 tree fn = check_classfn (context,
5533 new_friend, NULL_TREE);
5543 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5544 template arguments, as for tsubst.
5546 Returns an appropriate tsubst'd friend type or error_mark_node on
5550 tsubst_friend_class (tree friend_tmpl, tree args)
5556 context = DECL_CONTEXT (friend_tmpl);
5560 if (TREE_CODE (context) == NAMESPACE_DECL)
5561 push_nested_namespace (context);
5563 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
5566 /* Look for a class template declaration. We look for hidden names
5567 because two friend declarations of the same template are the
5568 same. For example, in:
5571 template <typename> friend class F;
5573 template <typename> struct B {
5574 template <typename> friend class F;
5577 both F templates are the same. */
5578 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
5579 /*block_p=*/true, 0,
5580 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
5582 /* But, if we don't find one, it might be because we're in a
5583 situation like this:
5591 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5592 for `S<int>', not the TEMPLATE_DECL. */
5593 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5595 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
5596 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
5599 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
5601 /* The friend template has already been declared. Just
5602 check to see that the declarations match, and install any new
5603 default parameters. We must tsubst the default parameters,
5604 of course. We only need the innermost template parameters
5605 because that is all that redeclare_class_template will look
5607 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5608 > TMPL_ARGS_DEPTH (args))
5611 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
5612 args, tf_warning_or_error);
5613 redeclare_class_template (TREE_TYPE (tmpl), parms);
5616 friend_type = TREE_TYPE (tmpl);
5620 /* The friend template has not already been declared. In this
5621 case, the instantiation of the template class will cause the
5622 injection of this template into the global scope. */
5623 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
5624 if (tmpl == error_mark_node)
5625 return error_mark_node;
5627 /* The new TMPL is not an instantiation of anything, so we
5628 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5629 the new type because that is supposed to be the corresponding
5630 template decl, i.e., TMPL. */
5631 DECL_USE_TEMPLATE (tmpl) = 0;
5632 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5633 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
5634 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5635 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
5637 /* Inject this template into the global scope. */
5638 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
5643 if (TREE_CODE (context) == NAMESPACE_DECL)
5644 pop_nested_namespace (context);
5646 pop_nested_class ();
5652 /* Returns zero if TYPE cannot be completed later due to circularity.
5653 Otherwise returns one. */
5656 can_complete_type_without_circularity (tree type)
5658 if (type == NULL_TREE || type == error_mark_node)
5660 else if (COMPLETE_TYPE_P (type))
5662 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5663 return can_complete_type_without_circularity (TREE_TYPE (type));
5664 else if (CLASS_TYPE_P (type)
5665 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
5672 instantiate_class_template (tree type)
5674 tree template, args, pattern, t, member;
5679 if (type == error_mark_node)
5680 return error_mark_node;
5682 if (TYPE_BEING_DEFINED (type)
5683 || COMPLETE_TYPE_P (type)
5684 || dependent_type_p (type))
5687 /* Figure out which template is being instantiated. */
5688 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5689 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
5691 /* Determine what specialization of the original template to
5693 t = most_specialized_class (type, template);
5694 if (t == error_mark_node)
5696 TYPE_BEING_DEFINED (type) = 1;
5697 return error_mark_node;
5701 /* This TYPE is actually an instantiation of a partial
5702 specialization. We replace the innermost set of ARGS with
5703 the arguments appropriate for substitution. For example,
5706 template <class T> struct S {};
5707 template <class T> struct S<T*> {};
5709 and supposing that we are instantiating S<int*>, ARGS will
5710 presently be {int*} -- but we need {int}. */
5711 pattern = TREE_TYPE (t);
5712 args = TREE_PURPOSE (t);
5716 pattern = TREE_TYPE (template);
5717 args = CLASSTYPE_TI_ARGS (type);
5720 /* If the template we're instantiating is incomplete, then clearly
5721 there's nothing we can do. */
5722 if (!COMPLETE_TYPE_P (pattern))
5725 /* If we've recursively instantiated too many templates, stop. */
5726 if (! push_tinst_level (type))
5729 /* Now we're really doing the instantiation. Mark the type as in
5730 the process of being defined. */
5731 TYPE_BEING_DEFINED (type) = 1;
5733 /* We may be in the middle of deferred access check. Disable
5735 push_deferring_access_checks (dk_no_deferred);
5737 push_to_top_level ();
5739 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
5741 /* Set the input location to the template definition. This is needed
5742 if tsubsting causes an error. */
5743 typedecl = TYPE_MAIN_DECL (type);
5744 input_location = DECL_SOURCE_LOCATION (typedecl);
5745 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
5747 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5748 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5749 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
5750 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
5751 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5752 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5753 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5754 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
5755 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5756 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5757 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5758 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
5759 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
5760 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
5761 if (ANON_AGGR_TYPE_P (pattern))
5762 SET_ANON_AGGR_TYPE_P (type);
5763 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5765 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5766 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5769 pbinfo = TYPE_BINFO (pattern);
5771 /* We should never instantiate a nested class before its enclosing
5772 class; we need to look up the nested class by name before we can
5773 instantiate it, and that lookup should instantiate the enclosing
5775 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5776 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5777 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
5779 base_list = NULL_TREE;
5780 if (BINFO_N_BASE_BINFOS (pbinfo))
5783 tree context = TYPE_CONTEXT (type);
5787 /* We must enter the scope containing the type, as that is where
5788 the accessibility of types named in dependent bases are
5790 pushed_scope = push_scope (context ? context : global_namespace);
5792 /* Substitute into each of the bases to determine the actual
5794 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
5797 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5799 /* Substitute to figure out the base class. */
5800 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
5801 if (base == error_mark_node)
5804 base_list = tree_cons (access, base, base_list);
5805 if (BINFO_VIRTUAL_P (pbase_binfo))
5806 TREE_TYPE (base_list) = integer_type_node;
5809 /* The list is now in reverse order; correct that. */
5810 base_list = nreverse (base_list);
5813 pop_scope (pushed_scope);
5815 /* Now call xref_basetypes to set up all the base-class
5817 xref_basetypes (type, base_list);
5820 /* Now that our base classes are set up, enter the scope of the
5821 class, so that name lookups into base classes, etc. will work
5822 correctly. This is precisely analogous to what we do in
5823 begin_class_definition when defining an ordinary non-template
5827 /* Now members are processed in the order of declaration. */
5828 for (member = CLASSTYPE_DECL_LIST (pattern);
5829 member; member = TREE_CHAIN (member))
5831 tree t = TREE_VALUE (member);
5833 if (TREE_PURPOSE (member))
5837 /* Build new CLASSTYPE_NESTED_UTDS. */
5840 bool class_template_p;
5842 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
5843 && TYPE_LANG_SPECIFIC (t)
5844 && CLASSTYPE_IS_TEMPLATE (t));
5845 /* If the member is a class template, then -- even after
5846 substitution -- there may be dependent types in the
5847 template argument list for the class. We increment
5848 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5849 that function will assume that no types are dependent
5850 when outside of a template. */
5851 if (class_template_p)
5852 ++processing_template_decl;
5853 newtag = tsubst (t, args, tf_error, NULL_TREE);
5854 if (class_template_p)
5855 --processing_template_decl;
5856 if (newtag == error_mark_node)
5859 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5861 tree name = TYPE_IDENTIFIER (t);
5863 if (class_template_p)
5864 /* Unfortunately, lookup_template_class sets
5865 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5866 instantiation (i.e., for the type of a member
5867 template class nested within a template class.)
5868 This behavior is required for
5869 maybe_process_partial_specialization to work
5870 correctly, but is not accurate in this case;
5871 the TAG is not an instantiation of anything.
5872 (The corresponding TEMPLATE_DECL is an
5873 instantiation, but the TYPE is not.) */
5874 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5876 /* Now, we call pushtag to put this NEWTAG into the scope of
5877 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5878 pushtag calling push_template_decl. We don't have to do
5879 this for enums because it will already have been done in
5882 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5883 pushtag (name, newtag, /*tag_scope=*/ts_current);
5886 else if (TREE_CODE (t) == FUNCTION_DECL
5887 || DECL_FUNCTION_TEMPLATE_P (t))
5889 /* Build new TYPE_METHODS. */
5892 if (TREE_CODE (t) == TEMPLATE_DECL)
5893 ++processing_template_decl;
5894 r = tsubst (t, args, tf_error, NULL_TREE);
5895 if (TREE_CODE (t) == TEMPLATE_DECL)
5896 --processing_template_decl;
5897 set_current_access_from_decl (r);
5898 finish_member_declaration (r);
5902 /* Build new TYPE_FIELDS. */
5904 if (TREE_CODE (t) != CONST_DECL)
5908 /* The the file and line for this declaration, to
5909 assist in error message reporting. Since we
5910 called push_tinst_level above, we don't need to
5912 input_location = DECL_SOURCE_LOCATION (t);
5914 if (TREE_CODE (t) == TEMPLATE_DECL)
5915 ++processing_template_decl;
5916 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
5917 if (TREE_CODE (t) == TEMPLATE_DECL)
5918 --processing_template_decl;
5919 if (TREE_CODE (r) == VAR_DECL)
5923 [t]he initialization (and any associated
5924 side-effects) of a static data member does
5925 not occur unless the static data member is
5926 itself used in a way that requires the
5927 definition of the static data member to
5930 Therefore, we do not substitute into the
5931 initialized for the static data member here. */
5932 finish_static_data_member_decl
5935 /*init_const_expr_p=*/false,
5936 /*asmspec_tree=*/NULL_TREE,
5938 if (DECL_INITIALIZED_IN_CLASS_P (r))
5939 check_static_variable_definition (r, TREE_TYPE (r));
5941 else if (TREE_CODE (r) == FIELD_DECL)
5943 /* Determine whether R has a valid type and can be
5944 completed later. If R is invalid, then it is
5945 replaced by error_mark_node so that it will not be
5946 added to TYPE_FIELDS. */
5947 tree rtype = TREE_TYPE (r);
5948 if (can_complete_type_without_circularity (rtype))
5949 complete_type (rtype);
5951 if (!COMPLETE_TYPE_P (rtype))
5953 cxx_incomplete_type_error (r, rtype);
5954 r = error_mark_node;
5958 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5959 such a thing will already have been added to the field
5960 list by tsubst_enum in finish_member_declaration in the
5961 CLASSTYPE_NESTED_UTDS case above. */
5962 if (!(TREE_CODE (r) == TYPE_DECL
5963 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5964 && DECL_ARTIFICIAL (r)))
5966 set_current_access_from_decl (r);
5967 finish_member_declaration (r);
5974 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5976 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5978 tree friend_type = t;
5979 bool adjust_processing_template_decl = false;
5981 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5983 /* template <class T> friend class C; */
5984 friend_type = tsubst_friend_class (friend_type, args);
5985 adjust_processing_template_decl = true;
5987 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
5989 /* template <class T> friend class C::D; */
5990 friend_type = tsubst (friend_type, args,
5991 tf_warning_or_error, NULL_TREE);
5992 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5993 friend_type = TREE_TYPE (friend_type);
5994 adjust_processing_template_decl = true;
5996 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
5998 /* This could be either
6002 when dependent_type_p is false or
6004 template <class U> friend class T::C;
6007 friend_type = tsubst (friend_type, args,
6008 tf_warning_or_error, NULL_TREE);
6009 /* Bump processing_template_decl for correct
6010 dependent_type_p calculation. */
6011 ++processing_template_decl;
6012 if (dependent_type_p (friend_type))
6013 adjust_processing_template_decl = true;
6014 --processing_template_decl;
6016 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
6017 && hidden_name_p (TYPE_NAME (friend_type)))
6021 where C hasn't been declared yet. Let's lookup name
6022 from namespace scope directly, bypassing any name that
6023 come from dependent base class. */
6024 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
6026 /* The call to xref_tag_from_type does injection for friend
6028 push_nested_namespace (ns);
6030 xref_tag_from_type (friend_type, NULL_TREE,
6031 /*tag_scope=*/ts_current);
6032 pop_nested_namespace (ns);
6034 else if (uses_template_parms (friend_type))
6035 /* friend class C<T>; */
6036 friend_type = tsubst (friend_type, args,
6037 tf_warning_or_error, NULL_TREE);
6042 where C is already declared or
6044 friend class C<int>;
6046 We don't have to do anything in these cases. */
6048 if (adjust_processing_template_decl)
6049 /* Trick make_friend_class into realizing that the friend
6050 we're adding is a template, not an ordinary class. It's
6051 important that we use make_friend_class since it will
6052 perform some error-checking and output cross-reference
6054 ++processing_template_decl;
6056 if (friend_type != error_mark_node)
6057 make_friend_class (type, friend_type, /*complain=*/false);
6059 if (adjust_processing_template_decl)
6060 --processing_template_decl;
6064 /* Build new DECL_FRIENDLIST. */
6067 /* The the file and line for this declaration, to
6068 assist in error message reporting. Since we
6069 called push_tinst_level above, we don't need to
6071 input_location = DECL_SOURCE_LOCATION (t);
6073 if (TREE_CODE (t) == TEMPLATE_DECL)
6075 ++processing_template_decl;
6076 push_deferring_access_checks (dk_no_check);
6079 r = tsubst_friend_function (t, args);
6080 add_friend (type, r, /*complain=*/false);
6081 if (TREE_CODE (t) == TEMPLATE_DECL)
6083 pop_deferring_access_checks ();
6084 --processing_template_decl;
6090 /* Set the file and line number information to whatever is given for
6091 the class itself. This puts error messages involving generated
6092 implicit functions at a predictable point, and the same point
6093 that would be used for non-template classes. */
6094 input_location = DECL_SOURCE_LOCATION (typedecl);
6096 unreverse_member_declarations (type);
6097 finish_struct_1 (type);
6098 TYPE_BEING_DEFINED (type) = 0;
6100 /* Now that the class is complete, instantiate default arguments for
6101 any member functions. We don't do this earlier because the
6102 default arguments may reference members of the class. */
6103 if (!PRIMARY_TEMPLATE_P (template))
6104 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
6105 if (TREE_CODE (t) == FUNCTION_DECL
6106 /* Implicitly generated member functions will not have template
6107 information; they are not instantiations, but instead are
6108 created "fresh" for each instantiation. */
6109 && DECL_TEMPLATE_INFO (t))
6110 tsubst_default_arguments (t);
6113 pop_from_top_level ();
6114 pop_deferring_access_checks ();
6117 /* The vtable for a template class can be emitted in any translation
6118 unit in which the class is instantiated. When there is no key
6119 method, however, finish_struct_1 will already have added TYPE to
6120 the keyed_classes list. */
6121 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
6122 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6128 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6134 else if (TYPE_P (t))
6135 r = tsubst (t, args, complain, in_decl);
6138 r = tsubst_expr (t, args, complain, in_decl,
6139 /*integral_constant_expression_p=*/true);
6140 r = fold_non_dependent_expr (r);
6145 /* Substitute ARGS into the vector or list of template arguments T. */
6148 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
6150 int len = TREE_VEC_LENGTH (t);
6151 int need_new = 0, i;
6152 tree *elts = (tree *) alloca (len * sizeof (tree));
6154 for (i = 0; i < len; i++)
6156 tree orig_arg = TREE_VEC_ELT (t, i);
6159 if (TREE_CODE (orig_arg) == TREE_VEC)
6160 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
6162 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
6164 if (new_arg == error_mark_node)
6165 return error_mark_node;
6168 if (new_arg != orig_arg)
6175 t = make_tree_vec (len);
6176 for (i = 0; i < len; i++)
6177 TREE_VEC_ELT (t, i) = elts[i];
6182 /* Return the result of substituting ARGS into the template parameters
6183 given by PARMS. If there are m levels of ARGS and m + n levels of
6184 PARMS, then the result will contain n levels of PARMS. For
6185 example, if PARMS is `template <class T> template <class U>
6186 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
6187 result will be `template <int*, double, class V>'. */
6190 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
6195 /* When substituting into a template, we must set
6196 PROCESSING_TEMPLATE_DECL as the template parameters may be
6197 dependent if they are based on one-another, and the dependency
6198 predicates are short-circuit outside of templates. */
6199 ++processing_template_decl;
6201 for (new_parms = &r;
6202 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
6203 new_parms = &(TREE_CHAIN (*new_parms)),
6204 parms = TREE_CHAIN (parms))
6207 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
6210 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
6216 if (parms == error_mark_node)
6219 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6221 if (tuple == error_mark_node)
6224 default_value = TREE_PURPOSE (tuple);
6225 parm_decl = TREE_VALUE (tuple);
6227 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
6228 if (TREE_CODE (parm_decl) == PARM_DECL
6229 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6230 parm_decl = error_mark_node;
6231 default_value = tsubst_template_arg (default_value, args,
6232 complain, NULL_TREE);
6234 tuple = build_tree_list (default_value, parm_decl);
6235 TREE_VEC_ELT (new_vec, i) = tuple;
6239 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
6240 - TMPL_ARGS_DEPTH (args)),
6241 new_vec, NULL_TREE);
6244 --processing_template_decl;
6249 /* Substitute the ARGS into the indicated aggregate (or enumeration)
6250 type T. If T is not an aggregate or enumeration type, it is
6251 handled as if by tsubst. IN_DECL is as for tsubst. If
6252 ENTERING_SCOPE is nonzero, T is the context for a template which
6253 we are presently tsubst'ing. Return the substituted value. */
6256 tsubst_aggr_type (tree t,
6258 tsubst_flags_t complain,
6265 switch (TREE_CODE (t))
6268 if (TYPE_PTRMEMFUNC_P (t))
6269 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
6271 /* Else fall through. */
6274 if (TYPE_TEMPLATE_INFO (t))
6279 bool saved_skip_evaluation;
6281 /* In "sizeof(X<I>)" we need to evaluate "I". */
6282 saved_skip_evaluation = skip_evaluation;
6283 skip_evaluation = false;
6285 /* First, determine the context for the type we are looking
6287 context = TYPE_CONTEXT (t);
6289 context = tsubst_aggr_type (context, args, complain,
6290 in_decl, /*entering_scope=*/1);
6292 /* Then, figure out what arguments are appropriate for the
6293 type we are trying to find. For example, given:
6295 template <class T> struct S;
6296 template <class T, class U> void f(T, U) { S<U> su; }
6298 and supposing that we are instantiating f<int, double>,
6299 then our ARGS will be {int, double}, but, when looking up
6300 S we only want {double}. */
6301 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6303 if (argvec == error_mark_node)
6304 r = error_mark_node;
6307 r = lookup_template_class (t, argvec, in_decl, context,
6308 entering_scope, complain);
6309 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6312 skip_evaluation = saved_skip_evaluation;
6317 /* This is not a template type, so there's nothing to do. */
6321 return tsubst (t, args, complain, in_decl);
6325 /* Substitute into the default argument ARG (a default argument for
6326 FN), which has the indicated TYPE. */
6329 tsubst_default_argument (tree fn, tree type, tree arg)
6331 tree saved_class_ptr = NULL_TREE;
6332 tree saved_class_ref = NULL_TREE;
6334 /* This default argument came from a template. Instantiate the
6335 default argument here, not in tsubst. In the case of
6344 we must be careful to do name lookup in the scope of S<T>,
6345 rather than in the current class. */
6346 push_access_scope (fn);
6347 /* The "this" pointer is not valid in a default argument. */
6350 saved_class_ptr = current_class_ptr;
6351 cp_function_chain->x_current_class_ptr = NULL_TREE;
6352 saved_class_ref = current_class_ref;
6353 cp_function_chain->x_current_class_ref = NULL_TREE;
6356 push_deferring_access_checks(dk_no_deferred);
6357 /* The default argument expression may cause implicitly defined
6358 member functions to be synthesized, which will result in garbage
6359 collection. We must treat this situation as if we were within
6360 the body of function so as to avoid collecting live data on the
6363 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
6364 tf_warning_or_error, NULL_TREE,
6365 /*integral_constant_expression_p=*/false);
6367 pop_deferring_access_checks();
6369 /* Restore the "this" pointer. */
6372 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6373 cp_function_chain->x_current_class_ref = saved_class_ref;
6376 pop_access_scope (fn);
6378 /* Make sure the default argument is reasonable. */
6379 arg = check_default_argument (type, arg);
6384 /* Substitute into all the default arguments for FN. */
6387 tsubst_default_arguments (tree fn)
6392 tmpl_args = DECL_TI_ARGS (fn);
6394 /* If this function is not yet instantiated, we certainly don't need
6395 its default arguments. */
6396 if (uses_template_parms (tmpl_args))
6399 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6401 arg = TREE_CHAIN (arg))
6402 if (TREE_PURPOSE (arg))
6403 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
6405 TREE_PURPOSE (arg));
6408 /* Substitute the ARGS into the T, which is a _DECL. Return the
6409 result of the substitution. Issue error and warning messages under
6410 control of COMPLAIN. */
6413 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
6415 location_t saved_loc;
6419 /* Set the filename and linenumber to improve error-reporting. */
6420 saved_loc = input_location;
6421 input_location = DECL_SOURCE_LOCATION (t);
6423 switch (TREE_CODE (t))
6427 /* We can get here when processing a member function template,
6428 member class template, and template template parameter of
6429 a template class. */
6430 tree decl = DECL_TEMPLATE_RESULT (t);
6435 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
6437 /* Template template parameter is treated here. */
6438 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6439 if (new_type == error_mark_node)
6440 return error_mark_node;
6443 TREE_CHAIN (r) = NULL_TREE;
6444 TREE_TYPE (r) = new_type;
6445 DECL_TEMPLATE_RESULT (r)
6446 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
6447 DECL_TEMPLATE_PARMS (r)
6448 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6450 TYPE_NAME (new_type) = r;
6454 /* We might already have an instance of this template.
6455 The ARGS are for the surrounding class type, so the
6456 full args contain the tsubst'd args for the context,
6457 plus the innermost args from the template decl. */
6458 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
6459 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6460 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
6461 /* Because this is a template, the arguments will still be
6462 dependent, even after substitution. If
6463 PROCESSING_TEMPLATE_DECL is not set, the dependency
6464 predicates will short-circuit. */
6465 ++processing_template_decl;
6466 full_args = tsubst_template_args (tmpl_args, args,
6468 --processing_template_decl;
6469 if (full_args == error_mark_node)
6470 return error_mark_node;
6472 /* tsubst_template_args doesn't copy the vector if
6473 nothing changed. But, *something* should have
6475 gcc_assert (full_args != tmpl_args);
6477 spec = retrieve_specialization (t, full_args,
6478 /*class_specializations_p=*/true);
6479 if (spec != NULL_TREE)
6485 /* Make a new template decl. It will be similar to the
6486 original, but will record the current template arguments.
6487 We also create a new function declaration, which is just
6488 like the old one, but points to this new template, rather
6489 than the old one. */
6491 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
6492 TREE_CHAIN (r) = NULL_TREE;
6494 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
6496 if (TREE_CODE (decl) == TYPE_DECL)
6499 ++processing_template_decl;
6500 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6501 --processing_template_decl;
6502 if (new_type == error_mark_node)
6503 return error_mark_node;
6505 TREE_TYPE (r) = new_type;
6506 CLASSTYPE_TI_TEMPLATE (new_type) = r;
6507 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
6508 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
6509 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
6514 ++processing_template_decl;
6515 new_decl = tsubst (decl, args, complain, in_decl);
6516 --processing_template_decl;
6517 if (new_decl == error_mark_node)
6518 return error_mark_node;
6520 DECL_TEMPLATE_RESULT (r) = new_decl;
6521 DECL_TI_TEMPLATE (new_decl) = r;
6522 TREE_TYPE (r) = TREE_TYPE (new_decl);
6523 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
6524 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
6527 SET_DECL_IMPLICIT_INSTANTIATION (r);
6528 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6529 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
6531 /* The template parameters for this new template are all the
6532 template parameters for the old template, except the
6533 outermost level of parameters. */
6534 DECL_TEMPLATE_PARMS (r)
6535 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6538 if (PRIMARY_TEMPLATE_P (t))
6539 DECL_PRIMARY_TEMPLATE (r) = r;
6541 if (TREE_CODE (decl) != TYPE_DECL)
6542 /* Record this non-type partial instantiation. */
6543 register_specialization (r, t,
6544 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6552 tree argvec = NULL_TREE;
6560 /* Nobody should be tsubst'ing into non-template functions. */
6561 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
6563 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6568 /* If T is not dependent, just return it. We have to
6569 increment PROCESSING_TEMPLATE_DECL because
6570 value_dependent_expression_p assumes that nothing is
6571 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6572 ++processing_template_decl;
6573 dependent_p = value_dependent_expression_p (t);
6574 --processing_template_decl;
6578 /* Calculate the most general template of which R is a
6579 specialization, and the complete set of arguments used to
6581 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
6582 argvec = tsubst_template_args (DECL_TI_ARGS
6583 (DECL_TEMPLATE_RESULT (gen_tmpl)),
6584 args, complain, in_decl);
6586 /* Check to see if we already have this specialization. */
6587 spec = retrieve_specialization (gen_tmpl, argvec,
6588 /*class_specializations_p=*/false);
6596 /* We can see more levels of arguments than parameters if
6597 there was a specialization of a member template, like
6600 template <class T> struct S { template <class U> void f(); }
6601 template <> template <class U> void S<int>::f(U);
6603 Here, we'll be substituting into the specialization,
6604 because that's where we can find the code we actually
6605 want to generate, but we'll have enough arguments for
6606 the most general template.
6608 We also deal with the peculiar case:
6610 template <class T> struct S {
6611 template <class U> friend void f();
6613 template <class U> void f() {}
6615 template void f<double>();
6617 Here, the ARGS for the instantiation of will be {int,
6618 double}. But, we only need as many ARGS as there are
6619 levels of template parameters in CODE_PATTERN. We are
6620 careful not to get fooled into reducing the ARGS in
6623 template <class T> struct S { template <class U> void f(U); }
6624 template <class T> template <> void S<T>::f(int) {}
6626 which we can spot because the pattern will be a
6627 specialization in this case. */
6628 args_depth = TMPL_ARGS_DEPTH (args);
6630 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
6631 if (args_depth > parms_depth
6632 && !DECL_TEMPLATE_SPECIALIZATION (t))
6633 args = get_innermost_template_args (args, parms_depth);
6637 /* This special case arises when we have something like this:
6639 template <class T> struct S {
6640 friend void f<int>(int, double);
6643 Here, the DECL_TI_TEMPLATE for the friend declaration
6644 will be an IDENTIFIER_NODE. We are being called from
6645 tsubst_friend_function, and we want only to create a
6646 new decl (R) with appropriate types so that we can call
6647 determine_specialization. */
6648 gen_tmpl = NULL_TREE;
6651 if (DECL_CLASS_SCOPE_P (t))
6653 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6657 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6658 complain, t, /*entering_scope=*/1);
6663 ctx = DECL_CONTEXT (t);
6665 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6666 if (type == error_mark_node)
6667 return error_mark_node;
6669 /* We do NOT check for matching decls pushed separately at this
6670 point, as they may not represent instantiations of this
6671 template, and in any case are considered separate under the
6674 DECL_USE_TEMPLATE (r) = 0;
6675 TREE_TYPE (r) = type;
6676 /* Clear out the mangled name and RTL for the instantiation. */
6677 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6678 SET_DECL_RTL (r, NULL_RTX);
6679 DECL_INITIAL (r) = NULL_TREE;
6680 DECL_CONTEXT (r) = ctx;
6682 if (member && DECL_CONV_FN_P (r))
6683 /* Type-conversion operator. Reconstruct the name, in
6684 case it's the name of one of the template's parameters. */
6685 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
6687 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
6689 DECL_RESULT (r) = NULL_TREE;
6691 TREE_STATIC (r) = 0;
6692 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6693 DECL_EXTERNAL (r) = 1;
6694 /* If this is an instantiation of a function with internal
6695 linkage, we already know what object file linkage will be
6696 assigned to the instantiation. */
6697 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
6698 DECL_DEFER_OUTPUT (r) = 0;
6699 TREE_CHAIN (r) = NULL_TREE;
6700 DECL_PENDING_INLINE_INFO (r) = 0;
6701 DECL_PENDING_INLINE_P (r) = 0;
6702 DECL_SAVED_TREE (r) = NULL_TREE;
6704 if (DECL_CLONED_FUNCTION (r))
6706 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
6708 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6709 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6712 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6713 this in the special friend case mentioned above where
6714 GEN_TMPL is NULL. */
6717 DECL_TEMPLATE_INFO (r)
6718 = tree_cons (gen_tmpl, argvec, NULL_TREE);
6719 SET_DECL_IMPLICIT_INSTANTIATION (r);
6720 register_specialization (r, gen_tmpl, argvec, false);
6722 /* We're not supposed to instantiate default arguments
6723 until they are called, for a template. But, for a
6726 template <class T> void f ()
6727 { extern void g(int i = T()); }
6729 we should do the substitution when the template is
6730 instantiated. We handle the member function case in
6731 instantiate_class_template since the default arguments
6732 might refer to other members of the class. */
6734 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6735 && !uses_template_parms (argvec))
6736 tsubst_default_arguments (r);
6739 DECL_TEMPLATE_INFO (r) = NULL_TREE;
6741 /* Copy the list of befriending classes. */
6742 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6744 friends = &TREE_CHAIN (*friends))
6746 *friends = copy_node (*friends);
6747 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
6752 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
6754 maybe_retrofit_in_chrg (r);
6755 if (DECL_CONSTRUCTOR_P (r))
6756 grok_ctor_properties (ctx, r);
6757 /* If this is an instantiation of a member template, clone it.
6758 If it isn't, that'll be handled by
6759 clone_constructors_and_destructors. */
6760 if (PRIMARY_TEMPLATE_P (gen_tmpl))
6761 clone_function_decl (r, /*update_method_vec_p=*/0);
6763 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
6764 && !grok_op_properties (r, (complain & tf_error) != 0))
6765 return error_mark_node;
6767 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6768 SET_DECL_FRIEND_CONTEXT (r,
6769 tsubst (DECL_FRIEND_CONTEXT (t),
6770 args, complain, in_decl));
6772 /* Possibly limit visibility based on template args. */
6773 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
6774 if (DECL_VISIBILITY_SPECIFIED (t))
6776 DECL_VISIBILITY_SPECIFIED (r) = 0;
6778 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
6780 determine_visibility (r);
6789 if (DECL_TEMPLATE_PARM_P (t))
6790 SET_DECL_TEMPLATE_PARM_P (r);
6792 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6793 type = type_decays_to (type);
6794 TREE_TYPE (r) = type;
6795 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6797 if (DECL_INITIAL (r))
6799 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6800 DECL_INITIAL (r) = TREE_TYPE (r);
6802 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6806 DECL_CONTEXT (r) = NULL_TREE;
6808 if (!DECL_TEMPLATE_PARM_P (r))
6809 DECL_ARG_TYPE (r) = type_passed_as (type);
6811 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
6812 complain, TREE_CHAIN (t));
6821 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6822 if (type == error_mark_node)
6823 return error_mark_node;
6824 TREE_TYPE (r) = type;
6825 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6827 /* DECL_INITIAL gives the number of bits in a bit-field. */
6829 = tsubst_expr (DECL_INITIAL (t), args,
6831 /*integral_constant_expression_p=*/true);
6832 /* We don't have to set DECL_CONTEXT here; it is set by
6833 finish_member_declaration. */
6834 TREE_CHAIN (r) = NULL_TREE;
6835 if (VOID_TYPE_P (type))
6836 error ("instantiation of %q+D as type %qT", r, type);
6841 /* We reach here only for member using decls. */
6842 if (DECL_DEPENDENT_P (t))
6844 r = do_class_using_decl
6845 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6846 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6848 r = error_mark_node;
6853 TREE_CHAIN (r) = NULL_TREE;
6860 tree argvec = NULL_TREE;
6861 tree gen_tmpl = NULL_TREE;
6863 tree tmpl = NULL_TREE;
6865 tree type = NULL_TREE;
6868 if (TREE_CODE (t) == TYPE_DECL)
6870 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6871 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6872 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6874 /* If this is the canonical decl, we don't have to
6875 mess with instantiations, and often we can't (for
6876 typename, template type parms and such). Note that
6877 TYPE_NAME is not correct for the above test if
6878 we've copied the type for a typedef. */
6879 r = TYPE_NAME (type);
6884 /* Check to see if we already have the specialization we
6887 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
6889 /* T is a static data member or namespace-scope entity.
6890 We have to substitute into namespace-scope variables
6891 (even though such entities are never templates) because
6894 template <class T> void f() { extern T t; }
6896 where the entity referenced is not known until
6897 instantiation time. */
6899 ctx = DECL_CONTEXT (t);
6900 if (DECL_CLASS_SCOPE_P (t))
6902 ctx = tsubst_aggr_type (ctx, args,
6904 in_decl, /*entering_scope=*/1);
6905 /* If CTX is unchanged, then T is in fact the
6906 specialization we want. That situation occurs when
6907 referencing a static data member within in its own
6908 class. We can use pointer equality, rather than
6909 same_type_p, because DECL_CONTEXT is always
6911 if (ctx == DECL_CONTEXT (t))
6917 tmpl = DECL_TI_TEMPLATE (t);
6918 gen_tmpl = most_general_template (tmpl);
6919 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6920 spec = (retrieve_specialization
6922 /*class_specializations_p=*/false));
6927 /* A local variable. */
6929 /* Subsequent calls to pushdecl will fill this in. */
6931 spec = retrieve_local_specialization (t);
6933 /* If we already have the specialization we need, there is
6934 nothing more to do. */
6941 /* Create a new node for the specialization we need. */
6943 if (TREE_CODE (r) == VAR_DECL)
6945 /* Even if the original location is out of scope, the
6946 newly substituted one is not. */
6947 DECL_DEAD_FOR_LOCAL (r) = 0;
6948 DECL_INITIALIZED_P (r) = 0;
6949 DECL_TEMPLATE_INSTANTIATED (r) = 0;
6950 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6951 if (type == error_mark_node)
6952 return error_mark_node;
6953 if (TREE_CODE (type) == FUNCTION_TYPE)
6955 /* It may seem that this case cannot occur, since:
6960 declares a function, not a variable. However:
6963 template <typename T> void g() { T t; }
6964 template void g<f>();
6966 is an attempt to declare a variable with function
6968 error ("variable %qD has function type",
6969 /* R is not yet sufficiently initialized, so we
6970 just use its name. */
6972 return error_mark_node;
6974 type = complete_type (type);
6975 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6976 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
6977 type = check_var_type (DECL_NAME (r), type);
6979 if (DECL_HAS_VALUE_EXPR_P (t))
6981 tree ve = DECL_VALUE_EXPR (t);
6982 ve = tsubst_expr (ve, args, complain, in_decl,
6983 /*constant_expression_p=*/false);
6984 SET_DECL_VALUE_EXPR (r, ve);
6987 else if (DECL_SELF_REFERENCE_P (t))
6988 SET_DECL_SELF_REFERENCE_P (r);
6989 TREE_TYPE (r) = type;
6990 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
6991 DECL_CONTEXT (r) = ctx;
6992 /* Clear out the mangled name and RTL for the instantiation. */
6993 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6994 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6995 SET_DECL_RTL (r, NULL_RTX);
6996 /* The initializer must not be expanded until it is required;
6998 DECL_INITIAL (r) = NULL_TREE;
6999 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
7000 SET_DECL_RTL (r, NULL_RTX);
7001 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
7002 if (TREE_CODE (r) == VAR_DECL)
7004 /* Possibly limit visibility based on template args. */
7005 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
7006 if (DECL_VISIBILITY_SPECIFIED (t))
7008 DECL_VISIBILITY_SPECIFIED (r) = 0;
7010 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
7012 determine_visibility (r);
7017 /* A static data member declaration is always marked
7018 external when it is declared in-class, even if an
7019 initializer is present. We mimic the non-template
7021 DECL_EXTERNAL (r) = 1;
7023 register_specialization (r, gen_tmpl, argvec, false);
7024 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
7025 SET_DECL_IMPLICIT_INSTANTIATION (r);
7028 register_local_specialization (r, t);
7030 TREE_CHAIN (r) = NULL_TREE;
7039 /* Restore the file and line information. */
7040 input_location = saved_loc;
7045 /* Substitute into the ARG_TYPES of a function type. */
7048 tsubst_arg_types (tree arg_types,
7050 tsubst_flags_t complain,
7053 tree remaining_arg_types;
7056 tree result = NULL_TREE;
7058 if (!arg_types || arg_types == void_list_node)
7061 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
7062 args, complain, in_decl);
7063 if (remaining_arg_types == error_mark_node)
7064 return error_mark_node;
7066 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
7067 if (type == error_mark_node)
7068 return error_mark_node;
7069 if (VOID_TYPE_P (type))
7071 if (complain & tf_error)
7073 error ("invalid parameter type %qT", type);
7075 error ("in declaration %q+D", in_decl);
7077 return error_mark_node;
7080 /* Do array-to-pointer, function-to-pointer conversion, and ignore
7081 top-level qualifiers as required. */
7082 type = TYPE_MAIN_VARIANT (type_decays_to (type));
7084 /* We do not substitute into default arguments here. The standard
7085 mandates that they be instantiated only when needed, which is
7086 done in build_over_call. */
7087 default_arg = TREE_PURPOSE (arg_types);
7089 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
7091 /* We've instantiated a template before its default arguments
7092 have been parsed. This can happen for a nested template
7093 class, and is not an error unless we require the default
7094 argument in a call of this function. */
7095 result = tree_cons (default_arg, type, remaining_arg_types);
7096 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
7099 result = hash_tree_cons (default_arg, type, remaining_arg_types);
7104 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
7105 *not* handle the exception-specification for FNTYPE, because the
7106 initial substitution of explicitly provided template parameters
7107 during argument deduction forbids substitution into the
7108 exception-specification:
7112 All references in the function type of the function template to the
7113 corresponding template parameters are replaced by the specified tem-
7114 plate argument values. If a substitution in a template parameter or
7115 in the function type of the function template results in an invalid
7116 type, type deduction fails. [Note: The equivalent substitution in
7117 exception specifications is done only when the function is instanti-
7118 ated, at which point a program is ill-formed if the substitution
7119 results in an invalid type.] */
7122 tsubst_function_type (tree t,
7124 tsubst_flags_t complain,
7131 /* The TYPE_CONTEXT is not used for function/method types. */
7132 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
7134 /* Substitute the return type. */
7135 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7136 if (return_type == error_mark_node)
7137 return error_mark_node;
7138 /* The standard does not presently indicate that creation of a
7139 function type with an invalid return type is a deduction failure.
7140 However, that is clearly analogous to creating an array of "void"
7141 or a reference to a reference. This is core issue #486. */
7142 if (TREE_CODE (return_type) == ARRAY_TYPE
7143 || TREE_CODE (return_type) == FUNCTION_TYPE)
7145 if (complain & tf_error)
7147 if (TREE_CODE (return_type) == ARRAY_TYPE)
7148 error ("function returning an array");
7150 error ("function returning a function");
7152 return error_mark_node;
7155 /* Substitute the argument types. */
7156 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
7158 if (arg_types == error_mark_node)
7159 return error_mark_node;
7161 /* Construct a new type node and return it. */
7162 if (TREE_CODE (t) == FUNCTION_TYPE)
7163 fntype = build_function_type (return_type, arg_types);
7166 tree r = TREE_TYPE (TREE_VALUE (arg_types));
7167 if (! IS_AGGR_TYPE (r))
7171 Type deduction may fail for any of the following
7174 -- Attempting to create "pointer to member of T" when T
7175 is not a class type. */
7176 if (complain & tf_error)
7177 error ("creating pointer to member function of non-class type %qT",
7179 return error_mark_node;
7182 fntype = build_method_type_directly (r, return_type,
7183 TREE_CHAIN (arg_types));
7185 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
7186 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
7191 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
7192 ARGS into that specification, and return the substituted
7193 specification. If there is no specification, return NULL_TREE. */
7196 tsubst_exception_specification (tree fntype,
7198 tsubst_flags_t complain,
7204 specs = TYPE_RAISES_EXCEPTIONS (fntype);
7205 new_specs = NULL_TREE;
7208 if (! TREE_VALUE (specs))
7214 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
7215 if (spec == error_mark_node)
7217 new_specs = add_exception_specifier (new_specs, spec, complain);
7218 specs = TREE_CHAIN (specs);
7224 /* Take the tree structure T and replace template parameters used
7225 therein with the argument vector ARGS. IN_DECL is an associated
7226 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
7227 Issue error and warning messages under control of COMPLAIN. Note
7228 that we must be relatively non-tolerant of extensions here, in
7229 order to preserve conformance; if we allow substitutions that
7230 should not be allowed, we may allow argument deductions that should
7231 not succeed, and therefore report ambiguous overload situations
7232 where there are none. In theory, we could allow the substitution,
7233 but indicate that it should have failed, and allow our caller to
7234 make sure that the right thing happens, but we don't try to do this
7237 This function is used for dealing with types, decls and the like;
7238 for expressions, use tsubst_expr or tsubst_copy. */
7241 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
7245 if (t == NULL_TREE || t == error_mark_node
7246 || t == integer_type_node
7247 || t == void_type_node
7248 || t == char_type_node
7249 || t == unknown_type_node
7250 || TREE_CODE (t) == NAMESPACE_DECL)
7254 return tsubst_decl (t, args, complain);
7256 if (TREE_CODE (t) == IDENTIFIER_NODE)
7257 type = IDENTIFIER_TYPE_VALUE (t);
7259 type = TREE_TYPE (t);
7261 gcc_assert (type != unknown_type_node);
7264 && TREE_CODE (t) != TYPENAME_TYPE
7265 && TREE_CODE (t) != IDENTIFIER_NODE
7266 && TREE_CODE (t) != FUNCTION_TYPE
7267 && TREE_CODE (t) != METHOD_TYPE)
7268 type = tsubst (type, args, complain, in_decl);
7269 if (type == error_mark_node)
7270 return error_mark_node;
7272 switch (TREE_CODE (t))
7277 return tsubst_aggr_type (t, args, complain, in_decl,
7278 /*entering_scope=*/0);
7281 case IDENTIFIER_NODE:
7293 if (t == integer_type_node)
7296 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
7297 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
7301 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7303 max = tsubst_expr (omax, args, complain, in_decl,
7304 /*integral_constant_expression_p=*/false);
7305 max = fold_decl_constant_value (max);
7307 if (TREE_CODE (max) != INTEGER_CST
7308 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
7309 && !at_function_scope_p ())
7311 if (complain & tf_error)
7312 error ("array bound is not an integer constant");
7313 return error_mark_node;
7318 Type deduction may fail for any of the following
7321 Attempting to create an array with a size that is
7322 zero or negative. */
7323 if (integer_zerop (max) && !(complain & tf_error))
7324 /* We must fail if performing argument deduction (as
7325 indicated by the state of complain), so that
7326 another substitution can be found. */
7327 return error_mark_node;
7328 else if (TREE_CODE (max) == INTEGER_CST
7329 && INT_CST_LT (max, integer_zero_node))
7331 if (complain & tf_error)
7332 error ("creating array with negative size (%qE)", max);
7334 return error_mark_node;
7337 return compute_array_index_type (NULL_TREE, max);
7340 case TEMPLATE_TYPE_PARM:
7341 case TEMPLATE_TEMPLATE_PARM:
7342 case BOUND_TEMPLATE_TEMPLATE_PARM:
7343 case TEMPLATE_PARM_INDEX:
7348 tree arg = NULL_TREE;
7352 gcc_assert (TREE_VEC_LENGTH (args) > 0);
7353 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7354 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7355 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7357 idx = TEMPLATE_TYPE_IDX (t);
7358 level = TEMPLATE_TYPE_LEVEL (t);
7362 idx = TEMPLATE_PARM_IDX (t);
7363 level = TEMPLATE_PARM_LEVEL (t);
7366 levels = TMPL_ARGS_DEPTH (args);
7367 if (level <= levels)
7368 arg = TMPL_ARG (args, level, idx);
7370 if (arg == error_mark_node)
7371 return error_mark_node;
7372 else if (arg != NULL_TREE)
7374 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
7377 gcc_assert (TYPE_P (arg));
7379 /* cv-quals from the template are discarded when
7380 substituting in a function or reference type. */
7381 if (TREE_CODE (arg) == FUNCTION_TYPE
7382 || TREE_CODE (arg) == METHOD_TYPE
7383 || TREE_CODE (arg) == REFERENCE_TYPE)
7384 quals = cp_type_quals (arg);
7386 quals = cp_type_quals (arg) | cp_type_quals (t);
7388 return cp_build_qualified_type_real
7389 (arg, quals, complain | tf_ignore_bad_quals);
7391 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7393 /* We are processing a type constructed from a
7394 template template parameter. */
7395 tree argvec = tsubst (TYPE_TI_ARGS (t),
7396 args, complain, in_decl);
7397 if (argvec == error_mark_node)
7398 return error_mark_node;
7400 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7401 are resolving nested-types in the signature of a
7402 member function templates. Otherwise ARG is a
7403 TEMPLATE_DECL and is the real template to be
7405 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7406 arg = TYPE_NAME (arg);
7408 r = lookup_template_class (arg,
7411 /*entering_scope=*/0,
7413 return cp_build_qualified_type_real
7414 (r, TYPE_QUALS (t), complain);
7417 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7422 /* This can happen during the attempted tsubst'ing in
7423 unify. This means that we don't yet have any information
7424 about the template parameter in question. */
7427 /* If we get here, we must have been looking at a parm for a
7428 more deeply nested template. Make a new version of this
7429 template parameter, but with a lower level. */
7430 switch (TREE_CODE (t))
7432 case TEMPLATE_TYPE_PARM:
7433 case TEMPLATE_TEMPLATE_PARM:
7434 case BOUND_TEMPLATE_TEMPLATE_PARM:
7435 if (cp_type_quals (t))
7437 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
7438 r = cp_build_qualified_type_real
7439 (r, cp_type_quals (t),
7440 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7441 ? tf_ignore_bad_quals : 0));
7446 TEMPLATE_TYPE_PARM_INDEX (r)
7447 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7449 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7450 TYPE_MAIN_VARIANT (r) = r;
7451 TYPE_POINTER_TO (r) = NULL_TREE;
7452 TYPE_REFERENCE_TO (r) = NULL_TREE;
7454 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7456 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
7458 if (argvec == error_mark_node)
7459 return error_mark_node;
7461 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7462 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7467 case TEMPLATE_PARM_INDEX:
7468 r = reduce_template_parm_level (t, type, levels);
7480 tree purpose, value, chain;
7482 if (t == void_list_node)
7485 purpose = TREE_PURPOSE (t);
7488 purpose = tsubst (purpose, args, complain, in_decl);
7489 if (purpose == error_mark_node)
7490 return error_mark_node;
7492 value = TREE_VALUE (t);
7495 value = tsubst (value, args, complain, in_decl);
7496 if (value == error_mark_node)
7497 return error_mark_node;
7499 chain = TREE_CHAIN (t);
7500 if (chain && chain != void_type_node)
7502 chain = tsubst (chain, args, complain, in_decl);
7503 if (chain == error_mark_node)
7504 return error_mark_node;
7506 if (purpose == TREE_PURPOSE (t)
7507 && value == TREE_VALUE (t)
7508 && chain == TREE_CHAIN (t))
7510 return hash_tree_cons (purpose, value, chain);
7514 /* We should never be tsubsting a binfo. */
7518 /* A vector of template arguments. */
7520 return tsubst_template_args (t, args, complain, in_decl);
7523 case REFERENCE_TYPE:
7525 enum tree_code code;
7527 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
7530 code = TREE_CODE (t);
7535 Type deduction may fail for any of the following
7538 -- Attempting to create a pointer to reference type.
7539 -- Attempting to create a reference to a reference type or
7540 a reference to void. */
7541 if (TREE_CODE (type) == REFERENCE_TYPE
7542 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
7544 static location_t last_loc;
7546 /* We keep track of the last time we issued this error
7547 message to avoid spewing a ton of messages during a
7548 single bad template instantiation. */
7549 if (complain & tf_error
7550 #ifdef USE_MAPPED_LOCATION
7551 && last_loc != input_location
7553 && (last_loc.line != input_line
7554 || last_loc.file != input_filename)
7558 if (TREE_CODE (type) == VOID_TYPE)
7559 error ("forming reference to void");
7561 error ("forming %s to reference type %qT",
7562 (code == POINTER_TYPE) ? "pointer" : "reference",
7564 last_loc = input_location;
7567 return error_mark_node;
7569 else if (code == POINTER_TYPE)
7571 r = build_pointer_type (type);
7572 if (TREE_CODE (type) == METHOD_TYPE)
7573 r = build_ptrmemfunc_type (r);
7576 r = build_reference_type (type);
7577 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
7579 if (r != error_mark_node)
7580 /* Will this ever be needed for TYPE_..._TO values? */
7587 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7588 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7592 Type deduction may fail for any of the following
7595 -- Attempting to create "pointer to member of T" when T
7596 is not a class type. */
7597 if (complain & tf_error)
7598 error ("creating pointer to member of non-class type %qT", r);
7599 return error_mark_node;
7601 if (TREE_CODE (type) == REFERENCE_TYPE)
7603 if (complain & tf_error)
7604 error ("creating pointer to member reference type %qT", type);
7605 return error_mark_node;
7607 if (TREE_CODE (type) == VOID_TYPE)
7609 if (complain & tf_error)
7610 error ("creating pointer to member of type void");
7611 return error_mark_node;
7613 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
7614 if (TREE_CODE (type) == FUNCTION_TYPE)
7616 /* The type of the implicit object parameter gets its
7617 cv-qualifiers from the FUNCTION_TYPE. */
7619 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7620 cp_type_quals (type));
7622 method_type = build_method_type_directly (this_type,
7624 TYPE_ARG_TYPES (type));
7625 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7626 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7630 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7639 fntype = tsubst_function_type (t, args, complain, in_decl);
7640 if (fntype == error_mark_node)
7641 return error_mark_node;
7643 /* Substitute the exception specification. */
7644 specs = tsubst_exception_specification (t, args, complain,
7646 if (specs == error_mark_node)
7647 return error_mark_node;
7649 fntype = build_exception_variant (fntype, specs);
7654 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7655 if (domain == error_mark_node)
7656 return error_mark_node;
7658 /* As an optimization, we avoid regenerating the array type if
7659 it will obviously be the same as T. */
7660 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7663 /* These checks should match the ones in grokdeclarator.
7667 The deduction may fail for any of the following reasons:
7669 -- Attempting to create an array with an element type that
7670 is void, a function type, or a reference type, or [DR337]
7671 an abstract class type. */
7672 if (TREE_CODE (type) == VOID_TYPE
7673 || TREE_CODE (type) == FUNCTION_TYPE
7674 || TREE_CODE (type) == REFERENCE_TYPE)
7676 if (complain & tf_error)
7677 error ("creating array of %qT", type);
7678 return error_mark_node;
7680 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7682 if (complain & tf_error)
7683 error ("creating array of %qT, which is an abstract class type",
7685 return error_mark_node;
7688 r = build_cplus_array_type (type, domain);
7695 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7696 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7698 if (e1 == error_mark_node || e2 == error_mark_node)
7699 return error_mark_node;
7701 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
7707 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7708 if (e == error_mark_node)
7709 return error_mark_node;
7711 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
7716 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7717 in_decl, /*entering_scope=*/1);
7718 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
7721 if (ctx == error_mark_node || f == error_mark_node)
7722 return error_mark_node;
7724 if (!IS_AGGR_TYPE (ctx))
7726 if (complain & tf_error)
7727 error ("%qT is not a class, struct, or union type", ctx);
7728 return error_mark_node;
7730 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7732 /* Normally, make_typename_type does not require that the CTX
7733 have complete type in order to allow things like:
7735 template <class T> struct S { typename S<T>::X Y; };
7737 But, such constructs have already been resolved by this
7738 point, so here CTX really should have complete type, unless
7739 it's a partial instantiation. */
7740 ctx = complete_type (ctx);
7741 if (!COMPLETE_TYPE_P (ctx))
7743 if (complain & tf_error)
7744 cxx_incomplete_type_error (NULL_TREE, ctx);
7745 return error_mark_node;
7749 f = make_typename_type (ctx, f, typename_type,
7750 (complain & tf_error) | tf_keep_type_decl);
7751 if (f == error_mark_node)
7753 if (TREE_CODE (f) == TYPE_DECL)
7755 complain |= tf_ignore_bad_quals;
7759 if (TREE_CODE (f) != TYPENAME_TYPE)
7761 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
7762 error ("%qT resolves to %qT, which is not an enumeration type",
7764 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
7765 error ("%qT resolves to %qT, which is is not a class type",
7769 return cp_build_qualified_type_real
7770 (f, cp_type_quals (f) | cp_type_quals (t), complain);
7773 case UNBOUND_CLASS_TEMPLATE:
7775 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7776 in_decl, /*entering_scope=*/1);
7777 tree name = TYPE_IDENTIFIER (t);
7778 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
7780 if (ctx == error_mark_node || name == error_mark_node)
7781 return error_mark_node;
7784 parm_list = tsubst_template_parms (parm_list, args, complain);
7785 return make_unbound_class_template (ctx, name, parm_list, complain);
7795 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7796 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
7797 /*integral_constant_expression_p=*/false);
7798 if (e1 == error_mark_node || e2 == error_mark_node)
7799 return error_mark_node;
7801 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
7806 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7807 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7808 if (e1 == error_mark_node || e2 == error_mark_node)
7809 return error_mark_node;
7811 return build_qualified_name (/*type=*/NULL_TREE,
7812 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
7819 type = finish_typeof (tsubst_expr
7820 (TYPEOF_TYPE_EXPR (t), args,
7822 /*integral_constant_expression_p=*/false));
7823 return cp_build_qualified_type_real (type,
7825 | cp_type_quals (type),
7830 sorry ("use of %qs in template",
7831 tree_code_name [(int) TREE_CODE (t)]);
7832 return error_mark_node;
7836 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7837 type of the expression on the left-hand side of the "." or "->"
7841 tsubst_baselink (tree baselink, tree object_type,
7842 tree args, tsubst_flags_t complain, tree in_decl)
7845 tree qualifying_scope;
7848 tree template_args = 0;
7849 bool template_id_p = false;
7851 /* A baselink indicates a function from a base class. Both the
7852 BASELINK_ACCESS_BINFO and the base class referenced may
7853 indicate bases of the template class, rather than the
7854 instantiated class. In addition, lookups that were not
7855 ambiguous before may be ambiguous now. Therefore, we perform
7856 the lookup again. */
7857 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7858 qualifying_scope = tsubst (qualifying_scope, args,
7860 fns = BASELINK_FUNCTIONS (baselink);
7861 optype = BASELINK_OPTYPE (baselink);
7862 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7864 template_id_p = true;
7865 template_args = TREE_OPERAND (fns, 1);
7866 fns = TREE_OPERAND (fns, 0);
7868 template_args = tsubst_template_args (template_args, args,
7871 name = DECL_NAME (get_first_fn (fns));
7872 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7874 /* If lookup found a single function, mark it as used at this
7875 point. (If it lookup found multiple functions the one selected
7876 later by overload resolution will be marked as used at that
7878 if (BASELINK_P (baselink))
7879 fns = BASELINK_FUNCTIONS (baselink);
7880 if (!template_id_p && !really_overloaded_fn (fns))
7881 mark_used (OVL_CURRENT (fns));
7883 /* Add back the template arguments, if present. */
7884 if (BASELINK_P (baselink) && template_id_p)
7885 BASELINK_FUNCTIONS (baselink)
7886 = build_nt (TEMPLATE_ID_EXPR,
7887 BASELINK_FUNCTIONS (baselink),
7889 /* Update the conversion operator type. */
7890 BASELINK_OPTYPE (baselink)
7891 = tsubst (optype, args, complain, in_decl);
7894 object_type = current_class_type;
7895 return adjust_result_of_qualified_name_lookup (baselink,
7900 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7901 true if the qualified-id will be a postfix-expression in-and-of
7902 itself; false if more of the postfix-expression follows the
7903 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7907 tsubst_qualified_id (tree qualified_id, tree args,
7908 tsubst_flags_t complain, tree in_decl,
7909 bool done, bool address_p)
7917 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
7919 /* Figure out what name to look up. */
7920 name = TREE_OPERAND (qualified_id, 1);
7921 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7924 template_args = TREE_OPERAND (name, 1);
7926 template_args = tsubst_template_args (template_args, args,
7928 name = TREE_OPERAND (name, 0);
7932 is_template = false;
7933 template_args = NULL_TREE;
7936 /* Substitute into the qualifying scope. When there are no ARGS, we
7937 are just trying to simplify a non-dependent expression. In that
7938 case the qualifying scope may be dependent, and, in any case,
7939 substituting will not help. */
7940 scope = TREE_OPERAND (qualified_id, 0);
7943 scope = tsubst (scope, args, complain, in_decl);
7944 expr = tsubst_copy (name, args, complain, in_decl);
7949 if (dependent_type_p (scope))
7950 return build_qualified_name (/*type=*/NULL_TREE,
7952 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
7954 if (!BASELINK_P (name) && !DECL_P (expr))
7956 if (TREE_CODE (expr) == BIT_NOT_EXPR)
7957 /* If this were actually a destructor call, it would have been
7958 parsed as such by the parser. */
7959 expr = error_mark_node;
7961 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
7962 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7963 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7965 if (complain & tf_error)
7967 error ("dependent-name %qE is parsed as a non-type, but "
7968 "instantiation yields a type", qualified_id);
7969 inform ("say %<typename %E%> if a type is meant", qualified_id);
7971 return error_mark_node;
7977 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7979 /* Remember that there was a reference to this entity. */
7983 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7985 if (complain & tf_error)
7986 qualified_name_lookup_error (scope,
7987 TREE_OPERAND (qualified_id, 1),
7989 return error_mark_node;
7993 expr = lookup_template_function (expr, template_args);
7995 if (expr == error_mark_node && complain & tf_error)
7996 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7998 else if (TYPE_P (scope))
8000 expr = (adjust_result_of_qualified_name_lookup
8001 (expr, scope, current_class_type));
8002 expr = (finish_qualified_id_expr
8003 (scope, expr, done, address_p,
8004 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
8005 /*template_arg_p=*/false));
8008 /* Expressions do not generally have reference type. */
8009 if (TREE_CODE (expr) != SCOPE_REF
8010 /* However, if we're about to form a pointer-to-member, we just
8011 want the referenced member referenced. */
8012 && TREE_CODE (expr) != OFFSET_REF)
8013 expr = convert_from_reference (expr);
8018 /* Like tsubst, but deals with expressions. This function just replaces
8019 template parms; to finish processing the resultant expression, use
8023 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
8025 enum tree_code code;
8028 if (t == NULL_TREE || t == error_mark_node)
8031 code = TREE_CODE (t);
8036 r = retrieve_local_specialization (t);
8037 gcc_assert (r != NULL);
8046 if (DECL_TEMPLATE_PARM_P (t))
8047 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
8048 /* There is no need to substitute into namespace-scope
8050 if (DECL_NAMESPACE_SCOPE_P (t))
8052 /* If ARGS is NULL, then T is known to be non-dependent. */
8053 if (args == NULL_TREE)
8054 return integral_constant_value (t);
8056 /* Unfortunately, we cannot just call lookup_name here.
8059 template <int I> int f() {
8061 struct S { void g() { E e = a; } };
8064 When we instantiate f<7>::S::g(), say, lookup_name is not
8065 clever enough to find f<7>::a. */
8067 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
8068 /*entering_scope=*/0);
8070 for (v = TYPE_VALUES (enum_type);
8073 if (TREE_PURPOSE (v) == DECL_NAME (t))
8074 return TREE_VALUE (v);
8076 /* We didn't find the name. That should never happen; if
8077 name-lookup found it during preliminary parsing, we
8078 should find it again here during instantiation. */
8084 if (DECL_CONTEXT (t))
8088 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
8089 /*entering_scope=*/1);
8090 if (ctx != DECL_CONTEXT (t))
8092 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
8095 if (complain & tf_error)
8096 error ("using invalid field %qD", t);
8097 return error_mark_node;
8107 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8108 || local_variable_p (t))
8109 t = tsubst (t, args, complain, in_decl);
8114 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
8117 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
8118 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
8119 args, complain, in_decl);
8120 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
8121 return tsubst (t, args, complain, in_decl);
8122 else if (DECL_CLASS_SCOPE_P (t)
8123 && uses_template_parms (DECL_CONTEXT (t)))
8125 /* Template template argument like the following example need
8128 template <template <class> class TT> struct C {};
8129 template <class T> struct D {
8130 template <class U> struct E {};
8135 We are processing the template argument `E' in #1 for
8136 the template instantiation #2. Originally, `E' is a
8137 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
8138 have to substitute this with one having context `D<int>'. */
8140 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
8141 return lookup_field (context, DECL_NAME(t), 0, false);
8144 /* Ordinary template template argument. */
8148 case REINTERPRET_CAST_EXPR:
8149 case CONST_CAST_EXPR:
8150 case STATIC_CAST_EXPR:
8151 case DYNAMIC_CAST_EXPR:
8154 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8155 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
8159 case TRUTH_NOT_EXPR:
8162 case UNARY_PLUS_EXPR: /* Unary + */
8171 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8172 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
8179 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8180 name = TREE_OPERAND (t, 1);
8181 if (TREE_CODE (name) == BIT_NOT_EXPR)
8183 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8185 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8187 else if (TREE_CODE (name) == SCOPE_REF
8188 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
8190 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
8192 name = TREE_OPERAND (name, 1);
8193 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8195 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8196 name = build_qualified_name (/*type=*/NULL_TREE,
8198 /*template_p=*/false);
8200 else if (TREE_CODE (name) == BASELINK)
8201 name = tsubst_baselink (name,
8202 non_reference (TREE_TYPE (object)),
8206 name = tsubst_copy (name, args, complain, in_decl);
8207 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
8213 case TRUNC_DIV_EXPR:
8215 case FLOOR_DIV_EXPR:
8216 case ROUND_DIV_EXPR:
8217 case EXACT_DIV_EXPR:
8221 case TRUNC_MOD_EXPR:
8222 case FLOOR_MOD_EXPR:
8223 case TRUTH_ANDIF_EXPR:
8224 case TRUTH_ORIF_EXPR:
8225 case TRUTH_AND_EXPR:
8242 case PREDECREMENT_EXPR:
8243 case PREINCREMENT_EXPR:
8244 case POSTDECREMENT_EXPR:
8245 case POSTINCREMENT_EXPR:
8247 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8248 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8251 return build_qualified_name (/*type=*/NULL_TREE,
8252 tsubst_copy (TREE_OPERAND (t, 0),
8253 args, complain, in_decl),
8254 tsubst_copy (TREE_OPERAND (t, 1),
8255 args, complain, in_decl),
8256 QUALIFIED_NAME_IS_TEMPLATE (t));
8261 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8262 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8263 NULL_TREE, NULL_TREE);
8266 return build_nt (code,
8267 tsubst_copy (TREE_OPERAND (t, 0), args,
8269 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
8275 case PSEUDO_DTOR_EXPR:
8278 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8279 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8280 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
8281 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
8288 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8289 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8290 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
8291 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
8298 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8299 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8300 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
8301 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
8305 case TEMPLATE_ID_EXPR:
8307 /* Substituted template arguments */
8308 tree fn = TREE_OPERAND (t, 0);
8309 tree targs = TREE_OPERAND (t, 1);
8311 fn = tsubst_copy (fn, args, complain, in_decl);
8313 targs = tsubst_template_args (targs, args, complain, in_decl);
8315 return lookup_template_function (fn, targs);
8320 tree purpose, value, chain;
8322 if (t == void_list_node)
8325 purpose = TREE_PURPOSE (t);
8327 purpose = tsubst_copy (purpose, args, complain, in_decl);
8328 value = TREE_VALUE (t);
8330 value = tsubst_copy (value, args, complain, in_decl);
8331 chain = TREE_CHAIN (t);
8332 if (chain && chain != void_type_node)
8333 chain = tsubst_copy (chain, args, complain, in_decl);
8334 if (purpose == TREE_PURPOSE (t)
8335 && value == TREE_VALUE (t)
8336 && chain == TREE_CHAIN (t))
8338 return tree_cons (purpose, value, chain);
8345 case TEMPLATE_TYPE_PARM:
8346 case TEMPLATE_TEMPLATE_PARM:
8347 case BOUND_TEMPLATE_TEMPLATE_PARM:
8348 case TEMPLATE_PARM_INDEX:
8350 case REFERENCE_TYPE:
8356 case UNBOUND_CLASS_TEMPLATE:
8359 return tsubst (t, args, complain, in_decl);
8361 case IDENTIFIER_NODE:
8362 if (IDENTIFIER_TYPENAME_P (t))
8364 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8365 return mangle_conv_op_name_for_type (new_type);
8371 /* This is handled by tsubst_copy_and_build. */
8375 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
8377 tsubst (TREE_TYPE (t), args, complain, in_decl));
8379 case CLEANUP_POINT_EXPR:
8380 /* We shouldn't have built any of these during initial template
8381 generation. Instead, they should be built during instantiation
8382 in response to the saved STMT_IS_FULL_EXPR_P setting. */
8386 mark_used (TREE_OPERAND (t, 1));
8394 /* Like tsubst_copy, but specifically for OpenMP clauses. */
8397 tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
8400 tree new_clauses = NULL, nc, oc;
8402 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
8404 nc = copy_node (oc);
8405 OMP_CLAUSE_CHAIN (nc) = new_clauses;
8408 switch (OMP_CLAUSE_CODE (nc))
8410 case OMP_CLAUSE_PRIVATE:
8411 case OMP_CLAUSE_SHARED:
8412 case OMP_CLAUSE_FIRSTPRIVATE:
8413 case OMP_CLAUSE_LASTPRIVATE:
8414 case OMP_CLAUSE_REDUCTION:
8415 case OMP_CLAUSE_COPYIN:
8416 case OMP_CLAUSE_COPYPRIVATE:
8418 case OMP_CLAUSE_NUM_THREADS:
8419 case OMP_CLAUSE_SCHEDULE:
8420 OMP_CLAUSE_OPERAND (nc, 0)
8421 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
8422 in_decl, /*integral_constant_expression_p=*/false);
8424 case OMP_CLAUSE_NOWAIT:
8425 case OMP_CLAUSE_ORDERED:
8426 case OMP_CLAUSE_DEFAULT:
8433 return finish_omp_clauses (nreverse (new_clauses));
8436 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8439 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8442 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8444 tree purpose, value, chain;
8449 if (TREE_CODE (t) != TREE_LIST)
8450 return tsubst_copy_and_build (t, args, complain, in_decl,
8451 /*function_p=*/false,
8452 /*integral_constant_expression_p=*/false);
8454 if (t == void_list_node)
8457 purpose = TREE_PURPOSE (t);
8459 purpose = RECUR (purpose);
8460 value = TREE_VALUE (t);
8462 value = RECUR (value);
8463 chain = TREE_CHAIN (t);
8464 if (chain && chain != void_type_node)
8465 chain = RECUR (chain);
8466 return tree_cons (purpose, value, chain);
8470 /* Like tsubst_copy for expressions, etc. but also does semantic
8474 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
8475 bool integral_constant_expression_p)
8477 #define RECUR(NODE) \
8478 tsubst_expr ((NODE), args, complain, in_decl, \
8479 integral_constant_expression_p)
8483 if (t == NULL_TREE || t == error_mark_node)
8486 if (EXPR_HAS_LOCATION (t))
8487 input_location = EXPR_LOCATION (t);
8488 if (STATEMENT_CODE_P (TREE_CODE (t)))
8489 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8491 switch (TREE_CODE (t))
8493 case STATEMENT_LIST:
8495 tree_stmt_iterator i;
8496 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
8497 RECUR (tsi_stmt (i));
8501 case CTOR_INITIALIZER:
8502 finish_mem_initializers (tsubst_initializer_list
8503 (TREE_OPERAND (t, 0), args));
8507 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
8511 tmp = RECUR (EXPR_STMT_EXPR (t));
8512 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8513 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8515 finish_expr_stmt (tmp);
8519 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
8527 decl = DECL_EXPR_DECL (t);
8528 if (TREE_CODE (decl) == LABEL_DECL)
8529 finish_label_decl (DECL_NAME (decl));
8530 else if (TREE_CODE (decl) == USING_DECL)
8532 tree scope = USING_DECL_SCOPE (decl);
8533 tree name = DECL_NAME (decl);
8536 scope = RECUR (scope);
8537 decl = lookup_qualified_name (scope, name,
8538 /*is_type_p=*/false,
8539 /*complain=*/false);
8540 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8541 qualified_name_lookup_error (scope, name, decl);
8543 do_local_using_decl (decl, scope, name);
8547 init = DECL_INITIAL (decl);
8548 decl = tsubst (decl, args, complain, in_decl);
8549 if (decl != error_mark_node)
8551 /* By marking the declaration as instantiated, we avoid
8552 trying to instantiate it. Since instantiate_decl can't
8553 handle local variables, and since we've already done
8554 all that needs to be done, that's the right thing to
8556 if (TREE_CODE (decl) == VAR_DECL)
8557 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8558 if (TREE_CODE (decl) == VAR_DECL
8559 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8560 /* Anonymous aggregates are a special case. */
8561 finish_anon_union (decl);
8564 maybe_push_decl (decl);
8565 if (TREE_CODE (decl) == VAR_DECL
8566 && DECL_PRETTY_FUNCTION_P (decl))
8568 /* For __PRETTY_FUNCTION__ we have to adjust the
8570 const char *const name
8571 = cxx_printable_name (current_function_decl, 2);
8572 init = cp_fname_init (name, &TREE_TYPE (decl));
8575 init = RECUR (init);
8576 finish_decl (decl, init, NULL_TREE);
8581 /* A DECL_EXPR can also be used as an expression, in the condition
8582 clause of an if/for/while construct. */
8587 stmt = begin_for_stmt ();
8588 RECUR (FOR_INIT_STMT (t));
8589 finish_for_init_stmt (stmt);
8590 tmp = RECUR (FOR_COND (t));
8591 finish_for_cond (tmp, stmt);
8592 tmp = RECUR (FOR_EXPR (t));
8593 finish_for_expr (tmp, stmt);
8594 RECUR (FOR_BODY (t));
8595 finish_for_stmt (stmt);
8599 stmt = begin_while_stmt ();
8600 tmp = RECUR (WHILE_COND (t));
8601 finish_while_stmt_cond (tmp, stmt);
8602 RECUR (WHILE_BODY (t));
8603 finish_while_stmt (stmt);
8607 stmt = begin_do_stmt ();
8608 RECUR (DO_BODY (t));
8609 finish_do_body (stmt);
8610 tmp = RECUR (DO_COND (t));
8611 finish_do_stmt (tmp, stmt);
8615 stmt = begin_if_stmt ();
8616 tmp = RECUR (IF_COND (t));
8617 finish_if_stmt_cond (tmp, stmt);
8618 RECUR (THEN_CLAUSE (t));
8619 finish_then_clause (stmt);
8621 if (ELSE_CLAUSE (t))
8623 begin_else_clause (stmt);
8624 RECUR (ELSE_CLAUSE (t));
8625 finish_else_clause (stmt);
8628 finish_if_stmt (stmt);
8632 if (BIND_EXPR_BODY_BLOCK (t))
8633 stmt = begin_function_body ();
8635 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8636 ? BCS_TRY_BLOCK : 0);
8638 RECUR (BIND_EXPR_BODY (t));
8640 if (BIND_EXPR_BODY_BLOCK (t))
8641 finish_function_body (stmt);
8643 finish_compound_stmt (stmt);
8647 finish_break_stmt ();
8651 finish_continue_stmt ();
8655 stmt = begin_switch_stmt ();
8656 tmp = RECUR (SWITCH_STMT_COND (t));
8657 finish_switch_cond (tmp, stmt);
8658 RECUR (SWITCH_STMT_BODY (t));
8659 finish_switch_stmt (stmt);
8662 case CASE_LABEL_EXPR:
8663 finish_case_label (RECUR (CASE_LOW (t)),
8664 RECUR (CASE_HIGH (t)));
8668 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
8672 tmp = GOTO_DESTINATION (t);
8673 if (TREE_CODE (tmp) != LABEL_DECL)
8674 /* Computed goto's must be tsubst'd into. On the other hand,
8675 non-computed gotos must not be; the identifier in question
8676 will have no binding. */
8679 tmp = DECL_NAME (tmp);
8680 finish_goto_stmt (tmp);
8684 tmp = finish_asm_stmt
8685 (ASM_VOLATILE_P (t),
8686 RECUR (ASM_STRING (t)),
8687 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8688 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8689 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
8691 tree asm_expr = tmp;
8692 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8693 asm_expr = TREE_OPERAND (asm_expr, 0);
8694 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8701 stmt = begin_try_block ();
8702 RECUR (TRY_STMTS (t));
8703 finish_cleanup_try_block (stmt);
8704 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
8708 tree compound_stmt = NULL_TREE;
8710 if (FN_TRY_BLOCK_P (t))
8711 stmt = begin_function_try_block (&compound_stmt);
8713 stmt = begin_try_block ();
8715 RECUR (TRY_STMTS (t));
8717 if (FN_TRY_BLOCK_P (t))
8718 finish_function_try_block (stmt);
8720 finish_try_block (stmt);
8722 RECUR (TRY_HANDLERS (t));
8723 if (FN_TRY_BLOCK_P (t))
8724 finish_function_handler_sequence (stmt, compound_stmt);
8726 finish_handler_sequence (stmt);
8732 tree decl = HANDLER_PARMS (t);
8736 decl = tsubst (decl, args, complain, in_decl);
8737 /* Prevent instantiate_decl from trying to instantiate
8738 this variable. We've already done all that needs to be
8740 if (decl != error_mark_node)
8741 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
8743 stmt = begin_handler ();
8744 finish_handler_parms (decl, stmt);
8745 RECUR (HANDLER_BODY (t));
8746 finish_handler (stmt);
8751 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
8755 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
8756 args, complain, in_decl);
8757 stmt = begin_omp_parallel ();
8758 RECUR (OMP_PARALLEL_BODY (t));
8759 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
8760 = OMP_PARALLEL_COMBINED (t);
8765 tree clauses, decl, init, cond, incr, body, pre_body;
8767 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
8768 args, complain, in_decl);
8769 init = OMP_FOR_INIT (t);
8770 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
8771 decl = RECUR (TREE_OPERAND (init, 0));
8772 init = RECUR (TREE_OPERAND (init, 1));
8773 cond = RECUR (OMP_FOR_COND (t));
8774 incr = RECUR (OMP_FOR_INCR (t));
8776 stmt = begin_omp_structured_block ();
8778 pre_body = push_stmt_list ();
8779 RECUR (OMP_FOR_PRE_BODY (t));
8780 pre_body = pop_stmt_list (pre_body);
8782 body = push_stmt_list ();
8783 RECUR (OMP_FOR_BODY (t));
8784 body = pop_stmt_list (body);
8786 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
8789 OMP_FOR_CLAUSES (t) = clauses;
8791 add_stmt (finish_omp_structured_block (stmt));
8797 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
8798 stmt = push_stmt_list ();
8799 RECUR (OMP_BODY (t));
8800 stmt = pop_stmt_list (stmt);
8803 OMP_BODY (t) = stmt;
8804 OMP_CLAUSES (t) = tmp;
8812 stmt = push_stmt_list ();
8813 RECUR (OMP_BODY (t));
8814 stmt = pop_stmt_list (stmt);
8817 OMP_BODY (t) = stmt;
8824 op0 = RECUR (TREE_OPERAND (t, 0));
8825 op1 = RECUR (TREE_OPERAND (t, 1));
8826 finish_omp_atomic (OMP_ATOMIC_CODE (t), op0, op1);
8831 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
8833 return tsubst_copy_and_build (t, args, complain, in_decl,
8834 /*function_p=*/false,
8835 integral_constant_expression_p);
8842 /* T is a postfix-expression that is not being used in a function
8843 call. Return the substituted version of T. */
8846 tsubst_non_call_postfix_expression (tree t, tree args,
8847 tsubst_flags_t complain,
8850 if (TREE_CODE (t) == SCOPE_REF)
8851 t = tsubst_qualified_id (t, args, complain, in_decl,
8852 /*done=*/false, /*address_p=*/false);
8854 t = tsubst_copy_and_build (t, args, complain, in_decl,
8855 /*function_p=*/false,
8856 /*integral_constant_expression_p=*/false);
8861 /* Like tsubst but deals with expressions and performs semantic
8862 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8865 tsubst_copy_and_build (tree t,
8867 tsubst_flags_t complain,
8870 bool integral_constant_expression_p)
8872 #define RECUR(NODE) \
8873 tsubst_copy_and_build (NODE, args, complain, in_decl, \
8874 /*function_p=*/false, \
8875 integral_constant_expression_p)
8879 if (t == NULL_TREE || t == error_mark_node)
8882 switch (TREE_CODE (t))
8887 case IDENTIFIER_NODE:
8891 bool non_integral_constant_expression_p;
8892 const char *error_msg;
8894 if (IDENTIFIER_TYPENAME_P (t))
8896 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8897 t = mangle_conv_op_name_for_type (new_type);
8900 /* Look up the name. */
8901 decl = lookup_name (t);
8903 /* By convention, expressions use ERROR_MARK_NODE to indicate
8904 failure, not NULL_TREE. */
8905 if (decl == NULL_TREE)
8906 decl = error_mark_node;
8908 decl = finish_id_expression (t, decl, NULL_TREE,
8910 integral_constant_expression_p,
8911 /*allow_non_integral_constant_expression_p=*/false,
8912 &non_integral_constant_expression_p,
8913 /*template_p=*/false,
8915 /*address_p=*/false,
8916 /*template_arg_p=*/false,
8920 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8921 decl = unqualified_name_lookup_error (decl);
8925 case TEMPLATE_ID_EXPR:
8928 tree template = RECUR (TREE_OPERAND (t, 0));
8929 tree targs = TREE_OPERAND (t, 1);
8932 targs = tsubst_template_args (targs, args, complain, in_decl);
8934 if (TREE_CODE (template) == COMPONENT_REF)
8936 object = TREE_OPERAND (template, 0);
8937 template = TREE_OPERAND (template, 1);
8941 template = lookup_template_function (template, targs);
8944 return build3 (COMPONENT_REF, TREE_TYPE (template),
8945 object, template, NULL_TREE);
8947 return baselink_for_fns (template);
8952 tree r = RECUR (TREE_OPERAND (t, 0));
8954 if (REFERENCE_REF_P (t))
8956 /* A type conversion to reference type will be enclosed in
8957 such an indirect ref, but the substitution of the cast
8958 will have also added such an indirect ref. */
8959 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8960 r = convert_from_reference (r);
8963 r = build_x_indirect_ref (r, "unary *");
8969 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8970 RECUR (TREE_OPERAND (t, 0)));
8973 case REINTERPRET_CAST_EXPR:
8974 case CONST_CAST_EXPR:
8975 case DYNAMIC_CAST_EXPR:
8976 case STATIC_CAST_EXPR:
8981 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8982 if (integral_constant_expression_p
8983 && !cast_valid_in_integral_constant_expression_p (type))
8985 error ("a cast to a type other than an integral or "
8986 "enumeration type cannot appear in a constant-expression");
8987 return error_mark_node;
8990 op = RECUR (TREE_OPERAND (t, 0));
8992 switch (TREE_CODE (t))
8995 return build_functional_cast (type, op);
8996 case REINTERPRET_CAST_EXPR:
8997 return build_reinterpret_cast (type, op);
8998 case CONST_CAST_EXPR:
8999 return build_const_cast (type, op);
9000 case DYNAMIC_CAST_EXPR:
9001 return build_dynamic_cast (type, op);
9002 case STATIC_CAST_EXPR:
9003 return build_static_cast (type, op);
9009 case POSTDECREMENT_EXPR:
9010 case POSTINCREMENT_EXPR:
9011 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9012 args, complain, in_decl);
9013 return build_x_unary_op (TREE_CODE (t), op1);
9015 case PREDECREMENT_EXPR:
9016 case PREINCREMENT_EXPR:
9020 case TRUTH_NOT_EXPR:
9021 case UNARY_PLUS_EXPR: /* Unary + */
9024 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
9027 op1 = TREE_OPERAND (t, 0);
9028 if (TREE_CODE (op1) == SCOPE_REF)
9029 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
9030 /*done=*/true, /*address_p=*/true);
9032 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
9034 if (TREE_CODE (op1) == LABEL_DECL)
9035 return finish_label_address_expr (DECL_NAME (op1));
9036 return build_x_unary_op (ADDR_EXPR, op1);
9041 case TRUNC_DIV_EXPR:
9043 case FLOOR_DIV_EXPR:
9044 case ROUND_DIV_EXPR:
9045 case EXACT_DIV_EXPR:
9049 case TRUNC_MOD_EXPR:
9050 case FLOOR_MOD_EXPR:
9051 case TRUTH_ANDIF_EXPR:
9052 case TRUTH_ORIF_EXPR:
9053 case TRUTH_AND_EXPR:
9069 return build_x_binary_op
9071 RECUR (TREE_OPERAND (t, 0)),
9072 RECUR (TREE_OPERAND (t, 1)),
9073 /*overloaded_p=*/NULL);
9076 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
9077 /*address_p=*/false);
9079 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9080 args, complain, in_decl);
9081 return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
9082 /*overloaded_p=*/NULL);
9086 op1 = TREE_OPERAND (t, 0);
9089 /* When there are no ARGS, we are trying to evaluate a
9090 non-dependent expression from the parser. Trying to do
9091 the substitutions may not work. */
9093 op1 = TREE_TYPE (op1);
9098 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
9099 /*function_p=*/false,
9100 /*integral_constant_expression_p=*/false);
9104 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
9106 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
9110 tree r = build_x_modify_expr
9111 (RECUR (TREE_OPERAND (t, 0)),
9112 TREE_CODE (TREE_OPERAND (t, 1)),
9113 RECUR (TREE_OPERAND (t, 2)));
9114 /* TREE_NO_WARNING must be set if either the expression was
9115 parenthesized or it uses an operator such as >>= rather
9116 than plain assignment. In the former case, it was already
9117 set and must be copied. In the latter case,
9118 build_x_modify_expr sets it and it must not be reset
9120 if (TREE_NO_WARNING (t))
9121 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
9126 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9127 args, complain, in_decl);
9128 /* Remember that there was a reference to this entity. */
9131 return build_x_arrow (op1);
9135 (RECUR (TREE_OPERAND (t, 0)),
9136 RECUR (TREE_OPERAND (t, 1)),
9137 RECUR (TREE_OPERAND (t, 2)),
9138 RECUR (TREE_OPERAND (t, 3)),
9139 NEW_EXPR_USE_GLOBAL (t));
9142 return delete_sanity
9143 (RECUR (TREE_OPERAND (t, 0)),
9144 RECUR (TREE_OPERAND (t, 1)),
9145 DELETE_EXPR_USE_VEC (t),
9146 DELETE_EXPR_USE_GLOBAL (t));
9149 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
9150 RECUR (TREE_OPERAND (t, 1)));
9159 function = TREE_OPERAND (t, 0);
9160 /* When we parsed the expression, we determined whether or
9161 not Koenig lookup should be performed. */
9162 koenig_p = KOENIG_LOOKUP_P (t);
9163 if (TREE_CODE (function) == SCOPE_REF)
9166 function = tsubst_qualified_id (function, args, complain, in_decl,
9168 /*address_p=*/false);
9172 if (TREE_CODE (function) == COMPONENT_REF)
9174 tree op = TREE_OPERAND (function, 1);
9176 qualified_p = (TREE_CODE (op) == SCOPE_REF
9178 && BASELINK_QUALIFIED_P (op)));
9181 qualified_p = false;
9183 function = tsubst_copy_and_build (function, args, complain,
9186 integral_constant_expression_p);
9188 if (BASELINK_P (function))
9192 call_args = RECUR (TREE_OPERAND (t, 1));
9194 /* We do not perform argument-dependent lookup if normal
9195 lookup finds a non-function, in accordance with the
9196 expected resolution of DR 218. */
9198 && ((is_overloaded_fn (function)
9199 /* If lookup found a member function, the Koenig lookup is
9200 not appropriate, even if an unqualified-name was used
9201 to denote the function. */
9202 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
9203 || TREE_CODE (function) == IDENTIFIER_NODE))
9204 function = perform_koenig_lookup (function, call_args);
9206 if (TREE_CODE (function) == IDENTIFIER_NODE)
9208 unqualified_name_lookup_error (function);
9209 return error_mark_node;
9212 /* Remember that there was a reference to this entity. */
9213 if (DECL_P (function))
9214 mark_used (function);
9216 if (TREE_CODE (function) == OFFSET_REF)
9217 return build_offset_ref_call_from_tree (function, call_args);
9218 if (TREE_CODE (function) == COMPONENT_REF)
9220 if (!BASELINK_P (TREE_OPERAND (function, 1)))
9221 return finish_call_expr (function, call_args,
9222 /*disallow_virtual=*/false,
9223 /*koenig_p=*/false);
9225 return (build_new_method_call
9226 (TREE_OPERAND (function, 0),
9227 TREE_OPERAND (function, 1),
9228 call_args, NULL_TREE,
9229 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
9232 return finish_call_expr (function, call_args,
9233 /*disallow_virtual=*/qualified_p,
9238 return build_x_conditional_expr
9239 (RECUR (TREE_OPERAND (t, 0)),
9240 RECUR (TREE_OPERAND (t, 1)),
9241 RECUR (TREE_OPERAND (t, 2)));
9243 case PSEUDO_DTOR_EXPR:
9244 return finish_pseudo_destructor_expr
9245 (RECUR (TREE_OPERAND (t, 0)),
9246 RECUR (TREE_OPERAND (t, 1)),
9247 RECUR (TREE_OPERAND (t, 2)));
9251 tree purpose, value, chain;
9253 if (t == void_list_node)
9256 purpose = TREE_PURPOSE (t);
9258 purpose = RECUR (purpose);
9259 value = TREE_VALUE (t);
9261 value = RECUR (value);
9262 chain = TREE_CHAIN (t);
9263 if (chain && chain != void_type_node)
9264 chain = RECUR (chain);
9265 if (purpose == TREE_PURPOSE (t)
9266 && value == TREE_VALUE (t)
9267 && chain == TREE_CHAIN (t))
9269 return tree_cons (purpose, value, chain);
9278 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9279 args, complain, in_decl);
9280 /* Remember that there was a reference to this entity. */
9281 if (DECL_P (object))
9283 object_type = TREE_TYPE (object);
9285 member = TREE_OPERAND (t, 1);
9286 if (BASELINK_P (member))
9287 member = tsubst_baselink (member,
9288 non_reference (TREE_TYPE (object)),
9289 args, complain, in_decl);
9291 member = tsubst_copy (member, args, complain, in_decl);
9292 if (member == error_mark_node)
9293 return error_mark_node;
9295 if (object_type && !CLASS_TYPE_P (object_type))
9297 if (TREE_CODE (member) == BIT_NOT_EXPR)
9298 return finish_pseudo_destructor_expr (object,
9301 else if (TREE_CODE (member) == SCOPE_REF
9302 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
9303 return finish_pseudo_destructor_expr (object,
9307 else if (TREE_CODE (member) == SCOPE_REF
9308 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
9313 /* Lookup the template functions now that we know what the
9315 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
9316 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
9317 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
9318 /*is_type_p=*/false,
9319 /*complain=*/false);
9320 if (BASELINK_P (member))
9322 BASELINK_FUNCTIONS (member)
9323 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
9325 member = (adjust_result_of_qualified_name_lookup
9326 (member, BINFO_TYPE (BASELINK_BINFO (member)),
9331 qualified_name_lookup_error (object_type, tmpl, member);
9332 return error_mark_node;
9335 else if (TREE_CODE (member) == SCOPE_REF
9336 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
9337 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
9339 if (complain & tf_error)
9341 if (TYPE_P (TREE_OPERAND (member, 0)))
9342 error ("%qT is not a class or namespace",
9343 TREE_OPERAND (member, 0));
9345 error ("%qD is not a class or namespace",
9346 TREE_OPERAND (member, 0));
9348 return error_mark_node;
9350 else if (TREE_CODE (member) == FIELD_DECL)
9351 return finish_non_static_data_member (member, object, NULL_TREE);
9353 return finish_class_member_access_expr (object, member,
9354 /*template_p=*/false);
9359 (RECUR (TREE_OPERAND (t, 0)));
9363 VEC(constructor_elt,gc) *n;
9364 constructor_elt *ce;
9365 unsigned HOST_WIDE_INT idx;
9366 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9367 bool process_index_p;
9369 if (type == error_mark_node)
9370 return error_mark_node;
9372 /* digest_init will do the wrong thing if we let it. */
9373 if (type && TYPE_PTRMEMFUNC_P (type))
9376 /* We do not want to process the index of aggregate
9377 initializers as they are identifier nodes which will be
9378 looked up by digest_init. */
9379 process_index_p = !(type && IS_AGGR_TYPE (type));
9381 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
9382 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
9384 if (ce->index && process_index_p)
9385 ce->index = RECUR (ce->index);
9386 ce->value = RECUR (ce->value);
9389 if (TREE_HAS_CONSTRUCTOR (t))
9390 return finish_compound_literal (type, n);
9392 return build_constructor (NULL_TREE, n);
9397 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
9398 if (TYPE_P (operand_0))
9399 return get_typeid (operand_0);
9400 return build_typeid (operand_0);
9410 tree r = tsubst_copy (t, args, complain, in_decl);
9412 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
9413 /* If the original type was a reference, we'll be wrapped in
9414 the appropriate INDIRECT_REF. */
9415 r = convert_from_reference (r);
9420 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
9421 tsubst_copy (TREE_TYPE (t), args, complain,
9425 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
9429 tree old_stmt_expr = cur_stmt_expr;
9430 tree stmt_expr = begin_stmt_expr ();
9432 cur_stmt_expr = stmt_expr;
9433 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
9434 integral_constant_expression_p);
9435 stmt_expr = finish_stmt_expr (stmt_expr, false);
9436 cur_stmt_expr = old_stmt_expr;
9442 t = tsubst_copy (t, args, complain, in_decl);
9443 /* As in finish_id_expression, we resolve enumeration constants
9444 to their underlying values. */
9445 if (TREE_CODE (t) == CONST_DECL)
9447 used_types_insert (TREE_TYPE (t));
9448 return DECL_INITIAL (t);
9453 /* Handle Objective-C++ constructs, if appropriate. */
9456 = objcp_tsubst_copy_and_build (t, args, complain,
9457 in_decl, /*function_p=*/false);
9461 return tsubst_copy (t, args, complain, in_decl);
9467 /* Verify that the instantiated ARGS are valid. For type arguments,
9468 make sure that the type's linkage is ok. For non-type arguments,
9469 make sure they are constants if they are integral or enumerations.
9470 Emit an error under control of COMPLAIN, and return TRUE on error. */
9473 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
9475 int ix, len = DECL_NTPARMS (tmpl);
9476 bool result = false;
9478 for (ix = 0; ix != len; ix++)
9480 tree t = TREE_VEC_ELT (args, ix);
9484 /* [basic.link]: A name with no linkage (notably, the name
9485 of a class or enumeration declared in a local scope)
9486 shall not be used to declare an entity with linkage.
9487 This implies that names with no linkage cannot be used as
9488 template arguments. */
9489 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
9493 /* DR 488 makes use of a type with no linkage cause
9494 type deduction to fail. */
9495 if (complain & tf_error)
9497 if (TYPE_ANONYMOUS_P (nt))
9498 error ("%qT is/uses anonymous type", t);
9500 error ("template argument for %qD uses local type %qT",
9505 /* In order to avoid all sorts of complications, we do not
9506 allow variably-modified types as template arguments. */
9507 else if (variably_modified_type_p (t, NULL_TREE))
9509 if (complain & tf_error)
9510 error ("%qT is a variably modified type", t);
9514 /* A non-type argument of integral or enumerated type must be a
9516 else if (TREE_TYPE (t)
9517 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9518 && !TREE_CONSTANT (t))
9520 if (complain & tf_error)
9521 error ("integral expression %qE is not constant", t);
9525 if (result && (complain & tf_error))
9526 error (" trying to instantiate %qD", tmpl);
9530 /* Instantiate the indicated variable or function template TMPL with
9531 the template arguments in TARG_PTR. */
9534 instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
9539 HOST_WIDE_INT saved_processing_template_decl;
9541 if (tmpl == error_mark_node)
9542 return error_mark_node;
9544 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
9546 /* If this function is a clone, handle it specially. */
9547 if (DECL_CLONED_FUNCTION_P (tmpl))
9552 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9554 if (spec == error_mark_node)
9555 return error_mark_node;
9557 /* Look for the clone. */
9558 FOR_EACH_CLONE (clone, spec)
9559 if (DECL_NAME (clone) == DECL_NAME (tmpl))
9561 /* We should always have found the clone by now. */
9566 /* Check to see if we already have this specialization. */
9567 spec = retrieve_specialization (tmpl, targ_ptr,
9568 /*class_specializations_p=*/false);
9569 if (spec != NULL_TREE)
9572 gen_tmpl = most_general_template (tmpl);
9573 if (tmpl != gen_tmpl)
9575 /* The TMPL is a partial instantiation. To get a full set of
9576 arguments we must add the arguments used to perform the
9577 partial instantiation. */
9578 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
9581 /* Check to see if we already have this specialization. */
9582 spec = retrieve_specialization (gen_tmpl, targ_ptr,
9583 /*class_specializations_p=*/false);
9584 if (spec != NULL_TREE)
9588 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9590 return error_mark_node;
9592 /* We are building a FUNCTION_DECL, during which the access of its
9593 parameters and return types have to be checked. However this
9594 FUNCTION_DECL which is the desired context for access checking
9595 is not built yet. We solve this chicken-and-egg problem by
9596 deferring all checks until we have the FUNCTION_DECL. */
9597 push_deferring_access_checks (dk_deferred);
9599 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
9600 (because, for example, we have encountered a non-dependent
9601 function call in the body of a template function and must now
9602 determine which of several overloaded functions will be called),
9603 within the instantiation itself we are not processing a
9605 saved_processing_template_decl = processing_template_decl;
9606 processing_template_decl = 0;
9607 /* Substitute template parameters to obtain the specialization. */
9608 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
9609 targ_ptr, complain, gen_tmpl);
9610 processing_template_decl = saved_processing_template_decl;
9611 if (fndecl == error_mark_node)
9612 return error_mark_node;
9614 /* Now we know the specialization, compute access previously
9616 push_access_scope (fndecl);
9617 perform_deferred_access_checks ();
9618 pop_access_scope (fndecl);
9619 pop_deferring_access_checks ();
9621 /* The DECL_TI_TEMPLATE should always be the immediate parent
9622 template, not the most general template. */
9623 DECL_TI_TEMPLATE (fndecl) = tmpl;
9625 /* If we've just instantiated the main entry point for a function,
9626 instantiate all the alternate entry points as well. We do this
9627 by cloning the instantiation of the main entry point, not by
9628 instantiating the template clones. */
9629 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9630 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
9635 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9636 arguments that are being used when calling it. TARGS is a vector
9637 into which the deduced template arguments are placed.
9639 Return zero for success, 2 for an incomplete match that doesn't resolve
9640 all the types, and 1 for complete failure. An error message will be
9641 printed only for an incomplete match.
9643 If FN is a conversion operator, or we are trying to produce a specific
9644 specialization, RETURN_TYPE is the return type desired.
9646 The EXPLICIT_TARGS are explicit template arguments provided via a
9649 The parameter STRICT is one of:
9652 We are deducing arguments for a function call, as in
9656 We are deducing arguments for a conversion function, as in
9660 We are deducing arguments when doing an explicit instantiation
9661 as in [temp.explicit], when determining an explicit specialization
9662 as in [temp.expl.spec], or when taking the address of a function
9663 template, as in [temp.deduct.funcaddr]. */
9666 fn_type_unification (tree fn,
9667 tree explicit_targs,
9671 unification_kind_t strict,
9678 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
9680 fntype = TREE_TYPE (fn);
9685 The specified template arguments must match the template
9686 parameters in kind (i.e., type, nontype, template), and there
9687 must not be more arguments than there are parameters;
9688 otherwise type deduction fails.
9690 Nontype arguments must match the types of the corresponding
9691 nontype template parameters, or must be convertible to the
9692 types of the corresponding nontype parameters as specified in
9693 _temp.arg.nontype_, otherwise type deduction fails.
9695 All references in the function type of the function template
9696 to the corresponding template parameters are replaced by the
9697 specified template argument values. If a substitution in a
9698 template parameter or in the function type of the function
9699 template results in an invalid type, type deduction fails. */
9701 tree converted_args;
9704 if (explicit_targs == error_mark_node)
9708 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9709 explicit_targs, NULL_TREE, tf_none,
9710 /*require_all_args=*/false,
9711 /*use_default_args=*/false));
9712 if (converted_args == error_mark_node)
9715 /* Substitute the explicit args into the function type. This is
9716 necessary so that, for instance, explicitly declared function
9717 arguments can match null pointed constants. If we were given
9718 an incomplete set of explicit args, we must not do semantic
9719 processing during substitution as we could create partial
9721 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9722 processing_template_decl += incomplete;
9723 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
9724 processing_template_decl -= incomplete;
9726 if (fntype == error_mark_node)
9729 /* Place the explicitly specified arguments in TARGS. */
9730 for (i = NUM_TMPL_ARGS (converted_args); i--;)
9731 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
9734 /* Never do unification on the 'this' parameter. */
9735 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
9739 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9740 args = tree_cons (NULL_TREE, return_type, args);
9743 /* We allow incomplete unification without an error message here
9744 because the standard doesn't seem to explicitly prohibit it. Our
9745 callers must be ready to deal with unification failures in any
9747 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9748 targs, parms, args, /*subr=*/0,
9752 /* All is well so far. Now, check:
9756 When all template arguments have been deduced, all uses of
9757 template parameters in nondeduced contexts are replaced with
9758 the corresponding deduced argument values. If the
9759 substitution results in an invalid type, as described above,
9760 type deduction fails. */
9761 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
9768 /* Adjust types before performing type deduction, as described in
9769 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
9770 sections are symmetric. PARM is the type of a function parameter
9771 or the return type of the conversion function. ARG is the type of
9772 the argument passed to the call, or the type of the value
9773 initialized with the result of the conversion function. */
9776 maybe_adjust_types_for_deduction (unification_kind_t strict,
9789 /* Swap PARM and ARG throughout the remainder of this
9790 function; the handling is precisely symmetric since PARM
9791 will initialize ARG rather than vice versa. */
9799 /* There is nothing to do in this case. */
9806 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9808 /* [temp.deduct.call]
9810 If P is not a reference type:
9812 --If A is an array type, the pointer type produced by the
9813 array-to-pointer standard conversion (_conv.array_) is
9814 used in place of A for type deduction; otherwise,
9816 --If A is a function type, the pointer type produced by
9817 the function-to-pointer standard conversion
9818 (_conv.func_) is used in place of A for type deduction;
9821 --If A is a cv-qualified type, the top level
9822 cv-qualifiers of A's type are ignored for type
9824 if (TREE_CODE (*arg) == ARRAY_TYPE)
9825 *arg = build_pointer_type (TREE_TYPE (*arg));
9826 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
9827 *arg = build_pointer_type (*arg);
9829 *arg = TYPE_MAIN_VARIANT (*arg);
9832 /* [temp.deduct.call]
9834 If P is a cv-qualified type, the top level cv-qualifiers
9835 of P's type are ignored for type deduction. If P is a
9836 reference type, the type referred to by P is used for
9838 *parm = TYPE_MAIN_VARIANT (*parm);
9839 if (TREE_CODE (*parm) == REFERENCE_TYPE)
9841 *parm = TREE_TYPE (*parm);
9842 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9845 /* DR 322. For conversion deduction, remove a reference type on parm
9846 too (which has been swapped into ARG). */
9847 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9848 *arg = TREE_TYPE (*arg);
9853 /* Most parms like fn_type_unification.
9855 If SUBR is 1, we're being called recursively (to unify the
9856 arguments of a function or method parameter of a function
9860 type_unification_real (tree tparms,
9865 unification_kind_t strict,
9870 int ntparms = TREE_VEC_LENGTH (tparms);
9872 int saw_undeduced = 0;
9875 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9876 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9877 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9878 gcc_assert (ntparms > 0);
9883 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
9884 | UNIFY_ALLOW_DERIVED);
9888 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9892 sub_strict = UNIFY_ALLOW_NONE;
9903 while (parms && parms != void_list_node
9904 && args && args != void_list_node)
9906 parm = TREE_VALUE (parms);
9907 parms = TREE_CHAIN (parms);
9908 arg = TREE_VALUE (args);
9909 args = TREE_CHAIN (args);
9911 if (arg == error_mark_node)
9913 if (arg == unknown_type_node)
9914 /* We can't deduce anything from this, but we might get all the
9915 template args from other function args. */
9918 /* Conversions will be performed on a function argument that
9919 corresponds with a function parameter that contains only
9920 non-deducible template parameters and explicitly specified
9921 template parameters. */
9922 if (!uses_template_parms (parm))
9927 type = TREE_TYPE (arg);
9931 if (same_type_p (parm, type))
9933 if (strict != DEDUCE_EXACT
9934 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
9943 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
9944 if (type_unknown_p (arg))
9946 /* [temp.deduct.type]
9948 A template-argument can be deduced from a pointer to
9949 function or pointer to member function argument if
9950 the set of overloaded functions does not contain
9951 function templates and at most one of a set of
9952 overloaded functions provides a unique match. */
9953 if (resolve_overloaded_unification
9954 (tparms, targs, parm, arg, strict, sub_strict))
9959 arg = unlowered_expr_type (arg);
9960 if (arg == error_mark_node)
9965 int arg_strict = sub_strict;
9968 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
9970 if (unify (tparms, targs, parm, arg, arg_strict))
9975 /* Fail if we've reached the end of the parm list, and more args
9976 are present, and the parm list isn't variadic. */
9977 if (args && args != void_list_node && parms == void_list_node)
9979 /* Fail if parms are left and they don't have default values. */
9980 if (parms && parms != void_list_node
9981 && TREE_PURPOSE (parms) == NULL_TREE)
9985 for (i = 0; i < ntparms; i++)
9986 if (!TREE_VEC_ELT (targs, i))
9990 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
9993 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
9995 /* If this is an undeduced nontype parameter that depends on
9996 a type parameter, try another pass; its type may have been
9997 deduced from a later argument than the one from which
9998 this parameter can be deduced. */
9999 if (TREE_CODE (tparm) == PARM_DECL
10000 && uses_template_parms (TREE_TYPE (tparm))
10001 && !saw_undeduced++)
10010 /* Subroutine of type_unification_real. Args are like the variables
10011 at the call site. ARG is an overloaded function (or template-id);
10012 we try deducing template args from each of the overloads, and if
10013 only one succeeds, we go with that. Modifies TARGS and returns
10014 true on success. */
10017 resolve_overloaded_unification (tree tparms,
10021 unification_kind_t strict,
10024 tree tempargs = copy_node (targs);
10028 if (TREE_CODE (arg) == ADDR_EXPR)
10030 arg = TREE_OPERAND (arg, 0);
10036 if (TREE_CODE (arg) == COMPONENT_REF)
10037 /* Handle `&x' where `x' is some static or non-static member
10039 arg = TREE_OPERAND (arg, 1);
10041 if (TREE_CODE (arg) == OFFSET_REF)
10042 arg = TREE_OPERAND (arg, 1);
10044 /* Strip baselink information. */
10045 if (BASELINK_P (arg))
10046 arg = BASELINK_FUNCTIONS (arg);
10048 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
10050 /* If we got some explicit template args, we need to plug them into
10051 the affected templates before we try to unify, in case the
10052 explicit args will completely resolve the templates in question. */
10054 tree expl_subargs = TREE_OPERAND (arg, 1);
10055 arg = TREE_OPERAND (arg, 0);
10057 for (; arg; arg = OVL_NEXT (arg))
10059 tree fn = OVL_CURRENT (arg);
10060 tree subargs, elem;
10062 if (TREE_CODE (fn) != TEMPLATE_DECL)
10065 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
10066 expl_subargs, /*check_ret=*/false);
10069 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
10070 good += try_one_overload (tparms, targs, tempargs, parm,
10071 elem, strict, sub_strict, addr_p);
10075 else if (TREE_CODE (arg) != OVERLOAD
10076 && TREE_CODE (arg) != FUNCTION_DECL)
10077 /* If ARG is, for example, "(0, &f)" then its type will be unknown
10078 -- but the deduction does not succeed because the expression is
10079 not just the function on its own. */
10082 for (; arg; arg = OVL_NEXT (arg))
10083 good += try_one_overload (tparms, targs, tempargs, parm,
10084 TREE_TYPE (OVL_CURRENT (arg)),
10085 strict, sub_strict, addr_p);
10087 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10088 to function or pointer to member function argument if the set of
10089 overloaded functions does not contain function templates and at most
10090 one of a set of overloaded functions provides a unique match.
10092 So if we found multiple possibilities, we return success but don't
10093 deduce anything. */
10097 int i = TREE_VEC_LENGTH (targs);
10099 if (TREE_VEC_ELT (tempargs, i))
10100 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
10108 /* Subroutine of resolve_overloaded_unification; does deduction for a single
10109 overload. Fills TARGS with any deduced arguments, or error_mark_node if
10110 different overloads deduce different arguments for a given parm.
10111 ADDR_P is true if the expression for which deduction is being
10112 performed was of the form "& fn" rather than simply "fn".
10114 Returns 1 on success. */
10117 try_one_overload (tree tparms,
10122 unification_kind_t strict,
10130 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10131 to function or pointer to member function argument if the set of
10132 overloaded functions does not contain function templates and at most
10133 one of a set of overloaded functions provides a unique match.
10135 So if this is a template, just return success. */
10137 if (uses_template_parms (arg))
10140 if (TREE_CODE (arg) == METHOD_TYPE)
10141 arg = build_ptrmemfunc_type (build_pointer_type (arg));
10143 arg = build_pointer_type (arg);
10145 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
10147 /* We don't copy orig_targs for this because if we have already deduced
10148 some template args from previous args, unify would complain when we
10149 try to deduce a template parameter for the same argument, even though
10150 there isn't really a conflict. */
10151 nargs = TREE_VEC_LENGTH (targs);
10152 tempargs = make_tree_vec (nargs);
10154 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
10157 /* First make sure we didn't deduce anything that conflicts with
10158 explicitly specified args. */
10159 for (i = nargs; i--; )
10161 tree elt = TREE_VEC_ELT (tempargs, i);
10162 tree oldelt = TREE_VEC_ELT (orig_targs, i);
10166 else if (uses_template_parms (elt))
10167 /* Since we're unifying against ourselves, we will fill in
10168 template args used in the function parm list with our own
10169 template parms. Discard them. */
10170 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
10171 else if (oldelt && !template_args_equal (oldelt, elt))
10175 for (i = nargs; i--; )
10177 tree elt = TREE_VEC_ELT (tempargs, i);
10180 TREE_VEC_ELT (targs, i) = elt;
10186 /* PARM is a template class (perhaps with unbound template
10187 parameters). ARG is a fully instantiated type. If ARG can be
10188 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
10189 TARGS are as for unify. */
10192 try_class_unification (tree tparms, tree targs, tree parm, tree arg)
10194 tree copy_of_targs;
10196 if (!CLASSTYPE_TEMPLATE_INFO (arg)
10197 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
10198 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
10201 /* We need to make a new template argument vector for the call to
10202 unify. If we used TARGS, we'd clutter it up with the result of
10203 the attempted unification, even if this class didn't work out.
10204 We also don't want to commit ourselves to all the unifications
10205 we've already done, since unification is supposed to be done on
10206 an argument-by-argument basis. In other words, consider the
10207 following pathological case:
10209 template <int I, int J, int K>
10212 template <int I, int J>
10213 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
10215 template <int I, int J, int K>
10216 void f(S<I, J, K>, S<I, I, I>);
10225 Now, by the time we consider the unification involving `s2', we
10226 already know that we must have `f<0, 0, 0>'. But, even though
10227 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
10228 because there are two ways to unify base classes of S<0, 1, 2>
10229 with S<I, I, I>. If we kept the already deduced knowledge, we
10230 would reject the possibility I=1. */
10231 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
10233 /* If unification failed, we're done. */
10234 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
10235 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
10241 /* Given a template type PARM and a class type ARG, find the unique
10242 base type in ARG that is an instance of PARM. We do not examine
10243 ARG itself; only its base-classes. If there is not exactly one
10244 appropriate base class, return NULL_TREE. PARM may be the type of
10245 a partial specialization, as well as a plain template type. Used
10249 get_template_base (tree tparms, tree targs, tree parm, tree arg)
10251 tree rval = NULL_TREE;
10254 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
10256 binfo = TYPE_BINFO (complete_type (arg));
10258 /* The type could not be completed. */
10261 /* Walk in inheritance graph order. The search order is not
10262 important, and this avoids multiple walks of virtual bases. */
10263 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
10265 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
10269 /* If there is more than one satisfactory baseclass, then:
10273 If they yield more than one possible deduced A, the type
10277 if (rval && !same_type_p (r, rval))
10287 /* Returns the level of DECL, which declares a template parameter. */
10290 template_decl_level (tree decl)
10292 switch (TREE_CODE (decl))
10295 case TEMPLATE_DECL:
10296 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
10299 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
10302 gcc_unreachable ();
10307 /* Decide whether ARG can be unified with PARM, considering only the
10308 cv-qualifiers of each type, given STRICT as documented for unify.
10309 Returns nonzero iff the unification is OK on that basis. */
10312 check_cv_quals_for_unify (int strict, tree arg, tree parm)
10314 int arg_quals = cp_type_quals (arg);
10315 int parm_quals = cp_type_quals (parm);
10317 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10318 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
10320 /* Although a CVR qualifier is ignored when being applied to a
10321 substituted template parameter ([8.3.2]/1 for example), that
10322 does not apply during deduction [14.8.2.4]/1, (even though
10323 that is not explicitly mentioned, [14.8.2.4]/9 indicates
10324 this). Except when we're allowing additional CV qualifiers
10325 at the outer level [14.8.2.1]/3,1st bullet. */
10326 if ((TREE_CODE (arg) == REFERENCE_TYPE
10327 || TREE_CODE (arg) == FUNCTION_TYPE
10328 || TREE_CODE (arg) == METHOD_TYPE)
10329 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
10332 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
10333 && (parm_quals & TYPE_QUAL_RESTRICT))
10337 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
10338 && (arg_quals & parm_quals) != parm_quals)
10341 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
10342 && (parm_quals & arg_quals) != arg_quals)
10348 /* Deduce the value of template parameters. TPARMS is the (innermost)
10349 set of template parameters to a template. TARGS is the bindings
10350 for those template parameters, as determined thus far; TARGS may
10351 include template arguments for outer levels of template parameters
10352 as well. PARM is a parameter to a template function, or a
10353 subcomponent of that parameter; ARG is the corresponding argument.
10354 This function attempts to match PARM with ARG in a manner
10355 consistent with the existing assignments in TARGS. If more values
10356 are deduced, then TARGS is updated.
10358 Returns 0 if the type deduction succeeds, 1 otherwise. The
10359 parameter STRICT is a bitwise or of the following flags:
10362 Require an exact match between PARM and ARG.
10363 UNIFY_ALLOW_MORE_CV_QUAL:
10364 Allow the deduced ARG to be more cv-qualified (by qualification
10365 conversion) than ARG.
10366 UNIFY_ALLOW_LESS_CV_QUAL:
10367 Allow the deduced ARG to be less cv-qualified than ARG.
10368 UNIFY_ALLOW_DERIVED:
10369 Allow the deduced ARG to be a template base class of ARG,
10370 or a pointer to a template base class of the type pointed to by
10372 UNIFY_ALLOW_INTEGER:
10373 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
10374 case for more information.
10375 UNIFY_ALLOW_OUTER_LEVEL:
10376 This is the outermost level of a deduction. Used to determine validity
10377 of qualification conversions. A valid qualification conversion must
10378 have const qualified pointers leading up to the inner type which
10379 requires additional CV quals, except at the outer level, where const
10380 is not required [conv.qual]. It would be normal to set this flag in
10381 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
10382 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
10383 This is the outermost level of a deduction, and PARM can be more CV
10384 qualified at this point.
10385 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
10386 This is the outermost level of a deduction, and PARM can be less CV
10387 qualified at this point. */
10390 unify (tree tparms, tree targs, tree parm, tree arg, int strict)
10395 int strict_in = strict;
10397 /* I don't think this will do the right thing with respect to types.
10398 But the only case I've seen it in so far has been array bounds, where
10399 signedness is the only information lost, and I think that will be
10401 while (TREE_CODE (parm) == NOP_EXPR)
10402 parm = TREE_OPERAND (parm, 0);
10404 if (arg == error_mark_node)
10406 if (arg == unknown_type_node)
10407 /* We can't deduce anything from this, but we might get all the
10408 template args from other function args. */
10411 /* If PARM uses template parameters, then we can't bail out here,
10412 even if ARG == PARM, since we won't record unifications for the
10413 template parameters. We might need them if we're trying to
10414 figure out which of two things is more specialized. */
10415 if (arg == parm && !uses_template_parms (parm))
10418 /* Immediately reject some pairs that won't unify because of
10419 cv-qualification mismatches. */
10420 if (TREE_CODE (arg) == TREE_CODE (parm)
10422 /* It is the elements of the array which hold the cv quals of an array
10423 type, and the elements might be template type parms. We'll check
10424 when we recurse. */
10425 && TREE_CODE (arg) != ARRAY_TYPE
10426 /* We check the cv-qualifiers when unifying with template type
10427 parameters below. We want to allow ARG `const T' to unify with
10428 PARM `T' for example, when computing which of two templates
10429 is more specialized, for example. */
10430 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
10431 && !check_cv_quals_for_unify (strict_in, arg, parm))
10434 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
10435 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
10436 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
10437 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
10438 strict &= ~UNIFY_ALLOW_DERIVED;
10439 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
10440 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
10442 switch (TREE_CODE (parm))
10444 case TYPENAME_TYPE:
10446 case UNBOUND_CLASS_TEMPLATE:
10447 /* In a type which contains a nested-name-specifier, template
10448 argument values cannot be deduced for template parameters used
10449 within the nested-name-specifier. */
10452 case TEMPLATE_TYPE_PARM:
10453 case TEMPLATE_TEMPLATE_PARM:
10454 case BOUND_TEMPLATE_TEMPLATE_PARM:
10455 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10457 if (TEMPLATE_TYPE_LEVEL (parm)
10458 != template_decl_level (tparm))
10459 /* The PARM is not one we're trying to unify. Just check
10460 to see if it matches ARG. */
10461 return (TREE_CODE (arg) == TREE_CODE (parm)
10462 && same_type_p (parm, arg)) ? 0 : 1;
10463 idx = TEMPLATE_TYPE_IDX (parm);
10464 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10465 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
10467 /* Check for mixed types and values. */
10468 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10469 && TREE_CODE (tparm) != TYPE_DECL)
10470 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10471 && TREE_CODE (tparm) != TEMPLATE_DECL))
10474 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10476 /* ARG must be constructed from a template class or a template
10477 template parameter. */
10478 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
10479 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
10483 tree parmvec = TYPE_TI_ARGS (parm);
10484 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
10486 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
10489 /* The resolution to DR150 makes clear that default
10490 arguments for an N-argument may not be used to bind T
10491 to a template template parameter with fewer than N
10492 parameters. It is not safe to permit the binding of
10493 default arguments as an extension, as that may change
10494 the meaning of a conforming program. Consider:
10496 struct Dense { static const unsigned int dim = 1; };
10498 template <template <typename> class View,
10500 void operator+(float, View<Block> const&);
10502 template <typename Block,
10503 unsigned int Dim = Block::dim>
10504 struct Lvalue_proxy { operator float() const; };
10508 Lvalue_proxy<Dense> p;
10513 Here, if Lvalue_proxy is permitted to bind to View, then
10514 the global operator+ will be used; if they are not, the
10515 Lvalue_proxy will be converted to float. */
10516 if (coerce_template_parms (argtmplvec, parmvec,
10517 TYPE_TI_TEMPLATE (parm),
10519 /*require_all_args=*/true,
10520 /*use_default_args=*/false)
10521 == error_mark_node)
10524 /* Deduce arguments T, i from TT<T> or TT<i>.
10525 We check each element of PARMVEC and ARGVEC individually
10526 rather than the whole TREE_VEC since they can have
10527 different number of elements. */
10529 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
10531 if (unify (tparms, targs,
10532 TREE_VEC_ELT (parmvec, i),
10533 TREE_VEC_ELT (argvec, i),
10538 arg = TYPE_TI_TEMPLATE (arg);
10540 /* Fall through to deduce template name. */
10543 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10544 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10546 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10548 /* Simple cases: Value already set, does match or doesn't. */
10549 if (targ != NULL_TREE && template_args_equal (targ, arg))
10556 /* If PARM is `const T' and ARG is only `int', we don't have
10557 a match unless we are allowing additional qualification.
10558 If ARG is `const int' and PARM is just `T' that's OK;
10559 that binds `const int' to `T'. */
10560 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
10564 /* Consider the case where ARG is `const volatile int' and
10565 PARM is `const T'. Then, T should be `volatile int'. */
10566 arg = cp_build_qualified_type_real
10567 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
10568 if (arg == error_mark_node)
10571 /* Simple cases: Value already set, does match or doesn't. */
10572 if (targ != NULL_TREE && same_type_p (targ, arg))
10577 /* Make sure that ARG is not a variable-sized array. (Note
10578 that were talking about variable-sized arrays (like
10579 `int[n]'), rather than arrays of unknown size (like
10580 `int[]').) We'll get very confused by such a type since
10581 the bound of the array will not be computable in an
10582 instantiation. Besides, such types are not allowed in
10583 ISO C++, so we can do as we please here. */
10584 if (variably_modified_type_p (arg, NULL_TREE))
10588 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10591 case TEMPLATE_PARM_INDEX:
10592 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10593 if (tparm == error_mark_node)
10596 if (TEMPLATE_PARM_LEVEL (parm)
10597 != template_decl_level (tparm))
10598 /* The PARM is not one we're trying to unify. Just check
10599 to see if it matches ARG. */
10600 return !(TREE_CODE (arg) == TREE_CODE (parm)
10601 && cp_tree_equal (parm, arg));
10603 idx = TEMPLATE_PARM_IDX (parm);
10604 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
10607 return !cp_tree_equal (targ, arg);
10609 /* [temp.deduct.type] If, in the declaration of a function template
10610 with a non-type template-parameter, the non-type
10611 template-parameter is used in an expression in the function
10612 parameter-list and, if the corresponding template-argument is
10613 deduced, the template-argument type shall match the type of the
10614 template-parameter exactly, except that a template-argument
10615 deduced from an array bound may be of any integral type.
10616 The non-type parameter might use already deduced type parameters. */
10617 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
10618 if (!TREE_TYPE (arg))
10619 /* Template-parameter dependent expression. Just accept it for now.
10620 It will later be processed in convert_template_argument. */
10622 else if (same_type_p (TREE_TYPE (arg), tparm))
10624 else if ((strict & UNIFY_ALLOW_INTEGER)
10625 && (TREE_CODE (tparm) == INTEGER_TYPE
10626 || TREE_CODE (tparm) == BOOLEAN_TYPE))
10627 /* Convert the ARG to the type of PARM; the deduced non-type
10628 template argument must exactly match the types of the
10629 corresponding parameter. */
10630 arg = fold (build_nop (TREE_TYPE (parm), arg));
10631 else if (uses_template_parms (tparm))
10632 /* We haven't deduced the type of this parameter yet. Try again
10638 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
10643 /* A pointer-to-member constant can be unified only with
10644 another constant. */
10645 if (TREE_CODE (arg) != PTRMEM_CST)
10648 /* Just unify the class member. It would be useless (and possibly
10649 wrong, depending on the strict flags) to unify also
10650 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10651 arg refer to the same variable, even if through different
10652 classes. For instance:
10654 struct A { int x; };
10657 Unification of &A::x and &B::x must succeed. */
10658 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
10659 PTRMEM_CST_MEMBER (arg), strict);
10664 if (TREE_CODE (arg) != POINTER_TYPE)
10667 /* [temp.deduct.call]
10669 A can be another pointer or pointer to member type that can
10670 be converted to the deduced A via a qualification
10671 conversion (_conv.qual_).
10673 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10674 This will allow for additional cv-qualification of the
10675 pointed-to types if appropriate. */
10677 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
10678 /* The derived-to-base conversion only persists through one
10679 level of pointers. */
10680 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
10682 return unify (tparms, targs, TREE_TYPE (parm),
10683 TREE_TYPE (arg), strict);
10686 case REFERENCE_TYPE:
10687 if (TREE_CODE (arg) != REFERENCE_TYPE)
10689 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10690 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10693 if (TREE_CODE (arg) != ARRAY_TYPE)
10695 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10696 != (TYPE_DOMAIN (arg) == NULL_TREE))
10698 if (TYPE_DOMAIN (parm) != NULL_TREE)
10705 /* Our representation of array types uses "N - 1" as the
10706 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10707 not an integer constant. We cannot unify arbitrarily
10708 complex expressions, so we eliminate the MINUS_EXPRs
10710 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10711 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
10714 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
10715 parm_max = TREE_OPERAND (parm_max, 0);
10717 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10718 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
10721 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
10722 trying to unify the type of a variable with the type
10723 of a template parameter. For example:
10725 template <unsigned int N>
10726 void f (char (&) [N]);
10733 Here, the type of the ARG will be "int [g(i)]", and
10734 may be a SAVE_EXPR, etc. */
10735 if (TREE_CODE (arg_max) != MINUS_EXPR)
10737 arg_max = TREE_OPERAND (arg_max, 0);
10740 /* If only one of the bounds used a MINUS_EXPR, compensate
10741 by adding one to the other bound. */
10742 if (parm_cst && !arg_cst)
10743 parm_max = fold_build2 (PLUS_EXPR,
10747 else if (arg_cst && !parm_cst)
10748 arg_max = fold_build2 (PLUS_EXPR,
10753 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10756 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10757 strict & UNIFY_ALLOW_MORE_CV_QUAL);
10764 case ENUMERAL_TYPE:
10766 if (TREE_CODE (arg) != TREE_CODE (parm))
10769 /* We have already checked cv-qualification at the top of the
10771 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
10774 /* As far as unification is concerned, this wins. Later checks
10775 will invalidate it if necessary. */
10778 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10779 /* Type INTEGER_CST can come from ordinary constant template args. */
10781 while (TREE_CODE (arg) == NOP_EXPR)
10782 arg = TREE_OPERAND (arg, 0);
10784 if (TREE_CODE (arg) != INTEGER_CST)
10786 return !tree_int_cst_equal (parm, arg);
10791 if (TREE_CODE (arg) != TREE_VEC)
10793 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10795 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
10796 if (unify (tparms, targs,
10797 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
10805 if (TREE_CODE (arg) != TREE_CODE (parm))
10808 if (TYPE_PTRMEMFUNC_P (parm))
10810 if (!TYPE_PTRMEMFUNC_P (arg))
10813 return unify (tparms, targs,
10814 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10815 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10819 if (CLASSTYPE_TEMPLATE_INFO (parm))
10821 tree t = NULL_TREE;
10823 if (strict_in & UNIFY_ALLOW_DERIVED)
10825 /* First, we try to unify the PARM and ARG directly. */
10826 t = try_class_unification (tparms, targs,
10831 /* Fallback to the special case allowed in
10832 [temp.deduct.call]:
10834 If P is a class, and P has the form
10835 template-id, then A can be a derived class of
10836 the deduced A. Likewise, if P is a pointer to
10837 a class of the form template-id, A can be a
10838 pointer to a derived class pointed to by the
10840 t = get_template_base (tparms, targs, parm, arg);
10846 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10847 && (CLASSTYPE_TI_TEMPLATE (parm)
10848 == CLASSTYPE_TI_TEMPLATE (arg)))
10849 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10850 Then, we should unify `int' and `U'. */
10853 /* There's no chance of unification succeeding. */
10856 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
10857 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
10859 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
10864 case FUNCTION_TYPE:
10865 if (TREE_CODE (arg) != TREE_CODE (parm))
10868 /* CV qualifications for methods can never be deduced, they must
10869 match exactly. We need to check them explicitly here,
10870 because type_unification_real treats them as any other
10871 cvqualified parameter. */
10872 if (TREE_CODE (parm) == METHOD_TYPE
10873 && (!check_cv_quals_for_unify
10875 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10876 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10879 if (unify (tparms, targs, TREE_TYPE (parm),
10880 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
10882 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
10883 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
10887 /* Unify a pointer to member with a pointer to member function, which
10888 deduces the type of the member as a function type. */
10889 if (TYPE_PTRMEMFUNC_P (arg))
10893 cp_cv_quals cv_quals;
10895 /* Check top-level cv qualifiers */
10896 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10899 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10900 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10903 /* Determine the type of the function we are unifying against. */
10904 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10906 build_function_type (TREE_TYPE (method_type),
10907 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10909 /* Extract the cv-qualifiers of the member function from the
10910 implicit object parameter and place them on the function
10911 type to be restored later. */
10913 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10914 fntype = build_qualified_type (fntype, cv_quals);
10915 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10918 if (TREE_CODE (arg) != OFFSET_TYPE)
10920 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10921 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
10923 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
10927 if (DECL_TEMPLATE_PARM_P (parm))
10928 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
10929 if (arg != integral_constant_value (parm))
10934 case TEMPLATE_DECL:
10935 /* Matched cases are handled by the ARG == PARM test above. */
10939 gcc_assert (EXPR_P (parm));
10941 /* We must be looking at an expression. This can happen with
10945 void foo(S<I>, S<I + 2>);
10947 This is a "nondeduced context":
10951 The nondeduced contexts are:
10953 --A type that is a template-id in which one or more of
10954 the template-arguments is an expression that references
10955 a template-parameter.
10957 In these cases, we assume deduction succeeded, but don't
10958 actually infer any unifications. */
10960 if (!uses_template_parms (parm)
10961 && !template_args_equal (parm, arg))
10968 /* Note that DECL can be defined in this translation unit, if
10972 mark_definable (tree decl)
10975 DECL_NOT_REALLY_EXTERN (decl) = 1;
10976 FOR_EACH_CLONE (clone, decl)
10977 DECL_NOT_REALLY_EXTERN (clone) = 1;
10980 /* Called if RESULT is explicitly instantiated, or is a member of an
10981 explicitly instantiated class. */
10984 mark_decl_instantiated (tree result, int extern_p)
10986 SET_DECL_EXPLICIT_INSTANTIATION (result);
10988 /* If this entity has already been written out, it's too late to
10989 make any modifications. */
10990 if (TREE_ASM_WRITTEN (result))
10993 if (TREE_CODE (result) != FUNCTION_DECL)
10994 /* The TREE_PUBLIC flag for function declarations will have been
10995 set correctly by tsubst. */
10996 TREE_PUBLIC (result) = 1;
10998 /* This might have been set by an earlier implicit instantiation. */
10999 DECL_COMDAT (result) = 0;
11002 DECL_NOT_REALLY_EXTERN (result) = 0;
11005 mark_definable (result);
11006 /* Always make artificials weak. */
11007 if (DECL_ARTIFICIAL (result) && flag_weak)
11008 comdat_linkage (result);
11009 /* For WIN32 we also want to put explicit instantiations in
11010 linkonce sections. */
11011 else if (TREE_PUBLIC (result))
11012 maybe_make_one_only (result);
11015 /* If EXTERN_P, then this function will not be emitted -- unless
11016 followed by an explicit instantiation, at which point its linkage
11017 will be adjusted. If !EXTERN_P, then this function will be
11018 emitted here. In neither circumstance do we want
11019 import_export_decl to adjust the linkage. */
11020 DECL_INTERFACE_KNOWN (result) = 1;
11023 /* Given two function templates PAT1 and PAT2, return:
11025 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
11026 -1 if PAT2 is more specialized than PAT1.
11027 0 if neither is more specialized.
11029 LEN indicates the number of parameters we should consider
11030 (defaulted parameters should not be considered).
11032 The 1998 std underspecified function template partial ordering, and
11033 DR214 addresses the issue. We take pairs of arguments, one from
11034 each of the templates, and deduce them against each other. One of
11035 the templates will be more specialized if all the *other*
11036 template's arguments deduce against its arguments and at least one
11037 of its arguments *does* *not* deduce against the other template's
11038 corresponding argument. Deduction is done as for class templates.
11039 The arguments used in deduction have reference and top level cv
11040 qualifiers removed. Iff both arguments were originally reference
11041 types *and* deduction succeeds in both directions, the template
11042 with the more cv-qualified argument wins for that pairing (if
11043 neither is more cv-qualified, they both are equal). Unlike regular
11044 deduction, after all the arguments have been deduced in this way,
11045 we do *not* verify the deduced template argument values can be
11046 substituted into non-deduced contexts, nor do we have to verify
11047 that all template arguments have been deduced. */
11050 more_specialized_fn (tree pat1, tree pat2, int len)
11052 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
11053 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
11054 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
11055 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
11056 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
11057 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
11058 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
11059 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
11063 /* Remove the this parameter from non-static member functions. If
11064 one is a non-static member function and the other is not a static
11065 member function, remove the first parameter from that function
11066 also. This situation occurs for operator functions where we
11067 locate both a member function (with this pointer) and non-member
11068 operator (with explicit first operand). */
11069 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
11071 len--; /* LEN is the number of significant arguments for DECL1 */
11072 args1 = TREE_CHAIN (args1);
11073 if (!DECL_STATIC_FUNCTION_P (decl2))
11074 args2 = TREE_CHAIN (args2);
11076 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
11078 args2 = TREE_CHAIN (args2);
11079 if (!DECL_STATIC_FUNCTION_P (decl1))
11082 args1 = TREE_CHAIN (args1);
11086 /* If only one is a conversion operator, they are unordered. */
11087 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
11090 /* Consider the return type for a conversion function */
11091 if (DECL_CONV_FN_P (decl1))
11093 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
11094 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
11098 processing_template_decl++;
11102 tree arg1 = TREE_VALUE (args1);
11103 tree arg2 = TREE_VALUE (args2);
11104 int deduce1, deduce2;
11108 if (TREE_CODE (arg1) == REFERENCE_TYPE)
11110 arg1 = TREE_TYPE (arg1);
11111 quals1 = cp_type_quals (arg1);
11114 if (TREE_CODE (arg2) == REFERENCE_TYPE)
11116 arg2 = TREE_TYPE (arg2);
11117 quals2 = cp_type_quals (arg2);
11120 if ((quals1 < 0) != (quals2 < 0))
11122 /* Only of the args is a reference, see if we should apply
11123 array/function pointer decay to it. This is not part of
11124 DR214, but is, IMHO, consistent with the deduction rules
11125 for the function call itself, and with our earlier
11126 implementation of the underspecified partial ordering
11127 rules. (nathan). */
11130 switch (TREE_CODE (arg1))
11133 arg1 = TREE_TYPE (arg1);
11135 case FUNCTION_TYPE:
11136 arg1 = build_pointer_type (arg1);
11145 switch (TREE_CODE (arg2))
11148 arg2 = TREE_TYPE (arg2);
11150 case FUNCTION_TYPE:
11151 arg2 = build_pointer_type (arg2);
11160 arg1 = TYPE_MAIN_VARIANT (arg1);
11161 arg2 = TYPE_MAIN_VARIANT (arg2);
11163 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
11164 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
11170 if (better1 < 0 && better2 < 0)
11171 /* We've failed to deduce something in either direction.
11172 These must be unordered. */
11175 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
11177 /* Deduces in both directions, see if quals can
11178 disambiguate. Pretend the worse one failed to deduce. */
11179 if ((quals1 & quals2) == quals2)
11181 if ((quals1 & quals2) == quals1)
11184 if (deduce1 && !deduce2 && !better2)
11186 if (deduce2 && !deduce1 && !better1)
11189 args1 = TREE_CHAIN (args1);
11190 args2 = TREE_CHAIN (args2);
11193 processing_template_decl--;
11195 return (better1 > 0) - (better2 > 0);
11198 /* Determine which of two partial specializations is more specialized.
11200 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
11201 to the first partial specialization. The TREE_VALUE is the
11202 innermost set of template parameters for the partial
11203 specialization. PAT2 is similar, but for the second template.
11205 Return 1 if the first partial specialization is more specialized;
11206 -1 if the second is more specialized; 0 if neither is more
11209 See [temp.class.order] for information about determining which of
11210 two templates is more specialized. */
11213 more_specialized_class (tree pat1, tree pat2)
11219 tmpl1 = TREE_TYPE (pat1);
11220 tmpl2 = TREE_TYPE (pat2);
11222 /* Just like what happens for functions, if we are ordering between
11223 different class template specializations, we may encounter dependent
11224 types in the arguments, and we need our dependency check functions
11225 to behave correctly. */
11226 ++processing_template_decl;
11227 targs = get_class_bindings (TREE_VALUE (pat1),
11228 CLASSTYPE_TI_ARGS (tmpl1),
11229 CLASSTYPE_TI_ARGS (tmpl2));
11233 targs = get_class_bindings (TREE_VALUE (pat2),
11234 CLASSTYPE_TI_ARGS (tmpl2),
11235 CLASSTYPE_TI_ARGS (tmpl1));
11238 --processing_template_decl;
11243 /* Return the template arguments that will produce the function signature
11244 DECL from the function template FN, with the explicit template
11245 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
11246 also match. Return NULL_TREE if no satisfactory arguments could be
11250 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
11252 int ntparms = DECL_NTPARMS (fn);
11253 tree targs = make_tree_vec (ntparms);
11255 tree decl_arg_types;
11257 /* Substitute the explicit template arguments into the type of DECL.
11258 The call to fn_type_unification will handle substitution into the
11260 decl_type = TREE_TYPE (decl);
11261 if (explicit_args && uses_template_parms (decl_type))
11264 tree converted_args;
11266 if (DECL_TEMPLATE_INFO (decl))
11267 tmpl = DECL_TI_TEMPLATE (decl);
11269 /* We can get here for some invalid specializations. */
11273 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
11274 explicit_args, NULL_TREE,
11276 /*require_all_args=*/false,
11277 /*use_default_args=*/false);
11278 if (converted_args == error_mark_node)
11281 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
11282 if (decl_type == error_mark_node)
11286 /* Never do unification on the 'this' parameter. */
11287 decl_arg_types = skip_artificial_parms_for (decl,
11288 TYPE_ARG_TYPES (decl_type));
11290 if (fn_type_unification (fn, explicit_args, targs,
11292 (check_rettype || DECL_CONV_FN_P (fn)
11293 ? TREE_TYPE (decl_type) : NULL_TREE),
11294 DEDUCE_EXACT, LOOKUP_NORMAL))
11300 /* Return the innermost template arguments that, when applied to a
11301 template specialization whose innermost template parameters are
11302 TPARMS, and whose specialization arguments are PARMS, yield the
11305 For example, suppose we have:
11307 template <class T, class U> struct S {};
11308 template <class T> struct S<T*, int> {};
11310 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
11311 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
11312 int}. The resulting vector will be {double}, indicating that `T'
11313 is bound to `double'. */
11316 get_class_bindings (tree tparms, tree spec_args, tree args)
11318 int i, ntparms = TREE_VEC_LENGTH (tparms);
11320 tree innermost_deduced_args;
11322 innermost_deduced_args = make_tree_vec (ntparms);
11323 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11325 deduced_args = copy_node (args);
11326 SET_TMPL_ARGS_LEVEL (deduced_args,
11327 TMPL_ARGS_DEPTH (deduced_args),
11328 innermost_deduced_args);
11331 deduced_args = innermost_deduced_args;
11333 if (unify (tparms, deduced_args,
11334 INNERMOST_TEMPLATE_ARGS (spec_args),
11335 INNERMOST_TEMPLATE_ARGS (args),
11339 for (i = 0; i < ntparms; ++i)
11340 if (! TREE_VEC_ELT (innermost_deduced_args, i))
11343 /* Verify that nondeduced template arguments agree with the type
11344 obtained from argument deduction.
11348 struct A { typedef int X; };
11349 template <class T, class U> struct C {};
11350 template <class T> struct C<T, typename T::X> {};
11352 Then with the instantiation `C<A, int>', we can deduce that
11353 `T' is `A' but unify () does not check whether `typename T::X'
11355 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
11356 if (spec_args == error_mark_node
11357 /* We only need to check the innermost arguments; the other
11358 arguments will always agree. */
11359 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
11360 INNERMOST_TEMPLATE_ARGS (args)))
11363 return deduced_args;
11366 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
11367 Return the TREE_LIST node with the most specialized template, if
11368 any. If there is no most specialized template, the error_mark_node
11371 Note that this function does not look at, or modify, the
11372 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
11373 returned is one of the elements of INSTANTIATIONS, callers may
11374 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
11375 and retrieve it from the value returned. */
11378 most_specialized_instantiation (tree templates)
11382 ++processing_template_decl;
11385 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
11389 if (get_bindings (TREE_VALUE (champ),
11390 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11391 NULL_TREE, /*check_ret=*/false))
11394 if (get_bindings (TREE_VALUE (fn),
11395 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11396 NULL_TREE, /*check_ret=*/false))
11403 /* Equally specialized, move to next function. If there
11404 is no next function, nothing's most specialized. */
11405 fn = TREE_CHAIN (fn);
11413 /* Now verify that champ is better than everything earlier in the
11414 instantiation list. */
11415 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
11416 if (get_bindings (TREE_VALUE (champ),
11417 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11418 NULL_TREE, /*check_ret=*/false)
11419 || !get_bindings (TREE_VALUE (fn),
11420 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11421 NULL_TREE, /*check_ret=*/false))
11427 processing_template_decl--;
11430 return error_mark_node;
11435 /* If DECL is a specialization of some template, return the most
11436 general such template. Otherwise, returns NULL_TREE.
11438 For example, given:
11440 template <class T> struct S { template <class U> void f(U); };
11442 if TMPL is `template <class U> void S<int>::f(U)' this will return
11443 the full template. This function will not trace past partial
11444 specializations, however. For example, given in addition:
11446 template <class T> struct S<T*> { template <class U> void f(U); };
11448 if TMPL is `template <class U> void S<int*>::f(U)' this will return
11449 `template <class T> template <class U> S<T*>::f(U)'. */
11452 most_general_template (tree decl)
11454 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
11455 an immediate specialization. */
11456 if (TREE_CODE (decl) == FUNCTION_DECL)
11458 if (DECL_TEMPLATE_INFO (decl)) {
11459 decl = DECL_TI_TEMPLATE (decl);
11461 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
11462 template friend. */
11463 if (TREE_CODE (decl) != TEMPLATE_DECL)
11469 /* Look for more and more general templates. */
11470 while (DECL_TEMPLATE_INFO (decl))
11472 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11473 (See cp-tree.h for details.) */
11474 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11477 if (CLASS_TYPE_P (TREE_TYPE (decl))
11478 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
11481 /* Stop if we run into an explicitly specialized class template. */
11482 if (!DECL_NAMESPACE_SCOPE_P (decl)
11483 && DECL_CONTEXT (decl)
11484 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
11487 decl = DECL_TI_TEMPLATE (decl);
11493 /* Return the most specialized of the class template partial
11494 specializations of TMPL which can produce TYPE, a specialization of
11495 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
11496 a _TYPE node corresponding to the partial specialization, while the
11497 TREE_PURPOSE is the set of template arguments that must be
11498 substituted into the TREE_TYPE in order to generate TYPE.
11500 If the choice of partial specialization is ambiguous, a diagnostic
11501 is issued, and the error_mark_node is returned. If there are no
11502 partial specializations of TMPL matching TYPE, then NULL_TREE is
11506 most_specialized_class (tree type, tree tmpl)
11508 tree list = NULL_TREE;
11515 tmpl = most_general_template (tmpl);
11516 args = CLASSTYPE_TI_ARGS (type);
11517 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
11519 tree partial_spec_args;
11522 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
11523 spec_args = get_class_bindings (TREE_VALUE (t),
11528 list = tree_cons (spec_args, TREE_VALUE (t), list);
11529 TREE_TYPE (list) = TREE_TYPE (t);
11536 ambiguous_p = false;
11539 t = TREE_CHAIN (t);
11540 for (; t; t = TREE_CHAIN (t))
11542 fate = more_specialized_class (champ, t);
11549 t = TREE_CHAIN (t);
11552 ambiguous_p = true;
11561 for (t = list; t && t != champ; t = TREE_CHAIN (t))
11563 fate = more_specialized_class (champ, t);
11566 ambiguous_p = true;
11573 const char *str = "candidates are:";
11574 error ("ambiguous class template instantiation for %q#T", type);
11575 for (t = list; t; t = TREE_CHAIN (t))
11577 error ("%s %+#T", str, TREE_TYPE (t));
11580 return error_mark_node;
11586 /* Explicitly instantiate DECL. */
11589 do_decl_instantiation (tree decl, tree storage)
11591 tree result = NULL_TREE;
11594 if (!decl || decl == error_mark_node)
11595 /* An error occurred, for which grokdeclarator has already issued
11596 an appropriate message. */
11598 else if (! DECL_LANG_SPECIFIC (decl))
11600 error ("explicit instantiation of non-template %q#D", decl);
11603 else if (TREE_CODE (decl) == VAR_DECL)
11605 /* There is an asymmetry here in the way VAR_DECLs and
11606 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11607 the latter, the DECL we get back will be marked as a
11608 template instantiation, and the appropriate
11609 DECL_TEMPLATE_INFO will be set up. This does not happen for
11610 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11611 should handle VAR_DECLs as it currently handles
11613 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
11614 if (!result || TREE_CODE (result) != VAR_DECL)
11616 error ("no matching template for %qD found", decl);
11620 else if (TREE_CODE (decl) != FUNCTION_DECL)
11622 error ("explicit instantiation of %q#D", decl);
11628 /* Check for various error cases. Note that if the explicit
11629 instantiation is valid the RESULT will currently be marked as an
11630 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11631 until we get here. */
11633 if (DECL_TEMPLATE_SPECIALIZATION (result))
11635 /* DR 259 [temp.spec].
11637 Both an explicit instantiation and a declaration of an explicit
11638 specialization shall not appear in a program unless the explicit
11639 instantiation follows a declaration of the explicit specialization.
11641 For a given set of template parameters, if an explicit
11642 instantiation of a template appears after a declaration of an
11643 explicit specialization for that template, the explicit
11644 instantiation has no effect. */
11647 else if (DECL_EXPLICIT_INSTANTIATION (result))
11651 No program shall explicitly instantiate any template more
11654 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11655 the first instantiation was `extern' and the second is not,
11656 and EXTERN_P for the opposite case. */
11657 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
11658 pedwarn ("duplicate explicit instantiation of %q#D", result);
11659 /* If an "extern" explicit instantiation follows an ordinary
11660 explicit instantiation, the template is instantiated. */
11664 else if (!DECL_IMPLICIT_INSTANTIATION (result))
11666 error ("no matching template for %qD found", result);
11669 else if (!DECL_TEMPLATE_INFO (result))
11671 pedwarn ("explicit instantiation of non-template %q#D", result);
11675 if (storage == NULL_TREE)
11677 else if (storage == ridpointers[(int) RID_EXTERN])
11679 if (pedantic && !in_system_header)
11680 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11685 error ("storage class %qD applied to template instantiation", storage);
11687 check_explicit_instantiation_namespace (result);
11688 mark_decl_instantiated (result, extern_p);
11690 instantiate_decl (result, /*defer_ok=*/1,
11691 /*expl_inst_class_mem_p=*/false);
11695 mark_class_instantiated (tree t, int extern_p)
11697 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
11698 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11699 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
11700 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11703 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11704 rest_of_type_compilation (t, 1);
11708 /* Called from do_type_instantiation through binding_table_foreach to
11709 do recursive instantiation for the type bound in ENTRY. */
11711 bt_instantiate_type_proc (binding_entry entry, void *data)
11713 tree storage = *(tree *) data;
11715 if (IS_AGGR_TYPE (entry->type)
11716 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11717 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11720 /* Called from do_type_instantiation to instantiate a member
11721 (a member function or a static member variable) of an
11722 explicitly instantiated class template. */
11724 instantiate_class_member (tree decl, int extern_p)
11726 mark_decl_instantiated (decl, extern_p);
11728 instantiate_decl (decl, /*defer_ok=*/1,
11729 /*expl_inst_class_mem_p=*/true);
11732 /* Perform an explicit instantiation of template class T. STORAGE, if
11733 non-null, is the RID for extern, inline or static. COMPLAIN is
11734 nonzero if this is called from the parser, zero if called recursively,
11735 since the standard is unclear (as detailed below). */
11738 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
11743 int previous_instantiation_extern_p = 0;
11745 if (TREE_CODE (t) == TYPE_DECL)
11748 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
11750 error ("explicit instantiation of non-template type %qT", t);
11756 if (!COMPLETE_TYPE_P (t))
11758 if (complain & tf_error)
11759 error ("explicit instantiation of %q#T before definition of template",
11764 if (storage != NULL_TREE)
11766 if (pedantic && !in_system_header)
11767 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11770 if (storage == ridpointers[(int) RID_INLINE])
11772 else if (storage == ridpointers[(int) RID_EXTERN])
11774 else if (storage == ridpointers[(int) RID_STATIC])
11778 error ("storage class %qD applied to template instantiation",
11784 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
11786 /* DR 259 [temp.spec].
11788 Both an explicit instantiation and a declaration of an explicit
11789 specialization shall not appear in a program unless the explicit
11790 instantiation follows a declaration of the explicit specialization.
11792 For a given set of template parameters, if an explicit
11793 instantiation of a template appears after a declaration of an
11794 explicit specialization for that template, the explicit
11795 instantiation has no effect. */
11798 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
11802 No program shall explicitly instantiate any template more
11805 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11806 instantiation was `extern'. If EXTERN_P then the second is.
11807 These cases are OK. */
11808 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11810 if (!previous_instantiation_extern_p && !extern_p
11811 && (complain & tf_error))
11812 pedwarn ("duplicate explicit instantiation of %q#T", t);
11814 /* If we've already instantiated the template, just return now. */
11815 if (!CLASSTYPE_INTERFACE_ONLY (t))
11819 check_explicit_instantiation_namespace (TYPE_NAME (t));
11820 mark_class_instantiated (t, extern_p);
11828 /* In contrast to implicit instantiation, where only the
11829 declarations, and not the definitions, of members are
11830 instantiated, we have here:
11834 The explicit instantiation of a class template specialization
11835 implies the instantiation of all of its members not
11836 previously explicitly specialized in the translation unit
11837 containing the explicit instantiation.
11839 Of course, we can't instantiate member template classes, since
11840 we don't have any arguments for them. Note that the standard
11841 is unclear on whether the instantiation of the members are
11842 *explicit* instantiations or not. However, the most natural
11843 interpretation is that it should be an explicit instantiation. */
11846 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
11847 if (TREE_CODE (tmp) == FUNCTION_DECL
11848 && DECL_TEMPLATE_INSTANTIATION (tmp))
11849 instantiate_class_member (tmp, extern_p);
11851 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11852 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
11853 instantiate_class_member (tmp, extern_p);
11855 if (CLASSTYPE_NESTED_UTDS (t))
11856 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
11857 bt_instantiate_type_proc, &storage);
11861 /* Given a function DECL, which is a specialization of TMPL, modify
11862 DECL to be a re-instantiation of TMPL with the same template
11863 arguments. TMPL should be the template into which tsubst'ing
11864 should occur for DECL, not the most general template.
11866 One reason for doing this is a scenario like this:
11869 void f(const T&, int i);
11871 void g() { f(3, 7); }
11874 void f(const T& t, const int i) { }
11876 Note that when the template is first instantiated, with
11877 instantiate_template, the resulting DECL will have no name for the
11878 first parameter, and the wrong type for the second. So, when we go
11879 to instantiate the DECL, we regenerate it. */
11882 regenerate_decl_from_template (tree decl, tree tmpl)
11884 /* The arguments used to instantiate DECL, from the most general
11889 args = DECL_TI_ARGS (decl);
11890 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11892 /* Make sure that we can see identifiers, and compute access
11894 push_access_scope (decl);
11896 if (TREE_CODE (decl) == FUNCTION_DECL)
11904 args_depth = TMPL_ARGS_DEPTH (args);
11905 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
11906 if (args_depth > parms_depth)
11907 args = get_innermost_template_args (args, parms_depth);
11909 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11910 args, tf_error, NULL_TREE);
11912 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11915 /* Merge parameter declarations. */
11916 decl_parm = skip_artificial_parms_for (decl,
11917 DECL_ARGUMENTS (decl));
11919 = skip_artificial_parms_for (code_pattern,
11920 DECL_ARGUMENTS (code_pattern));
11926 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11927 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11928 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11930 parm_type = type_decays_to (parm_type);
11931 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11932 TREE_TYPE (decl_parm) = parm_type;
11933 attributes = DECL_ATTRIBUTES (pattern_parm);
11934 if (DECL_ATTRIBUTES (decl_parm) != attributes)
11936 DECL_ATTRIBUTES (decl_parm) = attributes;
11937 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
11939 decl_parm = TREE_CHAIN (decl_parm);
11940 pattern_parm = TREE_CHAIN (pattern_parm);
11943 /* Merge additional specifiers from the CODE_PATTERN. */
11944 if (DECL_DECLARED_INLINE_P (code_pattern)
11945 && !DECL_DECLARED_INLINE_P (decl))
11946 DECL_DECLARED_INLINE_P (decl) = 1;
11947 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11948 DECL_INLINE (decl) = 1;
11950 else if (TREE_CODE (decl) == VAR_DECL)
11951 DECL_INITIAL (decl) =
11952 tsubst_expr (DECL_INITIAL (code_pattern), args,
11953 tf_error, DECL_TI_TEMPLATE (decl),
11954 /*integral_constant_expression_p=*/false);
11956 gcc_unreachable ();
11958 pop_access_scope (decl);
11961 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
11962 substituted to get DECL. */
11965 template_for_substitution (tree decl)
11967 tree tmpl = DECL_TI_TEMPLATE (decl);
11969 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
11970 for the instantiation. This is not always the most general
11971 template. Consider, for example:
11974 struct S { template <class U> void f();
11975 template <> void f<int>(); };
11977 and an instantiation of S<double>::f<int>. We want TD to be the
11978 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
11979 while (/* An instantiation cannot have a definition, so we need a
11980 more general template. */
11981 DECL_TEMPLATE_INSTANTIATION (tmpl)
11982 /* We must also deal with friend templates. Given:
11984 template <class T> struct S {
11985 template <class U> friend void f() {};
11988 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
11989 so far as the language is concerned, but that's still
11990 where we get the pattern for the instantiation from. On
11991 other hand, if the definition comes outside the class, say:
11993 template <class T> struct S {
11994 template <class U> friend void f();
11996 template <class U> friend void f() {}
11998 we don't need to look any further. That's what the check for
11999 DECL_INITIAL is for. */
12000 || (TREE_CODE (decl) == FUNCTION_DECL
12001 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
12002 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
12004 /* The present template, TD, should not be a definition. If it
12005 were a definition, we should be using it! Note that we
12006 cannot restructure the loop to just keep going until we find
12007 a template with a definition, since that might go too far if
12008 a specialization was declared, but not defined. */
12009 gcc_assert (TREE_CODE (decl) != VAR_DECL
12010 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
12012 /* Fetch the more general template. */
12013 tmpl = DECL_TI_TEMPLATE (tmpl);
12019 /* Produce the definition of D, a _DECL generated from a template. If
12020 DEFER_OK is nonzero, then we don't have to actually do the
12021 instantiation now; we just have to do it sometime. Normally it is
12022 an error if this is an explicit instantiation but D is undefined.
12023 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
12024 explicitly instantiated class template. */
12027 instantiate_decl (tree d, int defer_ok,
12028 bool expl_inst_class_mem_p)
12030 tree tmpl = DECL_TI_TEMPLATE (d);
12037 bool pattern_defined;
12039 location_t saved_loc = input_location;
12040 int saved_in_system_header = in_system_header;
12043 /* This function should only be used to instantiate templates for
12044 functions and static member variables. */
12045 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
12046 || TREE_CODE (d) == VAR_DECL);
12048 /* Variables are never deferred; if instantiation is required, they
12049 are instantiated right away. That allows for better code in the
12050 case that an expression refers to the value of the variable --
12051 if the variable has a constant value the referring expression can
12052 take advantage of that fact. */
12053 if (TREE_CODE (d) == VAR_DECL)
12056 /* Don't instantiate cloned functions. Instead, instantiate the
12057 functions they cloned. */
12058 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
12059 d = DECL_CLONED_FUNCTION (d);
12061 if (DECL_TEMPLATE_INSTANTIATED (d))
12062 /* D has already been instantiated. It might seem reasonable to
12063 check whether or not D is an explicit instantiation, and, if so,
12064 stop here. But when an explicit instantiation is deferred
12065 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
12066 is set, even though we still need to do the instantiation. */
12069 /* If we already have a specialization of this declaration, then
12070 there's no reason to instantiate it. Note that
12071 retrieve_specialization gives us both instantiations and
12072 specializations, so we must explicitly check
12073 DECL_TEMPLATE_SPECIALIZATION. */
12074 gen_tmpl = most_general_template (tmpl);
12075 gen_args = DECL_TI_ARGS (d);
12076 spec = retrieve_specialization (gen_tmpl, gen_args,
12077 /*class_specializations_p=*/false);
12078 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
12081 /* This needs to happen before any tsubsting. */
12082 if (! push_tinst_level (d))
12085 timevar_push (TV_PARSE);
12087 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
12088 for the instantiation. */
12089 td = template_for_substitution (d);
12090 code_pattern = DECL_TEMPLATE_RESULT (td);
12092 /* We should never be trying to instantiate a member of a class
12093 template or partial specialization. */
12094 gcc_assert (d != code_pattern);
12096 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
12097 || DECL_TEMPLATE_SPECIALIZATION (td))
12098 /* In the case of a friend template whose definition is provided
12099 outside the class, we may have too many arguments. Drop the
12100 ones we don't need. The same is true for specializations. */
12101 args = get_innermost_template_args
12102 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
12106 if (TREE_CODE (d) == FUNCTION_DECL)
12107 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
12109 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
12111 /* We may be in the middle of deferred access check. Disable it now. */
12112 push_deferring_access_checks (dk_no_deferred);
12114 /* Unless an explicit instantiation directive has already determined
12115 the linkage of D, remember that a definition is available for
12117 if (pattern_defined
12118 && !DECL_INTERFACE_KNOWN (d)
12119 && !DECL_NOT_REALLY_EXTERN (d))
12120 mark_definable (d);
12122 input_location = DECL_SOURCE_LOCATION (d);
12123 in_system_header = DECL_IN_SYSTEM_HEADER (d);
12125 /* If D is a member of an explicitly instantiated class template,
12126 and no definition is available, treat it like an implicit
12128 if (!pattern_defined && expl_inst_class_mem_p
12129 && DECL_EXPLICIT_INSTANTIATION (d))
12131 DECL_NOT_REALLY_EXTERN (d) = 0;
12132 DECL_INTERFACE_KNOWN (d) = 0;
12133 SET_DECL_IMPLICIT_INSTANTIATION (d);
12138 /* Recheck the substitutions to obtain any warning messages
12139 about ignoring cv qualifiers. */
12140 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
12141 tree type = TREE_TYPE (gen);
12143 /* Make sure that we can see identifiers, and compute access
12144 correctly. D is already the target FUNCTION_DECL with the
12146 push_access_scope (d);
12148 if (TREE_CODE (gen) == FUNCTION_DECL)
12150 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
12151 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
12152 tf_warning_or_error, d);
12153 /* Don't simply tsubst the function type, as that will give
12154 duplicate warnings about poor parameter qualifications.
12155 The function arguments are the same as the decl_arguments
12156 without the top level cv qualifiers. */
12157 type = TREE_TYPE (type);
12159 tsubst (type, gen_args, tf_warning_or_error, d);
12161 pop_access_scope (d);
12164 /* Check to see whether we know that this template will be
12165 instantiated in some other file, as with "extern template"
12167 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
12168 /* In general, we do not instantiate such templates... */
12170 /* ... but we instantiate inline functions so that we can inline
12172 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
12173 /* ... we instantiate static data members whose values are
12174 needed in integral constant expressions. */
12175 && ! (TREE_CODE (d) == VAR_DECL
12176 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
12178 /* Defer all other templates, unless we have been explicitly
12179 forbidden from doing so. */
12180 if (/* If there is no definition, we cannot instantiate the
12183 /* If it's OK to postpone instantiation, do so. */
12185 /* If this is a static data member that will be defined
12186 elsewhere, we don't want to instantiate the entire data
12187 member, but we do want to instantiate the initializer so that
12188 we can substitute that elsewhere. */
12189 || (external_p && TREE_CODE (d) == VAR_DECL))
12191 /* The definition of the static data member is now required so
12192 we must substitute the initializer. */
12193 if (TREE_CODE (d) == VAR_DECL
12194 && !DECL_INITIAL (d)
12195 && DECL_INITIAL (code_pattern))
12200 ns = decl_namespace_context (d);
12201 push_nested_namespace (ns);
12202 push_nested_class (DECL_CONTEXT (d));
12203 init = tsubst_expr (DECL_INITIAL (code_pattern),
12205 tf_warning_or_error, NULL_TREE,
12206 /*integral_constant_expression_p=*/false);
12207 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
12208 /*asmspec_tree=*/NULL_TREE,
12209 LOOKUP_ONLYCONVERTING);
12210 pop_nested_class ();
12211 pop_nested_namespace (ns);
12214 /* We restore the source position here because it's used by
12215 add_pending_template. */
12216 input_location = saved_loc;
12218 if (at_eof && !pattern_defined
12219 && DECL_EXPLICIT_INSTANTIATION (d))
12222 The definition of a non-exported function template, a
12223 non-exported member function template, or a non-exported
12224 member function or static data member of a class template
12225 shall be present in every translation unit in which it is
12226 explicitly instantiated. */
12228 ("explicit instantiation of %qD but no definition available", d);
12230 /* ??? Historically, we have instantiated inline functions, even
12231 when marked as "extern template". */
12232 if (!(external_p && TREE_CODE (d) == VAR_DECL))
12233 add_pending_template (d);
12236 /* Tell the repository that D is available in this translation unit
12237 -- and see if it is supposed to be instantiated here. */
12238 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
12240 /* In a PCH file, despite the fact that the repository hasn't
12241 requested instantiation in the PCH it is still possible that
12242 an instantiation will be required in a file that includes the
12245 add_pending_template (d);
12246 /* Instantiate inline functions so that the inliner can do its
12247 job, even though we'll not be emitting a copy of this
12249 if (!(TREE_CODE (d) == FUNCTION_DECL
12250 && flag_inline_trees
12251 && DECL_DECLARED_INLINE_P (d)))
12255 need_push = !cfun || !global_bindings_p ();
12257 push_to_top_level ();
12259 /* Mark D as instantiated so that recursive calls to
12260 instantiate_decl do not try to instantiate it again. */
12261 DECL_TEMPLATE_INSTANTIATED (d) = 1;
12263 /* Regenerate the declaration in case the template has been modified
12264 by a subsequent redeclaration. */
12265 regenerate_decl_from_template (d, td);
12267 /* We already set the file and line above. Reset them now in case
12268 they changed as a result of calling regenerate_decl_from_template. */
12269 input_location = DECL_SOURCE_LOCATION (d);
12271 if (TREE_CODE (d) == VAR_DECL)
12275 /* Clear out DECL_RTL; whatever was there before may not be right
12276 since we've reset the type of the declaration. */
12277 SET_DECL_RTL (d, NULL_RTX);
12278 DECL_IN_AGGR_P (d) = 0;
12280 /* The initializer is placed in DECL_INITIAL by
12281 regenerate_decl_from_template. Pull it out so that
12282 finish_decl can process it. */
12283 init = DECL_INITIAL (d);
12284 DECL_INITIAL (d) = NULL_TREE;
12285 DECL_INITIALIZED_P (d) = 0;
12287 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
12288 initializer. That function will defer actual emission until
12289 we have a chance to determine linkage. */
12290 DECL_EXTERNAL (d) = 0;
12292 /* Enter the scope of D so that access-checking works correctly. */
12293 push_nested_class (DECL_CONTEXT (d));
12294 finish_decl (d, init, NULL_TREE);
12295 pop_nested_class ();
12297 else if (TREE_CODE (d) == FUNCTION_DECL)
12299 htab_t saved_local_specializations;
12304 /* Save away the current list, in case we are instantiating one
12305 template from within the body of another. */
12306 saved_local_specializations = local_specializations;
12308 /* Set up the list of local specializations. */
12309 local_specializations = htab_create (37,
12310 hash_local_specialization,
12311 eq_local_specializations,
12314 /* Set up context. */
12315 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
12317 /* Create substitution entries for the parameters. */
12318 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
12319 tmpl_parm = DECL_ARGUMENTS (subst_decl);
12320 spec_parm = DECL_ARGUMENTS (d);
12321 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
12323 register_local_specialization (spec_parm, tmpl_parm);
12324 spec_parm = skip_artificial_parms_for (d, spec_parm);
12325 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
12329 register_local_specialization (spec_parm, tmpl_parm);
12330 tmpl_parm = TREE_CHAIN (tmpl_parm);
12331 spec_parm = TREE_CHAIN (spec_parm);
12333 gcc_assert (!spec_parm);
12335 /* Substitute into the body of the function. */
12336 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
12337 tf_warning_or_error, tmpl,
12338 /*integral_constant_expression_p=*/false);
12340 /* We don't need the local specializations any more. */
12341 htab_delete (local_specializations);
12342 local_specializations = saved_local_specializations;
12344 /* Finish the function. */
12345 d = finish_function (0);
12346 expand_or_defer_fn (d);
12349 /* We're not deferring instantiation any more. */
12350 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
12353 pop_from_top_level ();
12356 input_location = saved_loc;
12357 in_system_header = saved_in_system_header;
12358 pop_deferring_access_checks ();
12359 pop_tinst_level ();
12361 timevar_pop (TV_PARSE);
12366 /* Run through the list of templates that we wish we could
12367 instantiate, and instantiate any we can. RETRIES is the
12368 number of times we retry pending template instantiation. */
12371 instantiate_pending_templates (int retries)
12374 tree last = NULL_TREE;
12376 location_t saved_loc = input_location;
12377 int saved_in_system_header = in_system_header;
12379 /* Instantiating templates may trigger vtable generation. This in turn
12380 may require further template instantiations. We place a limit here
12381 to avoid infinite loop. */
12382 if (pending_templates && retries >= max_tinst_depth)
12384 tree decl = TREE_VALUE (pending_templates);
12386 error ("template instantiation depth exceeds maximum of %d"
12387 " instantiating %q+D, possibly from virtual table generation"
12388 " (use -ftemplate-depth-NN to increase the maximum)",
12389 max_tinst_depth, decl);
12390 if (TREE_CODE (decl) == FUNCTION_DECL)
12391 /* Pretend that we defined it. */
12392 DECL_INITIAL (decl) = error_mark_node;
12400 t = &pending_templates;
12403 tree instantiation = TREE_VALUE (*t);
12405 reopen_tinst_level (TREE_PURPOSE (*t));
12407 if (TYPE_P (instantiation))
12411 if (!COMPLETE_TYPE_P (instantiation))
12413 instantiate_class_template (instantiation);
12414 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
12415 for (fn = TYPE_METHODS (instantiation);
12417 fn = TREE_CHAIN (fn))
12418 if (! DECL_ARTIFICIAL (fn))
12419 instantiate_decl (fn,
12421 /*expl_inst_class_mem_p=*/false);
12422 if (COMPLETE_TYPE_P (instantiation))
12426 if (COMPLETE_TYPE_P (instantiation))
12427 /* If INSTANTIATION has been instantiated, then we don't
12428 need to consider it again in the future. */
12429 *t = TREE_CHAIN (*t);
12433 t = &TREE_CHAIN (*t);
12438 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
12439 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
12442 = instantiate_decl (instantiation,
12444 /*expl_inst_class_mem_p=*/false);
12445 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
12449 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
12450 || DECL_TEMPLATE_INSTANTIATED (instantiation))
12451 /* If INSTANTIATION has been instantiated, then we don't
12452 need to consider it again in the future. */
12453 *t = TREE_CHAIN (*t);
12457 t = &TREE_CHAIN (*t);
12461 current_tinst_level = NULL_TREE;
12463 last_pending_template = last;
12465 while (reconsider);
12467 input_location = saved_loc;
12468 in_system_header = saved_in_system_header;
12471 /* Substitute ARGVEC into T, which is a list of initializers for
12472 either base class or a non-static data member. The TREE_PURPOSEs
12473 are DECLs, and the TREE_VALUEs are the initializer values. Used by
12474 instantiate_decl. */
12477 tsubst_initializer_list (tree t, tree argvec)
12479 tree inits = NULL_TREE;
12481 for (; t; t = TREE_CHAIN (t))
12486 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error,
12488 decl = expand_member_init (decl);
12489 if (decl && !DECL_P (decl))
12490 in_base_initializer = 1;
12492 init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error,
12494 /*integral_constant_expression_p=*/false);
12495 in_base_initializer = 0;
12499 init = build_tree_list (decl, init);
12500 TREE_CHAIN (init) = inits;
12507 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
12510 set_current_access_from_decl (tree decl)
12512 if (TREE_PRIVATE (decl))
12513 current_access_specifier = access_private_node;
12514 else if (TREE_PROTECTED (decl))
12515 current_access_specifier = access_protected_node;
12517 current_access_specifier = access_public_node;
12520 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
12521 is the instantiation (which should have been created with
12522 start_enum) and ARGS are the template arguments to use. */
12525 tsubst_enum (tree tag, tree newtag, tree args)
12529 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
12534 decl = TREE_VALUE (e);
12535 /* Note that in a template enum, the TREE_VALUE is the
12536 CONST_DECL, not the corresponding INTEGER_CST. */
12537 value = tsubst_expr (DECL_INITIAL (decl),
12538 args, tf_warning_or_error, NULL_TREE,
12539 /*integral_constant_expression_p=*/true);
12541 /* Give this enumeration constant the correct access. */
12542 set_current_access_from_decl (decl);
12544 /* Actually build the enumerator itself. */
12545 build_enumerator (DECL_NAME (decl), value, newtag);
12548 finish_enum (newtag);
12549 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12550 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
12553 /* DECL is a FUNCTION_DECL that is a template specialization. Return
12554 its type -- but without substituting the innermost set of template
12555 arguments. So, innermost set of template parameters will appear in
12559 get_mostly_instantiated_function_type (tree decl)
12567 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12568 targs = DECL_TI_ARGS (decl);
12569 tparms = DECL_TEMPLATE_PARMS (tmpl);
12570 parm_depth = TMPL_PARMS_DEPTH (tparms);
12572 /* There should be as many levels of arguments as there are levels
12574 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
12576 fn_type = TREE_TYPE (tmpl);
12578 if (parm_depth == 1)
12579 /* No substitution is necessary. */
12583 int i, save_access_control;
12586 /* Replace the innermost level of the TARGS with NULL_TREEs to
12587 let tsubst know not to substitute for those parameters. */
12588 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
12589 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
12590 SET_TMPL_ARGS_LEVEL (partial_args, i,
12591 TMPL_ARGS_LEVEL (targs, i));
12592 SET_TMPL_ARGS_LEVEL (partial_args,
12593 TMPL_ARGS_DEPTH (targs),
12594 make_tree_vec (DECL_NTPARMS (tmpl)));
12596 /* Disable access control as this function is used only during
12598 save_access_control = flag_access_control;
12599 flag_access_control = 0;
12601 ++processing_template_decl;
12602 /* Now, do the (partial) substitution to figure out the
12603 appropriate function type. */
12604 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
12605 --processing_template_decl;
12607 /* Substitute into the template parameters to obtain the real
12608 innermost set of parameters. This step is important if the
12609 innermost set of template parameters contains value
12610 parameters whose types depend on outer template parameters. */
12611 TREE_VEC_LENGTH (partial_args)--;
12612 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
12614 flag_access_control = save_access_control;
12620 /* Return truthvalue if we're processing a template different from
12621 the last one involved in diagnostics. */
12623 problematic_instantiation_changed (void)
12625 return last_template_error_tick != tinst_level_tick;
12628 /* Remember current template involved in diagnostics. */
12630 record_last_problematic_instantiation (void)
12632 last_template_error_tick = tinst_level_tick;
12636 current_instantiation (void)
12638 return current_tinst_level;
12641 /* [temp.param] Check that template non-type parm TYPE is of an allowable
12642 type. Return zero for ok, nonzero for disallowed. Issue error and
12643 warning messages under control of COMPLAIN. */
12646 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
12648 if (INTEGRAL_TYPE_P (type))
12650 else if (POINTER_TYPE_P (type))
12652 else if (TYPE_PTR_TO_MEMBER_P (type))
12654 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12656 else if (TREE_CODE (type) == TYPENAME_TYPE)
12659 if (complain & tf_error)
12660 error ("%q#T is not a valid type for a template constant parameter", type);
12664 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12665 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
12668 dependent_type_p_r (tree type)
12674 A type is dependent if it is:
12676 -- a template parameter. Template template parameters are types
12677 for us (since TYPE_P holds true for them) so we handle
12679 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
12680 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
12682 /* -- a qualified-id with a nested-name-specifier which contains a
12683 class-name that names a dependent type or whose unqualified-id
12684 names a dependent type. */
12685 if (TREE_CODE (type) == TYPENAME_TYPE)
12687 /* -- a cv-qualified type where the cv-unqualified type is
12689 type = TYPE_MAIN_VARIANT (type);
12690 /* -- a compound type constructed from any dependent type. */
12691 if (TYPE_PTR_TO_MEMBER_P (type))
12692 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
12693 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
12695 else if (TREE_CODE (type) == POINTER_TYPE
12696 || TREE_CODE (type) == REFERENCE_TYPE)
12697 return dependent_type_p (TREE_TYPE (type));
12698 else if (TREE_CODE (type) == FUNCTION_TYPE
12699 || TREE_CODE (type) == METHOD_TYPE)
12703 if (dependent_type_p (TREE_TYPE (type)))
12705 for (arg_type = TYPE_ARG_TYPES (type);
12707 arg_type = TREE_CHAIN (arg_type))
12708 if (dependent_type_p (TREE_VALUE (arg_type)))
12712 /* -- an array type constructed from any dependent type or whose
12713 size is specified by a constant expression that is
12714 value-dependent. */
12715 if (TREE_CODE (type) == ARRAY_TYPE)
12717 if (TYPE_DOMAIN (type)
12718 && ((value_dependent_expression_p
12719 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12720 || (type_dependent_expression_p
12721 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12723 return dependent_type_p (TREE_TYPE (type));
12726 /* -- a template-id in which either the template name is a template
12728 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
12730 /* ... or any of the template arguments is a dependent type or
12731 an expression that is type-dependent or value-dependent. */
12732 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
12733 && (any_dependent_template_arguments_p
12734 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
12737 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12738 expression is not type-dependent, then it should already been
12740 if (TREE_CODE (type) == TYPEOF_TYPE)
12743 /* The standard does not specifically mention types that are local
12744 to template functions or local classes, but they should be
12745 considered dependent too. For example:
12747 template <int I> void f() {
12752 The size of `E' cannot be known until the value of `I' has been
12753 determined. Therefore, `E' must be considered dependent. */
12754 scope = TYPE_CONTEXT (type);
12755 if (scope && TYPE_P (scope))
12756 return dependent_type_p (scope);
12757 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12758 return type_dependent_expression_p (scope);
12760 /* Other types are non-dependent. */
12764 /* Returns TRUE if TYPE is dependent, in the sense of
12765 [temp.dep.type]. */
12768 dependent_type_p (tree type)
12770 /* If there are no template parameters in scope, then there can't be
12771 any dependent types. */
12772 if (!processing_template_decl)
12774 /* If we are not processing a template, then nobody should be
12775 providing us with a dependent type. */
12777 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
12781 /* If the type is NULL, we have not computed a type for the entity
12782 in question; in that case, the type is dependent. */
12786 /* Erroneous types can be considered non-dependent. */
12787 if (type == error_mark_node)
12790 /* If we have not already computed the appropriate value for TYPE,
12792 if (!TYPE_DEPENDENT_P_VALID (type))
12794 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12795 TYPE_DEPENDENT_P_VALID (type) = 1;
12798 return TYPE_DEPENDENT_P (type);
12801 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12804 dependent_scope_ref_p (tree expression, bool criterion (tree))
12809 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
12811 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12814 scope = TREE_OPERAND (expression, 0);
12815 name = TREE_OPERAND (expression, 1);
12819 An id-expression is type-dependent if it contains a
12820 nested-name-specifier that contains a class-name that names a
12822 /* The suggested resolution to Core Issue 2 implies that if the
12823 qualifying type is the current class, then we must peek
12826 && currently_open_class (scope)
12827 && !criterion (name))
12829 if (dependent_type_p (scope))
12835 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12836 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
12840 value_dependent_expression_p (tree expression)
12842 if (!processing_template_decl)
12845 /* A name declared with a dependent type. */
12846 if (DECL_P (expression) && type_dependent_expression_p (expression))
12849 switch (TREE_CODE (expression))
12851 case IDENTIFIER_NODE:
12852 /* A name that has not been looked up -- must be dependent. */
12855 case TEMPLATE_PARM_INDEX:
12856 /* A non-type template parm. */
12860 /* A non-type template parm. */
12861 if (DECL_TEMPLATE_PARM_P (expression))
12866 /* A constant with integral or enumeration type and is initialized
12867 with an expression that is value-dependent. */
12868 if (DECL_INITIAL (expression)
12869 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12870 && value_dependent_expression_p (DECL_INITIAL (expression)))
12874 case DYNAMIC_CAST_EXPR:
12875 case STATIC_CAST_EXPR:
12876 case CONST_CAST_EXPR:
12877 case REINTERPRET_CAST_EXPR:
12879 /* These expressions are value-dependent if the type to which
12880 the cast occurs is dependent or the expression being casted
12881 is value-dependent. */
12883 tree type = TREE_TYPE (expression);
12885 if (dependent_type_p (type))
12888 /* A functional cast has a list of operands. */
12889 expression = TREE_OPERAND (expression, 0);
12892 /* If there are no operands, it must be an expression such
12893 as "int()". This should not happen for aggregate types
12894 because it would form non-constant expressions. */
12895 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
12900 if (TREE_CODE (expression) == TREE_LIST)
12901 return any_value_dependent_elements_p (expression);
12903 return value_dependent_expression_p (expression);
12908 /* A `sizeof' expression is value-dependent if the operand is
12910 expression = TREE_OPERAND (expression, 0);
12911 if (TYPE_P (expression))
12912 return dependent_type_p (expression);
12913 return type_dependent_expression_p (expression);
12916 return dependent_scope_ref_p (expression, value_dependent_expression_p);
12918 case COMPONENT_REF:
12919 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12920 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12923 /* A CALL_EXPR may appear in a constant expression if it is a
12924 call to a builtin function, e.g., __builtin_constant_p. All
12925 such calls are value-dependent. */
12929 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
12930 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
12933 /* A constant expression is value-dependent if any subexpression is
12934 value-dependent. */
12935 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12937 case tcc_reference:
12939 return (value_dependent_expression_p
12940 (TREE_OPERAND (expression, 0)));
12942 case tcc_comparison:
12944 return ((value_dependent_expression_p
12945 (TREE_OPERAND (expression, 0)))
12946 || (value_dependent_expression_p
12947 (TREE_OPERAND (expression, 1))));
12949 case tcc_expression:
12952 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
12953 /* In some cases, some of the operands may be missing.
12954 (For example, in the case of PREDECREMENT_EXPR, the
12955 amount to increment by may be missing.) That doesn't
12956 make the expression dependent. */
12957 if (TREE_OPERAND (expression, i)
12958 && (value_dependent_expression_p
12959 (TREE_OPERAND (expression, i))))
12969 /* The expression is not value-dependent. */
12973 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
12974 [temp.dep.expr]. */
12977 type_dependent_expression_p (tree expression)
12979 if (!processing_template_decl)
12982 if (expression == error_mark_node)
12985 /* An unresolved name is always dependent. */
12986 if (TREE_CODE (expression) == IDENTIFIER_NODE
12987 || TREE_CODE (expression) == USING_DECL)
12990 /* Some expression forms are never type-dependent. */
12991 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
12992 || TREE_CODE (expression) == SIZEOF_EXPR
12993 || TREE_CODE (expression) == ALIGNOF_EXPR
12994 || TREE_CODE (expression) == TYPEID_EXPR
12995 || TREE_CODE (expression) == DELETE_EXPR
12996 || TREE_CODE (expression) == VEC_DELETE_EXPR
12997 || TREE_CODE (expression) == THROW_EXPR)
13000 /* The types of these expressions depends only on the type to which
13001 the cast occurs. */
13002 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
13003 || TREE_CODE (expression) == STATIC_CAST_EXPR
13004 || TREE_CODE (expression) == CONST_CAST_EXPR
13005 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
13006 || TREE_CODE (expression) == CAST_EXPR)
13007 return dependent_type_p (TREE_TYPE (expression));
13009 /* The types of these expressions depends only on the type created
13010 by the expression. */
13011 if (TREE_CODE (expression) == NEW_EXPR
13012 || TREE_CODE (expression) == VEC_NEW_EXPR)
13014 /* For NEW_EXPR tree nodes created inside a template, either
13015 the object type itself or a TREE_LIST may appear as the
13017 tree type = TREE_OPERAND (expression, 1);
13018 if (TREE_CODE (type) == TREE_LIST)
13019 /* This is an array type. We need to check array dimensions
13021 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
13022 || value_dependent_expression_p
13023 (TREE_OPERAND (TREE_VALUE (type), 1));
13025 return dependent_type_p (type);
13028 if (TREE_CODE (expression) == SCOPE_REF
13029 && dependent_scope_ref_p (expression,
13030 type_dependent_expression_p))
13033 if (TREE_CODE (expression) == FUNCTION_DECL
13034 && DECL_LANG_SPECIFIC (expression)
13035 && DECL_TEMPLATE_INFO (expression)
13036 && (any_dependent_template_arguments_p
13037 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
13040 if (TREE_CODE (expression) == TEMPLATE_DECL
13041 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
13044 if (TREE_TYPE (expression) == unknown_type_node)
13046 if (TREE_CODE (expression) == ADDR_EXPR)
13047 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
13048 if (TREE_CODE (expression) == COMPONENT_REF
13049 || TREE_CODE (expression) == OFFSET_REF)
13051 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
13053 expression = TREE_OPERAND (expression, 1);
13054 if (TREE_CODE (expression) == IDENTIFIER_NODE)
13057 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
13058 if (TREE_CODE (expression) == SCOPE_REF)
13061 if (TREE_CODE (expression) == BASELINK)
13062 expression = BASELINK_FUNCTIONS (expression);
13064 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
13066 if (any_dependent_template_arguments_p
13067 (TREE_OPERAND (expression, 1)))
13069 expression = TREE_OPERAND (expression, 0);
13071 gcc_assert (TREE_CODE (expression) == OVERLOAD
13072 || TREE_CODE (expression) == FUNCTION_DECL);
13076 if (type_dependent_expression_p (OVL_CURRENT (expression)))
13078 expression = OVL_NEXT (expression);
13083 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
13085 return (dependent_type_p (TREE_TYPE (expression)));
13088 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
13089 contains a type-dependent expression. */
13092 any_type_dependent_arguments_p (tree args)
13096 tree arg = TREE_VALUE (args);
13098 if (type_dependent_expression_p (arg))
13100 args = TREE_CHAIN (args);
13105 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
13106 expressions) contains any value-dependent expressions. */
13109 any_value_dependent_elements_p (tree list)
13111 for (; list; list = TREE_CHAIN (list))
13112 if (value_dependent_expression_p (TREE_VALUE (list)))
13118 /* Returns TRUE if the ARG (a template argument) is dependent. */
13121 dependent_template_arg_p (tree arg)
13123 if (!processing_template_decl)
13126 if (TREE_CODE (arg) == TEMPLATE_DECL
13127 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13128 return dependent_template_p (arg);
13129 else if (TYPE_P (arg))
13130 return dependent_type_p (arg);
13132 return (type_dependent_expression_p (arg)
13133 || value_dependent_expression_p (arg));
13136 /* Returns true if ARGS (a collection of template arguments) contains
13137 any dependent arguments. */
13140 any_dependent_template_arguments_p (tree args)
13147 if (args == error_mark_node)
13150 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
13152 tree level = TMPL_ARGS_LEVEL (args, i + 1);
13153 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
13154 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
13161 /* Returns TRUE if the template TMPL is dependent. */
13164 dependent_template_p (tree tmpl)
13166 if (TREE_CODE (tmpl) == OVERLOAD)
13170 if (dependent_template_p (OVL_FUNCTION (tmpl)))
13172 tmpl = OVL_CHAIN (tmpl);
13177 /* Template template parameters are dependent. */
13178 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
13179 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
13181 /* So are names that have not been looked up. */
13182 if (TREE_CODE (tmpl) == SCOPE_REF
13183 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
13185 /* So are member templates of dependent classes. */
13186 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
13187 return dependent_type_p (DECL_CONTEXT (tmpl));
13191 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
13194 dependent_template_id_p (tree tmpl, tree args)
13196 return (dependent_template_p (tmpl)
13197 || any_dependent_template_arguments_p (args));
13200 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13201 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
13202 can be found. Note that this function peers inside uninstantiated
13203 templates and therefore should be used only in extremely limited
13204 situations. ONLY_CURRENT_P restricts this peering to the currently
13205 open classes hierarchy (which is required when comparing types). */
13208 resolve_typename_type (tree type, bool only_current_p)
13216 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
13218 scope = TYPE_CONTEXT (type);
13219 name = TYPE_IDENTIFIER (type);
13221 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13222 it first before we can figure out what NAME refers to. */
13223 if (TREE_CODE (scope) == TYPENAME_TYPE)
13224 scope = resolve_typename_type (scope, only_current_p);
13225 /* If we don't know what SCOPE refers to, then we cannot resolve the
13227 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
13228 return error_mark_node;
13229 /* If the SCOPE is a template type parameter, we have no way of
13230 resolving the name. */
13231 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13233 /* If the SCOPE is not the current instantiation, there's no reason
13234 to look inside it. */
13235 if (only_current_p && !currently_open_class (scope))
13236 return error_mark_node;
13237 /* If SCOPE is a partial instantiation, it will not have a valid
13238 TYPE_FIELDS list, so use the original template. */
13239 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
13240 /* Enter the SCOPE so that name lookup will be resolved as if we
13241 were in the class definition. In particular, SCOPE will no
13242 longer be considered a dependent type. */
13243 pushed_scope = push_scope (scope);
13244 /* Look up the declaration. */
13245 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
13246 /* Obtain the set of qualifiers applied to the TYPE. */
13247 quals = cp_type_quals (type);
13248 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
13249 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
13251 type = error_mark_node;
13252 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
13253 && TREE_CODE (decl) == TYPE_DECL)
13254 type = TREE_TYPE (decl);
13255 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
13256 && DECL_CLASS_TEMPLATE_P (decl))
13260 /* Obtain the template and the arguments. */
13261 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
13262 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
13263 /* Instantiate the template. */
13264 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
13265 /*entering_scope=*/0, tf_error | tf_user);
13268 type = error_mark_node;
13269 /* Qualify the resulting type. */
13270 if (type != error_mark_node && quals)
13271 type = cp_build_qualified_type (type, quals);
13272 /* Leave the SCOPE. */
13274 pop_scope (pushed_scope);
13279 /* EXPR is an expression which is not type-dependent. Return a proxy
13280 for EXPR that can be used to compute the types of larger
13281 expressions containing EXPR. */
13284 build_non_dependent_expr (tree expr)
13288 /* Preserve null pointer constants so that the type of things like
13289 "p == 0" where "p" is a pointer can be determined. */
13290 if (null_ptr_cst_p (expr))
13292 /* Preserve OVERLOADs; the functions must be available to resolve
13295 if (TREE_CODE (inner_expr) == ADDR_EXPR)
13296 inner_expr = TREE_OPERAND (inner_expr, 0);
13297 if (TREE_CODE (inner_expr) == COMPONENT_REF)
13298 inner_expr = TREE_OPERAND (inner_expr, 1);
13299 if (is_overloaded_fn (inner_expr)
13300 || TREE_CODE (inner_expr) == OFFSET_REF)
13302 /* There is no need to return a proxy for a variable. */
13303 if (TREE_CODE (expr) == VAR_DECL)
13305 /* Preserve string constants; conversions from string constants to
13306 "char *" are allowed, even though normally a "const char *"
13307 cannot be used to initialize a "char *". */
13308 if (TREE_CODE (expr) == STRING_CST)
13310 /* Preserve arithmetic constants, as an optimization -- there is no
13311 reason to create a new node. */
13312 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
13314 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
13315 There is at least one place where we want to know that a
13316 particular expression is a throw-expression: when checking a ?:
13317 expression, there are special rules if the second or third
13318 argument is a throw-expression. */
13319 if (TREE_CODE (expr) == THROW_EXPR)
13322 if (TREE_CODE (expr) == COND_EXPR)
13323 return build3 (COND_EXPR,
13325 TREE_OPERAND (expr, 0),
13326 (TREE_OPERAND (expr, 1)
13327 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
13328 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
13329 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
13330 if (TREE_CODE (expr) == COMPOUND_EXPR
13331 && !COMPOUND_EXPR_OVERLOADED (expr))
13332 return build2 (COMPOUND_EXPR,
13334 TREE_OPERAND (expr, 0),
13335 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
13337 /* If the type is unknown, it can't really be non-dependent */
13338 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
13340 /* Otherwise, build a NON_DEPENDENT_EXPR.
13342 REFERENCE_TYPEs are not stripped for expressions in templates
13343 because doing so would play havoc with mangling. Consider, for
13346 template <typename T> void f<T& g>() { g(); }
13348 In the body of "f", the expression for "g" will have
13349 REFERENCE_TYPE, even though the standard says that it should
13350 not. The reason is that we must preserve the syntactic form of
13351 the expression so that mangling (say) "f<g>" inside the body of
13352 "f" works out correctly. Therefore, the REFERENCE_TYPE is
13354 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
13357 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
13358 Return a new TREE_LIST with the various arguments replaced with
13359 equivalent non-dependent expressions. */
13362 build_non_dependent_args (tree args)
13367 new_args = NULL_TREE;
13368 for (a = args; a; a = TREE_CHAIN (a))
13369 new_args = tree_cons (NULL_TREE,
13370 build_non_dependent_expr (TREE_VALUE (a)),
13372 return nreverse (new_args);
13375 #include "gt-cp-pt.h"