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 int 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 /* DECL might be a specialization of FN. */
1434 /* Adjust the type of DECL in case FN is a static member. */
1435 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1436 if (DECL_STATIC_FUNCTION_P (fn
)
1437 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1438 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1440 /* Check that the number of function parameters matches.
1442 template <class T> void f(int i = 0);
1443 template <> void f<int>();
1444 The specialization f<int> is invalid but is not caught
1445 by get_bindings below. */
1447 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1448 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1451 /* For a non-static member function, we need to make sure that
1452 the const qualification is the same. This can be done by
1453 checking the 'this' in the argument list. */
1454 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1455 && !same_type_p (TREE_VALUE (fn_arg_types
),
1456 TREE_VALUE (decl_arg_types
)))
1459 /* In case of explicit specialization, we need to check if
1460 the number of template headers appearing in the specialization
1461 is correct. This is usually done in check_explicit_specialization,
1462 but the check done there cannot be exhaustive when specializing
1463 member functions. Consider the following code:
1465 template <> void A<int>::f(int);
1466 template <> template <> void A<int>::f(int);
1468 Assuming that A<int> is not itself an explicit specialization
1469 already, the first line specializes "f" which is a non-template
1470 member function, whilst the second line specializes "f" which
1471 is a template member function. So both lines are syntactically
1472 correct, and check_explicit_specialization does not reject
1475 Here, we can do better, as we are matching the specialization
1476 against the declarations. We count the number of template
1477 headers, and we check if they match TEMPLATE_COUNT + 1
1478 (TEMPLATE_COUNT is the number of qualifying template classes,
1479 plus there must be another header for the member template
1482 Notice that if header_count is zero, this is not a
1483 specialization but rather a template instantiation, so there
1484 is no check we can perform here. */
1485 if (header_count
&& header_count
!= template_count
+ 1)
1488 /* Check that the number of template arguments at the
1489 innermost level for DECL is the same as for FN. */
1490 if (current_binding_level
->kind
== sk_template_parms
1491 && !current_binding_level
->explicit_spec_p
1492 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1493 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1494 (current_template_parms
))))
1497 /* Function templates cannot be specializations; there are
1498 no partial specializations of functions. Therefore, if
1499 the type of DECL does not match FN, there is no
1501 if (tsk
== tsk_template
)
1503 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1505 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1509 /* See whether this function might be a specialization of this
1511 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1514 /* We cannot deduce template arguments that when used to
1515 specialize TMPL will produce DECL. */
1518 /* Save this template, and the arguments deduced. */
1519 templates
= tree_cons (targs
, fn
, templates
);
1521 else if (need_member_template
)
1522 /* FN is an ordinary member function, and we need a
1523 specialization of a member template. */
1525 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1526 /* We can get IDENTIFIER_NODEs here in certain erroneous
1529 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1530 /* This is just an ordinary non-member function. Nothing can
1531 be a specialization of that. */
1533 else if (DECL_ARTIFICIAL (fn
))
1534 /* Cannot specialize functions that are created implicitly. */
1538 tree decl_arg_types
;
1540 /* This is an ordinary member function. However, since
1541 we're here, we can assume it's enclosing class is a
1542 template class. For example,
1544 template <typename T> struct S { void f(); };
1545 template <> void S<int>::f() {}
1547 Here, S<int>::f is a non-template, but S<int> is a
1548 template class. If FN has the same type as DECL, we
1549 might be in business. */
1551 if (!DECL_TEMPLATE_INFO (fn
))
1552 /* Its enclosing class is an explicit specialization
1553 of a template class. This is not a candidate. */
1556 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1557 TREE_TYPE (TREE_TYPE (fn
))))
1558 /* The return types differ. */
1561 /* Adjust the type of DECL in case FN is a static member. */
1562 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1563 if (DECL_STATIC_FUNCTION_P (fn
)
1564 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1565 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1567 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1570 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1574 if (templates
&& TREE_CHAIN (templates
))
1580 It is possible for a specialization with a given function
1581 signature to be instantiated from more than one function
1582 template. In such cases, explicit specification of the
1583 template arguments must be used to uniquely identify the
1584 function template specialization being specialized.
1586 Note that here, there's no suggestion that we're supposed to
1587 determine which of the candidate templates is most
1588 specialized. However, we, also have:
1592 Partial ordering of overloaded function template
1593 declarations is used in the following contexts to select
1594 the function template to which a function template
1595 specialization refers:
1597 -- when an explicit specialization refers to a function
1600 So, we do use the partial ordering rules, at least for now.
1601 This extension can only serve to make invalid programs valid,
1602 so it's safe. And, there is strong anecdotal evidence that
1603 the committee intended the partial ordering rules to apply;
1604 the EDG front-end has that behavior, and John Spicer claims
1605 that the committee simply forgot to delete the wording in
1606 [temp.expl.spec]. */
1607 tree tmpl
= most_specialized_instantiation (templates
);
1608 if (tmpl
!= error_mark_node
)
1611 TREE_CHAIN (templates
) = NULL_TREE
;
1615 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
1617 error ("template-id %qD for %q+D does not match any template "
1618 "declaration", template_id
, decl
);
1619 return error_mark_node
;
1621 else if ((templates
&& TREE_CHAIN (templates
))
1622 || (candidates
&& TREE_CHAIN (candidates
))
1623 || (templates
&& candidates
))
1625 error ("ambiguous template specialization %qD for %q+D",
1627 chainon (candidates
, templates
);
1628 print_candidates (candidates
);
1629 return error_mark_node
;
1632 /* We have one, and exactly one, match. */
1635 tree fn
= TREE_VALUE (candidates
);
1636 /* DECL is a re-declaration of a template function. */
1637 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1639 /* It was a specialization of an ordinary member function in a
1641 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
1642 return DECL_TI_TEMPLATE (fn
);
1645 /* It was a specialization of a template. */
1646 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
1647 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
1649 *targs_out
= copy_node (targs
);
1650 SET_TMPL_ARGS_LEVEL (*targs_out
,
1651 TMPL_ARGS_DEPTH (*targs_out
),
1652 TREE_PURPOSE (templates
));
1655 *targs_out
= TREE_PURPOSE (templates
);
1656 return TREE_VALUE (templates
);
1659 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1660 but with the default argument values filled in from those in the
1664 copy_default_args_to_explicit_spec_1 (tree spec_types
,
1667 tree new_spec_types
;
1672 if (spec_types
== void_list_node
)
1673 return void_list_node
;
1675 /* Substitute into the rest of the list. */
1677 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
1678 TREE_CHAIN (tmpl_types
));
1680 /* Add the default argument for this parameter. */
1681 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
1682 TREE_VALUE (spec_types
),
1686 /* DECL is an explicit specialization. Replicate default arguments
1687 from the template it specializes. (That way, code like:
1689 template <class T> void f(T = 3);
1690 template <> void f(double);
1693 works, as required.) An alternative approach would be to look up
1694 the correct default arguments at the call-site, but this approach
1695 is consistent with how implicit instantiations are handled. */
1698 copy_default_args_to_explicit_spec (tree decl
)
1703 tree new_spec_types
;
1707 tree object_type
= NULL_TREE
;
1708 tree in_charge
= NULL_TREE
;
1709 tree vtt
= NULL_TREE
;
1711 /* See if there's anything we need to do. */
1712 tmpl
= DECL_TI_TEMPLATE (decl
);
1713 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
1714 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
1715 if (TREE_PURPOSE (t
))
1720 old_type
= TREE_TYPE (decl
);
1721 spec_types
= TYPE_ARG_TYPES (old_type
);
1723 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1725 /* Remove the this pointer, but remember the object's type for
1727 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
1728 spec_types
= TREE_CHAIN (spec_types
);
1729 tmpl_types
= TREE_CHAIN (tmpl_types
);
1731 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
1733 /* DECL may contain more parameters than TMPL due to the extra
1734 in-charge parameter in constructors and destructors. */
1735 in_charge
= spec_types
;
1736 spec_types
= TREE_CHAIN (spec_types
);
1738 if (DECL_HAS_VTT_PARM_P (decl
))
1741 spec_types
= TREE_CHAIN (spec_types
);
1745 /* Compute the merged default arguments. */
1747 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
1749 /* Compute the new FUNCTION_TYPE. */
1753 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
1758 /* Put the in-charge parameter back. */
1759 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
1760 TREE_VALUE (in_charge
),
1763 new_type
= build_method_type_directly (object_type
,
1764 TREE_TYPE (old_type
),
1768 new_type
= build_function_type (TREE_TYPE (old_type
),
1770 new_type
= cp_build_type_attribute_variant (new_type
,
1771 TYPE_ATTRIBUTES (old_type
));
1772 new_type
= build_exception_variant (new_type
,
1773 TYPE_RAISES_EXCEPTIONS (old_type
));
1774 TREE_TYPE (decl
) = new_type
;
1777 /* Check to see if the function just declared, as indicated in
1778 DECLARATOR, and in DECL, is a specialization of a function
1779 template. We may also discover that the declaration is an explicit
1780 instantiation at this point.
1782 Returns DECL, or an equivalent declaration that should be used
1783 instead if all goes well. Issues an error message if something is
1784 amiss. Returns error_mark_node if the error is not easily
1787 FLAGS is a bitmask consisting of the following flags:
1789 2: The function has a definition.
1790 4: The function is a friend.
1792 The TEMPLATE_COUNT is the number of references to qualifying
1793 template classes that appeared in the name of the function. For
1796 template <class T> struct S { void f(); };
1799 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1800 classes are not counted in the TEMPLATE_COUNT, so that in
1802 template <class T> struct S {};
1803 template <> struct S<int> { void f(); }
1804 template <> void S<int>::f();
1806 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1807 invalid; there should be no template <>.)
1809 If the function is a specialization, it is marked as such via
1810 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1811 is set up correctly, and it is added to the list of specializations
1812 for that template. */
1815 check_explicit_specialization (tree declarator
,
1820 int have_def
= flags
& 2;
1821 int is_friend
= flags
& 4;
1822 int specialization
= 0;
1823 int explicit_instantiation
= 0;
1824 int member_specialization
= 0;
1825 tree ctype
= DECL_CLASS_CONTEXT (decl
);
1826 tree dname
= DECL_NAME (decl
);
1831 if (!processing_specialization
)
1834 tsk
= tsk_excessive_parms
;
1837 tsk
= current_tmpl_spec_kind (template_count
);
1842 if (processing_specialization
)
1845 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1847 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1850 /* This could be something like:
1852 template <class T> void f(T);
1853 class S { friend void f<>(int); } */
1857 /* This case handles bogus declarations like template <>
1858 template <class T> void f<int>(); */
1860 error ("template-id %qD in declaration of primary template",
1867 case tsk_invalid_member_spec
:
1868 /* The error has already been reported in
1869 check_specialization_scope. */
1870 return error_mark_node
;
1872 case tsk_invalid_expl_inst
:
1873 error ("template parameter list used in explicit instantiation");
1879 error ("definition provided for explicit instantiation");
1881 explicit_instantiation
= 1;
1884 case tsk_excessive_parms
:
1885 case tsk_insufficient_parms
:
1886 if (tsk
== tsk_excessive_parms
)
1887 error ("too many template parameter lists in declaration of %qD",
1889 else if (template_header_count
)
1890 error("too few template parameter lists in declaration of %qD", decl
);
1892 error("explicit specialization of %qD must be introduced by "
1893 "%<template <>%>", decl
);
1897 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
1899 member_specialization
= 1;
1905 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
1907 /* This case handles bogus declarations like template <>
1908 template <class T> void f<int>(); */
1910 if (uses_template_parms (declarator
))
1911 error ("function template partial specialization %qD "
1912 "is not allowed", declarator
);
1914 error ("template-id %qD in declaration of primary template",
1919 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
1920 /* This is a specialization of a member template, without
1921 specialization the containing class. Something like:
1923 template <class T> struct S {
1924 template <class U> void f (U);
1926 template <> template <class U> void S<int>::f(U) {}
1928 That's a specialization -- but of the entire template. */
1936 if (specialization
|| member_specialization
)
1938 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1939 for (; t
; t
= TREE_CHAIN (t
))
1940 if (TREE_PURPOSE (t
))
1943 ("default argument specified in explicit specialization");
1948 if (specialization
|| member_specialization
|| explicit_instantiation
)
1950 tree tmpl
= NULL_TREE
;
1951 tree targs
= NULL_TREE
;
1953 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1954 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
1958 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
1963 /* If there is no class context, the explicit instantiation
1964 must be at namespace scope. */
1965 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
1967 /* Find the namespace binding, using the declaration
1969 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
1971 if (!fns
|| !is_overloaded_fn (fns
))
1973 error ("%qD is not a template function", dname
);
1974 fns
= error_mark_node
;
1978 tree fn
= OVL_CURRENT (fns
);
1979 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
1980 CP_DECL_CONTEXT (fn
)))
1981 error ("%qD is not declared in %qD",
1982 decl
, current_namespace
);
1986 declarator
= lookup_template_function (fns
, NULL_TREE
);
1989 if (declarator
== error_mark_node
)
1990 return error_mark_node
;
1992 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1994 if (!explicit_instantiation
)
1995 /* A specialization in class scope. This is invalid,
1996 but the error will already have been flagged by
1997 check_specialization_scope. */
1998 return error_mark_node
;
2001 /* It's not valid to write an explicit instantiation in
2004 class C { template void f(); }
2006 This case is caught by the parser. However, on
2009 template class C { void f(); };
2011 (which is invalid) we can get here. The error will be
2018 else if (ctype
!= NULL_TREE
2019 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2022 /* Find the list of functions in ctype that have the same
2023 name as the declared function. */
2024 tree name
= TREE_OPERAND (declarator
, 0);
2025 tree fns
= NULL_TREE
;
2028 if (constructor_name_p (name
, ctype
))
2030 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2032 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
2033 : !CLASSTYPE_DESTRUCTORS (ctype
))
2035 /* From [temp.expl.spec]:
2037 If such an explicit specialization for the member
2038 of a class template names an implicitly-declared
2039 special member function (clause _special_), the
2040 program is ill-formed.
2042 Similar language is found in [temp.explicit]. */
2043 error ("specialization of implicitly-declared special member function");
2044 return error_mark_node
;
2047 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2050 if (!DECL_CONV_FN_P (decl
))
2052 idx
= lookup_fnfields_1 (ctype
, name
);
2054 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2058 VEC(tree
,gc
) *methods
;
2061 /* For a type-conversion operator, we cannot do a
2062 name-based lookup. We might be looking for `operator
2063 int' which will be a specialization of `operator T'.
2064 So, we find *all* the conversion operators, and then
2065 select from them. */
2068 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2070 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2071 VEC_iterate (tree
, methods
, idx
, ovl
);
2074 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2075 /* There are no more conversion functions. */
2078 /* Glue all these conversion functions together
2079 with those we already have. */
2080 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2081 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2085 if (fns
== NULL_TREE
)
2087 error ("no member function %qD declared in %qT", name
, ctype
);
2088 return error_mark_node
;
2091 TREE_OPERAND (declarator
, 0) = fns
;
2094 /* Figure out what exactly is being specialized at this point.
2095 Note that for an explicit instantiation, even one for a
2096 member function, we cannot tell apriori whether the
2097 instantiation is for a member template, or just a member
2098 function of a template class. Even if a member template is
2099 being instantiated, the member template arguments may be
2100 elided if they can be deduced from the rest of the
2102 tmpl
= determine_specialization (declarator
, decl
,
2104 member_specialization
,
2108 if (!tmpl
|| tmpl
== error_mark_node
)
2109 /* We couldn't figure out what this declaration was
2111 return error_mark_node
;
2114 tree gen_tmpl
= most_general_template (tmpl
);
2116 if (explicit_instantiation
)
2118 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2119 is done by do_decl_instantiation later. */
2121 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2122 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2124 if (arg_depth
> parm_depth
)
2126 /* If TMPL is not the most general template (for
2127 example, if TMPL is a friend template that is
2128 injected into namespace scope), then there will
2129 be too many levels of TARGS. Remove some of them
2134 new_targs
= make_tree_vec (parm_depth
);
2135 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2136 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2137 = TREE_VEC_ELT (targs
, i
);
2141 return instantiate_template (tmpl
, targs
, tf_error
);
2144 /* If we thought that the DECL was a member function, but it
2145 turns out to be specializing a static member function,
2146 make DECL a static member function as well. */
2147 if (DECL_STATIC_FUNCTION_P (tmpl
)
2148 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2149 revert_static_member_fn (decl
);
2151 /* If this is a specialization of a member template of a
2152 template class, we want to return the TEMPLATE_DECL, not
2153 the specialization of it. */
2154 if (tsk
== tsk_template
)
2156 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2157 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
)) = NULL_TREE
;
2160 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2161 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl
))
2162 = DECL_SOURCE_LOCATION (decl
);
2163 /* We want to use the argument list specified in the
2164 definition, not in the original declaration. */
2165 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl
))
2166 = DECL_ARGUMENTS (decl
);
2171 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2172 DECL_TEMPLATE_INFO (decl
) = tree_cons (tmpl
, targs
, NULL_TREE
);
2174 /* Inherit default function arguments from the template
2175 DECL is specializing. */
2176 copy_default_args_to_explicit_spec (decl
);
2178 /* This specialization has the same protection as the
2179 template it specializes. */
2180 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2181 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2183 /* If DECL is a friend declaration, declared using an
2184 unqualified name, the namespace associated with DECL may
2185 have been set incorrectly. For example, in:
2187 template <typename T> void f(T);
2189 struct S { friend void f<int>(int); }
2192 we will have set the DECL_CONTEXT for the friend
2193 declaration to N, rather than to the global namespace. */
2194 if (DECL_NAMESPACE_SCOPE_P (decl
))
2195 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2197 if (is_friend
&& !have_def
)
2198 /* This is not really a declaration of a specialization.
2199 It's just the name of an instantiation. But, it's not
2200 a request for an instantiation, either. */
2201 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2202 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2203 /* This is indeed a specialization. In case of constructors
2204 and destructors, we need in-charge and not-in-charge
2205 versions in V3 ABI. */
2206 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2208 /* Register this specialization so that we can find it
2210 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
);
2217 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2218 parameters. These are represented in the same format used for
2219 DECL_TEMPLATE_PARMS. */
2222 comp_template_parms (tree parms1
, tree parms2
)
2227 if (parms1
== parms2
)
2230 for (p1
= parms1
, p2
= parms2
;
2231 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2232 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2234 tree t1
= TREE_VALUE (p1
);
2235 tree t2
= TREE_VALUE (p2
);
2238 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2239 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2241 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2244 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2246 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2247 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2249 /* If either of the template parameters are invalid, assume
2250 they match for the sake of error recovery. */
2251 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2254 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2257 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
2259 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2264 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2265 /* One set of parameters has more parameters lists than the
2272 /* Complain if DECL shadows a template parameter.
2274 [temp.local]: A template-parameter shall not be redeclared within its
2275 scope (including nested scopes). */
2278 check_template_shadow (tree decl
)
2282 /* If we're not in a template, we can't possibly shadow a template
2284 if (!current_template_parms
)
2287 /* Figure out what we're shadowing. */
2288 if (TREE_CODE (decl
) == OVERLOAD
)
2289 decl
= OVL_CURRENT (decl
);
2290 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
2292 /* If there's no previous binding for this name, we're not shadowing
2293 anything, let alone a template parameter. */
2297 /* If we're not shadowing a template parameter, we're done. Note
2298 that OLDDECL might be an OVERLOAD (or perhaps even an
2299 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2301 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
2304 /* We check for decl != olddecl to avoid bogus errors for using a
2305 name inside a class. We check TPFI to avoid duplicate errors for
2306 inline member templates. */
2308 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
2311 error ("declaration of %q+#D", decl
);
2312 error (" shadows template parm %q+#D", olddecl
);
2315 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
2316 ORIG_LEVEL, DECL, and TYPE. */
2319 build_template_parm_index (int index
,
2325 tree t
= make_node (TEMPLATE_PARM_INDEX
);
2326 TEMPLATE_PARM_IDX (t
) = index
;
2327 TEMPLATE_PARM_LEVEL (t
) = level
;
2328 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
2329 TEMPLATE_PARM_DECL (t
) = decl
;
2330 TREE_TYPE (t
) = type
;
2331 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
2332 TREE_INVARIANT (t
) = TREE_INVARIANT (decl
);
2333 TREE_READONLY (t
) = TREE_READONLY (decl
);
2338 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
2339 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
2340 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2341 new one is created. */
2344 reduce_template_parm_level (tree index
, tree type
, int levels
)
2346 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
2347 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
2348 != TEMPLATE_PARM_LEVEL (index
) - levels
))
2350 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
2353 decl
= build_decl (TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
2354 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
2355 TREE_INVARIANT (decl
) = TREE_INVARIANT (orig_decl
);
2356 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
2357 DECL_ARTIFICIAL (decl
) = 1;
2358 SET_DECL_TEMPLATE_PARM_P (decl
);
2360 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
2361 TEMPLATE_PARM_LEVEL (index
) - levels
,
2362 TEMPLATE_PARM_ORIG_LEVEL (index
),
2364 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
2366 /* Template template parameters need this. */
2367 if (TREE_CODE (decl
) != CONST_DECL
)
2368 DECL_TEMPLATE_PARMS (decl
)
2369 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
2372 return TEMPLATE_PARM_DESCENDANTS (index
);
2375 /* Process information from new template parameter PARM and append it to the
2376 LIST being built. This new parameter is a non-type parameter iff
2377 IS_NON_TYPE is true. */
2380 process_template_parm (tree list
, tree parm
, bool is_non_type
)
2387 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
2388 defval
= TREE_PURPOSE (parm
);
2392 tree p
= tree_last (list
);
2394 if (p
&& TREE_VALUE (p
) != error_mark_node
)
2397 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
2398 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
2400 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
2410 parm
= TREE_VALUE (parm
);
2412 SET_DECL_TEMPLATE_PARM_P (parm
);
2414 if (TREE_TYPE (parm
) == error_mark_node
)
2416 err_parm_list
= build_tree_list (defval
, parm
);
2417 TREE_VALUE (err_parm_list
) = error_mark_node
;
2418 return chainon (list
, err_parm_list
);
2424 The top-level cv-qualifiers on the template-parameter are
2425 ignored when determining its type. */
2426 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
2427 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
2429 err_parm_list
= build_tree_list (defval
, parm
);
2430 TREE_VALUE (err_parm_list
) = error_mark_node
;
2431 return chainon (list
, err_parm_list
);
2435 /* A template parameter is not modifiable. */
2436 TREE_CONSTANT (parm
) = 1;
2437 TREE_INVARIANT (parm
) = 1;
2438 TREE_READONLY (parm
) = 1;
2439 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
2440 TREE_CONSTANT (decl
) = 1;
2441 TREE_INVARIANT (decl
) = 1;
2442 TREE_READONLY (decl
) = 1;
2443 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
2444 = build_template_parm_index (idx
, processing_template_decl
,
2445 processing_template_decl
,
2446 decl
, TREE_TYPE (parm
));
2451 parm
= TREE_VALUE (TREE_VALUE (parm
));
2453 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
2455 t
= make_aggr_type (TEMPLATE_TEMPLATE_PARM
);
2456 /* This is for distinguishing between real templates and template
2457 template parameters */
2458 TREE_TYPE (parm
) = t
;
2459 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
2464 t
= make_aggr_type (TEMPLATE_TYPE_PARM
);
2465 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
2466 decl
= build_decl (TYPE_DECL
, parm
, t
);
2469 TYPE_NAME (t
) = decl
;
2470 TYPE_STUB_DECL (t
) = decl
;
2472 TEMPLATE_TYPE_PARM_INDEX (t
)
2473 = build_template_parm_index (idx
, processing_template_decl
,
2474 processing_template_decl
,
2475 decl
, TREE_TYPE (parm
));
2477 DECL_ARTIFICIAL (decl
) = 1;
2478 SET_DECL_TEMPLATE_PARM_P (decl
);
2480 parm
= build_tree_list (defval
, parm
);
2481 return chainon (list
, parm
);
2484 /* The end of a template parameter list has been reached. Process the
2485 tree list into a parameter vector, converting each parameter into a more
2486 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2490 end_template_parm_list (tree parms
)
2494 tree saved_parmlist
= make_tree_vec (list_length (parms
));
2496 current_template_parms
2497 = tree_cons (size_int (processing_template_decl
),
2498 saved_parmlist
, current_template_parms
);
2500 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
2502 next
= TREE_CHAIN (parm
);
2503 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
2504 TREE_CHAIN (parm
) = NULL_TREE
;
2507 --processing_template_parmlist
;
2509 return saved_parmlist
;
2512 /* end_template_decl is called after a template declaration is seen. */
2515 end_template_decl (void)
2517 reset_specialization ();
2519 if (! processing_template_decl
)
2522 /* This matches the pushlevel in begin_template_parm_list. */
2525 --processing_template_decl
;
2526 current_template_parms
= TREE_CHAIN (current_template_parms
);
2529 /* Given a template argument vector containing the template PARMS.
2530 The innermost PARMS are given first. */
2533 current_template_args (void)
2536 tree args
= NULL_TREE
;
2537 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
2540 /* If there is only one level of template parameters, we do not
2541 create a TREE_VEC of TREE_VECs. Instead, we return a single
2542 TREE_VEC containing the arguments. */
2544 args
= make_tree_vec (length
);
2546 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
2548 tree a
= copy_node (TREE_VALUE (header
));
2551 TREE_TYPE (a
) = NULL_TREE
;
2552 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
2554 tree t
= TREE_VEC_ELT (a
, i
);
2556 /* T will be a list if we are called from within a
2557 begin/end_template_parm_list pair, but a vector directly
2558 if within a begin/end_member_template_processing pair. */
2559 if (TREE_CODE (t
) == TREE_LIST
)
2563 if (t
!= error_mark_node
)
2565 if (TREE_CODE (t
) == TYPE_DECL
2566 || TREE_CODE (t
) == TEMPLATE_DECL
)
2569 t
= DECL_INITIAL (t
);
2572 TREE_VEC_ELT (a
, i
) = t
;
2577 TREE_VEC_ELT (args
, --l
) = a
;
2585 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
2586 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
2587 a member template. Used by push_template_decl below. */
2590 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
2592 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
2593 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
2594 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
2595 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
2596 if (DECL_LANG_SPECIFIC (decl
))
2598 DECL_STATIC_FUNCTION_P (tmpl
) = DECL_STATIC_FUNCTION_P (decl
);
2599 DECL_CONSTRUCTOR_P (tmpl
) = DECL_CONSTRUCTOR_P (decl
);
2600 DECL_DESTRUCTOR_P (tmpl
) = DECL_DESTRUCTOR_P (decl
);
2601 DECL_NONCONVERTING_P (tmpl
) = DECL_NONCONVERTING_P (decl
);
2602 DECL_ASSIGNMENT_OPERATOR_P (tmpl
) = DECL_ASSIGNMENT_OPERATOR_P (decl
);
2603 if (DECL_OVERLOADED_OPERATOR_P (decl
))
2604 SET_OVERLOADED_OPERATOR_CODE (tmpl
,
2605 DECL_OVERLOADED_OPERATOR_P (decl
));
2611 struct template_parm_data
2613 /* The level of the template parameters we are currently
2617 /* The index of the specialization argument we are currently
2621 /* An array whose size is the number of template parameters. The
2622 elements are nonzero if the parameter has been used in any one
2623 of the arguments processed so far. */
2626 /* An array whose size is the number of template arguments. The
2627 elements are nonzero if the argument makes use of template
2628 parameters of this level. */
2629 int* arg_uses_template_parms
;
2632 /* Subroutine of push_template_decl used to see if each template
2633 parameter in a partial specialization is used in the explicit
2634 argument list. If T is of the LEVEL given in DATA (which is
2635 treated as a template_parm_data*), then DATA->PARMS is marked
2639 mark_template_parm (tree t
, void* data
)
2643 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
2645 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
2647 level
= TEMPLATE_PARM_LEVEL (t
);
2648 idx
= TEMPLATE_PARM_IDX (t
);
2652 level
= TEMPLATE_TYPE_LEVEL (t
);
2653 idx
= TEMPLATE_TYPE_IDX (t
);
2656 if (level
== tpd
->level
)
2658 tpd
->parms
[idx
] = 1;
2659 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
2662 /* Return zero so that for_each_template_parm will continue the
2663 traversal of the tree; we want to mark *every* template parm. */
2667 /* Process the partial specialization DECL. */
2670 process_partial_specialization (tree decl
)
2672 tree type
= TREE_TYPE (decl
);
2673 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
2674 tree specargs
= CLASSTYPE_TI_ARGS (type
);
2675 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
2676 tree inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
2677 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
2678 int nargs
= TREE_VEC_LENGTH (inner_args
);
2679 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2681 int did_error_intro
= 0;
2682 struct template_parm_data tpd
;
2683 struct template_parm_data tpd2
;
2685 /* We check that each of the template parameters given in the
2686 partial specialization is used in the argument list to the
2687 specialization. For example:
2689 template <class T> struct S;
2690 template <class T> struct S<T*>;
2692 The second declaration is OK because `T*' uses the template
2693 parameter T, whereas
2695 template <class T> struct S<int>;
2697 is no good. Even trickier is:
2708 The S2<T> declaration is actually invalid; it is a
2709 full-specialization. Of course,
2712 struct S2<T (*)(U)>;
2714 or some such would have been OK. */
2715 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
2716 tpd
.parms
= (int *) alloca (sizeof (int) * ntparms
);
2717 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
2719 tpd
.arg_uses_template_parms
= (int *) alloca (sizeof (int) * nargs
);
2720 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
2721 for (i
= 0; i
< nargs
; ++i
)
2723 tpd
.current_arg
= i
;
2724 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
2725 &mark_template_parm
,
2729 for (i
= 0; i
< ntparms
; ++i
)
2730 if (tpd
.parms
[i
] == 0)
2732 /* One of the template parms was not used in the
2734 if (!did_error_intro
)
2736 error ("template parameters not used in partial specialization:");
2737 did_error_intro
= 1;
2740 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
2743 /* [temp.class.spec]
2745 The argument list of the specialization shall not be identical to
2746 the implicit argument list of the primary template. */
2747 if (comp_template_args
2749 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2751 error ("partial specialization %qT does not specialize any template arguments", type
);
2753 /* [temp.class.spec]
2755 A partially specialized non-type argument expression shall not
2756 involve template parameters of the partial specialization except
2757 when the argument expression is a simple identifier.
2759 The type of a template parameter corresponding to a specialized
2760 non-type argument shall not be dependent on a parameter of the
2762 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
2764 for (i
= 0; i
< nargs
; ++i
)
2766 tree arg
= TREE_VEC_ELT (inner_args
, i
);
2767 if (/* These first two lines are the `non-type' bit. */
2769 && TREE_CODE (arg
) != TEMPLATE_DECL
2770 /* This next line is the `argument expression is not just a
2771 simple identifier' condition and also the `specialized
2772 non-type argument' bit. */
2773 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
2775 if (tpd
.arg_uses_template_parms
[i
])
2776 error ("template argument %qE involves template parameter(s)", arg
);
2779 /* Look at the corresponding template parameter,
2780 marking which template parameters its type depends
2783 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms
,
2788 /* We haven't yet initialized TPD2. Do so now. */
2789 tpd2
.arg_uses_template_parms
2790 = (int *) alloca (sizeof (int) * nargs
);
2791 /* The number of parameters here is the number in the
2792 main template, which, as checked in the assertion
2794 tpd2
.parms
= (int *) alloca (sizeof (int) * nargs
);
2796 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
2799 /* Mark the template parameters. But this time, we're
2800 looking for the template parameters of the main
2801 template, not in the specialization. */
2802 tpd2
.current_arg
= i
;
2803 tpd2
.arg_uses_template_parms
[i
] = 0;
2804 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
2805 for_each_template_parm (type
,
2806 &mark_template_parm
,
2810 if (tpd2
.arg_uses_template_parms
[i
])
2812 /* The type depended on some template parameters.
2813 If they are fully specialized in the
2814 specialization, that's OK. */
2816 for (j
= 0; j
< nargs
; ++j
)
2817 if (tpd2
.parms
[j
] != 0
2818 && tpd
.arg_uses_template_parms
[j
])
2820 error ("type %qT of template argument %qE depends "
2821 "on template parameter(s)",
2831 if (retrieve_specialization (maintmpl
, specargs
,
2832 /*class_specializations_p=*/true))
2833 /* We've already got this specialization. */
2836 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
2837 = tree_cons (specargs
, inner_parms
,
2838 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
2839 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
2843 /* Check that a template declaration's use of default arguments is not
2844 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2845 nonzero if DECL is the thing declared by a primary template.
2846 IS_PARTIAL is nonzero if DECL is a partial specialization. */
2849 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
, int is_partial
)
2852 int last_level_to_check
;
2857 A default template-argument shall not be specified in a
2858 function template declaration or a function template definition, nor
2859 in the template-parameter-list of the definition of a member of a
2862 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
2863 /* You can't have a function template declaration in a local
2864 scope, nor you can you define a member of a class template in a
2868 if (current_class_type
2869 && !TYPE_BEING_DEFINED (current_class_type
)
2870 && DECL_LANG_SPECIFIC (decl
)
2871 /* If this is either a friend defined in the scope of the class
2872 or a member function. */
2873 && (DECL_FUNCTION_MEMBER_P (decl
)
2874 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
2875 : DECL_FRIEND_CONTEXT (decl
)
2876 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
2878 /* And, if it was a member function, it really was defined in
2879 the scope of the class. */
2880 && (!DECL_FUNCTION_MEMBER_P (decl
)
2881 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
2882 /* We already checked these parameters when the template was
2883 declared, so there's no need to do it again now. This function
2884 was defined in class scope, but we're processing it's body now
2885 that the class is complete. */
2890 If a template-parameter has a default template-argument, all
2891 subsequent template-parameters shall have a default
2892 template-argument supplied. */
2893 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
2895 tree inner_parms
= TREE_VALUE (parm_level
);
2896 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
2897 int seen_def_arg_p
= 0;
2900 for (i
= 0; i
< ntparms
; ++i
)
2902 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
2904 if (parm
== error_mark_node
)
2907 if (TREE_PURPOSE (parm
))
2909 else if (seen_def_arg_p
)
2911 error ("no default argument for %qD", TREE_VALUE (parm
));
2912 /* For better subsequent error-recovery, we indicate that
2913 there should have been a default argument. */
2914 TREE_PURPOSE (parm
) = error_mark_node
;
2919 if (TREE_CODE (decl
) != TYPE_DECL
|| is_partial
|| !is_primary
)
2920 /* For an ordinary class template, default template arguments are
2921 allowed at the innermost level, e.g.:
2922 template <class T = int>
2924 but, in a partial specialization, they're not allowed even
2925 there, as we have in [temp.class.spec]:
2927 The template parameter list of a specialization shall not
2928 contain default template argument values.
2930 So, for a partial specialization, or for a function template,
2931 we look at all of them. */
2934 /* But, for a primary class template that is not a partial
2935 specialization we look at all template parameters except the
2937 parms
= TREE_CHAIN (parms
);
2939 /* Figure out what error message to issue. */
2940 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2941 msg
= "default template arguments may not be used in function templates";
2942 else if (is_partial
)
2943 msg
= "default template arguments may not be used in partial specializations";
2945 msg
= "default argument for template parameter for class enclosing %qD";
2947 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
2948 /* If we're inside a class definition, there's no need to
2949 examine the parameters to the class itself. On the one
2950 hand, they will be checked when the class is defined, and,
2951 on the other, default arguments are valid in things like:
2952 template <class T = double>
2953 struct S { template <class U> void f(U); };
2954 Here the default argument for `S' has no bearing on the
2955 declaration of `f'. */
2956 last_level_to_check
= template_class_depth (current_class_type
) + 1;
2958 /* Check everything. */
2959 last_level_to_check
= 0;
2961 for (parm_level
= parms
;
2962 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
2963 parm_level
= TREE_CHAIN (parm_level
))
2965 tree inner_parms
= TREE_VALUE (parm_level
);
2969 ntparms
= TREE_VEC_LENGTH (inner_parms
);
2970 for (i
= 0; i
< ntparms
; ++i
)
2972 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
2975 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
2983 /* Clear out the default argument so that we are not
2985 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
2989 /* At this point, if we're still interested in issuing messages,
2990 they must apply to classes surrounding the object declared. */
2992 msg
= "default argument for template parameter for class enclosing %qD";
2996 /* Worker for push_template_decl_real, called via
2997 for_each_template_parm. DATA is really an int, indicating the
2998 level of the parameters we are interested in. If T is a template
2999 parameter of that level, return nonzero. */
3002 template_parm_this_level_p (tree t
, void* data
)
3004 int this_level
= *(int *)data
;
3007 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3008 level
= TEMPLATE_PARM_LEVEL (t
);
3010 level
= TEMPLATE_TYPE_LEVEL (t
);
3011 return level
== this_level
;
3014 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
3015 parameters given by current_template_args, or reuses a
3016 previously existing one, if appropriate. Returns the DECL, or an
3017 equivalent one, if it is replaced via a call to duplicate_decls.
3019 If IS_FRIEND is true, DECL is a friend declaration. */
3022 push_template_decl_real (tree decl
, bool is_friend
)
3030 int new_template_p
= 0;
3031 /* True if the template is a member template, in the sense of
3033 bool member_template_p
= false;
3035 if (decl
== error_mark_node
)
3038 /* See if this is a partial specialization. */
3039 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
3040 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
3041 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
3043 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
3047 /* For a friend, we want the context of the friend function, not
3048 the type of which it is a friend. */
3049 ctx
= DECL_CONTEXT (decl
);
3050 else if (CP_DECL_CONTEXT (decl
)
3051 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
3052 /* In the case of a virtual function, we want the class in which
3054 ctx
= CP_DECL_CONTEXT (decl
);
3056 /* Otherwise, if we're currently defining some class, the DECL
3057 is assumed to be a member of the class. */
3058 ctx
= current_scope ();
3060 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
3063 if (!DECL_CONTEXT (decl
))
3064 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
3066 /* See if this is a primary template. */
3067 if (is_friend
&& ctx
)
3068 /* A friend template that specifies a class context, i.e.
3069 template <typename T> friend void A<T>::f();
3073 primary
= template_parm_scope_p ();
3077 if (DECL_CLASS_SCOPE_P (decl
))
3078 member_template_p
= true;
3079 if (TREE_CODE (decl
) == TYPE_DECL
3080 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
3081 error ("template class without a name");
3082 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
3084 if (DECL_DESTRUCTOR_P (decl
))
3088 A destructor shall not be a member template. */
3089 error ("destructor %qD declared as member template", decl
);
3090 return error_mark_node
;
3092 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
3093 && (!TYPE_ARG_TYPES (TREE_TYPE (decl
))
3094 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
3095 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
3096 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
3097 == void_list_node
)))
3099 /* [basic.stc.dynamic.allocation]
3101 An allocation function can be a function
3102 template. ... Template allocation functions shall
3103 have two or more parameters. */
3104 error ("invalid template declaration of %qD", decl
);
3105 return error_mark_node
;
3108 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
3109 && CLASS_TYPE_P (TREE_TYPE (decl
)))
3113 error ("template declaration of %q#D", decl
);
3114 return error_mark_node
;
3118 /* Check to see that the rules regarding the use of default
3119 arguments are not being violated. */
3120 check_default_tmpl_args (decl
, current_template_parms
,
3121 primary
, is_partial
);
3124 return process_partial_specialization (decl
);
3126 args
= current_template_args ();
3129 || TREE_CODE (ctx
) == FUNCTION_DECL
3130 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
3131 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
3133 if (DECL_LANG_SPECIFIC (decl
)
3134 && DECL_TEMPLATE_INFO (decl
)
3135 && DECL_TI_TEMPLATE (decl
))
3136 tmpl
= DECL_TI_TEMPLATE (decl
);
3137 /* If DECL is a TYPE_DECL for a class-template, then there won't
3138 be DECL_LANG_SPECIFIC. The information equivalent to
3139 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
3140 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
3141 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
3142 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
3144 /* Since a template declaration already existed for this
3145 class-type, we must be redeclaring it here. Make sure
3146 that the redeclaration is valid. */
3147 redeclare_class_template (TREE_TYPE (decl
),
3148 current_template_parms
);
3149 /* We don't need to create a new TEMPLATE_DECL; just use the
3150 one we already had. */
3151 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
3155 tmpl
= build_template_decl (decl
, current_template_parms
,
3159 if (DECL_LANG_SPECIFIC (decl
)
3160 && DECL_TEMPLATE_SPECIALIZATION (decl
))
3162 /* A specialization of a member template of a template
3164 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
3165 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
3166 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
3172 tree a
, t
, current
, parms
;
3175 if (TREE_CODE (decl
) == TYPE_DECL
)
3177 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl
)))
3178 || TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
)
3179 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
3180 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
3181 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
3184 error ("%qD does not declare a template type", decl
);
3188 else if (!DECL_LANG_SPECIFIC (decl
) || !DECL_TEMPLATE_INFO (decl
))
3190 error ("template definition of non-template %q#D", decl
);
3194 tmpl
= DECL_TI_TEMPLATE (decl
);
3196 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
3197 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
3198 && DECL_TEMPLATE_SPECIALIZATION (decl
)
3199 && DECL_MEMBER_TEMPLATE_P (tmpl
))
3203 /* The declaration is a specialization of a member
3204 template, declared outside the class. Therefore, the
3205 innermost template arguments will be NULL, so we
3206 replace them with the arguments determined by the
3207 earlier call to check_explicit_specialization. */
3208 args
= DECL_TI_ARGS (decl
);
3211 = build_template_decl (decl
, current_template_parms
,
3213 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
3214 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
3215 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
3216 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
3217 DECL_TEMPLATE_INFO (new_tmpl
)
3218 = tree_cons (tmpl
, args
, NULL_TREE
);
3220 register_specialization (new_tmpl
,
3221 most_general_template (tmpl
),
3227 /* Make sure the template headers we got make sense. */
3229 parms
= DECL_TEMPLATE_PARMS (tmpl
);
3230 i
= TMPL_PARMS_DEPTH (parms
);
3231 if (TMPL_ARGS_DEPTH (args
) != i
)
3233 error ("expected %d levels of template parms for %q#D, got %d",
3234 i
, decl
, TMPL_ARGS_DEPTH (args
));
3237 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
3239 a
= TMPL_ARGS_LEVEL (args
, i
);
3240 t
= INNERMOST_TEMPLATE_PARMS (parms
);
3242 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
3244 if (current
== decl
)
3245 error ("got %d template parameters for %q#D",
3246 TREE_VEC_LENGTH (a
), decl
);
3248 error ("got %d template parameters for %q#T",
3249 TREE_VEC_LENGTH (a
), current
);
3250 error (" but %d required", TREE_VEC_LENGTH (t
));
3251 return error_mark_node
;
3254 /* Perhaps we should also check that the parms are used in the
3255 appropriate qualifying scopes in the declarator? */
3257 if (current
== decl
)
3260 current
= TYPE_CONTEXT (current
);
3264 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
3265 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
3267 /* Push template declarations for global functions and types. Note
3268 that we do not try to push a global template friend declared in a
3269 template class; such a thing may well depend on the template
3270 parameters of the class. */
3271 if (new_template_p
&& !ctx
3272 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
3274 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
3275 if (tmpl
== error_mark_node
)
3276 return error_mark_node
;
3278 /* Hide template friend classes that haven't been declared yet. */
3279 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
3281 DECL_ANTICIPATED (tmpl
) = 1;
3282 DECL_FRIEND_P (tmpl
) = 1;
3288 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
3289 if (DECL_CONV_FN_P (tmpl
))
3291 int depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
3293 /* It is a conversion operator. See if the type converted to
3294 depends on innermost template operands. */
3296 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
3298 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
3302 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
3303 back to its most general template. If TMPL is a specialization,
3304 ARGS may only have the innermost set of arguments. Add the missing
3305 argument levels if necessary. */
3306 if (DECL_TEMPLATE_INFO (tmpl
))
3307 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
3309 info
= tree_cons (tmpl
, args
, NULL_TREE
);
3311 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
3313 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
3314 if ((!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
3315 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
3316 /* Don't change the name if we've already set it up. */
3317 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl
)))
3318 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
3320 else if (DECL_LANG_SPECIFIC (decl
))
3321 DECL_TEMPLATE_INFO (decl
) = info
;
3323 return DECL_TEMPLATE_RESULT (tmpl
);
3327 push_template_decl (tree decl
)
3329 return push_template_decl_real (decl
, false);
3332 /* Called when a class template TYPE is redeclared with the indicated
3333 template PARMS, e.g.:
3335 template <class T> struct S;
3336 template <class T> struct S {}; */
3339 redeclare_class_template (tree type
, tree parms
)
3345 if (!TYPE_TEMPLATE_INFO (type
))
3347 error ("%qT is not a template type", type
);
3351 tmpl
= TYPE_TI_TEMPLATE (type
);
3352 if (!PRIMARY_TEMPLATE_P (tmpl
))
3353 /* The type is nested in some template class. Nothing to worry
3354 about here; there are no new template parameters for the nested
3360 error ("template specifiers not specified in declaration of %qD",
3365 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
3366 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
3368 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
3370 error ("previous declaration %q+D", tmpl
);
3371 error ("used %d template parameter(s) instead of %d",
3372 TREE_VEC_LENGTH (tmpl_parms
),
3373 TREE_VEC_LENGTH (parms
));
3377 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
3384 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
3385 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
3388 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
3389 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
3390 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
3391 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
3393 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3395 if (tmpl_parm
!= error_mark_node
3396 && (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
3397 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
3398 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))))
3400 error ("template parameter %q+#D", tmpl_parm
);
3401 error ("redeclared here as %q#D", parm
);
3405 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
3407 /* We have in [temp.param]:
3409 A template-parameter may not be given default arguments
3410 by two different declarations in the same scope. */
3411 error ("redefinition of default argument for %q#D", parm
);
3412 error ("%J original definition appeared here", tmpl_parm
);
3416 if (parm_default
!= NULL_TREE
)
3417 /* Update the previous template parameters (which are the ones
3418 that will really count) with the new default value. */
3419 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
3420 else if (tmpl_default
!= NULL_TREE
)
3421 /* Update the new parameters, too; they'll be used as the
3422 parameters for any members. */
3423 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
3429 /* Simplify EXPR if it is a non-dependent expression. Returns the
3430 (possibly simplified) expression. */
3433 fold_non_dependent_expr (tree expr
)
3435 if (expr
== NULL_TREE
)
3438 /* If we're in a template, but EXPR isn't value dependent, simplify
3439 it. We're supposed to treat:
3441 template <typename T> void f(T[1 + 1]);
3442 template <typename T> void f(T[2]);
3444 as two declarations of the same function, for example. */
3445 if (processing_template_decl
3446 && !type_dependent_expression_p (expr
)
3447 && !value_dependent_expression_p (expr
))
3449 HOST_WIDE_INT saved_processing_template_decl
;
3451 saved_processing_template_decl
= processing_template_decl
;
3452 processing_template_decl
= 0;
3453 expr
= tsubst_copy_and_build (expr
,
3456 /*in_decl=*/NULL_TREE
,
3457 /*function_p=*/false,
3458 /*integral_constant_expression_p=*/true);
3459 processing_template_decl
= saved_processing_template_decl
;
3464 /* EXPR is an expression which is used in a constant-expression context.
3465 For instance, it could be a VAR_DECL with a constant initializer.
3466 Extract the innest constant expression.
3468 This is basically a more powerful version of
3469 integral_constant_value, which can be used also in templates where
3470 initializers can maintain a syntactic rather than semantic form
3471 (even if they are non-dependent, for access-checking purposes). */
3474 fold_decl_constant_value (tree expr
)
3476 tree const_expr
= expr
;
3479 expr
= fold_non_dependent_expr (const_expr
);
3480 const_expr
= integral_constant_value (expr
);
3482 while (expr
!= const_expr
);
3487 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3488 must be a function or a pointer-to-function type, as specified
3489 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3490 and check that the resulting function has external linkage. */
3493 convert_nontype_argument_function (tree type
, tree expr
)
3498 fn
= instantiate_type (type
, fns
, tf_none
);
3499 if (fn
== error_mark_node
)
3500 return error_mark_node
;
3503 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
3504 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
3505 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
3506 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
3508 /* [temp.arg.nontype]/1
3510 A template-argument for a non-type, non-template template-parameter
3513 -- the address of an object or function with external linkage. */
3514 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
3516 error ("%qE is not a valid template argument for type %qT "
3517 "because function %qD has not external linkage",
3518 expr
, type
, fn_no_ptr
);
3525 /* Attempt to convert the non-type template parameter EXPR to the
3526 indicated TYPE. If the conversion is successful, return the
3527 converted value. If the conversion is unsuccessful, return
3528 NULL_TREE if we issued an error message, or error_mark_node if we
3529 did not. We issue error messages for out-and-out bad template
3530 parameters, but not simply because the conversion failed, since we
3531 might be just trying to do argument deduction. Both TYPE and EXPR
3532 must be non-dependent.
3534 The conversion follows the special rules described in
3535 [temp.arg.nontype], and it is much more strict than an implicit
3538 This function is called twice for each template argument (see
3539 lookup_template_class for a more accurate description of this
3540 problem). This means that we need to handle expressions which
3541 are not valid in a C++ source, but can be created from the
3542 first call (for instance, casts to perform conversions). These
3543 hacks can go away after we fix the double coercion problem. */
3546 convert_nontype_argument (tree type
, tree expr
)
3550 /* Detect immediately string literals as invalid non-type argument.
3551 This special-case is not needed for correctness (we would easily
3552 catch this later), but only to provide better diagnostic for this
3553 common user mistake. As suggested by DR 100, we do not mention
3554 linkage issues in the diagnostic as this is not the point. */
3555 if (TREE_CODE (expr
) == STRING_CST
)
3557 error ("%qE is not a valid template argument for type %qT "
3558 "because string literals can never be used in this context",
3563 /* If we are in a template, EXPR may be non-dependent, but still
3564 have a syntactic, rather than semantic, form. For example, EXPR
3565 might be a SCOPE_REF, rather than the VAR_DECL to which the
3566 SCOPE_REF refers. Preserving the qualifying scope is necessary
3567 so that access checking can be performed when the template is
3568 instantiated -- but here we need the resolved form so that we can
3569 convert the argument. */
3570 expr
= fold_non_dependent_expr (expr
);
3571 if (error_operand_p (expr
))
3572 return error_mark_node
;
3573 expr_type
= TREE_TYPE (expr
);
3575 /* HACK: Due to double coercion, we can get a
3576 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3577 which is the tree that we built on the first call (see
3578 below when coercing to reference to object or to reference to
3579 function). We just strip everything and get to the arg.
3580 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3582 if (TREE_CODE (expr
) == NOP_EXPR
)
3584 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
3586 /* ??? Maybe we could use convert_from_reference here, but we
3587 would need to relax its constraints because the NOP_EXPR
3588 could actually change the type to something more cv-qualified,
3589 and this is not folded by convert_from_reference. */
3590 tree addr
= TREE_OPERAND (expr
, 0);
3591 gcc_assert (TREE_CODE (expr_type
) == REFERENCE_TYPE
);
3592 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
3593 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
3594 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3595 (TREE_TYPE (expr_type
),
3596 TREE_TYPE (TREE_TYPE (addr
))));
3598 expr
= TREE_OPERAND (addr
, 0);
3599 expr_type
= TREE_TYPE (expr
);
3602 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3603 parameter is a pointer to object, through decay and
3604 qualification conversion. Let's strip everything. */
3605 else if (TYPE_PTROBV_P (type
))
3608 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
3609 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
3610 /* Skip the ADDR_EXPR only if it is part of the decay for
3611 an array. Otherwise, it is part of the original argument
3612 in the source code. */
3613 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
3614 expr
= TREE_OPERAND (expr
, 0);
3615 expr_type
= TREE_TYPE (expr
);
3619 /* [temp.arg.nontype]/5, bullet 1
3621 For a non-type template-parameter of integral or enumeration type,
3622 integral promotions (_conv.prom_) and integral conversions
3623 (_conv.integral_) are applied. */
3624 if (INTEGRAL_TYPE_P (type
))
3626 if (!INTEGRAL_TYPE_P (expr_type
))
3627 return error_mark_node
;
3629 expr
= fold_decl_constant_value (expr
);
3630 /* Notice that there are constant expressions like '4 % 0' which
3631 do not fold into integer constants. */
3632 if (TREE_CODE (expr
) != INTEGER_CST
)
3634 error ("%qE is not a valid template argument for type %qT "
3635 "because it is a non-constant expression", expr
, type
);
3639 /* At this point, an implicit conversion does what we want,
3640 because we already know that the expression is of integral
3642 expr
= ocp_convert (type
, expr
, CONV_IMPLICIT
, LOOKUP_PROTECT
);
3643 if (expr
== error_mark_node
)
3644 return error_mark_node
;
3646 /* Conversion was allowed: fold it to a bare integer constant. */
3649 /* [temp.arg.nontype]/5, bullet 2
3651 For a non-type template-parameter of type pointer to object,
3652 qualification conversions (_conv.qual_) and the array-to-pointer
3653 conversion (_conv.array_) are applied. */
3654 else if (TYPE_PTROBV_P (type
))
3656 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
3658 A template-argument for a non-type, non-template template-parameter
3659 shall be one of: [...]
3661 -- the name of a non-type template-parameter;
3662 -- the address of an object or function with external linkage, [...]
3663 expressed as "& id-expression" where the & is optional if the name
3664 refers to a function or array, or if the corresponding
3665 template-parameter is a reference.
3667 Here, we do not care about functions, as they are invalid anyway
3668 for a parameter of type pointer-to-object. */
3670 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
3671 /* Non-type template parameters are OK. */
3673 else if (TREE_CODE (expr
) != ADDR_EXPR
3674 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
3676 if (TREE_CODE (expr
) == VAR_DECL
)
3678 error ("%qD is not a valid template argument "
3679 "because %qD is a variable, not the address of "
3684 /* Other values, like integer constants, might be valid
3685 non-type arguments of some other type. */
3686 return error_mark_node
;
3692 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
3693 ? TREE_OPERAND (expr
, 0) : expr
);
3694 if (TREE_CODE (decl
) != VAR_DECL
)
3696 error ("%qE is not a valid template argument of type %qT "
3697 "because %qE is not a variable",
3701 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
3703 error ("%qE is not a valid template argument of type %qT "
3704 "because %qD does not have external linkage",
3710 expr
= decay_conversion (expr
);
3711 if (expr
== error_mark_node
)
3712 return error_mark_node
;
3714 expr
= perform_qualification_conversions (type
, expr
);
3715 if (expr
== error_mark_node
)
3716 return error_mark_node
;
3718 /* [temp.arg.nontype]/5, bullet 3
3720 For a non-type template-parameter of type reference to object, no
3721 conversions apply. The type referred to by the reference may be more
3722 cv-qualified than the (otherwise identical) type of the
3723 template-argument. The template-parameter is bound directly to the
3724 template-argument, which must be an lvalue. */
3725 else if (TYPE_REF_OBJ_P (type
))
3727 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
3729 return error_mark_node
;
3731 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
3733 error ("%qE is not a valid template argument for type %qT "
3734 "because of conflicts in cv-qualification", expr
, type
);
3738 if (!real_lvalue_p (expr
))
3740 error ("%qE is not a valid template argument for type %qT "
3741 "because it is not an lvalue", expr
, type
);
3745 /* [temp.arg.nontype]/1
3747 A template-argument for a non-type, non-template template-parameter
3748 shall be one of: [...]
3750 -- the address of an object or function with external linkage. */
3751 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
3753 error ("%qE is not a valid template argument for type %qT "
3754 "because object %qD has not external linkage",
3759 expr
= build_nop (type
, build_address (expr
));
3761 /* [temp.arg.nontype]/5, bullet 4
3763 For a non-type template-parameter of type pointer to function, only
3764 the function-to-pointer conversion (_conv.func_) is applied. If the
3765 template-argument represents a set of overloaded functions (or a
3766 pointer to such), the matching function is selected from the set
3768 else if (TYPE_PTRFN_P (type
))
3770 /* If the argument is a template-id, we might not have enough
3771 context information to decay the pointer. */
3772 if (!type_unknown_p (expr_type
))
3774 expr
= decay_conversion (expr
);
3775 if (expr
== error_mark_node
)
3776 return error_mark_node
;
3779 expr
= convert_nontype_argument_function (type
, expr
);
3780 if (!expr
|| expr
== error_mark_node
)
3783 /* [temp.arg.nontype]/5, bullet 5
3785 For a non-type template-parameter of type reference to function, no
3786 conversions apply. If the template-argument represents a set of
3787 overloaded functions, the matching function is selected from the set
3789 else if (TYPE_REFFN_P (type
))
3791 if (TREE_CODE (expr
) == ADDR_EXPR
)
3793 error ("%qE is not a valid template argument for type %qT "
3794 "because it is a pointer", expr
, type
);
3795 inform ("try using %qE instead", TREE_OPERAND (expr
, 0));
3799 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
3800 if (!expr
|| expr
== error_mark_node
)
3803 expr
= build_nop (type
, build_address (expr
));
3805 /* [temp.arg.nontype]/5, bullet 6
3807 For a non-type template-parameter of type pointer to member function,
3808 no conversions apply. If the template-argument represents a set of
3809 overloaded member functions, the matching member function is selected
3810 from the set (_over.over_). */
3811 else if (TYPE_PTRMEMFUNC_P (type
))
3813 expr
= instantiate_type (type
, expr
, tf_none
);
3814 if (expr
== error_mark_node
)
3815 return error_mark_node
;
3817 /* There is no way to disable standard conversions in
3818 resolve_address_of_overloaded_function (called by
3819 instantiate_type). It is possible that the call succeeded by
3820 converting &B::I to &D::I (where B is a base of D), so we need
3821 to reject this conversion here.
3823 Actually, even if there was a way to disable standard conversions,
3824 it would still be better to reject them here so that we can
3825 provide a superior diagnostic. */
3826 if (!same_type_p (TREE_TYPE (expr
), type
))
3828 /* Make sure we are just one standard conversion off. */
3829 gcc_assert (can_convert (type
, TREE_TYPE (expr
)));
3830 error ("%qE is not a valid template argument for type %qT "
3831 "because it is of type %qT", expr
, type
,
3833 inform ("standard conversions are not allowed in this context");
3837 /* [temp.arg.nontype]/5, bullet 7
3839 For a non-type template-parameter of type pointer to data member,
3840 qualification conversions (_conv.qual_) are applied. */
3841 else if (TYPE_PTRMEM_P (type
))
3843 expr
= perform_qualification_conversions (type
, expr
);
3844 if (expr
== error_mark_node
)
3847 /* A template non-type parameter must be one of the above. */
3851 /* Sanity check: did we actually convert the argument to the
3853 gcc_assert (same_type_p (type
, TREE_TYPE (expr
)));
3858 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3859 template template parameters. Both PARM_PARMS and ARG_PARMS are
3860 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3863 Consider the example:
3864 template <class T> class A;
3865 template<template <class U> class TT> class B;
3867 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3868 the parameters to A, and OUTER_ARGS contains A. */
3871 coerce_template_template_parms (tree parm_parms
,
3873 tsubst_flags_t complain
,
3877 int nparms
, nargs
, i
;
3880 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
3881 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
3883 nparms
= TREE_VEC_LENGTH (parm_parms
);
3884 nargs
= TREE_VEC_LENGTH (arg_parms
);
3886 if (nargs
!= nparms
)
3889 for (i
= 0; i
< nparms
; ++i
)
3891 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
3892 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
3895 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
3896 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
3898 if (arg
== NULL_TREE
|| arg
== error_mark_node
3899 || parm
== NULL_TREE
|| parm
== error_mark_node
)
3902 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3905 switch (TREE_CODE (parm
))
3911 /* We encounter instantiations of templates like
3912 template <template <template <class> class> class TT>
3915 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
3916 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
3918 if (!coerce_template_template_parms
3919 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
3925 /* The tsubst call is used to handle cases such as
3927 template <int> class C {};
3928 template <class T, template <T> class TT> class D {};
3931 i.e. the parameter list of TT depends on earlier parameters. */
3932 if (!dependent_type_p (TREE_TYPE (arg
))
3934 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
3946 /* Convert the indicated template ARG as necessary to match the
3947 indicated template PARM. Returns the converted ARG, or
3948 error_mark_node if the conversion was unsuccessful. Error and
3949 warning messages are issued under control of COMPLAIN. This
3950 conversion is for the Ith parameter in the parameter list. ARGS is
3951 the full set of template arguments deduced so far. */
3954 convert_template_argument (tree parm
,
3957 tsubst_flags_t complain
,
3962 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
3964 if (TREE_CODE (arg
) == TREE_LIST
3965 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
3967 /* The template argument was the name of some
3968 member function. That's usually
3969 invalid, but static members are OK. In any
3970 case, grab the underlying fields/functions
3971 and issue an error later if required. */
3972 arg
= TREE_VALUE (arg
);
3973 TREE_TYPE (arg
) = unknown_type_node
;
3976 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
3977 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
3978 || requires_tmpl_type
);
3980 is_tmpl_type
= ((TREE_CODE (arg
) == TEMPLATE_DECL
3981 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
3982 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3983 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
3986 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
3987 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
3988 arg
= TYPE_STUB_DECL (arg
);
3990 is_type
= TYPE_P (arg
) || is_tmpl_type
;
3992 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
3993 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
3995 pedwarn ("to refer to a type member of a template parameter, "
3996 "use %<typename %E%>", arg
);
3998 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
3999 TREE_OPERAND (arg
, 1),
4001 complain
& tf_error
);
4004 if (is_type
!= requires_type
)
4008 if (complain
& tf_error
)
4010 error ("type/value mismatch at argument %d in template "
4011 "parameter list for %qD",
4014 error (" expected a constant of type %qT, got %qT",
4016 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
4017 else if (requires_tmpl_type
)
4018 error (" expected a class template, got %qE", arg
);
4020 error (" expected a type, got %qE", arg
);
4023 return error_mark_node
;
4025 if (is_tmpl_type
^ requires_tmpl_type
)
4027 if (in_decl
&& (complain
& tf_error
))
4029 error ("type/value mismatch at argument %d in template "
4030 "parameter list for %qD",
4033 error (" expected a type, got %qT", DECL_NAME (arg
));
4035 error (" expected a class template, got %qT", arg
);
4037 return error_mark_node
;
4042 if (requires_tmpl_type
)
4044 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
4045 /* The number of argument required is not known yet.
4046 Just accept it for now. */
4047 val
= TREE_TYPE (arg
);
4050 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
4051 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
4053 if (coerce_template_template_parms (parmparm
, argparm
,
4059 /* TEMPLATE_TEMPLATE_PARM node is preferred over
4061 if (val
!= error_mark_node
4062 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
4063 val
= TREE_TYPE (val
);
4067 if (in_decl
&& (complain
& tf_error
))
4069 error ("type/value mismatch at argument %d in "
4070 "template parameter list for %qD",
4072 error (" expected a template of type %qD, got %qD",
4076 val
= error_mark_node
;
4082 /* We only form one instance of each template specialization.
4083 Therefore, if we use a non-canonical variant (i.e., a
4084 typedef), any future messages referring to the type will use
4085 the typedef, which is confusing if those future uses do not
4086 themselves also use the typedef. */
4088 val
= canonical_type_variant (val
);
4092 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
4094 if (invalid_nontype_parm_type_p (t
, complain
))
4095 return error_mark_node
;
4097 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
4098 /* We used to call digest_init here. However, digest_init
4099 will report errors, which we don't want when complain
4100 is zero. More importantly, digest_init will try too
4101 hard to convert things: for example, `0' should not be
4102 converted to pointer type at this point according to
4103 the standard. Accepting this is not merely an
4104 extension, since deciding whether or not these
4105 conversions can occur is part of determining which
4106 function template to call, or whether a given explicit
4107 argument specification is valid. */
4108 val
= convert_nontype_argument (t
, arg
);
4112 if (val
== NULL_TREE
)
4113 val
= error_mark_node
;
4114 else if (val
== error_mark_node
&& (complain
& tf_error
))
4115 error ("could not convert template argument %qE to %qT", arg
, t
);
4121 /* Convert all template arguments to their appropriate types, and
4122 return a vector containing the innermost resulting template
4123 arguments. If any error occurs, return error_mark_node. Error and
4124 warning messages are issued under control of COMPLAIN.
4126 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
4127 for arguments not specified in ARGS. Otherwise, if
4128 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4129 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4130 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4134 coerce_template_parms (tree parms
,
4137 tsubst_flags_t complain
,
4138 bool require_all_args
,
4139 bool use_default_args
)
4141 int nparms
, nargs
, i
, lost
= 0;
4144 tree new_inner_args
;
4145 bool saved_skip_evaluation
;
4147 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
4148 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
4149 nparms
= TREE_VEC_LENGTH (parms
);
4154 && (!use_default_args
4155 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
4156 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
4158 if (complain
& tf_error
)
4160 error ("wrong number of template arguments (%d, should be %d)",
4164 error ("provided for %q+D", in_decl
);
4167 return error_mark_node
;
4170 /* We need to evaluate the template arguments, even though this
4171 template-id may be nested within a "sizeof". */
4172 saved_skip_evaluation
= skip_evaluation
;
4173 skip_evaluation
= false;
4174 new_inner_args
= make_tree_vec (nparms
);
4175 new_args
= add_outermost_template_args (args
, new_inner_args
);
4176 for (i
= 0; i
< nparms
; i
++)
4181 /* Get the Ith template parameter. */
4182 parm
= TREE_VEC_ELT (parms
, i
);
4184 if (parm
== error_mark_node
)
4186 TREE_VEC_ELT (new_inner_args
, i
) = error_mark_node
;
4190 /* Calculate the Ith argument. */
4192 arg
= TREE_VEC_ELT (inner_args
, i
);
4193 else if (require_all_args
)
4194 /* There must be a default arg in this case. */
4195 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
4201 if (arg
== error_mark_node
)
4203 if (complain
& tf_error
)
4204 error ("template argument %d is invalid", i
+ 1);
4207 arg
= convert_template_argument (TREE_VALUE (parm
),
4208 arg
, new_args
, complain
, i
,
4211 if (arg
== error_mark_node
)
4213 TREE_VEC_ELT (new_inner_args
, i
) = arg
;
4215 skip_evaluation
= saved_skip_evaluation
;
4218 return error_mark_node
;
4220 return new_inner_args
;
4223 /* Returns 1 if template args OT and NT are equivalent. */
4226 template_args_equal (tree ot
, tree nt
)
4231 if (TREE_CODE (nt
) == TREE_VEC
)
4232 /* For member templates */
4233 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
4234 else if (TYPE_P (nt
))
4235 return TYPE_P (ot
) && same_type_p (ot
, nt
);
4236 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
4239 return cp_tree_equal (ot
, nt
);
4242 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
4243 of template arguments. Returns 0 otherwise. */
4246 comp_template_args (tree oldargs
, tree newargs
)
4250 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
4253 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
4255 tree nt
= TREE_VEC_ELT (newargs
, i
);
4256 tree ot
= TREE_VEC_ELT (oldargs
, i
);
4258 if (! template_args_equal (ot
, nt
))
4264 /* Given class template name and parameter list, produce a user-friendly name
4265 for the instantiation. */
4268 mangle_class_name_for_template (const char* name
, tree parms
, tree arglist
)
4270 static struct obstack scratch_obstack
;
4271 static char *scratch_firstobj
;
4274 if (!scratch_firstobj
)
4275 gcc_obstack_init (&scratch_obstack
);
4277 obstack_free (&scratch_obstack
, scratch_firstobj
);
4278 scratch_firstobj
= (char *) obstack_alloc (&scratch_obstack
, 1);
4280 #define ccat(C) obstack_1grow (&scratch_obstack, (C));
4281 #define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
4285 nparms
= TREE_VEC_LENGTH (parms
);
4286 arglist
= INNERMOST_TEMPLATE_ARGS (arglist
);
4287 gcc_assert (nparms
== TREE_VEC_LENGTH (arglist
));
4288 for (i
= 0; i
< nparms
; i
++)
4293 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4294 arg
= TREE_VEC_ELT (arglist
, i
);
4296 if (parm
== error_mark_node
)
4302 if (TREE_CODE (parm
) == TYPE_DECL
)
4304 cat (type_as_string (arg
, TFF_CHASE_TYPEDEF
));
4307 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4309 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
4311 /* Already substituted with real template. Just output
4312 the template name here */
4313 tree context
= DECL_CONTEXT (arg
);
4316 /* The template may be defined in a namespace, or
4317 may be a member template. */
4318 gcc_assert (TREE_CODE (context
) == NAMESPACE_DECL
4319 || CLASS_TYPE_P (context
));
4320 cat (decl_as_string (DECL_CONTEXT (arg
),
4321 TFF_PLAIN_IDENTIFIER
));
4324 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
4327 /* Output the parameter declaration. */
4328 cat (type_as_string (arg
, TFF_CHASE_TYPEDEF
));
4332 gcc_assert (TREE_CODE (parm
) == PARM_DECL
);
4334 /* No need to check arglist against parmlist here; we did that
4335 in coerce_template_parms, called from lookup_template_class. */
4336 cat (expr_as_string (arg
, TFF_PLAIN_IDENTIFIER
));
4339 char *bufp
= obstack_next_free (&scratch_obstack
);
4341 while (bufp
[offset
- 1] == ' ')
4343 obstack_blank_fast (&scratch_obstack
, offset
);
4345 /* B<C<char> >, not B<C<char>> */
4346 if (bufp
[offset
- 1] == '>')
4351 return (char *) obstack_base (&scratch_obstack
);
4355 classtype_mangled_name (tree t
)
4357 if (CLASSTYPE_TEMPLATE_INFO (t
)
4358 /* Specializations have already had their names set up in
4359 lookup_template_class. */
4360 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
4362 tree tmpl
= most_general_template (CLASSTYPE_TI_TEMPLATE (t
));
4364 /* For non-primary templates, the template parameters are
4365 implicit from their surrounding context. */
4366 if (PRIMARY_TEMPLATE_P (tmpl
))
4368 tree name
= DECL_NAME (tmpl
);
4369 char *mangled_name
= mangle_class_name_for_template
4370 (IDENTIFIER_POINTER (name
),
4371 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
4372 CLASSTYPE_TI_ARGS (t
));
4373 tree id
= get_identifier (mangled_name
);
4374 IDENTIFIER_TEMPLATE (id
) = name
;
4379 return TYPE_IDENTIFIER (t
);
4383 add_pending_template (tree d
)
4385 tree ti
= (TYPE_P (d
)
4386 ? CLASSTYPE_TEMPLATE_INFO (d
)
4387 : DECL_TEMPLATE_INFO (d
));
4391 if (TI_PENDING_TEMPLATE_FLAG (ti
))
4394 /* We are called both from instantiate_decl, where we've already had a
4395 tinst_level pushed, and instantiate_template, where we haven't.
4397 level
= !(current_tinst_level
&& TINST_DECL (current_tinst_level
) == d
);
4400 push_tinst_level (d
);
4402 pt
= tree_cons (current_tinst_level
, d
, NULL_TREE
);
4403 if (last_pending_template
)
4404 TREE_CHAIN (last_pending_template
) = pt
;
4406 pending_templates
= pt
;
4408 last_pending_template
= pt
;
4410 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
4417 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4418 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4419 documentation for TEMPLATE_ID_EXPR. */
4422 lookup_template_function (tree fns
, tree arglist
)
4426 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
4427 return error_mark_node
;
4429 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
4430 gcc_assert (fns
&& (is_overloaded_fn (fns
)
4431 || TREE_CODE (fns
) == IDENTIFIER_NODE
));
4433 if (BASELINK_P (fns
))
4435 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
4437 BASELINK_FUNCTIONS (fns
),
4442 type
= TREE_TYPE (fns
);
4443 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
4444 type
= unknown_type_node
;
4446 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
4449 /* Within the scope of a template class S<T>, the name S gets bound
4450 (in build_self_reference) to a TYPE_DECL for the class, not a
4451 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4452 or one of its enclosing classes, and that type is a template,
4453 return the associated TEMPLATE_DECL. Otherwise, the original
4454 DECL is returned. */
4457 maybe_get_template_decl_from_type_decl (tree decl
)
4459 return (decl
!= NULL_TREE
4460 && TREE_CODE (decl
) == TYPE_DECL
4461 && DECL_ARTIFICIAL (decl
)
4462 && CLASS_TYPE_P (TREE_TYPE (decl
))
4463 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
4464 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
4467 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4468 parameters, find the desired type.
4470 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
4472 IN_DECL, if non-NULL, is the template declaration we are trying to
4475 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
4476 the class we are looking up.
4478 Issue error and warning messages under control of COMPLAIN.
4480 If the template class is really a local class in a template
4481 function, then the FUNCTION_CONTEXT is the function in which it is
4484 ??? Note that this function is currently called *twice* for each
4485 template-id: the first time from the parser, while creating the
4486 incomplete type (finish_template_type), and the second type during the
4487 real instantiation (instantiate_template_class). This is surely something
4488 that we want to avoid. It also causes some problems with argument
4489 coercion (see convert_nontype_argument for more information on this). */
4492 lookup_template_class (tree d1
,
4497 tsubst_flags_t complain
)
4499 tree
template = NULL_TREE
, parmlist
;
4502 timevar_push (TV_NAME_LOOKUP
);
4504 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
4506 tree value
= innermost_non_namespace_value (d1
);
4507 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
4512 push_decl_namespace (context
);
4513 template = lookup_name (d1
);
4514 template = maybe_get_template_decl_from_type_decl (template);
4516 pop_decl_namespace ();
4519 context
= DECL_CONTEXT (template);
4521 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
4523 tree type
= TREE_TYPE (d1
);
4525 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4526 an implicit typename for the second A. Deal with it. */
4527 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
4528 type
= TREE_TYPE (type
);
4530 if (CLASSTYPE_TEMPLATE_INFO (type
))
4532 template = CLASSTYPE_TI_TEMPLATE (type
);
4533 d1
= DECL_NAME (template);
4536 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
4537 || (TYPE_P (d1
) && IS_AGGR_TYPE (d1
)))
4539 template = TYPE_TI_TEMPLATE (d1
);
4540 d1
= DECL_NAME (template);
4542 else if (TREE_CODE (d1
) == TEMPLATE_DECL
4543 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
4546 d1
= DECL_NAME (template);
4547 context
= DECL_CONTEXT (template);
4550 /* Issue an error message if we didn't find a template. */
4553 if (complain
& tf_error
)
4554 error ("%qT is not a template", d1
);
4555 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4558 if (TREE_CODE (template) != TEMPLATE_DECL
4559 /* Make sure it's a user visible template, if it was named by
4561 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (template)
4562 && !PRIMARY_TEMPLATE_P (template)))
4564 if (complain
& tf_error
)
4566 error ("non-template type %qT used as a template", d1
);
4568 error ("for template declaration %q+D", in_decl
);
4570 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4573 complain
&= ~tf_user
;
4575 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4577 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
4578 template arguments */
4583 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
4585 /* Consider an example where a template template parameter declared as
4587 template <class T, class U = std::allocator<T> > class TT
4589 The template parameter level of T and U are one level larger than
4590 of TT. To proper process the default argument of U, say when an
4591 instantiation `TT<int>' is seen, we need to build the full
4592 arguments containing {int} as the innermost level. Outer levels,
4593 available when not appearing as default template argument, can be
4594 obtained from `current_template_args ()'.
4596 Suppose that TT is later substituted with std::vector. The above
4597 instantiation is `TT<int, std::allocator<T> >' with TT at
4598 level 1, and T at level 2, while the template arguments at level 1
4599 becomes {std::vector} and the inner level 2 is {int}. */
4601 if (current_template_parms
)
4602 arglist
= add_to_template_args (current_template_args (), arglist
);
4604 arglist2
= coerce_template_parms (parmlist
, arglist
, template,
4606 /*require_all_args=*/true,
4607 /*use_default_args=*/true);
4608 if (arglist2
== error_mark_node
4609 || (!uses_template_parms (arglist2
)
4610 && check_instantiated_args (template, arglist2
, complain
)))
4611 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4613 parm
= bind_template_template_parm (TREE_TYPE (template), arglist2
);
4614 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, parm
);
4618 tree template_type
= TREE_TYPE (template);
4621 tree found
= NULL_TREE
;
4624 int is_partial_instantiation
;
4626 gen_tmpl
= most_general_template (template);
4627 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
4628 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
4629 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
4631 if (arg_depth
== 1 && parm_depth
> 1)
4633 /* We've been given an incomplete set of template arguments.
4636 template <class T> struct S1 {
4637 template <class U> struct S2 {};
4638 template <class U> struct S2<U*> {};
4641 we will be called with an ARGLIST of `U*', but the
4642 TEMPLATE will be `template <class T> template
4643 <class U> struct S1<T>::S2'. We must fill in the missing
4646 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4648 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
4651 /* Now we should have enough arguments. */
4652 gcc_assert (parm_depth
== arg_depth
);
4654 /* From here on, we're only interested in the most general
4656 template = gen_tmpl
;
4658 /* Calculate the BOUND_ARGS. These will be the args that are
4659 actually tsubst'd into the definition to create the
4663 /* We have multiple levels of arguments to coerce, at once. */
4665 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
4667 tree bound_args
= make_tree_vec (parm_depth
);
4669 for (i
= saved_depth
,
4670 t
= DECL_TEMPLATE_PARMS (template);
4671 i
> 0 && t
!= NULL_TREE
;
4672 --i
, t
= TREE_CHAIN (t
))
4674 tree a
= coerce_template_parms (TREE_VALUE (t
),
4677 /*require_all_args=*/true,
4678 /*use_default_args=*/true);
4680 /* Don't process further if one of the levels fails. */
4681 if (a
== error_mark_node
)
4683 /* Restore the ARGLIST to its full size. */
4684 TREE_VEC_LENGTH (arglist
) = saved_depth
;
4685 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4688 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
4690 /* We temporarily reduce the length of the ARGLIST so
4691 that coerce_template_parms will see only the arguments
4692 corresponding to the template parameters it is
4694 TREE_VEC_LENGTH (arglist
)--;
4697 /* Restore the ARGLIST to its full size. */
4698 TREE_VEC_LENGTH (arglist
) = saved_depth
;
4700 arglist
= bound_args
;
4704 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
4705 INNERMOST_TEMPLATE_ARGS (arglist
),
4708 /*require_all_args=*/true,
4709 /*use_default_args=*/true);
4711 if (arglist
== error_mark_node
)
4712 /* We were unable to bind the arguments. */
4713 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4715 /* In the scope of a template class, explicit references to the
4716 template class refer to the type of the template, not any
4717 instantiation of it. For example, in:
4719 template <class T> class C { void f(C<T>); }
4721 the `C<T>' is just the same as `C'. Outside of the
4722 class, however, such a reference is an instantiation. */
4723 if (comp_template_args (TYPE_TI_ARGS (template_type
),
4726 found
= template_type
;
4728 if (!entering_scope
&& PRIMARY_TEMPLATE_P (template))
4732 for (ctx
= current_class_type
;
4733 ctx
&& TREE_CODE (ctx
) != NAMESPACE_DECL
;
4735 ? TYPE_CONTEXT (ctx
)
4736 : DECL_CONTEXT (ctx
)))
4737 if (TYPE_P (ctx
) && same_type_p (ctx
, template_type
))
4740 /* We're not in the scope of the class, so the
4741 TEMPLATE_TYPE is not the type we want after all. */
4747 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
4749 /* If we already have this specialization, return it. */
4750 found
= retrieve_specialization (template, arglist
,
4751 /*class_specializations_p=*/false);
4753 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
4755 /* This type is a "partial instantiation" if any of the template
4756 arguments still involve template parameters. Note that we set
4757 IS_PARTIAL_INSTANTIATION for partial specializations as
4759 is_partial_instantiation
= uses_template_parms (arglist
);
4761 /* If the deduced arguments are invalid, then the binding
4763 if (!is_partial_instantiation
4764 && check_instantiated_args (template,
4765 INNERMOST_TEMPLATE_ARGS (arglist
),
4767 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
4769 if (!is_partial_instantiation
4770 && !PRIMARY_TEMPLATE_P (template)
4771 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL
)
4773 found
= xref_tag_from_type (TREE_TYPE (template),
4774 DECL_NAME (template),
4775 /*tag_scope=*/ts_global
);
4776 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
4779 context
= tsubst (DECL_CONTEXT (template), arglist
,
4782 context
= global_namespace
;
4784 /* Create the type. */
4785 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
4787 if (!is_partial_instantiation
)
4789 set_current_access_from_decl (TYPE_NAME (template_type
));
4790 t
= start_enum (TYPE_IDENTIFIER (template_type
));
4793 /* We don't want to call start_enum for this type, since
4794 the values for the enumeration constants may involve
4795 template parameters. And, no one should be interested
4796 in the enumeration constants for such a type. */
4797 t
= make_node (ENUMERAL_TYPE
);
4801 t
= make_aggr_type (TREE_CODE (template_type
));
4802 CLASSTYPE_DECLARED_CLASS (t
)
4803 = CLASSTYPE_DECLARED_CLASS (template_type
);
4804 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
4805 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
4807 /* A local class. Make sure the decl gets registered properly. */
4808 if (context
== current_function_decl
)
4809 pushtag (DECL_NAME (template), t
, /*tag_scope=*/ts_current
);
4812 /* If we called start_enum or pushtag above, this information
4813 will already be set up. */
4816 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
4818 type_decl
= create_implicit_typedef (DECL_NAME (template), t
);
4819 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
4820 TYPE_STUB_DECL (t
) = type_decl
;
4821 DECL_SOURCE_LOCATION (type_decl
)
4822 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
4825 type_decl
= TYPE_NAME (t
);
4827 TREE_PRIVATE (type_decl
)
4828 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
4829 TREE_PROTECTED (type_decl
)
4830 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
4831 DECL_IN_SYSTEM_HEADER (type_decl
)
4832 = DECL_IN_SYSTEM_HEADER (template);
4833 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
4835 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
4836 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
4839 /* Set up the template information. We have to figure out which
4840 template is the immediate parent if this is a full
4842 if (parm_depth
== 1 || is_partial_instantiation
4843 || !PRIMARY_TEMPLATE_P (template))
4844 /* This case is easy; there are no member templates involved. */
4848 /* This is a full instantiation of a member template. Look
4849 for a partial instantiation of which this is an instance. */
4851 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
4852 found
; found
= TREE_CHAIN (found
))
4855 tree tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found
));
4857 /* We only want partial instantiations, here, not
4858 specializations or full instantiations. */
4859 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found
))
4860 || !uses_template_parms (TREE_VALUE (found
)))
4863 /* Temporarily reduce by one the number of levels in the
4864 ARGLIST and in FOUND so as to avoid comparing the
4865 last set of arguments. */
4866 TREE_VEC_LENGTH (arglist
)--;
4867 TREE_VEC_LENGTH (TREE_PURPOSE (found
)) --;
4869 /* See if the arguments match. If they do, then TMPL is
4870 the partial instantiation we want. */
4871 success
= comp_template_args (TREE_PURPOSE (found
), arglist
);
4873 /* Restore the argument vectors to their full size. */
4874 TREE_VEC_LENGTH (arglist
)++;
4875 TREE_VEC_LENGTH (TREE_PURPOSE (found
))++;
4886 /* There was no partial instantiation. This happens
4887 where C<T> is a member template of A<T> and it's used
4890 template <typename T> struct B { A<T>::C<int> m; };
4893 Create the partial instantiation.
4895 TREE_VEC_LENGTH (arglist
)--;
4896 found
= tsubst (template, arglist
, complain
, NULL_TREE
);
4897 TREE_VEC_LENGTH (arglist
)++;
4901 SET_TYPE_TEMPLATE_INFO (t
, tree_cons (found
, arglist
, NULL_TREE
));
4902 DECL_TEMPLATE_INSTANTIATIONS (template)
4903 = tree_cons (arglist
, t
,
4904 DECL_TEMPLATE_INSTANTIATIONS (template));
4906 if (TREE_CODE (t
) == ENUMERAL_TYPE
4907 && !is_partial_instantiation
)
4908 /* Now that the type has been registered on the instantiations
4909 list, we set up the enumerators. Because the enumeration
4910 constants may involve the enumeration type itself, we make
4911 sure to register the type first, and then create the
4912 constants. That way, doing tsubst_expr for the enumeration
4913 constants won't result in recursive calls here; we'll find
4914 the instantiation and exit above. */
4915 tsubst_enum (template_type
, t
, arglist
);
4917 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4919 if (TREE_CODE (t
) != ENUMERAL_TYPE
)
4920 DECL_NAME (type_decl
) = classtype_mangled_name (t
);
4921 if (is_partial_instantiation
)
4922 /* If the type makes use of template parameters, the
4923 code that generates debugging information will crash. */
4924 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
4926 /* Possibly limit visibility based on template args. */
4927 TREE_PUBLIC (type_decl
) = 1;
4928 determine_visibility (type_decl
);
4930 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
4932 timevar_pop (TV_NAME_LOOKUP
);
4939 struct pointer_set_t
*visited
;
4942 /* Called from for_each_template_parm via walk_tree. */
4945 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
4948 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
4949 tree_fn_t fn
= pfd
->fn
;
4950 void *data
= pfd
->data
;
4953 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
))
4954 return error_mark_node
;
4956 switch (TREE_CODE (t
))
4959 if (TYPE_PTRMEMFUNC_P (t
))
4965 if (!TYPE_TEMPLATE_INFO (t
))
4967 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t
)),
4968 fn
, data
, pfd
->visited
))
4969 return error_mark_node
;
4973 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
4974 fn
, data
, pfd
->visited
)
4975 || for_each_template_parm (TYPE_MAX_VALUE (t
),
4976 fn
, data
, pfd
->visited
))
4977 return error_mark_node
;
4981 /* Since we're not going to walk subtrees, we have to do this
4983 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
4985 return error_mark_node
;
4989 /* Check the return type. */
4990 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
))
4991 return error_mark_node
;
4993 /* Check the parameter types. Since default arguments are not
4994 instantiated until they are needed, the TYPE_ARG_TYPES may
4995 contain expressions that involve template parameters. But,
4996 no-one should be looking at them yet. And, once they're
4997 instantiated, they don't contain template parameters, so
4998 there's no point in looking at them then, either. */
5002 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
5003 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
5005 return error_mark_node
;
5007 /* Since we've already handled the TYPE_ARG_TYPES, we don't
5008 want walk_tree walking into them itself. */
5014 if (for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
5016 return error_mark_node
;
5021 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
5022 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
5024 return error_mark_node
;
5029 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
5030 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
5032 return error_mark_node
;
5033 if (DECL_CONTEXT (t
)
5034 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
5036 return error_mark_node
;
5039 case BOUND_TEMPLATE_TEMPLATE_PARM
:
5040 /* Record template parameters such as `T' inside `TT<T>'. */
5041 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
))
5042 return error_mark_node
;
5045 case TEMPLATE_TEMPLATE_PARM
:
5046 case TEMPLATE_TYPE_PARM
:
5047 case TEMPLATE_PARM_INDEX
:
5048 if (fn
&& (*fn
)(t
, data
))
5049 return error_mark_node
;
5051 return error_mark_node
;
5055 /* A template template parameter is encountered. */
5056 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
5057 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
))
5058 return error_mark_node
;
5060 /* Already substituted template template parameter */
5066 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
5067 data
, pfd
->visited
))
5068 return error_mark_node
;
5072 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
5073 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
5074 (TREE_TYPE (t
)), fn
, data
,
5076 return error_mark_node
;
5081 /* If there's no type, then this thing must be some expression
5082 involving template parameters. */
5083 if (!fn
&& !TREE_TYPE (t
))
5084 return error_mark_node
;
5089 case REINTERPRET_CAST_EXPR
:
5090 case CONST_CAST_EXPR
:
5091 case STATIC_CAST_EXPR
:
5092 case DYNAMIC_CAST_EXPR
:
5096 case PSEUDO_DTOR_EXPR
:
5098 return error_mark_node
;
5102 /* If we do not handle this case specially, we end up walking
5103 the BINFO hierarchy, which is circular, and therefore
5104 confuses walk_tree. */
5106 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp
), fn
, data
,
5108 return error_mark_node
;
5115 /* We didn't find any template parameters we liked. */
5119 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5120 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
5121 call FN with the parameter and the DATA.
5122 If FN returns nonzero, the iteration is terminated, and
5123 for_each_template_parm returns 1. Otherwise, the iteration
5124 continues. If FN never returns a nonzero value, the value
5125 returned by for_each_template_parm is 0. If FN is NULL, it is
5126 considered to be the function which always returns 1. */
5129 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
5130 struct pointer_set_t
*visited
)
5132 struct pair_fn_data pfd
;
5139 /* Walk the tree. (Conceptually, we would like to walk without
5140 duplicates, but for_each_template_parm_r recursively calls
5141 for_each_template_parm, so we would need to reorganize a fair
5142 bit to use walk_tree_without_duplicates, so we keep our own
5145 pfd
.visited
= visited
;
5147 pfd
.visited
= pointer_set_create ();
5148 result
= walk_tree (&t
,
5149 for_each_template_parm_r
,
5151 pfd
.visited
) != NULL_TREE
;
5156 pointer_set_destroy (pfd
.visited
);
5163 /* Returns true if T depends on any template parameter. */
5166 uses_template_parms (tree t
)
5169 int saved_processing_template_decl
;
5171 saved_processing_template_decl
= processing_template_decl
;
5172 if (!saved_processing_template_decl
)
5173 processing_template_decl
= 1;
5175 dependent_p
= dependent_type_p (t
);
5176 else if (TREE_CODE (t
) == TREE_VEC
)
5177 dependent_p
= any_dependent_template_arguments_p (t
);
5178 else if (TREE_CODE (t
) == TREE_LIST
)
5179 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
5180 || uses_template_parms (TREE_CHAIN (t
)));
5181 else if (TREE_CODE (t
) == TYPE_DECL
)
5182 dependent_p
= dependent_type_p (TREE_TYPE (t
));
5185 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
5186 || TREE_CODE (t
) == OVERLOAD
5187 || TREE_CODE (t
) == BASELINK
5188 || TREE_CODE (t
) == IDENTIFIER_NODE
5189 || CONSTANT_CLASS_P (t
))
5190 dependent_p
= (type_dependent_expression_p (t
)
5191 || value_dependent_expression_p (t
));
5194 gcc_assert (t
== error_mark_node
);
5195 dependent_p
= false;
5198 processing_template_decl
= saved_processing_template_decl
;
5203 /* Returns true if T depends on any template parameter with level LEVEL. */
5206 uses_template_parms_level (tree t
, int level
)
5208 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
);
5211 static int tinst_depth
;
5212 extern int max_tinst_depth
;
5213 #ifdef GATHER_STATISTICS
5216 static int tinst_level_tick
;
5217 static int last_template_error_tick
;
5219 /* We're starting to instantiate D; record the template instantiation context
5220 for diagnostics and to restore it later. */
5223 push_tinst_level (tree d
)
5227 if (tinst_depth
>= max_tinst_depth
)
5229 /* If the instantiation in question still has unbound template parms,
5230 we don't really care if we can't instantiate it, so just return.
5231 This happens with base instantiation for implicit `typename'. */
5232 if (uses_template_parms (d
))
5235 last_template_error_tick
= tinst_level_tick
;
5236 error ("template instantiation depth exceeds maximum of %d (use "
5237 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
5238 max_tinst_depth
, d
);
5240 print_instantiation_context ();
5245 new = make_node (TINST_LEVEL
);
5246 TINST_DECL (new) = d
;
5247 TINST_LOCATION (new) = input_location
;
5248 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header
;
5249 TREE_CHAIN (new) = current_tinst_level
;
5250 current_tinst_level
= new;
5253 #ifdef GATHER_STATISTICS
5254 if (tinst_depth
> depth_reached
)
5255 depth_reached
= tinst_depth
;
5262 /* We're done instantiating this template; return to the instantiation
5266 pop_tinst_level (void)
5268 tree old
= current_tinst_level
;
5270 /* Restore the filename and line number stashed away when we started
5271 this instantiation. */
5272 input_location
= TINST_LOCATION (old
);
5273 in_system_header
= TINST_IN_SYSTEM_HEADER_P (old
);
5274 current_tinst_level
= TREE_CHAIN (old
);
5279 /* We're instantiating a deferred template; restore the template
5280 instantiation context in which the instantiation was requested, which
5281 is one step out from LEVEL. */
5284 reopen_tinst_level (tree level
)
5289 for (t
= level
; t
; t
= TREE_CHAIN (t
))
5292 current_tinst_level
= level
;
5296 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5297 vector of template arguments, as for tsubst.
5299 Returns an appropriate tsubst'd friend declaration. */
5302 tsubst_friend_function (tree decl
, tree args
)
5306 if (TREE_CODE (decl
) == FUNCTION_DECL
5307 && DECL_TEMPLATE_INSTANTIATION (decl
)
5308 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
5309 /* This was a friend declared with an explicit template
5310 argument list, e.g.:
5314 to indicate that f was a template instantiation, not a new
5315 function declaration. Now, we have to figure out what
5316 instantiation of what template. */
5318 tree template_id
, arglist
, fns
;
5321 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
5323 /* Friend functions are looked up in the containing namespace scope.
5324 We must enter that scope, to avoid finding member functions of the
5325 current cless with same name. */
5326 push_nested_namespace (ns
);
5327 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
5328 tf_warning_or_error
, NULL_TREE
,
5329 /*integral_constant_expression_p=*/false);
5330 pop_nested_namespace (ns
);
5331 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
5332 tf_warning_or_error
, NULL_TREE
);
5333 template_id
= lookup_template_function (fns
, arglist
);
5335 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
5336 tmpl
= determine_specialization (template_id
, new_friend
,
5338 /*need_member_template=*/0,
5339 TREE_VEC_LENGTH (args
),
5341 return instantiate_template (tmpl
, new_args
, tf_error
);
5344 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
5346 /* The NEW_FRIEND will look like an instantiation, to the
5347 compiler, but is not an instantiation from the point of view of
5348 the language. For example, we might have had:
5350 template <class T> struct S {
5351 template <class U> friend void f(T, U);
5354 Then, in S<int>, template <class U> void f(int, U) is not an
5355 instantiation of anything. */
5356 if (new_friend
== error_mark_node
)
5357 return error_mark_node
;
5359 DECL_USE_TEMPLATE (new_friend
) = 0;
5360 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
5362 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
5363 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
5364 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
5367 /* The mangled name for the NEW_FRIEND is incorrect. The function
5368 is not a template instantiation and should not be mangled like
5369 one. Therefore, we forget the mangling here; we'll recompute it
5370 later if we need it. */
5371 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
5373 SET_DECL_RTL (new_friend
, NULL_RTX
);
5374 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
5377 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
5380 tree new_friend_template_info
;
5381 tree new_friend_result_template_info
;
5383 int new_friend_is_defn
;
5385 /* We must save some information from NEW_FRIEND before calling
5386 duplicate decls since that function will free NEW_FRIEND if
5388 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
5389 new_friend_is_defn
=
5390 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5391 (template_for_substitution (new_friend
)))
5393 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
5395 /* This declaration is a `primary' template. */
5396 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
5398 new_friend_result_template_info
5399 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
5402 new_friend_result_template_info
= NULL_TREE
;
5404 /* Make the init_value nonzero so pushdecl knows this is a defn. */
5405 if (new_friend_is_defn
)
5406 DECL_INITIAL (new_friend
) = error_mark_node
;
5408 /* Inside pushdecl_namespace_level, we will push into the
5409 current namespace. However, the friend function should go
5410 into the namespace of the template. */
5411 ns
= decl_namespace_context (new_friend
);
5412 push_nested_namespace (ns
);
5413 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
5414 pop_nested_namespace (ns
);
5416 if (old_decl
== error_mark_node
)
5417 return error_mark_node
;
5419 if (old_decl
!= new_friend
)
5421 /* This new friend declaration matched an existing
5422 declaration. For example, given:
5424 template <class T> void f(T);
5425 template <class U> class C {
5426 template <class T> friend void f(T) {}
5429 the friend declaration actually provides the definition
5430 of `f', once C has been instantiated for some type. So,
5431 old_decl will be the out-of-class template declaration,
5432 while new_friend is the in-class definition.
5434 But, if `f' was called before this point, the
5435 instantiation of `f' will have DECL_TI_ARGS corresponding
5436 to `T' but not to `U', references to which might appear
5437 in the definition of `f'. Previously, the most general
5438 template for an instantiation of `f' was the out-of-class
5439 version; now it is the in-class version. Therefore, we
5440 run through all specialization of `f', adding to their
5441 DECL_TI_ARGS appropriately. In particular, they need a
5442 new set of outer arguments, corresponding to the
5443 arguments for this class instantiation.
5445 The same situation can arise with something like this:
5448 template <class T> class C {
5452 when `C<int>' is instantiated. Now, `f(int)' is defined
5455 if (!new_friend_is_defn
)
5456 /* On the other hand, if the in-class declaration does
5457 *not* provide a definition, then we don't want to alter
5458 existing definitions. We can just leave everything
5463 /* Overwrite whatever template info was there before, if
5464 any, with the new template information pertaining to
5466 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
5468 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
5469 reregister_specialization (new_friend
,
5470 most_general_template (old_decl
),
5475 tree new_friend_args
;
5477 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
5478 = new_friend_result_template_info
;
5480 new_friend_args
= TI_ARGS (new_friend_template_info
);
5481 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (old_decl
);
5485 tree spec
= TREE_VALUE (t
);
5488 = add_outermost_template_args (new_friend_args
,
5489 DECL_TI_ARGS (spec
));
5492 /* Now, since specializations are always supposed to
5493 hang off of the most general template, we must move
5495 t
= most_general_template (old_decl
);
5498 DECL_TEMPLATE_SPECIALIZATIONS (t
)
5499 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t
),
5500 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
));
5501 DECL_TEMPLATE_SPECIALIZATIONS (old_decl
) = NULL_TREE
;
5506 /* The information from NEW_FRIEND has been merged into OLD_DECL
5507 by duplicate_decls. */
5508 new_friend
= old_decl
;
5513 tree context
= DECL_CONTEXT (new_friend
);
5517 template <class T> class C {
5518 template <class U> friend void C1<U>::f (); // case 1
5519 friend void C2<T>::f (); // case 2
5521 we only need to make sure CONTEXT is a complete type for
5522 case 2. To distinguish between the two cases, we note that
5523 CONTEXT of case 1 remains dependent type after tsubst while
5524 this isn't true for case 2. */
5525 ++processing_template_decl
;
5526 dependent_p
= dependent_type_p (context
);
5527 --processing_template_decl
;
5530 && !complete_type_or_else (context
, NULL_TREE
))
5531 return error_mark_node
;
5533 if (COMPLETE_TYPE_P (context
))
5535 /* Check to see that the declaration is really present, and,
5536 possibly obtain an improved declaration. */
5537 tree fn
= check_classfn (context
,
5538 new_friend
, NULL_TREE
);
5548 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5549 template arguments, as for tsubst.
5551 Returns an appropriate tsubst'd friend type or error_mark_node on
5555 tsubst_friend_class (tree friend_tmpl
, tree args
)
5561 context
= DECL_CONTEXT (friend_tmpl
);
5565 if (TREE_CODE (context
) == NAMESPACE_DECL
)
5566 push_nested_namespace (context
);
5568 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
5571 /* Look for a class template declaration. We look for hidden names
5572 because two friend declarations of the same template are the
5573 same. For example, in:
5576 template <typename> friend class F;
5578 template <typename> struct B {
5579 template <typename> friend class F;
5582 both F templates are the same. */
5583 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
5584 /*block_p=*/true, 0,
5585 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
5587 /* But, if we don't find one, it might be because we're in a
5588 situation like this:
5596 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5597 for `S<int>', not the TEMPLATE_DECL. */
5598 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
5600 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
5601 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
5604 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
5606 /* The friend template has already been declared. Just
5607 check to see that the declarations match, and install any new
5608 default parameters. We must tsubst the default parameters,
5609 of course. We only need the innermost template parameters
5610 because that is all that redeclare_class_template will look
5612 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
5613 > TMPL_ARGS_DEPTH (args
))
5616 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
5617 args
, tf_warning_or_error
);
5618 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
5621 friend_type
= TREE_TYPE (tmpl
);
5625 /* The friend template has not already been declared. In this
5626 case, the instantiation of the template class will cause the
5627 injection of this template into the global scope. */
5628 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
5629 if (tmpl
== error_mark_node
)
5630 return error_mark_node
;
5632 /* The new TMPL is not an instantiation of anything, so we
5633 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
5634 the new type because that is supposed to be the corresponding
5635 template decl, i.e., TMPL. */
5636 DECL_USE_TEMPLATE (tmpl
) = 0;
5637 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
5638 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
5639 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
5640 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
5642 /* Inject this template into the global scope. */
5643 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
5648 if (TREE_CODE (context
) == NAMESPACE_DECL
)
5649 pop_nested_namespace (context
);
5651 pop_nested_class ();
5657 /* Returns zero if TYPE cannot be completed later due to circularity.
5658 Otherwise returns one. */
5661 can_complete_type_without_circularity (tree type
)
5663 if (type
== NULL_TREE
|| type
== error_mark_node
)
5665 else if (COMPLETE_TYPE_P (type
))
5667 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
5668 return can_complete_type_without_circularity (TREE_TYPE (type
));
5669 else if (CLASS_TYPE_P (type
)
5670 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
5677 instantiate_class_template (tree type
)
5679 tree
template, args
, pattern
, t
, member
;
5684 if (type
== error_mark_node
)
5685 return error_mark_node
;
5687 if (TYPE_BEING_DEFINED (type
)
5688 || COMPLETE_TYPE_P (type
)
5689 || dependent_type_p (type
))
5692 /* Figure out which template is being instantiated. */
5693 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
5694 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL
);
5696 /* Determine what specialization of the original template to
5698 t
= most_specialized_class (type
, template);
5699 if (t
== error_mark_node
)
5701 TYPE_BEING_DEFINED (type
) = 1;
5702 return error_mark_node
;
5706 /* This TYPE is actually an instantiation of a partial
5707 specialization. We replace the innermost set of ARGS with
5708 the arguments appropriate for substitution. For example,
5711 template <class T> struct S {};
5712 template <class T> struct S<T*> {};
5714 and supposing that we are instantiating S<int*>, ARGS will
5715 presently be {int*} -- but we need {int}. */
5716 pattern
= TREE_TYPE (t
);
5717 args
= TREE_PURPOSE (t
);
5721 pattern
= TREE_TYPE (template);
5722 args
= CLASSTYPE_TI_ARGS (type
);
5725 /* If the template we're instantiating is incomplete, then clearly
5726 there's nothing we can do. */
5727 if (!COMPLETE_TYPE_P (pattern
))
5730 /* If we've recursively instantiated too many templates, stop. */
5731 if (! push_tinst_level (type
))
5734 /* Now we're really doing the instantiation. Mark the type as in
5735 the process of being defined. */
5736 TYPE_BEING_DEFINED (type
) = 1;
5738 /* We may be in the middle of deferred access check. Disable
5740 push_deferring_access_checks (dk_no_deferred
);
5742 push_to_top_level ();
5744 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
5746 /* Set the input location to the template definition. This is needed
5747 if tsubsting causes an error. */
5748 typedecl
= TYPE_MAIN_DECL (type
);
5749 input_location
= DECL_SOURCE_LOCATION (typedecl
);
5750 in_system_header
= DECL_IN_SYSTEM_HEADER (typedecl
);
5752 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
5753 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
5754 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
5755 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
5756 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
5757 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
5758 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
5759 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
5760 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
5761 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
5762 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
5763 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
5764 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
5765 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
5766 if (ANON_AGGR_TYPE_P (pattern
))
5767 SET_ANON_AGGR_TYPE_P (type
);
5768 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
5770 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
5771 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
5774 pbinfo
= TYPE_BINFO (pattern
);
5776 /* We should never instantiate a nested class before its enclosing
5777 class; we need to look up the nested class by name before we can
5778 instantiate it, and that lookup should instantiate the enclosing
5780 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
5781 || COMPLETE_TYPE_P (TYPE_CONTEXT (type
))
5782 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type
)));
5784 base_list
= NULL_TREE
;
5785 if (BINFO_N_BASE_BINFOS (pbinfo
))
5788 tree context
= TYPE_CONTEXT (type
);
5792 /* We must enter the scope containing the type, as that is where
5793 the accessibility of types named in dependent bases are
5795 pushed_scope
= push_scope (context
? context
: global_namespace
);
5797 /* Substitute into each of the bases to determine the actual
5799 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
5802 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
5804 /* Substitute to figure out the base class. */
5805 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
, NULL_TREE
);
5806 if (base
== error_mark_node
)
5809 base_list
= tree_cons (access
, base
, base_list
);
5810 if (BINFO_VIRTUAL_P (pbase_binfo
))
5811 TREE_TYPE (base_list
) = integer_type_node
;
5814 /* The list is now in reverse order; correct that. */
5815 base_list
= nreverse (base_list
);
5818 pop_scope (pushed_scope
);
5820 /* Now call xref_basetypes to set up all the base-class
5822 xref_basetypes (type
, base_list
);
5825 /* Now that our base classes are set up, enter the scope of the
5826 class, so that name lookups into base classes, etc. will work
5827 correctly. This is precisely analogous to what we do in
5828 begin_class_definition when defining an ordinary non-template
5832 /* Now members are processed in the order of declaration. */
5833 for (member
= CLASSTYPE_DECL_LIST (pattern
);
5834 member
; member
= TREE_CHAIN (member
))
5836 tree t
= TREE_VALUE (member
);
5838 if (TREE_PURPOSE (member
))
5842 /* Build new CLASSTYPE_NESTED_UTDS. */
5845 bool class_template_p
;
5847 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
5848 && TYPE_LANG_SPECIFIC (t
)
5849 && CLASSTYPE_IS_TEMPLATE (t
));
5850 /* If the member is a class template, then -- even after
5851 substitution -- there may be dependent types in the
5852 template argument list for the class. We increment
5853 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5854 that function will assume that no types are dependent
5855 when outside of a template. */
5856 if (class_template_p
)
5857 ++processing_template_decl
;
5858 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
5859 if (class_template_p
)
5860 --processing_template_decl
;
5861 if (newtag
== error_mark_node
)
5864 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
5866 tree name
= TYPE_IDENTIFIER (t
);
5868 if (class_template_p
)
5869 /* Unfortunately, lookup_template_class sets
5870 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5871 instantiation (i.e., for the type of a member
5872 template class nested within a template class.)
5873 This behavior is required for
5874 maybe_process_partial_specialization to work
5875 correctly, but is not accurate in this case;
5876 the TAG is not an instantiation of anything.
5877 (The corresponding TEMPLATE_DECL is an
5878 instantiation, but the TYPE is not.) */
5879 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
5881 /* Now, we call pushtag to put this NEWTAG into the scope of
5882 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5883 pushtag calling push_template_decl. We don't have to do
5884 this for enums because it will already have been done in
5887 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
5888 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
5891 else if (TREE_CODE (t
) == FUNCTION_DECL
5892 || DECL_FUNCTION_TEMPLATE_P (t
))
5894 /* Build new TYPE_METHODS. */
5897 if (TREE_CODE (t
) == TEMPLATE_DECL
)
5898 ++processing_template_decl
;
5899 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
5900 if (TREE_CODE (t
) == TEMPLATE_DECL
)
5901 --processing_template_decl
;
5902 set_current_access_from_decl (r
);
5903 finish_member_declaration (r
);
5907 /* Build new TYPE_FIELDS. */
5908 if (TREE_CODE (t
) == STATIC_ASSERT
)
5911 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
5912 tf_warning_or_error
, NULL_TREE
,
5913 /*integral_constant_expression_p=*/true);
5914 finish_static_assert (condition
,
5915 STATIC_ASSERT_MESSAGE (t
),
5916 STATIC_ASSERT_SOURCE_LOCATION (t
),
5919 else if (TREE_CODE (t
) != CONST_DECL
)
5923 /* The the file and line for this declaration, to
5924 assist in error message reporting. Since we
5925 called push_tinst_level above, we don't need to
5927 input_location
= DECL_SOURCE_LOCATION (t
);
5929 if (TREE_CODE (t
) == TEMPLATE_DECL
)
5930 ++processing_template_decl
;
5931 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
5932 if (TREE_CODE (t
) == TEMPLATE_DECL
)
5933 --processing_template_decl
;
5934 if (TREE_CODE (r
) == VAR_DECL
)
5938 [t]he initialization (and any associated
5939 side-effects) of a static data member does
5940 not occur unless the static data member is
5941 itself used in a way that requires the
5942 definition of the static data member to
5945 Therefore, we do not substitute into the
5946 initialized for the static data member here. */
5947 finish_static_data_member_decl
5950 /*init_const_expr_p=*/false,
5951 /*asmspec_tree=*/NULL_TREE
,
5953 if (DECL_INITIALIZED_IN_CLASS_P (r
))
5954 check_static_variable_definition (r
, TREE_TYPE (r
));
5956 else if (TREE_CODE (r
) == FIELD_DECL
)
5958 /* Determine whether R has a valid type and can be
5959 completed later. If R is invalid, then it is
5960 replaced by error_mark_node so that it will not be
5961 added to TYPE_FIELDS. */
5962 tree rtype
= TREE_TYPE (r
);
5963 if (can_complete_type_without_circularity (rtype
))
5964 complete_type (rtype
);
5966 if (!COMPLETE_TYPE_P (rtype
))
5968 cxx_incomplete_type_error (r
, rtype
);
5969 r
= error_mark_node
;
5973 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5974 such a thing will already have been added to the field
5975 list by tsubst_enum in finish_member_declaration in the
5976 CLASSTYPE_NESTED_UTDS case above. */
5977 if (!(TREE_CODE (r
) == TYPE_DECL
5978 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
5979 && DECL_ARTIFICIAL (r
)))
5981 set_current_access_from_decl (r
);
5982 finish_member_declaration (r
);
5989 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
5991 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5993 tree friend_type
= t
;
5994 bool adjust_processing_template_decl
= false;
5996 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
5998 /* template <class T> friend class C; */
5999 friend_type
= tsubst_friend_class (friend_type
, args
);
6000 adjust_processing_template_decl
= true;
6002 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
6004 /* template <class T> friend class C::D; */
6005 friend_type
= tsubst (friend_type
, args
,
6006 tf_warning_or_error
, NULL_TREE
);
6007 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
6008 friend_type
= TREE_TYPE (friend_type
);
6009 adjust_processing_template_decl
= true;
6011 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
6013 /* This could be either
6017 when dependent_type_p is false or
6019 template <class U> friend class T::C;
6022 friend_type
= tsubst (friend_type
, args
,
6023 tf_warning_or_error
, NULL_TREE
);
6024 /* Bump processing_template_decl for correct
6025 dependent_type_p calculation. */
6026 ++processing_template_decl
;
6027 if (dependent_type_p (friend_type
))
6028 adjust_processing_template_decl
= true;
6029 --processing_template_decl
;
6031 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
6032 && hidden_name_p (TYPE_NAME (friend_type
)))
6036 where C hasn't been declared yet. Let's lookup name
6037 from namespace scope directly, bypassing any name that
6038 come from dependent base class. */
6039 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
6041 /* The call to xref_tag_from_type does injection for friend
6043 push_nested_namespace (ns
);
6045 xref_tag_from_type (friend_type
, NULL_TREE
,
6046 /*tag_scope=*/ts_current
);
6047 pop_nested_namespace (ns
);
6049 else if (uses_template_parms (friend_type
))
6050 /* friend class C<T>; */
6051 friend_type
= tsubst (friend_type
, args
,
6052 tf_warning_or_error
, NULL_TREE
);
6057 where C is already declared or
6059 friend class C<int>;
6061 We don't have to do anything in these cases. */
6063 if (adjust_processing_template_decl
)
6064 /* Trick make_friend_class into realizing that the friend
6065 we're adding is a template, not an ordinary class. It's
6066 important that we use make_friend_class since it will
6067 perform some error-checking and output cross-reference
6069 ++processing_template_decl
;
6071 if (friend_type
!= error_mark_node
)
6072 make_friend_class (type
, friend_type
, /*complain=*/false);
6074 if (adjust_processing_template_decl
)
6075 --processing_template_decl
;
6079 /* Build new DECL_FRIENDLIST. */
6082 /* The the file and line for this declaration, to
6083 assist in error message reporting. Since we
6084 called push_tinst_level above, we don't need to
6086 input_location
= DECL_SOURCE_LOCATION (t
);
6088 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6090 ++processing_template_decl
;
6091 push_deferring_access_checks (dk_no_check
);
6094 r
= tsubst_friend_function (t
, args
);
6095 add_friend (type
, r
, /*complain=*/false);
6096 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6098 pop_deferring_access_checks ();
6099 --processing_template_decl
;
6105 /* Set the file and line number information to whatever is given for
6106 the class itself. This puts error messages involving generated
6107 implicit functions at a predictable point, and the same point
6108 that would be used for non-template classes. */
6109 input_location
= DECL_SOURCE_LOCATION (typedecl
);
6111 unreverse_member_declarations (type
);
6112 finish_struct_1 (type
);
6113 TYPE_BEING_DEFINED (type
) = 0;
6115 /* Now that the class is complete, instantiate default arguments for
6116 any member functions. We don't do this earlier because the
6117 default arguments may reference members of the class. */
6118 if (!PRIMARY_TEMPLATE_P (template))
6119 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
6120 if (TREE_CODE (t
) == FUNCTION_DECL
6121 /* Implicitly generated member functions will not have template
6122 information; they are not instantiations, but instead are
6123 created "fresh" for each instantiation. */
6124 && DECL_TEMPLATE_INFO (t
))
6125 tsubst_default_arguments (t
);
6128 pop_from_top_level ();
6129 pop_deferring_access_checks ();
6132 /* The vtable for a template class can be emitted in any translation
6133 unit in which the class is instantiated. When there is no key
6134 method, however, finish_struct_1 will already have added TYPE to
6135 the keyed_classes list. */
6136 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
6137 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
6143 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
6149 else if (TYPE_P (t
))
6150 r
= tsubst (t
, args
, complain
, in_decl
);
6153 r
= tsubst_expr (t
, args
, complain
, in_decl
,
6154 /*integral_constant_expression_p=*/true);
6155 r
= fold_non_dependent_expr (r
);
6160 /* Substitute ARGS into the vector or list of template arguments T. */
6163 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
6165 int len
= TREE_VEC_LENGTH (t
);
6166 int need_new
= 0, i
;
6167 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
6169 for (i
= 0; i
< len
; i
++)
6171 tree orig_arg
= TREE_VEC_ELT (t
, i
);
6174 if (TREE_CODE (orig_arg
) == TREE_VEC
)
6175 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
6177 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
6179 if (new_arg
== error_mark_node
)
6180 return error_mark_node
;
6183 if (new_arg
!= orig_arg
)
6190 t
= make_tree_vec (len
);
6191 for (i
= 0; i
< len
; i
++)
6192 TREE_VEC_ELT (t
, i
) = elts
[i
];
6197 /* Return the result of substituting ARGS into the template parameters
6198 given by PARMS. If there are m levels of ARGS and m + n levels of
6199 PARMS, then the result will contain n levels of PARMS. For
6200 example, if PARMS is `template <class T> template <class U>
6201 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
6202 result will be `template <int*, double, class V>'. */
6205 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
6210 /* When substituting into a template, we must set
6211 PROCESSING_TEMPLATE_DECL as the template parameters may be
6212 dependent if they are based on one-another, and the dependency
6213 predicates are short-circuit outside of templates. */
6214 ++processing_template_decl
;
6216 for (new_parms
= &r
;
6217 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
6218 new_parms
= &(TREE_CHAIN (*new_parms
)),
6219 parms
= TREE_CHAIN (parms
))
6222 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
6225 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
6231 if (parms
== error_mark_node
)
6234 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
6236 if (tuple
== error_mark_node
)
6239 default_value
= TREE_PURPOSE (tuple
);
6240 parm_decl
= TREE_VALUE (tuple
);
6242 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
6243 if (TREE_CODE (parm_decl
) == PARM_DECL
6244 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
6245 parm_decl
= error_mark_node
;
6246 default_value
= tsubst_template_arg (default_value
, args
,
6247 complain
, NULL_TREE
);
6249 tuple
= build_tree_list (default_value
, parm_decl
);
6250 TREE_VEC_ELT (new_vec
, i
) = tuple
;
6254 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
6255 - TMPL_ARGS_DEPTH (args
)),
6256 new_vec
, NULL_TREE
);
6259 --processing_template_decl
;
6264 /* Substitute the ARGS into the indicated aggregate (or enumeration)
6265 type T. If T is not an aggregate or enumeration type, it is
6266 handled as if by tsubst. IN_DECL is as for tsubst. If
6267 ENTERING_SCOPE is nonzero, T is the context for a template which
6268 we are presently tsubst'ing. Return the substituted value. */
6271 tsubst_aggr_type (tree t
,
6273 tsubst_flags_t complain
,
6280 switch (TREE_CODE (t
))
6283 if (TYPE_PTRMEMFUNC_P (t
))
6284 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
6286 /* Else fall through. */
6289 if (TYPE_TEMPLATE_INFO (t
))
6294 bool saved_skip_evaluation
;
6296 /* In "sizeof(X<I>)" we need to evaluate "I". */
6297 saved_skip_evaluation
= skip_evaluation
;
6298 skip_evaluation
= false;
6300 /* First, determine the context for the type we are looking
6302 context
= TYPE_CONTEXT (t
);
6304 context
= tsubst_aggr_type (context
, args
, complain
,
6305 in_decl
, /*entering_scope=*/1);
6307 /* Then, figure out what arguments are appropriate for the
6308 type we are trying to find. For example, given:
6310 template <class T> struct S;
6311 template <class T, class U> void f(T, U) { S<U> su; }
6313 and supposing that we are instantiating f<int, double>,
6314 then our ARGS will be {int, double}, but, when looking up
6315 S we only want {double}. */
6316 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
6318 if (argvec
== error_mark_node
)
6319 r
= error_mark_node
;
6322 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
6323 entering_scope
, complain
);
6324 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
6327 skip_evaluation
= saved_skip_evaluation
;
6332 /* This is not a template type, so there's nothing to do. */
6336 return tsubst (t
, args
, complain
, in_decl
);
6340 /* Substitute into the default argument ARG (a default argument for
6341 FN), which has the indicated TYPE. */
6344 tsubst_default_argument (tree fn
, tree type
, tree arg
)
6346 tree saved_class_ptr
= NULL_TREE
;
6347 tree saved_class_ref
= NULL_TREE
;
6349 /* This default argument came from a template. Instantiate the
6350 default argument here, not in tsubst. In the case of
6359 we must be careful to do name lookup in the scope of S<T>,
6360 rather than in the current class. */
6361 push_access_scope (fn
);
6362 /* The "this" pointer is not valid in a default argument. */
6365 saved_class_ptr
= current_class_ptr
;
6366 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
6367 saved_class_ref
= current_class_ref
;
6368 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
6371 push_deferring_access_checks(dk_no_deferred
);
6372 /* The default argument expression may cause implicitly defined
6373 member functions to be synthesized, which will result in garbage
6374 collection. We must treat this situation as if we were within
6375 the body of function so as to avoid collecting live data on the
6378 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
6379 tf_warning_or_error
, NULL_TREE
,
6380 /*integral_constant_expression_p=*/false);
6382 pop_deferring_access_checks();
6384 /* Restore the "this" pointer. */
6387 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
6388 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
6391 pop_access_scope (fn
);
6393 /* Make sure the default argument is reasonable. */
6394 arg
= check_default_argument (type
, arg
);
6399 /* Substitute into all the default arguments for FN. */
6402 tsubst_default_arguments (tree fn
)
6407 tmpl_args
= DECL_TI_ARGS (fn
);
6409 /* If this function is not yet instantiated, we certainly don't need
6410 its default arguments. */
6411 if (uses_template_parms (tmpl_args
))
6414 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
6416 arg
= TREE_CHAIN (arg
))
6417 if (TREE_PURPOSE (arg
))
6418 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
6420 TREE_PURPOSE (arg
));
6423 /* Substitute the ARGS into the T, which is a _DECL. Return the
6424 result of the substitution. Issue error and warning messages under
6425 control of COMPLAIN. */
6428 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
6430 location_t saved_loc
;
6434 /* Set the filename and linenumber to improve error-reporting. */
6435 saved_loc
= input_location
;
6436 input_location
= DECL_SOURCE_LOCATION (t
);
6438 switch (TREE_CODE (t
))
6442 /* We can get here when processing a member function template,
6443 member class template, and template template parameter of
6444 a template class. */
6445 tree decl
= DECL_TEMPLATE_RESULT (t
);
6450 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
6452 /* Template template parameter is treated here. */
6453 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6454 if (new_type
== error_mark_node
)
6455 return error_mark_node
;
6458 TREE_CHAIN (r
) = NULL_TREE
;
6459 TREE_TYPE (r
) = new_type
;
6460 DECL_TEMPLATE_RESULT (r
)
6461 = build_decl (TYPE_DECL
, DECL_NAME (decl
), new_type
);
6462 DECL_TEMPLATE_PARMS (r
)
6463 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
6465 TYPE_NAME (new_type
) = r
;
6469 /* We might already have an instance of this template.
6470 The ARGS are for the surrounding class type, so the
6471 full args contain the tsubst'd args for the context,
6472 plus the innermost args from the template decl. */
6473 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
6474 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
6475 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
6476 /* Because this is a template, the arguments will still be
6477 dependent, even after substitution. If
6478 PROCESSING_TEMPLATE_DECL is not set, the dependency
6479 predicates will short-circuit. */
6480 ++processing_template_decl
;
6481 full_args
= tsubst_template_args (tmpl_args
, args
,
6483 --processing_template_decl
;
6484 if (full_args
== error_mark_node
)
6485 return error_mark_node
;
6487 /* tsubst_template_args doesn't copy the vector if
6488 nothing changed. But, *something* should have
6490 gcc_assert (full_args
!= tmpl_args
);
6492 spec
= retrieve_specialization (t
, full_args
,
6493 /*class_specializations_p=*/true);
6494 if (spec
!= NULL_TREE
)
6500 /* Make a new template decl. It will be similar to the
6501 original, but will record the current template arguments.
6502 We also create a new function declaration, which is just
6503 like the old one, but points to this new template, rather
6504 than the old one. */
6506 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
6507 TREE_CHAIN (r
) = NULL_TREE
;
6509 DECL_TEMPLATE_INFO (r
) = build_tree_list (t
, args
);
6511 if (TREE_CODE (decl
) == TYPE_DECL
)
6514 ++processing_template_decl
;
6515 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6516 --processing_template_decl
;
6517 if (new_type
== error_mark_node
)
6518 return error_mark_node
;
6520 TREE_TYPE (r
) = new_type
;
6521 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
6522 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
6523 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
6524 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
6529 ++processing_template_decl
;
6530 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
6531 --processing_template_decl
;
6532 if (new_decl
== error_mark_node
)
6533 return error_mark_node
;
6535 DECL_TEMPLATE_RESULT (r
) = new_decl
;
6536 DECL_TI_TEMPLATE (new_decl
) = r
;
6537 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
6538 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
6539 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
6542 SET_DECL_IMPLICIT_INSTANTIATION (r
);
6543 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
6544 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
6546 /* The template parameters for this new template are all the
6547 template parameters for the old template, except the
6548 outermost level of parameters. */
6549 DECL_TEMPLATE_PARMS (r
)
6550 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
6553 if (PRIMARY_TEMPLATE_P (t
))
6554 DECL_PRIMARY_TEMPLATE (r
) = r
;
6556 if (TREE_CODE (decl
) != TYPE_DECL
)
6557 /* Record this non-type partial instantiation. */
6558 register_specialization (r
, t
,
6559 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
6567 tree argvec
= NULL_TREE
;
6575 /* Nobody should be tsubst'ing into non-template functions. */
6576 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
6578 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
6583 /* If T is not dependent, just return it. We have to
6584 increment PROCESSING_TEMPLATE_DECL because
6585 value_dependent_expression_p assumes that nothing is
6586 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6587 ++processing_template_decl
;
6588 dependent_p
= value_dependent_expression_p (t
);
6589 --processing_template_decl
;
6593 /* Calculate the most general template of which R is a
6594 specialization, and the complete set of arguments used to
6596 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
6597 argvec
= tsubst_template_args (DECL_TI_ARGS
6598 (DECL_TEMPLATE_RESULT (gen_tmpl
)),
6599 args
, complain
, in_decl
);
6601 /* Check to see if we already have this specialization. */
6602 spec
= retrieve_specialization (gen_tmpl
, argvec
,
6603 /*class_specializations_p=*/false);
6611 /* We can see more levels of arguments than parameters if
6612 there was a specialization of a member template, like
6615 template <class T> struct S { template <class U> void f(); }
6616 template <> template <class U> void S<int>::f(U);
6618 Here, we'll be substituting into the specialization,
6619 because that's where we can find the code we actually
6620 want to generate, but we'll have enough arguments for
6621 the most general template.
6623 We also deal with the peculiar case:
6625 template <class T> struct S {
6626 template <class U> friend void f();
6628 template <class U> void f() {}
6630 template void f<double>();
6632 Here, the ARGS for the instantiation of will be {int,
6633 double}. But, we only need as many ARGS as there are
6634 levels of template parameters in CODE_PATTERN. We are
6635 careful not to get fooled into reducing the ARGS in
6638 template <class T> struct S { template <class U> void f(U); }
6639 template <class T> template <> void S<T>::f(int) {}
6641 which we can spot because the pattern will be a
6642 specialization in this case. */
6643 args_depth
= TMPL_ARGS_DEPTH (args
);
6645 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
6646 if (args_depth
> parms_depth
6647 && !DECL_TEMPLATE_SPECIALIZATION (t
))
6648 args
= get_innermost_template_args (args
, parms_depth
);
6652 /* This special case arises when we have something like this:
6654 template <class T> struct S {
6655 friend void f<int>(int, double);
6658 Here, the DECL_TI_TEMPLATE for the friend declaration
6659 will be an IDENTIFIER_NODE. We are being called from
6660 tsubst_friend_function, and we want only to create a
6661 new decl (R) with appropriate types so that we can call
6662 determine_specialization. */
6663 gen_tmpl
= NULL_TREE
;
6666 if (DECL_CLASS_SCOPE_P (t
))
6668 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
6672 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
6673 complain
, t
, /*entering_scope=*/1);
6678 ctx
= DECL_CONTEXT (t
);
6680 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6681 if (type
== error_mark_node
)
6682 return error_mark_node
;
6684 /* We do NOT check for matching decls pushed separately at this
6685 point, as they may not represent instantiations of this
6686 template, and in any case are considered separate under the
6689 DECL_USE_TEMPLATE (r
) = 0;
6690 TREE_TYPE (r
) = type
;
6691 /* Clear out the mangled name and RTL for the instantiation. */
6692 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
6693 SET_DECL_RTL (r
, NULL_RTX
);
6694 DECL_INITIAL (r
) = NULL_TREE
;
6695 DECL_CONTEXT (r
) = ctx
;
6697 if (member
&& DECL_CONV_FN_P (r
))
6698 /* Type-conversion operator. Reconstruct the name, in
6699 case it's the name of one of the template's parameters. */
6700 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
6702 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
6704 DECL_RESULT (r
) = NULL_TREE
;
6706 TREE_STATIC (r
) = 0;
6707 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
6708 DECL_EXTERNAL (r
) = 1;
6709 /* If this is an instantiation of a function with internal
6710 linkage, we already know what object file linkage will be
6711 assigned to the instantiation. */
6712 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
6713 DECL_DEFER_OUTPUT (r
) = 0;
6714 TREE_CHAIN (r
) = NULL_TREE
;
6715 DECL_PENDING_INLINE_INFO (r
) = 0;
6716 DECL_PENDING_INLINE_P (r
) = 0;
6717 DECL_SAVED_TREE (r
) = NULL_TREE
;
6719 if (DECL_CLONED_FUNCTION (r
))
6721 DECL_CLONED_FUNCTION (r
) = tsubst (DECL_CLONED_FUNCTION (t
),
6723 TREE_CHAIN (r
) = TREE_CHAIN (DECL_CLONED_FUNCTION (r
));
6724 TREE_CHAIN (DECL_CLONED_FUNCTION (r
)) = r
;
6727 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6728 this in the special friend case mentioned above where
6729 GEN_TMPL is NULL. */
6732 DECL_TEMPLATE_INFO (r
)
6733 = tree_cons (gen_tmpl
, argvec
, NULL_TREE
);
6734 SET_DECL_IMPLICIT_INSTANTIATION (r
);
6735 register_specialization (r
, gen_tmpl
, argvec
, false);
6737 /* We're not supposed to instantiate default arguments
6738 until they are called, for a template. But, for a
6741 template <class T> void f ()
6742 { extern void g(int i = T()); }
6744 we should do the substitution when the template is
6745 instantiated. We handle the member function case in
6746 instantiate_class_template since the default arguments
6747 might refer to other members of the class. */
6749 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
6750 && !uses_template_parms (argvec
))
6751 tsubst_default_arguments (r
);
6754 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
6756 /* Copy the list of befriending classes. */
6757 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
6759 friends
= &TREE_CHAIN (*friends
))
6761 *friends
= copy_node (*friends
);
6762 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
6767 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
6769 maybe_retrofit_in_chrg (r
);
6770 if (DECL_CONSTRUCTOR_P (r
))
6771 grok_ctor_properties (ctx
, r
);
6772 /* If this is an instantiation of a member template, clone it.
6773 If it isn't, that'll be handled by
6774 clone_constructors_and_destructors. */
6775 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
6776 clone_function_decl (r
, /*update_method_vec_p=*/0);
6778 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r
))
6779 && !grok_op_properties (r
, (complain
& tf_error
) != 0))
6780 return error_mark_node
;
6782 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
6783 SET_DECL_FRIEND_CONTEXT (r
,
6784 tsubst (DECL_FRIEND_CONTEXT (t
),
6785 args
, complain
, in_decl
));
6787 /* Possibly limit visibility based on template args. */
6788 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
6789 if (DECL_VISIBILITY_SPECIFIED (t
))
6791 DECL_VISIBILITY_SPECIFIED (r
) = 0;
6793 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
6795 determine_visibility (r
);
6804 if (DECL_TEMPLATE_PARM_P (t
))
6805 SET_DECL_TEMPLATE_PARM_P (r
);
6807 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6808 type
= type_decays_to (type
);
6809 TREE_TYPE (r
) = type
;
6810 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
6812 if (DECL_INITIAL (r
))
6814 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
6815 DECL_INITIAL (r
) = TREE_TYPE (r
);
6817 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
6821 DECL_CONTEXT (r
) = NULL_TREE
;
6823 if (!DECL_TEMPLATE_PARM_P (r
))
6824 DECL_ARG_TYPE (r
) = type_passed_as (type
);
6826 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
,
6827 complain
, TREE_CHAIN (t
));
6836 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6837 if (type
== error_mark_node
)
6838 return error_mark_node
;
6839 TREE_TYPE (r
) = type
;
6840 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
6842 /* DECL_INITIAL gives the number of bits in a bit-field. */
6844 = tsubst_expr (DECL_INITIAL (t
), args
,
6846 /*integral_constant_expression_p=*/true);
6847 /* We don't have to set DECL_CONTEXT here; it is set by
6848 finish_member_declaration. */
6849 TREE_CHAIN (r
) = NULL_TREE
;
6850 if (VOID_TYPE_P (type
))
6851 error ("instantiation of %q+D as type %qT", r
, type
);
6856 /* We reach here only for member using decls. */
6857 if (DECL_DEPENDENT_P (t
))
6859 r
= do_class_using_decl
6860 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
6861 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
6863 r
= error_mark_node
;
6868 TREE_CHAIN (r
) = NULL_TREE
;
6875 tree argvec
= NULL_TREE
;
6876 tree gen_tmpl
= NULL_TREE
;
6878 tree tmpl
= NULL_TREE
;
6880 tree type
= NULL_TREE
;
6883 if (TREE_CODE (t
) == TYPE_DECL
)
6885 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6886 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
6887 || t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
6889 /* If this is the canonical decl, we don't have to
6890 mess with instantiations, and often we can't (for
6891 typename, template type parms and such). Note that
6892 TYPE_NAME is not correct for the above test if
6893 we've copied the type for a typedef. */
6894 r
= TYPE_NAME (type
);
6899 /* Check to see if we already have the specialization we
6902 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
6904 /* T is a static data member or namespace-scope entity.
6905 We have to substitute into namespace-scope variables
6906 (even though such entities are never templates) because
6909 template <class T> void f() { extern T t; }
6911 where the entity referenced is not known until
6912 instantiation time. */
6914 ctx
= DECL_CONTEXT (t
);
6915 if (DECL_CLASS_SCOPE_P (t
))
6917 ctx
= tsubst_aggr_type (ctx
, args
,
6919 in_decl
, /*entering_scope=*/1);
6920 /* If CTX is unchanged, then T is in fact the
6921 specialization we want. That situation occurs when
6922 referencing a static data member within in its own
6923 class. We can use pointer equality, rather than
6924 same_type_p, because DECL_CONTEXT is always
6926 if (ctx
== DECL_CONTEXT (t
))
6932 tmpl
= DECL_TI_TEMPLATE (t
);
6933 gen_tmpl
= most_general_template (tmpl
);
6934 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
6935 spec
= (retrieve_specialization
6937 /*class_specializations_p=*/false));
6942 /* A local variable. */
6944 /* Subsequent calls to pushdecl will fill this in. */
6946 spec
= retrieve_local_specialization (t
);
6948 /* If we already have the specialization we need, there is
6949 nothing more to do. */
6956 /* Create a new node for the specialization we need. */
6958 if (TREE_CODE (r
) == VAR_DECL
)
6960 /* Even if the original location is out of scope, the
6961 newly substituted one is not. */
6962 DECL_DEAD_FOR_LOCAL (r
) = 0;
6963 DECL_INITIALIZED_P (r
) = 0;
6964 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
6965 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
6966 if (type
== error_mark_node
)
6967 return error_mark_node
;
6968 if (TREE_CODE (type
) == FUNCTION_TYPE
)
6970 /* It may seem that this case cannot occur, since:
6975 declares a function, not a variable. However:
6978 template <typename T> void g() { T t; }
6979 template void g<f>();
6981 is an attempt to declare a variable with function
6983 error ("variable %qD has function type",
6984 /* R is not yet sufficiently initialized, so we
6985 just use its name. */
6987 return error_mark_node
;
6989 type
= complete_type (type
);
6990 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
6991 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
);
6992 type
= check_var_type (DECL_NAME (r
), type
);
6994 if (DECL_HAS_VALUE_EXPR_P (t
))
6996 tree ve
= DECL_VALUE_EXPR (t
);
6997 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
6998 /*constant_expression_p=*/false);
6999 SET_DECL_VALUE_EXPR (r
, ve
);
7002 else if (DECL_SELF_REFERENCE_P (t
))
7003 SET_DECL_SELF_REFERENCE_P (r
);
7004 TREE_TYPE (r
) = type
;
7005 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
7006 DECL_CONTEXT (r
) = ctx
;
7007 /* Clear out the mangled name and RTL for the instantiation. */
7008 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
7009 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
7010 SET_DECL_RTL (r
, NULL_RTX
);
7011 /* The initializer must not be expanded until it is required;
7013 DECL_INITIAL (r
) = NULL_TREE
;
7014 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
7015 SET_DECL_RTL (r
, NULL_RTX
);
7016 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
7017 if (TREE_CODE (r
) == VAR_DECL
)
7019 /* Possibly limit visibility based on template args. */
7020 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
7021 if (DECL_VISIBILITY_SPECIFIED (t
))
7023 DECL_VISIBILITY_SPECIFIED (r
) = 0;
7025 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
7027 determine_visibility (r
);
7032 /* A static data member declaration is always marked
7033 external when it is declared in-class, even if an
7034 initializer is present. We mimic the non-template
7036 DECL_EXTERNAL (r
) = 1;
7038 register_specialization (r
, gen_tmpl
, argvec
, false);
7039 DECL_TEMPLATE_INFO (r
) = tree_cons (tmpl
, argvec
, NULL_TREE
);
7040 SET_DECL_IMPLICIT_INSTANTIATION (r
);
7043 register_local_specialization (r
, t
);
7045 TREE_CHAIN (r
) = NULL_TREE
;
7054 /* Restore the file and line information. */
7055 input_location
= saved_loc
;
7060 /* Substitute into the ARG_TYPES of a function type. */
7063 tsubst_arg_types (tree arg_types
,
7065 tsubst_flags_t complain
,
7068 tree remaining_arg_types
;
7071 tree result
= NULL_TREE
;
7073 if (!arg_types
|| arg_types
== void_list_node
)
7076 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
7077 args
, complain
, in_decl
);
7078 if (remaining_arg_types
== error_mark_node
)
7079 return error_mark_node
;
7081 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
7082 if (type
== error_mark_node
)
7083 return error_mark_node
;
7084 if (VOID_TYPE_P (type
))
7086 if (complain
& tf_error
)
7088 error ("invalid parameter type %qT", type
);
7090 error ("in declaration %q+D", in_decl
);
7092 return error_mark_node
;
7095 /* Do array-to-pointer, function-to-pointer conversion, and ignore
7096 top-level qualifiers as required. */
7097 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
7099 /* We do not substitute into default arguments here. The standard
7100 mandates that they be instantiated only when needed, which is
7101 done in build_over_call. */
7102 default_arg
= TREE_PURPOSE (arg_types
);
7104 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
7106 /* We've instantiated a template before its default arguments
7107 have been parsed. This can happen for a nested template
7108 class, and is not an error unless we require the default
7109 argument in a call of this function. */
7110 result
= tree_cons (default_arg
, type
, remaining_arg_types
);
7111 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
), result
);
7114 result
= hash_tree_cons (default_arg
, type
, remaining_arg_types
);
7119 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
7120 *not* handle the exception-specification for FNTYPE, because the
7121 initial substitution of explicitly provided template parameters
7122 during argument deduction forbids substitution into the
7123 exception-specification:
7127 All references in the function type of the function template to the
7128 corresponding template parameters are replaced by the specified tem-
7129 plate argument values. If a substitution in a template parameter or
7130 in the function type of the function template results in an invalid
7131 type, type deduction fails. [Note: The equivalent substitution in
7132 exception specifications is done only when the function is instanti-
7133 ated, at which point a program is ill-formed if the substitution
7134 results in an invalid type.] */
7137 tsubst_function_type (tree t
,
7139 tsubst_flags_t complain
,
7146 /* The TYPE_CONTEXT is not used for function/method types. */
7147 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
7149 /* Substitute the return type. */
7150 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
7151 if (return_type
== error_mark_node
)
7152 return error_mark_node
;
7153 /* The standard does not presently indicate that creation of a
7154 function type with an invalid return type is a deduction failure.
7155 However, that is clearly analogous to creating an array of "void"
7156 or a reference to a reference. This is core issue #486. */
7157 if (TREE_CODE (return_type
) == ARRAY_TYPE
7158 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
7160 if (complain
& tf_error
)
7162 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
7163 error ("function returning an array");
7165 error ("function returning a function");
7167 return error_mark_node
;
7170 /* Substitute the argument types. */
7171 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
7173 if (arg_types
== error_mark_node
)
7174 return error_mark_node
;
7176 if (TYPE_QUALS (return_type
) != TYPE_UNQUALIFIED
7177 && in_decl
!= NULL_TREE
7178 && !TREE_NO_WARNING (in_decl
)
7179 && (SCALAR_TYPE_P (return_type
) || VOID_TYPE_P (return_type
)))
7180 warning (OPT_Wreturn_type
,
7181 "type qualifiers ignored on function return type");
7183 /* Construct a new type node and return it. */
7184 if (TREE_CODE (t
) == FUNCTION_TYPE
)
7185 fntype
= build_function_type (return_type
, arg_types
);
7188 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
7189 if (! IS_AGGR_TYPE (r
))
7193 Type deduction may fail for any of the following
7196 -- Attempting to create "pointer to member of T" when T
7197 is not a class type. */
7198 if (complain
& tf_error
)
7199 error ("creating pointer to member function of non-class type %qT",
7201 return error_mark_node
;
7204 fntype
= build_method_type_directly (r
, return_type
,
7205 TREE_CHAIN (arg_types
));
7207 fntype
= cp_build_qualified_type_real (fntype
, TYPE_QUALS (t
), complain
);
7208 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
7213 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
7214 ARGS into that specification, and return the substituted
7215 specification. If there is no specification, return NULL_TREE. */
7218 tsubst_exception_specification (tree fntype
,
7220 tsubst_flags_t complain
,
7226 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
7227 new_specs
= NULL_TREE
;
7230 if (! TREE_VALUE (specs
))
7236 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
7237 if (spec
== error_mark_node
)
7239 new_specs
= add_exception_specifier (new_specs
, spec
, complain
);
7240 specs
= TREE_CHAIN (specs
);
7246 /* Take the tree structure T and replace template parameters used
7247 therein with the argument vector ARGS. IN_DECL is an associated
7248 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
7249 Issue error and warning messages under control of COMPLAIN. Note
7250 that we must be relatively non-tolerant of extensions here, in
7251 order to preserve conformance; if we allow substitutions that
7252 should not be allowed, we may allow argument deductions that should
7253 not succeed, and therefore report ambiguous overload situations
7254 where there are none. In theory, we could allow the substitution,
7255 but indicate that it should have failed, and allow our caller to
7256 make sure that the right thing happens, but we don't try to do this
7259 This function is used for dealing with types, decls and the like;
7260 for expressions, use tsubst_expr or tsubst_copy. */
7263 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
7267 if (t
== NULL_TREE
|| t
== error_mark_node
7268 || t
== integer_type_node
7269 || t
== void_type_node
7270 || t
== char_type_node
7271 || t
== unknown_type_node
7272 || TREE_CODE (t
) == NAMESPACE_DECL
)
7276 return tsubst_decl (t
, args
, complain
);
7278 if (TREE_CODE (t
) == IDENTIFIER_NODE
)
7279 type
= IDENTIFIER_TYPE_VALUE (t
);
7281 type
= TREE_TYPE (t
);
7283 gcc_assert (type
!= unknown_type_node
);
7286 && TREE_CODE (t
) != TYPENAME_TYPE
7287 && TREE_CODE (t
) != IDENTIFIER_NODE
7288 && TREE_CODE (t
) != FUNCTION_TYPE
7289 && TREE_CODE (t
) != METHOD_TYPE
)
7290 type
= tsubst (type
, args
, complain
, in_decl
);
7291 if (type
== error_mark_node
)
7292 return error_mark_node
;
7294 switch (TREE_CODE (t
))
7299 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
7300 /*entering_scope=*/0);
7303 case IDENTIFIER_NODE
:
7315 if (t
== integer_type_node
)
7318 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
7319 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
7323 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
7325 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
7326 /*integral_constant_expression_p=*/false);
7327 max
= fold_decl_constant_value (max
);
7329 if (TREE_CODE (max
) != INTEGER_CST
7330 && TREE_CODE (max
) != TEMPLATE_PARM_INDEX
7331 && !at_function_scope_p ())
7333 if (complain
& tf_error
)
7334 error ("array bound is not an integer constant");
7335 return error_mark_node
;
7340 Type deduction may fail for any of the following
7343 Attempting to create an array with a size that is
7344 zero or negative. */
7345 if (integer_zerop (max
) && !(complain
& tf_error
))
7346 /* We must fail if performing argument deduction (as
7347 indicated by the state of complain), so that
7348 another substitution can be found. */
7349 return error_mark_node
;
7350 else if (TREE_CODE (max
) == INTEGER_CST
7351 && INT_CST_LT (max
, integer_zero_node
))
7353 if (complain
& tf_error
)
7354 error ("creating array with negative size (%qE)", max
);
7356 return error_mark_node
;
7359 return compute_array_index_type (NULL_TREE
, max
);
7362 case TEMPLATE_TYPE_PARM
:
7363 case TEMPLATE_TEMPLATE_PARM
:
7364 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7365 case TEMPLATE_PARM_INDEX
:
7370 tree arg
= NULL_TREE
;
7374 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
7375 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
7376 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
7377 || TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
7379 idx
= TEMPLATE_TYPE_IDX (t
);
7380 level
= TEMPLATE_TYPE_LEVEL (t
);
7384 idx
= TEMPLATE_PARM_IDX (t
);
7385 level
= TEMPLATE_PARM_LEVEL (t
);
7388 levels
= TMPL_ARGS_DEPTH (args
);
7389 if (level
<= levels
)
7390 arg
= TMPL_ARG (args
, level
, idx
);
7392 if (arg
== error_mark_node
)
7393 return error_mark_node
;
7394 else if (arg
!= NULL_TREE
)
7396 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
7399 gcc_assert (TYPE_P (arg
));
7401 /* cv-quals from the template are discarded when
7402 substituting in a function or reference type. */
7403 if (TREE_CODE (arg
) == FUNCTION_TYPE
7404 || TREE_CODE (arg
) == METHOD_TYPE
7405 || TREE_CODE (arg
) == REFERENCE_TYPE
)
7406 quals
= cp_type_quals (arg
);
7408 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
7410 return cp_build_qualified_type_real
7411 (arg
, quals
, complain
| tf_ignore_bad_quals
);
7413 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
7415 /* We are processing a type constructed from a
7416 template template parameter. */
7417 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
7418 args
, complain
, in_decl
);
7419 if (argvec
== error_mark_node
)
7420 return error_mark_node
;
7422 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7423 are resolving nested-types in the signature of a
7424 member function templates. Otherwise ARG is a
7425 TEMPLATE_DECL and is the real template to be
7427 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
7428 arg
= TYPE_NAME (arg
);
7430 r
= lookup_template_class (arg
,
7433 /*entering_scope=*/0,
7435 return cp_build_qualified_type_real
7436 (r
, TYPE_QUALS (t
), complain
);
7439 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7444 /* This can happen during the attempted tsubst'ing in
7445 unify. This means that we don't yet have any information
7446 about the template parameter in question. */
7449 /* If we get here, we must have been looking at a parm for a
7450 more deeply nested template. Make a new version of this
7451 template parameter, but with a lower level. */
7452 switch (TREE_CODE (t
))
7454 case TEMPLATE_TYPE_PARM
:
7455 case TEMPLATE_TEMPLATE_PARM
:
7456 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7457 if (cp_type_quals (t
))
7459 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
7460 r
= cp_build_qualified_type_real
7461 (r
, cp_type_quals (t
),
7462 complain
| (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
7463 ? tf_ignore_bad_quals
: 0));
7468 TEMPLATE_TYPE_PARM_INDEX (r
)
7469 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
7471 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
7472 TYPE_MAIN_VARIANT (r
) = r
;
7473 TYPE_POINTER_TO (r
) = NULL_TREE
;
7474 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
7476 if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
7478 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
7480 if (argvec
== error_mark_node
)
7481 return error_mark_node
;
7483 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
7484 = tree_cons (TYPE_TI_TEMPLATE (t
), argvec
, NULL_TREE
);
7489 case TEMPLATE_PARM_INDEX
:
7490 r
= reduce_template_parm_level (t
, type
, levels
);
7502 tree purpose
, value
, chain
;
7504 if (t
== void_list_node
)
7507 purpose
= TREE_PURPOSE (t
);
7510 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
7511 if (purpose
== error_mark_node
)
7512 return error_mark_node
;
7514 value
= TREE_VALUE (t
);
7517 value
= tsubst (value
, args
, complain
, in_decl
);
7518 if (value
== error_mark_node
)
7519 return error_mark_node
;
7521 chain
= TREE_CHAIN (t
);
7522 if (chain
&& chain
!= void_type_node
)
7524 chain
= tsubst (chain
, args
, complain
, in_decl
);
7525 if (chain
== error_mark_node
)
7526 return error_mark_node
;
7528 if (purpose
== TREE_PURPOSE (t
)
7529 && value
== TREE_VALUE (t
)
7530 && chain
== TREE_CHAIN (t
))
7532 return hash_tree_cons (purpose
, value
, chain
);
7536 /* We should never be tsubsting a binfo. */
7540 /* A vector of template arguments. */
7542 return tsubst_template_args (t
, args
, complain
, in_decl
);
7545 case REFERENCE_TYPE
:
7547 enum tree_code code
;
7549 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
7552 code
= TREE_CODE (t
);
7557 Type deduction may fail for any of the following
7560 -- Attempting to create a pointer to reference type.
7561 -- Attempting to create a reference to a reference type or
7562 a reference to void. */
7563 if (TREE_CODE (type
) == REFERENCE_TYPE
7564 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
7566 static location_t last_loc
;
7568 /* We keep track of the last time we issued this error
7569 message to avoid spewing a ton of messages during a
7570 single bad template instantiation. */
7571 if (complain
& tf_error
7572 #ifdef USE_MAPPED_LOCATION
7573 && last_loc
!= input_location
7575 && (last_loc
.line
!= input_line
7576 || last_loc
.file
!= input_filename
)
7580 if (TREE_CODE (type
) == VOID_TYPE
)
7581 error ("forming reference to void");
7583 error ("forming %s to reference type %qT",
7584 (code
== POINTER_TYPE
) ? "pointer" : "reference",
7586 last_loc
= input_location
;
7589 return error_mark_node
;
7591 else if (code
== POINTER_TYPE
)
7593 r
= build_pointer_type (type
);
7594 if (TREE_CODE (type
) == METHOD_TYPE
)
7595 r
= build_ptrmemfunc_type (r
);
7598 r
= build_reference_type (type
);
7599 r
= cp_build_qualified_type_real (r
, TYPE_QUALS (t
), complain
);
7601 if (r
!= error_mark_node
)
7602 /* Will this ever be needed for TYPE_..._TO values? */
7609 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
7610 if (r
== error_mark_node
|| !IS_AGGR_TYPE (r
))
7614 Type deduction may fail for any of the following
7617 -- Attempting to create "pointer to member of T" when T
7618 is not a class type. */
7619 if (complain
& tf_error
)
7620 error ("creating pointer to member of non-class type %qT", r
);
7621 return error_mark_node
;
7623 if (TREE_CODE (type
) == REFERENCE_TYPE
)
7625 if (complain
& tf_error
)
7626 error ("creating pointer to member reference type %qT", type
);
7627 return error_mark_node
;
7629 if (TREE_CODE (type
) == VOID_TYPE
)
7631 if (complain
& tf_error
)
7632 error ("creating pointer to member of type void");
7633 return error_mark_node
;
7635 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
7636 if (TREE_CODE (type
) == FUNCTION_TYPE
)
7638 /* The type of the implicit object parameter gets its
7639 cv-qualifiers from the FUNCTION_TYPE. */
7641 tree this_type
= cp_build_qualified_type (TYPE_MAIN_VARIANT (r
),
7642 cp_type_quals (type
));
7644 method_type
= build_method_type_directly (this_type
,
7646 TYPE_ARG_TYPES (type
));
7647 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
7648 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
7652 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
7661 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
7662 if (fntype
== error_mark_node
)
7663 return error_mark_node
;
7665 /* Substitute the exception specification. */
7666 specs
= tsubst_exception_specification (t
, args
, complain
,
7668 if (specs
== error_mark_node
)
7669 return error_mark_node
;
7671 fntype
= build_exception_variant (fntype
, specs
);
7676 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
7677 if (domain
== error_mark_node
)
7678 return error_mark_node
;
7680 /* As an optimization, we avoid regenerating the array type if
7681 it will obviously be the same as T. */
7682 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
7685 /* These checks should match the ones in grokdeclarator.
7689 The deduction may fail for any of the following reasons:
7691 -- Attempting to create an array with an element type that
7692 is void, a function type, or a reference type, or [DR337]
7693 an abstract class type. */
7694 if (TREE_CODE (type
) == VOID_TYPE
7695 || TREE_CODE (type
) == FUNCTION_TYPE
7696 || TREE_CODE (type
) == REFERENCE_TYPE
)
7698 if (complain
& tf_error
)
7699 error ("creating array of %qT", type
);
7700 return error_mark_node
;
7702 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
7704 if (complain
& tf_error
)
7705 error ("creating array of %qT, which is an abstract class type",
7707 return error_mark_node
;
7710 r
= build_cplus_array_type (type
, domain
);
7717 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7718 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
7720 if (e1
== error_mark_node
|| e2
== error_mark_node
)
7721 return error_mark_node
;
7723 return fold_build2 (TREE_CODE (t
), TREE_TYPE (t
), e1
, e2
);
7729 tree e
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7730 if (e
== error_mark_node
)
7731 return error_mark_node
;
7733 return fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), e
);
7738 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
7739 in_decl
, /*entering_scope=*/1);
7740 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
7743 if (ctx
== error_mark_node
|| f
== error_mark_node
)
7744 return error_mark_node
;
7746 if (!IS_AGGR_TYPE (ctx
))
7748 if (complain
& tf_error
)
7749 error ("%qT is not a class, struct, or union type", ctx
);
7750 return error_mark_node
;
7752 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
7754 /* Normally, make_typename_type does not require that the CTX
7755 have complete type in order to allow things like:
7757 template <class T> struct S { typename S<T>::X Y; };
7759 But, such constructs have already been resolved by this
7760 point, so here CTX really should have complete type, unless
7761 it's a partial instantiation. */
7762 ctx
= complete_type (ctx
);
7763 if (!COMPLETE_TYPE_P (ctx
))
7765 if (complain
& tf_error
)
7766 cxx_incomplete_type_error (NULL_TREE
, ctx
);
7767 return error_mark_node
;
7771 f
= make_typename_type (ctx
, f
, typename_type
,
7772 (complain
& tf_error
) | tf_keep_type_decl
);
7773 if (f
== error_mark_node
)
7775 if (TREE_CODE (f
) == TYPE_DECL
)
7777 complain
|= tf_ignore_bad_quals
;
7781 if (TREE_CODE (f
) != TYPENAME_TYPE
)
7783 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
7784 error ("%qT resolves to %qT, which is not an enumeration type",
7786 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
7787 error ("%qT resolves to %qT, which is is not a class type",
7791 return cp_build_qualified_type_real
7792 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
7795 case UNBOUND_CLASS_TEMPLATE
:
7797 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
7798 in_decl
, /*entering_scope=*/1);
7799 tree name
= TYPE_IDENTIFIER (t
);
7800 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
7802 if (ctx
== error_mark_node
|| name
== error_mark_node
)
7803 return error_mark_node
;
7806 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
7807 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
7817 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7818 tree e2
= tsubst_expr (TREE_OPERAND (t
, 1), args
, complain
, in_decl
,
7819 /*integral_constant_expression_p=*/false);
7820 if (e1
== error_mark_node
|| e2
== error_mark_node
)
7821 return error_mark_node
;
7823 return build_nt (ARRAY_REF
, e1
, e2
, NULL_TREE
, NULL_TREE
);
7828 tree e1
= tsubst (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
7829 tree e2
= tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
);
7830 if (e1
== error_mark_node
|| e2
== error_mark_node
)
7831 return error_mark_node
;
7833 return build_qualified_name (/*type=*/NULL_TREE
,
7834 e1
, e2
, QUALIFIED_NAME_IS_TEMPLATE (t
));
7841 type
= finish_typeof (tsubst_expr
7842 (TYPEOF_TYPE_EXPR (t
), args
,
7844 /*integral_constant_expression_p=*/false));
7845 return cp_build_qualified_type_real (type
,
7847 | cp_type_quals (type
),
7852 sorry ("use of %qs in template",
7853 tree_code_name
[(int) TREE_CODE (t
)]);
7854 return error_mark_node
;
7858 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7859 type of the expression on the left-hand side of the "." or "->"
7863 tsubst_baselink (tree baselink
, tree object_type
,
7864 tree args
, tsubst_flags_t complain
, tree in_decl
)
7867 tree qualifying_scope
;
7870 tree template_args
= 0;
7871 bool template_id_p
= false;
7873 /* A baselink indicates a function from a base class. Both the
7874 BASELINK_ACCESS_BINFO and the base class referenced may
7875 indicate bases of the template class, rather than the
7876 instantiated class. In addition, lookups that were not
7877 ambiguous before may be ambiguous now. Therefore, we perform
7878 the lookup again. */
7879 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
7880 qualifying_scope
= tsubst (qualifying_scope
, args
,
7882 fns
= BASELINK_FUNCTIONS (baselink
);
7883 optype
= BASELINK_OPTYPE (baselink
);
7884 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
7886 template_id_p
= true;
7887 template_args
= TREE_OPERAND (fns
, 1);
7888 fns
= TREE_OPERAND (fns
, 0);
7890 template_args
= tsubst_template_args (template_args
, args
,
7893 name
= DECL_NAME (get_first_fn (fns
));
7894 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
7896 /* If lookup found a single function, mark it as used at this
7897 point. (If it lookup found multiple functions the one selected
7898 later by overload resolution will be marked as used at that
7900 if (BASELINK_P (baselink
))
7901 fns
= BASELINK_FUNCTIONS (baselink
);
7902 if (!template_id_p
&& !really_overloaded_fn (fns
))
7903 mark_used (OVL_CURRENT (fns
));
7905 /* Add back the template arguments, if present. */
7906 if (BASELINK_P (baselink
) && template_id_p
)
7907 BASELINK_FUNCTIONS (baselink
)
7908 = build_nt (TEMPLATE_ID_EXPR
,
7909 BASELINK_FUNCTIONS (baselink
),
7911 /* Update the conversion operator type. */
7912 BASELINK_OPTYPE (baselink
)
7913 = tsubst (optype
, args
, complain
, in_decl
);
7916 object_type
= current_class_type
;
7917 return adjust_result_of_qualified_name_lookup (baselink
,
7922 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7923 true if the qualified-id will be a postfix-expression in-and-of
7924 itself; false if more of the postfix-expression follows the
7925 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7929 tsubst_qualified_id (tree qualified_id
, tree args
,
7930 tsubst_flags_t complain
, tree in_decl
,
7931 bool done
, bool address_p
)
7939 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
7941 /* Figure out what name to look up. */
7942 name
= TREE_OPERAND (qualified_id
, 1);
7943 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
7946 template_args
= TREE_OPERAND (name
, 1);
7948 template_args
= tsubst_template_args (template_args
, args
,
7950 name
= TREE_OPERAND (name
, 0);
7954 is_template
= false;
7955 template_args
= NULL_TREE
;
7958 /* Substitute into the qualifying scope. When there are no ARGS, we
7959 are just trying to simplify a non-dependent expression. In that
7960 case the qualifying scope may be dependent, and, in any case,
7961 substituting will not help. */
7962 scope
= TREE_OPERAND (qualified_id
, 0);
7965 scope
= tsubst (scope
, args
, complain
, in_decl
);
7966 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
7971 if (dependent_type_p (scope
))
7972 return build_qualified_name (/*type=*/NULL_TREE
,
7974 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
7976 if (!BASELINK_P (name
) && !DECL_P (expr
))
7978 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
7979 /* If this were actually a destructor call, it would have been
7980 parsed as such by the parser. */
7981 expr
= error_mark_node
;
7983 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
7984 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
7985 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
7987 if (complain
& tf_error
)
7989 error ("dependent-name %qE is parsed as a non-type, but "
7990 "instantiation yields a type", qualified_id
);
7991 inform ("say %<typename %E%> if a type is meant", qualified_id
);
7993 return error_mark_node
;
7999 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
8001 /* Remember that there was a reference to this entity. */
8005 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
8007 if (complain
& tf_error
)
8008 qualified_name_lookup_error (scope
,
8009 TREE_OPERAND (qualified_id
, 1),
8011 return error_mark_node
;
8015 expr
= lookup_template_function (expr
, template_args
);
8017 if (expr
== error_mark_node
&& complain
& tf_error
)
8018 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
8020 else if (TYPE_P (scope
))
8022 expr
= (adjust_result_of_qualified_name_lookup
8023 (expr
, scope
, current_class_type
));
8024 expr
= (finish_qualified_id_expr
8025 (scope
, expr
, done
, address_p
,
8026 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
8027 /*template_arg_p=*/false));
8030 /* Expressions do not generally have reference type. */
8031 if (TREE_CODE (expr
) != SCOPE_REF
8032 /* However, if we're about to form a pointer-to-member, we just
8033 want the referenced member referenced. */
8034 && TREE_CODE (expr
) != OFFSET_REF
)
8035 expr
= convert_from_reference (expr
);
8040 /* Like tsubst, but deals with expressions. This function just replaces
8041 template parms; to finish processing the resultant expression, use
8045 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8047 enum tree_code code
;
8050 if (t
== NULL_TREE
|| t
== error_mark_node
)
8053 code
= TREE_CODE (t
);
8058 r
= retrieve_local_specialization (t
);
8059 gcc_assert (r
!= NULL
);
8068 if (DECL_TEMPLATE_PARM_P (t
))
8069 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
8070 /* There is no need to substitute into namespace-scope
8072 if (DECL_NAMESPACE_SCOPE_P (t
))
8074 /* If ARGS is NULL, then T is known to be non-dependent. */
8075 if (args
== NULL_TREE
)
8076 return integral_constant_value (t
);
8078 /* Unfortunately, we cannot just call lookup_name here.
8081 template <int I> int f() {
8083 struct S { void g() { E e = a; } };
8086 When we instantiate f<7>::S::g(), say, lookup_name is not
8087 clever enough to find f<7>::a. */
8089 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
8090 /*entering_scope=*/0);
8092 for (v
= TYPE_VALUES (enum_type
);
8095 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
8096 return TREE_VALUE (v
);
8098 /* We didn't find the name. That should never happen; if
8099 name-lookup found it during preliminary parsing, we
8100 should find it again here during instantiation. */
8106 if (DECL_CONTEXT (t
))
8110 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
8111 /*entering_scope=*/1);
8112 if (ctx
!= DECL_CONTEXT (t
))
8114 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
8117 if (complain
& tf_error
)
8118 error ("using invalid field %qD", t
);
8119 return error_mark_node
;
8129 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
8130 || local_variable_p (t
))
8131 t
= tsubst (t
, args
, complain
, in_decl
);
8136 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
8139 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8140 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
8141 args
, complain
, in_decl
);
8142 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
8143 return tsubst (t
, args
, complain
, in_decl
);
8144 else if (DECL_CLASS_SCOPE_P (t
)
8145 && uses_template_parms (DECL_CONTEXT (t
)))
8147 /* Template template argument like the following example need
8150 template <template <class> class TT> struct C {};
8151 template <class T> struct D {
8152 template <class U> struct E {};
8157 We are processing the template argument `E' in #1 for
8158 the template instantiation #2. Originally, `E' is a
8159 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
8160 have to substitute this with one having context `D<int>'. */
8162 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
8163 return lookup_field (context
, DECL_NAME(t
), 0, false);
8166 /* Ordinary template template argument. */
8170 case REINTERPRET_CAST_EXPR
:
8171 case CONST_CAST_EXPR
:
8172 case STATIC_CAST_EXPR
:
8173 case DYNAMIC_CAST_EXPR
:
8176 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
8177 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
8181 case TRUTH_NOT_EXPR
:
8184 case UNARY_PLUS_EXPR
: /* Unary + */
8193 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
8194 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
8201 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
8202 name
= TREE_OPERAND (t
, 1);
8203 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
8205 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
8207 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
8209 else if (TREE_CODE (name
) == SCOPE_REF
8210 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
8212 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
8214 name
= TREE_OPERAND (name
, 1);
8215 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
8217 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
8218 name
= build_qualified_name (/*type=*/NULL_TREE
,
8220 /*template_p=*/false);
8222 else if (TREE_CODE (name
) == BASELINK
)
8223 name
= tsubst_baselink (name
,
8224 non_reference (TREE_TYPE (object
)),
8228 name
= tsubst_copy (name
, args
, complain
, in_decl
);
8229 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
8235 case TRUNC_DIV_EXPR
:
8237 case FLOOR_DIV_EXPR
:
8238 case ROUND_DIV_EXPR
:
8239 case EXACT_DIV_EXPR
:
8243 case TRUNC_MOD_EXPR
:
8244 case FLOOR_MOD_EXPR
:
8245 case TRUTH_ANDIF_EXPR
:
8246 case TRUTH_ORIF_EXPR
:
8247 case TRUTH_AND_EXPR
:
8264 case PREDECREMENT_EXPR
:
8265 case PREINCREMENT_EXPR
:
8266 case POSTDECREMENT_EXPR
:
8267 case POSTINCREMENT_EXPR
:
8269 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8270 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
8273 return build_qualified_name (/*type=*/NULL_TREE
,
8274 tsubst_copy (TREE_OPERAND (t
, 0),
8275 args
, complain
, in_decl
),
8276 tsubst_copy (TREE_OPERAND (t
, 1),
8277 args
, complain
, in_decl
),
8278 QUALIFIED_NAME_IS_TEMPLATE (t
));
8283 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8284 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
8285 NULL_TREE
, NULL_TREE
);
8288 return build_nt (code
,
8289 tsubst_copy (TREE_OPERAND (t
, 0), args
,
8291 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
,
8297 case PSEUDO_DTOR_EXPR
:
8300 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8301 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
8302 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
8303 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
8310 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8311 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
8312 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
8313 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
8320 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
8321 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
8322 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
8323 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
8327 case TEMPLATE_ID_EXPR
:
8329 /* Substituted template arguments */
8330 tree fn
= TREE_OPERAND (t
, 0);
8331 tree targs
= TREE_OPERAND (t
, 1);
8333 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
8335 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
8337 return lookup_template_function (fn
, targs
);
8342 tree purpose
, value
, chain
;
8344 if (t
== void_list_node
)
8347 purpose
= TREE_PURPOSE (t
);
8349 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
8350 value
= TREE_VALUE (t
);
8352 value
= tsubst_copy (value
, args
, complain
, in_decl
);
8353 chain
= TREE_CHAIN (t
);
8354 if (chain
&& chain
!= void_type_node
)
8355 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
8356 if (purpose
== TREE_PURPOSE (t
)
8357 && value
== TREE_VALUE (t
)
8358 && chain
== TREE_CHAIN (t
))
8360 return tree_cons (purpose
, value
, chain
);
8367 case TEMPLATE_TYPE_PARM
:
8368 case TEMPLATE_TEMPLATE_PARM
:
8369 case BOUND_TEMPLATE_TEMPLATE_PARM
:
8370 case TEMPLATE_PARM_INDEX
:
8372 case REFERENCE_TYPE
:
8378 case UNBOUND_CLASS_TEMPLATE
:
8381 return tsubst (t
, args
, complain
, in_decl
);
8383 case IDENTIFIER_NODE
:
8384 if (IDENTIFIER_TYPENAME_P (t
))
8386 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8387 return mangle_conv_op_name_for_type (new_type
);
8393 /* This is handled by tsubst_copy_and_build. */
8397 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
8399 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
8401 case CLEANUP_POINT_EXPR
:
8402 /* We shouldn't have built any of these during initial template
8403 generation. Instead, they should be built during instantiation
8404 in response to the saved STMT_IS_FULL_EXPR_P setting. */
8408 mark_used (TREE_OPERAND (t
, 1));
8416 /* Like tsubst_copy, but specifically for OpenMP clauses. */
8419 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
8422 tree new_clauses
= NULL
, nc
, oc
;
8424 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
8426 nc
= copy_node (oc
);
8427 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
8430 switch (OMP_CLAUSE_CODE (nc
))
8432 case OMP_CLAUSE_PRIVATE
:
8433 case OMP_CLAUSE_SHARED
:
8434 case OMP_CLAUSE_FIRSTPRIVATE
:
8435 case OMP_CLAUSE_LASTPRIVATE
:
8436 case OMP_CLAUSE_REDUCTION
:
8437 case OMP_CLAUSE_COPYIN
:
8438 case OMP_CLAUSE_COPYPRIVATE
:
8440 case OMP_CLAUSE_NUM_THREADS
:
8441 case OMP_CLAUSE_SCHEDULE
:
8442 OMP_CLAUSE_OPERAND (nc
, 0)
8443 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
8444 in_decl
, /*integral_constant_expression_p=*/false);
8446 case OMP_CLAUSE_NOWAIT
:
8447 case OMP_CLAUSE_ORDERED
:
8448 case OMP_CLAUSE_DEFAULT
:
8455 return finish_omp_clauses (nreverse (new_clauses
));
8458 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8461 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
8464 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8466 tree purpose
, value
, chain
;
8471 if (TREE_CODE (t
) != TREE_LIST
)
8472 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
8473 /*function_p=*/false,
8474 /*integral_constant_expression_p=*/false);
8476 if (t
== void_list_node
)
8479 purpose
= TREE_PURPOSE (t
);
8481 purpose
= RECUR (purpose
);
8482 value
= TREE_VALUE (t
);
8484 value
= RECUR (value
);
8485 chain
= TREE_CHAIN (t
);
8486 if (chain
&& chain
!= void_type_node
)
8487 chain
= RECUR (chain
);
8488 return tree_cons (purpose
, value
, chain
);
8492 /* Like tsubst_copy for expressions, etc. but also does semantic
8496 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
8497 bool integral_constant_expression_p
)
8499 #define RECUR(NODE) \
8500 tsubst_expr ((NODE), args, complain, in_decl, \
8501 integral_constant_expression_p)
8505 if (t
== NULL_TREE
|| t
== error_mark_node
)
8508 if (EXPR_HAS_LOCATION (t
))
8509 input_location
= EXPR_LOCATION (t
);
8510 if (STATEMENT_CODE_P (TREE_CODE (t
)))
8511 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
8513 switch (TREE_CODE (t
))
8515 case STATEMENT_LIST
:
8517 tree_stmt_iterator i
;
8518 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
8519 RECUR (tsi_stmt (i
));
8523 case CTOR_INITIALIZER
:
8524 finish_mem_initializers (tsubst_initializer_list
8525 (TREE_OPERAND (t
, 0), args
));
8529 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
8533 tmp
= RECUR (EXPR_STMT_EXPR (t
));
8534 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
8535 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
8537 finish_expr_stmt (tmp
);
8541 do_using_directive (RECUR (USING_STMT_NAMESPACE (t
)));
8549 decl
= DECL_EXPR_DECL (t
);
8550 if (TREE_CODE (decl
) == LABEL_DECL
)
8551 finish_label_decl (DECL_NAME (decl
));
8552 else if (TREE_CODE (decl
) == USING_DECL
)
8554 tree scope
= USING_DECL_SCOPE (decl
);
8555 tree name
= DECL_NAME (decl
);
8558 scope
= RECUR (scope
);
8559 decl
= lookup_qualified_name (scope
, name
,
8560 /*is_type_p=*/false,
8561 /*complain=*/false);
8562 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
8563 qualified_name_lookup_error (scope
, name
, decl
);
8565 do_local_using_decl (decl
, scope
, name
);
8569 init
= DECL_INITIAL (decl
);
8570 decl
= tsubst (decl
, args
, complain
, in_decl
);
8571 if (decl
!= error_mark_node
)
8573 /* By marking the declaration as instantiated, we avoid
8574 trying to instantiate it. Since instantiate_decl can't
8575 handle local variables, and since we've already done
8576 all that needs to be done, that's the right thing to
8578 if (TREE_CODE (decl
) == VAR_DECL
)
8579 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
8580 if (TREE_CODE (decl
) == VAR_DECL
8581 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
8582 /* Anonymous aggregates are a special case. */
8583 finish_anon_union (decl
);
8586 maybe_push_decl (decl
);
8587 if (TREE_CODE (decl
) == VAR_DECL
8588 && DECL_PRETTY_FUNCTION_P (decl
))
8590 /* For __PRETTY_FUNCTION__ we have to adjust the
8592 const char *const name
8593 = cxx_printable_name (current_function_decl
, 2);
8594 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
8597 init
= RECUR (init
);
8598 finish_decl (decl
, init
, NULL_TREE
);
8603 /* A DECL_EXPR can also be used as an expression, in the condition
8604 clause of an if/for/while construct. */
8609 stmt
= begin_for_stmt ();
8610 RECUR (FOR_INIT_STMT (t
));
8611 finish_for_init_stmt (stmt
);
8612 tmp
= RECUR (FOR_COND (t
));
8613 finish_for_cond (tmp
, stmt
);
8614 tmp
= RECUR (FOR_EXPR (t
));
8615 finish_for_expr (tmp
, stmt
);
8616 RECUR (FOR_BODY (t
));
8617 finish_for_stmt (stmt
);
8621 stmt
= begin_while_stmt ();
8622 tmp
= RECUR (WHILE_COND (t
));
8623 finish_while_stmt_cond (tmp
, stmt
);
8624 RECUR (WHILE_BODY (t
));
8625 finish_while_stmt (stmt
);
8629 stmt
= begin_do_stmt ();
8630 RECUR (DO_BODY (t
));
8631 finish_do_body (stmt
);
8632 tmp
= RECUR (DO_COND (t
));
8633 finish_do_stmt (tmp
, stmt
);
8637 stmt
= begin_if_stmt ();
8638 tmp
= RECUR (IF_COND (t
));
8639 finish_if_stmt_cond (tmp
, stmt
);
8640 RECUR (THEN_CLAUSE (t
));
8641 finish_then_clause (stmt
);
8643 if (ELSE_CLAUSE (t
))
8645 begin_else_clause (stmt
);
8646 RECUR (ELSE_CLAUSE (t
));
8647 finish_else_clause (stmt
);
8650 finish_if_stmt (stmt
);
8654 if (BIND_EXPR_BODY_BLOCK (t
))
8655 stmt
= begin_function_body ();
8657 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
8658 ? BCS_TRY_BLOCK
: 0);
8660 RECUR (BIND_EXPR_BODY (t
));
8662 if (BIND_EXPR_BODY_BLOCK (t
))
8663 finish_function_body (stmt
);
8665 finish_compound_stmt (stmt
);
8669 finish_break_stmt ();
8673 finish_continue_stmt ();
8677 stmt
= begin_switch_stmt ();
8678 tmp
= RECUR (SWITCH_STMT_COND (t
));
8679 finish_switch_cond (tmp
, stmt
);
8680 RECUR (SWITCH_STMT_BODY (t
));
8681 finish_switch_stmt (stmt
);
8684 case CASE_LABEL_EXPR
:
8685 finish_case_label (RECUR (CASE_LOW (t
)),
8686 RECUR (CASE_HIGH (t
)));
8690 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t
)));
8694 tmp
= GOTO_DESTINATION (t
);
8695 if (TREE_CODE (tmp
) != LABEL_DECL
)
8696 /* Computed goto's must be tsubst'd into. On the other hand,
8697 non-computed gotos must not be; the identifier in question
8698 will have no binding. */
8701 tmp
= DECL_NAME (tmp
);
8702 finish_goto_stmt (tmp
);
8706 tmp
= finish_asm_stmt
8707 (ASM_VOLATILE_P (t
),
8708 RECUR (ASM_STRING (t
)),
8709 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
8710 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
8711 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
));
8713 tree asm_expr
= tmp
;
8714 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
8715 asm_expr
= TREE_OPERAND (asm_expr
, 0);
8716 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
8723 stmt
= begin_try_block ();
8724 RECUR (TRY_STMTS (t
));
8725 finish_cleanup_try_block (stmt
);
8726 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
8730 tree compound_stmt
= NULL_TREE
;
8732 if (FN_TRY_BLOCK_P (t
))
8733 stmt
= begin_function_try_block (&compound_stmt
);
8735 stmt
= begin_try_block ();
8737 RECUR (TRY_STMTS (t
));
8739 if (FN_TRY_BLOCK_P (t
))
8740 finish_function_try_block (stmt
);
8742 finish_try_block (stmt
);
8744 RECUR (TRY_HANDLERS (t
));
8745 if (FN_TRY_BLOCK_P (t
))
8746 finish_function_handler_sequence (stmt
, compound_stmt
);
8748 finish_handler_sequence (stmt
);
8754 tree decl
= HANDLER_PARMS (t
);
8758 decl
= tsubst (decl
, args
, complain
, in_decl
);
8759 /* Prevent instantiate_decl from trying to instantiate
8760 this variable. We've already done all that needs to be
8762 if (decl
!= error_mark_node
)
8763 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
8765 stmt
= begin_handler ();
8766 finish_handler_parms (decl
, stmt
);
8767 RECUR (HANDLER_BODY (t
));
8768 finish_handler (stmt
);
8773 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
8779 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
8782 /*integral_constant_expression_p=*/true);
8783 finish_static_assert (condition
,
8784 STATIC_ASSERT_MESSAGE (t
),
8785 STATIC_ASSERT_SOURCE_LOCATION (t
),
8786 /*member_p=*/false);
8791 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
8792 args
, complain
, in_decl
);
8793 stmt
= begin_omp_parallel ();
8794 RECUR (OMP_PARALLEL_BODY (t
));
8795 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
8796 = OMP_PARALLEL_COMBINED (t
);
8801 tree clauses
, decl
, init
, cond
, incr
, body
, pre_body
;
8803 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
8804 args
, complain
, in_decl
);
8805 init
= OMP_FOR_INIT (t
);
8806 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
8807 decl
= RECUR (TREE_OPERAND (init
, 0));
8808 init
= RECUR (TREE_OPERAND (init
, 1));
8809 cond
= RECUR (OMP_FOR_COND (t
));
8810 incr
= RECUR (OMP_FOR_INCR (t
));
8812 stmt
= begin_omp_structured_block ();
8814 pre_body
= push_stmt_list ();
8815 RECUR (OMP_FOR_PRE_BODY (t
));
8816 pre_body
= pop_stmt_list (pre_body
);
8818 body
= push_stmt_list ();
8819 RECUR (OMP_FOR_BODY (t
));
8820 body
= pop_stmt_list (body
);
8822 t
= finish_omp_for (EXPR_LOCATION (t
), decl
, init
, cond
, incr
, body
,
8825 OMP_FOR_CLAUSES (t
) = clauses
;
8827 add_stmt (finish_omp_structured_block (stmt
));
8833 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
8834 stmt
= push_stmt_list ();
8835 RECUR (OMP_BODY (t
));
8836 stmt
= pop_stmt_list (stmt
);
8839 OMP_BODY (t
) = stmt
;
8840 OMP_CLAUSES (t
) = tmp
;
8848 stmt
= push_stmt_list ();
8849 RECUR (OMP_BODY (t
));
8850 stmt
= pop_stmt_list (stmt
);
8853 OMP_BODY (t
) = stmt
;
8860 op0
= RECUR (TREE_OPERAND (t
, 0));
8861 op1
= RECUR (TREE_OPERAND (t
, 1));
8862 finish_omp_atomic (OMP_ATOMIC_CODE (t
), op0
, op1
);
8867 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
8869 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
8870 /*function_p=*/false,
8871 integral_constant_expression_p
);
8878 /* T is a postfix-expression that is not being used in a function
8879 call. Return the substituted version of T. */
8882 tsubst_non_call_postfix_expression (tree t
, tree args
,
8883 tsubst_flags_t complain
,
8886 if (TREE_CODE (t
) == SCOPE_REF
)
8887 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
8888 /*done=*/false, /*address_p=*/false);
8890 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
8891 /*function_p=*/false,
8892 /*integral_constant_expression_p=*/false);
8897 /* Like tsubst but deals with expressions and performs semantic
8898 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
8901 tsubst_copy_and_build (tree t
,
8903 tsubst_flags_t complain
,
8906 bool integral_constant_expression_p
)
8908 #define RECUR(NODE) \
8909 tsubst_copy_and_build (NODE, args, complain, in_decl, \
8910 /*function_p=*/false, \
8911 integral_constant_expression_p)
8915 if (t
== NULL_TREE
|| t
== error_mark_node
)
8918 switch (TREE_CODE (t
))
8923 case IDENTIFIER_NODE
:
8927 bool non_integral_constant_expression_p
;
8928 const char *error_msg
;
8930 if (IDENTIFIER_TYPENAME_P (t
))
8932 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8933 t
= mangle_conv_op_name_for_type (new_type
);
8936 /* Look up the name. */
8937 decl
= lookup_name (t
);
8939 /* By convention, expressions use ERROR_MARK_NODE to indicate
8940 failure, not NULL_TREE. */
8941 if (decl
== NULL_TREE
)
8942 decl
= error_mark_node
;
8944 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
8946 integral_constant_expression_p
,
8947 /*allow_non_integral_constant_expression_p=*/false,
8948 &non_integral_constant_expression_p
,
8949 /*template_p=*/false,
8951 /*address_p=*/false,
8952 /*template_arg_p=*/false,
8956 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
8957 decl
= unqualified_name_lookup_error (decl
);
8961 case TEMPLATE_ID_EXPR
:
8964 tree
template = RECUR (TREE_OPERAND (t
, 0));
8965 tree targs
= TREE_OPERAND (t
, 1);
8968 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
8970 if (TREE_CODE (template) == COMPONENT_REF
)
8972 object
= TREE_OPERAND (template, 0);
8973 template = TREE_OPERAND (template, 1);
8977 template = lookup_template_function (template, targs
);
8980 return build3 (COMPONENT_REF
, TREE_TYPE (template),
8981 object
, template, NULL_TREE
);
8983 return baselink_for_fns (template);
8988 tree r
= RECUR (TREE_OPERAND (t
, 0));
8990 if (REFERENCE_REF_P (t
))
8992 /* A type conversion to reference type will be enclosed in
8993 such an indirect ref, but the substitution of the cast
8994 will have also added such an indirect ref. */
8995 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
8996 r
= convert_from_reference (r
);
8999 r
= build_x_indirect_ref (r
, "unary *");
9005 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
9006 RECUR (TREE_OPERAND (t
, 0)));
9009 case REINTERPRET_CAST_EXPR
:
9010 case CONST_CAST_EXPR
:
9011 case DYNAMIC_CAST_EXPR
:
9012 case STATIC_CAST_EXPR
:
9017 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9018 if (integral_constant_expression_p
9019 && !cast_valid_in_integral_constant_expression_p (type
))
9021 error ("a cast to a type other than an integral or "
9022 "enumeration type cannot appear in a constant-expression");
9023 return error_mark_node
;
9026 op
= RECUR (TREE_OPERAND (t
, 0));
9028 switch (TREE_CODE (t
))
9031 return build_functional_cast (type
, op
);
9032 case REINTERPRET_CAST_EXPR
:
9033 return build_reinterpret_cast (type
, op
);
9034 case CONST_CAST_EXPR
:
9035 return build_const_cast (type
, op
);
9036 case DYNAMIC_CAST_EXPR
:
9037 return build_dynamic_cast (type
, op
);
9038 case STATIC_CAST_EXPR
:
9039 return build_static_cast (type
, op
);
9045 case POSTDECREMENT_EXPR
:
9046 case POSTINCREMENT_EXPR
:
9047 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
9048 args
, complain
, in_decl
);
9049 return build_x_unary_op (TREE_CODE (t
), op1
);
9051 case PREDECREMENT_EXPR
:
9052 case PREINCREMENT_EXPR
:
9056 case TRUTH_NOT_EXPR
:
9057 case UNARY_PLUS_EXPR
: /* Unary + */
9060 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)));
9063 op1
= TREE_OPERAND (t
, 0);
9064 if (TREE_CODE (op1
) == SCOPE_REF
)
9065 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
9066 /*done=*/true, /*address_p=*/true);
9068 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
9070 if (TREE_CODE (op1
) == LABEL_DECL
)
9071 return finish_label_address_expr (DECL_NAME (op1
));
9072 return build_x_unary_op (ADDR_EXPR
, op1
);
9077 case TRUNC_DIV_EXPR
:
9079 case FLOOR_DIV_EXPR
:
9080 case ROUND_DIV_EXPR
:
9081 case EXACT_DIV_EXPR
:
9085 case TRUNC_MOD_EXPR
:
9086 case FLOOR_MOD_EXPR
:
9087 case TRUTH_ANDIF_EXPR
:
9088 case TRUTH_ORIF_EXPR
:
9089 case TRUTH_AND_EXPR
:
9105 return build_x_binary_op
9107 RECUR (TREE_OPERAND (t
, 0)),
9108 RECUR (TREE_OPERAND (t
, 1)),
9109 /*overloaded_p=*/NULL
);
9112 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
9113 /*address_p=*/false);
9115 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
9116 args
, complain
, in_decl
);
9117 return build_x_binary_op (ARRAY_REF
, op1
, RECUR (TREE_OPERAND (t
, 1)),
9118 /*overloaded_p=*/NULL
);
9122 op1
= TREE_OPERAND (t
, 0);
9125 /* When there are no ARGS, we are trying to evaluate a
9126 non-dependent expression from the parser. Trying to do
9127 the substitutions may not work. */
9129 op1
= TREE_TYPE (op1
);
9134 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
9135 /*function_p=*/false,
9136 /*integral_constant_expression_p=*/false);
9140 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
), true);
9142 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
));
9146 tree r
= build_x_modify_expr
9147 (RECUR (TREE_OPERAND (t
, 0)),
9148 TREE_CODE (TREE_OPERAND (t
, 1)),
9149 RECUR (TREE_OPERAND (t
, 2)));
9150 /* TREE_NO_WARNING must be set if either the expression was
9151 parenthesized or it uses an operator such as >>= rather
9152 than plain assignment. In the former case, it was already
9153 set and must be copied. In the latter case,
9154 build_x_modify_expr sets it and it must not be reset
9156 if (TREE_NO_WARNING (t
))
9157 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
9162 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
9163 args
, complain
, in_decl
);
9164 /* Remember that there was a reference to this entity. */
9167 return build_x_arrow (op1
);
9171 (RECUR (TREE_OPERAND (t
, 0)),
9172 RECUR (TREE_OPERAND (t
, 1)),
9173 RECUR (TREE_OPERAND (t
, 2)),
9174 RECUR (TREE_OPERAND (t
, 3)),
9175 NEW_EXPR_USE_GLOBAL (t
));
9178 return delete_sanity
9179 (RECUR (TREE_OPERAND (t
, 0)),
9180 RECUR (TREE_OPERAND (t
, 1)),
9181 DELETE_EXPR_USE_VEC (t
),
9182 DELETE_EXPR_USE_GLOBAL (t
));
9185 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
9186 RECUR (TREE_OPERAND (t
, 1)));
9195 function
= TREE_OPERAND (t
, 0);
9196 /* When we parsed the expression, we determined whether or
9197 not Koenig lookup should be performed. */
9198 koenig_p
= KOENIG_LOOKUP_P (t
);
9199 if (TREE_CODE (function
) == SCOPE_REF
)
9202 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
9204 /*address_p=*/false);
9208 if (TREE_CODE (function
) == COMPONENT_REF
)
9210 tree op
= TREE_OPERAND (function
, 1);
9212 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
9214 && BASELINK_QUALIFIED_P (op
)));
9217 qualified_p
= false;
9219 function
= tsubst_copy_and_build (function
, args
, complain
,
9222 integral_constant_expression_p
);
9224 if (BASELINK_P (function
))
9228 call_args
= RECUR (TREE_OPERAND (t
, 1));
9230 /* We do not perform argument-dependent lookup if normal
9231 lookup finds a non-function, in accordance with the
9232 expected resolution of DR 218. */
9234 && ((is_overloaded_fn (function
)
9235 /* If lookup found a member function, the Koenig lookup is
9236 not appropriate, even if an unqualified-name was used
9237 to denote the function. */
9238 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
9239 || TREE_CODE (function
) == IDENTIFIER_NODE
))
9240 function
= perform_koenig_lookup (function
, call_args
);
9242 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
9244 unqualified_name_lookup_error (function
);
9245 return error_mark_node
;
9248 /* Remember that there was a reference to this entity. */
9249 if (DECL_P (function
))
9250 mark_used (function
);
9252 if (TREE_CODE (function
) == OFFSET_REF
)
9253 return build_offset_ref_call_from_tree (function
, call_args
);
9254 if (TREE_CODE (function
) == COMPONENT_REF
)
9256 if (!BASELINK_P (TREE_OPERAND (function
, 1)))
9257 return finish_call_expr (function
, call_args
,
9258 /*disallow_virtual=*/false,
9259 /*koenig_p=*/false);
9261 return (build_new_method_call
9262 (TREE_OPERAND (function
, 0),
9263 TREE_OPERAND (function
, 1),
9264 call_args
, NULL_TREE
,
9265 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
9268 return finish_call_expr (function
, call_args
,
9269 /*disallow_virtual=*/qualified_p
,
9274 return build_x_conditional_expr
9275 (RECUR (TREE_OPERAND (t
, 0)),
9276 RECUR (TREE_OPERAND (t
, 1)),
9277 RECUR (TREE_OPERAND (t
, 2)));
9279 case PSEUDO_DTOR_EXPR
:
9280 return finish_pseudo_destructor_expr
9281 (RECUR (TREE_OPERAND (t
, 0)),
9282 RECUR (TREE_OPERAND (t
, 1)),
9283 RECUR (TREE_OPERAND (t
, 2)));
9287 tree purpose
, value
, chain
;
9289 if (t
== void_list_node
)
9292 purpose
= TREE_PURPOSE (t
);
9294 purpose
= RECUR (purpose
);
9295 value
= TREE_VALUE (t
);
9297 value
= RECUR (value
);
9298 chain
= TREE_CHAIN (t
);
9299 if (chain
&& chain
!= void_type_node
)
9300 chain
= RECUR (chain
);
9301 if (purpose
== TREE_PURPOSE (t
)
9302 && value
== TREE_VALUE (t
)
9303 && chain
== TREE_CHAIN (t
))
9305 return tree_cons (purpose
, value
, chain
);
9314 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
9315 args
, complain
, in_decl
);
9316 /* Remember that there was a reference to this entity. */
9317 if (DECL_P (object
))
9319 object_type
= TREE_TYPE (object
);
9321 member
= TREE_OPERAND (t
, 1);
9322 if (BASELINK_P (member
))
9323 member
= tsubst_baselink (member
,
9324 non_reference (TREE_TYPE (object
)),
9325 args
, complain
, in_decl
);
9327 member
= tsubst_copy (member
, args
, complain
, in_decl
);
9328 if (member
== error_mark_node
)
9329 return error_mark_node
;
9331 if (object_type
&& !CLASS_TYPE_P (object_type
))
9333 if (TREE_CODE (member
) == BIT_NOT_EXPR
)
9334 return finish_pseudo_destructor_expr (object
,
9337 else if (TREE_CODE (member
) == SCOPE_REF
9338 && (TREE_CODE (TREE_OPERAND (member
, 1)) == BIT_NOT_EXPR
))
9339 return finish_pseudo_destructor_expr (object
,
9343 else if (TREE_CODE (member
) == SCOPE_REF
9344 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
9349 /* Lookup the template functions now that we know what the
9351 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
9352 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
9353 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
9354 /*is_type_p=*/false,
9355 /*complain=*/false);
9356 if (BASELINK_P (member
))
9358 BASELINK_FUNCTIONS (member
)
9359 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
9361 member
= (adjust_result_of_qualified_name_lookup
9362 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
9367 qualified_name_lookup_error (object_type
, tmpl
, member
);
9368 return error_mark_node
;
9371 else if (TREE_CODE (member
) == SCOPE_REF
9372 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
9373 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
9375 if (complain
& tf_error
)
9377 if (TYPE_P (TREE_OPERAND (member
, 0)))
9378 error ("%qT is not a class or namespace",
9379 TREE_OPERAND (member
, 0));
9381 error ("%qD is not a class or namespace",
9382 TREE_OPERAND (member
, 0));
9384 return error_mark_node
;
9386 else if (TREE_CODE (member
) == FIELD_DECL
)
9387 return finish_non_static_data_member (member
, object
, NULL_TREE
);
9389 return finish_class_member_access_expr (object
, member
,
9390 /*template_p=*/false);
9395 (RECUR (TREE_OPERAND (t
, 0)));
9399 VEC(constructor_elt
,gc
) *n
;
9400 constructor_elt
*ce
;
9401 unsigned HOST_WIDE_INT idx
;
9402 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9403 bool process_index_p
;
9405 if (type
== error_mark_node
)
9406 return error_mark_node
;
9408 /* digest_init will do the wrong thing if we let it. */
9409 if (type
&& TYPE_PTRMEMFUNC_P (type
))
9412 /* We do not want to process the index of aggregate
9413 initializers as they are identifier nodes which will be
9414 looked up by digest_init. */
9415 process_index_p
= !(type
&& IS_AGGR_TYPE (type
));
9417 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
9418 for (idx
= 0; VEC_iterate (constructor_elt
, n
, idx
, ce
); idx
++)
9420 if (ce
->index
&& process_index_p
)
9421 ce
->index
= RECUR (ce
->index
);
9422 ce
->value
= RECUR (ce
->value
);
9425 if (TREE_HAS_CONSTRUCTOR (t
))
9426 return finish_compound_literal (type
, n
);
9428 return build_constructor (NULL_TREE
, n
);
9433 tree operand_0
= RECUR (TREE_OPERAND (t
, 0));
9434 if (TYPE_P (operand_0
))
9435 return get_typeid (operand_0
);
9436 return build_typeid (operand_0
);
9446 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
9448 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
9449 /* If the original type was a reference, we'll be wrapped in
9450 the appropriate INDIRECT_REF. */
9451 r
= convert_from_reference (r
);
9456 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
9457 tsubst_copy (TREE_TYPE (t
), args
, complain
,
9461 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
9465 tree old_stmt_expr
= cur_stmt_expr
;
9466 tree stmt_expr
= begin_stmt_expr ();
9468 cur_stmt_expr
= stmt_expr
;
9469 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
9470 integral_constant_expression_p
);
9471 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
9472 cur_stmt_expr
= old_stmt_expr
;
9478 t
= tsubst_copy (t
, args
, complain
, in_decl
);
9479 /* As in finish_id_expression, we resolve enumeration constants
9480 to their underlying values. */
9481 if (TREE_CODE (t
) == CONST_DECL
)
9483 used_types_insert (TREE_TYPE (t
));
9484 return DECL_INITIAL (t
);
9489 /* Handle Objective-C++ constructs, if appropriate. */
9492 = objcp_tsubst_copy_and_build (t
, args
, complain
,
9493 in_decl
, /*function_p=*/false);
9497 return tsubst_copy (t
, args
, complain
, in_decl
);
9503 /* Verify that the instantiated ARGS are valid. For type arguments,
9504 make sure that the type's linkage is ok. For non-type arguments,
9505 make sure they are constants if they are integral or enumerations.
9506 Emit an error under control of COMPLAIN, and return TRUE on error. */
9509 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
9511 int ix
, len
= DECL_NTPARMS (tmpl
);
9512 bool result
= false;
9514 for (ix
= 0; ix
!= len
; ix
++)
9516 tree t
= TREE_VEC_ELT (args
, ix
);
9520 /* [basic.link]: A name with no linkage (notably, the name
9521 of a class or enumeration declared in a local scope)
9522 shall not be used to declare an entity with linkage.
9523 This implies that names with no linkage cannot be used as
9524 template arguments. */
9525 tree nt
= no_linkage_check (t
, /*relaxed_p=*/false);
9529 /* DR 488 makes use of a type with no linkage cause
9530 type deduction to fail. */
9531 if (complain
& tf_error
)
9533 if (TYPE_ANONYMOUS_P (nt
))
9534 error ("%qT is/uses anonymous type", t
);
9536 error ("template argument for %qD uses local type %qT",
9541 /* In order to avoid all sorts of complications, we do not
9542 allow variably-modified types as template arguments. */
9543 else if (variably_modified_type_p (t
, NULL_TREE
))
9545 if (complain
& tf_error
)
9546 error ("%qT is a variably modified type", t
);
9550 /* A non-type argument of integral or enumerated type must be a
9552 else if (TREE_TYPE (t
)
9553 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
9554 && !TREE_CONSTANT (t
))
9556 if (complain
& tf_error
)
9557 error ("integral expression %qE is not constant", t
);
9561 if (result
&& (complain
& tf_error
))
9562 error (" trying to instantiate %qD", tmpl
);
9566 /* Instantiate the indicated variable or function template TMPL with
9567 the template arguments in TARG_PTR. */
9570 instantiate_template (tree tmpl
, tree targ_ptr
, tsubst_flags_t complain
)
9575 HOST_WIDE_INT saved_processing_template_decl
;
9577 if (tmpl
== error_mark_node
)
9578 return error_mark_node
;
9580 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
9582 /* If this function is a clone, handle it specially. */
9583 if (DECL_CLONED_FUNCTION_P (tmpl
))
9588 spec
= instantiate_template (DECL_CLONED_FUNCTION (tmpl
), targ_ptr
,
9590 if (spec
== error_mark_node
)
9591 return error_mark_node
;
9593 /* Look for the clone. */
9594 FOR_EACH_CLONE (clone
, spec
)
9595 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
9597 /* We should always have found the clone by now. */
9602 /* Check to see if we already have this specialization. */
9603 spec
= retrieve_specialization (tmpl
, targ_ptr
,
9604 /*class_specializations_p=*/false);
9605 if (spec
!= NULL_TREE
)
9608 gen_tmpl
= most_general_template (tmpl
);
9609 if (tmpl
!= gen_tmpl
)
9611 /* The TMPL is a partial instantiation. To get a full set of
9612 arguments we must add the arguments used to perform the
9613 partial instantiation. */
9614 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
9617 /* Check to see if we already have this specialization. */
9618 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
,
9619 /*class_specializations_p=*/false);
9620 if (spec
!= NULL_TREE
)
9624 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
9626 return error_mark_node
;
9628 /* We are building a FUNCTION_DECL, during which the access of its
9629 parameters and return types have to be checked. However this
9630 FUNCTION_DECL which is the desired context for access checking
9631 is not built yet. We solve this chicken-and-egg problem by
9632 deferring all checks until we have the FUNCTION_DECL. */
9633 push_deferring_access_checks (dk_deferred
);
9635 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
9636 (because, for example, we have encountered a non-dependent
9637 function call in the body of a template function and must now
9638 determine which of several overloaded functions will be called),
9639 within the instantiation itself we are not processing a
9641 saved_processing_template_decl
= processing_template_decl
;
9642 processing_template_decl
= 0;
9643 /* Substitute template parameters to obtain the specialization. */
9644 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
9645 targ_ptr
, complain
, gen_tmpl
);
9646 processing_template_decl
= saved_processing_template_decl
;
9647 if (fndecl
== error_mark_node
)
9648 return error_mark_node
;
9650 /* Now we know the specialization, compute access previously
9652 push_access_scope (fndecl
);
9653 perform_deferred_access_checks ();
9654 pop_access_scope (fndecl
);
9655 pop_deferring_access_checks ();
9657 /* The DECL_TI_TEMPLATE should always be the immediate parent
9658 template, not the most general template. */
9659 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
9661 /* If we've just instantiated the main entry point for a function,
9662 instantiate all the alternate entry points as well. We do this
9663 by cloning the instantiation of the main entry point, not by
9664 instantiating the template clones. */
9665 if (TREE_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl
)))
9666 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
9671 /* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9672 arguments that are being used when calling it. TARGS is a vector
9673 into which the deduced template arguments are placed.
9675 Return zero for success, 2 for an incomplete match that doesn't resolve
9676 all the types, and 1 for complete failure. An error message will be
9677 printed only for an incomplete match.
9679 If FN is a conversion operator, or we are trying to produce a specific
9680 specialization, RETURN_TYPE is the return type desired.
9682 The EXPLICIT_TARGS are explicit template arguments provided via a
9685 The parameter STRICT is one of:
9688 We are deducing arguments for a function call, as in
9692 We are deducing arguments for a conversion function, as in
9696 We are deducing arguments when doing an explicit instantiation
9697 as in [temp.explicit], when determining an explicit specialization
9698 as in [temp.expl.spec], or when taking the address of a function
9699 template, as in [temp.deduct.funcaddr]. */
9702 fn_type_unification (tree fn
,
9703 tree explicit_targs
,
9707 unification_kind_t strict
,
9714 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
9716 fntype
= TREE_TYPE (fn
);
9721 The specified template arguments must match the template
9722 parameters in kind (i.e., type, nontype, template), and there
9723 must not be more arguments than there are parameters;
9724 otherwise type deduction fails.
9726 Nontype arguments must match the types of the corresponding
9727 nontype template parameters, or must be convertible to the
9728 types of the corresponding nontype parameters as specified in
9729 _temp.arg.nontype_, otherwise type deduction fails.
9731 All references in the function type of the function template
9732 to the corresponding template parameters are replaced by the
9733 specified template argument values. If a substitution in a
9734 template parameter or in the function type of the function
9735 template results in an invalid type, type deduction fails. */
9737 tree converted_args
;
9740 if (explicit_targs
== error_mark_node
)
9744 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
9745 explicit_targs
, NULL_TREE
, tf_none
,
9746 /*require_all_args=*/false,
9747 /*use_default_args=*/false));
9748 if (converted_args
== error_mark_node
)
9751 /* Substitute the explicit args into the function type. This is
9752 necessary so that, for instance, explicitly declared function
9753 arguments can match null pointed constants. If we were given
9754 an incomplete set of explicit args, we must not do semantic
9755 processing during substitution as we could create partial
9757 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
9758 processing_template_decl
+= incomplete
;
9759 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
9760 processing_template_decl
-= incomplete
;
9762 if (fntype
== error_mark_node
)
9765 /* Place the explicitly specified arguments in TARGS. */
9766 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
9767 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
9770 parms
= TYPE_ARG_TYPES (fntype
);
9771 /* Never do unification on the 'this' parameter. */
9772 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
9773 parms
= TREE_CHAIN (parms
);
9777 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
9778 args
= tree_cons (NULL_TREE
, return_type
, args
);
9781 /* We allow incomplete unification without an error message here
9782 because the standard doesn't seem to explicitly prohibit it. Our
9783 callers must be ready to deal with unification failures in any
9785 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
9786 targs
, parms
, args
, /*subr=*/0,
9790 /* All is well so far. Now, check:
9794 When all template arguments have been deduced, all uses of
9795 template parameters in nondeduced contexts are replaced with
9796 the corresponding deduced argument values. If the
9797 substitution results in an invalid type, as described above,
9798 type deduction fails. */
9799 if (tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
)
9806 /* Adjust types before performing type deduction, as described in
9807 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
9808 sections are symmetric. PARM is the type of a function parameter
9809 or the return type of the conversion function. ARG is the type of
9810 the argument passed to the call, or the type of the value
9811 initialized with the result of the conversion function. */
9814 maybe_adjust_types_for_deduction (unification_kind_t strict
,
9827 /* Swap PARM and ARG throughout the remainder of this
9828 function; the handling is precisely symmetric since PARM
9829 will initialize ARG rather than vice versa. */
9837 /* There is nothing to do in this case. */
9844 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
9846 /* [temp.deduct.call]
9848 If P is not a reference type:
9850 --If A is an array type, the pointer type produced by the
9851 array-to-pointer standard conversion (_conv.array_) is
9852 used in place of A for type deduction; otherwise,
9854 --If A is a function type, the pointer type produced by
9855 the function-to-pointer standard conversion
9856 (_conv.func_) is used in place of A for type deduction;
9859 --If A is a cv-qualified type, the top level
9860 cv-qualifiers of A's type are ignored for type
9862 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
9863 *arg
= build_pointer_type (TREE_TYPE (*arg
));
9864 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
9865 *arg
= build_pointer_type (*arg
);
9867 *arg
= TYPE_MAIN_VARIANT (*arg
);
9870 /* [temp.deduct.call]
9872 If P is a cv-qualified type, the top level cv-qualifiers
9873 of P's type are ignored for type deduction. If P is a
9874 reference type, the type referred to by P is used for
9876 *parm
= TYPE_MAIN_VARIANT (*parm
);
9877 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
9879 *parm
= TREE_TYPE (*parm
);
9880 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
9883 /* DR 322. For conversion deduction, remove a reference type on parm
9884 too (which has been swapped into ARG). */
9885 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
9886 *arg
= TREE_TYPE (*arg
);
9891 /* Most parms like fn_type_unification.
9893 If SUBR is 1, we're being called recursively (to unify the
9894 arguments of a function or method parameter of a function
9898 type_unification_real (tree tparms
,
9903 unification_kind_t strict
,
9908 int ntparms
= TREE_VEC_LENGTH (tparms
);
9910 int saw_undeduced
= 0;
9913 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
9914 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
9915 gcc_assert (!xargs
|| TREE_CODE (xargs
) == TREE_LIST
);
9916 gcc_assert (ntparms
> 0);
9921 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
9922 | UNIFY_ALLOW_DERIVED
);
9926 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
9930 sub_strict
= UNIFY_ALLOW_NONE
;
9941 while (parms
&& parms
!= void_list_node
9942 && args
&& args
!= void_list_node
)
9944 parm
= TREE_VALUE (parms
);
9945 parms
= TREE_CHAIN (parms
);
9946 arg
= TREE_VALUE (args
);
9947 args
= TREE_CHAIN (args
);
9949 if (arg
== error_mark_node
)
9951 if (arg
== unknown_type_node
)
9952 /* We can't deduce anything from this, but we might get all the
9953 template args from other function args. */
9956 /* Conversions will be performed on a function argument that
9957 corresponds with a function parameter that contains only
9958 non-deducible template parameters and explicitly specified
9959 template parameters. */
9960 if (!uses_template_parms (parm
))
9965 type
= TREE_TYPE (arg
);
9969 if (same_type_p (parm
, type
))
9971 if (strict
!= DEDUCE_EXACT
9972 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
9981 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
9982 if (type_unknown_p (arg
))
9984 /* [temp.deduct.type] A template-argument can be deduced from
9985 a pointer to function or pointer to member function
9986 argument if the set of overloaded functions does not
9987 contain function templates and at most one of a set of
9988 overloaded functions provides a unique match. */
9990 if (resolve_overloaded_unification
9991 (tparms
, targs
, parm
, arg
, strict
, sub_strict
)
9996 arg
= TREE_TYPE (arg
);
9997 if (arg
== error_mark_node
)
10002 int arg_strict
= sub_strict
;
10005 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
10007 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
10012 /* Fail if we've reached the end of the parm list, and more args
10013 are present, and the parm list isn't variadic. */
10014 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
10016 /* Fail if parms are left and they don't have default values. */
10017 if (parms
&& parms
!= void_list_node
10018 && TREE_PURPOSE (parms
) == NULL_TREE
)
10022 for (i
= 0; i
< ntparms
; i
++)
10023 if (!TREE_VEC_ELT (targs
, i
))
10027 if (TREE_VEC_ELT (tparms
, i
) == error_mark_node
)
10030 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
10032 /* If this is an undeduced nontype parameter that depends on
10033 a type parameter, try another pass; its type may have been
10034 deduced from a later argument than the one from which
10035 this parameter can be deduced. */
10036 if (TREE_CODE (tparm
) == PARM_DECL
10037 && uses_template_parms (TREE_TYPE (tparm
))
10038 && !saw_undeduced
++)
10047 /* Subroutine of type_unification_real. Args are like the variables at the
10048 call site. ARG is an overloaded function (or template-id); we try
10049 deducing template args from each of the overloads, and if only one
10050 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
10053 resolve_overloaded_unification (tree tparms
,
10057 unification_kind_t strict
,
10060 tree tempargs
= copy_node (targs
);
10064 if (TREE_CODE (arg
) == ADDR_EXPR
)
10066 arg
= TREE_OPERAND (arg
, 0);
10072 if (TREE_CODE (arg
) == COMPONENT_REF
)
10073 /* Handle `&x' where `x' is some static or non-static member
10075 arg
= TREE_OPERAND (arg
, 1);
10077 if (TREE_CODE (arg
) == OFFSET_REF
)
10078 arg
= TREE_OPERAND (arg
, 1);
10080 /* Strip baselink information. */
10081 if (BASELINK_P (arg
))
10082 arg
= BASELINK_FUNCTIONS (arg
);
10084 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
10086 /* If we got some explicit template args, we need to plug them into
10087 the affected templates before we try to unify, in case the
10088 explicit args will completely resolve the templates in question. */
10090 tree expl_subargs
= TREE_OPERAND (arg
, 1);
10091 arg
= TREE_OPERAND (arg
, 0);
10093 for (; arg
; arg
= OVL_NEXT (arg
))
10095 tree fn
= OVL_CURRENT (arg
);
10096 tree subargs
, elem
;
10098 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
10101 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
10102 expl_subargs
, /*check_ret=*/false);
10105 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
10106 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
10107 elem
, strict
, sub_strict
, addr_p
);
10113 gcc_assert (TREE_CODE (arg
) == OVERLOAD
10114 || TREE_CODE (arg
) == FUNCTION_DECL
);
10116 for (; arg
; arg
= OVL_NEXT (arg
))
10117 good
+= try_one_overload (tparms
, targs
, tempargs
, parm
,
10118 TREE_TYPE (OVL_CURRENT (arg
)),
10119 strict
, sub_strict
, addr_p
);
10122 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10123 to function or pointer to member function argument if the set of
10124 overloaded functions does not contain function templates and at most
10125 one of a set of overloaded functions provides a unique match.
10127 So if we found multiple possibilities, we return success but don't
10128 deduce anything. */
10132 int i
= TREE_VEC_LENGTH (targs
);
10134 if (TREE_VEC_ELT (tempargs
, i
))
10135 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
10143 /* Subroutine of resolve_overloaded_unification; does deduction for a single
10144 overload. Fills TARGS with any deduced arguments, or error_mark_node if
10145 different overloads deduce different arguments for a given parm.
10146 ADDR_P is true if the expression for which deduction is being
10147 performed was of the form "& fn" rather than simply "fn".
10149 Returns 1 on success. */
10152 try_one_overload (tree tparms
,
10157 unification_kind_t strict
,
10165 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10166 to function or pointer to member function argument if the set of
10167 overloaded functions does not contain function templates and at most
10168 one of a set of overloaded functions provides a unique match.
10170 So if this is a template, just return success. */
10172 if (uses_template_parms (arg
))
10175 if (TREE_CODE (arg
) == METHOD_TYPE
)
10176 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
10178 arg
= build_pointer_type (arg
);
10180 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
);
10182 /* We don't copy orig_targs for this because if we have already deduced
10183 some template args from previous args, unify would complain when we
10184 try to deduce a template parameter for the same argument, even though
10185 there isn't really a conflict. */
10186 nargs
= TREE_VEC_LENGTH (targs
);
10187 tempargs
= make_tree_vec (nargs
);
10189 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
10192 /* First make sure we didn't deduce anything that conflicts with
10193 explicitly specified args. */
10194 for (i
= nargs
; i
--; )
10196 tree elt
= TREE_VEC_ELT (tempargs
, i
);
10197 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
10201 else if (uses_template_parms (elt
))
10202 /* Since we're unifying against ourselves, we will fill in
10203 template args used in the function parm list with our own
10204 template parms. Discard them. */
10205 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
10206 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
10210 for (i
= nargs
; i
--; )
10212 tree elt
= TREE_VEC_ELT (tempargs
, i
);
10215 TREE_VEC_ELT (targs
, i
) = elt
;
10221 /* PARM is a template class (perhaps with unbound template
10222 parameters). ARG is a fully instantiated type. If ARG can be
10223 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
10224 TARGS are as for unify. */
10227 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
10229 tree copy_of_targs
;
10231 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
10232 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
10233 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
10236 /* We need to make a new template argument vector for the call to
10237 unify. If we used TARGS, we'd clutter it up with the result of
10238 the attempted unification, even if this class didn't work out.
10239 We also don't want to commit ourselves to all the unifications
10240 we've already done, since unification is supposed to be done on
10241 an argument-by-argument basis. In other words, consider the
10242 following pathological case:
10244 template <int I, int J, int K>
10247 template <int I, int J>
10248 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
10250 template <int I, int J, int K>
10251 void f(S<I, J, K>, S<I, I, I>);
10260 Now, by the time we consider the unification involving `s2', we
10261 already know that we must have `f<0, 0, 0>'. But, even though
10262 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
10263 because there are two ways to unify base classes of S<0, 1, 2>
10264 with S<I, I, I>. If we kept the already deduced knowledge, we
10265 would reject the possibility I=1. */
10266 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
10268 /* If unification failed, we're done. */
10269 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
10270 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
10276 /* Given a template type PARM and a class type ARG, find the unique
10277 base type in ARG that is an instance of PARM. We do not examine
10278 ARG itself; only its base-classes. If there is not exactly one
10279 appropriate base class, return NULL_TREE. PARM may be the type of
10280 a partial specialization, as well as a plain template type. Used
10284 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
10286 tree rval
= NULL_TREE
;
10289 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg
)));
10291 binfo
= TYPE_BINFO (complete_type (arg
));
10293 /* The type could not be completed. */
10296 /* Walk in inheritance graph order. The search order is not
10297 important, and this avoids multiple walks of virtual bases. */
10298 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
10300 tree r
= try_class_unification (tparms
, targs
, parm
, BINFO_TYPE (binfo
));
10304 /* If there is more than one satisfactory baseclass, then:
10308 If they yield more than one possible deduced A, the type
10312 if (rval
&& !same_type_p (r
, rval
))
10322 /* Returns the level of DECL, which declares a template parameter. */
10325 template_decl_level (tree decl
)
10327 switch (TREE_CODE (decl
))
10330 case TEMPLATE_DECL
:
10331 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
10334 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
10337 gcc_unreachable ();
10342 /* Decide whether ARG can be unified with PARM, considering only the
10343 cv-qualifiers of each type, given STRICT as documented for unify.
10344 Returns nonzero iff the unification is OK on that basis. */
10347 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
10349 int arg_quals
= cp_type_quals (arg
);
10350 int parm_quals
= cp_type_quals (parm
);
10352 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
10353 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
10355 /* Although a CVR qualifier is ignored when being applied to a
10356 substituted template parameter ([8.3.2]/1 for example), that
10357 does not apply during deduction [14.8.2.4]/1, (even though
10358 that is not explicitly mentioned, [14.8.2.4]/9 indicates
10359 this). Except when we're allowing additional CV qualifiers
10360 at the outer level [14.8.2.1]/3,1st bullet. */
10361 if ((TREE_CODE (arg
) == REFERENCE_TYPE
10362 || TREE_CODE (arg
) == FUNCTION_TYPE
10363 || TREE_CODE (arg
) == METHOD_TYPE
)
10364 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
10367 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
10368 && (parm_quals
& TYPE_QUAL_RESTRICT
))
10372 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
10373 && (arg_quals
& parm_quals
) != parm_quals
)
10376 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
10377 && (parm_quals
& arg_quals
) != arg_quals
)
10383 /* Deduce the value of template parameters. TPARMS is the (innermost)
10384 set of template parameters to a template. TARGS is the bindings
10385 for those template parameters, as determined thus far; TARGS may
10386 include template arguments for outer levels of template parameters
10387 as well. PARM is a parameter to a template function, or a
10388 subcomponent of that parameter; ARG is the corresponding argument.
10389 This function attempts to match PARM with ARG in a manner
10390 consistent with the existing assignments in TARGS. If more values
10391 are deduced, then TARGS is updated.
10393 Returns 0 if the type deduction succeeds, 1 otherwise. The
10394 parameter STRICT is a bitwise or of the following flags:
10397 Require an exact match between PARM and ARG.
10398 UNIFY_ALLOW_MORE_CV_QUAL:
10399 Allow the deduced ARG to be more cv-qualified (by qualification
10400 conversion) than ARG.
10401 UNIFY_ALLOW_LESS_CV_QUAL:
10402 Allow the deduced ARG to be less cv-qualified than ARG.
10403 UNIFY_ALLOW_DERIVED:
10404 Allow the deduced ARG to be a template base class of ARG,
10405 or a pointer to a template base class of the type pointed to by
10407 UNIFY_ALLOW_INTEGER:
10408 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
10409 case for more information.
10410 UNIFY_ALLOW_OUTER_LEVEL:
10411 This is the outermost level of a deduction. Used to determine validity
10412 of qualification conversions. A valid qualification conversion must
10413 have const qualified pointers leading up to the inner type which
10414 requires additional CV quals, except at the outer level, where const
10415 is not required [conv.qual]. It would be normal to set this flag in
10416 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
10417 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
10418 This is the outermost level of a deduction, and PARM can be more CV
10419 qualified at this point.
10420 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
10421 This is the outermost level of a deduction, and PARM can be less CV
10422 qualified at this point. */
10425 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
10430 int strict_in
= strict
;
10432 /* I don't think this will do the right thing with respect to types.
10433 But the only case I've seen it in so far has been array bounds, where
10434 signedness is the only information lost, and I think that will be
10436 while (TREE_CODE (parm
) == NOP_EXPR
)
10437 parm
= TREE_OPERAND (parm
, 0);
10439 if (arg
== error_mark_node
)
10441 if (arg
== unknown_type_node
)
10442 /* We can't deduce anything from this, but we might get all the
10443 template args from other function args. */
10446 /* If PARM uses template parameters, then we can't bail out here,
10447 even if ARG == PARM, since we won't record unifications for the
10448 template parameters. We might need them if we're trying to
10449 figure out which of two things is more specialized. */
10450 if (arg
== parm
&& !uses_template_parms (parm
))
10453 /* Immediately reject some pairs that won't unify because of
10454 cv-qualification mismatches. */
10455 if (TREE_CODE (arg
) == TREE_CODE (parm
)
10457 /* It is the elements of the array which hold the cv quals of an array
10458 type, and the elements might be template type parms. We'll check
10459 when we recurse. */
10460 && TREE_CODE (arg
) != ARRAY_TYPE
10461 /* We check the cv-qualifiers when unifying with template type
10462 parameters below. We want to allow ARG `const T' to unify with
10463 PARM `T' for example, when computing which of two templates
10464 is more specialized, for example. */
10465 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
10466 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
10469 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
10470 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
10471 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
10472 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
10473 strict
&= ~UNIFY_ALLOW_DERIVED
;
10474 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
10475 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
10477 switch (TREE_CODE (parm
))
10479 case TYPENAME_TYPE
:
10481 case UNBOUND_CLASS_TEMPLATE
:
10482 /* In a type which contains a nested-name-specifier, template
10483 argument values cannot be deduced for template parameters used
10484 within the nested-name-specifier. */
10487 case TEMPLATE_TYPE_PARM
:
10488 case TEMPLATE_TEMPLATE_PARM
:
10489 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10490 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
10492 if (TEMPLATE_TYPE_LEVEL (parm
)
10493 != template_decl_level (tparm
))
10494 /* The PARM is not one we're trying to unify. Just check
10495 to see if it matches ARG. */
10496 return (TREE_CODE (arg
) == TREE_CODE (parm
)
10497 && same_type_p (parm
, arg
)) ? 0 : 1;
10498 idx
= TEMPLATE_TYPE_IDX (parm
);
10499 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
10500 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
10502 /* Check for mixed types and values. */
10503 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
10504 && TREE_CODE (tparm
) != TYPE_DECL
)
10505 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
10506 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
10509 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10511 /* ARG must be constructed from a template class or a template
10512 template parameter. */
10513 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
10514 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
10518 tree parmvec
= TYPE_TI_ARGS (parm
);
10519 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
10521 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg
));
10524 /* The resolution to DR150 makes clear that default
10525 arguments for an N-argument may not be used to bind T
10526 to a template template parameter with fewer than N
10527 parameters. It is not safe to permit the binding of
10528 default arguments as an extension, as that may change
10529 the meaning of a conforming program. Consider:
10531 struct Dense { static const unsigned int dim = 1; };
10533 template <template <typename> class View,
10535 void operator+(float, View<Block> const&);
10537 template <typename Block,
10538 unsigned int Dim = Block::dim>
10539 struct Lvalue_proxy { operator float() const; };
10543 Lvalue_proxy<Dense> p;
10548 Here, if Lvalue_proxy is permitted to bind to View, then
10549 the global operator+ will be used; if they are not, the
10550 Lvalue_proxy will be converted to float. */
10551 if (coerce_template_parms (argtmplvec
, parmvec
,
10552 TYPE_TI_TEMPLATE (parm
),
10554 /*require_all_args=*/true,
10555 /*use_default_args=*/false)
10556 == error_mark_node
)
10559 /* Deduce arguments T, i from TT<T> or TT<i>.
10560 We check each element of PARMVEC and ARGVEC individually
10561 rather than the whole TREE_VEC since they can have
10562 different number of elements. */
10564 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
10566 if (unify (tparms
, targs
,
10567 TREE_VEC_ELT (parmvec
, i
),
10568 TREE_VEC_ELT (argvec
, i
),
10573 arg
= TYPE_TI_TEMPLATE (arg
);
10575 /* Fall through to deduce template name. */
10578 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
10579 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
10581 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10583 /* Simple cases: Value already set, does match or doesn't. */
10584 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
10591 /* If PARM is `const T' and ARG is only `int', we don't have
10592 a match unless we are allowing additional qualification.
10593 If ARG is `const int' and PARM is just `T' that's OK;
10594 that binds `const int' to `T'. */
10595 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
10599 /* Consider the case where ARG is `const volatile int' and
10600 PARM is `const T'. Then, T should be `volatile int'. */
10601 arg
= cp_build_qualified_type_real
10602 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
10603 if (arg
== error_mark_node
)
10606 /* Simple cases: Value already set, does match or doesn't. */
10607 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
10612 /* Make sure that ARG is not a variable-sized array. (Note
10613 that were talking about variable-sized arrays (like
10614 `int[n]'), rather than arrays of unknown size (like
10615 `int[]').) We'll get very confused by such a type since
10616 the bound of the array will not be computable in an
10617 instantiation. Besides, such types are not allowed in
10618 ISO C++, so we can do as we please here. */
10619 if (variably_modified_type_p (arg
, NULL_TREE
))
10623 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
10626 case TEMPLATE_PARM_INDEX
:
10627 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
10628 if (tparm
== error_mark_node
)
10631 if (TEMPLATE_PARM_LEVEL (parm
)
10632 != template_decl_level (tparm
))
10633 /* The PARM is not one we're trying to unify. Just check
10634 to see if it matches ARG. */
10635 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
10636 && cp_tree_equal (parm
, arg
));
10638 idx
= TEMPLATE_PARM_IDX (parm
);
10639 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
10642 return !cp_tree_equal (targ
, arg
);
10644 /* [temp.deduct.type] If, in the declaration of a function template
10645 with a non-type template-parameter, the non-type
10646 template-parameter is used in an expression in the function
10647 parameter-list and, if the corresponding template-argument is
10648 deduced, the template-argument type shall match the type of the
10649 template-parameter exactly, except that a template-argument
10650 deduced from an array bound may be of any integral type.
10651 The non-type parameter might use already deduced type parameters. */
10652 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
10653 if (!TREE_TYPE (arg
))
10654 /* Template-parameter dependent expression. Just accept it for now.
10655 It will later be processed in convert_template_argument. */
10657 else if (same_type_p (TREE_TYPE (arg
), tparm
))
10659 else if ((strict
& UNIFY_ALLOW_INTEGER
)
10660 && (TREE_CODE (tparm
) == INTEGER_TYPE
10661 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
10662 /* Convert the ARG to the type of PARM; the deduced non-type
10663 template argument must exactly match the types of the
10664 corresponding parameter. */
10665 arg
= fold (build_nop (TREE_TYPE (parm
), arg
));
10666 else if (uses_template_parms (tparm
))
10667 /* We haven't deduced the type of this parameter yet. Try again
10673 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
10678 /* A pointer-to-member constant can be unified only with
10679 another constant. */
10680 if (TREE_CODE (arg
) != PTRMEM_CST
)
10683 /* Just unify the class member. It would be useless (and possibly
10684 wrong, depending on the strict flags) to unify also
10685 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10686 arg refer to the same variable, even if through different
10687 classes. For instance:
10689 struct A { int x; };
10692 Unification of &A::x and &B::x must succeed. */
10693 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
10694 PTRMEM_CST_MEMBER (arg
), strict
);
10699 if (TREE_CODE (arg
) != POINTER_TYPE
)
10702 /* [temp.deduct.call]
10704 A can be another pointer or pointer to member type that can
10705 be converted to the deduced A via a qualification
10706 conversion (_conv.qual_).
10708 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10709 This will allow for additional cv-qualification of the
10710 pointed-to types if appropriate. */
10712 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
10713 /* The derived-to-base conversion only persists through one
10714 level of pointers. */
10715 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
10717 return unify (tparms
, targs
, TREE_TYPE (parm
),
10718 TREE_TYPE (arg
), strict
);
10721 case REFERENCE_TYPE
:
10722 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
10724 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
10725 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
10728 if (TREE_CODE (arg
) != ARRAY_TYPE
)
10730 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
10731 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
10733 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
10740 /* Our representation of array types uses "N - 1" as the
10741 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10742 not an integer constant. We cannot unify arbitrarily
10743 complex expressions, so we eliminate the MINUS_EXPRs
10745 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
10746 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
10749 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
10750 parm_max
= TREE_OPERAND (parm_max
, 0);
10752 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
10753 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
10756 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
10757 trying to unify the type of a variable with the type
10758 of a template parameter. For example:
10760 template <unsigned int N>
10761 void f (char (&) [N]);
10768 Here, the type of the ARG will be "int [g(i)]", and
10769 may be a SAVE_EXPR, etc. */
10770 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
10772 arg_max
= TREE_OPERAND (arg_max
, 0);
10775 /* If only one of the bounds used a MINUS_EXPR, compensate
10776 by adding one to the other bound. */
10777 if (parm_cst
&& !arg_cst
)
10778 parm_max
= fold_build2 (PLUS_EXPR
,
10782 else if (arg_cst
&& !parm_cst
)
10783 arg_max
= fold_build2 (PLUS_EXPR
,
10788 if (unify (tparms
, targs
, parm_max
, arg_max
, UNIFY_ALLOW_INTEGER
))
10791 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
10792 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
10799 case ENUMERAL_TYPE
:
10801 if (TREE_CODE (arg
) != TREE_CODE (parm
))
10804 /* We have already checked cv-qualification at the top of the
10806 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
10809 /* As far as unification is concerned, this wins. Later checks
10810 will invalidate it if necessary. */
10813 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
10814 /* Type INTEGER_CST can come from ordinary constant template args. */
10816 while (TREE_CODE (arg
) == NOP_EXPR
)
10817 arg
= TREE_OPERAND (arg
, 0);
10819 if (TREE_CODE (arg
) != INTEGER_CST
)
10821 return !tree_int_cst_equal (parm
, arg
);
10826 if (TREE_CODE (arg
) != TREE_VEC
)
10828 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
10830 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
10831 if (unify (tparms
, targs
,
10832 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
10840 if (TREE_CODE (arg
) != TREE_CODE (parm
))
10843 if (TYPE_PTRMEMFUNC_P (parm
))
10845 if (!TYPE_PTRMEMFUNC_P (arg
))
10848 return unify (tparms
, targs
,
10849 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
10850 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
10854 if (CLASSTYPE_TEMPLATE_INFO (parm
))
10856 tree t
= NULL_TREE
;
10858 if (strict_in
& UNIFY_ALLOW_DERIVED
)
10860 /* First, we try to unify the PARM and ARG directly. */
10861 t
= try_class_unification (tparms
, targs
,
10866 /* Fallback to the special case allowed in
10867 [temp.deduct.call]:
10869 If P is a class, and P has the form
10870 template-id, then A can be a derived class of
10871 the deduced A. Likewise, if P is a pointer to
10872 a class of the form template-id, A can be a
10873 pointer to a derived class pointed to by the
10875 t
= get_template_base (tparms
, targs
, parm
, arg
);
10881 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
10882 && (CLASSTYPE_TI_TEMPLATE (parm
)
10883 == CLASSTYPE_TI_TEMPLATE (arg
)))
10884 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10885 Then, we should unify `int' and `U'. */
10888 /* There's no chance of unification succeeding. */
10891 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
10892 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
10894 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
10899 case FUNCTION_TYPE
:
10900 if (TREE_CODE (arg
) != TREE_CODE (parm
))
10903 /* CV qualifications for methods can never be deduced, they must
10904 match exactly. We need to check them explicitly here,
10905 because type_unification_real treats them as any other
10906 cvqualified parameter. */
10907 if (TREE_CODE (parm
) == METHOD_TYPE
10908 && (!check_cv_quals_for_unify
10910 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg
))),
10911 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm
))))))
10914 if (unify (tparms
, targs
, TREE_TYPE (parm
),
10915 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
10917 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
10918 TYPE_ARG_TYPES (arg
), 1, DEDUCE_EXACT
,
10922 /* Unify a pointer to member with a pointer to member function, which
10923 deduces the type of the member as a function type. */
10924 if (TYPE_PTRMEMFUNC_P (arg
))
10928 cp_cv_quals cv_quals
;
10930 /* Check top-level cv qualifiers */
10931 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
10934 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
10935 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
), UNIFY_ALLOW_NONE
))
10938 /* Determine the type of the function we are unifying against. */
10939 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
10941 build_function_type (TREE_TYPE (method_type
),
10942 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
10944 /* Extract the cv-qualifiers of the member function from the
10945 implicit object parameter and place them on the function
10946 type to be restored later. */
10948 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type
))));
10949 fntype
= build_qualified_type (fntype
, cv_quals
);
10950 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
);
10953 if (TREE_CODE (arg
) != OFFSET_TYPE
)
10955 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
10956 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
10958 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
10962 if (DECL_TEMPLATE_PARM_P (parm
))
10963 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
10964 if (arg
!= integral_constant_value (parm
))
10969 case TEMPLATE_DECL
:
10970 /* Matched cases are handled by the ARG == PARM test above. */
10974 gcc_assert (EXPR_P (parm
));
10976 /* We must be looking at an expression. This can happen with
10980 void foo(S<I>, S<I + 2>);
10982 This is a "nondeduced context":
10986 The nondeduced contexts are:
10988 --A type that is a template-id in which one or more of
10989 the template-arguments is an expression that references
10990 a template-parameter.
10992 In these cases, we assume deduction succeeded, but don't
10993 actually infer any unifications. */
10995 if (!uses_template_parms (parm
)
10996 && !template_args_equal (parm
, arg
))
11003 /* Note that DECL can be defined in this translation unit, if
11007 mark_definable (tree decl
)
11010 DECL_NOT_REALLY_EXTERN (decl
) = 1;
11011 FOR_EACH_CLONE (clone
, decl
)
11012 DECL_NOT_REALLY_EXTERN (clone
) = 1;
11015 /* Called if RESULT is explicitly instantiated, or is a member of an
11016 explicitly instantiated class. */
11019 mark_decl_instantiated (tree result
, int extern_p
)
11021 SET_DECL_EXPLICIT_INSTANTIATION (result
);
11023 /* If this entity has already been written out, it's too late to
11024 make any modifications. */
11025 if (TREE_ASM_WRITTEN (result
))
11028 if (TREE_CODE (result
) != FUNCTION_DECL
)
11029 /* The TREE_PUBLIC flag for function declarations will have been
11030 set correctly by tsubst. */
11031 TREE_PUBLIC (result
) = 1;
11033 /* This might have been set by an earlier implicit instantiation. */
11034 DECL_COMDAT (result
) = 0;
11037 DECL_NOT_REALLY_EXTERN (result
) = 0;
11040 mark_definable (result
);
11041 /* Always make artificials weak. */
11042 if (DECL_ARTIFICIAL (result
) && flag_weak
)
11043 comdat_linkage (result
);
11044 /* For WIN32 we also want to put explicit instantiations in
11045 linkonce sections. */
11046 else if (TREE_PUBLIC (result
))
11047 maybe_make_one_only (result
);
11050 /* If EXTERN_P, then this function will not be emitted -- unless
11051 followed by an explicit instantiation, at which point its linkage
11052 will be adjusted. If !EXTERN_P, then this function will be
11053 emitted here. In neither circumstance do we want
11054 import_export_decl to adjust the linkage. */
11055 DECL_INTERFACE_KNOWN (result
) = 1;
11058 /* Given two function templates PAT1 and PAT2, return:
11060 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
11061 -1 if PAT2 is more specialized than PAT1.
11062 0 if neither is more specialized.
11064 LEN indicates the number of parameters we should consider
11065 (defaulted parameters should not be considered).
11067 The 1998 std underspecified function template partial ordering, and
11068 DR214 addresses the issue. We take pairs of arguments, one from
11069 each of the templates, and deduce them against each other. One of
11070 the templates will be more specialized if all the *other*
11071 template's arguments deduce against its arguments and at least one
11072 of its arguments *does* *not* deduce against the other template's
11073 corresponding argument. Deduction is done as for class templates.
11074 The arguments used in deduction have reference and top level cv
11075 qualifiers removed. Iff both arguments were originally reference
11076 types *and* deduction succeeds in both directions, the template
11077 with the more cv-qualified argument wins for that pairing (if
11078 neither is more cv-qualified, they both are equal). Unlike regular
11079 deduction, after all the arguments have been deduced in this way,
11080 we do *not* verify the deduced template argument values can be
11081 substituted into non-deduced contexts, nor do we have to verify
11082 that all template arguments have been deduced. */
11085 more_specialized_fn (tree pat1
, tree pat2
, int len
)
11087 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
11088 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
11089 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
11090 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
11091 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
11092 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
11093 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
11094 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
11098 /* Remove the this parameter from non-static member functions. If
11099 one is a non-static member function and the other is not a static
11100 member function, remove the first parameter from that function
11101 also. This situation occurs for operator functions where we
11102 locate both a member function (with this pointer) and non-member
11103 operator (with explicit first operand). */
11104 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
11106 len
--; /* LEN is the number of significant arguments for DECL1 */
11107 args1
= TREE_CHAIN (args1
);
11108 if (!DECL_STATIC_FUNCTION_P (decl2
))
11109 args2
= TREE_CHAIN (args2
);
11111 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
11113 args2
= TREE_CHAIN (args2
);
11114 if (!DECL_STATIC_FUNCTION_P (decl1
))
11117 args1
= TREE_CHAIN (args1
);
11121 /* If only one is a conversion operator, they are unordered. */
11122 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
11125 /* Consider the return type for a conversion function */
11126 if (DECL_CONV_FN_P (decl1
))
11128 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
11129 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
11133 processing_template_decl
++;
11137 tree arg1
= TREE_VALUE (args1
);
11138 tree arg2
= TREE_VALUE (args2
);
11139 int deduce1
, deduce2
;
11143 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
11145 arg1
= TREE_TYPE (arg1
);
11146 quals1
= cp_type_quals (arg1
);
11149 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
11151 arg2
= TREE_TYPE (arg2
);
11152 quals2
= cp_type_quals (arg2
);
11155 if ((quals1
< 0) != (quals2
< 0))
11157 /* Only of the args is a reference, see if we should apply
11158 array/function pointer decay to it. This is not part of
11159 DR214, but is, IMHO, consistent with the deduction rules
11160 for the function call itself, and with our earlier
11161 implementation of the underspecified partial ordering
11162 rules. (nathan). */
11165 switch (TREE_CODE (arg1
))
11168 arg1
= TREE_TYPE (arg1
);
11170 case FUNCTION_TYPE
:
11171 arg1
= build_pointer_type (arg1
);
11180 switch (TREE_CODE (arg2
))
11183 arg2
= TREE_TYPE (arg2
);
11185 case FUNCTION_TYPE
:
11186 arg2
= build_pointer_type (arg2
);
11195 arg1
= TYPE_MAIN_VARIANT (arg1
);
11196 arg2
= TYPE_MAIN_VARIANT (arg2
);
11198 deduce1
= !unify (tparms1
, targs1
, arg1
, arg2
, UNIFY_ALLOW_NONE
);
11199 deduce2
= !unify (tparms2
, targs2
, arg2
, arg1
, UNIFY_ALLOW_NONE
);
11205 if (better1
< 0 && better2
< 0)
11206 /* We've failed to deduce something in either direction.
11207 These must be unordered. */
11210 if (deduce1
&& deduce2
&& quals1
>= 0 && quals2
>= 0)
11212 /* Deduces in both directions, see if quals can
11213 disambiguate. Pretend the worse one failed to deduce. */
11214 if ((quals1
& quals2
) == quals2
)
11216 if ((quals1
& quals2
) == quals1
)
11219 if (deduce1
&& !deduce2
&& !better2
)
11221 if (deduce2
&& !deduce1
&& !better1
)
11224 args1
= TREE_CHAIN (args1
);
11225 args2
= TREE_CHAIN (args2
);
11228 processing_template_decl
--;
11230 return (better1
> 0) - (better2
> 0);
11233 /* Determine which of two partial specializations is more specialized.
11235 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
11236 to the first partial specialization. The TREE_VALUE is the
11237 innermost set of template parameters for the partial
11238 specialization. PAT2 is similar, but for the second template.
11240 Return 1 if the first partial specialization is more specialized;
11241 -1 if the second is more specialized; 0 if neither is more
11244 See [temp.class.order] for information about determining which of
11245 two templates is more specialized. */
11248 more_specialized_class (tree pat1
, tree pat2
)
11254 tmpl1
= TREE_TYPE (pat1
);
11255 tmpl2
= TREE_TYPE (pat2
);
11257 /* Just like what happens for functions, if we are ordering between
11258 different class template specializations, we may encounter dependent
11259 types in the arguments, and we need our dependency check functions
11260 to behave correctly. */
11261 ++processing_template_decl
;
11262 targs
= get_class_bindings (TREE_VALUE (pat1
),
11263 CLASSTYPE_TI_ARGS (tmpl1
),
11264 CLASSTYPE_TI_ARGS (tmpl2
));
11268 targs
= get_class_bindings (TREE_VALUE (pat2
),
11269 CLASSTYPE_TI_ARGS (tmpl2
),
11270 CLASSTYPE_TI_ARGS (tmpl1
));
11273 --processing_template_decl
;
11278 /* Return the template arguments that will produce the function signature
11279 DECL from the function template FN, with the explicit template
11280 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
11281 also match. Return NULL_TREE if no satisfactory arguments could be
11285 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
11287 int ntparms
= DECL_NTPARMS (fn
);
11288 tree targs
= make_tree_vec (ntparms
);
11290 tree decl_arg_types
;
11292 /* Substitute the explicit template arguments into the type of DECL.
11293 The call to fn_type_unification will handle substitution into the
11295 decl_type
= TREE_TYPE (decl
);
11296 if (explicit_args
&& uses_template_parms (decl_type
))
11299 tree converted_args
;
11301 if (DECL_TEMPLATE_INFO (decl
))
11302 tmpl
= DECL_TI_TEMPLATE (decl
);
11304 /* We can get here for some invalid specializations. */
11308 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
11309 explicit_args
, NULL_TREE
,
11311 /*require_all_args=*/false,
11312 /*use_default_args=*/false);
11313 if (converted_args
== error_mark_node
)
11316 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
11317 if (decl_type
== error_mark_node
)
11321 decl_arg_types
= TYPE_ARG_TYPES (decl_type
);
11322 /* Never do unification on the 'this' parameter. */
11323 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
11324 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
11326 if (fn_type_unification (fn
, explicit_args
, targs
,
11328 (check_rettype
|| DECL_CONV_FN_P (fn
)
11329 ? TREE_TYPE (decl_type
) : NULL_TREE
),
11330 DEDUCE_EXACT
, LOOKUP_NORMAL
))
11336 /* Return the innermost template arguments that, when applied to a
11337 template specialization whose innermost template parameters are
11338 TPARMS, and whose specialization arguments are PARMS, yield the
11341 For example, suppose we have:
11343 template <class T, class U> struct S {};
11344 template <class T> struct S<T*, int> {};
11346 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
11347 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
11348 int}. The resulting vector will be {double}, indicating that `T'
11349 is bound to `double'. */
11352 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
11354 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
11356 tree innermost_deduced_args
;
11358 innermost_deduced_args
= make_tree_vec (ntparms
);
11359 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
11361 deduced_args
= copy_node (args
);
11362 SET_TMPL_ARGS_LEVEL (deduced_args
,
11363 TMPL_ARGS_DEPTH (deduced_args
),
11364 innermost_deduced_args
);
11367 deduced_args
= innermost_deduced_args
;
11369 if (unify (tparms
, deduced_args
,
11370 INNERMOST_TEMPLATE_ARGS (spec_args
),
11371 INNERMOST_TEMPLATE_ARGS (args
),
11375 for (i
= 0; i
< ntparms
; ++i
)
11376 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
11379 /* Verify that nondeduced template arguments agree with the type
11380 obtained from argument deduction.
11384 struct A { typedef int X; };
11385 template <class T, class U> struct C {};
11386 template <class T> struct C<T, typename T::X> {};
11388 Then with the instantiation `C<A, int>', we can deduce that
11389 `T' is `A' but unify () does not check whether `typename T::X'
11391 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
11392 if (spec_args
== error_mark_node
11393 /* We only need to check the innermost arguments; the other
11394 arguments will always agree. */
11395 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
11396 INNERMOST_TEMPLATE_ARGS (args
)))
11399 return deduced_args
;
11402 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
11403 Return the TREE_LIST node with the most specialized template, if
11404 any. If there is no most specialized template, the error_mark_node
11407 Note that this function does not look at, or modify, the
11408 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
11409 returned is one of the elements of INSTANTIATIONS, callers may
11410 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
11411 and retrieve it from the value returned. */
11414 most_specialized_instantiation (tree templates
)
11418 ++processing_template_decl
;
11421 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
11425 if (get_bindings (TREE_VALUE (champ
),
11426 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
11427 NULL_TREE
, /*check_ret=*/false))
11430 if (get_bindings (TREE_VALUE (fn
),
11431 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
11432 NULL_TREE
, /*check_ret=*/false))
11439 /* Equally specialized, move to next function. If there
11440 is no next function, nothing's most specialized. */
11441 fn
= TREE_CHAIN (fn
);
11449 /* Now verify that champ is better than everything earlier in the
11450 instantiation list. */
11451 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
11452 if (get_bindings (TREE_VALUE (champ
),
11453 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
11454 NULL_TREE
, /*check_ret=*/false)
11455 || !get_bindings (TREE_VALUE (fn
),
11456 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
11457 NULL_TREE
, /*check_ret=*/false))
11463 processing_template_decl
--;
11466 return error_mark_node
;
11471 /* If DECL is a specialization of some template, return the most
11472 general such template. Otherwise, returns NULL_TREE.
11474 For example, given:
11476 template <class T> struct S { template <class U> void f(U); };
11478 if TMPL is `template <class U> void S<int>::f(U)' this will return
11479 the full template. This function will not trace past partial
11480 specializations, however. For example, given in addition:
11482 template <class T> struct S<T*> { template <class U> void f(U); };
11484 if TMPL is `template <class U> void S<int*>::f(U)' this will return
11485 `template <class T> template <class U> S<T*>::f(U)'. */
11488 most_general_template (tree decl
)
11490 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
11491 an immediate specialization. */
11492 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11494 if (DECL_TEMPLATE_INFO (decl
)) {
11495 decl
= DECL_TI_TEMPLATE (decl
);
11497 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
11498 template friend. */
11499 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
11505 /* Look for more and more general templates. */
11506 while (DECL_TEMPLATE_INFO (decl
))
11508 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11509 (See cp-tree.h for details.) */
11510 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
11513 if (CLASS_TYPE_P (TREE_TYPE (decl
))
11514 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
11517 /* Stop if we run into an explicitly specialized class template. */
11518 if (!DECL_NAMESPACE_SCOPE_P (decl
)
11519 && DECL_CONTEXT (decl
)
11520 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
11523 decl
= DECL_TI_TEMPLATE (decl
);
11529 /* Return the most specialized of the class template partial
11530 specializations of TMPL which can produce TYPE, a specialization of
11531 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
11532 a _TYPE node corresponding to the partial specialization, while the
11533 TREE_PURPOSE is the set of template arguments that must be
11534 substituted into the TREE_TYPE in order to generate TYPE.
11536 If the choice of partial specialization is ambiguous, a diagnostic
11537 is issued, and the error_mark_node is returned. If there are no
11538 partial specializations of TMPL matching TYPE, then NULL_TREE is
11542 most_specialized_class (tree type
, tree tmpl
)
11544 tree list
= NULL_TREE
;
11551 tmpl
= most_general_template (tmpl
);
11552 args
= CLASSTYPE_TI_ARGS (type
);
11553 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
11555 tree partial_spec_args
;
11558 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
11559 spec_args
= get_class_bindings (TREE_VALUE (t
),
11564 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
11565 TREE_TYPE (list
) = TREE_TYPE (t
);
11572 ambiguous_p
= false;
11575 t
= TREE_CHAIN (t
);
11576 for (; t
; t
= TREE_CHAIN (t
))
11578 fate
= more_specialized_class (champ
, t
);
11585 t
= TREE_CHAIN (t
);
11588 ambiguous_p
= true;
11597 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
11599 fate
= more_specialized_class (champ
, t
);
11602 ambiguous_p
= true;
11609 const char *str
= "candidates are:";
11610 error ("ambiguous class template instantiation for %q#T", type
);
11611 for (t
= list
; t
; t
= TREE_CHAIN (t
))
11613 error ("%s %+#T", str
, TREE_TYPE (t
));
11616 return error_mark_node
;
11622 /* Explicitly instantiate DECL. */
11625 do_decl_instantiation (tree decl
, tree storage
)
11627 tree result
= NULL_TREE
;
11630 if (!decl
|| decl
== error_mark_node
)
11631 /* An error occurred, for which grokdeclarator has already issued
11632 an appropriate message. */
11634 else if (! DECL_LANG_SPECIFIC (decl
))
11636 error ("explicit instantiation of non-template %q#D", decl
);
11639 else if (TREE_CODE (decl
) == VAR_DECL
)
11641 /* There is an asymmetry here in the way VAR_DECLs and
11642 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11643 the latter, the DECL we get back will be marked as a
11644 template instantiation, and the appropriate
11645 DECL_TEMPLATE_INFO will be set up. This does not happen for
11646 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11647 should handle VAR_DECLs as it currently handles
11649 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
11650 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
11652 error ("no matching template for %qD found", decl
);
11656 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
11658 error ("explicit instantiation of %q#D", decl
);
11664 /* Check for various error cases. Note that if the explicit
11665 instantiation is valid the RESULT will currently be marked as an
11666 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11667 until we get here. */
11669 if (DECL_TEMPLATE_SPECIALIZATION (result
))
11671 /* DR 259 [temp.spec].
11673 Both an explicit instantiation and a declaration of an explicit
11674 specialization shall not appear in a program unless the explicit
11675 instantiation follows a declaration of the explicit specialization.
11677 For a given set of template parameters, if an explicit
11678 instantiation of a template appears after a declaration of an
11679 explicit specialization for that template, the explicit
11680 instantiation has no effect. */
11683 else if (DECL_EXPLICIT_INSTANTIATION (result
))
11687 No program shall explicitly instantiate any template more
11690 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11691 the first instantiation was `extern' and the second is not,
11692 and EXTERN_P for the opposite case. */
11693 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
11694 pedwarn ("duplicate explicit instantiation of %q#D", result
);
11695 /* If an "extern" explicit instantiation follows an ordinary
11696 explicit instantiation, the template is instantiated. */
11700 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
11702 error ("no matching template for %qD found", result
);
11705 else if (!DECL_TEMPLATE_INFO (result
))
11707 pedwarn ("explicit instantiation of non-template %q#D", result
);
11711 if (storage
== NULL_TREE
)
11713 else if (storage
== ridpointers
[(int) RID_EXTERN
])
11715 if (pedantic
&& !in_system_header
)
11716 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
11721 error ("storage class %qD applied to template instantiation", storage
);
11723 check_explicit_instantiation_namespace (result
);
11724 mark_decl_instantiated (result
, extern_p
);
11726 instantiate_decl (result
, /*defer_ok=*/1,
11727 /*expl_inst_class_mem_p=*/false);
11731 mark_class_instantiated (tree t
, int extern_p
)
11733 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
11734 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
11735 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
11736 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
11739 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
11740 rest_of_type_compilation (t
, 1);
11744 /* Called from do_type_instantiation through binding_table_foreach to
11745 do recursive instantiation for the type bound in ENTRY. */
11747 bt_instantiate_type_proc (binding_entry entry
, void *data
)
11749 tree storage
= *(tree
*) data
;
11751 if (IS_AGGR_TYPE (entry
->type
)
11752 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
11753 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
11756 /* Called from do_type_instantiation to instantiate a member
11757 (a member function or a static member variable) of an
11758 explicitly instantiated class template. */
11760 instantiate_class_member (tree decl
, int extern_p
)
11762 mark_decl_instantiated (decl
, extern_p
);
11764 instantiate_decl (decl
, /*defer_ok=*/1,
11765 /*expl_inst_class_mem_p=*/true);
11768 /* Perform an explicit instantiation of template class T. STORAGE, if
11769 non-null, is the RID for extern, inline or static. COMPLAIN is
11770 nonzero if this is called from the parser, zero if called recursively,
11771 since the standard is unclear (as detailed below). */
11774 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
11779 int previous_instantiation_extern_p
= 0;
11781 if (TREE_CODE (t
) == TYPE_DECL
)
11784 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
11786 error ("explicit instantiation of non-template type %qT", t
);
11792 if (!COMPLETE_TYPE_P (t
))
11794 if (complain
& tf_error
)
11795 error ("explicit instantiation of %q#T before definition of template",
11800 if (storage
!= NULL_TREE
)
11802 if (pedantic
&& !in_system_header
)
11803 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
11806 if (storage
== ridpointers
[(int) RID_INLINE
])
11808 else if (storage
== ridpointers
[(int) RID_EXTERN
])
11810 else if (storage
== ridpointers
[(int) RID_STATIC
])
11814 error ("storage class %qD applied to template instantiation",
11820 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
11822 /* DR 259 [temp.spec].
11824 Both an explicit instantiation and a declaration of an explicit
11825 specialization shall not appear in a program unless the explicit
11826 instantiation follows a declaration of the explicit specialization.
11828 For a given set of template parameters, if an explicit
11829 instantiation of a template appears after a declaration of an
11830 explicit specialization for that template, the explicit
11831 instantiation has no effect. */
11834 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
11838 No program shall explicitly instantiate any template more
11841 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
11842 instantiation was `extern'. If EXTERN_P then the second is.
11843 These cases are OK. */
11844 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
11846 if (!previous_instantiation_extern_p
&& !extern_p
11847 && (complain
& tf_error
))
11848 pedwarn ("duplicate explicit instantiation of %q#T", t
);
11850 /* If we've already instantiated the template, just return now. */
11851 if (!CLASSTYPE_INTERFACE_ONLY (t
))
11855 check_explicit_instantiation_namespace (TYPE_NAME (t
));
11856 mark_class_instantiated (t
, extern_p
);
11864 /* In contrast to implicit instantiation, where only the
11865 declarations, and not the definitions, of members are
11866 instantiated, we have here:
11870 The explicit instantiation of a class template specialization
11871 implies the instantiation of all of its members not
11872 previously explicitly specialized in the translation unit
11873 containing the explicit instantiation.
11875 Of course, we can't instantiate member template classes, since
11876 we don't have any arguments for them. Note that the standard
11877 is unclear on whether the instantiation of the members are
11878 *explicit* instantiations or not. However, the most natural
11879 interpretation is that it should be an explicit instantiation. */
11882 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
11883 if (TREE_CODE (tmp
) == FUNCTION_DECL
11884 && DECL_TEMPLATE_INSTANTIATION (tmp
))
11885 instantiate_class_member (tmp
, extern_p
);
11887 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
11888 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
11889 instantiate_class_member (tmp
, extern_p
);
11891 if (CLASSTYPE_NESTED_UTDS (t
))
11892 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
11893 bt_instantiate_type_proc
, &storage
);
11897 /* Given a function DECL, which is a specialization of TMPL, modify
11898 DECL to be a re-instantiation of TMPL with the same template
11899 arguments. TMPL should be the template into which tsubst'ing
11900 should occur for DECL, not the most general template.
11902 One reason for doing this is a scenario like this:
11905 void f(const T&, int i);
11907 void g() { f(3, 7); }
11910 void f(const T& t, const int i) { }
11912 Note that when the template is first instantiated, with
11913 instantiate_template, the resulting DECL will have no name for the
11914 first parameter, and the wrong type for the second. So, when we go
11915 to instantiate the DECL, we regenerate it. */
11918 regenerate_decl_from_template (tree decl
, tree tmpl
)
11920 /* The arguments used to instantiate DECL, from the most general
11925 args
= DECL_TI_ARGS (decl
);
11926 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
11928 /* Make sure that we can see identifiers, and compute access
11930 push_access_scope (decl
);
11932 if (TREE_CODE (decl
) == FUNCTION_DECL
)
11940 args_depth
= TMPL_ARGS_DEPTH (args
);
11941 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
11942 if (args_depth
> parms_depth
)
11943 args
= get_innermost_template_args (args
, parms_depth
);
11945 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
11946 args
, tf_error
, NULL_TREE
);
11948 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
11951 /* Merge parameter declarations. */
11952 decl_parm
= skip_artificial_parms_for (decl
,
11953 DECL_ARGUMENTS (decl
));
11955 = skip_artificial_parms_for (code_pattern
,
11956 DECL_ARGUMENTS (code_pattern
));
11962 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
11963 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
11964 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
11966 parm_type
= type_decays_to (parm_type
);
11967 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
11968 TREE_TYPE (decl_parm
) = parm_type
;
11969 attributes
= DECL_ATTRIBUTES (pattern_parm
);
11970 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
11972 DECL_ATTRIBUTES (decl_parm
) = attributes
;
11973 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
11975 decl_parm
= TREE_CHAIN (decl_parm
);
11976 pattern_parm
= TREE_CHAIN (pattern_parm
);
11979 /* Merge additional specifiers from the CODE_PATTERN. */
11980 if (DECL_DECLARED_INLINE_P (code_pattern
)
11981 && !DECL_DECLARED_INLINE_P (decl
))
11982 DECL_DECLARED_INLINE_P (decl
) = 1;
11983 if (DECL_INLINE (code_pattern
) && !DECL_INLINE (decl
))
11984 DECL_INLINE (decl
) = 1;
11986 else if (TREE_CODE (decl
) == VAR_DECL
)
11987 DECL_INITIAL (decl
) =
11988 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
11989 tf_error
, DECL_TI_TEMPLATE (decl
),
11990 /*integral_constant_expression_p=*/false);
11992 gcc_unreachable ();
11994 pop_access_scope (decl
);
11997 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
11998 substituted to get DECL. */
12001 template_for_substitution (tree decl
)
12003 tree tmpl
= DECL_TI_TEMPLATE (decl
);
12005 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
12006 for the instantiation. This is not always the most general
12007 template. Consider, for example:
12010 struct S { template <class U> void f();
12011 template <> void f<int>(); };
12013 and an instantiation of S<double>::f<int>. We want TD to be the
12014 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
12015 while (/* An instantiation cannot have a definition, so we need a
12016 more general template. */
12017 DECL_TEMPLATE_INSTANTIATION (tmpl
)
12018 /* We must also deal with friend templates. Given:
12020 template <class T> struct S {
12021 template <class U> friend void f() {};
12024 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
12025 so far as the language is concerned, but that's still
12026 where we get the pattern for the instantiation from. On
12027 other hand, if the definition comes outside the class, say:
12029 template <class T> struct S {
12030 template <class U> friend void f();
12032 template <class U> friend void f() {}
12034 we don't need to look any further. That's what the check for
12035 DECL_INITIAL is for. */
12036 || (TREE_CODE (decl
) == FUNCTION_DECL
12037 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
12038 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
12040 /* The present template, TD, should not be a definition. If it
12041 were a definition, we should be using it! Note that we
12042 cannot restructure the loop to just keep going until we find
12043 a template with a definition, since that might go too far if
12044 a specialization was declared, but not defined. */
12045 gcc_assert (TREE_CODE (decl
) != VAR_DECL
12046 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
12048 /* Fetch the more general template. */
12049 tmpl
= DECL_TI_TEMPLATE (tmpl
);
12055 /* Produce the definition of D, a _DECL generated from a template. If
12056 DEFER_OK is nonzero, then we don't have to actually do the
12057 instantiation now; we just have to do it sometime. Normally it is
12058 an error if this is an explicit instantiation but D is undefined.
12059 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
12060 explicitly instantiated class template. */
12063 instantiate_decl (tree d
, int defer_ok
,
12064 bool expl_inst_class_mem_p
)
12066 tree tmpl
= DECL_TI_TEMPLATE (d
);
12073 bool pattern_defined
;
12075 location_t saved_loc
= input_location
;
12078 /* This function should only be used to instantiate templates for
12079 functions and static member variables. */
12080 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
12081 || TREE_CODE (d
) == VAR_DECL
);
12083 /* Variables are never deferred; if instantiation is required, they
12084 are instantiated right away. That allows for better code in the
12085 case that an expression refers to the value of the variable --
12086 if the variable has a constant value the referring expression can
12087 take advantage of that fact. */
12088 if (TREE_CODE (d
) == VAR_DECL
)
12091 /* Don't instantiate cloned functions. Instead, instantiate the
12092 functions they cloned. */
12093 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
12094 d
= DECL_CLONED_FUNCTION (d
);
12096 if (DECL_TEMPLATE_INSTANTIATED (d
))
12097 /* D has already been instantiated. It might seem reasonable to
12098 check whether or not D is an explicit instantiation, and, if so,
12099 stop here. But when an explicit instantiation is deferred
12100 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
12101 is set, even though we still need to do the instantiation. */
12104 /* If we already have a specialization of this declaration, then
12105 there's no reason to instantiate it. Note that
12106 retrieve_specialization gives us both instantiations and
12107 specializations, so we must explicitly check
12108 DECL_TEMPLATE_SPECIALIZATION. */
12109 gen_tmpl
= most_general_template (tmpl
);
12110 gen_args
= DECL_TI_ARGS (d
);
12111 spec
= retrieve_specialization (gen_tmpl
, gen_args
,
12112 /*class_specializations_p=*/false);
12113 if (spec
!= NULL_TREE
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
12116 /* This needs to happen before any tsubsting. */
12117 if (! push_tinst_level (d
))
12120 timevar_push (TV_PARSE
);
12122 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
12123 for the instantiation. */
12124 td
= template_for_substitution (d
);
12125 code_pattern
= DECL_TEMPLATE_RESULT (td
);
12127 /* We should never be trying to instantiate a member of a class
12128 template or partial specialization. */
12129 gcc_assert (d
!= code_pattern
);
12131 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
12132 || DECL_TEMPLATE_SPECIALIZATION (td
))
12133 /* In the case of a friend template whose definition is provided
12134 outside the class, we may have too many arguments. Drop the
12135 ones we don't need. The same is true for specializations. */
12136 args
= get_innermost_template_args
12137 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
12141 if (TREE_CODE (d
) == FUNCTION_DECL
)
12142 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
12144 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
12146 /* We may be in the middle of deferred access check. Disable it now. */
12147 push_deferring_access_checks (dk_no_deferred
);
12149 /* Unless an explicit instantiation directive has already determined
12150 the linkage of D, remember that a definition is available for
12152 if (pattern_defined
12153 && !DECL_INTERFACE_KNOWN (d
)
12154 && !DECL_NOT_REALLY_EXTERN (d
))
12155 mark_definable (d
);
12157 input_location
= DECL_SOURCE_LOCATION (d
);
12159 /* If D is a member of an explicitly instantiated class template,
12160 and no definition is available, treat it like an implicit
12162 if (!pattern_defined
&& expl_inst_class_mem_p
12163 && DECL_EXPLICIT_INSTANTIATION (d
))
12165 DECL_NOT_REALLY_EXTERN (d
) = 0;
12166 DECL_INTERFACE_KNOWN (d
) = 0;
12167 SET_DECL_IMPLICIT_INSTANTIATION (d
);
12172 /* Recheck the substitutions to obtain any warning messages
12173 about ignoring cv qualifiers. */
12174 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
12175 tree type
= TREE_TYPE (gen
);
12177 /* Make sure that we can see identifiers, and compute access
12178 correctly. D is already the target FUNCTION_DECL with the
12180 push_access_scope (d
);
12182 if (TREE_CODE (gen
) == FUNCTION_DECL
)
12184 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
12185 tsubst (TYPE_RAISES_EXCEPTIONS (type
), gen_args
,
12186 tf_warning_or_error
, d
);
12187 /* Don't simply tsubst the function type, as that will give
12188 duplicate warnings about poor parameter qualifications.
12189 The function arguments are the same as the decl_arguments
12190 without the top level cv qualifiers. */
12191 type
= TREE_TYPE (type
);
12193 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
12195 pop_access_scope (d
);
12198 /* Check to see whether we know that this template will be
12199 instantiated in some other file, as with "extern template"
12201 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
12202 /* In general, we do not instantiate such templates... */
12204 /* ... but we instantiate inline functions so that we can inline
12206 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
))
12207 /* ... we instantiate static data members whose values are
12208 needed in integral constant expressions. */
12209 && ! (TREE_CODE (d
) == VAR_DECL
12210 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d
)))
12212 /* Defer all other templates, unless we have been explicitly
12213 forbidden from doing so. */
12214 if (/* If there is no definition, we cannot instantiate the
12217 /* If it's OK to postpone instantiation, do so. */
12219 /* If this is a static data member that will be defined
12220 elsewhere, we don't want to instantiate the entire data
12221 member, but we do want to instantiate the initializer so that
12222 we can substitute that elsewhere. */
12223 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
12225 /* The definition of the static data member is now required so
12226 we must substitute the initializer. */
12227 if (TREE_CODE (d
) == VAR_DECL
12228 && !DECL_INITIAL (d
)
12229 && DECL_INITIAL (code_pattern
))
12234 ns
= decl_namespace_context (d
);
12235 push_nested_namespace (ns
);
12236 push_nested_class (DECL_CONTEXT (d
));
12237 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
12239 tf_warning_or_error
, NULL_TREE
,
12240 /*integral_constant_expression_p=*/false);
12241 cp_finish_decl (d
, init
, /*init_const_expr_p=*/false,
12242 /*asmspec_tree=*/NULL_TREE
,
12243 LOOKUP_ONLYCONVERTING
);
12244 pop_nested_class ();
12245 pop_nested_namespace (ns
);
12248 /* We restore the source position here because it's used by
12249 add_pending_template. */
12250 input_location
= saved_loc
;
12252 if (at_eof
&& !pattern_defined
12253 && DECL_EXPLICIT_INSTANTIATION (d
))
12256 The definition of a non-exported function template, a
12257 non-exported member function template, or a non-exported
12258 member function or static data member of a class template
12259 shall be present in every translation unit in which it is
12260 explicitly instantiated. */
12262 ("explicit instantiation of %qD but no definition available", d
);
12264 /* ??? Historically, we have instantiated inline functions, even
12265 when marked as "extern template". */
12266 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
12267 add_pending_template (d
);
12270 /* Tell the repository that D is available in this translation unit
12271 -- and see if it is supposed to be instantiated here. */
12272 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
12274 /* In a PCH file, despite the fact that the repository hasn't
12275 requested instantiation in the PCH it is still possible that
12276 an instantiation will be required in a file that includes the
12279 add_pending_template (d
);
12280 /* Instantiate inline functions so that the inliner can do its
12281 job, even though we'll not be emitting a copy of this
12283 if (!(TREE_CODE (d
) == FUNCTION_DECL
12284 && flag_inline_trees
12285 && DECL_DECLARED_INLINE_P (d
)))
12289 need_push
= !cfun
|| !global_bindings_p ();
12291 push_to_top_level ();
12293 /* Mark D as instantiated so that recursive calls to
12294 instantiate_decl do not try to instantiate it again. */
12295 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
12297 /* Regenerate the declaration in case the template has been modified
12298 by a subsequent redeclaration. */
12299 regenerate_decl_from_template (d
, td
);
12301 /* We already set the file and line above. Reset them now in case
12302 they changed as a result of calling regenerate_decl_from_template. */
12303 input_location
= DECL_SOURCE_LOCATION (d
);
12305 if (TREE_CODE (d
) == VAR_DECL
)
12309 /* Clear out DECL_RTL; whatever was there before may not be right
12310 since we've reset the type of the declaration. */
12311 SET_DECL_RTL (d
, NULL_RTX
);
12312 DECL_IN_AGGR_P (d
) = 0;
12314 /* The initializer is placed in DECL_INITIAL by
12315 regenerate_decl_from_template. Pull it out so that
12316 finish_decl can process it. */
12317 init
= DECL_INITIAL (d
);
12318 DECL_INITIAL (d
) = NULL_TREE
;
12319 DECL_INITIALIZED_P (d
) = 0;
12321 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
12322 initializer. That function will defer actual emission until
12323 we have a chance to determine linkage. */
12324 DECL_EXTERNAL (d
) = 0;
12326 /* Enter the scope of D so that access-checking works correctly. */
12327 push_nested_class (DECL_CONTEXT (d
));
12328 finish_decl (d
, init
, NULL_TREE
);
12329 pop_nested_class ();
12331 else if (TREE_CODE (d
) == FUNCTION_DECL
)
12333 htab_t saved_local_specializations
;
12338 /* Save away the current list, in case we are instantiating one
12339 template from within the body of another. */
12340 saved_local_specializations
= local_specializations
;
12342 /* Set up the list of local specializations. */
12343 local_specializations
= htab_create (37,
12344 hash_local_specialization
,
12345 eq_local_specializations
,
12348 /* Set up context. */
12349 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
12351 /* Create substitution entries for the parameters. */
12352 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
12353 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
12354 spec_parm
= DECL_ARGUMENTS (d
);
12355 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
12357 register_local_specialization (spec_parm
, tmpl_parm
);
12358 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
12359 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
12363 register_local_specialization (spec_parm
, tmpl_parm
);
12364 tmpl_parm
= TREE_CHAIN (tmpl_parm
);
12365 spec_parm
= TREE_CHAIN (spec_parm
);
12367 gcc_assert (!spec_parm
);
12369 /* Substitute into the body of the function. */
12370 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
12371 tf_warning_or_error
, tmpl
,
12372 /*integral_constant_expression_p=*/false);
12374 /* We don't need the local specializations any more. */
12375 htab_delete (local_specializations
);
12376 local_specializations
= saved_local_specializations
;
12378 /* Finish the function. */
12379 d
= finish_function (0);
12380 expand_or_defer_fn (d
);
12383 /* We're not deferring instantiation any more. */
12384 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
12387 pop_from_top_level ();
12390 input_location
= saved_loc
;
12391 pop_deferring_access_checks ();
12392 pop_tinst_level ();
12394 timevar_pop (TV_PARSE
);
12399 /* Run through the list of templates that we wish we could
12400 instantiate, and instantiate any we can. RETRIES is the
12401 number of times we retry pending template instantiation. */
12404 instantiate_pending_templates (int retries
)
12407 tree last
= NULL_TREE
;
12409 location_t saved_loc
= input_location
;
12410 int saved_in_system_header
= in_system_header
;
12412 /* Instantiating templates may trigger vtable generation. This in turn
12413 may require further template instantiations. We place a limit here
12414 to avoid infinite loop. */
12415 if (pending_templates
&& retries
>= max_tinst_depth
)
12417 tree decl
= TREE_VALUE (pending_templates
);
12419 error ("template instantiation depth exceeds maximum of %d"
12420 " instantiating %q+D, possibly from virtual table generation"
12421 " (use -ftemplate-depth-NN to increase the maximum)",
12422 max_tinst_depth
, decl
);
12423 if (TREE_CODE (decl
) == FUNCTION_DECL
)
12424 /* Pretend that we defined it. */
12425 DECL_INITIAL (decl
) = error_mark_node
;
12433 t
= &pending_templates
;
12436 tree instantiation
= TREE_VALUE (*t
);
12438 reopen_tinst_level (TREE_PURPOSE (*t
));
12440 if (TYPE_P (instantiation
))
12444 if (!COMPLETE_TYPE_P (instantiation
))
12446 instantiate_class_template (instantiation
);
12447 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
12448 for (fn
= TYPE_METHODS (instantiation
);
12450 fn
= TREE_CHAIN (fn
))
12451 if (! DECL_ARTIFICIAL (fn
))
12452 instantiate_decl (fn
,
12454 /*expl_inst_class_mem_p=*/false);
12455 if (COMPLETE_TYPE_P (instantiation
))
12459 if (COMPLETE_TYPE_P (instantiation
))
12460 /* If INSTANTIATION has been instantiated, then we don't
12461 need to consider it again in the future. */
12462 *t
= TREE_CHAIN (*t
);
12466 t
= &TREE_CHAIN (*t
);
12471 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
12472 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
12475 = instantiate_decl (instantiation
,
12477 /*expl_inst_class_mem_p=*/false);
12478 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
12482 if (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
12483 || DECL_TEMPLATE_INSTANTIATED (instantiation
))
12484 /* If INSTANTIATION has been instantiated, then we don't
12485 need to consider it again in the future. */
12486 *t
= TREE_CHAIN (*t
);
12490 t
= &TREE_CHAIN (*t
);
12494 current_tinst_level
= NULL_TREE
;
12496 last_pending_template
= last
;
12498 while (reconsider
);
12500 input_location
= saved_loc
;
12501 in_system_header
= saved_in_system_header
;
12504 /* Substitute ARGVEC into T, which is a list of initializers for
12505 either base class or a non-static data member. The TREE_PURPOSEs
12506 are DECLs, and the TREE_VALUEs are the initializer values. Used by
12507 instantiate_decl. */
12510 tsubst_initializer_list (tree t
, tree argvec
)
12512 tree inits
= NULL_TREE
;
12514 for (; t
; t
= TREE_CHAIN (t
))
12519 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
, tf_warning_or_error
,
12521 decl
= expand_member_init (decl
);
12522 if (decl
&& !DECL_P (decl
))
12523 in_base_initializer
= 1;
12525 init
= tsubst_expr (TREE_VALUE (t
), argvec
, tf_warning_or_error
,
12527 /*integral_constant_expression_p=*/false);
12528 in_base_initializer
= 0;
12532 init
= build_tree_list (decl
, init
);
12533 TREE_CHAIN (init
) = inits
;
12540 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
12543 set_current_access_from_decl (tree decl
)
12545 if (TREE_PRIVATE (decl
))
12546 current_access_specifier
= access_private_node
;
12547 else if (TREE_PROTECTED (decl
))
12548 current_access_specifier
= access_protected_node
;
12550 current_access_specifier
= access_public_node
;
12553 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
12554 is the instantiation (which should have been created with
12555 start_enum) and ARGS are the template arguments to use. */
12558 tsubst_enum (tree tag
, tree newtag
, tree args
)
12562 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
12567 decl
= TREE_VALUE (e
);
12568 /* Note that in a template enum, the TREE_VALUE is the
12569 CONST_DECL, not the corresponding INTEGER_CST. */
12570 value
= tsubst_expr (DECL_INITIAL (decl
),
12571 args
, tf_warning_or_error
, NULL_TREE
,
12572 /*integral_constant_expression_p=*/true);
12574 /* Give this enumeration constant the correct access. */
12575 set_current_access_from_decl (decl
);
12577 /* Actually build the enumerator itself. */
12578 build_enumerator (DECL_NAME (decl
), value
, newtag
);
12581 finish_enum (newtag
);
12582 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
12583 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
12586 /* DECL is a FUNCTION_DECL that is a template specialization. Return
12587 its type -- but without substituting the innermost set of template
12588 arguments. So, innermost set of template parameters will appear in
12592 get_mostly_instantiated_function_type (tree decl
)
12600 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
12601 targs
= DECL_TI_ARGS (decl
);
12602 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
12603 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
12605 /* There should be as many levels of arguments as there are levels
12607 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
12609 fn_type
= TREE_TYPE (tmpl
);
12611 if (parm_depth
== 1)
12612 /* No substitution is necessary. */
12616 int i
, save_access_control
;
12619 /* Replace the innermost level of the TARGS with NULL_TREEs to
12620 let tsubst know not to substitute for those parameters. */
12621 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
12622 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
12623 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
12624 TMPL_ARGS_LEVEL (targs
, i
));
12625 SET_TMPL_ARGS_LEVEL (partial_args
,
12626 TMPL_ARGS_DEPTH (targs
),
12627 make_tree_vec (DECL_NTPARMS (tmpl
)));
12629 /* Disable access control as this function is used only during
12631 save_access_control
= flag_access_control
;
12632 flag_access_control
= 0;
12634 ++processing_template_decl
;
12635 /* Now, do the (partial) substitution to figure out the
12636 appropriate function type. */
12637 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
12638 --processing_template_decl
;
12640 /* Substitute into the template parameters to obtain the real
12641 innermost set of parameters. This step is important if the
12642 innermost set of template parameters contains value
12643 parameters whose types depend on outer template parameters. */
12644 TREE_VEC_LENGTH (partial_args
)--;
12645 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
12647 flag_access_control
= save_access_control
;
12653 /* Return truthvalue if we're processing a template different from
12654 the last one involved in diagnostics. */
12656 problematic_instantiation_changed (void)
12658 return last_template_error_tick
!= tinst_level_tick
;
12661 /* Remember current template involved in diagnostics. */
12663 record_last_problematic_instantiation (void)
12665 last_template_error_tick
= tinst_level_tick
;
12669 current_instantiation (void)
12671 return current_tinst_level
;
12674 /* [temp.param] Check that template non-type parm TYPE is of an allowable
12675 type. Return zero for ok, nonzero for disallowed. Issue error and
12676 warning messages under control of COMPLAIN. */
12679 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
12681 if (INTEGRAL_TYPE_P (type
))
12683 else if (POINTER_TYPE_P (type
))
12685 else if (TYPE_PTR_TO_MEMBER_P (type
))
12687 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
12689 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
12692 if (complain
& tf_error
)
12693 error ("%q#T is not a valid type for a template constant parameter", type
);
12697 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12698 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
12701 dependent_type_p_r (tree type
)
12707 A type is dependent if it is:
12709 -- a template parameter. Template template parameters are types
12710 for us (since TYPE_P holds true for them) so we handle
12712 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
12713 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
12715 /* -- a qualified-id with a nested-name-specifier which contains a
12716 class-name that names a dependent type or whose unqualified-id
12717 names a dependent type. */
12718 if (TREE_CODE (type
) == TYPENAME_TYPE
)
12720 /* -- a cv-qualified type where the cv-unqualified type is
12722 type
= TYPE_MAIN_VARIANT (type
);
12723 /* -- a compound type constructed from any dependent type. */
12724 if (TYPE_PTR_TO_MEMBER_P (type
))
12725 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
12726 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
12728 else if (TREE_CODE (type
) == POINTER_TYPE
12729 || TREE_CODE (type
) == REFERENCE_TYPE
)
12730 return dependent_type_p (TREE_TYPE (type
));
12731 else if (TREE_CODE (type
) == FUNCTION_TYPE
12732 || TREE_CODE (type
) == METHOD_TYPE
)
12736 if (dependent_type_p (TREE_TYPE (type
)))
12738 for (arg_type
= TYPE_ARG_TYPES (type
);
12740 arg_type
= TREE_CHAIN (arg_type
))
12741 if (dependent_type_p (TREE_VALUE (arg_type
)))
12745 /* -- an array type constructed from any dependent type or whose
12746 size is specified by a constant expression that is
12747 value-dependent. */
12748 if (TREE_CODE (type
) == ARRAY_TYPE
)
12750 if (TYPE_DOMAIN (type
)
12751 && ((value_dependent_expression_p
12752 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))
12753 || (type_dependent_expression_p
12754 (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))))))
12756 return dependent_type_p (TREE_TYPE (type
));
12759 /* -- a template-id in which either the template name is a template
12761 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
12763 /* ... or any of the template arguments is a dependent type or
12764 an expression that is type-dependent or value-dependent. */
12765 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
12766 && (any_dependent_template_arguments_p
12767 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
12770 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12771 expression is not type-dependent, then it should already been
12773 if (TREE_CODE (type
) == TYPEOF_TYPE
)
12776 /* The standard does not specifically mention types that are local
12777 to template functions or local classes, but they should be
12778 considered dependent too. For example:
12780 template <int I> void f() {
12785 The size of `E' cannot be known until the value of `I' has been
12786 determined. Therefore, `E' must be considered dependent. */
12787 scope
= TYPE_CONTEXT (type
);
12788 if (scope
&& TYPE_P (scope
))
12789 return dependent_type_p (scope
);
12790 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
12791 return type_dependent_expression_p (scope
);
12793 /* Other types are non-dependent. */
12797 /* Returns TRUE if TYPE is dependent, in the sense of
12798 [temp.dep.type]. */
12801 dependent_type_p (tree type
)
12803 /* If there are no template parameters in scope, then there can't be
12804 any dependent types. */
12805 if (!processing_template_decl
)
12807 /* If we are not processing a template, then nobody should be
12808 providing us with a dependent type. */
12810 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
);
12814 /* If the type is NULL, we have not computed a type for the entity
12815 in question; in that case, the type is dependent. */
12819 /* Erroneous types can be considered non-dependent. */
12820 if (type
== error_mark_node
)
12823 /* If we have not already computed the appropriate value for TYPE,
12825 if (!TYPE_DEPENDENT_P_VALID (type
))
12827 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
12828 TYPE_DEPENDENT_P_VALID (type
) = 1;
12831 return TYPE_DEPENDENT_P (type
);
12834 /* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12837 dependent_scope_ref_p (tree expression
, bool criterion (tree
))
12842 gcc_assert (TREE_CODE (expression
) == SCOPE_REF
);
12844 if (!TYPE_P (TREE_OPERAND (expression
, 0)))
12847 scope
= TREE_OPERAND (expression
, 0);
12848 name
= TREE_OPERAND (expression
, 1);
12852 An id-expression is type-dependent if it contains a
12853 nested-name-specifier that contains a class-name that names a
12855 /* The suggested resolution to Core Issue 2 implies that if the
12856 qualifying type is the current class, then we must peek
12859 && currently_open_class (scope
)
12860 && !criterion (name
))
12862 if (dependent_type_p (scope
))
12868 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
12869 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
12873 value_dependent_expression_p (tree expression
)
12875 if (!processing_template_decl
)
12878 /* A name declared with a dependent type. */
12879 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
12882 switch (TREE_CODE (expression
))
12884 case IDENTIFIER_NODE
:
12885 /* A name that has not been looked up -- must be dependent. */
12888 case TEMPLATE_PARM_INDEX
:
12889 /* A non-type template parm. */
12893 /* A non-type template parm. */
12894 if (DECL_TEMPLATE_PARM_P (expression
))
12899 /* A constant with integral or enumeration type and is initialized
12900 with an expression that is value-dependent. */
12901 if (DECL_INITIAL (expression
)
12902 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression
))
12903 && value_dependent_expression_p (DECL_INITIAL (expression
)))
12907 case DYNAMIC_CAST_EXPR
:
12908 case STATIC_CAST_EXPR
:
12909 case CONST_CAST_EXPR
:
12910 case REINTERPRET_CAST_EXPR
:
12912 /* These expressions are value-dependent if the type to which
12913 the cast occurs is dependent or the expression being casted
12914 is value-dependent. */
12916 tree type
= TREE_TYPE (expression
);
12918 if (dependent_type_p (type
))
12921 /* A functional cast has a list of operands. */
12922 expression
= TREE_OPERAND (expression
, 0);
12925 /* If there are no operands, it must be an expression such
12926 as "int()". This should not happen for aggregate types
12927 because it would form non-constant expressions. */
12928 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
12933 if (TREE_CODE (expression
) == TREE_LIST
)
12935 for (; expression
; expression
= TREE_CHAIN (expression
))
12936 if (value_dependent_expression_p (TREE_VALUE (expression
)))
12941 return value_dependent_expression_p (expression
);
12946 /* A `sizeof' expression is value-dependent if the operand is
12948 expression
= TREE_OPERAND (expression
, 0);
12949 if (TYPE_P (expression
))
12950 return dependent_type_p (expression
);
12951 return type_dependent_expression_p (expression
);
12954 return dependent_scope_ref_p (expression
, value_dependent_expression_p
);
12956 case COMPONENT_REF
:
12957 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
12958 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
12961 /* A CALL_EXPR may appear in a constant expression if it is a
12962 call to a builtin function, e.g., __builtin_constant_p. All
12963 such calls are value-dependent. */
12967 /* A constant expression is value-dependent if any subexpression is
12968 value-dependent. */
12969 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
12971 case tcc_reference
:
12973 return (value_dependent_expression_p
12974 (TREE_OPERAND (expression
, 0)));
12976 case tcc_comparison
:
12978 return ((value_dependent_expression_p
12979 (TREE_OPERAND (expression
, 0)))
12980 || (value_dependent_expression_p
12981 (TREE_OPERAND (expression
, 1))));
12983 case tcc_expression
:
12986 for (i
= 0; i
< TREE_CODE_LENGTH (TREE_CODE (expression
)); ++i
)
12987 /* In some cases, some of the operands may be missing.
12988 (For example, in the case of PREDECREMENT_EXPR, the
12989 amount to increment by may be missing.) That doesn't
12990 make the expression dependent. */
12991 if (TREE_OPERAND (expression
, i
)
12992 && (value_dependent_expression_p
12993 (TREE_OPERAND (expression
, i
))))
13003 /* The expression is not value-dependent. */
13007 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
13008 [temp.dep.expr]. */
13011 type_dependent_expression_p (tree expression
)
13013 if (!processing_template_decl
)
13016 if (expression
== error_mark_node
)
13019 /* An unresolved name is always dependent. */
13020 if (TREE_CODE (expression
) == IDENTIFIER_NODE
13021 || TREE_CODE (expression
) == USING_DECL
)
13024 /* Some expression forms are never type-dependent. */
13025 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
13026 || TREE_CODE (expression
) == SIZEOF_EXPR
13027 || TREE_CODE (expression
) == ALIGNOF_EXPR
13028 || TREE_CODE (expression
) == TYPEID_EXPR
13029 || TREE_CODE (expression
) == DELETE_EXPR
13030 || TREE_CODE (expression
) == VEC_DELETE_EXPR
13031 || TREE_CODE (expression
) == THROW_EXPR
)
13034 /* The types of these expressions depends only on the type to which
13035 the cast occurs. */
13036 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
13037 || TREE_CODE (expression
) == STATIC_CAST_EXPR
13038 || TREE_CODE (expression
) == CONST_CAST_EXPR
13039 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
13040 || TREE_CODE (expression
) == CAST_EXPR
)
13041 return dependent_type_p (TREE_TYPE (expression
));
13043 /* The types of these expressions depends only on the type created
13044 by the expression. */
13045 if (TREE_CODE (expression
) == NEW_EXPR
13046 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
13048 /* For NEW_EXPR tree nodes created inside a template, either
13049 the object type itself or a TREE_LIST may appear as the
13051 tree type
= TREE_OPERAND (expression
, 1);
13052 if (TREE_CODE (type
) == TREE_LIST
)
13053 /* This is an array type. We need to check array dimensions
13055 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
13056 || value_dependent_expression_p
13057 (TREE_OPERAND (TREE_VALUE (type
), 1));
13059 return dependent_type_p (type
);
13062 if (TREE_CODE (expression
) == SCOPE_REF
13063 && dependent_scope_ref_p (expression
,
13064 type_dependent_expression_p
))
13067 if (TREE_CODE (expression
) == FUNCTION_DECL
13068 && DECL_LANG_SPECIFIC (expression
)
13069 && DECL_TEMPLATE_INFO (expression
)
13070 && (any_dependent_template_arguments_p
13071 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
13074 if (TREE_CODE (expression
) == TEMPLATE_DECL
13075 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
13078 if (TREE_TYPE (expression
) == unknown_type_node
)
13080 if (TREE_CODE (expression
) == ADDR_EXPR
)
13081 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
13082 if (TREE_CODE (expression
) == COMPONENT_REF
13083 || TREE_CODE (expression
) == OFFSET_REF
)
13085 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
13087 expression
= TREE_OPERAND (expression
, 1);
13088 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
13091 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
13092 if (TREE_CODE (expression
) == SCOPE_REF
)
13095 if (TREE_CODE (expression
) == BASELINK
)
13096 expression
= BASELINK_FUNCTIONS (expression
);
13098 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
13100 if (any_dependent_template_arguments_p
13101 (TREE_OPERAND (expression
, 1)))
13103 expression
= TREE_OPERAND (expression
, 0);
13105 gcc_assert (TREE_CODE (expression
) == OVERLOAD
13106 || TREE_CODE (expression
) == FUNCTION_DECL
);
13110 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
13112 expression
= OVL_NEXT (expression
);
13117 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
13119 return (dependent_type_p (TREE_TYPE (expression
)));
13122 /* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
13123 contains a type-dependent expression. */
13126 any_type_dependent_arguments_p (tree args
)
13130 tree arg
= TREE_VALUE (args
);
13132 if (type_dependent_expression_p (arg
))
13134 args
= TREE_CHAIN (args
);
13139 /* Returns TRUE if the ARG (a template argument) is dependent. */
13142 dependent_template_arg_p (tree arg
)
13144 if (!processing_template_decl
)
13147 if (TREE_CODE (arg
) == TEMPLATE_DECL
13148 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
13149 return dependent_template_p (arg
);
13150 else if (TYPE_P (arg
))
13151 return dependent_type_p (arg
);
13153 return (type_dependent_expression_p (arg
)
13154 || value_dependent_expression_p (arg
));
13157 /* Returns true if ARGS (a collection of template arguments) contains
13158 any dependent arguments. */
13161 any_dependent_template_arguments_p (tree args
)
13168 if (args
== error_mark_node
)
13171 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
13173 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
13174 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
13175 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
13182 /* Returns TRUE if the template TMPL is dependent. */
13185 dependent_template_p (tree tmpl
)
13187 if (TREE_CODE (tmpl
) == OVERLOAD
)
13191 if (dependent_template_p (OVL_FUNCTION (tmpl
)))
13193 tmpl
= OVL_CHAIN (tmpl
);
13198 /* Template template parameters are dependent. */
13199 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
13200 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
13202 /* So are names that have not been looked up. */
13203 if (TREE_CODE (tmpl
) == SCOPE_REF
13204 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
13206 /* So are member templates of dependent classes. */
13207 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
13208 return dependent_type_p (DECL_CONTEXT (tmpl
));
13212 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
13215 dependent_template_id_p (tree tmpl
, tree args
)
13217 return (dependent_template_p (tmpl
)
13218 || any_dependent_template_arguments_p (args
));
13221 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13222 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
13223 can be found. Note that this function peers inside uninstantiated
13224 templates and therefore should be used only in extremely limited
13225 situations. ONLY_CURRENT_P restricts this peering to the currently
13226 open classes hierarchy (which is required when comparing types). */
13229 resolve_typename_type (tree type
, bool only_current_p
)
13237 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
13239 scope
= TYPE_CONTEXT (type
);
13240 name
= TYPE_IDENTIFIER (type
);
13242 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13243 it first before we can figure out what NAME refers to. */
13244 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
13245 scope
= resolve_typename_type (scope
, only_current_p
);
13246 /* If we don't know what SCOPE refers to, then we cannot resolve the
13248 if (scope
== error_mark_node
|| TREE_CODE (scope
) == TYPENAME_TYPE
)
13249 return error_mark_node
;
13250 /* If the SCOPE is a template type parameter, we have no way of
13251 resolving the name. */
13252 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
13254 /* If the SCOPE is not the current instantiation, there's no reason
13255 to look inside it. */
13256 if (only_current_p
&& !currently_open_class (scope
))
13257 return error_mark_node
;
13258 /* If SCOPE is a partial instantiation, it will not have a valid
13259 TYPE_FIELDS list, so use the original template. */
13260 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
13261 /* Enter the SCOPE so that name lookup will be resolved as if we
13262 were in the class definition. In particular, SCOPE will no
13263 longer be considered a dependent type. */
13264 pushed_scope
= push_scope (scope
);
13265 /* Look up the declaration. */
13266 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
13267 /* Obtain the set of qualifiers applied to the TYPE. */
13268 quals
= cp_type_quals (type
);
13269 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
13270 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
13272 type
= error_mark_node
;
13273 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
13274 && TREE_CODE (decl
) == TYPE_DECL
)
13275 type
= TREE_TYPE (decl
);
13276 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
13277 && DECL_CLASS_TEMPLATE_P (decl
))
13281 /* Obtain the template and the arguments. */
13282 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
13283 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
13284 /* Instantiate the template. */
13285 type
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
13286 /*entering_scope=*/0, tf_error
| tf_user
);
13289 type
= error_mark_node
;
13290 /* Qualify the resulting type. */
13291 if (type
!= error_mark_node
&& quals
)
13292 type
= cp_build_qualified_type (type
, quals
);
13293 /* Leave the SCOPE. */
13295 pop_scope (pushed_scope
);
13300 /* EXPR is an expression which is not type-dependent. Return a proxy
13301 for EXPR that can be used to compute the types of larger
13302 expressions containing EXPR. */
13305 build_non_dependent_expr (tree expr
)
13309 /* Preserve null pointer constants so that the type of things like
13310 "p == 0" where "p" is a pointer can be determined. */
13311 if (null_ptr_cst_p (expr
))
13313 /* Preserve OVERLOADs; the functions must be available to resolve
13316 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
13317 inner_expr
= TREE_OPERAND (inner_expr
, 0);
13318 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
13319 inner_expr
= TREE_OPERAND (inner_expr
, 1);
13320 if (is_overloaded_fn (inner_expr
)
13321 || TREE_CODE (inner_expr
) == OFFSET_REF
)
13323 /* There is no need to return a proxy for a variable. */
13324 if (TREE_CODE (expr
) == VAR_DECL
)
13326 /* Preserve string constants; conversions from string constants to
13327 "char *" are allowed, even though normally a "const char *"
13328 cannot be used to initialize a "char *". */
13329 if (TREE_CODE (expr
) == STRING_CST
)
13331 /* Preserve arithmetic constants, as an optimization -- there is no
13332 reason to create a new node. */
13333 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
13335 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
13336 There is at least one place where we want to know that a
13337 particular expression is a throw-expression: when checking a ?:
13338 expression, there are special rules if the second or third
13339 argument is a throw-expression. */
13340 if (TREE_CODE (expr
) == THROW_EXPR
)
13343 if (TREE_CODE (expr
) == COND_EXPR
)
13344 return build3 (COND_EXPR
,
13346 TREE_OPERAND (expr
, 0),
13347 (TREE_OPERAND (expr
, 1)
13348 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
13349 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
13350 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
13351 if (TREE_CODE (expr
) == COMPOUND_EXPR
13352 && !COMPOUND_EXPR_OVERLOADED (expr
))
13353 return build2 (COMPOUND_EXPR
,
13355 TREE_OPERAND (expr
, 0),
13356 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
13358 /* If the type is unknown, it can't really be non-dependent */
13359 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
13361 /* Otherwise, build a NON_DEPENDENT_EXPR.
13363 REFERENCE_TYPEs are not stripped for expressions in templates
13364 because doing so would play havoc with mangling. Consider, for
13367 template <typename T> void f<T& g>() { g(); }
13369 In the body of "f", the expression for "g" will have
13370 REFERENCE_TYPE, even though the standard says that it should
13371 not. The reason is that we must preserve the syntactic form of
13372 the expression so that mangling (say) "f<g>" inside the body of
13373 "f" works out correctly. Therefore, the REFERENCE_TYPE is
13375 return build1 (NON_DEPENDENT_EXPR
, non_reference (TREE_TYPE (expr
)), expr
);
13378 /* ARGS is a TREE_LIST of expressions as arguments to a function call.
13379 Return a new TREE_LIST with the various arguments replaced with
13380 equivalent non-dependent expressions. */
13383 build_non_dependent_args (tree args
)
13388 new_args
= NULL_TREE
;
13389 for (a
= args
; a
; a
= TREE_CHAIN (a
))
13390 new_args
= tree_cons (NULL_TREE
,
13391 build_non_dependent_expr (TREE_VALUE (a
)),
13393 return nreverse (new_args
);
13396 #include "gt-cp-pt.h"