1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
38 #include "c-family/c-common.h"
39 #include "cp-objcp-common.h"
40 #include "tree-inline.h"
45 #include "tree-iterator.h"
48 /* The type of functions taking a tree, and some additional data, and
50 typedef int (*tree_fn_t
) (tree
, void*);
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the work. */
55 struct GTY (()) pending_template
{
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*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(()) struct tinst_level
*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 typedef struct GTY(()) spec_entry
90 static GTY ((param_is (spec_entry
)))
91 htab_t decl_specializations
;
93 static GTY ((param_is (spec_entry
)))
94 htab_t type_specializations
;
96 /* Contains canonical template parameter types. The vector is indexed by
97 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
98 TREE_LIST, whose TREE_VALUEs contain the canonical template
99 parameters of various types and levels. */
100 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
102 #define UNIFY_ALLOW_NONE 0
103 #define UNIFY_ALLOW_MORE_CV_QUAL 1
104 #define UNIFY_ALLOW_LESS_CV_QUAL 2
105 #define UNIFY_ALLOW_DERIVED 4
106 #define UNIFY_ALLOW_INTEGER 8
107 #define UNIFY_ALLOW_OUTER_LEVEL 16
108 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
109 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
111 static void push_access_scope (tree
);
112 static void pop_access_scope (tree
);
113 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
114 unification_kind_t
, int);
115 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
116 unification_kind_t
, int, bool);
117 static int unify (tree
, tree
, tree
, tree
, int);
118 static void add_pending_template (tree
);
119 static tree
reopen_tinst_level (struct tinst_level
*);
120 static tree
tsubst_initializer_list (tree
, tree
);
121 static tree
get_class_bindings (tree
, tree
, tree
);
122 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
124 static void tsubst_enum (tree
, tree
, tree
);
125 static tree
add_to_template_args (tree
, tree
);
126 static tree
add_outermost_template_args (tree
, tree
);
127 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
128 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
130 static int type_unification_real (tree
, tree
, tree
, const tree
*,
131 unsigned int, int, unification_kind_t
, int);
132 static void note_template_header (int);
133 static tree
convert_nontype_argument_function (tree
, tree
);
134 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
135 static tree
convert_template_argument (tree
, tree
, tree
,
136 tsubst_flags_t
, int, tree
);
137 static int for_each_template_parm (tree
, tree_fn_t
, void*,
138 struct pointer_set_t
*, bool);
139 static tree
expand_template_argument_pack (tree
);
140 static tree
build_template_parm_index (int, int, int, tree
, tree
);
141 static bool inline_needs_template_parms (tree
);
142 static void push_inline_template_parms_recursive (tree
, int);
143 static tree
retrieve_local_specialization (tree
);
144 static void register_local_specialization (tree
, tree
);
145 static hashval_t
hash_specialization (const void *p
);
146 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
147 static int mark_template_parm (tree
, void *);
148 static int template_parm_this_level_p (tree
, void *);
149 static tree
tsubst_friend_function (tree
, tree
);
150 static tree
tsubst_friend_class (tree
, tree
);
151 static int can_complete_type_without_circularity (tree
);
152 static tree
get_bindings (tree
, tree
, tree
, bool);
153 static int template_decl_level (tree
);
154 static int check_cv_quals_for_unify (int, tree
, tree
);
155 static void template_parm_level_and_index (tree
, int*, int*);
156 static int unify_pack_expansion (tree
, tree
, tree
, tree
, int, bool, bool);
157 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
158 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
159 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
160 static void regenerate_decl_from_template (tree
, tree
);
161 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
162 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
163 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
164 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
165 static bool check_specialization_scope (void);
166 static tree
process_partial_specialization (tree
);
167 static void set_current_access_from_decl (tree
);
168 static tree
get_template_base (tree
, tree
, tree
, tree
);
169 static tree
try_class_unification (tree
, tree
, tree
, tree
);
170 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
172 static bool template_template_parm_bindings_ok_p (tree
, tree
);
173 static int template_args_equal (tree
, tree
);
174 static void tsubst_default_arguments (tree
);
175 static tree
for_each_template_parm_r (tree
*, int *, void *);
176 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
177 static void copy_default_args_to_explicit_spec (tree
);
178 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
179 static int eq_local_specializations (const void *, const void *);
180 static bool dependent_template_arg_p (tree
);
181 static bool any_template_arguments_need_structural_equality_p (tree
);
182 static bool dependent_type_p_r (tree
);
183 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
184 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
185 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
186 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
187 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
188 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
190 static hashval_t
iterative_hash_template_arg (tree arg
, hashval_t val
);
191 static tree
listify (tree
);
192 static tree
listify_autos (tree
, tree
);
194 /* Make the current scope suitable for access checking when we are
195 processing T. T can be FUNCTION_DECL for instantiated function
196 template, or VAR_DECL for static member variable (need by
197 instantiate_decl). */
200 push_access_scope (tree t
)
202 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
203 || TREE_CODE (t
) == VAR_DECL
);
205 if (DECL_FRIEND_CONTEXT (t
))
206 push_nested_class (DECL_FRIEND_CONTEXT (t
));
207 else if (DECL_CLASS_SCOPE_P (t
))
208 push_nested_class (DECL_CONTEXT (t
));
210 push_to_top_level ();
212 if (TREE_CODE (t
) == FUNCTION_DECL
)
214 saved_access_scope
= tree_cons
215 (NULL_TREE
, current_function_decl
, saved_access_scope
);
216 current_function_decl
= t
;
220 /* Restore the scope set up by push_access_scope. T is the node we
224 pop_access_scope (tree t
)
226 if (TREE_CODE (t
) == FUNCTION_DECL
)
228 current_function_decl
= TREE_VALUE (saved_access_scope
);
229 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
232 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
235 pop_from_top_level ();
238 /* Do any processing required when DECL (a member template
239 declaration) is finished. Returns the TEMPLATE_DECL corresponding
240 to DECL, unless it is a specialization, in which case the DECL
241 itself is returned. */
244 finish_member_template_decl (tree decl
)
246 if (decl
== error_mark_node
)
247 return error_mark_node
;
249 gcc_assert (DECL_P (decl
));
251 if (TREE_CODE (decl
) == TYPE_DECL
)
255 type
= TREE_TYPE (decl
);
256 if (type
== error_mark_node
)
257 return error_mark_node
;
258 if (MAYBE_CLASS_TYPE_P (type
)
259 && CLASSTYPE_TEMPLATE_INFO (type
)
260 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
262 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
263 check_member_template (tmpl
);
268 else if (TREE_CODE (decl
) == FIELD_DECL
)
269 error ("data member %qD cannot be a member template", decl
);
270 else if (DECL_TEMPLATE_INFO (decl
))
272 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
274 check_member_template (DECL_TI_TEMPLATE (decl
));
275 return DECL_TI_TEMPLATE (decl
);
281 error ("invalid member template declaration %qD", decl
);
283 return error_mark_node
;
286 /* Create a template info node. */
289 build_template_info (tree template_decl
, tree template_args
)
291 tree result
= make_node (TEMPLATE_INFO
);
292 TI_TEMPLATE (result
) = template_decl
;
293 TI_ARGS (result
) = template_args
;
297 /* Return the template info node corresponding to T, whatever T is. */
300 get_template_info (const_tree t
)
302 tree tinfo
= NULL_TREE
;
304 if (!t
|| t
== error_mark_node
)
307 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
308 tinfo
= DECL_TEMPLATE_INFO (t
);
310 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
313 if (TAGGED_TYPE_P (t
))
314 tinfo
= TYPE_TEMPLATE_INFO (t
);
315 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
316 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
321 /* Returns the template nesting level of the indicated class TYPE.
331 A<T>::B<U> has depth two, while A<T> has depth one.
332 Both A<T>::B<int> and A<int>::B<U> have depth one, if
333 they are instantiations, not specializations.
335 This function is guaranteed to return 0 if passed NULL_TREE so
336 that, for example, `template_class_depth (current_class_type)' is
340 template_class_depth (tree type
)
345 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
346 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
347 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
349 tree tinfo
= get_template_info (type
);
351 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
352 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
359 /* Subroutine of maybe_begin_member_template_processing.
360 Returns true if processing DECL needs us to push template parms. */
363 inline_needs_template_parms (tree decl
)
365 if (! DECL_TEMPLATE_INFO (decl
))
368 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
369 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
372 /* Subroutine of maybe_begin_member_template_processing.
373 Push the template parms in PARMS, starting from LEVELS steps into the
374 chain, and ending at the beginning, since template parms are listed
378 push_inline_template_parms_recursive (tree parmlist
, int levels
)
380 tree parms
= TREE_VALUE (parmlist
);
384 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
386 ++processing_template_decl
;
387 current_template_parms
388 = tree_cons (size_int (processing_template_decl
),
389 parms
, current_template_parms
);
390 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
392 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
394 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
396 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
398 if (parm
== error_mark_node
)
401 gcc_assert (DECL_P (parm
));
403 switch (TREE_CODE (parm
))
412 /* Make a CONST_DECL as is done in process_template_parm.
413 It is ugly that we recreate this here; the original
414 version built in process_template_parm is no longer
416 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
417 CONST_DECL
, DECL_NAME (parm
),
419 DECL_ARTIFICIAL (decl
) = 1;
420 TREE_CONSTANT (decl
) = 1;
421 TREE_READONLY (decl
) = 1;
422 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
423 SET_DECL_TEMPLATE_PARM_P (decl
);
434 /* Restore the template parameter context for a member template or
435 a friend template defined in a class definition. */
438 maybe_begin_member_template_processing (tree decl
)
443 if (inline_needs_template_parms (decl
))
445 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
446 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
448 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
451 parms
= TREE_CHAIN (parms
);
454 push_inline_template_parms_recursive (parms
, levels
);
457 /* Remember how many levels of template parameters we pushed so that
458 we can pop them later. */
459 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
462 /* Undo the effects of maybe_begin_member_template_processing. */
465 maybe_end_member_template_processing (void)
470 if (VEC_length (int, inline_parm_levels
) == 0)
473 last
= VEC_pop (int, inline_parm_levels
);
474 for (i
= 0; i
< last
; ++i
)
476 --processing_template_decl
;
477 current_template_parms
= TREE_CHAIN (current_template_parms
);
482 /* Return a new template argument vector which contains all of ARGS,
483 but has as its innermost set of arguments the EXTRA_ARGS. */
486 add_to_template_args (tree args
, tree extra_args
)
493 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
496 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
497 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
499 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
500 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
502 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
503 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
508 /* Like add_to_template_args, but only the outermost ARGS are added to
509 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
510 (EXTRA_ARGS) levels are added. This function is used to combine
511 the template arguments from a partial instantiation with the
512 template arguments used to attain the full instantiation from the
513 partial instantiation. */
516 add_outermost_template_args (tree args
, tree extra_args
)
520 /* If there are more levels of EXTRA_ARGS than there are ARGS,
521 something very fishy is going on. */
522 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
524 /* If *all* the new arguments will be the EXTRA_ARGS, just return
526 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
529 /* For the moment, we make ARGS look like it contains fewer levels. */
530 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
532 new_args
= add_to_template_args (args
, extra_args
);
534 /* Now, we restore ARGS to its full dimensions. */
535 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
540 /* Return the N levels of innermost template arguments from the ARGS. */
543 get_innermost_template_args (tree args
, int n
)
551 /* If N is 1, just return the innermost set of template arguments. */
553 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
555 /* If we're not removing anything, just return the arguments we were
557 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
558 gcc_assert (extra_levels
>= 0);
559 if (extra_levels
== 0)
562 /* Make a new set of arguments, not containing the outer arguments. */
563 new_args
= make_tree_vec (n
);
564 for (i
= 1; i
<= n
; ++i
)
565 SET_TMPL_ARGS_LEVEL (new_args
, i
,
566 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
571 /* The inverse of get_innermost_template_args: Return all but the innermost
572 EXTRA_LEVELS levels of template arguments from the ARGS. */
575 strip_innermost_template_args (tree args
, int extra_levels
)
578 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
583 /* If N is 1, just return the outermost set of template arguments. */
585 return TMPL_ARGS_LEVEL (args
, 1);
587 /* If we're not removing anything, just return the arguments we were
589 gcc_assert (extra_levels
>= 0);
590 if (extra_levels
== 0)
593 /* Make a new set of arguments, not containing the inner arguments. */
594 new_args
= make_tree_vec (n
);
595 for (i
= 1; i
<= n
; ++i
)
596 SET_TMPL_ARGS_LEVEL (new_args
, i
,
597 TMPL_ARGS_LEVEL (args
, i
));
602 /* We've got a template header coming up; push to a new level for storing
606 begin_template_parm_list (void)
608 /* We use a non-tag-transparent scope here, which causes pushtag to
609 put tags in this scope, rather than in the enclosing class or
610 namespace scope. This is the right thing, since we want
611 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
612 global template class, push_template_decl handles putting the
613 TEMPLATE_DECL into top-level scope. For a nested template class,
616 template <class T> struct S1 {
617 template <class T> struct S2 {};
620 pushtag contains special code to call pushdecl_with_scope on the
621 TEMPLATE_DECL for S2. */
622 begin_scope (sk_template_parms
, NULL
);
623 ++processing_template_decl
;
624 ++processing_template_parmlist
;
625 note_template_header (0);
628 /* This routine is called when a specialization is declared. If it is
629 invalid to declare a specialization here, an error is reported and
630 false is returned, otherwise this routine will return true. */
633 check_specialization_scope (void)
635 tree scope
= current_scope ();
639 An explicit specialization shall be declared in the namespace of
640 which the template is a member, or, for member templates, in the
641 namespace of which the enclosing class or enclosing class
642 template is a member. An explicit specialization of a member
643 function, member class or static data member of a class template
644 shall be declared in the namespace of which the class template
646 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
648 error ("explicit specialization in non-namespace scope %qD", scope
);
654 In an explicit specialization declaration for a member of a class
655 template or a member template that appears in namespace scope,
656 the member template and some of its enclosing class templates may
657 remain unspecialized, except that the declaration shall not
658 explicitly specialize a class member template if its enclosing
659 class templates are not explicitly specialized as well. */
660 if (current_template_parms
)
662 error ("enclosing class templates are not explicitly specialized");
669 /* We've just seen template <>. */
672 begin_specialization (void)
674 begin_scope (sk_template_spec
, NULL
);
675 note_template_header (1);
676 return check_specialization_scope ();
679 /* Called at then end of processing a declaration preceded by
683 end_specialization (void)
686 reset_specialization ();
689 /* Any template <>'s that we have seen thus far are not referring to a
690 function specialization. */
693 reset_specialization (void)
695 processing_specialization
= 0;
696 template_header_count
= 0;
699 /* We've just seen a template header. If SPECIALIZATION is nonzero,
700 it was of the form template <>. */
703 note_template_header (int specialization
)
705 processing_specialization
= specialization
;
706 template_header_count
++;
709 /* We're beginning an explicit instantiation. */
712 begin_explicit_instantiation (void)
714 gcc_assert (!processing_explicit_instantiation
);
715 processing_explicit_instantiation
= true;
720 end_explicit_instantiation (void)
722 gcc_assert (processing_explicit_instantiation
);
723 processing_explicit_instantiation
= false;
726 /* An explicit specialization or partial specialization TMPL is being
727 declared. Check that the namespace in which the specialization is
728 occurring is permissible. Returns false iff it is invalid to
729 specialize TMPL in the current namespace. */
732 check_specialization_namespace (tree tmpl
)
734 tree tpl_ns
= decl_namespace_context (tmpl
);
738 An explicit specialization shall be declared in the namespace of
739 which the template is a member, or, for member templates, in the
740 namespace of which the enclosing class or enclosing class
741 template is a member. An explicit specialization of a member
742 function, member class or static data member of a class template
743 shall be declared in the namespace of which the class template is
745 if (current_scope() != DECL_CONTEXT (tmpl
)
746 && !at_namespace_scope_p ())
748 error ("specialization of %qD must appear at namespace scope", tmpl
);
751 if (is_associated_namespace (current_namespace
, tpl_ns
))
752 /* Same or super-using namespace. */
756 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
757 permerror (input_location
, " from definition of %q+#D", tmpl
);
762 /* SPEC is an explicit instantiation. Check that it is valid to
763 perform this explicit instantiation in the current namespace. */
766 check_explicit_instantiation_namespace (tree spec
)
770 /* DR 275: An explicit instantiation shall appear in an enclosing
771 namespace of its template. */
772 ns
= decl_namespace_context (spec
);
773 if (!is_ancestor (current_namespace
, ns
))
774 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
775 "(which does not enclose namespace %qD)",
776 spec
, current_namespace
, ns
);
779 /* The TYPE is being declared. If it is a template type, that means it
780 is a partial specialization. Do appropriate error-checking. */
783 maybe_process_partial_specialization (tree type
)
787 if (type
== error_mark_node
)
788 return error_mark_node
;
790 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
792 error ("name of class shadows template template parameter %qD",
794 return error_mark_node
;
797 context
= TYPE_CONTEXT (type
);
799 if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
801 /* This is for ordinary explicit specialization and partial
802 specialization of a template class such as:
804 template <> class C<int>;
808 template <class T> class C<T*>;
810 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
812 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
813 && !COMPLETE_TYPE_P (type
))
815 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
816 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
817 if (processing_template_decl
)
819 if (push_template_decl (TYPE_MAIN_DECL (type
))
821 return error_mark_node
;
824 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
825 error ("specialization of %qT after instantiation", type
);
827 else if (CLASS_TYPE_P (type
)
828 && !CLASSTYPE_USE_TEMPLATE (type
)
829 && CLASSTYPE_TEMPLATE_INFO (type
)
830 && context
&& CLASS_TYPE_P (context
)
831 && CLASSTYPE_TEMPLATE_INFO (context
))
833 /* This is for an explicit specialization of member class
834 template according to [temp.expl.spec/18]:
836 template <> template <class U> class C<int>::D;
838 The context `C<int>' must be an implicit instantiation.
839 Otherwise this is just a member class template declared
842 template <> class C<int> { template <class U> class D; };
843 template <> template <class U> class C<int>::D;
845 In the first case, `C<int>::D' is a specialization of `C<T>::D'
846 while in the second case, `C<int>::D' is a primary template
847 and `C<T>::D' may not exist. */
849 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
850 && !COMPLETE_TYPE_P (type
))
853 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
855 if (current_namespace
856 != decl_namespace_context (tmpl
))
858 permerror (input_location
, "specializing %q#T in different namespace", type
);
859 permerror (input_location
, " from definition of %q+#D", tmpl
);
862 /* Check for invalid specialization after instantiation:
864 template <> template <> class C<int>::D<int>;
865 template <> template <class U> class C<int>::D; */
867 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
868 t
; t
= TREE_CHAIN (t
))
870 tree inst
= TREE_VALUE (t
);
871 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
873 /* We already have a full specialization of this partial
874 instantiation. Reassign it to the new member
875 specialization template. */
879 elt
.tmpl
= most_general_template (tmpl
);
880 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
883 htab_remove_elt (type_specializations
, &elt
);
886 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
888 slot
= (spec_entry
**)
889 htab_find_slot (type_specializations
, &elt
, INSERT
);
890 *slot
= ggc_alloc_spec_entry ();
893 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
894 /* But if we've had an implicit instantiation, that's a
895 problem ([temp.expl.spec]/6). */
896 error ("specialization %qT after instantiation %qT",
900 /* Mark TYPE as a specialization. And as a result, we only
901 have one level of template argument for the innermost
903 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
904 CLASSTYPE_TI_ARGS (type
)
905 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
908 else if (processing_specialization
)
910 /* Someday C++0x may allow for enum template specialization. */
911 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
912 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
913 pedwarn (input_location
, OPT_pedantic
, "template specialization "
914 "of %qD not allowed by ISO C++", type
);
917 error ("explicit specialization of non-template %qT", type
);
918 return error_mark_node
;
925 /* Returns nonzero if we can optimize the retrieval of specializations
926 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
927 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
930 optimize_specialization_lookup_p (tree tmpl
)
932 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
933 && DECL_CLASS_SCOPE_P (tmpl
)
934 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
936 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
937 /* The optimized lookup depends on the fact that the
938 template arguments for the member function template apply
939 purely to the containing class, which is not true if the
940 containing class is an explicit or partial
942 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
943 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
944 && !DECL_CONV_FN_P (tmpl
)
945 /* It is possible to have a template that is not a member
946 template and is not a member of a template class:
948 template <typename T>
949 struct S { friend A::f(); };
951 Here, the friend function is a template, but the context does
952 not have template information. The optimized lookup relies
953 on having ARGS be the template arguments for both the class
954 and the function template. */
955 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
958 /* Retrieve the specialization (in the sense of [temp.spec] - a
959 specialization is either an instantiation or an explicit
960 specialization) of TMPL for the given template ARGS. If there is
961 no such specialization, return NULL_TREE. The ARGS are a vector of
962 arguments, or a vector of vectors of arguments, in the case of
963 templates with more than one level of parameters.
965 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
966 then we search for a partial specialization matching ARGS. This
967 parameter is ignored if TMPL is not a class template. */
970 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
972 if (args
== error_mark_node
)
975 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
977 /* There should be as many levels of arguments as there are
978 levels of parameters. */
979 gcc_assert (TMPL_ARGS_DEPTH (args
)
980 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
982 if (optimize_specialization_lookup_p (tmpl
))
985 tree class_specialization
;
986 VEC(tree
,gc
) *methods
;
990 /* The template arguments actually apply to the containing
991 class. Find the class specialization with those
993 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
995 = retrieve_specialization (class_template
, args
, 0);
996 if (!class_specialization
)
998 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
999 for the specialization. */
1000 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1003 /* Iterate through the methods with the indicated name, looking
1004 for the one that has an instance of TMPL. */
1005 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1006 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
1008 tree fn
= OVL_CURRENT (fns
);
1009 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1010 /* using-declarations can add base methods to the method vec,
1011 and we don't want those here. */
1012 && DECL_CONTEXT (fn
) == class_specialization
)
1021 htab_t specializations
;
1025 elt
.spec
= NULL_TREE
;
1027 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1028 specializations
= type_specializations
;
1030 specializations
= decl_specializations
;
1033 hash
= hash_specialization (&elt
);
1034 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1042 /* Like retrieve_specialization, but for local declarations. */
1045 retrieve_local_specialization (tree tmpl
)
1049 if (local_specializations
== NULL
)
1052 spec
= (tree
) htab_find_with_hash (local_specializations
, tmpl
,
1053 htab_hash_pointer (tmpl
));
1054 return spec
? TREE_PURPOSE (spec
) : NULL_TREE
;
1057 /* Returns nonzero iff DECL is a specialization of TMPL. */
1060 is_specialization_of (tree decl
, tree tmpl
)
1064 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1068 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1074 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1076 for (t
= TREE_TYPE (decl
);
1078 t
= CLASSTYPE_USE_TEMPLATE (t
)
1079 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1080 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1087 /* Returns nonzero iff DECL is a specialization of friend declaration
1088 FRIEND_DECL according to [temp.friend]. */
1091 is_specialization_of_friend (tree decl
, tree friend_decl
)
1093 bool need_template
= true;
1096 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1097 || TREE_CODE (decl
) == TYPE_DECL
);
1099 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1100 of a template class, we want to check if DECL is a specialization
1102 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1103 && DECL_TEMPLATE_INFO (friend_decl
)
1104 && !DECL_USE_TEMPLATE (friend_decl
))
1106 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1107 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1108 need_template
= false;
1110 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1111 && !PRIMARY_TEMPLATE_P (friend_decl
))
1112 need_template
= false;
1114 /* There is nothing to do if this is not a template friend. */
1115 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1118 if (is_specialization_of (decl
, friend_decl
))
1122 A member of a class template may be declared to be a friend of a
1123 non-template class. In this case, the corresponding member of
1124 every specialization of the class template is a friend of the
1125 class granting friendship.
1127 For example, given a template friend declaration
1129 template <class T> friend void A<T>::f();
1131 the member function below is considered a friend
1133 template <> struct A<int> {
1137 For this type of template friend, TEMPLATE_DEPTH below will be
1138 nonzero. To determine if DECL is a friend of FRIEND, we first
1139 check if the enclosing class is a specialization of another. */
1141 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1143 && DECL_CLASS_SCOPE_P (decl
)
1144 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1145 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1147 /* Next, we check the members themselves. In order to handle
1148 a few tricky cases, such as when FRIEND_DECL's are
1150 template <class T> friend void A<T>::g(T t);
1151 template <class T> template <T t> friend void A<T>::h();
1155 void A<int>::g(int);
1156 template <int> void A<int>::h();
1158 we need to figure out ARGS, the template arguments from
1159 the context of DECL. This is required for template substitution
1160 of `T' in the function parameter of `g' and template parameter
1161 of `h' in the above examples. Here ARGS corresponds to `int'. */
1163 tree context
= DECL_CONTEXT (decl
);
1164 tree args
= NULL_TREE
;
1165 int current_depth
= 0;
1167 while (current_depth
< template_depth
)
1169 if (CLASSTYPE_TEMPLATE_INFO (context
))
1171 if (current_depth
== 0)
1172 args
= TYPE_TI_ARGS (context
);
1174 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1177 context
= TYPE_CONTEXT (context
);
1180 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1185 tree friend_args_type
;
1186 tree decl_args_type
;
1188 /* Make sure that both DECL and FRIEND_DECL are templates or
1190 is_template
= DECL_TEMPLATE_INFO (decl
)
1191 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1192 if (need_template
^ is_template
)
1194 else if (is_template
)
1196 /* If both are templates, check template parameter list. */
1198 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1200 if (!comp_template_parms
1201 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1205 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1208 decl_type
= TREE_TYPE (decl
);
1210 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1211 tf_none
, NULL_TREE
);
1212 if (friend_type
== error_mark_node
)
1215 /* Check if return types match. */
1216 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1219 /* Check if function parameter types match, ignoring the
1220 `this' parameter. */
1221 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1222 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1223 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1224 friend_args_type
= TREE_CHAIN (friend_args_type
);
1225 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1226 decl_args_type
= TREE_CHAIN (decl_args_type
);
1228 return compparms (decl_args_type
, friend_args_type
);
1232 /* DECL is a TYPE_DECL */
1234 tree decl_type
= TREE_TYPE (decl
);
1236 /* Make sure that both DECL and FRIEND_DECL are templates or
1239 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1240 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1242 if (need_template
^ is_template
)
1244 else if (is_template
)
1247 /* If both are templates, check the name of the two
1248 TEMPLATE_DECL's first because is_friend didn't. */
1249 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1250 != DECL_NAME (friend_decl
))
1253 /* Now check template parameter list. */
1255 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1257 return comp_template_parms
1258 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1262 return (DECL_NAME (decl
)
1263 == DECL_NAME (friend_decl
));
1269 /* Register the specialization SPEC as a specialization of TMPL with
1270 the indicated ARGS. IS_FRIEND indicates whether the specialization
1271 is actually just a friend declaration. Returns SPEC, or an
1272 equivalent prior declaration, if available. */
1275 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1279 spec_entry
**slot
= NULL
;
1282 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1284 if (TREE_CODE (spec
) == FUNCTION_DECL
1285 && uses_template_parms (DECL_TI_ARGS (spec
)))
1286 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1287 register it; we want the corresponding TEMPLATE_DECL instead.
1288 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1289 the more obvious `uses_template_parms (spec)' to avoid problems
1290 with default function arguments. In particular, given
1291 something like this:
1293 template <class T> void f(T t1, T t = T())
1295 the default argument expression is not substituted for in an
1296 instantiation unless and until it is actually needed. */
1299 if (optimize_specialization_lookup_p (tmpl
))
1300 /* We don't put these specializations in the hash table, but we might
1301 want to give an error about a mismatch. */
1302 fn
= retrieve_specialization (tmpl
, args
, 0);
1310 hash
= hash_specialization (&elt
);
1312 slot
= (spec_entry
**)
1313 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1320 /* We can sometimes try to re-register a specialization that we've
1321 already got. In particular, regenerate_decl_from_template calls
1322 duplicate_decls which will update the specialization list. But,
1323 we'll still get called again here anyhow. It's more convenient
1324 to simply allow this than to try to prevent it. */
1327 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1329 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1331 if (DECL_ODR_USED (fn
)
1332 || DECL_EXPLICIT_INSTANTIATION (fn
))
1334 error ("specialization of %qD after instantiation",
1336 return error_mark_node
;
1341 /* This situation should occur only if the first
1342 specialization is an implicit instantiation, the
1343 second is an explicit specialization, and the
1344 implicit instantiation has not yet been used. That
1345 situation can occur if we have implicitly
1346 instantiated a member function and then specialized
1349 We can also wind up here if a friend declaration that
1350 looked like an instantiation turns out to be a
1353 template <class T> void foo(T);
1354 class S { friend void foo<>(int) };
1355 template <> void foo(int);
1357 We transform the existing DECL in place so that any
1358 pointers to it become pointers to the updated
1361 If there was a definition for the template, but not
1362 for the specialization, we want this to look as if
1363 there were no definition, and vice versa. */
1364 DECL_INITIAL (fn
) = NULL_TREE
;
1365 duplicate_decls (spec
, fn
, is_friend
);
1366 /* The call to duplicate_decls will have applied
1369 An explicit specialization of a function template
1370 is inline only if it is explicitly declared to be,
1371 and independently of whether its function template
1374 to the primary function; now copy the inline bits to
1375 the various clones. */
1376 FOR_EACH_CLONE (clone
, fn
)
1378 DECL_DECLARED_INLINE_P (clone
)
1379 = DECL_DECLARED_INLINE_P (fn
);
1380 DECL_SOURCE_LOCATION (clone
)
1381 = DECL_SOURCE_LOCATION (fn
);
1383 check_specialization_namespace (fn
);
1388 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1390 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1391 /* Dup decl failed, but this is a new definition. Set the
1392 line number so any errors match this new
1394 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1400 return duplicate_decls (spec
, fn
, is_friend
);
1402 /* A specialization must be declared in the same namespace as the
1403 template it is specializing. */
1404 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1405 && !check_specialization_namespace (tmpl
))
1406 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1408 if (!optimize_specialization_lookup_p (tmpl
))
1410 gcc_assert (tmpl
&& args
&& spec
);
1411 *slot
= ggc_alloc_spec_entry ();
1413 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1414 && PRIMARY_TEMPLATE_P (tmpl
)
1415 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1416 /* TMPL is a forward declaration of a template function; keep a list
1417 of all specializations in case we need to reassign them to a friend
1418 template later in tsubst_friend_function. */
1419 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1420 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1426 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1427 TMPL and ARGS members, ignores SPEC. */
1430 eq_specializations (const void *p1
, const void *p2
)
1432 const spec_entry
*e1
= (const spec_entry
*)p1
;
1433 const spec_entry
*e2
= (const spec_entry
*)p2
;
1435 return (e1
->tmpl
== e2
->tmpl
1436 && comp_template_args (e1
->args
, e2
->args
));
1439 /* Returns a hash for a template TMPL and template arguments ARGS. */
1442 hash_tmpl_and_args (tree tmpl
, tree args
)
1444 hashval_t val
= DECL_UID (tmpl
);
1445 return iterative_hash_template_arg (args
, val
);
1448 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1452 hash_specialization (const void *p
)
1454 const spec_entry
*e
= (const spec_entry
*)p
;
1455 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1458 /* Recursively calculate a hash value for a template argument ARG, for use
1459 in the hash tables of template specializations. */
1462 iterative_hash_template_arg (tree arg
, hashval_t val
)
1464 unsigned HOST_WIDE_INT i
;
1465 enum tree_code code
;
1468 if (arg
== NULL_TREE
)
1469 return iterative_hash_object (arg
, val
);
1474 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1475 /* We can get one of these when re-hashing a previous entry in the middle
1476 of substituting into a pack expansion. Just look through it. */
1477 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1479 code
= TREE_CODE (arg
);
1480 tclass
= TREE_CODE_CLASS (code
);
1482 val
= iterative_hash_object (code
, val
);
1489 case IDENTIFIER_NODE
:
1490 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1494 int i
, len
= TREE_VEC_LENGTH (arg
);
1495 for (i
= 0; i
< len
; ++i
)
1496 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1500 case TYPE_PACK_EXPANSION
:
1501 case EXPR_PACK_EXPANSION
:
1502 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1504 case TYPE_ARGUMENT_PACK
:
1505 case NONTYPE_ARGUMENT_PACK
:
1506 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1509 for (; arg
; arg
= TREE_CHAIN (arg
))
1510 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1514 for (; arg
; arg
= OVL_CHAIN (arg
))
1515 val
= iterative_hash_template_arg (OVL_FUNCTION (arg
), val
);
1521 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1523 val
= iterative_hash_template_arg (field
, val
);
1524 val
= iterative_hash_template_arg (value
, val
);
1530 if (!DECL_ARTIFICIAL (arg
))
1531 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1532 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1535 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1538 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1539 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1541 case TEMPLATE_PARM_INDEX
:
1542 val
= iterative_hash_template_arg
1543 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1544 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1545 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1548 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1549 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1550 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1553 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1555 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1559 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1560 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1561 val
= iterative_hash_object (code
, val
);
1562 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1565 /* layout_type sets structural equality for arrays of
1566 incomplete type, so we can't rely on the canonical type
1568 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1569 return iterative_hash_template_arg (TYPE_DOMAIN (arg
), val
);
1572 /* A lambda can't appear in a template arg, but don't crash on
1574 gcc_assert (seen_error ());
1578 case STATIC_CAST_EXPR
:
1579 case REINTERPRET_CAST_EXPR
:
1580 case CONST_CAST_EXPR
:
1581 case DYNAMIC_CAST_EXPR
:
1583 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1584 /* Now hash operands as usual. */
1594 if (TYPE_CANONICAL (arg
))
1595 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1597 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1598 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1599 /* Otherwise just compare the types during lookup. */
1602 case tcc_declaration
:
1604 return iterative_hash_expr (arg
, val
);
1607 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1609 unsigned n
= TREE_OPERAND_LENGTH (arg
);
1610 for (i
= 0; i
< n
; ++i
)
1611 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1619 /* Unregister the specialization SPEC as a specialization of TMPL.
1620 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1621 if the SPEC was listed as a specialization of TMPL.
1623 Note that SPEC has been ggc_freed, so we can't look inside it. */
1626 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1631 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1632 elt
.args
= TI_ARGS (tinfo
);
1633 elt
.spec
= NULL_TREE
;
1635 slot
= (spec_entry
**) htab_find_slot (decl_specializations
, &elt
, INSERT
);
1638 gcc_assert ((*slot
)->spec
== spec
|| (*slot
)->spec
== new_spec
);
1639 gcc_assert (new_spec
!= NULL_TREE
);
1640 (*slot
)->spec
= new_spec
;
1647 /* Compare an entry in the local specializations hash table P1 (which
1648 is really a pointer to a TREE_LIST) with P2 (which is really a
1652 eq_local_specializations (const void *p1
, const void *p2
)
1654 return TREE_VALUE ((const_tree
) p1
) == (const_tree
) p2
;
1657 /* Hash P1, an entry in the local specializations table. */
1660 hash_local_specialization (const void* p1
)
1662 return htab_hash_pointer (TREE_VALUE ((const_tree
) p1
));
1665 /* Like register_specialization, but for local declarations. We are
1666 registering SPEC, an instantiation of TMPL. */
1669 register_local_specialization (tree spec
, tree tmpl
)
1673 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1674 htab_hash_pointer (tmpl
), INSERT
);
1675 *slot
= build_tree_list (spec
, tmpl
);
1678 /* TYPE is a class type. Returns true if TYPE is an explicitly
1679 specialized class. */
1682 explicit_class_specialization_p (tree type
)
1684 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1686 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1689 /* Print the list of functions at FNS, going through all the overloads
1690 for each element of the list. Alternatively, FNS can not be a
1691 TREE_LIST, in which case it will be printed together with all the
1694 MORE and *STR should respectively be FALSE and NULL when the function
1695 is called from the outside. They are used internally on recursive
1696 calls. print_candidates manages the two parameters and leaves NULL
1697 in *STR when it ends. */
1700 print_candidates_1 (tree fns
, bool more
, const char **str
)
1703 char *spaces
= NULL
;
1705 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1706 if (TREE_CODE (fn
) == TREE_LIST
)
1708 gcc_assert (!OVL_NEXT (fn
) && !is_overloaded_fn (fn
));
1709 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1710 print_candidates_1 (TREE_VALUE (fn2
),
1711 TREE_CHAIN (fn2
) || more
, str
);
1717 /* Pick the prefix string. */
1718 if (!more
&& !OVL_NEXT (fns
))
1720 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1724 *str
= _("candidates are:");
1725 spaces
= get_spaces (*str
);
1727 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1728 *str
= spaces
? spaces
: *str
;
1738 /* Print the list of candidate FNS in an error message. */
1741 print_candidates (tree fns
)
1743 const char *str
= NULL
;
1744 print_candidates_1 (fns
, false, &str
);
1745 gcc_assert (str
== NULL
);
1748 /* Returns the template (one of the functions given by TEMPLATE_ID)
1749 which can be specialized to match the indicated DECL with the
1750 explicit template args given in TEMPLATE_ID. The DECL may be
1751 NULL_TREE if none is available. In that case, the functions in
1752 TEMPLATE_ID are non-members.
1754 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1755 specialization of a member template.
1757 The TEMPLATE_COUNT is the number of references to qualifying
1758 template classes that appeared in the name of the function. See
1759 check_explicit_specialization for a more accurate description.
1761 TSK indicates what kind of template declaration (if any) is being
1762 declared. TSK_TEMPLATE indicates that the declaration given by
1763 DECL, though a FUNCTION_DECL, has template parameters, and is
1764 therefore a template function.
1766 The template args (those explicitly specified and those deduced)
1767 are output in a newly created vector *TARGS_OUT.
1769 If it is impossible to determine the result, an error message is
1770 issued. The error_mark_node is returned to indicate failure. */
1773 determine_specialization (tree template_id
,
1776 int need_member_template
,
1782 tree explicit_targs
;
1783 tree candidates
= NULL_TREE
;
1784 /* A TREE_LIST of templates of which DECL may be a specialization.
1785 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1786 corresponding TREE_PURPOSE is the set of template arguments that,
1787 when used to instantiate the template, would produce a function
1788 with the signature of DECL. */
1789 tree templates
= NULL_TREE
;
1791 struct cp_binding_level
*b
;
1793 *targs_out
= NULL_TREE
;
1795 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1796 return error_mark_node
;
1798 fns
= TREE_OPERAND (template_id
, 0);
1799 explicit_targs
= TREE_OPERAND (template_id
, 1);
1801 if (fns
== error_mark_node
)
1802 return error_mark_node
;
1804 /* Check for baselinks. */
1805 if (BASELINK_P (fns
))
1806 fns
= BASELINK_FUNCTIONS (fns
);
1808 if (!is_overloaded_fn (fns
))
1810 error ("%qD is not a function template", fns
);
1811 return error_mark_node
;
1814 /* Count the number of template headers specified for this
1817 for (b
= current_binding_level
;
1818 b
->kind
== sk_template_parms
;
1822 for (; fns
; fns
= OVL_NEXT (fns
))
1824 tree fn
= OVL_CURRENT (fns
);
1826 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1828 tree decl_arg_types
;
1831 /* In case of explicit specialization, we need to check if
1832 the number of template headers appearing in the specialization
1833 is correct. This is usually done in check_explicit_specialization,
1834 but the check done there cannot be exhaustive when specializing
1835 member functions. Consider the following code:
1837 template <> void A<int>::f(int);
1838 template <> template <> void A<int>::f(int);
1840 Assuming that A<int> is not itself an explicit specialization
1841 already, the first line specializes "f" which is a non-template
1842 member function, whilst the second line specializes "f" which
1843 is a template member function. So both lines are syntactically
1844 correct, and check_explicit_specialization does not reject
1847 Here, we can do better, as we are matching the specialization
1848 against the declarations. We count the number of template
1849 headers, and we check if they match TEMPLATE_COUNT + 1
1850 (TEMPLATE_COUNT is the number of qualifying template classes,
1851 plus there must be another header for the member template
1854 Notice that if header_count is zero, this is not a
1855 specialization but rather a template instantiation, so there
1856 is no check we can perform here. */
1857 if (header_count
&& header_count
!= template_count
+ 1)
1860 /* Check that the number of template arguments at the
1861 innermost level for DECL is the same as for FN. */
1862 if (current_binding_level
->kind
== sk_template_parms
1863 && !current_binding_level
->explicit_spec_p
1864 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1865 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1866 (current_template_parms
))))
1869 /* DECL might be a specialization of FN. */
1870 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1871 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1873 /* For a non-static member function, we need to make sure
1874 that the const qualification is the same. Since
1875 get_bindings does not try to merge the "this" parameter,
1876 we must do the comparison explicitly. */
1877 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1878 && !same_type_p (TREE_VALUE (fn_arg_types
),
1879 TREE_VALUE (decl_arg_types
)))
1882 /* Skip the "this" parameter and, for constructors of
1883 classes with virtual bases, the VTT parameter. A
1884 full specialization of a constructor will have a VTT
1885 parameter, but a template never will. */
1887 = skip_artificial_parms_for (decl
, decl_arg_types
);
1889 = skip_artificial_parms_for (fn
, fn_arg_types
);
1891 /* Check that the number of function parameters matches.
1893 template <class T> void f(int i = 0);
1894 template <> void f<int>();
1895 The specialization f<int> is invalid but is not caught
1896 by get_bindings below. */
1897 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1900 /* Function templates cannot be specializations; there are
1901 no partial specializations of functions. Therefore, if
1902 the type of DECL does not match FN, there is no
1904 if (tsk
== tsk_template
)
1906 if (compparms (fn_arg_types
, decl_arg_types
))
1907 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1911 /* See whether this function might be a specialization of this
1913 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1916 /* We cannot deduce template arguments that when used to
1917 specialize TMPL will produce DECL. */
1920 /* Save this template, and the arguments deduced. */
1921 templates
= tree_cons (targs
, fn
, templates
);
1923 else if (need_member_template
)
1924 /* FN is an ordinary member function, and we need a
1925 specialization of a member template. */
1927 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1928 /* We can get IDENTIFIER_NODEs here in certain erroneous
1931 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1932 /* This is just an ordinary non-member function. Nothing can
1933 be a specialization of that. */
1935 else if (DECL_ARTIFICIAL (fn
))
1936 /* Cannot specialize functions that are created implicitly. */
1940 tree decl_arg_types
;
1942 /* This is an ordinary member function. However, since
1943 we're here, we can assume it's enclosing class is a
1944 template class. For example,
1946 template <typename T> struct S { void f(); };
1947 template <> void S<int>::f() {}
1949 Here, S<int>::f is a non-template, but S<int> is a
1950 template class. If FN has the same type as DECL, we
1951 might be in business. */
1953 if (!DECL_TEMPLATE_INFO (fn
))
1954 /* Its enclosing class is an explicit specialization
1955 of a template class. This is not a candidate. */
1958 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1959 TREE_TYPE (TREE_TYPE (fn
))))
1960 /* The return types differ. */
1963 /* Adjust the type of DECL in case FN is a static member. */
1964 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1965 if (DECL_STATIC_FUNCTION_P (fn
)
1966 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1967 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1969 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1972 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1976 if (templates
&& TREE_CHAIN (templates
))
1982 It is possible for a specialization with a given function
1983 signature to be instantiated from more than one function
1984 template. In such cases, explicit specification of the
1985 template arguments must be used to uniquely identify the
1986 function template specialization being specialized.
1988 Note that here, there's no suggestion that we're supposed to
1989 determine which of the candidate templates is most
1990 specialized. However, we, also have:
1994 Partial ordering of overloaded function template
1995 declarations is used in the following contexts to select
1996 the function template to which a function template
1997 specialization refers:
1999 -- when an explicit specialization refers to a function
2002 So, we do use the partial ordering rules, at least for now.
2003 This extension can only serve to make invalid programs valid,
2004 so it's safe. And, there is strong anecdotal evidence that
2005 the committee intended the partial ordering rules to apply;
2006 the EDG front end has that behavior, and John Spicer claims
2007 that the committee simply forgot to delete the wording in
2008 [temp.expl.spec]. */
2009 tree tmpl
= most_specialized_instantiation (templates
);
2010 if (tmpl
!= error_mark_node
)
2013 TREE_CHAIN (templates
) = NULL_TREE
;
2017 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2019 error ("template-id %qD for %q+D does not match any template "
2020 "declaration", template_id
, decl
);
2021 if (header_count
&& header_count
!= template_count
+ 1)
2022 inform (input_location
, "saw %d %<template<>%>, need %d for "
2023 "specializing a member function template",
2024 header_count
, template_count
+ 1);
2025 return error_mark_node
;
2027 else if ((templates
&& TREE_CHAIN (templates
))
2028 || (candidates
&& TREE_CHAIN (candidates
))
2029 || (templates
&& candidates
))
2031 error ("ambiguous template specialization %qD for %q+D",
2033 candidates
= chainon (candidates
, templates
);
2034 print_candidates (candidates
);
2035 return error_mark_node
;
2038 /* We have one, and exactly one, match. */
2041 tree fn
= TREE_VALUE (candidates
);
2042 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2043 /* DECL is a re-declaration or partial instantiation of a template
2045 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2047 /* It was a specialization of an ordinary member function in a
2049 return DECL_TI_TEMPLATE (fn
);
2052 /* It was a specialization of a template. */
2053 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2054 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2056 *targs_out
= copy_node (targs
);
2057 SET_TMPL_ARGS_LEVEL (*targs_out
,
2058 TMPL_ARGS_DEPTH (*targs_out
),
2059 TREE_PURPOSE (templates
));
2062 *targs_out
= TREE_PURPOSE (templates
);
2063 return TREE_VALUE (templates
);
2066 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2067 but with the default argument values filled in from those in the
2071 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2074 tree new_spec_types
;
2079 if (spec_types
== void_list_node
)
2080 return void_list_node
;
2082 /* Substitute into the rest of the list. */
2084 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2085 TREE_CHAIN (tmpl_types
));
2087 /* Add the default argument for this parameter. */
2088 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2089 TREE_VALUE (spec_types
),
2093 /* DECL is an explicit specialization. Replicate default arguments
2094 from the template it specializes. (That way, code like:
2096 template <class T> void f(T = 3);
2097 template <> void f(double);
2100 works, as required.) An alternative approach would be to look up
2101 the correct default arguments at the call-site, but this approach
2102 is consistent with how implicit instantiations are handled. */
2105 copy_default_args_to_explicit_spec (tree decl
)
2110 tree new_spec_types
;
2114 tree object_type
= NULL_TREE
;
2115 tree in_charge
= NULL_TREE
;
2116 tree vtt
= NULL_TREE
;
2118 /* See if there's anything we need to do. */
2119 tmpl
= DECL_TI_TEMPLATE (decl
);
2120 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2121 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2122 if (TREE_PURPOSE (t
))
2127 old_type
= TREE_TYPE (decl
);
2128 spec_types
= TYPE_ARG_TYPES (old_type
);
2130 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2132 /* Remove the this pointer, but remember the object's type for
2134 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2135 spec_types
= TREE_CHAIN (spec_types
);
2136 tmpl_types
= TREE_CHAIN (tmpl_types
);
2138 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2140 /* DECL may contain more parameters than TMPL due to the extra
2141 in-charge parameter in constructors and destructors. */
2142 in_charge
= spec_types
;
2143 spec_types
= TREE_CHAIN (spec_types
);
2145 if (DECL_HAS_VTT_PARM_P (decl
))
2148 spec_types
= TREE_CHAIN (spec_types
);
2152 /* Compute the merged default arguments. */
2154 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2156 /* Compute the new FUNCTION_TYPE. */
2160 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2165 /* Put the in-charge parameter back. */
2166 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2167 TREE_VALUE (in_charge
),
2170 new_type
= build_method_type_directly (object_type
,
2171 TREE_TYPE (old_type
),
2175 new_type
= build_function_type (TREE_TYPE (old_type
),
2177 new_type
= cp_build_type_attribute_variant (new_type
,
2178 TYPE_ATTRIBUTES (old_type
));
2179 new_type
= build_exception_variant (new_type
,
2180 TYPE_RAISES_EXCEPTIONS (old_type
));
2181 TREE_TYPE (decl
) = new_type
;
2184 /* Check to see if the function just declared, as indicated in
2185 DECLARATOR, and in DECL, is a specialization of a function
2186 template. We may also discover that the declaration is an explicit
2187 instantiation at this point.
2189 Returns DECL, or an equivalent declaration that should be used
2190 instead if all goes well. Issues an error message if something is
2191 amiss. Returns error_mark_node if the error is not easily
2194 FLAGS is a bitmask consisting of the following flags:
2196 2: The function has a definition.
2197 4: The function is a friend.
2199 The TEMPLATE_COUNT is the number of references to qualifying
2200 template classes that appeared in the name of the function. For
2203 template <class T> struct S { void f(); };
2206 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2207 classes are not counted in the TEMPLATE_COUNT, so that in
2209 template <class T> struct S {};
2210 template <> struct S<int> { void f(); }
2211 template <> void S<int>::f();
2213 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2214 invalid; there should be no template <>.)
2216 If the function is a specialization, it is marked as such via
2217 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2218 is set up correctly, and it is added to the list of specializations
2219 for that template. */
2222 check_explicit_specialization (tree declarator
,
2227 int have_def
= flags
& 2;
2228 int is_friend
= flags
& 4;
2229 int specialization
= 0;
2230 int explicit_instantiation
= 0;
2231 int member_specialization
= 0;
2232 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2233 tree dname
= DECL_NAME (decl
);
2238 if (!processing_specialization
)
2241 tsk
= tsk_excessive_parms
;
2244 tsk
= current_tmpl_spec_kind (template_count
);
2249 if (processing_specialization
)
2252 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2254 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2257 /* This could be something like:
2259 template <class T> void f(T);
2260 class S { friend void f<>(int); } */
2264 /* This case handles bogus declarations like template <>
2265 template <class T> void f<int>(); */
2267 error ("template-id %qD in declaration of primary template",
2274 case tsk_invalid_member_spec
:
2275 /* The error has already been reported in
2276 check_specialization_scope. */
2277 return error_mark_node
;
2279 case tsk_invalid_expl_inst
:
2280 error ("template parameter list used in explicit instantiation");
2286 error ("definition provided for explicit instantiation");
2288 explicit_instantiation
= 1;
2291 case tsk_excessive_parms
:
2292 case tsk_insufficient_parms
:
2293 if (tsk
== tsk_excessive_parms
)
2294 error ("too many template parameter lists in declaration of %qD",
2296 else if (template_header_count
)
2297 error("too few template parameter lists in declaration of %qD", decl
);
2299 error("explicit specialization of %qD must be introduced by "
2300 "%<template <>%>", decl
);
2304 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2306 member_specialization
= 1;
2312 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2314 /* This case handles bogus declarations like template <>
2315 template <class T> void f<int>(); */
2317 if (uses_template_parms (declarator
))
2318 error ("function template partial specialization %qD "
2319 "is not allowed", declarator
);
2321 error ("template-id %qD in declaration of primary template",
2326 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2327 /* This is a specialization of a member template, without
2328 specialization the containing class. Something like:
2330 template <class T> struct S {
2331 template <class U> void f (U);
2333 template <> template <class U> void S<int>::f(U) {}
2335 That's a specialization -- but of the entire template. */
2343 if (specialization
|| member_specialization
)
2345 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2346 for (; t
; t
= TREE_CHAIN (t
))
2347 if (TREE_PURPOSE (t
))
2349 permerror (input_location
,
2350 "default argument specified in explicit specialization");
2355 if (specialization
|| member_specialization
|| explicit_instantiation
)
2357 tree tmpl
= NULL_TREE
;
2358 tree targs
= NULL_TREE
;
2360 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2361 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2365 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2370 /* If there is no class context, the explicit instantiation
2371 must be at namespace scope. */
2372 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2374 /* Find the namespace binding, using the declaration
2376 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2378 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2380 error ("%qD is not a template function", dname
);
2381 fns
= error_mark_node
;
2385 tree fn
= OVL_CURRENT (fns
);
2386 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2387 CP_DECL_CONTEXT (fn
)))
2388 error ("%qD is not declared in %qD",
2389 decl
, current_namespace
);
2393 declarator
= lookup_template_function (fns
, NULL_TREE
);
2396 if (declarator
== error_mark_node
)
2397 return error_mark_node
;
2399 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2401 if (!explicit_instantiation
)
2402 /* A specialization in class scope. This is invalid,
2403 but the error will already have been flagged by
2404 check_specialization_scope. */
2405 return error_mark_node
;
2408 /* It's not valid to write an explicit instantiation in
2411 class C { template void f(); }
2413 This case is caught by the parser. However, on
2416 template class C { void f(); };
2418 (which is invalid) we can get here. The error will be
2425 else if (ctype
!= NULL_TREE
2426 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2429 /* Find the list of functions in ctype that have the same
2430 name as the declared function. */
2431 tree name
= TREE_OPERAND (declarator
, 0);
2432 tree fns
= NULL_TREE
;
2435 if (constructor_name_p (name
, ctype
))
2437 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2439 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2440 : !CLASSTYPE_DESTRUCTORS (ctype
))
2442 /* From [temp.expl.spec]:
2444 If such an explicit specialization for the member
2445 of a class template names an implicitly-declared
2446 special member function (clause _special_), the
2447 program is ill-formed.
2449 Similar language is found in [temp.explicit]. */
2450 error ("specialization of implicitly-declared special member function");
2451 return error_mark_node
;
2454 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2457 if (!DECL_CONV_FN_P (decl
))
2459 idx
= lookup_fnfields_1 (ctype
, name
);
2461 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2465 VEC(tree
,gc
) *methods
;
2468 /* For a type-conversion operator, we cannot do a
2469 name-based lookup. We might be looking for `operator
2470 int' which will be a specialization of `operator T'.
2471 So, we find *all* the conversion operators, and then
2472 select from them. */
2475 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2477 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2478 VEC_iterate (tree
, methods
, idx
, ovl
);
2481 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2482 /* There are no more conversion functions. */
2485 /* Glue all these conversion functions together
2486 with those we already have. */
2487 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2488 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2492 if (fns
== NULL_TREE
)
2494 error ("no member function %qD declared in %qT", name
, ctype
);
2495 return error_mark_node
;
2498 TREE_OPERAND (declarator
, 0) = fns
;
2501 /* Figure out what exactly is being specialized at this point.
2502 Note that for an explicit instantiation, even one for a
2503 member function, we cannot tell apriori whether the
2504 instantiation is for a member template, or just a member
2505 function of a template class. Even if a member template is
2506 being instantiated, the member template arguments may be
2507 elided if they can be deduced from the rest of the
2509 tmpl
= determine_specialization (declarator
, decl
,
2511 member_specialization
,
2515 if (!tmpl
|| tmpl
== error_mark_node
)
2516 /* We couldn't figure out what this declaration was
2518 return error_mark_node
;
2521 tree gen_tmpl
= most_general_template (tmpl
);
2523 if (explicit_instantiation
)
2525 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2526 is done by do_decl_instantiation later. */
2528 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2529 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2531 if (arg_depth
> parm_depth
)
2533 /* If TMPL is not the most general template (for
2534 example, if TMPL is a friend template that is
2535 injected into namespace scope), then there will
2536 be too many levels of TARGS. Remove some of them
2541 new_targs
= make_tree_vec (parm_depth
);
2542 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2543 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2544 = TREE_VEC_ELT (targs
, i
);
2548 return instantiate_template (tmpl
, targs
, tf_error
);
2551 /* If we thought that the DECL was a member function, but it
2552 turns out to be specializing a static member function,
2553 make DECL a static member function as well. */
2554 if (DECL_STATIC_FUNCTION_P (tmpl
)
2555 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2556 revert_static_member_fn (decl
);
2558 /* If this is a specialization of a member template of a
2559 template class, we want to return the TEMPLATE_DECL, not
2560 the specialization of it. */
2561 if (tsk
== tsk_template
)
2563 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2564 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2565 DECL_INITIAL (result
) = NULL_TREE
;
2569 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2570 DECL_SOURCE_LOCATION (result
)
2571 = DECL_SOURCE_LOCATION (decl
);
2572 /* We want to use the argument list specified in the
2573 definition, not in the original declaration. */
2574 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2575 for (parm
= DECL_ARGUMENTS (result
); parm
;
2576 parm
= DECL_CHAIN (parm
))
2577 DECL_CONTEXT (parm
) = result
;
2579 return register_specialization (tmpl
, gen_tmpl
, targs
,
2583 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2584 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2586 /* Inherit default function arguments from the template
2587 DECL is specializing. */
2588 copy_default_args_to_explicit_spec (decl
);
2590 /* This specialization has the same protection as the
2591 template it specializes. */
2592 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2593 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2595 /* 7.1.1-1 [dcl.stc]
2597 A storage-class-specifier shall not be specified in an
2598 explicit specialization...
2600 The parser rejects these, so unless action is taken here,
2601 explicit function specializations will always appear with
2604 The action recommended by the C++ CWG in response to C++
2605 defect report 605 is to make the storage class and linkage
2606 of the explicit specialization match the templated function:
2608 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2610 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2612 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2613 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2615 /* This specialization has the same linkage and visibility as
2616 the function template it specializes. */
2617 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2618 if (! TREE_PUBLIC (decl
))
2620 DECL_INTERFACE_KNOWN (decl
) = 1;
2621 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2623 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2624 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2626 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2627 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2631 /* If DECL is a friend declaration, declared using an
2632 unqualified name, the namespace associated with DECL may
2633 have been set incorrectly. For example, in:
2635 template <typename T> void f(T);
2637 struct S { friend void f<int>(int); }
2640 we will have set the DECL_CONTEXT for the friend
2641 declaration to N, rather than to the global namespace. */
2642 if (DECL_NAMESPACE_SCOPE_P (decl
))
2643 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2645 if (is_friend
&& !have_def
)
2646 /* This is not really a declaration of a specialization.
2647 It's just the name of an instantiation. But, it's not
2648 a request for an instantiation, either. */
2649 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2650 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2651 /* This is indeed a specialization. In case of constructors
2652 and destructors, we need in-charge and not-in-charge
2653 versions in V3 ABI. */
2654 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2656 /* Register this specialization so that we can find it
2658 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2665 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2666 parameters. These are represented in the same format used for
2667 DECL_TEMPLATE_PARMS. */
2670 comp_template_parms (const_tree parms1
, const_tree parms2
)
2675 if (parms1
== parms2
)
2678 for (p1
= parms1
, p2
= parms2
;
2679 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2680 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2682 tree t1
= TREE_VALUE (p1
);
2683 tree t2
= TREE_VALUE (p2
);
2686 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2687 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2689 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2692 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2694 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2695 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2697 /* If either of the template parameters are invalid, assume
2698 they match for the sake of error recovery. */
2699 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2702 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2705 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2706 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2707 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2709 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2714 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2715 /* One set of parameters has more parameters lists than the
2722 /* Determine whether PARM is a parameter pack. */
2725 template_parameter_pack_p (const_tree parm
)
2727 /* Determine if we have a non-type template parameter pack. */
2728 if (TREE_CODE (parm
) == PARM_DECL
)
2729 return (DECL_TEMPLATE_PARM_P (parm
)
2730 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2732 /* If this is a list of template parameters, we could get a
2733 TYPE_DECL or a TEMPLATE_DECL. */
2734 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2735 parm
= TREE_TYPE (parm
);
2737 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2738 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2739 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2742 /* Determine if T is a function parameter pack. */
2745 function_parameter_pack_p (const_tree t
)
2747 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2748 return FUNCTION_PARAMETER_PACK_P (t
);
2752 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2753 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2756 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2758 if (! primary_func_tmpl_inst
2759 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2760 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2763 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2766 /* Return true iff the function parameter PARAM_DECL was expanded
2767 from the function parameter pack PACK. */
2770 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2772 if (DECL_ARTIFICIAL (param_decl
)
2773 || !function_parameter_pack_p (pack
))
2776 /* The parameter pack and its pack arguments have the same
2778 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2781 /* Determine whether ARGS describes a variadic template args list,
2782 i.e., one that is terminated by a template argument pack. */
2785 template_args_variadic_p (tree args
)
2790 if (args
== NULL_TREE
)
2793 args
= INNERMOST_TEMPLATE_ARGS (args
);
2794 nargs
= TREE_VEC_LENGTH (args
);
2799 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2801 return ARGUMENT_PACK_P (last_parm
);
2804 /* Generate a new name for the parameter pack name NAME (an
2805 IDENTIFIER_NODE) that incorporates its */
2808 make_ith_pack_parameter_name (tree name
, int i
)
2810 /* Munge the name to include the parameter index. */
2811 #define NUMBUF_LEN 128
2812 char numbuf
[NUMBUF_LEN
];
2816 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2817 newname_len
= IDENTIFIER_LENGTH (name
)
2818 + strlen (numbuf
) + 2;
2819 newname
= (char*)alloca (newname_len
);
2820 snprintf (newname
, newname_len
,
2821 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2822 return get_identifier (newname
);
2825 /* Return true if T is a primary function
2826 or class template instantiation. */
2829 primary_template_instantiation_p (const_tree t
)
2834 if (TREE_CODE (t
) == FUNCTION_DECL
)
2835 return DECL_LANG_SPECIFIC (t
)
2836 && DECL_TEMPLATE_INSTANTIATION (t
)
2837 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2838 else if (CLASS_TYPE_P (t
))
2839 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2840 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2844 /* Return true if PARM is a template template parameter. */
2847 template_template_parameter_p (const_tree parm
)
2849 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2852 /* Return the template parameters of T if T is a
2853 primary template instantiation, NULL otherwise. */
2856 get_primary_template_innermost_parameters (const_tree t
)
2858 tree parms
= NULL
, template_info
= NULL
;
2860 if ((template_info
= get_template_info (t
))
2861 && primary_template_instantiation_p (t
))
2862 parms
= INNERMOST_TEMPLATE_PARMS
2863 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2868 /* Return the template parameters of the LEVELth level from the full list
2869 of template parameters PARMS. */
2872 get_template_parms_at_level (tree parms
, int level
)
2876 || TREE_CODE (parms
) != TREE_LIST
2877 || level
> TMPL_PARMS_DEPTH (parms
))
2880 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2881 if (TMPL_PARMS_DEPTH (p
) == level
)
2887 /* Returns the template arguments of T if T is a template instantiation,
2891 get_template_innermost_arguments (const_tree t
)
2893 tree args
= NULL
, template_info
= NULL
;
2895 if ((template_info
= get_template_info (t
))
2896 && TI_ARGS (template_info
))
2897 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2902 /* Return the argument pack elements of T if T is a template argument pack,
2906 get_template_argument_pack_elems (const_tree t
)
2908 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2909 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2912 return ARGUMENT_PACK_ARGS (t
);
2915 /* Structure used to track the progress of find_parameter_packs_r. */
2916 struct find_parameter_pack_data
2918 /* TREE_LIST that will contain all of the parameter packs found by
2920 tree
* parameter_packs
;
2922 /* Set of AST nodes that have been visited by the traversal. */
2923 struct pointer_set_t
*visited
;
2926 /* Identifies all of the argument packs that occur in a template
2927 argument and appends them to the TREE_LIST inside DATA, which is a
2928 find_parameter_pack_data structure. This is a subroutine of
2929 make_pack_expansion and uses_parameter_packs. */
2931 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2934 struct find_parameter_pack_data
* ppd
=
2935 (struct find_parameter_pack_data
*)data
;
2936 bool parameter_pack_p
= false;
2938 /* Identify whether this is a parameter pack or not. */
2939 switch (TREE_CODE (t
))
2941 case TEMPLATE_PARM_INDEX
:
2942 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2943 parameter_pack_p
= true;
2946 case TEMPLATE_TYPE_PARM
:
2947 case TEMPLATE_TEMPLATE_PARM
:
2948 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2949 parameter_pack_p
= true;
2953 if (FUNCTION_PARAMETER_PACK_P (t
))
2955 /* We don't want to walk into the type of a PARM_DECL,
2956 because we don't want to see the type parameter pack. */
2958 parameter_pack_p
= true;
2963 /* Not a parameter pack. */
2967 if (parameter_pack_p
)
2969 /* Add this parameter pack to the list. */
2970 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
2974 cp_walk_tree (&TYPE_CONTEXT (t
),
2975 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2977 /* This switch statement will return immediately if we don't find a
2979 switch (TREE_CODE (t
))
2981 case TEMPLATE_PARM_INDEX
:
2984 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2985 /* Check the template itself. */
2986 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
2987 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2988 /* Check the template arguments. */
2989 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
2994 case TEMPLATE_TYPE_PARM
:
2995 case TEMPLATE_TEMPLATE_PARM
:
3002 if (TYPE_PTRMEMFUNC_P (t
))
3008 if (TYPE_TEMPLATE_INFO (t
))
3009 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3010 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3016 cp_walk_tree (&TREE_TYPE (t
),
3017 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3021 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3026 case TYPE_PACK_EXPANSION
:
3027 case EXPR_PACK_EXPANSION
:
3032 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3037 case IDENTIFIER_NODE
:
3038 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3050 /* Determines if the expression or type T uses any parameter packs. */
3052 uses_parameter_packs (tree t
)
3054 tree parameter_packs
= NULL_TREE
;
3055 struct find_parameter_pack_data ppd
;
3056 ppd
.parameter_packs
= ¶meter_packs
;
3057 ppd
.visited
= pointer_set_create ();
3058 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3059 pointer_set_destroy (ppd
.visited
);
3060 return parameter_packs
!= NULL_TREE
;
3063 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3064 representation a base-class initializer into a parameter pack
3065 expansion. If all goes well, the resulting node will be an
3066 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3069 make_pack_expansion (tree arg
)
3072 tree parameter_packs
= NULL_TREE
;
3073 bool for_types
= false;
3074 struct find_parameter_pack_data ppd
;
3076 if (!arg
|| arg
== error_mark_node
)
3079 if (TREE_CODE (arg
) == TREE_LIST
)
3081 /* The only time we will see a TREE_LIST here is for a base
3082 class initializer. In this case, the TREE_PURPOSE will be a
3083 _TYPE node (representing the base class expansion we're
3084 initializing) and the TREE_VALUE will be a TREE_LIST
3085 containing the initialization arguments.
3087 The resulting expansion looks somewhat different from most
3088 expansions. Rather than returning just one _EXPANSION, we
3089 return a TREE_LIST whose TREE_PURPOSE is a
3090 TYPE_PACK_EXPANSION containing the bases that will be
3091 initialized. The TREE_VALUE will be identical to the
3092 original TREE_VALUE, which is a list of arguments that will
3093 be passed to each base. We do not introduce any new pack
3094 expansion nodes into the TREE_VALUE (although it is possible
3095 that some already exist), because the TREE_PURPOSE and
3096 TREE_VALUE all need to be expanded together with the same
3097 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3098 resulting TREE_PURPOSE will mention the parameter packs in
3099 both the bases and the arguments to the bases. */
3102 tree parameter_packs
= NULL_TREE
;
3104 /* Determine which parameter packs will be used by the base
3106 ppd
.visited
= pointer_set_create ();
3107 ppd
.parameter_packs
= ¶meter_packs
;
3108 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3111 if (parameter_packs
== NULL_TREE
)
3113 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3114 pointer_set_destroy (ppd
.visited
);
3115 return error_mark_node
;
3118 if (TREE_VALUE (arg
) != void_type_node
)
3120 /* Collect the sets of parameter packs used in each of the
3121 initialization arguments. */
3122 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3124 /* Determine which parameter packs will be expanded in this
3126 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3131 pointer_set_destroy (ppd
.visited
);
3133 /* Create the pack expansion type for the base type. */
3134 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3135 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3136 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3138 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3139 they will rarely be compared to anything. */
3140 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3142 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3145 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3148 /* Build the PACK_EXPANSION_* node. */
3150 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3151 : make_node (EXPR_PACK_EXPANSION
);
3152 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3153 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3155 /* Propagate type and const-expression information. */
3156 TREE_TYPE (result
) = TREE_TYPE (arg
);
3157 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3160 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3161 they will rarely be compared to anything. */
3162 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3164 /* Determine which parameter packs will be expanded. */
3165 ppd
.parameter_packs
= ¶meter_packs
;
3166 ppd
.visited
= pointer_set_create ();
3167 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3168 pointer_set_destroy (ppd
.visited
);
3170 /* Make sure we found some parameter packs. */
3171 if (parameter_packs
== NULL_TREE
)
3174 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3176 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3177 return error_mark_node
;
3179 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3184 /* Checks T for any "bare" parameter packs, which have not yet been
3185 expanded, and issues an error if any are found. This operation can
3186 only be done on full expressions or types (e.g., an expression
3187 statement, "if" condition, etc.), because we could have expressions like:
3189 foo(f(g(h(args)))...)
3191 where "args" is a parameter pack. check_for_bare_parameter_packs
3192 should not be called for the subexpressions args, h(args),
3193 g(h(args)), or f(g(h(args))), because we would produce erroneous
3196 Returns TRUE and emits an error if there were bare parameter packs,
3197 returns FALSE otherwise. */
3199 check_for_bare_parameter_packs (tree t
)
3201 tree parameter_packs
= NULL_TREE
;
3202 struct find_parameter_pack_data ppd
;
3204 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3207 if (TREE_CODE (t
) == TYPE_DECL
)
3210 ppd
.parameter_packs
= ¶meter_packs
;
3211 ppd
.visited
= pointer_set_create ();
3212 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3213 pointer_set_destroy (ppd
.visited
);
3215 if (parameter_packs
)
3217 error ("parameter packs not expanded with %<...%>:");
3218 while (parameter_packs
)
3220 tree pack
= TREE_VALUE (parameter_packs
);
3221 tree name
= NULL_TREE
;
3223 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3224 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3225 name
= TYPE_NAME (pack
);
3226 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3227 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3229 name
= DECL_NAME (pack
);
3232 inform (input_location
, " %qD", name
);
3234 inform (input_location
, " <anonymous>");
3236 parameter_packs
= TREE_CHAIN (parameter_packs
);
3245 /* Expand any parameter packs that occur in the template arguments in
3248 expand_template_argument_pack (tree args
)
3250 tree result_args
= NULL_TREE
;
3251 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3252 int num_result_args
= -1;
3253 int non_default_args_count
= -1;
3255 /* First, determine if we need to expand anything, and the number of
3256 slots we'll need. */
3257 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3259 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3260 if (arg
== NULL_TREE
)
3262 if (ARGUMENT_PACK_P (arg
))
3264 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3265 if (num_result_args
< 0)
3266 num_result_args
= in_arg
+ num_packed
;
3268 num_result_args
+= num_packed
;
3272 if (num_result_args
>= 0)
3277 /* If no expansion is necessary, we're done. */
3278 if (num_result_args
< 0)
3281 /* Expand arguments. */
3282 result_args
= make_tree_vec (num_result_args
);
3283 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3284 non_default_args_count
=
3285 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3286 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3288 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3289 if (ARGUMENT_PACK_P (arg
))
3291 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3292 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3293 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3294 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3295 if (non_default_args_count
> 0)
3296 non_default_args_count
+= num_packed
;
3300 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3304 if (non_default_args_count
>= 0)
3305 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3309 /* Checks if DECL shadows a template parameter.
3311 [temp.local]: A template-parameter shall not be redeclared within its
3312 scope (including nested scopes).
3314 Emits an error and returns TRUE if the DECL shadows a parameter,
3315 returns FALSE otherwise. */
3318 check_template_shadow (tree decl
)
3322 /* If we're not in a template, we can't possibly shadow a template
3324 if (!current_template_parms
)
3327 /* Figure out what we're shadowing. */
3328 if (TREE_CODE (decl
) == OVERLOAD
)
3329 decl
= OVL_CURRENT (decl
);
3330 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3332 /* If there's no previous binding for this name, we're not shadowing
3333 anything, let alone a template parameter. */
3337 /* If we're not shadowing a template parameter, we're done. Note
3338 that OLDDECL might be an OVERLOAD (or perhaps even an
3339 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3341 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3344 /* We check for decl != olddecl to avoid bogus errors for using a
3345 name inside a class. We check TPFI to avoid duplicate errors for
3346 inline member templates. */
3348 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3351 error ("declaration of %q+#D", decl
);
3352 error (" shadows template parm %q+#D", olddecl
);
3356 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3357 ORIG_LEVEL, DECL, and TYPE. */
3360 build_template_parm_index (int index
,
3366 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3367 TEMPLATE_PARM_IDX (t
) = index
;
3368 TEMPLATE_PARM_LEVEL (t
) = level
;
3369 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3370 TEMPLATE_PARM_DECL (t
) = decl
;
3371 TREE_TYPE (t
) = type
;
3372 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3373 TREE_READONLY (t
) = TREE_READONLY (decl
);
3378 /* Find the canonical type parameter for the given template type
3379 parameter. Returns the canonical type parameter, which may be TYPE
3380 if no such parameter existed. */
3382 canonical_type_parameter (tree type
)
3385 int idx
= TEMPLATE_TYPE_IDX (type
);
3386 if (!canonical_template_parms
)
3387 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3389 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3390 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3392 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3393 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3394 list
= TREE_CHAIN (list
);
3397 return TREE_VALUE (list
);
3400 VEC_replace(tree
, canonical_template_parms
, idx
,
3401 tree_cons (NULL_TREE
, type
,
3402 VEC_index (tree
, canonical_template_parms
, idx
)));
3407 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3408 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3409 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3410 new one is created. */
3413 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3414 tsubst_flags_t complain
)
3416 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3417 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3418 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3419 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3421 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3424 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3425 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3426 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3427 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3428 DECL_ARTIFICIAL (decl
) = 1;
3429 SET_DECL_TEMPLATE_PARM_P (decl
);
3431 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3432 TEMPLATE_PARM_LEVEL (index
) - levels
,
3433 TEMPLATE_PARM_ORIG_LEVEL (index
),
3435 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3436 TEMPLATE_PARM_PARAMETER_PACK (t
)
3437 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3439 /* Template template parameters need this. */
3440 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3441 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3442 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3446 return TEMPLATE_PARM_DESCENDANTS (index
);
3449 /* Process information from new template parameter PARM and append it to the
3450 LIST being built. This new parameter is a non-type parameter iff
3451 IS_NON_TYPE is true. This new parameter is a parameter
3452 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3456 process_template_parm (tree list
, location_t parm_loc
, tree parm
, bool is_non_type
,
3457 bool is_parameter_pack
)
3464 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3465 defval
= TREE_PURPOSE (parm
);
3469 tree p
= tree_last (list
);
3471 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3474 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3475 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3477 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3487 parm
= TREE_VALUE (parm
);
3489 SET_DECL_TEMPLATE_PARM_P (parm
);
3491 if (TREE_TYPE (parm
) == error_mark_node
)
3493 err_parm_list
= build_tree_list (defval
, parm
);
3494 TREE_VALUE (err_parm_list
) = error_mark_node
;
3495 return chainon (list
, err_parm_list
);
3501 The top-level cv-qualifiers on the template-parameter are
3502 ignored when determining its type. */
3503 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3504 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3506 err_parm_list
= build_tree_list (defval
, parm
);
3507 TREE_VALUE (err_parm_list
) = error_mark_node
;
3508 return chainon (list
, err_parm_list
);
3511 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3513 /* This template parameter is not a parameter pack, but it
3514 should be. Complain about "bare" parameter packs. */
3515 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3517 /* Recover by calling this a parameter pack. */
3518 is_parameter_pack
= true;
3522 /* A template parameter is not modifiable. */
3523 TREE_CONSTANT (parm
) = 1;
3524 TREE_READONLY (parm
) = 1;
3525 decl
= build_decl (parm_loc
,
3526 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3527 TREE_CONSTANT (decl
) = 1;
3528 TREE_READONLY (decl
) = 1;
3529 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3530 = build_template_parm_index (idx
, processing_template_decl
,
3531 processing_template_decl
,
3532 decl
, TREE_TYPE (parm
));
3534 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3535 = is_parameter_pack
;
3540 parm
= TREE_VALUE (TREE_VALUE (parm
));
3542 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3544 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3545 /* This is for distinguishing between real templates and template
3546 template parameters */
3547 TREE_TYPE (parm
) = t
;
3548 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3553 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3554 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3555 decl
= build_decl (parm_loc
,
3556 TYPE_DECL
, parm
, t
);
3559 TYPE_NAME (t
) = decl
;
3560 TYPE_STUB_DECL (t
) = decl
;
3562 TEMPLATE_TYPE_PARM_INDEX (t
)
3563 = build_template_parm_index (idx
, processing_template_decl
,
3564 processing_template_decl
,
3565 decl
, TREE_TYPE (parm
));
3566 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3567 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3569 DECL_ARTIFICIAL (decl
) = 1;
3570 SET_DECL_TEMPLATE_PARM_P (decl
);
3572 parm
= build_tree_list (defval
, parm
);
3573 return chainon (list
, parm
);
3576 /* The end of a template parameter list has been reached. Process the
3577 tree list into a parameter vector, converting each parameter into a more
3578 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3582 end_template_parm_list (tree parms
)
3586 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3588 current_template_parms
3589 = tree_cons (size_int (processing_template_decl
),
3590 saved_parmlist
, current_template_parms
);
3592 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3594 next
= TREE_CHAIN (parm
);
3595 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3596 TREE_CHAIN (parm
) = NULL_TREE
;
3597 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
3598 TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm
))) =
3599 current_template_parms
;
3602 --processing_template_parmlist
;
3604 return saved_parmlist
;
3607 /* end_template_decl is called after a template declaration is seen. */
3610 end_template_decl (void)
3612 reset_specialization ();
3614 if (! processing_template_decl
)
3617 /* This matches the pushlevel in begin_template_parm_list. */
3620 --processing_template_decl
;
3621 current_template_parms
= TREE_CHAIN (current_template_parms
);
3624 /* Within the declaration of a template, return all levels of template
3625 parameters that apply. The template parameters are represented as
3626 a TREE_VEC, in the form documented in cp-tree.h for template
3630 current_template_args (void)
3633 tree args
= NULL_TREE
;
3634 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
3637 /* If there is only one level of template parameters, we do not
3638 create a TREE_VEC of TREE_VECs. Instead, we return a single
3639 TREE_VEC containing the arguments. */
3641 args
= make_tree_vec (length
);
3643 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
3645 tree a
= copy_node (TREE_VALUE (header
));
3648 TREE_TYPE (a
) = NULL_TREE
;
3649 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3651 tree t
= TREE_VEC_ELT (a
, i
);
3653 /* T will be a list if we are called from within a
3654 begin/end_template_parm_list pair, but a vector directly
3655 if within a begin/end_member_template_processing pair. */
3656 if (TREE_CODE (t
) == TREE_LIST
)
3660 if (!error_operand_p (t
))
3662 if (TREE_CODE (t
) == TYPE_DECL
3663 || TREE_CODE (t
) == TEMPLATE_DECL
)
3667 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3669 /* Turn this argument into a TYPE_ARGUMENT_PACK
3670 with a single element, which expands T. */
3671 tree vec
= make_tree_vec (1);
3672 #ifdef ENABLE_CHECKING
3673 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3674 (vec
, TREE_VEC_LENGTH (vec
));
3676 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3678 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3679 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3684 t
= DECL_INITIAL (t
);
3686 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3688 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3689 with a single element, which expands T. */
3690 tree vec
= make_tree_vec (1);
3691 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3692 #ifdef ENABLE_CHECKING
3693 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3694 (vec
, TREE_VEC_LENGTH (vec
));
3696 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3698 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3699 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3700 TREE_TYPE (t
) = type
;
3703 TREE_VEC_ELT (a
, i
) = t
;
3708 #ifdef ENABLE_CHECKING
3709 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3713 TREE_VEC_ELT (args
, --l
) = a
;
3721 /* Update the declared TYPE by doing any lookups which were thought to be
3722 dependent, but are not now that we know the SCOPE of the declarator. */
3725 maybe_update_decl_type (tree orig_type
, tree scope
)
3727 tree type
= orig_type
;
3729 if (type
== NULL_TREE
)
3732 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3733 type
= TREE_TYPE (type
);
3735 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3736 && dependent_type_p (type
)
3737 /* Don't bother building up the args in this case. */
3738 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3740 /* tsubst in the args corresponding to the template parameters,
3741 including auto if present. Most things will be unchanged, but
3742 make_typename_type and tsubst_qualified_id will resolve
3743 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3744 tree args
= current_template_args ();
3745 tree auto_node
= type_uses_auto (type
);
3749 tree auto_vec
= make_tree_vec (1);
3750 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3751 args
= add_to_template_args (args
, auto_vec
);
3753 pushed
= push_scope (scope
);
3754 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3759 if (type
== error_mark_node
)
3762 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3764 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3767 type
= TYPE_NAME (type
);
3772 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3773 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3774 a member template. Used by push_template_decl below. */
3777 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3779 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3780 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3781 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3782 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3787 struct template_parm_data
3789 /* The level of the template parameters we are currently
3793 /* The index of the specialization argument we are currently
3797 /* An array whose size is the number of template parameters. The
3798 elements are nonzero if the parameter has been used in any one
3799 of the arguments processed so far. */
3802 /* An array whose size is the number of template arguments. The
3803 elements are nonzero if the argument makes use of template
3804 parameters of this level. */
3805 int* arg_uses_template_parms
;
3808 /* Subroutine of push_template_decl used to see if each template
3809 parameter in a partial specialization is used in the explicit
3810 argument list. If T is of the LEVEL given in DATA (which is
3811 treated as a template_parm_data*), then DATA->PARMS is marked
3815 mark_template_parm (tree t
, void* data
)
3819 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
3821 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
3823 level
= TEMPLATE_PARM_LEVEL (t
);
3824 idx
= TEMPLATE_PARM_IDX (t
);
3828 level
= TEMPLATE_TYPE_LEVEL (t
);
3829 idx
= TEMPLATE_TYPE_IDX (t
);
3832 if (level
== tpd
->level
)
3834 tpd
->parms
[idx
] = 1;
3835 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
3838 /* Return zero so that for_each_template_parm will continue the
3839 traversal of the tree; we want to mark *every* template parm. */
3843 /* Process the partial specialization DECL. */
3846 process_partial_specialization (tree decl
)
3848 tree type
= TREE_TYPE (decl
);
3849 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
3850 tree specargs
= CLASSTYPE_TI_ARGS (type
);
3851 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
3852 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
3855 int nargs
= TREE_VEC_LENGTH (inner_args
);
3858 bool did_error_intro
= false;
3859 struct template_parm_data tpd
;
3860 struct template_parm_data tpd2
;
3862 gcc_assert (current_template_parms
);
3864 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3865 ntparms
= TREE_VEC_LENGTH (inner_parms
);
3867 /* We check that each of the template parameters given in the
3868 partial specialization is used in the argument list to the
3869 specialization. For example:
3871 template <class T> struct S;
3872 template <class T> struct S<T*>;
3874 The second declaration is OK because `T*' uses the template
3875 parameter T, whereas
3877 template <class T> struct S<int>;
3879 is no good. Even trickier is:
3890 The S2<T> declaration is actually invalid; it is a
3891 full-specialization. Of course,
3894 struct S2<T (*)(U)>;
3896 or some such would have been OK. */
3897 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
3898 tpd
.parms
= XALLOCAVEC (int, ntparms
);
3899 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
3901 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
3902 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
3903 for (i
= 0; i
< nargs
; ++i
)
3905 tpd
.current_arg
= i
;
3906 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
3907 &mark_template_parm
,
3910 /*include_nondeduced_p=*/false);
3912 for (i
= 0; i
< ntparms
; ++i
)
3913 if (tpd
.parms
[i
] == 0)
3915 /* One of the template parms was not used in the
3917 if (!did_error_intro
)
3919 error ("template parameters not used in partial specialization:");
3920 did_error_intro
= true;
3923 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
3926 if (did_error_intro
)
3927 return error_mark_node
;
3929 /* [temp.class.spec]
3931 The argument list of the specialization shall not be identical to
3932 the implicit argument list of the primary template. */
3933 if (comp_template_args
3935 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3937 error ("partial specialization %qT does not specialize any template arguments", type
);
3939 /* [temp.class.spec]
3941 A partially specialized non-type argument expression shall not
3942 involve template parameters of the partial specialization except
3943 when the argument expression is a simple identifier.
3945 The type of a template parameter corresponding to a specialized
3946 non-type argument shall not be dependent on a parameter of the
3949 Also, we verify that pack expansions only occur at the
3950 end of the argument list. */
3951 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
3953 for (i
= 0; i
< nargs
; ++i
)
3955 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
3956 tree arg
= TREE_VEC_ELT (inner_args
, i
);
3957 tree packed_args
= NULL_TREE
;
3960 if (ARGUMENT_PACK_P (arg
))
3962 /* Extract the arguments from the argument pack. We'll be
3963 iterating over these in the following loop. */
3964 packed_args
= ARGUMENT_PACK_ARGS (arg
);
3965 len
= TREE_VEC_LENGTH (packed_args
);
3968 for (j
= 0; j
< len
; j
++)
3971 /* Get the Jth argument in the parameter pack. */
3972 arg
= TREE_VEC_ELT (packed_args
, j
);
3974 if (PACK_EXPANSION_P (arg
))
3976 /* Pack expansions must come at the end of the
3978 if ((packed_args
&& j
< len
- 1)
3979 || (!packed_args
&& i
< nargs
- 1))
3981 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3982 error ("parameter pack argument %qE must be at the "
3983 "end of the template argument list", arg
);
3985 error ("parameter pack argument %qT must be at the "
3986 "end of the template argument list", arg
);
3990 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
3991 /* We only care about the pattern. */
3992 arg
= PACK_EXPANSION_PATTERN (arg
);
3994 if (/* These first two lines are the `non-type' bit. */
3996 && TREE_CODE (arg
) != TEMPLATE_DECL
3997 /* This next line is the `argument expression is not just a
3998 simple identifier' condition and also the `specialized
3999 non-type argument' bit. */
4000 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4002 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4003 || (packed_args
&& uses_template_parms (arg
)))
4004 error ("template argument %qE involves template parameter(s)",
4008 /* Look at the corresponding template parameter,
4009 marking which template parameters its type depends
4011 tree type
= TREE_TYPE (parm
);
4015 /* We haven't yet initialized TPD2. Do so now. */
4016 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4017 /* The number of parameters here is the number in the
4018 main template, which, as checked in the assertion
4020 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4022 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4025 /* Mark the template parameters. But this time, we're
4026 looking for the template parameters of the main
4027 template, not in the specialization. */
4028 tpd2
.current_arg
= i
;
4029 tpd2
.arg_uses_template_parms
[i
] = 0;
4030 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4031 for_each_template_parm (type
,
4032 &mark_template_parm
,
4035 /*include_nondeduced_p=*/false);
4037 if (tpd2
.arg_uses_template_parms
[i
])
4039 /* The type depended on some template parameters.
4040 If they are fully specialized in the
4041 specialization, that's OK. */
4044 for (j
= 0; j
< nargs
; ++j
)
4045 if (tpd2
.parms
[j
] != 0
4046 && tpd
.arg_uses_template_parms
[j
])
4049 error_n (input_location
, count
,
4050 "type %qT of template argument %qE depends "
4051 "on a template parameter",
4052 "type %qT of template argument %qE depends "
4053 "on template parameters",
4062 /* We should only get here once. */
4063 gcc_assert (!COMPLETE_TYPE_P (type
));
4065 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4066 = tree_cons (specargs
, inner_parms
,
4067 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4068 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4070 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4071 inst
= TREE_CHAIN (inst
))
4073 tree inst_type
= TREE_VALUE (inst
);
4074 if (COMPLETE_TYPE_P (inst_type
)
4075 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4077 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4078 if (spec
&& TREE_TYPE (spec
) == type
)
4079 permerror (input_location
,
4080 "partial specialization of %qT after instantiation "
4081 "of %qT", type
, inst_type
);
4088 /* Check that a template declaration's use of default arguments and
4089 parameter packs is not invalid. Here, PARMS are the template
4090 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4091 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4095 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4096 declaration (but not a definition); 1 indicates a declaration, 2
4097 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4098 emitted for extraneous default arguments.
4100 Returns TRUE if there were no errors found, FALSE otherwise. */
4103 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
4104 int is_partial
, int is_friend_decl
)
4107 int last_level_to_check
;
4109 bool no_errors
= true;
4113 A default template-argument shall not be specified in a
4114 function template declaration or a function template definition, nor
4115 in the template-parameter-list of the definition of a member of a
4118 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4119 /* You can't have a function template declaration in a local
4120 scope, nor you can you define a member of a class template in a
4124 if (current_class_type
4125 && !TYPE_BEING_DEFINED (current_class_type
)
4126 && DECL_LANG_SPECIFIC (decl
)
4127 && DECL_DECLARES_FUNCTION_P (decl
)
4128 /* If this is either a friend defined in the scope of the class
4129 or a member function. */
4130 && (DECL_FUNCTION_MEMBER_P (decl
)
4131 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4132 : DECL_FRIEND_CONTEXT (decl
)
4133 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4135 /* And, if it was a member function, it really was defined in
4136 the scope of the class. */
4137 && (!DECL_FUNCTION_MEMBER_P (decl
)
4138 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4139 /* We already checked these parameters when the template was
4140 declared, so there's no need to do it again now. This function
4141 was defined in class scope, but we're processing it's body now
4142 that the class is complete. */
4145 /* Core issue 226 (C++0x only): the following only applies to class
4147 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4151 If a template-parameter has a default template-argument, all
4152 subsequent template-parameters shall have a default
4153 template-argument supplied. */
4154 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4156 tree inner_parms
= TREE_VALUE (parm_level
);
4157 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4158 int seen_def_arg_p
= 0;
4161 for (i
= 0; i
< ntparms
; ++i
)
4163 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4165 if (parm
== error_mark_node
)
4168 if (TREE_PURPOSE (parm
))
4170 else if (seen_def_arg_p
4171 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4173 error ("no default argument for %qD", TREE_VALUE (parm
));
4174 /* For better subsequent error-recovery, we indicate that
4175 there should have been a default argument. */
4176 TREE_PURPOSE (parm
) = error_mark_node
;
4182 /* Don't complain about an enclosing partial
4184 && parm_level
== parms
4185 && TREE_CODE (decl
) == TYPE_DECL
4187 && template_parameter_pack_p (TREE_VALUE (parm
)))
4189 /* A primary class template can only have one
4190 parameter pack, at the end of the template
4193 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4194 error ("parameter pack %qE must be at the end of the"
4195 " template parameter list", TREE_VALUE (parm
));
4197 error ("parameter pack %qT must be at the end of the"
4198 " template parameter list",
4199 TREE_TYPE (TREE_VALUE (parm
)));
4201 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4209 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4213 /* For an ordinary class template, default template arguments are
4214 allowed at the innermost level, e.g.:
4215 template <class T = int>
4217 but, in a partial specialization, they're not allowed even
4218 there, as we have in [temp.class.spec]:
4220 The template parameter list of a specialization shall not
4221 contain default template argument values.
4223 So, for a partial specialization, or for a function template
4224 (in C++98/C++03), we look at all of them. */
4227 /* But, for a primary class template that is not a partial
4228 specialization we look at all template parameters except the
4230 parms
= TREE_CHAIN (parms
);
4232 /* Figure out what error message to issue. */
4233 if (is_friend_decl
== 2)
4234 msg
= G_("default template arguments may not be used in function template "
4235 "friend re-declaration");
4236 else if (is_friend_decl
)
4237 msg
= G_("default template arguments may not be used in function template "
4238 "friend declarations");
4239 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4240 msg
= G_("default template arguments may not be used in function templates "
4241 "without -std=c++0x or -std=gnu++0x");
4242 else if (is_partial
)
4243 msg
= G_("default template arguments may not be used in "
4244 "partial specializations");
4246 msg
= G_("default argument for template parameter for class enclosing %qD");
4248 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4249 /* If we're inside a class definition, there's no need to
4250 examine the parameters to the class itself. On the one
4251 hand, they will be checked when the class is defined, and,
4252 on the other, default arguments are valid in things like:
4253 template <class T = double>
4254 struct S { template <class U> void f(U); };
4255 Here the default argument for `S' has no bearing on the
4256 declaration of `f'. */
4257 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4259 /* Check everything. */
4260 last_level_to_check
= 0;
4262 for (parm_level
= parms
;
4263 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4264 parm_level
= TREE_CHAIN (parm_level
))
4266 tree inner_parms
= TREE_VALUE (parm_level
);
4270 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4271 for (i
= 0; i
< ntparms
; ++i
)
4273 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4276 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4281 if (is_friend_decl
== 2)
4288 /* Clear out the default argument so that we are not
4290 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4294 /* At this point, if we're still interested in issuing messages,
4295 they must apply to classes surrounding the object declared. */
4297 msg
= G_("default argument for template parameter for class "
4304 /* Worker for push_template_decl_real, called via
4305 for_each_template_parm. DATA is really an int, indicating the
4306 level of the parameters we are interested in. If T is a template
4307 parameter of that level, return nonzero. */
4310 template_parm_this_level_p (tree t
, void* data
)
4312 int this_level
= *(int *)data
;
4315 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4316 level
= TEMPLATE_PARM_LEVEL (t
);
4318 level
= TEMPLATE_TYPE_LEVEL (t
);
4319 return level
== this_level
;
4322 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4323 parameters given by current_template_args, or reuses a
4324 previously existing one, if appropriate. Returns the DECL, or an
4325 equivalent one, if it is replaced via a call to duplicate_decls.
4327 If IS_FRIEND is true, DECL is a friend declaration. */
4330 push_template_decl_real (tree decl
, bool is_friend
)
4338 int new_template_p
= 0;
4339 /* True if the template is a member template, in the sense of
4341 bool member_template_p
= false;
4343 if (decl
== error_mark_node
|| !current_template_parms
)
4344 return error_mark_node
;
4346 /* See if this is a partial specialization. */
4347 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4348 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4349 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4351 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4355 /* For a friend, we want the context of the friend function, not
4356 the type of which it is a friend. */
4357 ctx
= CP_DECL_CONTEXT (decl
);
4358 else if (CP_DECL_CONTEXT (decl
)
4359 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4360 /* In the case of a virtual function, we want the class in which
4362 ctx
= CP_DECL_CONTEXT (decl
);
4364 /* Otherwise, if we're currently defining some class, the DECL
4365 is assumed to be a member of the class. */
4366 ctx
= current_scope ();
4368 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4371 if (!DECL_CONTEXT (decl
))
4372 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4374 /* See if this is a primary template. */
4375 if (is_friend
&& ctx
)
4376 /* A friend template that specifies a class context, i.e.
4377 template <typename T> friend void A<T>::f();
4381 primary
= template_parm_scope_p ();
4385 if (DECL_CLASS_SCOPE_P (decl
))
4386 member_template_p
= true;
4387 if (TREE_CODE (decl
) == TYPE_DECL
4388 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4390 error ("template class without a name");
4391 return error_mark_node
;
4393 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4395 if (DECL_DESTRUCTOR_P (decl
))
4399 A destructor shall not be a member template. */
4400 error ("destructor %qD declared as member template", decl
);
4401 return error_mark_node
;
4403 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4404 && (!TYPE_ARG_TYPES (TREE_TYPE (decl
))
4405 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4406 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4407 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4408 == void_list_node
)))
4410 /* [basic.stc.dynamic.allocation]
4412 An allocation function can be a function
4413 template. ... Template allocation functions shall
4414 have two or more parameters. */
4415 error ("invalid template declaration of %qD", decl
);
4416 return error_mark_node
;
4419 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4420 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4424 error ("template declaration of %q#D", decl
);
4425 return error_mark_node
;
4429 /* Check to see that the rules regarding the use of default
4430 arguments are not being violated. */
4431 check_default_tmpl_args (decl
, current_template_parms
,
4432 primary
, is_partial
, /*is_friend_decl=*/0);
4434 /* Ensure that there are no parameter packs in the type of this
4435 declaration that have not been expanded. */
4436 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4438 /* Check each of the arguments individually to see if there are
4439 any bare parameter packs. */
4440 tree type
= TREE_TYPE (decl
);
4441 tree arg
= DECL_ARGUMENTS (decl
);
4442 tree argtype
= TYPE_ARG_TYPES (type
);
4444 while (arg
&& argtype
)
4446 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4447 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4449 /* This is a PARM_DECL that contains unexpanded parameter
4450 packs. We have already complained about this in the
4451 check_for_bare_parameter_packs call, so just replace
4452 these types with ERROR_MARK_NODE. */
4453 TREE_TYPE (arg
) = error_mark_node
;
4454 TREE_VALUE (argtype
) = error_mark_node
;
4457 arg
= DECL_CHAIN (arg
);
4458 argtype
= TREE_CHAIN (argtype
);
4461 /* Check for bare parameter packs in the return type and the
4462 exception specifiers. */
4463 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4464 /* Errors were already issued, set return type to int
4465 as the frontend doesn't expect error_mark_node as
4467 TREE_TYPE (type
) = integer_type_node
;
4468 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4469 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4471 else if (check_for_bare_parameter_packs (TREE_TYPE (decl
)))
4473 TREE_TYPE (decl
) = error_mark_node
;
4474 return error_mark_node
;
4478 return process_partial_specialization (decl
);
4480 args
= current_template_args ();
4483 || TREE_CODE (ctx
) == FUNCTION_DECL
4484 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4485 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4487 if (DECL_LANG_SPECIFIC (decl
)
4488 && DECL_TEMPLATE_INFO (decl
)
4489 && DECL_TI_TEMPLATE (decl
))
4490 tmpl
= DECL_TI_TEMPLATE (decl
);
4491 /* If DECL is a TYPE_DECL for a class-template, then there won't
4492 be DECL_LANG_SPECIFIC. The information equivalent to
4493 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4494 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4495 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4496 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4498 /* Since a template declaration already existed for this
4499 class-type, we must be redeclaring it here. Make sure
4500 that the redeclaration is valid. */
4501 redeclare_class_template (TREE_TYPE (decl
),
4502 current_template_parms
);
4503 /* We don't need to create a new TEMPLATE_DECL; just use the
4504 one we already had. */
4505 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4509 tmpl
= build_template_decl (decl
, current_template_parms
,
4513 if (DECL_LANG_SPECIFIC (decl
)
4514 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4516 /* A specialization of a member template of a template
4518 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4519 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4520 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4526 tree a
, t
, current
, parms
;
4528 tree tinfo
= get_template_info (decl
);
4532 error ("template definition of non-template %q#D", decl
);
4533 return error_mark_node
;
4536 tmpl
= TI_TEMPLATE (tinfo
);
4538 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4539 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4540 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4541 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4545 /* The declaration is a specialization of a member
4546 template, declared outside the class. Therefore, the
4547 innermost template arguments will be NULL, so we
4548 replace them with the arguments determined by the
4549 earlier call to check_explicit_specialization. */
4550 args
= DECL_TI_ARGS (decl
);
4553 = build_template_decl (decl
, current_template_parms
,
4555 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4556 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4557 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4558 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4559 DECL_TEMPLATE_INFO (new_tmpl
)
4560 = build_template_info (tmpl
, args
);
4562 register_specialization (new_tmpl
,
4563 most_general_template (tmpl
),
4569 /* Make sure the template headers we got make sense. */
4571 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4572 i
= TMPL_PARMS_DEPTH (parms
);
4573 if (TMPL_ARGS_DEPTH (args
) != i
)
4575 error ("expected %d levels of template parms for %q#D, got %d",
4576 i
, decl
, TMPL_ARGS_DEPTH (args
));
4579 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4581 a
= TMPL_ARGS_LEVEL (args
, i
);
4582 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4584 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4586 if (current
== decl
)
4587 error ("got %d template parameters for %q#D",
4588 TREE_VEC_LENGTH (a
), decl
);
4590 error ("got %d template parameters for %q#T",
4591 TREE_VEC_LENGTH (a
), current
);
4592 error (" but %d required", TREE_VEC_LENGTH (t
));
4593 return error_mark_node
;
4596 if (current
== decl
)
4598 else if (current
== NULL_TREE
)
4599 /* Can happen in erroneous input. */
4602 current
= (TYPE_P (current
)
4603 ? TYPE_CONTEXT (current
)
4604 : DECL_CONTEXT (current
));
4607 /* Check that the parms are used in the appropriate qualifying scopes
4608 in the declarator. */
4609 if (!comp_template_args
4611 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4614 template arguments to %qD do not match original template %qD",
4615 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4616 if (!uses_template_parms (TI_ARGS (tinfo
)))
4617 inform (input_location
, "use template<> for an explicit specialization");
4618 /* Avoid crash in import_export_decl. */
4619 DECL_INTERFACE_KNOWN (decl
) = 1;
4620 return error_mark_node
;
4624 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4625 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4627 /* Push template declarations for global functions and types. Note
4628 that we do not try to push a global template friend declared in a
4629 template class; such a thing may well depend on the template
4630 parameters of the class. */
4631 if (new_template_p
&& !ctx
4632 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4634 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4635 if (tmpl
== error_mark_node
)
4636 return error_mark_node
;
4638 /* Hide template friend classes that haven't been declared yet. */
4639 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4641 DECL_ANTICIPATED (tmpl
) = 1;
4642 DECL_FRIEND_P (tmpl
) = 1;
4648 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4651 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4652 if (DECL_CONV_FN_P (tmpl
))
4654 int depth
= TMPL_PARMS_DEPTH (parms
);
4656 /* It is a conversion operator. See if the type converted to
4657 depends on innermost template operands. */
4659 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4661 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4664 /* Give template template parms a DECL_CONTEXT of the template
4665 for which they are a parameter. */
4666 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4667 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4669 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4670 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4671 DECL_CONTEXT (parm
) = tmpl
;
4675 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4676 back to its most general template. If TMPL is a specialization,
4677 ARGS may only have the innermost set of arguments. Add the missing
4678 argument levels if necessary. */
4679 if (DECL_TEMPLATE_INFO (tmpl
))
4680 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4682 info
= build_template_info (tmpl
, args
);
4684 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4685 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4686 else if (DECL_LANG_SPECIFIC (decl
))
4687 DECL_TEMPLATE_INFO (decl
) = info
;
4689 return DECL_TEMPLATE_RESULT (tmpl
);
4693 push_template_decl (tree decl
)
4695 return push_template_decl_real (decl
, false);
4698 /* Called when a class template TYPE is redeclared with the indicated
4699 template PARMS, e.g.:
4701 template <class T> struct S;
4702 template <class T> struct S {}; */
4705 redeclare_class_template (tree type
, tree parms
)
4711 if (!TYPE_TEMPLATE_INFO (type
))
4713 error ("%qT is not a template type", type
);
4717 tmpl
= TYPE_TI_TEMPLATE (type
);
4718 if (!PRIMARY_TEMPLATE_P (tmpl
))
4719 /* The type is nested in some template class. Nothing to worry
4720 about here; there are no new template parameters for the nested
4726 error ("template specifiers not specified in declaration of %qD",
4731 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4732 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4734 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4736 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4737 "redeclared with %d template parameter",
4738 "redeclared with %d template parameters",
4739 TREE_VEC_LENGTH (parms
));
4740 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
4741 "previous declaration %q+D used %d template parameter",
4742 "previous declaration %q+D used %d template parameters",
4743 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4747 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4754 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4755 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4758 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4759 if (tmpl_parm
== error_mark_node
)
4762 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4763 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4764 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4766 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4768 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4769 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4770 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4771 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4772 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4773 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4774 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4775 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4776 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
4778 error ("template parameter %q+#D", tmpl_parm
);
4779 error ("redeclared here as %q#D", parm
);
4783 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
4785 /* We have in [temp.param]:
4787 A template-parameter may not be given default arguments
4788 by two different declarations in the same scope. */
4789 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
4790 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
4791 "original definition appeared here");
4795 if (parm_default
!= NULL_TREE
)
4796 /* Update the previous template parameters (which are the ones
4797 that will really count) with the new default value. */
4798 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
4799 else if (tmpl_default
!= NULL_TREE
)
4800 /* Update the new parameters, too; they'll be used as the
4801 parameters for any members. */
4802 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
4808 /* Simplify EXPR if it is a non-dependent expression. Returns the
4809 (possibly simplified) expression. */
4812 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
4814 if (expr
== NULL_TREE
)
4817 /* If we're in a template, but EXPR isn't value dependent, simplify
4818 it. We're supposed to treat:
4820 template <typename T> void f(T[1 + 1]);
4821 template <typename T> void f(T[2]);
4823 as two declarations of the same function, for example. */
4824 if (processing_template_decl
4825 && !type_dependent_expression_p (expr
)
4826 && !value_dependent_expression_p (expr
))
4828 HOST_WIDE_INT saved_processing_template_decl
;
4830 saved_processing_template_decl
= processing_template_decl
;
4831 processing_template_decl
= 0;
4832 expr
= tsubst_copy_and_build (expr
,
4835 /*in_decl=*/NULL_TREE
,
4836 /*function_p=*/false,
4837 /*integral_constant_expression_p=*/true);
4838 processing_template_decl
= saved_processing_template_decl
;
4844 fold_non_dependent_expr (tree expr
)
4846 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
4849 /* EXPR is an expression which is used in a constant-expression context.
4850 For instance, it could be a VAR_DECL with a constant initializer.
4851 Extract the innermost constant expression.
4853 This is basically a more powerful version of
4854 integral_constant_value, which can be used also in templates where
4855 initializers can maintain a syntactic rather than semantic form
4856 (even if they are non-dependent, for access-checking purposes). */
4859 fold_decl_constant_value (tree expr
)
4861 tree const_expr
= expr
;
4864 expr
= fold_non_dependent_expr (const_expr
);
4865 const_expr
= integral_constant_value (expr
);
4867 while (expr
!= const_expr
);
4872 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4873 must be a function or a pointer-to-function type, as specified
4874 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4875 and check that the resulting function has external linkage. */
4878 convert_nontype_argument_function (tree type
, tree expr
)
4883 fn
= instantiate_type (type
, fns
, tf_none
);
4884 if (fn
== error_mark_node
)
4885 return error_mark_node
;
4888 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
4889 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
4890 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
4891 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
4893 /* [temp.arg.nontype]/1
4895 A template-argument for a non-type, non-template template-parameter
4898 -- the address of an object or function with external linkage. */
4899 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
4901 error ("%qE is not a valid template argument for type %qT "
4902 "because function %qD has not external linkage",
4903 expr
, type
, fn_no_ptr
);
4910 /* Subroutine of convert_nontype_argument.
4911 Check if EXPR of type TYPE is a valid pointer-to-member constant.
4912 Emit an error otherwise. */
4915 check_valid_ptrmem_cst_expr (tree type
, tree expr
)
4918 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
4920 error ("%qE is not a valid template argument for type %qT",
4922 error ("it must be a pointer-to-member of the form `&X::Y'");
4926 /* Returns TRUE iff the address of OP is value-dependent.
4928 14.6.2.4 [temp.dep.temp]:
4929 A non-integral non-type template-argument is dependent if its type is
4930 dependent or it has either of the following forms
4933 and contains a nested-name-specifier which specifies a class-name that
4934 names a dependent type.
4936 We generalize this to just say that the address of a member of a
4937 dependent class is value-dependent; the above doesn't cover the
4938 address of a static data member named with an unqualified-id. */
4941 has_value_dependent_address (tree op
)
4943 /* We could use get_inner_reference here, but there's no need;
4944 this is only relevant for template non-type arguments, which
4945 can only be expressed as &id-expression. */
4948 tree ctx
= CP_DECL_CONTEXT (op
);
4949 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
4956 /* Attempt to convert the non-type template parameter EXPR to the
4957 indicated TYPE. If the conversion is successful, return the
4958 converted value. If the conversion is unsuccessful, return
4959 NULL_TREE if we issued an error message, or error_mark_node if we
4960 did not. We issue error messages for out-and-out bad template
4961 parameters, but not simply because the conversion failed, since we
4962 might be just trying to do argument deduction. Both TYPE and EXPR
4963 must be non-dependent.
4965 The conversion follows the special rules described in
4966 [temp.arg.nontype], and it is much more strict than an implicit
4969 This function is called twice for each template argument (see
4970 lookup_template_class for a more accurate description of this
4971 problem). This means that we need to handle expressions which
4972 are not valid in a C++ source, but can be created from the
4973 first call (for instance, casts to perform conversions). These
4974 hacks can go away after we fix the double coercion problem. */
4977 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
4981 /* Detect immediately string literals as invalid non-type argument.
4982 This special-case is not needed for correctness (we would easily
4983 catch this later), but only to provide better diagnostic for this
4984 common user mistake. As suggested by DR 100, we do not mention
4985 linkage issues in the diagnostic as this is not the point. */
4986 /* FIXME we're making this OK. */
4987 if (TREE_CODE (expr
) == STRING_CST
)
4989 if (complain
& tf_error
)
4990 error ("%qE is not a valid template argument for type %qT "
4991 "because string literals can never be used in this context",
4996 /* Add the ADDR_EXPR now for the benefit of
4997 value_dependent_expression_p. */
4998 if (TYPE_PTROBV_P (type
))
4999 expr
= decay_conversion (expr
);
5001 /* If we are in a template, EXPR may be non-dependent, but still
5002 have a syntactic, rather than semantic, form. For example, EXPR
5003 might be a SCOPE_REF, rather than the VAR_DECL to which the
5004 SCOPE_REF refers. Preserving the qualifying scope is necessary
5005 so that access checking can be performed when the template is
5006 instantiated -- but here we need the resolved form so that we can
5007 convert the argument. */
5008 if (TYPE_REF_OBJ_P (type
)
5009 && has_value_dependent_address (expr
))
5010 /* If we want the address and it's value-dependent, don't fold. */;
5011 else if (!type_unknown_p (expr
))
5012 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5013 if (error_operand_p (expr
))
5014 return error_mark_node
;
5015 expr_type
= TREE_TYPE (expr
);
5016 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5017 expr
= mark_lvalue_use (expr
);
5019 expr
= mark_rvalue_use (expr
);
5021 /* HACK: Due to double coercion, we can get a
5022 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5023 which is the tree that we built on the first call (see
5024 below when coercing to reference to object or to reference to
5025 function). We just strip everything and get to the arg.
5026 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5028 if (TREE_CODE (expr
) == NOP_EXPR
)
5030 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5032 /* ??? Maybe we could use convert_from_reference here, but we
5033 would need to relax its constraints because the NOP_EXPR
5034 could actually change the type to something more cv-qualified,
5035 and this is not folded by convert_from_reference. */
5036 tree addr
= TREE_OPERAND (expr
, 0);
5037 gcc_assert (TREE_CODE (expr_type
) == REFERENCE_TYPE
);
5038 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5039 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5040 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5041 (TREE_TYPE (expr_type
),
5042 TREE_TYPE (TREE_TYPE (addr
))));
5044 expr
= TREE_OPERAND (addr
, 0);
5045 expr_type
= TREE_TYPE (expr
);
5048 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5049 parameter is a pointer to object, through decay and
5050 qualification conversion. Let's strip everything. */
5051 else if (TYPE_PTROBV_P (type
))
5054 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5055 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5056 /* Skip the ADDR_EXPR only if it is part of the decay for
5057 an array. Otherwise, it is part of the original argument
5058 in the source code. */
5059 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5060 expr
= TREE_OPERAND (expr
, 0);
5061 expr_type
= TREE_TYPE (expr
);
5065 /* [temp.arg.nontype]/5, bullet 1
5067 For a non-type template-parameter of integral or enumeration type,
5068 integral promotions (_conv.prom_) and integral conversions
5069 (_conv.integral_) are applied. */
5070 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5072 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type
))
5073 return error_mark_node
;
5075 expr
= fold_decl_constant_value (expr
);
5076 /* Notice that there are constant expressions like '4 % 0' which
5077 do not fold into integer constants. */
5078 if (TREE_CODE (expr
) != INTEGER_CST
)
5080 if (complain
& tf_error
)
5081 error ("%qE is not a valid template argument for type %qT "
5082 "because it is a non-constant expression", expr
, type
);
5086 /* At this point, an implicit conversion does what we want,
5087 because we already know that the expression is of integral
5089 expr
= perform_implicit_conversion (type
, expr
, complain
);
5090 if (expr
== error_mark_node
)
5091 return error_mark_node
;
5093 /* Conversion was allowed: fold it to a bare integer constant. */
5096 /* [temp.arg.nontype]/5, bullet 2
5098 For a non-type template-parameter of type pointer to object,
5099 qualification conversions (_conv.qual_) and the array-to-pointer
5100 conversion (_conv.array_) are applied. */
5101 else if (TYPE_PTROBV_P (type
))
5103 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5105 A template-argument for a non-type, non-template template-parameter
5106 shall be one of: [...]
5108 -- the name of a non-type template-parameter;
5109 -- the address of an object or function with external linkage, [...]
5110 expressed as "& id-expression" where the & is optional if the name
5111 refers to a function or array, or if the corresponding
5112 template-parameter is a reference.
5114 Here, we do not care about functions, as they are invalid anyway
5115 for a parameter of type pointer-to-object. */
5117 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5118 /* Non-type template parameters are OK. */
5120 else if (TREE_CODE (expr
) != ADDR_EXPR
5121 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5123 if (TREE_CODE (expr
) == VAR_DECL
)
5125 error ("%qD is not a valid template argument "
5126 "because %qD is a variable, not the address of "
5131 /* Other values, like integer constants, might be valid
5132 non-type arguments of some other type. */
5133 return error_mark_node
;
5139 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5140 ? TREE_OPERAND (expr
, 0) : expr
);
5141 if (TREE_CODE (decl
) != VAR_DECL
)
5143 error ("%qE is not a valid template argument of type %qT "
5144 "because %qE is not a variable",
5148 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
5150 error ("%qE is not a valid template argument of type %qT "
5151 "because %qD does not have external linkage",
5157 expr
= decay_conversion (expr
);
5158 if (expr
== error_mark_node
)
5159 return error_mark_node
;
5161 expr
= perform_qualification_conversions (type
, expr
);
5162 if (expr
== error_mark_node
)
5163 return error_mark_node
;
5165 /* [temp.arg.nontype]/5, bullet 3
5167 For a non-type template-parameter of type reference to object, no
5168 conversions apply. The type referred to by the reference may be more
5169 cv-qualified than the (otherwise identical) type of the
5170 template-argument. The template-parameter is bound directly to the
5171 template-argument, which must be an lvalue. */
5172 else if (TYPE_REF_OBJ_P (type
))
5174 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5176 return error_mark_node
;
5178 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5180 error ("%qE is not a valid template argument for type %qT "
5181 "because of conflicts in cv-qualification", expr
, type
);
5185 if (!real_lvalue_p (expr
))
5187 error ("%qE is not a valid template argument for type %qT "
5188 "because it is not an lvalue", expr
, type
);
5192 /* [temp.arg.nontype]/1
5194 A template-argument for a non-type, non-template template-parameter
5195 shall be one of: [...]
5197 -- the address of an object or function with external linkage. */
5198 if (TREE_CODE (expr
) == INDIRECT_REF
5199 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5201 expr
= TREE_OPERAND (expr
, 0);
5204 error ("%q#D is not a valid template argument for type %qT "
5205 "because a reference variable does not have a constant "
5206 "address", expr
, type
);
5213 error ("%qE is not a valid template argument for type %qT "
5214 "because it is not an object with external linkage",
5219 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5221 error ("%qE is not a valid template argument for type %qT "
5222 "because object %qD has not external linkage",
5227 expr
= build_nop (type
, build_address (expr
));
5229 /* [temp.arg.nontype]/5, bullet 4
5231 For a non-type template-parameter of type pointer to function, only
5232 the function-to-pointer conversion (_conv.func_) is applied. If the
5233 template-argument represents a set of overloaded functions (or a
5234 pointer to such), the matching function is selected from the set
5236 else if (TYPE_PTRFN_P (type
))
5238 /* If the argument is a template-id, we might not have enough
5239 context information to decay the pointer. */
5240 if (!type_unknown_p (expr_type
))
5242 expr
= decay_conversion (expr
);
5243 if (expr
== error_mark_node
)
5244 return error_mark_node
;
5247 expr
= convert_nontype_argument_function (type
, expr
);
5248 if (!expr
|| expr
== error_mark_node
)
5251 if (TREE_CODE (expr
) != ADDR_EXPR
)
5253 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5254 error ("it must be the address of a function with external linkage");
5258 /* [temp.arg.nontype]/5, bullet 5
5260 For a non-type template-parameter of type reference to function, no
5261 conversions apply. If the template-argument represents a set of
5262 overloaded functions, the matching function is selected from the set
5264 else if (TYPE_REFFN_P (type
))
5266 if (TREE_CODE (expr
) == ADDR_EXPR
)
5268 error ("%qE is not a valid template argument for type %qT "
5269 "because it is a pointer", expr
, type
);
5270 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5274 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5275 if (!expr
|| expr
== error_mark_node
)
5278 expr
= build_nop (type
, build_address (expr
));
5280 /* [temp.arg.nontype]/5, bullet 6
5282 For a non-type template-parameter of type pointer to member function,
5283 no conversions apply. If the template-argument represents a set of
5284 overloaded member functions, the matching member function is selected
5285 from the set (_over.over_). */
5286 else if (TYPE_PTRMEMFUNC_P (type
))
5288 expr
= instantiate_type (type
, expr
, tf_none
);
5289 if (expr
== error_mark_node
)
5290 return error_mark_node
;
5292 /* [temp.arg.nontype] bullet 1 says the pointer to member
5293 expression must be a pointer-to-member constant. */
5294 if (!check_valid_ptrmem_cst_expr (type
, expr
))
5295 return error_mark_node
;
5297 /* There is no way to disable standard conversions in
5298 resolve_address_of_overloaded_function (called by
5299 instantiate_type). It is possible that the call succeeded by
5300 converting &B::I to &D::I (where B is a base of D), so we need
5301 to reject this conversion here.
5303 Actually, even if there was a way to disable standard conversions,
5304 it would still be better to reject them here so that we can
5305 provide a superior diagnostic. */
5306 if (!same_type_p (TREE_TYPE (expr
), type
))
5308 error ("%qE is not a valid template argument for type %qT "
5309 "because it is of type %qT", expr
, type
,
5311 /* If we are just one standard conversion off, explain. */
5312 if (can_convert (type
, TREE_TYPE (expr
)))
5313 inform (input_location
,
5314 "standard conversions are not allowed in this context");
5318 /* [temp.arg.nontype]/5, bullet 7
5320 For a non-type template-parameter of type pointer to data member,
5321 qualification conversions (_conv.qual_) are applied. */
5322 else if (TYPE_PTRMEM_P (type
))
5324 /* [temp.arg.nontype] bullet 1 says the pointer to member
5325 expression must be a pointer-to-member constant. */
5326 if (!check_valid_ptrmem_cst_expr (type
, expr
))
5327 return error_mark_node
;
5329 expr
= perform_qualification_conversions (type
, expr
);
5330 if (expr
== error_mark_node
)
5333 /* A template non-type parameter must be one of the above. */
5337 /* Sanity check: did we actually convert the argument to the
5339 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5340 (type
, TREE_TYPE (expr
)));
5344 /* Subroutine of coerce_template_template_parms, which returns 1 if
5345 PARM_PARM and ARG_PARM match using the rule for the template
5346 parameters of template template parameters. Both PARM and ARG are
5347 template parameters; the rest of the arguments are the same as for
5348 coerce_template_template_parms.
5351 coerce_template_template_parm (tree parm
,
5353 tsubst_flags_t complain
,
5357 if (arg
== NULL_TREE
|| arg
== error_mark_node
5358 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5361 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5364 switch (TREE_CODE (parm
))
5367 /* We encounter instantiations of templates like
5368 template <template <template <class> class> class TT>
5371 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5372 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5374 if (!coerce_template_template_parms
5375 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5381 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5382 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5383 /* Argument is a parameter pack but parameter is not. */
5388 /* The tsubst call is used to handle cases such as
5390 template <int> class C {};
5391 template <class T, template <T> class TT> class D {};
5394 i.e. the parameter list of TT depends on earlier parameters. */
5395 if (!uses_template_parms (TREE_TYPE (arg
))
5397 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5401 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5402 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5403 /* Argument is a parameter pack but parameter is not. */
5416 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5417 template template parameters. Both PARM_PARMS and ARG_PARMS are
5418 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5421 Consider the example:
5422 template <class T> class A;
5423 template<template <class U> class TT> class B;
5425 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5426 the parameters to A, and OUTER_ARGS contains A. */
5429 coerce_template_template_parms (tree parm_parms
,
5431 tsubst_flags_t complain
,
5435 int nparms
, nargs
, i
;
5439 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5440 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5442 nparms
= TREE_VEC_LENGTH (parm_parms
);
5443 nargs
= TREE_VEC_LENGTH (arg_parms
);
5445 /* Determine whether we have a parameter pack at the end of the
5446 template template parameter's template parameter list. */
5447 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5449 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5451 if (parm
== error_mark_node
)
5454 switch (TREE_CODE (parm
))
5458 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5463 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5473 && !(variadic_p
&& nargs
>= nparms
- 1))
5476 /* Check all of the template parameters except the parameter pack at
5477 the end (if any). */
5478 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
5480 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
5481 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5484 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5485 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5487 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5495 /* Check each of the template parameters in the template
5496 argument against the template parameter pack at the end of
5497 the template template parameter. */
5498 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
5501 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5503 for (; i
< nargs
; ++i
)
5505 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5508 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5510 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
5519 /* Verifies that the deduced template arguments (in TARGS) for the
5520 template template parameters (in TPARMS) represent valid bindings,
5521 by comparing the template parameter list of each template argument
5522 to the template parameter list of its corresponding template
5523 template parameter, in accordance with DR150. This
5524 routine can only be called after all template arguments have been
5525 deduced. It will return TRUE if all of the template template
5526 parameter bindings are okay, FALSE otherwise. */
5528 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
5530 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
5533 /* We're dealing with template parms in this process. */
5534 ++processing_template_decl
;
5536 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
5538 for (i
= 0; i
< ntparms
; ++i
)
5540 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
5541 tree targ
= TREE_VEC_ELT (targs
, i
);
5543 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
5545 tree packed_args
= NULL_TREE
;
5548 if (ARGUMENT_PACK_P (targ
))
5550 /* Look inside the argument pack. */
5551 packed_args
= ARGUMENT_PACK_ARGS (targ
);
5552 len
= TREE_VEC_LENGTH (packed_args
);
5555 for (idx
= 0; idx
< len
; ++idx
)
5557 tree targ_parms
= NULL_TREE
;
5560 /* Extract the next argument from the argument
5562 targ
= TREE_VEC_ELT (packed_args
, idx
);
5564 if (PACK_EXPANSION_P (targ
))
5565 /* Look at the pattern of the pack expansion. */
5566 targ
= PACK_EXPANSION_PATTERN (targ
);
5568 /* Extract the template parameters from the template
5570 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
5571 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
5572 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
5573 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
5575 /* Verify that we can coerce the template template
5576 parameters from the template argument to the template
5577 parameter. This requires an exact match. */
5579 && !coerce_template_template_parms
5580 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
5595 --processing_template_decl
;
5599 /* Convert the indicated template ARG as necessary to match the
5600 indicated template PARM. Returns the converted ARG, or
5601 error_mark_node if the conversion was unsuccessful. Error and
5602 warning messages are issued under control of COMPLAIN. This
5603 conversion is for the Ith parameter in the parameter list. ARGS is
5604 the full set of template arguments deduced so far. */
5607 convert_template_argument (tree parm
,
5610 tsubst_flags_t complain
,
5616 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
5618 if (TREE_CODE (arg
) == TREE_LIST
5619 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
5621 /* The template argument was the name of some
5622 member function. That's usually
5623 invalid, but static members are OK. In any
5624 case, grab the underlying fields/functions
5625 and issue an error later if required. */
5626 orig_arg
= TREE_VALUE (arg
);
5627 TREE_TYPE (arg
) = unknown_type_node
;
5632 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
5633 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
5634 || requires_tmpl_type
);
5636 /* When determining whether an argument pack expansion is a template,
5637 look at the pattern. */
5638 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
5639 arg
= PACK_EXPANSION_PATTERN (arg
);
5641 /* Deal with an injected-class-name used as a template template arg. */
5642 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
5644 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
5645 if (TREE_CODE (t
) == TEMPLATE_DECL
)
5647 if (complain
& tf_warning_or_error
)
5648 pedwarn (input_location
, OPT_pedantic
, "injected-class-name %qD"
5649 " used as template template argument", TYPE_NAME (arg
));
5650 else if (flag_pedantic_errors
)
5658 ((TREE_CODE (arg
) == TEMPLATE_DECL
5659 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
5660 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5661 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
5664 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
5665 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
5666 arg
= TYPE_STUB_DECL (arg
);
5668 is_type
= TYPE_P (arg
) || is_tmpl_type
;
5670 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
5671 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
5673 permerror (input_location
, "to refer to a type member of a template parameter, "
5674 "use %<typename %E%>", orig_arg
);
5676 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
5677 TREE_OPERAND (arg
, 1),
5679 complain
& tf_error
);
5683 if (is_type
!= requires_type
)
5687 if (complain
& tf_error
)
5689 error ("type/value mismatch at argument %d in template "
5690 "parameter list for %qD",
5693 error (" expected a constant of type %qT, got %qT",
5695 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
5696 else if (requires_tmpl_type
)
5697 error (" expected a class template, got %qE", orig_arg
);
5699 error (" expected a type, got %qE", orig_arg
);
5702 return error_mark_node
;
5704 if (is_tmpl_type
^ requires_tmpl_type
)
5706 if (in_decl
&& (complain
& tf_error
))
5708 error ("type/value mismatch at argument %d in template "
5709 "parameter list for %qD",
5712 error (" expected a type, got %qT", DECL_NAME (arg
));
5714 error (" expected a class template, got %qT", orig_arg
);
5716 return error_mark_node
;
5721 if (requires_tmpl_type
)
5723 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
5724 /* The number of argument required is not known yet.
5725 Just accept it for now. */
5726 val
= TREE_TYPE (arg
);
5729 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5732 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5734 if (coerce_template_template_parms (parmparm
, argparm
,
5740 /* TEMPLATE_TEMPLATE_PARM node is preferred over
5742 if (val
!= error_mark_node
)
5744 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
5745 val
= TREE_TYPE (val
);
5746 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
5747 val
= make_pack_expansion (val
);
5752 if (in_decl
&& (complain
& tf_error
))
5754 error ("type/value mismatch at argument %d in "
5755 "template parameter list for %qD",
5757 error (" expected a template of type %qD, got %qT",
5761 val
= error_mark_node
;
5767 /* We only form one instance of each template specialization.
5768 Therefore, if we use a non-canonical variant (i.e., a
5769 typedef), any future messages referring to the type will use
5770 the typedef, which is confusing if those future uses do not
5771 themselves also use the typedef. */
5773 val
= strip_typedefs (val
);
5777 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
5779 if (invalid_nontype_parm_type_p (t
, complain
))
5780 return error_mark_node
;
5782 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
5784 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
5788 /* Not sure if this is reachable, but it doesn't hurt
5790 error ("type mismatch in nontype parameter pack");
5791 val
= error_mark_node
;
5794 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
5795 /* We used to call digest_init here. However, digest_init
5796 will report errors, which we don't want when complain
5797 is zero. More importantly, digest_init will try too
5798 hard to convert things: for example, `0' should not be
5799 converted to pointer type at this point according to
5800 the standard. Accepting this is not merely an
5801 extension, since deciding whether or not these
5802 conversions can occur is part of determining which
5803 function template to call, or whether a given explicit
5804 argument specification is valid. */
5805 val
= convert_nontype_argument (t
, orig_arg
, complain
);
5809 if (val
== NULL_TREE
)
5810 val
= error_mark_node
;
5811 else if (val
== error_mark_node
&& (complain
& tf_error
))
5812 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
5814 if (TREE_CODE (val
) == SCOPE_REF
)
5816 /* Strip typedefs from the SCOPE_REF. */
5817 tree type
= strip_typedefs (TREE_TYPE (val
));
5818 tree scope
= strip_typedefs (TREE_OPERAND (val
, 0));
5819 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
5820 QUALIFIED_NAME_IS_TEMPLATE (val
));
5827 /* Coerces the remaining template arguments in INNER_ARGS (from
5828 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5829 Returns the coerced argument pack. PARM_IDX is the position of this
5830 parameter in the template parameter list. ARGS is the original
5831 template argument list. */
5833 coerce_template_parameter_pack (tree parms
,
5841 tsubst_flags_t complain
)
5843 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
5844 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
5847 tree packed_types
= NULL_TREE
;
5849 if (arg_idx
> nargs
)
5852 packed_args
= make_tree_vec (nargs
- arg_idx
);
5854 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
5855 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
5857 /* When the template parameter is a non-type template
5858 parameter pack whose type uses parameter packs, we need
5859 to look at each of the template arguments
5860 separately. Build a vector of the types for these
5861 non-type template parameters in PACKED_TYPES. */
5863 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
5864 packed_types
= tsubst_pack_expansion (expansion
, args
,
5867 if (packed_types
== error_mark_node
)
5868 return error_mark_node
;
5870 /* Check that we have the right number of arguments. */
5872 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
5873 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
5876 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
5877 error ("wrong number of template arguments (%d, should be %d)",
5878 nargs
, needed_parms
);
5879 return error_mark_node
;
5882 /* If we aren't able to check the actual arguments now
5883 (because they haven't been expanded yet), we can at least
5884 verify that all of the types used for the non-type
5885 template parameter pack are, in fact, valid for non-type
5886 template parameters. */
5888 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
5890 int j
, len
= TREE_VEC_LENGTH (packed_types
);
5891 for (j
= 0; j
< len
; ++j
)
5893 tree t
= TREE_VEC_ELT (packed_types
, j
);
5894 if (invalid_nontype_parm_type_p (t
, complain
))
5895 return error_mark_node
;
5900 /* Convert the remaining arguments, which will be a part of the
5901 parameter pack "parm". */
5902 for (; arg_idx
< nargs
; ++arg_idx
)
5904 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
5905 tree actual_parm
= TREE_VALUE (parm
);
5907 if (packed_types
&& !PACK_EXPANSION_P (arg
))
5909 /* When we have a vector of types (corresponding to the
5910 non-type template parameter pack that uses parameter
5911 packs in its type, as mention above), and the
5912 argument is not an expansion (which expands to a
5913 currently unknown number of arguments), clone the
5914 parm and give it the next type in PACKED_TYPES. */
5915 actual_parm
= copy_node (actual_parm
);
5916 TREE_TYPE (actual_parm
) =
5917 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
5920 if (arg
!= error_mark_node
)
5921 arg
= convert_template_argument (actual_parm
,
5922 arg
, new_args
, complain
, parm_idx
,
5924 if (arg
== error_mark_node
)
5926 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
5929 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
5930 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
5931 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
5934 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
5935 TREE_TYPE (argument_pack
)
5936 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
5937 TREE_CONSTANT (argument_pack
) = 1;
5940 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
5941 #ifdef ENABLE_CHECKING
5942 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
5943 TREE_VEC_LENGTH (packed_args
));
5945 return argument_pack
;
5948 /* Convert all template arguments to their appropriate types, and
5949 return a vector containing the innermost resulting template
5950 arguments. If any error occurs, return error_mark_node. Error and
5951 warning messages are issued under control of COMPLAIN.
5953 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
5954 for arguments not specified in ARGS. Otherwise, if
5955 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5956 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5957 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5961 coerce_template_parms (tree parms
,
5964 tsubst_flags_t complain
,
5965 bool require_all_args
,
5966 bool use_default_args
)
5968 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
5971 tree new_inner_args
;
5972 int saved_unevaluated_operand
;
5973 int saved_inhibit_evaluation_warnings
;
5975 /* When used as a boolean value, indicates whether this is a
5976 variadic template parameter list. Since it's an int, we can also
5977 subtract it from nparms to get the number of non-variadic
5981 if (args
== error_mark_node
)
5982 return error_mark_node
;
5984 nparms
= TREE_VEC_LENGTH (parms
);
5986 /* Determine if there are any parameter packs. */
5987 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
5989 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
5990 if (template_parameter_pack_p (tparm
))
5994 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
5995 /* If there are 0 or 1 parameter packs, we need to expand any argument
5996 packs so that we can deduce a parameter pack from some non-packed args
5997 followed by an argument pack, as in variadic85.C. If there are more
5998 than that, we need to leave argument packs intact so the arguments are
5999 assigned to the right parameter packs. This should only happen when
6000 dealing with a nested class inside a partial specialization of a class
6001 template, as in variadic92.C. */
6002 if (variadic_p
<= 1)
6003 inner_args
= expand_template_argument_pack (inner_args
);
6005 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6006 if ((nargs
> nparms
&& !variadic_p
)
6007 || (nargs
< nparms
- variadic_p
6009 && (!use_default_args
6010 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6011 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6013 if (complain
& tf_error
)
6018 error ("wrong number of template arguments "
6019 "(%d, should be %d or more)", nargs
, nparms
);
6022 error ("wrong number of template arguments "
6023 "(%d, should be %d)", nargs
, nparms
);
6026 error ("provided for %q+D", in_decl
);
6029 return error_mark_node
;
6032 /* We need to evaluate the template arguments, even though this
6033 template-id may be nested within a "sizeof". */
6034 saved_unevaluated_operand
= cp_unevaluated_operand
;
6035 cp_unevaluated_operand
= 0;
6036 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6037 c_inhibit_evaluation_warnings
= 0;
6038 new_inner_args
= make_tree_vec (nparms
);
6039 new_args
= add_outermost_template_args (args
, new_inner_args
);
6040 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6045 /* Get the Ith template parameter. */
6046 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6048 if (parm
== error_mark_node
)
6050 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6054 /* Calculate the next argument. */
6055 if (arg_idx
< nargs
)
6056 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6060 if (template_parameter_pack_p (TREE_VALUE (parm
))
6061 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6063 /* All remaining arguments will be placed in the
6064 template parameter pack PARM. */
6065 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6066 inner_args
, arg_idx
,
6070 /* Store this argument. */
6071 if (arg
== error_mark_node
)
6073 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6075 /* We are done with all of the arguments. */
6082 if (PACK_EXPANSION_P (arg
))
6084 if (complain
& tf_error
)
6086 /* FIXME this restriction was removed by N2555; see
6088 /* If ARG is a pack expansion, but PARM is not a
6089 template parameter pack (if it were, we would have
6090 handled it above), we're trying to expand into a
6091 fixed-length argument list. */
6092 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
6093 sorry ("cannot expand %<%E%> into a fixed-length "
6094 "argument list", arg
);
6096 sorry ("cannot expand %<%T%> into a fixed-length "
6097 "argument list", arg
);
6099 return error_mark_node
;
6102 else if (require_all_args
)
6104 /* There must be a default arg in this case. */
6105 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6107 /* The position of the first default template argument,
6108 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6110 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6111 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6116 if (arg
== error_mark_node
)
6118 if (complain
& tf_error
)
6119 error ("template argument %d is invalid", arg_idx
+ 1);
6122 /* This only occurs if there was an error in the template
6123 parameter list itself (which we would already have
6124 reported) that we are trying to recover from, e.g., a class
6125 template with a parameter list such as
6126 template<typename..., typename>. */
6127 return error_mark_node
;
6129 arg
= convert_template_argument (TREE_VALUE (parm
),
6130 arg
, new_args
, complain
,
6133 if (arg
== error_mark_node
)
6135 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6137 cp_unevaluated_operand
= saved_unevaluated_operand
;
6138 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6141 return error_mark_node
;
6143 #ifdef ENABLE_CHECKING
6144 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6145 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6146 TREE_VEC_LENGTH (new_inner_args
));
6149 return new_inner_args
;
6152 /* Returns 1 if template args OT and NT are equivalent. */
6155 template_args_equal (tree ot
, tree nt
)
6160 if (TREE_CODE (nt
) == TREE_VEC
)
6161 /* For member templates */
6162 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6163 else if (PACK_EXPANSION_P (ot
))
6164 return PACK_EXPANSION_P (nt
)
6165 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6166 PACK_EXPANSION_PATTERN (nt
));
6167 else if (ARGUMENT_PACK_P (ot
))
6172 if (!ARGUMENT_PACK_P (nt
))
6175 opack
= ARGUMENT_PACK_ARGS (ot
);
6176 npack
= ARGUMENT_PACK_ARGS (nt
);
6177 len
= TREE_VEC_LENGTH (opack
);
6178 if (TREE_VEC_LENGTH (npack
) != len
)
6180 for (i
= 0; i
< len
; ++i
)
6181 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6182 TREE_VEC_ELT (npack
, i
)))
6186 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6188 /* We get here probably because we are in the middle of substituting
6189 into the pattern of a pack expansion. In that case the
6190 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6191 interested in. So we want to use the initial pack argument for
6193 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6194 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6195 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6196 return template_args_equal (ot
, nt
);
6198 else if (TYPE_P (nt
))
6199 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6200 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6203 return cp_tree_equal (ot
, nt
);
6206 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6207 of template arguments. Returns 0 otherwise. */
6210 comp_template_args (tree oldargs
, tree newargs
)
6214 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6217 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6219 tree nt
= TREE_VEC_ELT (newargs
, i
);
6220 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6222 if (! template_args_equal (ot
, nt
))
6229 add_pending_template (tree d
)
6231 tree ti
= (TYPE_P (d
)
6232 ? CLASSTYPE_TEMPLATE_INFO (d
)
6233 : DECL_TEMPLATE_INFO (d
));
6234 struct pending_template
*pt
;
6237 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6240 /* We are called both from instantiate_decl, where we've already had a
6241 tinst_level pushed, and instantiate_template, where we haven't.
6243 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6246 push_tinst_level (d
);
6248 pt
= ggc_alloc_pending_template ();
6250 pt
->tinst
= current_tinst_level
;
6251 if (last_pending_template
)
6252 last_pending_template
->next
= pt
;
6254 pending_templates
= pt
;
6256 last_pending_template
= pt
;
6258 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6265 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6266 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6267 documentation for TEMPLATE_ID_EXPR. */
6270 lookup_template_function (tree fns
, tree arglist
)
6274 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6275 return error_mark_node
;
6277 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6278 gcc_assert (fns
&& (is_overloaded_fn (fns
)
6279 || TREE_CODE (fns
) == IDENTIFIER_NODE
));
6281 if (BASELINK_P (fns
))
6283 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6285 BASELINK_FUNCTIONS (fns
),
6290 type
= TREE_TYPE (fns
);
6291 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6292 type
= unknown_type_node
;
6294 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6297 /* Within the scope of a template class S<T>, the name S gets bound
6298 (in build_self_reference) to a TYPE_DECL for the class, not a
6299 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6300 or one of its enclosing classes, and that type is a template,
6301 return the associated TEMPLATE_DECL. Otherwise, the original
6304 Also handle the case when DECL is a TREE_LIST of ambiguous
6305 injected-class-names from different bases. */
6308 maybe_get_template_decl_from_type_decl (tree decl
)
6310 if (decl
== NULL_TREE
)
6313 /* DR 176: A lookup that finds an injected-class-name (10.2
6314 [class.member.lookup]) can result in an ambiguity in certain cases
6315 (for example, if it is found in more than one base class). If all of
6316 the injected-class-names that are found refer to specializations of
6317 the same class template, and if the name is followed by a
6318 template-argument-list, the reference refers to the class template
6319 itself and not a specialization thereof, and is not ambiguous. */
6320 if (TREE_CODE (decl
) == TREE_LIST
)
6322 tree t
, tmpl
= NULL_TREE
;
6323 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6325 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6328 else if (tmpl
!= elt
)
6331 if (tmpl
&& t
== NULL_TREE
)
6337 return (decl
!= NULL_TREE
6338 && DECL_SELF_REFERENCE_P (decl
)
6339 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6340 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
6343 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6344 parameters, find the desired type.
6346 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6348 IN_DECL, if non-NULL, is the template declaration we are trying to
6351 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6352 the class we are looking up.
6354 Issue error and warning messages under control of COMPLAIN.
6356 If the template class is really a local class in a template
6357 function, then the FUNCTION_CONTEXT is the function in which it is
6360 ??? Note that this function is currently called *twice* for each
6361 template-id: the first time from the parser, while creating the
6362 incomplete type (finish_template_type), and the second type during the
6363 real instantiation (instantiate_template_class). This is surely something
6364 that we want to avoid. It also causes some problems with argument
6365 coercion (see convert_nontype_argument for more information on this). */
6368 lookup_template_class (tree d1
,
6373 tsubst_flags_t complain
)
6375 tree templ
= NULL_TREE
, parmlist
;
6382 timevar_push (TV_NAME_LOOKUP
);
6384 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
6386 tree value
= innermost_non_namespace_value (d1
);
6387 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
6392 push_decl_namespace (context
);
6393 templ
= lookup_name (d1
);
6394 templ
= maybe_get_template_decl_from_type_decl (templ
);
6396 pop_decl_namespace ();
6399 context
= DECL_CONTEXT (templ
);
6401 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
6403 tree type
= TREE_TYPE (d1
);
6405 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6406 an implicit typename for the second A. Deal with it. */
6407 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
6408 type
= TREE_TYPE (type
);
6410 if (CLASSTYPE_TEMPLATE_INFO (type
))
6412 templ
= CLASSTYPE_TI_TEMPLATE (type
);
6413 d1
= DECL_NAME (templ
);
6416 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
6417 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
6419 templ
= TYPE_TI_TEMPLATE (d1
);
6420 d1
= DECL_NAME (templ
);
6422 else if (TREE_CODE (d1
) == TEMPLATE_DECL
6423 && DECL_TEMPLATE_RESULT (d1
)
6424 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
6427 d1
= DECL_NAME (templ
);
6428 context
= DECL_CONTEXT (templ
);
6431 /* Issue an error message if we didn't find a template. */
6434 if (complain
& tf_error
)
6435 error ("%qT is not a template", d1
);
6436 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6439 if (TREE_CODE (templ
) != TEMPLATE_DECL
6440 /* Make sure it's a user visible template, if it was named by
6442 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
6443 && !PRIMARY_TEMPLATE_P (templ
)))
6445 if (complain
& tf_error
)
6447 error ("non-template type %qT used as a template", d1
);
6449 error ("for template declaration %q+D", in_decl
);
6451 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6454 complain
&= ~tf_user
;
6456 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
6458 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
6459 template arguments */
6465 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
6467 /* Consider an example where a template template parameter declared as
6469 template <class T, class U = std::allocator<T> > class TT
6471 The template parameter level of T and U are one level larger than
6472 of TT. To proper process the default argument of U, say when an
6473 instantiation `TT<int>' is seen, we need to build the full
6474 arguments containing {int} as the innermost level. Outer levels,
6475 available when not appearing as default template argument, can be
6476 obtained from the arguments of the enclosing template.
6478 Suppose that TT is later substituted with std::vector. The above
6479 instantiation is `TT<int, std::allocator<T> >' with TT at
6480 level 1, and T at level 2, while the template arguments at level 1
6481 becomes {std::vector} and the inner level 2 is {int}. */
6483 outer
= DECL_CONTEXT (templ
);
6485 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
6486 else if (current_template_parms
)
6487 /* This is an argument of the current template, so we haven't set
6488 DECL_CONTEXT yet. */
6489 outer
= current_template_args ();
6492 arglist
= add_to_template_args (outer
, arglist
);
6494 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
6496 /*require_all_args=*/true,
6497 /*use_default_args=*/true);
6498 if (arglist2
== error_mark_node
6499 || (!uses_template_parms (arglist2
)
6500 && check_instantiated_args (templ
, arglist2
, complain
)))
6501 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6503 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
6504 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, parm
);
6508 tree template_type
= TREE_TYPE (templ
);
6511 tree found
= NULL_TREE
;
6514 int is_dependent_type
;
6515 int use_partial_inst_tmpl
= false;
6517 gen_tmpl
= most_general_template (templ
);
6518 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6519 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
6520 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6522 if (arg_depth
== 1 && parm_depth
> 1)
6524 /* We've been given an incomplete set of template arguments.
6527 template <class T> struct S1 {
6528 template <class U> struct S2 {};
6529 template <class U> struct S2<U*> {};
6532 we will be called with an ARGLIST of `U*', but the
6533 TEMPLATE will be `template <class T> template
6534 <class U> struct S1<T>::S2'. We must fill in the missing
6537 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
6539 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
6542 /* Now we should have enough arguments. */
6543 gcc_assert (parm_depth
== arg_depth
);
6545 /* From here on, we're only interested in the most general
6548 /* Calculate the BOUND_ARGS. These will be the args that are
6549 actually tsubst'd into the definition to create the
6553 /* We have multiple levels of arguments to coerce, at once. */
6555 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
6557 tree bound_args
= make_tree_vec (parm_depth
);
6559 for (i
= saved_depth
,
6560 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
6561 i
> 0 && t
!= NULL_TREE
;
6562 --i
, t
= TREE_CHAIN (t
))
6565 if (i
== saved_depth
)
6566 a
= coerce_template_parms (TREE_VALUE (t
),
6569 /*require_all_args=*/true,
6570 /*use_default_args=*/true);
6572 /* Outer levels should have already been coerced. */
6573 a
= TMPL_ARGS_LEVEL (arglist
, i
);
6575 /* Don't process further if one of the levels fails. */
6576 if (a
== error_mark_node
)
6578 /* Restore the ARGLIST to its full size. */
6579 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6580 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6583 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
6585 /* We temporarily reduce the length of the ARGLIST so
6586 that coerce_template_parms will see only the arguments
6587 corresponding to the template parameters it is
6589 TREE_VEC_LENGTH (arglist
)--;
6592 /* Restore the ARGLIST to its full size. */
6593 TREE_VEC_LENGTH (arglist
) = saved_depth
;
6595 arglist
= bound_args
;
6599 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
6600 INNERMOST_TEMPLATE_ARGS (arglist
),
6603 /*require_all_args=*/true,
6604 /*use_default_args=*/true);
6606 if (arglist
== error_mark_node
)
6607 /* We were unable to bind the arguments. */
6608 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6610 /* In the scope of a template class, explicit references to the
6611 template class refer to the type of the template, not any
6612 instantiation of it. For example, in:
6614 template <class T> class C { void f(C<T>); }
6616 the `C<T>' is just the same as `C'. Outside of the
6617 class, however, such a reference is an instantiation. */
6619 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
6620 || currently_open_class (template_type
))
6621 /* comp_template_args is expensive, check it last. */
6622 && comp_template_args (TYPE_TI_ARGS (template_type
),
6624 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, template_type
);
6626 /* If we already have this specialization, return it. */
6627 elt
.tmpl
= gen_tmpl
;
6629 hash
= hash_specialization (&elt
);
6630 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
6634 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, entry
->spec
);
6636 is_dependent_type
= uses_template_parms (arglist
);
6638 /* If the deduced arguments are invalid, then the binding
6640 if (!is_dependent_type
6641 && check_instantiated_args (gen_tmpl
,
6642 INNERMOST_TEMPLATE_ARGS (arglist
),
6644 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, error_mark_node
);
6646 if (!is_dependent_type
6647 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
6648 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
6649 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
6651 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
6652 DECL_NAME (gen_tmpl
),
6653 /*tag_scope=*/ts_global
);
6654 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, found
);
6657 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
6660 context
= global_namespace
;
6662 /* Create the type. */
6663 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
6665 if (!is_dependent_type
)
6667 set_current_access_from_decl (TYPE_NAME (template_type
));
6668 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
6669 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
6670 arglist
, complain
, in_decl
),
6671 SCOPED_ENUM_P (template_type
), NULL
);
6675 /* We don't want to call start_enum for this type, since
6676 the values for the enumeration constants may involve
6677 template parameters. And, no one should be interested
6678 in the enumeration constants for such a type. */
6679 t
= cxx_make_type (ENUMERAL_TYPE
);
6680 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
6682 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
6686 t
= make_class_type (TREE_CODE (template_type
));
6687 CLASSTYPE_DECLARED_CLASS (t
)
6688 = CLASSTYPE_DECLARED_CLASS (template_type
);
6689 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
6690 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
6692 /* A local class. Make sure the decl gets registered properly. */
6693 if (context
== current_function_decl
)
6694 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
6696 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
6697 /* This instantiation is another name for the primary
6698 template type. Set the TYPE_CANONICAL field
6700 TYPE_CANONICAL (t
) = template_type
;
6701 else if (any_template_arguments_need_structural_equality_p (arglist
))
6702 /* Some of the template arguments require structural
6703 equality testing, so this template class requires
6704 structural equality testing. */
6705 SET_TYPE_STRUCTURAL_EQUALITY (t
);
6708 /* If we called start_enum or pushtag above, this information
6709 will already be set up. */
6712 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
6714 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
6715 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
6716 DECL_SOURCE_LOCATION (type_decl
)
6717 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
6720 type_decl
= TYPE_NAME (t
);
6722 TREE_PRIVATE (type_decl
)
6723 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
6724 TREE_PROTECTED (type_decl
)
6725 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
6726 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
6728 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
6729 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
6732 /* Let's consider the explicit specialization of a member
6733 of a class template specialization that is implicitely instantiated,
6738 template<class U> struct M {}; //#0
6743 struct S<int>::M<char> //#1
6747 [temp.expl.spec]/4 says this is valid.
6749 In this case, when we write:
6752 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
6755 When we encounter #1, we want to store the partial instantiation
6756 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
6758 For all cases other than this "explicit specialization of member of a
6759 class template", we just want to store the most general template into
6760 the CLASSTYPE_TI_TEMPLATE of M.
6762 This case of "explicit specialization of member of a class template"
6764 1/ the enclosing class is an instantiation of, and therefore not
6765 the same as, the context of the most general template, and
6766 2/ we aren't looking at the partial instantiation itself, i.e.
6767 the innermost arguments are not the same as the innermost parms of
6768 the most general template.
6770 So it's only when 1/ and 2/ happens that we want to use the partial
6771 instantiation of the member template in lieu of its most general
6774 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
6775 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
6776 /* the enclosing class must be an instantiation... */
6777 && CLASS_TYPE_P (context
)
6778 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
6780 tree partial_inst_args
;
6781 TREE_VEC_LENGTH (arglist
)--;
6782 ++processing_template_decl
;
6784 tsubst (INNERMOST_TEMPLATE_ARGS
6785 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
6786 arglist
, complain
, NULL_TREE
);
6787 --processing_template_decl
;
6788 TREE_VEC_LENGTH (arglist
)++;
6789 use_partial_inst_tmpl
=
6790 /*...and we must not be looking at the partial instantiation
6792 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
6796 if (!use_partial_inst_tmpl
)
6797 /* This case is easy; there are no member templates involved. */
6801 /* This is a full instantiation of a member template. Find
6802 the partial instantiation of which this is an instance. */
6804 /* Temporarily reduce by one the number of levels in the ARGLIST
6805 so as to avoid comparing the last set of arguments. */
6806 TREE_VEC_LENGTH (arglist
)--;
6807 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
6808 TREE_VEC_LENGTH (arglist
)++;
6809 found
= CLASSTYPE_TI_TEMPLATE (found
);
6812 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
6815 slot
= (spec_entry
**) htab_find_slot_with_hash (type_specializations
,
6816 &elt
, hash
, INSERT
);
6817 *slot
= ggc_alloc_spec_entry ();
6820 /* Note this use of the partial instantiation so we can check it
6821 later in maybe_process_partial_specialization. */
6822 DECL_TEMPLATE_INSTANTIATIONS (templ
)
6823 = tree_cons (arglist
, t
,
6824 DECL_TEMPLATE_INSTANTIATIONS (templ
));
6826 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
6827 /* Now that the type has been registered on the instantiations
6828 list, we set up the enumerators. Because the enumeration
6829 constants may involve the enumeration type itself, we make
6830 sure to register the type first, and then create the
6831 constants. That way, doing tsubst_expr for the enumeration
6832 constants won't result in recursive calls here; we'll find
6833 the instantiation and exit above. */
6834 tsubst_enum (template_type
, t
, arglist
);
6836 if (is_dependent_type
)
6837 /* If the type makes use of template parameters, the
6838 code that generates debugging information will crash. */
6839 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
6841 /* Possibly limit visibility based on template args. */
6842 TREE_PUBLIC (type_decl
) = 1;
6843 determine_visibility (type_decl
);
6845 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP
, t
);
6847 timevar_pop (TV_NAME_LOOKUP
);
6854 /* True when we should also visit template parameters that occur in
6855 non-deduced contexts. */
6856 bool include_nondeduced_p
;
6857 struct pointer_set_t
*visited
;
6860 /* Called from for_each_template_parm via walk_tree. */
6863 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
6866 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
6867 tree_fn_t fn
= pfd
->fn
;
6868 void *data
= pfd
->data
;
6871 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
6872 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
6873 pfd
->include_nondeduced_p
))
6874 return error_mark_node
;
6876 switch (TREE_CODE (t
))
6879 if (TYPE_PTRMEMFUNC_P (t
))
6885 if (!TYPE_TEMPLATE_INFO (t
))
6887 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
6888 fn
, data
, pfd
->visited
,
6889 pfd
->include_nondeduced_p
))
6890 return error_mark_node
;
6894 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
6895 fn
, data
, pfd
->visited
,
6896 pfd
->include_nondeduced_p
)
6897 || for_each_template_parm (TYPE_MAX_VALUE (t
),
6898 fn
, data
, pfd
->visited
,
6899 pfd
->include_nondeduced_p
))
6900 return error_mark_node
;
6904 /* Since we're not going to walk subtrees, we have to do this
6906 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
6907 pfd
->visited
, pfd
->include_nondeduced_p
))
6908 return error_mark_node
;
6912 /* Check the return type. */
6913 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
6914 pfd
->include_nondeduced_p
))
6915 return error_mark_node
;
6917 /* Check the parameter types. Since default arguments are not
6918 instantiated until they are needed, the TYPE_ARG_TYPES may
6919 contain expressions that involve template parameters. But,
6920 no-one should be looking at them yet. And, once they're
6921 instantiated, they don't contain template parameters, so
6922 there's no point in looking at them then, either. */
6926 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
6927 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
6928 pfd
->visited
, pfd
->include_nondeduced_p
))
6929 return error_mark_node
;
6931 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6932 want walk_tree walking into them itself. */
6938 if (pfd
->include_nondeduced_p
6939 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
6941 pfd
->include_nondeduced_p
))
6942 return error_mark_node
;
6947 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
6948 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
6949 pfd
->visited
, pfd
->include_nondeduced_p
))
6950 return error_mark_node
;
6955 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
6956 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
6957 pfd
->visited
, pfd
->include_nondeduced_p
))
6958 return error_mark_node
;
6959 if (DECL_CONTEXT (t
)
6960 && pfd
->include_nondeduced_p
6961 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
6962 pfd
->visited
, pfd
->include_nondeduced_p
))
6963 return error_mark_node
;
6966 case BOUND_TEMPLATE_TEMPLATE_PARM
:
6967 /* Record template parameters such as `T' inside `TT<T>'. */
6968 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
6969 pfd
->include_nondeduced_p
))
6970 return error_mark_node
;
6973 case TEMPLATE_TEMPLATE_PARM
:
6974 case TEMPLATE_TYPE_PARM
:
6975 case TEMPLATE_PARM_INDEX
:
6976 if (fn
&& (*fn
)(t
, data
))
6977 return error_mark_node
;
6979 return error_mark_node
;
6983 /* A template template parameter is encountered. */
6984 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
6985 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
6986 pfd
->include_nondeduced_p
))
6987 return error_mark_node
;
6989 /* Already substituted template template parameter */
6995 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
6997 pfd
->include_nondeduced_p
))
6998 return error_mark_node
;
7002 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7003 && pfd
->include_nondeduced_p
7004 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7005 (TREE_TYPE (t
)), fn
, data
,
7006 pfd
->visited
, pfd
->include_nondeduced_p
))
7007 return error_mark_node
;
7012 /* If there's no type, then this thing must be some expression
7013 involving template parameters. */
7014 if (!fn
&& !TREE_TYPE (t
))
7015 return error_mark_node
;
7020 case REINTERPRET_CAST_EXPR
:
7021 case CONST_CAST_EXPR
:
7022 case STATIC_CAST_EXPR
:
7023 case DYNAMIC_CAST_EXPR
:
7027 case PSEUDO_DTOR_EXPR
:
7029 return error_mark_node
;
7036 /* We didn't find any template parameters we liked. */
7040 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7041 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7042 call FN with the parameter and the DATA.
7043 If FN returns nonzero, the iteration is terminated, and
7044 for_each_template_parm returns 1. Otherwise, the iteration
7045 continues. If FN never returns a nonzero value, the value
7046 returned by for_each_template_parm is 0. If FN is NULL, it is
7047 considered to be the function which always returns 1.
7049 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7050 parameters that occur in non-deduced contexts. When false, only
7051 visits those template parameters that can be deduced. */
7054 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7055 struct pointer_set_t
*visited
,
7056 bool include_nondeduced_p
)
7058 struct pair_fn_data pfd
;
7064 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7066 /* Walk the tree. (Conceptually, we would like to walk without
7067 duplicates, but for_each_template_parm_r recursively calls
7068 for_each_template_parm, so we would need to reorganize a fair
7069 bit to use walk_tree_without_duplicates, so we keep our own
7072 pfd
.visited
= visited
;
7074 pfd
.visited
= pointer_set_create ();
7075 result
= cp_walk_tree (&t
,
7076 for_each_template_parm_r
,
7078 pfd
.visited
) != NULL_TREE
;
7083 pointer_set_destroy (pfd
.visited
);
7090 /* Returns true if T depends on any template parameter. */
7093 uses_template_parms (tree t
)
7096 int saved_processing_template_decl
;
7098 saved_processing_template_decl
= processing_template_decl
;
7099 if (!saved_processing_template_decl
)
7100 processing_template_decl
= 1;
7102 dependent_p
= dependent_type_p (t
);
7103 else if (TREE_CODE (t
) == TREE_VEC
)
7104 dependent_p
= any_dependent_template_arguments_p (t
);
7105 else if (TREE_CODE (t
) == TREE_LIST
)
7106 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7107 || uses_template_parms (TREE_CHAIN (t
)));
7108 else if (TREE_CODE (t
) == TYPE_DECL
)
7109 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7112 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7113 || TREE_CODE (t
) == OVERLOAD
7114 || TREE_CODE (t
) == BASELINK
7115 || TREE_CODE (t
) == IDENTIFIER_NODE
7116 || TREE_CODE (t
) == TRAIT_EXPR
7117 || TREE_CODE (t
) == CONSTRUCTOR
7118 || CONSTANT_CLASS_P (t
))
7119 dependent_p
= (type_dependent_expression_p (t
)
7120 || value_dependent_expression_p (t
));
7123 gcc_assert (t
== error_mark_node
);
7124 dependent_p
= false;
7127 processing_template_decl
= saved_processing_template_decl
;
7132 /* Returns true if T depends on any template parameter with level LEVEL. */
7135 uses_template_parms_level (tree t
, int level
)
7137 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7138 /*include_nondeduced_p=*/true);
7141 static int tinst_depth
;
7142 extern int max_tinst_depth
;
7143 #ifdef GATHER_STATISTICS
7146 static int tinst_level_tick
;
7147 static int last_template_error_tick
;
7149 /* We're starting to instantiate D; record the template instantiation context
7150 for diagnostics and to restore it later. */
7153 push_tinst_level (tree d
)
7155 struct tinst_level
*new_level
;
7157 if (tinst_depth
>= max_tinst_depth
)
7159 /* If the instantiation in question still has unbound template parms,
7160 we don't really care if we can't instantiate it, so just return.
7161 This happens with base instantiation for implicit `typename'. */
7162 if (uses_template_parms (d
))
7165 last_template_error_tick
= tinst_level_tick
;
7166 error ("template instantiation depth exceeds maximum of %d (use "
7167 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7168 max_tinst_depth
, d
);
7170 print_instantiation_context ();
7175 new_level
= ggc_alloc_tinst_level ();
7176 new_level
->decl
= d
;
7177 new_level
->locus
= input_location
;
7178 new_level
->in_system_header_p
= in_system_header
;
7179 new_level
->next
= current_tinst_level
;
7180 current_tinst_level
= new_level
;
7183 #ifdef GATHER_STATISTICS
7184 if (tinst_depth
> depth_reached
)
7185 depth_reached
= tinst_depth
;
7192 /* We're done instantiating this template; return to the instantiation
7196 pop_tinst_level (void)
7198 /* Restore the filename and line number stashed away when we started
7199 this instantiation. */
7200 input_location
= current_tinst_level
->locus
;
7201 current_tinst_level
= current_tinst_level
->next
;
7206 /* We're instantiating a deferred template; restore the template
7207 instantiation context in which the instantiation was requested, which
7208 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7211 reopen_tinst_level (struct tinst_level
*level
)
7213 struct tinst_level
*t
;
7216 for (t
= level
; t
; t
= t
->next
)
7219 current_tinst_level
= level
;
7224 /* Returns the TINST_LEVEL which gives the original instantiation
7227 struct tinst_level
*
7228 outermost_tinst_level (void)
7230 struct tinst_level
*level
= current_tinst_level
;
7233 level
= level
->next
;
7237 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7240 parameter_of_template_p (tree parm
, tree templ
)
7245 if (!parm
|| !templ
)
7248 gcc_assert (DECL_TEMPLATE_PARM_P (parm
));
7249 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7251 parms
= DECL_TEMPLATE_PARMS (templ
);
7252 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
7254 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
7255 if (parm
== TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
7261 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7262 vector of template arguments, as for tsubst.
7264 Returns an appropriate tsubst'd friend declaration. */
7267 tsubst_friend_function (tree decl
, tree args
)
7271 if (TREE_CODE (decl
) == FUNCTION_DECL
7272 && DECL_TEMPLATE_INSTANTIATION (decl
)
7273 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
7274 /* This was a friend declared with an explicit template
7275 argument list, e.g.:
7279 to indicate that f was a template instantiation, not a new
7280 function declaration. Now, we have to figure out what
7281 instantiation of what template. */
7283 tree template_id
, arglist
, fns
;
7286 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
7288 /* Friend functions are looked up in the containing namespace scope.
7289 We must enter that scope, to avoid finding member functions of the
7290 current class with same name. */
7291 push_nested_namespace (ns
);
7292 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
7293 tf_warning_or_error
, NULL_TREE
,
7294 /*integral_constant_expression_p=*/false);
7295 pop_nested_namespace (ns
);
7296 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
7297 tf_warning_or_error
, NULL_TREE
);
7298 template_id
= lookup_template_function (fns
, arglist
);
7300 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7301 tmpl
= determine_specialization (template_id
, new_friend
,
7303 /*need_member_template=*/0,
7304 TREE_VEC_LENGTH (args
),
7306 return instantiate_template (tmpl
, new_args
, tf_error
);
7309 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7311 /* The NEW_FRIEND will look like an instantiation, to the
7312 compiler, but is not an instantiation from the point of view of
7313 the language. For example, we might have had:
7315 template <class T> struct S {
7316 template <class U> friend void f(T, U);
7319 Then, in S<int>, template <class U> void f(int, U) is not an
7320 instantiation of anything. */
7321 if (new_friend
== error_mark_node
)
7322 return error_mark_node
;
7324 DECL_USE_TEMPLATE (new_friend
) = 0;
7325 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7327 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
7328 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
7329 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
7332 /* The mangled name for the NEW_FRIEND is incorrect. The function
7333 is not a template instantiation and should not be mangled like
7334 one. Therefore, we forget the mangling here; we'll recompute it
7335 later if we need it. */
7336 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
7338 SET_DECL_RTL (new_friend
, NULL
);
7339 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
7342 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
7345 tree new_friend_template_info
;
7346 tree new_friend_result_template_info
;
7348 int new_friend_is_defn
;
7350 /* We must save some information from NEW_FRIEND before calling
7351 duplicate decls since that function will free NEW_FRIEND if
7353 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
7354 new_friend_is_defn
=
7355 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7356 (template_for_substitution (new_friend
)))
7358 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
7360 /* This declaration is a `primary' template. */
7361 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
7363 new_friend_result_template_info
7364 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
7367 new_friend_result_template_info
= NULL_TREE
;
7369 /* Make the init_value nonzero so pushdecl knows this is a defn. */
7370 if (new_friend_is_defn
)
7371 DECL_INITIAL (new_friend
) = error_mark_node
;
7373 /* Inside pushdecl_namespace_level, we will push into the
7374 current namespace. However, the friend function should go
7375 into the namespace of the template. */
7376 ns
= decl_namespace_context (new_friend
);
7377 push_nested_namespace (ns
);
7378 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
7379 pop_nested_namespace (ns
);
7381 if (old_decl
== error_mark_node
)
7382 return error_mark_node
;
7384 if (old_decl
!= new_friend
)
7386 /* This new friend declaration matched an existing
7387 declaration. For example, given:
7389 template <class T> void f(T);
7390 template <class U> class C {
7391 template <class T> friend void f(T) {}
7394 the friend declaration actually provides the definition
7395 of `f', once C has been instantiated for some type. So,
7396 old_decl will be the out-of-class template declaration,
7397 while new_friend is the in-class definition.
7399 But, if `f' was called before this point, the
7400 instantiation of `f' will have DECL_TI_ARGS corresponding
7401 to `T' but not to `U', references to which might appear
7402 in the definition of `f'. Previously, the most general
7403 template for an instantiation of `f' was the out-of-class
7404 version; now it is the in-class version. Therefore, we
7405 run through all specialization of `f', adding to their
7406 DECL_TI_ARGS appropriately. In particular, they need a
7407 new set of outer arguments, corresponding to the
7408 arguments for this class instantiation.
7410 The same situation can arise with something like this:
7413 template <class T> class C {
7417 when `C<int>' is instantiated. Now, `f(int)' is defined
7420 if (!new_friend_is_defn
)
7421 /* On the other hand, if the in-class declaration does
7422 *not* provide a definition, then we don't want to alter
7423 existing definitions. We can just leave everything
7428 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
7429 tree new_args
= TI_ARGS (new_friend_template_info
);
7431 /* Overwrite whatever template info was there before, if
7432 any, with the new template information pertaining to
7434 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
7436 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
7438 /* We should have called reregister_specialization in
7440 gcc_assert (retrieve_specialization (new_template
,
7444 /* Instantiate it if the global has already been used. */
7445 if (DECL_ODR_USED (old_decl
))
7446 instantiate_decl (old_decl
, /*defer_ok=*/true,
7447 /*expl_inst_class_mem_p=*/false);
7453 /* Indicate that the old function template is a partial
7455 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
7456 = new_friend_result_template_info
;
7458 gcc_assert (new_template
7459 == most_general_template (new_template
));
7460 gcc_assert (new_template
!= old_decl
);
7462 /* Reassign any specializations already in the hash table
7463 to the new more general template, and add the
7464 additional template args. */
7465 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
7469 tree spec
= TREE_VALUE (t
);
7472 elt
.tmpl
= old_decl
;
7473 elt
.args
= DECL_TI_ARGS (spec
);
7474 elt
.spec
= NULL_TREE
;
7476 htab_remove_elt (decl_specializations
, &elt
);
7479 = add_outermost_template_args (new_args
,
7480 DECL_TI_ARGS (spec
));
7482 register_specialization
7483 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
7486 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
7490 /* The information from NEW_FRIEND has been merged into OLD_DECL
7491 by duplicate_decls. */
7492 new_friend
= old_decl
;
7497 tree context
= DECL_CONTEXT (new_friend
);
7501 template <class T> class C {
7502 template <class U> friend void C1<U>::f (); // case 1
7503 friend void C2<T>::f (); // case 2
7505 we only need to make sure CONTEXT is a complete type for
7506 case 2. To distinguish between the two cases, we note that
7507 CONTEXT of case 1 remains dependent type after tsubst while
7508 this isn't true for case 2. */
7509 ++processing_template_decl
;
7510 dependent_p
= dependent_type_p (context
);
7511 --processing_template_decl
;
7514 && !complete_type_or_else (context
, NULL_TREE
))
7515 return error_mark_node
;
7517 if (COMPLETE_TYPE_P (context
))
7519 /* Check to see that the declaration is really present, and,
7520 possibly obtain an improved declaration. */
7521 tree fn
= check_classfn (context
,
7522 new_friend
, NULL_TREE
);
7532 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7533 template arguments, as for tsubst.
7535 Returns an appropriate tsubst'd friend type or error_mark_node on
7539 tsubst_friend_class (tree friend_tmpl
, tree args
)
7545 context
= CP_DECL_CONTEXT (friend_tmpl
);
7547 if (context
!= global_namespace
)
7549 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7550 push_nested_namespace (context
);
7552 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
7555 /* Look for a class template declaration. We look for hidden names
7556 because two friend declarations of the same template are the
7557 same. For example, in:
7560 template <typename> friend class F;
7562 template <typename> struct B {
7563 template <typename> friend class F;
7566 both F templates are the same. */
7567 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
7568 /*block_p=*/true, 0,
7569 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
7571 /* But, if we don't find one, it might be because we're in a
7572 situation like this:
7580 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7581 for `S<int>', not the TEMPLATE_DECL. */
7582 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
7584 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
7585 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
7588 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
7590 /* The friend template has already been declared. Just
7591 check to see that the declarations match, and install any new
7592 default parameters. We must tsubst the default parameters,
7593 of course. We only need the innermost template parameters
7594 because that is all that redeclare_class_template will look
7596 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
7597 > TMPL_ARGS_DEPTH (args
))
7600 location_t saved_input_location
;
7601 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
7602 args
, tf_warning_or_error
);
7604 saved_input_location
= input_location
;
7605 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
7606 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
7607 input_location
= saved_input_location
;
7611 friend_type
= TREE_TYPE (tmpl
);
7615 /* The friend template has not already been declared. In this
7616 case, the instantiation of the template class will cause the
7617 injection of this template into the global scope. */
7618 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
7619 if (tmpl
== error_mark_node
)
7620 return error_mark_node
;
7622 /* The new TMPL is not an instantiation of anything, so we
7623 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
7624 the new type because that is supposed to be the corresponding
7625 template decl, i.e., TMPL. */
7626 DECL_USE_TEMPLATE (tmpl
) = 0;
7627 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
7628 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
7629 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
7630 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
7632 /* Inject this template into the global scope. */
7633 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
7636 if (context
!= global_namespace
)
7638 if (TREE_CODE (context
) == NAMESPACE_DECL
)
7639 pop_nested_namespace (context
);
7641 pop_nested_class ();
7647 /* Returns zero if TYPE cannot be completed later due to circularity.
7648 Otherwise returns one. */
7651 can_complete_type_without_circularity (tree type
)
7653 if (type
== NULL_TREE
|| type
== error_mark_node
)
7655 else if (COMPLETE_TYPE_P (type
))
7657 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
7658 return can_complete_type_without_circularity (TREE_TYPE (type
));
7659 else if (CLASS_TYPE_P (type
)
7660 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
7666 /* Apply any attributes which had to be deferred until instantiation
7667 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7668 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7671 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
7672 tree args
, tsubst_flags_t complain
, tree in_decl
)
7674 tree last_dep
= NULL_TREE
;
7678 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
7679 if (ATTR_IS_DEPENDENT (t
))
7682 attributes
= copy_list (attributes
);
7686 if (DECL_P (*decl_p
))
7688 if (TREE_TYPE (*decl_p
) == error_mark_node
)
7690 p
= &DECL_ATTRIBUTES (*decl_p
);
7693 p
= &TYPE_ATTRIBUTES (*decl_p
);
7697 tree late_attrs
= NULL_TREE
;
7698 tree
*q
= &late_attrs
;
7700 for (*p
= attributes
; *p
; )
7703 if (ATTR_IS_DEPENDENT (t
))
7705 *p
= TREE_CHAIN (t
);
7706 TREE_CHAIN (t
) = NULL_TREE
;
7707 /* If the first attribute argument is an identifier, don't
7708 pass it through tsubst. Attributes like mode, format,
7709 cleanup and several target specific attributes expect it
7712 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
7713 && TREE_VALUE (TREE_VALUE (t
))
7714 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
7715 == IDENTIFIER_NODE
))
7718 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
7720 /*integral_constant_expression_p=*/false);
7721 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
7723 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
7728 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
7729 /*integral_constant_expression_p=*/false);
7731 q
= &TREE_CHAIN (t
);
7734 p
= &TREE_CHAIN (t
);
7737 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
7741 /* Perform (or defer) access check for typedefs that were referenced
7742 from within the template TMPL code.
7743 This is a subroutine of instantiate_template and instantiate_class_template.
7744 TMPL is the template to consider and TARGS is the list of arguments of
7748 perform_typedefs_access_check (tree tmpl
, tree targs
)
7750 location_t saved_location
;
7752 qualified_typedef_usage_t
*iter
;
7755 || (!CLASS_TYPE_P (tmpl
)
7756 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
7759 saved_location
= input_location
;
7760 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
7761 get_types_needing_access_check (tmpl
),
7764 tree type_decl
= iter
->typedef_decl
;
7765 tree type_scope
= iter
->context
;
7767 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
7770 if (uses_template_parms (type_decl
))
7771 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
7772 if (uses_template_parms (type_scope
))
7773 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
7775 /* Make access check error messages point to the location
7776 of the use of the typedef. */
7777 input_location
= iter
->locus
;
7778 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
7779 type_decl
, type_decl
);
7781 input_location
= saved_location
;
7785 instantiate_class_template (tree type
)
7787 tree templ
, args
, pattern
, t
, member
;
7791 unsigned int saved_maximum_field_alignment
;
7793 if (type
== error_mark_node
)
7794 return error_mark_node
;
7796 if (COMPLETE_OR_OPEN_TYPE_P (type
)
7797 || uses_template_parms (type
))
7800 /* Figure out which template is being instantiated. */
7801 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
7802 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7804 /* Determine what specialization of the original template to
7806 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
7807 if (t
== error_mark_node
)
7809 TYPE_BEING_DEFINED (type
) = 1;
7810 return error_mark_node
;
7814 /* This TYPE is actually an instantiation of a partial
7815 specialization. We replace the innermost set of ARGS with
7816 the arguments appropriate for substitution. For example,
7819 template <class T> struct S {};
7820 template <class T> struct S<T*> {};
7822 and supposing that we are instantiating S<int*>, ARGS will
7823 presently be {int*} -- but we need {int}. */
7824 pattern
= TREE_TYPE (t
);
7825 args
= TREE_PURPOSE (t
);
7829 pattern
= TREE_TYPE (templ
);
7830 args
= CLASSTYPE_TI_ARGS (type
);
7833 /* If the template we're instantiating is incomplete, then clearly
7834 there's nothing we can do. */
7835 if (!COMPLETE_TYPE_P (pattern
))
7838 /* If we've recursively instantiated too many templates, stop. */
7839 if (! push_tinst_level (type
))
7842 /* Now we're really doing the instantiation. Mark the type as in
7843 the process of being defined. */
7844 TYPE_BEING_DEFINED (type
) = 1;
7846 /* We may be in the middle of deferred access check. Disable
7848 push_deferring_access_checks (dk_no_deferred
);
7850 push_to_top_level ();
7851 /* Use #pragma pack from the template context. */
7852 saved_maximum_field_alignment
= maximum_field_alignment
;
7853 maximum_field_alignment
= TYPE_PRECISION (pattern
);
7855 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
7857 /* Set the input location to the most specialized template definition.
7858 This is needed if tsubsting causes an error. */
7859 typedecl
= TYPE_MAIN_DECL (pattern
);
7860 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
7861 DECL_SOURCE_LOCATION (typedecl
);
7863 TYPE_HAS_USER_CONSTRUCTOR (type
) = TYPE_HAS_USER_CONSTRUCTOR (pattern
);
7864 TYPE_HAS_NEW_OPERATOR (type
) = TYPE_HAS_NEW_OPERATOR (pattern
);
7865 TYPE_HAS_ARRAY_NEW_OPERATOR (type
) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern
);
7866 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
7867 TYPE_HAS_COPY_ASSIGN (type
) = TYPE_HAS_COPY_ASSIGN (pattern
);
7868 TYPE_HAS_CONST_COPY_ASSIGN (type
) = TYPE_HAS_CONST_COPY_ASSIGN (pattern
);
7869 TYPE_HAS_COPY_CTOR (type
) = TYPE_HAS_COPY_CTOR (pattern
);
7870 TYPE_HAS_CONST_COPY_CTOR (type
) = TYPE_HAS_CONST_COPY_CTOR (pattern
);
7871 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
7872 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
7873 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
7874 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
7875 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
7876 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
7877 if (ANON_AGGR_TYPE_P (pattern
))
7878 SET_ANON_AGGR_TYPE_P (type
);
7879 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
7881 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
7882 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
7885 pbinfo
= TYPE_BINFO (pattern
);
7887 /* We should never instantiate a nested class before its enclosing
7888 class; we need to look up the nested class by name before we can
7889 instantiate it, and that lookup should instantiate the enclosing
7891 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
7892 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
7894 base_list
= NULL_TREE
;
7895 if (BINFO_N_BASE_BINFOS (pbinfo
))
7901 /* We must enter the scope containing the type, as that is where
7902 the accessibility of types named in dependent bases are
7904 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
7906 /* Substitute into each of the bases to determine the actual
7908 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
7911 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
7912 tree expanded_bases
= NULL_TREE
;
7915 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
7918 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
7919 args
, tf_error
, NULL_TREE
);
7920 if (expanded_bases
== error_mark_node
)
7923 len
= TREE_VEC_LENGTH (expanded_bases
);
7926 for (idx
= 0; idx
< len
; idx
++)
7929 /* Extract the already-expanded base class. */
7930 base
= TREE_VEC_ELT (expanded_bases
, idx
);
7932 /* Substitute to figure out the base class. */
7933 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
7936 if (base
== error_mark_node
)
7939 base_list
= tree_cons (access
, base
, base_list
);
7940 if (BINFO_VIRTUAL_P (pbase_binfo
))
7941 TREE_TYPE (base_list
) = integer_type_node
;
7945 /* The list is now in reverse order; correct that. */
7946 base_list
= nreverse (base_list
);
7949 pop_scope (pushed_scope
);
7951 /* Now call xref_basetypes to set up all the base-class
7953 xref_basetypes (type
, base_list
);
7955 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
7956 (int) ATTR_FLAG_TYPE_IN_PLACE
,
7957 args
, tf_error
, NULL_TREE
);
7958 fixup_attribute_variants (type
);
7960 /* Now that our base classes are set up, enter the scope of the
7961 class, so that name lookups into base classes, etc. will work
7962 correctly. This is precisely analogous to what we do in
7963 begin_class_definition when defining an ordinary non-template
7964 class, except we also need to push the enclosing classes. */
7965 push_nested_class (type
);
7967 /* Now members are processed in the order of declaration. */
7968 for (member
= CLASSTYPE_DECL_LIST (pattern
);
7969 member
; member
= TREE_CHAIN (member
))
7971 tree t
= TREE_VALUE (member
);
7973 if (TREE_PURPOSE (member
))
7977 /* Build new CLASSTYPE_NESTED_UTDS. */
7980 bool class_template_p
;
7982 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
7983 && TYPE_LANG_SPECIFIC (t
)
7984 && CLASSTYPE_IS_TEMPLATE (t
));
7985 /* If the member is a class template, then -- even after
7986 substitution -- there may be dependent types in the
7987 template argument list for the class. We increment
7988 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7989 that function will assume that no types are dependent
7990 when outside of a template. */
7991 if (class_template_p
)
7992 ++processing_template_decl
;
7993 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
7994 if (class_template_p
)
7995 --processing_template_decl
;
7996 if (newtag
== error_mark_node
)
7999 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8001 tree name
= TYPE_IDENTIFIER (t
);
8003 if (class_template_p
)
8004 /* Unfortunately, lookup_template_class sets
8005 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8006 instantiation (i.e., for the type of a member
8007 template class nested within a template class.)
8008 This behavior is required for
8009 maybe_process_partial_specialization to work
8010 correctly, but is not accurate in this case;
8011 the TAG is not an instantiation of anything.
8012 (The corresponding TEMPLATE_DECL is an
8013 instantiation, but the TYPE is not.) */
8014 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8016 /* Now, we call pushtag to put this NEWTAG into the scope of
8017 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8018 pushtag calling push_template_decl. We don't have to do
8019 this for enums because it will already have been done in
8022 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8023 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8026 else if (TREE_CODE (t
) == FUNCTION_DECL
8027 || DECL_FUNCTION_TEMPLATE_P (t
))
8029 /* Build new TYPE_METHODS. */
8032 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8033 ++processing_template_decl
;
8034 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8035 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8036 --processing_template_decl
;
8037 set_current_access_from_decl (r
);
8038 finish_member_declaration (r
);
8042 /* Build new TYPE_FIELDS. */
8043 if (TREE_CODE (t
) == STATIC_ASSERT
)
8046 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8047 tf_warning_or_error
, NULL_TREE
,
8048 /*integral_constant_expression_p=*/true);
8049 finish_static_assert (condition
,
8050 STATIC_ASSERT_MESSAGE (t
),
8051 STATIC_ASSERT_SOURCE_LOCATION (t
),
8054 else if (TREE_CODE (t
) != CONST_DECL
)
8058 /* The file and line for this declaration, to
8059 assist in error message reporting. Since we
8060 called push_tinst_level above, we don't need to
8062 input_location
= DECL_SOURCE_LOCATION (t
);
8064 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8065 ++processing_template_decl
;
8066 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8067 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8068 --processing_template_decl
;
8069 if (TREE_CODE (r
) == VAR_DECL
)
8073 [t]he initialization (and any associated
8074 side-effects) of a static data member does
8075 not occur unless the static data member is
8076 itself used in a way that requires the
8077 definition of the static data member to
8080 Therefore, we do not substitute into the
8081 initialized for the static data member here. */
8082 finish_static_data_member_decl
8085 /*init_const_expr_p=*/false,
8086 /*asmspec_tree=*/NULL_TREE
,
8088 if (DECL_INITIALIZED_IN_CLASS_P (r
))
8089 check_static_variable_definition (r
, TREE_TYPE (r
));
8091 else if (TREE_CODE (r
) == FIELD_DECL
)
8093 /* Determine whether R has a valid type and can be
8094 completed later. If R is invalid, then it is
8095 replaced by error_mark_node so that it will not be
8096 added to TYPE_FIELDS. */
8097 tree rtype
= TREE_TYPE (r
);
8098 if (can_complete_type_without_circularity (rtype
))
8099 complete_type (rtype
);
8101 if (!COMPLETE_TYPE_P (rtype
))
8103 cxx_incomplete_type_error (r
, rtype
);
8104 r
= error_mark_node
;
8108 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8109 such a thing will already have been added to the field
8110 list by tsubst_enum in finish_member_declaration in the
8111 CLASSTYPE_NESTED_UTDS case above. */
8112 if (!(TREE_CODE (r
) == TYPE_DECL
8113 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8114 && DECL_ARTIFICIAL (r
)))
8116 set_current_access_from_decl (r
);
8117 finish_member_declaration (r
);
8124 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
8126 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8128 tree friend_type
= t
;
8129 bool adjust_processing_template_decl
= false;
8131 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8133 /* template <class T> friend class C; */
8134 friend_type
= tsubst_friend_class (friend_type
, args
);
8135 adjust_processing_template_decl
= true;
8137 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8139 /* template <class T> friend class C::D; */
8140 friend_type
= tsubst (friend_type
, args
,
8141 tf_warning_or_error
, NULL_TREE
);
8142 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8143 friend_type
= TREE_TYPE (friend_type
);
8144 adjust_processing_template_decl
= true;
8146 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
)
8148 /* This could be either
8152 when dependent_type_p is false or
8154 template <class U> friend class T::C;
8157 friend_type
= tsubst (friend_type
, args
,
8158 tf_warning_or_error
, NULL_TREE
);
8159 /* Bump processing_template_decl for correct
8160 dependent_type_p calculation. */
8161 ++processing_template_decl
;
8162 if (dependent_type_p (friend_type
))
8163 adjust_processing_template_decl
= true;
8164 --processing_template_decl
;
8166 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8167 && hidden_name_p (TYPE_NAME (friend_type
)))
8171 where C hasn't been declared yet. Let's lookup name
8172 from namespace scope directly, bypassing any name that
8173 come from dependent base class. */
8174 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8176 /* The call to xref_tag_from_type does injection for friend
8178 push_nested_namespace (ns
);
8180 xref_tag_from_type (friend_type
, NULL_TREE
,
8181 /*tag_scope=*/ts_current
);
8182 pop_nested_namespace (ns
);
8184 else if (uses_template_parms (friend_type
))
8185 /* friend class C<T>; */
8186 friend_type
= tsubst (friend_type
, args
,
8187 tf_warning_or_error
, NULL_TREE
);
8192 where C is already declared or
8194 friend class C<int>;
8196 We don't have to do anything in these cases. */
8198 if (adjust_processing_template_decl
)
8199 /* Trick make_friend_class into realizing that the friend
8200 we're adding is a template, not an ordinary class. It's
8201 important that we use make_friend_class since it will
8202 perform some error-checking and output cross-reference
8204 ++processing_template_decl
;
8206 if (friend_type
!= error_mark_node
)
8207 make_friend_class (type
, friend_type
, /*complain=*/false);
8209 if (adjust_processing_template_decl
)
8210 --processing_template_decl
;
8214 /* Build new DECL_FRIENDLIST. */
8217 /* The file and line for this declaration, to
8218 assist in error message reporting. Since we
8219 called push_tinst_level above, we don't need to
8221 input_location
= DECL_SOURCE_LOCATION (t
);
8223 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8225 ++processing_template_decl
;
8226 push_deferring_access_checks (dk_no_check
);
8229 r
= tsubst_friend_function (t
, args
);
8230 add_friend (type
, r
, /*complain=*/false);
8231 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8233 pop_deferring_access_checks ();
8234 --processing_template_decl
;
8240 /* Set the file and line number information to whatever is given for
8241 the class itself. This puts error messages involving generated
8242 implicit functions at a predictable point, and the same point
8243 that would be used for non-template classes. */
8244 input_location
= DECL_SOURCE_LOCATION (typedecl
);
8246 unreverse_member_declarations (type
);
8247 finish_struct_1 (type
);
8248 TYPE_BEING_DEFINED (type
) = 0;
8250 /* We don't instantiate default arguments for member functions. 14.7.1:
8252 The implicit instantiation of a class template specialization causes
8253 the implicit instantiation of the declarations, but not of the
8254 definitions or default arguments, of the class member functions,
8255 member classes, static data members and member templates.... */
8257 /* Some typedefs referenced from within the template code need to be access
8258 checked at template instantiation time, i.e now. These types were
8259 added to the template at parsing time. Let's get those and perform
8260 the access checks then. */
8261 perform_typedefs_access_check (pattern
, args
);
8262 perform_deferred_access_checks ();
8263 pop_nested_class ();
8264 maximum_field_alignment
= saved_maximum_field_alignment
;
8265 pop_from_top_level ();
8266 pop_deferring_access_checks ();
8269 /* The vtable for a template class can be emitted in any translation
8270 unit in which the class is instantiated. When there is no key
8271 method, however, finish_struct_1 will already have added TYPE to
8272 the keyed_classes list. */
8273 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
8274 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
8280 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8286 else if (TYPE_P (t
))
8287 r
= tsubst (t
, args
, complain
, in_decl
);
8290 if (!(complain
& tf_warning
))
8291 ++c_inhibit_evaluation_warnings
;
8292 r
= tsubst_expr (t
, args
, complain
, in_decl
,
8293 /*integral_constant_expression_p=*/true);
8294 if (!(complain
& tf_warning
))
8295 --c_inhibit_evaluation_warnings
;
8300 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
8301 NONTYPE_ARGUMENT_PACK. */
8304 make_fnparm_pack (tree spec_parm
)
8306 /* Collect all of the extra "packed" parameters into an
8310 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
8311 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8312 int i
, len
= list_length (spec_parm
);
8314 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8315 parmvec
= make_tree_vec (len
);
8316 parmtypevec
= make_tree_vec (len
);
8317 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
8319 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
8320 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
8323 /* Build the argument packs. */
8324 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
8325 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
8326 TREE_TYPE (argpack
) = argtypepack
;
8331 /* Substitute ARGS into T, which is an pack expansion
8332 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
8333 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
8334 (if only a partial substitution could be performed) or
8335 ERROR_MARK_NODE if there was an error. */
8337 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
8341 tree pack
, packs
= NULL_TREE
, unsubstituted_packs
= NULL_TREE
;
8345 htab_t saved_local_specializations
= NULL
;
8347 gcc_assert (PACK_EXPANSION_P (t
));
8348 pattern
= PACK_EXPANSION_PATTERN (t
);
8350 /* Determine the argument packs that will instantiate the parameter
8351 packs used in the expansion expression. While we're at it,
8352 compute the number of arguments to be expanded and make sure it
8354 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
8355 pack
= TREE_CHAIN (pack
))
8357 tree parm_pack
= TREE_VALUE (pack
);
8358 tree arg_pack
= NULL_TREE
;
8359 tree orig_arg
= NULL_TREE
;
8361 if (TREE_CODE (parm_pack
) == PARM_DECL
)
8363 if (!cp_unevaluated_operand
)
8364 arg_pack
= retrieve_local_specialization (parm_pack
);
8367 /* We can't rely on local_specializations for a parameter
8368 name used later in a function declaration (such as in a
8369 late-specified return type). Even if it exists, it might
8370 have the wrong value for a recursive call. Just make a
8371 dummy decl, since it's only used for its type. */
8372 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
8373 arg_pack
= make_fnparm_pack (arg_pack
);
8378 int level
, idx
, levels
;
8379 template_parm_level_and_index (parm_pack
, &level
, &idx
);
8381 levels
= TMPL_ARGS_DEPTH (args
);
8382 if (level
<= levels
)
8383 arg_pack
= TMPL_ARG (args
, level
, idx
);
8386 orig_arg
= arg_pack
;
8387 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
8388 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
8390 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
8391 /* This can only happen if we forget to expand an argument
8392 pack somewhere else. Just return an error, silently. */
8394 result
= make_tree_vec (1);
8395 TREE_VEC_ELT (result
, 0) = error_mark_node
;
8400 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
8401 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
8403 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
8404 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
8405 if ((TYPE_P (pattern
) && same_type_p (pattern
, parm_pack
))
8406 || (!TYPE_P (pattern
) && cp_tree_equal (parm_pack
, pattern
)))
8407 /* The argument pack that the parameter maps to is just an
8408 expansion of the parameter itself, such as one would
8409 find in the implicit typedef of a class inside the
8410 class itself. Consider this parameter "unsubstituted",
8411 so that we will maintain the outer pack expansion. */
8412 arg_pack
= NULL_TREE
;
8418 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
8420 /* It's all-or-nothing with incomplete argument packs. */
8421 if (incomplete
&& !ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8422 return error_mark_node
;
8424 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
8429 else if (len
!= my_len
)
8432 /* We got explicit args for some packs but not others;
8433 do nothing now and try again after deduction. */
8435 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
8436 error ("mismatched argument pack lengths while expanding "
8440 error ("mismatched argument pack lengths while expanding "
8443 return error_mark_node
;
8446 /* Keep track of the parameter packs and their corresponding
8448 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
8449 TREE_TYPE (packs
) = orig_arg
;
8452 /* We can't substitute for this parameter pack. */
8453 unsubstituted_packs
= tree_cons (TREE_PURPOSE (pack
),
8455 unsubstituted_packs
);
8458 /* We cannot expand this expansion expression, because we don't have
8459 all of the argument packs we need. Substitute into the pattern
8460 and return a PACK_EXPANSION_*. The caller will need to deal with
8462 if (unsubstituted_packs
)
8465 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8466 new_pat
= tsubst_expr (pattern
, args
, complain
, in_decl
,
8467 /*integral_constant_expression_p=*/false);
8469 new_pat
= tsubst (pattern
, args
, complain
, in_decl
);
8470 return make_pack_expansion (new_pat
);
8473 /* We could not find any argument packs that work. */
8475 return error_mark_node
;
8477 if (cp_unevaluated_operand
)
8479 /* We're in a late-specified return type, so create our own local
8480 specializations table; the current table is either NULL or (in the
8481 case of recursive unification) might have bindings that we don't
8482 want to use or alter. */
8483 saved_local_specializations
= local_specializations
;
8484 local_specializations
= htab_create (37,
8485 hash_local_specialization
,
8486 eq_local_specializations
,
8490 /* For each argument in each argument pack, substitute into the
8492 result
= make_tree_vec (len
+ incomplete
);
8493 for (i
= 0; i
< len
+ incomplete
; ++i
)
8495 /* For parameter pack, change the substitution of the parameter
8496 pack to the ith argument in its argument pack, then expand
8498 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8500 tree parm
= TREE_PURPOSE (pack
);
8502 if (TREE_CODE (parm
) == PARM_DECL
)
8504 /* Select the Ith argument from the pack. */
8505 tree arg
= make_node (ARGUMENT_PACK_SELECT
);
8506 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
8507 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
8509 register_local_specialization (arg
, parm
);
8515 template_parm_level_and_index (parm
, &level
, &idx
);
8519 /* Select the Ith argument from the pack. */
8520 value
= make_node (ARGUMENT_PACK_SELECT
);
8521 ARGUMENT_PACK_SELECT_FROM_PACK (value
) = TREE_VALUE (pack
);
8522 ARGUMENT_PACK_SELECT_INDEX (value
) = i
;
8525 /* Update the corresponding argument. */
8526 TMPL_ARG (args
, level
, idx
) = value
;
8530 /* Substitute into the PATTERN with the altered arguments. */
8531 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
8532 TREE_VEC_ELT (result
, i
) =
8533 tsubst_expr (pattern
, args
, complain
, in_decl
,
8534 /*integral_constant_expression_p=*/false);
8536 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
8539 /* When we have incomplete argument packs, the last "expanded"
8540 result is itself a pack expansion, which allows us
8541 to deduce more arguments. */
8542 TREE_VEC_ELT (result
, i
) =
8543 make_pack_expansion (TREE_VEC_ELT (result
, i
));
8545 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
8547 result
= error_mark_node
;
8552 /* Update ARGS to restore the substitution from parameter packs to
8553 their argument packs. */
8554 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
8556 tree parm
= TREE_PURPOSE (pack
);
8558 if (TREE_CODE (parm
) == PARM_DECL
)
8559 register_local_specialization (TREE_TYPE (pack
), parm
);
8563 template_parm_level_and_index (parm
, &level
, &idx
);
8565 /* Update the corresponding argument. */
8566 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
8567 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
8570 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
8574 if (saved_local_specializations
)
8576 htab_delete (local_specializations
);
8577 local_specializations
= saved_local_specializations
;
8583 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
8584 TMPL. We do this using DECL_PARM_INDEX, which should work even with
8585 parameter packs; all parms generated from a function parameter pack will
8586 have the same DECL_PARM_INDEX. */
8589 get_pattern_parm (tree parm
, tree tmpl
)
8591 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
8594 if (DECL_ARTIFICIAL (parm
))
8596 for (patparm
= DECL_ARGUMENTS (pattern
);
8597 patparm
; patparm
= DECL_CHAIN (patparm
))
8598 if (DECL_ARTIFICIAL (patparm
)
8599 && DECL_NAME (parm
) == DECL_NAME (patparm
))
8604 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
8605 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
8606 gcc_assert (DECL_PARM_INDEX (patparm
)
8607 == DECL_PARM_INDEX (parm
));
8613 /* Substitute ARGS into the vector or list of template arguments T. */
8616 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8619 int len
= TREE_VEC_LENGTH (t
);
8620 int need_new
= 0, i
, expanded_len_adjust
= 0, out
;
8621 tree
*elts
= XALLOCAVEC (tree
, len
);
8623 for (i
= 0; i
< len
; i
++)
8625 tree orig_arg
= TREE_VEC_ELT (t
, i
);
8628 if (TREE_CODE (orig_arg
) == TREE_VEC
)
8629 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
8630 else if (PACK_EXPANSION_P (orig_arg
))
8632 /* Substitute into an expansion expression. */
8633 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
8635 if (TREE_CODE (new_arg
) == TREE_VEC
)
8636 /* Add to the expanded length adjustment the number of
8637 expanded arguments. We subtract one from this
8638 measurement, because the argument pack expression
8639 itself is already counted as 1 in
8640 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8641 the argument pack is empty. */
8642 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
8644 else if (ARGUMENT_PACK_P (orig_arg
))
8646 /* Substitute into each of the arguments. */
8647 new_arg
= TYPE_P (orig_arg
)
8648 ? cxx_make_type (TREE_CODE (orig_arg
))
8649 : make_node (TREE_CODE (orig_arg
));
8651 SET_ARGUMENT_PACK_ARGS (
8653 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
8654 args
, complain
, in_decl
));
8656 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
8657 new_arg
= error_mark_node
;
8659 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
8660 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
8662 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
8664 if (TREE_TYPE (new_arg
) == error_mark_node
)
8665 new_arg
= error_mark_node
;
8669 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
8671 if (new_arg
== error_mark_node
)
8672 return error_mark_node
;
8675 if (new_arg
!= orig_arg
)
8682 /* Make space for the expanded arguments coming from template
8684 t
= make_tree_vec (len
+ expanded_len_adjust
);
8685 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
8686 arguments for a member template.
8687 In that case each TREE_VEC in ORIG_T represents a level of template
8688 arguments, and ORIG_T won't carry any non defaulted argument count.
8689 It will rather be the nested TREE_VECs that will carry one.
8690 In other words, ORIG_T carries a non defaulted argument count only
8691 if it doesn't contain any nested TREE_VEC. */
8692 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
8694 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
8695 count
+= expanded_len_adjust
;
8696 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
8698 for (i
= 0, out
= 0; i
< len
; i
++)
8700 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
8701 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
8702 && TREE_CODE (elts
[i
]) == TREE_VEC
)
8706 /* Now expand the template argument pack "in place". */
8707 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
8708 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
8712 TREE_VEC_ELT (t
, out
) = elts
[i
];
8720 /* Return the result of substituting ARGS into the template parameters
8721 given by PARMS. If there are m levels of ARGS and m + n levels of
8722 PARMS, then the result will contain n levels of PARMS. For
8723 example, if PARMS is `template <class T> template <class U>
8724 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8725 result will be `template <int*, double, class V>'. */
8728 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
8733 /* When substituting into a template, we must set
8734 PROCESSING_TEMPLATE_DECL as the template parameters may be
8735 dependent if they are based on one-another, and the dependency
8736 predicates are short-circuit outside of templates. */
8737 ++processing_template_decl
;
8739 for (new_parms
= &r
;
8740 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
8741 new_parms
= &(TREE_CHAIN (*new_parms
)),
8742 parms
= TREE_CHAIN (parms
))
8745 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
8748 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
8754 if (parms
== error_mark_node
)
8757 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
8759 if (tuple
== error_mark_node
)
8762 default_value
= TREE_PURPOSE (tuple
);
8763 parm_decl
= TREE_VALUE (tuple
);
8765 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
8766 if (TREE_CODE (parm_decl
) == PARM_DECL
8767 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
8768 parm_decl
= error_mark_node
;
8769 default_value
= tsubst_template_arg (default_value
, args
,
8770 complain
, NULL_TREE
);
8772 tuple
= build_tree_list (default_value
, parm_decl
);
8773 TREE_VEC_ELT (new_vec
, i
) = tuple
;
8777 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
8778 - TMPL_ARGS_DEPTH (args
)),
8779 new_vec
, NULL_TREE
);
8782 --processing_template_decl
;
8787 /* Substitute the ARGS into the indicated aggregate (or enumeration)
8788 type T. If T is not an aggregate or enumeration type, it is
8789 handled as if by tsubst. IN_DECL is as for tsubst. If
8790 ENTERING_SCOPE is nonzero, T is the context for a template which
8791 we are presently tsubst'ing. Return the substituted value. */
8794 tsubst_aggr_type (tree t
,
8796 tsubst_flags_t complain
,
8803 switch (TREE_CODE (t
))
8806 if (TYPE_PTRMEMFUNC_P (t
))
8807 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
8809 /* Else fall through. */
8812 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
8817 int saved_unevaluated_operand
;
8818 int saved_inhibit_evaluation_warnings
;
8820 /* In "sizeof(X<I>)" we need to evaluate "I". */
8821 saved_unevaluated_operand
= cp_unevaluated_operand
;
8822 cp_unevaluated_operand
= 0;
8823 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8824 c_inhibit_evaluation_warnings
= 0;
8826 /* First, determine the context for the type we are looking
8828 context
= TYPE_CONTEXT (t
);
8831 context
= tsubst_aggr_type (context
, args
, complain
,
8832 in_decl
, /*entering_scope=*/1);
8833 /* If context is a nested class inside a class template,
8834 it may still need to be instantiated (c++/33959). */
8835 if (TYPE_P (context
))
8836 context
= complete_type (context
);
8839 /* Then, figure out what arguments are appropriate for the
8840 type we are trying to find. For example, given:
8842 template <class T> struct S;
8843 template <class T, class U> void f(T, U) { S<U> su; }
8845 and supposing that we are instantiating f<int, double>,
8846 then our ARGS will be {int, double}, but, when looking up
8847 S we only want {double}. */
8848 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
8850 if (argvec
== error_mark_node
)
8851 r
= error_mark_node
;
8854 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
8855 entering_scope
, complain
);
8856 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
8859 cp_unevaluated_operand
= saved_unevaluated_operand
;
8860 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
8865 /* This is not a template type, so there's nothing to do. */
8869 return tsubst (t
, args
, complain
, in_decl
);
8873 /* Substitute into the default argument ARG (a default argument for
8874 FN), which has the indicated TYPE. */
8877 tsubst_default_argument (tree fn
, tree type
, tree arg
)
8879 tree saved_class_ptr
= NULL_TREE
;
8880 tree saved_class_ref
= NULL_TREE
;
8882 /* This can happen in invalid code. */
8883 if (TREE_CODE (arg
) == DEFAULT_ARG
)
8886 /* This default argument came from a template. Instantiate the
8887 default argument here, not in tsubst. In the case of
8896 we must be careful to do name lookup in the scope of S<T>,
8897 rather than in the current class. */
8898 push_access_scope (fn
);
8899 /* The "this" pointer is not valid in a default argument. */
8902 saved_class_ptr
= current_class_ptr
;
8903 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
8904 saved_class_ref
= current_class_ref
;
8905 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
8908 push_deferring_access_checks(dk_no_deferred
);
8909 /* The default argument expression may cause implicitly defined
8910 member functions to be synthesized, which will result in garbage
8911 collection. We must treat this situation as if we were within
8912 the body of function so as to avoid collecting live data on the
8915 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
8916 tf_warning_or_error
, NULL_TREE
,
8917 /*integral_constant_expression_p=*/false);
8919 pop_deferring_access_checks();
8921 /* Restore the "this" pointer. */
8924 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
8925 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
8928 /* Make sure the default argument is reasonable. */
8929 arg
= check_default_argument (type
, arg
);
8931 pop_access_scope (fn
);
8936 /* Substitute into all the default arguments for FN. */
8939 tsubst_default_arguments (tree fn
)
8944 tmpl_args
= DECL_TI_ARGS (fn
);
8946 /* If this function is not yet instantiated, we certainly don't need
8947 its default arguments. */
8948 if (uses_template_parms (tmpl_args
))
8950 /* Don't do this again for clones. */
8951 if (DECL_CLONED_FUNCTION_P (fn
))
8954 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
8956 arg
= TREE_CHAIN (arg
))
8957 if (TREE_PURPOSE (arg
))
8958 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
8960 TREE_PURPOSE (arg
));
8963 /* Substitute the ARGS into the T, which is a _DECL. Return the
8964 result of the substitution. Issue error and warning messages under
8965 control of COMPLAIN. */
8968 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
8970 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
8971 location_t saved_loc
;
8976 /* Set the filename and linenumber to improve error-reporting. */
8977 saved_loc
= input_location
;
8978 input_location
= DECL_SOURCE_LOCATION (t
);
8980 switch (TREE_CODE (t
))
8984 /* We can get here when processing a member function template,
8985 member class template, or template template parameter. */
8986 tree decl
= DECL_TEMPLATE_RESULT (t
);
8991 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8993 /* Template template parameter is treated here. */
8994 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
8995 if (new_type
== error_mark_node
)
8996 RETURN (error_mark_node
);
8999 DECL_CHAIN (r
) = NULL_TREE
;
9000 TREE_TYPE (r
) = new_type
;
9001 DECL_TEMPLATE_RESULT (r
)
9002 = build_decl (DECL_SOURCE_LOCATION (decl
),
9003 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9004 DECL_TEMPLATE_PARMS (r
)
9005 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9007 TYPE_NAME (new_type
) = r
;
9011 /* We might already have an instance of this template.
9012 The ARGS are for the surrounding class type, so the
9013 full args contain the tsubst'd args for the context,
9014 plus the innermost args from the template decl. */
9015 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9016 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9017 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9018 /* Because this is a template, the arguments will still be
9019 dependent, even after substitution. If
9020 PROCESSING_TEMPLATE_DECL is not set, the dependency
9021 predicates will short-circuit. */
9022 ++processing_template_decl
;
9023 full_args
= tsubst_template_args (tmpl_args
, args
,
9025 --processing_template_decl
;
9026 if (full_args
== error_mark_node
)
9027 RETURN (error_mark_node
);
9029 /* If this is a default template template argument,
9030 tsubst might not have changed anything. */
9031 if (full_args
== tmpl_args
)
9034 hash
= hash_tmpl_and_args (t
, full_args
);
9035 spec
= retrieve_specialization (t
, full_args
, hash
);
9036 if (spec
!= NULL_TREE
)
9042 /* Make a new template decl. It will be similar to the
9043 original, but will record the current template arguments.
9044 We also create a new function declaration, which is just
9045 like the old one, but points to this new template, rather
9046 than the old one. */
9048 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
9049 DECL_CHAIN (r
) = NULL_TREE
;
9051 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
9053 if (TREE_CODE (decl
) == TYPE_DECL
)
9056 ++processing_template_decl
;
9057 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9058 --processing_template_decl
;
9059 if (new_type
== error_mark_node
)
9060 RETURN (error_mark_node
);
9062 TREE_TYPE (r
) = new_type
;
9063 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
9064 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
9065 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
9066 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
9071 ++processing_template_decl
;
9072 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
9073 --processing_template_decl
;
9074 if (new_decl
== error_mark_node
)
9075 RETURN (error_mark_node
);
9077 DECL_TEMPLATE_RESULT (r
) = new_decl
;
9078 DECL_TI_TEMPLATE (new_decl
) = r
;
9079 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
9080 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
9081 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
9084 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9085 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
9086 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
9088 /* The template parameters for this new template are all the
9089 template parameters for the old template, except the
9090 outermost level of parameters. */
9091 DECL_TEMPLATE_PARMS (r
)
9092 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9095 if (PRIMARY_TEMPLATE_P (t
))
9096 DECL_PRIMARY_TEMPLATE (r
) = r
;
9098 if (TREE_CODE (decl
) != TYPE_DECL
)
9099 /* Record this non-type partial instantiation. */
9100 register_specialization (r
, t
,
9101 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
9109 tree argvec
= NULL_TREE
;
9117 /* Nobody should be tsubst'ing into non-template functions. */
9118 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
9120 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
9125 /* If T is not dependent, just return it. We have to
9126 increment PROCESSING_TEMPLATE_DECL because
9127 value_dependent_expression_p assumes that nothing is
9128 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9129 ++processing_template_decl
;
9130 dependent_p
= value_dependent_expression_p (t
);
9131 --processing_template_decl
;
9135 /* Calculate the most general template of which R is a
9136 specialization, and the complete set of arguments used to
9138 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
9139 argvec
= tsubst_template_args (DECL_TI_ARGS
9140 (DECL_TEMPLATE_RESULT
9141 (DECL_TI_TEMPLATE (t
))),
9142 args
, complain
, in_decl
);
9144 /* Check to see if we already have this specialization. */
9145 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9146 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9154 /* We can see more levels of arguments than parameters if
9155 there was a specialization of a member template, like
9158 template <class T> struct S { template <class U> void f(); }
9159 template <> template <class U> void S<int>::f(U);
9161 Here, we'll be substituting into the specialization,
9162 because that's where we can find the code we actually
9163 want to generate, but we'll have enough arguments for
9164 the most general template.
9166 We also deal with the peculiar case:
9168 template <class T> struct S {
9169 template <class U> friend void f();
9171 template <class U> void f() {}
9173 template void f<double>();
9175 Here, the ARGS for the instantiation of will be {int,
9176 double}. But, we only need as many ARGS as there are
9177 levels of template parameters in CODE_PATTERN. We are
9178 careful not to get fooled into reducing the ARGS in
9181 template <class T> struct S { template <class U> void f(U); }
9182 template <class T> template <> void S<T>::f(int) {}
9184 which we can spot because the pattern will be a
9185 specialization in this case. */
9186 args_depth
= TMPL_ARGS_DEPTH (args
);
9188 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
9189 if (args_depth
> parms_depth
9190 && !DECL_TEMPLATE_SPECIALIZATION (t
))
9191 args
= get_innermost_template_args (args
, parms_depth
);
9195 /* This special case arises when we have something like this:
9197 template <class T> struct S {
9198 friend void f<int>(int, double);
9201 Here, the DECL_TI_TEMPLATE for the friend declaration
9202 will be an IDENTIFIER_NODE. We are being called from
9203 tsubst_friend_function, and we want only to create a
9204 new decl (R) with appropriate types so that we can call
9205 determine_specialization. */
9206 gen_tmpl
= NULL_TREE
;
9209 if (DECL_CLASS_SCOPE_P (t
))
9211 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
9215 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
9216 complain
, t
, /*entering_scope=*/1);
9221 ctx
= DECL_CONTEXT (t
);
9223 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9224 if (type
== error_mark_node
)
9225 RETURN (error_mark_node
);
9227 /* We do NOT check for matching decls pushed separately at this
9228 point, as they may not represent instantiations of this
9229 template, and in any case are considered separate under the
9232 DECL_USE_TEMPLATE (r
) = 0;
9233 TREE_TYPE (r
) = type
;
9234 /* Clear out the mangled name and RTL for the instantiation. */
9235 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
9236 SET_DECL_RTL (r
, NULL
);
9237 /* Leave DECL_INITIAL set on deleted instantiations. */
9238 if (!DECL_DELETED_FN (r
))
9239 DECL_INITIAL (r
) = NULL_TREE
;
9240 DECL_CONTEXT (r
) = ctx
;
9242 if (member
&& DECL_CONV_FN_P (r
))
9243 /* Type-conversion operator. Reconstruct the name, in
9244 case it's the name of one of the template's parameters. */
9245 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
9247 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
9249 DECL_RESULT (r
) = NULL_TREE
;
9251 TREE_STATIC (r
) = 0;
9252 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
9253 DECL_EXTERNAL (r
) = 1;
9254 /* If this is an instantiation of a function with internal
9255 linkage, we already know what object file linkage will be
9256 assigned to the instantiation. */
9257 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
9258 DECL_DEFER_OUTPUT (r
) = 0;
9259 DECL_CHAIN (r
) = NULL_TREE
;
9260 DECL_PENDING_INLINE_INFO (r
) = 0;
9261 DECL_PENDING_INLINE_P (r
) = 0;
9262 DECL_SAVED_TREE (r
) = NULL_TREE
;
9263 DECL_STRUCT_FUNCTION (r
) = NULL
;
9265 /* We'll re-clone as appropriate in instantiate_template. */
9266 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
9268 /* If we aren't complaining now, return on error before we register
9269 the specialization so that we'll complain eventually. */
9270 if ((complain
& tf_error
) == 0
9271 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9272 && !grok_op_properties (r
, /*complain=*/false))
9273 RETURN (error_mark_node
);
9275 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
9276 this in the special friend case mentioned above where
9277 GEN_TMPL is NULL. */
9280 DECL_TEMPLATE_INFO (r
)
9281 = build_template_info (gen_tmpl
, argvec
);
9282 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9283 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
9285 /* We're not supposed to instantiate default arguments
9286 until they are called, for a template. But, for a
9289 template <class T> void f ()
9290 { extern void g(int i = T()); }
9292 we should do the substitution when the template is
9293 instantiated. We handle the member function case in
9294 instantiate_class_template since the default arguments
9295 might refer to other members of the class. */
9297 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
9298 && !uses_template_parms (argvec
))
9299 tsubst_default_arguments (r
);
9302 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
9304 /* Copy the list of befriending classes. */
9305 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
9307 friends
= &TREE_CHAIN (*friends
))
9309 *friends
= copy_node (*friends
);
9310 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
9315 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
9317 maybe_retrofit_in_chrg (r
);
9318 if (DECL_CONSTRUCTOR_P (r
))
9319 grok_ctor_properties (ctx
, r
);
9320 /* If this is an instantiation of a member template, clone it.
9321 If it isn't, that'll be handled by
9322 clone_constructors_and_destructors. */
9323 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
9324 clone_function_decl (r
, /*update_method_vec_p=*/0);
9326 else if ((complain
& tf_error
) != 0
9327 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
9328 && !grok_op_properties (r
, /*complain=*/true))
9329 RETURN (error_mark_node
);
9331 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
9332 SET_DECL_FRIEND_CONTEXT (r
,
9333 tsubst (DECL_FRIEND_CONTEXT (t
),
9334 args
, complain
, in_decl
));
9336 /* Possibly limit visibility based on template args. */
9337 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
9338 if (DECL_VISIBILITY_SPECIFIED (t
))
9340 DECL_VISIBILITY_SPECIFIED (r
) = 0;
9342 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
9344 determine_visibility (r
);
9345 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
9346 && !processing_template_decl
)
9347 defaulted_late_check (r
);
9349 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9350 args
, complain
, in_decl
);
9356 tree type
= NULL_TREE
;
9358 tree expanded_types
= NULL_TREE
;
9359 tree prev_r
= NULL_TREE
;
9360 tree first_r
= NULL_TREE
;
9362 if (FUNCTION_PARAMETER_PACK_P (t
))
9364 /* If there is a local specialization that isn't a
9365 parameter pack, it means that we're doing a "simple"
9366 substitution from inside tsubst_pack_expansion. Just
9367 return the local specialization (which will be a single
9369 tree spec
= retrieve_local_specialization (t
);
9371 && TREE_CODE (spec
) == PARM_DECL
9372 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
9375 /* Expand the TYPE_PACK_EXPANSION that provides the types for
9376 the parameters in this function parameter pack. */
9377 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
9379 if (TREE_CODE (expanded_types
) == TREE_VEC
)
9381 len
= TREE_VEC_LENGTH (expanded_types
);
9383 /* Zero-length parameter packs are boring. Just substitute
9386 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
9391 /* All we did was update the type. Make a note of that. */
9392 type
= expanded_types
;
9393 expanded_types
= NULL_TREE
;
9397 /* Loop through all of the parameter's we'll build. When T is
9398 a function parameter pack, LEN is the number of expanded
9399 types in EXPANDED_TYPES; otherwise, LEN is 1. */
9401 for (i
= 0; i
< len
; ++i
)
9405 if (DECL_TEMPLATE_PARM_P (t
))
9406 SET_DECL_TEMPLATE_PARM_P (r
);
9408 /* An argument of a function parameter pack is not a parameter
9410 FUNCTION_PARAMETER_PACK_P (r
) = false;
9413 /* We're on the Ith parameter of the function parameter
9416 /* Get the Ith type. */
9417 type
= TREE_VEC_ELT (expanded_types
, i
);
9420 /* Rename the parameter to include the index. */
9422 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
9425 /* We're dealing with a normal parameter. */
9426 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9428 type
= type_decays_to (type
);
9429 TREE_TYPE (r
) = type
;
9430 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9432 if (DECL_INITIAL (r
))
9434 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
9435 DECL_INITIAL (r
) = TREE_TYPE (r
);
9437 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
9441 DECL_CONTEXT (r
) = NULL_TREE
;
9443 if (!DECL_TEMPLATE_PARM_P (r
))
9444 DECL_ARG_TYPE (r
) = type_passed_as (type
);
9446 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9447 args
, complain
, in_decl
);
9449 /* Keep track of the first new parameter we
9450 generate. That's what will be returned to the
9455 /* Build a proper chain of parameters when substituting
9456 into a function parameter pack. */
9458 DECL_CHAIN (prev_r
) = r
;
9462 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
9463 complain
, DECL_CHAIN (t
));
9465 /* FIRST_R contains the start of the chain we've built. */
9475 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9476 if (type
== error_mark_node
)
9477 RETURN (error_mark_node
);
9478 TREE_TYPE (r
) = type
;
9479 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9481 /* DECL_INITIAL gives the number of bits in a bit-field. */
9483 = tsubst_expr (DECL_INITIAL (t
), args
,
9485 /*integral_constant_expression_p=*/true);
9486 /* We don't have to set DECL_CONTEXT here; it is set by
9487 finish_member_declaration. */
9488 DECL_CHAIN (r
) = NULL_TREE
;
9489 if (VOID_TYPE_P (type
))
9490 error ("instantiation of %q+D as type %qT", r
, type
);
9492 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
9493 args
, complain
, in_decl
);
9498 /* We reach here only for member using decls. */
9499 if (DECL_DEPENDENT_P (t
))
9501 r
= do_class_using_decl
9502 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
9503 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
9505 r
= error_mark_node
;
9508 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
9509 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
9515 DECL_CHAIN (r
) = NULL_TREE
;
9522 tree argvec
= NULL_TREE
;
9523 tree gen_tmpl
= NULL_TREE
;
9525 tree tmpl
= NULL_TREE
;
9527 tree type
= NULL_TREE
;
9530 if (TREE_CODE (t
) == TYPE_DECL
9531 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
9533 /* If this is the canonical decl, we don't have to
9534 mess with instantiations, and often we can't (for
9535 typename, template type parms and such). Note that
9536 TYPE_NAME is not correct for the above test if
9537 we've copied the type for a typedef. */
9538 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9539 if (type
== error_mark_node
)
9540 RETURN (error_mark_node
);
9541 r
= TYPE_NAME (type
);
9545 /* Check to see if we already have the specialization we
9548 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
9550 /* T is a static data member or namespace-scope entity.
9551 We have to substitute into namespace-scope variables
9552 (even though such entities are never templates) because
9555 template <class T> void f() { extern T t; }
9557 where the entity referenced is not known until
9558 instantiation time. */
9560 ctx
= DECL_CONTEXT (t
);
9561 if (DECL_CLASS_SCOPE_P (t
))
9563 ctx
= tsubst_aggr_type (ctx
, args
,
9565 in_decl
, /*entering_scope=*/1);
9566 /* If CTX is unchanged, then T is in fact the
9567 specialization we want. That situation occurs when
9568 referencing a static data member within in its own
9569 class. We can use pointer equality, rather than
9570 same_type_p, because DECL_CONTEXT is always
9572 if (ctx
== DECL_CONTEXT (t
))
9578 tmpl
= DECL_TI_TEMPLATE (t
);
9579 gen_tmpl
= most_general_template (tmpl
);
9580 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
9581 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9582 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9587 /* A local variable. */
9589 /* Subsequent calls to pushdecl will fill this in. */
9591 spec
= retrieve_local_specialization (t
);
9593 /* If we already have the specialization we need, there is
9594 nothing more to do. */
9601 /* Create a new node for the specialization we need. */
9603 if (type
== NULL_TREE
)
9605 if (is_typedef_decl (t
))
9606 type
= DECL_ORIGINAL_TYPE (t
);
9608 type
= TREE_TYPE (t
);
9609 if (TREE_CODE (t
) == VAR_DECL
&& VAR_HAD_UNKNOWN_BOUND (t
))
9610 type
= strip_array_domain (type
);
9611 type
= tsubst (type
, args
, complain
, in_decl
);
9613 if (TREE_CODE (r
) == VAR_DECL
)
9615 /* Even if the original location is out of scope, the
9616 newly substituted one is not. */
9617 DECL_DEAD_FOR_LOCAL (r
) = 0;
9618 DECL_INITIALIZED_P (r
) = 0;
9619 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
9620 if (type
== error_mark_node
)
9621 RETURN (error_mark_node
);
9622 if (TREE_CODE (type
) == FUNCTION_TYPE
)
9624 /* It may seem that this case cannot occur, since:
9629 declares a function, not a variable. However:
9632 template <typename T> void g() { T t; }
9633 template void g<f>();
9635 is an attempt to declare a variable with function
9637 error ("variable %qD has function type",
9638 /* R is not yet sufficiently initialized, so we
9639 just use its name. */
9641 RETURN (error_mark_node
);
9643 type
= complete_type (type
);
9644 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
)
9645 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
);
9646 type
= check_var_type (DECL_NAME (r
), type
);
9648 if (DECL_HAS_VALUE_EXPR_P (t
))
9650 tree ve
= DECL_VALUE_EXPR (t
);
9651 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
9652 /*constant_expression_p=*/false);
9653 SET_DECL_VALUE_EXPR (r
, ve
);
9656 else if (DECL_SELF_REFERENCE_P (t
))
9657 SET_DECL_SELF_REFERENCE_P (r
);
9658 TREE_TYPE (r
) = type
;
9659 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
9660 DECL_CONTEXT (r
) = ctx
;
9661 /* Clear out the mangled name and RTL for the instantiation. */
9662 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
9663 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
9664 SET_DECL_RTL (r
, NULL
);
9665 /* The initializer must not be expanded until it is required;
9667 DECL_INITIAL (r
) = NULL_TREE
;
9668 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
9669 SET_DECL_RTL (r
, NULL
);
9670 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
9671 if (TREE_CODE (r
) == VAR_DECL
)
9673 /* Possibly limit visibility based on template args. */
9674 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
9675 if (DECL_VISIBILITY_SPECIFIED (t
))
9677 DECL_VISIBILITY_SPECIFIED (r
) = 0;
9679 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
9681 determine_visibility (r
);
9686 /* A static data member declaration is always marked
9687 external when it is declared in-class, even if an
9688 initializer is present. We mimic the non-template
9690 DECL_EXTERNAL (r
) = 1;
9692 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
9693 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
9694 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9696 else if (cp_unevaluated_operand
)
9698 /* We're substituting this var in a decltype outside of its
9699 scope, such as for a lambda return type. Don't add it to
9700 local_specializations, do perform auto deduction. */
9701 tree auto_node
= type_uses_auto (type
);
9703 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
9704 /*constant_expression_p=*/false);
9706 if (auto_node
&& init
&& describable_type (init
))
9708 type
= do_auto_deduction (type
, init
, auto_node
);
9709 TREE_TYPE (r
) = type
;
9713 register_local_specialization (r
, t
);
9715 DECL_CHAIN (r
) = NULL_TREE
;
9717 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
9719 args
, complain
, in_decl
);
9721 /* Preserve a typedef that names a type. */
9722 if (is_typedef_decl (r
))
9724 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
9725 set_underlying_type (r
);
9738 /* Restore the file and line information. */
9739 input_location
= saved_loc
;
9744 /* Substitute into the ARG_TYPES of a function type. */
9747 tsubst_arg_types (tree arg_types
,
9749 tsubst_flags_t complain
,
9752 tree remaining_arg_types
;
9753 tree type
= NULL_TREE
;
9755 tree expanded_args
= NULL_TREE
;
9758 if (!arg_types
|| arg_types
== void_list_node
)
9761 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
9762 args
, complain
, in_decl
);
9763 if (remaining_arg_types
== error_mark_node
)
9764 return error_mark_node
;
9766 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
9768 /* For a pack expansion, perform substitution on the
9769 entire expression. Later on, we'll handle the arguments
9771 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
9772 args
, complain
, in_decl
);
9774 if (TREE_CODE (expanded_args
) == TREE_VEC
)
9775 /* So that we'll spin through the parameters, one by one. */
9776 i
= TREE_VEC_LENGTH (expanded_args
);
9779 /* We only partially substituted into the parameter
9780 pack. Our type is TYPE_PACK_EXPANSION. */
9781 type
= expanded_args
;
9782 expanded_args
= NULL_TREE
;
9790 type
= TREE_VEC_ELT (expanded_args
, i
);
9792 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
9794 if (type
== error_mark_node
)
9795 return error_mark_node
;
9796 if (VOID_TYPE_P (type
))
9798 if (complain
& tf_error
)
9800 error ("invalid parameter type %qT", type
);
9802 error ("in declaration %q+D", in_decl
);
9804 return error_mark_node
;
9807 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9808 top-level qualifiers as required. */
9809 type
= TYPE_MAIN_VARIANT (type_decays_to (type
));
9811 /* We do not substitute into default arguments here. The standard
9812 mandates that they be instantiated only when needed, which is
9813 done in build_over_call. */
9814 default_arg
= TREE_PURPOSE (arg_types
);
9816 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
9818 /* We've instantiated a template before its default arguments
9819 have been parsed. This can happen for a nested template
9820 class, and is not an error unless we require the default
9821 argument in a call of this function. */
9822 remaining_arg_types
=
9823 tree_cons (default_arg
, type
, remaining_arg_types
);
9824 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
9825 remaining_arg_types
);
9828 remaining_arg_types
=
9829 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
9832 return remaining_arg_types
;
9835 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9836 *not* handle the exception-specification for FNTYPE, because the
9837 initial substitution of explicitly provided template parameters
9838 during argument deduction forbids substitution into the
9839 exception-specification:
9843 All references in the function type of the function template to the
9844 corresponding template parameters are replaced by the specified tem-
9845 plate argument values. If a substitution in a template parameter or
9846 in the function type of the function template results in an invalid
9847 type, type deduction fails. [Note: The equivalent substitution in
9848 exception specifications is done only when the function is instanti-
9849 ated, at which point a program is ill-formed if the substitution
9850 results in an invalid type.] */
9853 tsubst_function_type (tree t
,
9855 tsubst_flags_t complain
,
9862 /* The TYPE_CONTEXT is not used for function/method types. */
9863 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
9865 /* Substitute the return type. */
9866 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9867 if (return_type
== error_mark_node
)
9868 return error_mark_node
;
9869 /* The standard does not presently indicate that creation of a
9870 function type with an invalid return type is a deduction failure.
9871 However, that is clearly analogous to creating an array of "void"
9872 or a reference to a reference. This is core issue #486. */
9873 if (TREE_CODE (return_type
) == ARRAY_TYPE
9874 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
9876 if (complain
& tf_error
)
9878 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
9879 error ("function returning an array");
9881 error ("function returning a function");
9883 return error_mark_node
;
9886 /* Substitute the argument types. */
9887 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
9889 if (arg_types
== error_mark_node
)
9890 return error_mark_node
;
9892 /* Construct a new type node and return it. */
9893 if (TREE_CODE (t
) == FUNCTION_TYPE
)
9895 fntype
= build_function_type (return_type
, arg_types
);
9896 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
9900 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
9901 if (! MAYBE_CLASS_TYPE_P (r
))
9905 Type deduction may fail for any of the following
9908 -- Attempting to create "pointer to member of T" when T
9909 is not a class type. */
9910 if (complain
& tf_error
)
9911 error ("creating pointer to member function of non-class type %qT",
9913 return error_mark_node
;
9916 fntype
= build_method_type_directly (r
, return_type
,
9917 TREE_CHAIN (arg_types
));
9919 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
9924 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9925 ARGS into that specification, and return the substituted
9926 specification. If there is no specification, return NULL_TREE. */
9929 tsubst_exception_specification (tree fntype
,
9931 tsubst_flags_t complain
,
9937 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
9938 new_specs
= NULL_TREE
;
9939 if (specs
&& TREE_PURPOSE (specs
))
9941 /* A noexcept-specifier. */
9942 new_specs
= tsubst_copy_and_build
9943 (TREE_PURPOSE (specs
), args
, complain
, in_decl
, /*function_p=*/false,
9944 /*integral_constant_expression_p=*/true);
9945 new_specs
= build_noexcept_spec (new_specs
, complain
);
9949 if (! TREE_VALUE (specs
))
9956 tree expanded_specs
= NULL_TREE
;
9958 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
9960 /* Expand the pack expansion type. */
9961 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
9965 if (expanded_specs
== error_mark_node
)
9966 return error_mark_node
;
9967 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
9968 len
= TREE_VEC_LENGTH (expanded_specs
);
9971 /* We're substituting into a member template, so
9972 we got a TYPE_PACK_EXPANSION back. Add that
9973 expansion and move on. */
9974 gcc_assert (TREE_CODE (expanded_specs
)
9975 == TYPE_PACK_EXPANSION
);
9976 new_specs
= add_exception_specifier (new_specs
,
9979 specs
= TREE_CHAIN (specs
);
9984 for (i
= 0; i
< len
; ++i
)
9987 spec
= TREE_VEC_ELT (expanded_specs
, i
);
9989 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
9990 if (spec
== error_mark_node
)
9992 new_specs
= add_exception_specifier (new_specs
, spec
,
9996 specs
= TREE_CHAIN (specs
);
10002 /* Take the tree structure T and replace template parameters used
10003 therein with the argument vector ARGS. IN_DECL is an associated
10004 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10005 Issue error and warning messages under control of COMPLAIN. Note
10006 that we must be relatively non-tolerant of extensions here, in
10007 order to preserve conformance; if we allow substitutions that
10008 should not be allowed, we may allow argument deductions that should
10009 not succeed, and therefore report ambiguous overload situations
10010 where there are none. In theory, we could allow the substitution,
10011 but indicate that it should have failed, and allow our caller to
10012 make sure that the right thing happens, but we don't try to do this
10015 This function is used for dealing with types, decls and the like;
10016 for expressions, use tsubst_expr or tsubst_copy. */
10019 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10021 enum tree_code code
;
10024 if (t
== NULL_TREE
|| t
== error_mark_node
10025 || t
== integer_type_node
10026 || t
== void_type_node
10027 || t
== char_type_node
10028 || t
== unknown_type_node
10029 || TREE_CODE (t
) == NAMESPACE_DECL
10030 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
10034 return tsubst_decl (t
, args
, complain
);
10036 if (args
== NULL_TREE
)
10039 code
= TREE_CODE (t
);
10041 if (code
== IDENTIFIER_NODE
)
10042 type
= IDENTIFIER_TYPE_VALUE (t
);
10044 type
= TREE_TYPE (t
);
10046 gcc_assert (type
!= unknown_type_node
);
10048 /* Reuse typedefs. We need to do this to handle dependent attributes,
10049 such as attribute aligned. */
10051 && typedef_variant_p (t
))
10053 tree decl
= TYPE_NAME (t
);
10055 if (DECL_CLASS_SCOPE_P (decl
)
10056 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10057 && uses_template_parms (DECL_CONTEXT (decl
)))
10059 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10060 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
10061 r
= retrieve_specialization (tmpl
, gen_args
, 0);
10063 else if (DECL_FUNCTION_SCOPE_P (decl
)
10064 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10065 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
10066 r
= retrieve_local_specialization (decl
);
10068 /* The typedef is from a non-template context. */
10074 r
= cp_build_qualified_type_real
10075 (r
, cp_type_quals (t
) | cp_type_quals (r
),
10076 complain
| tf_ignore_bad_quals
);
10079 /* Else we must be instantiating the typedef, so fall through. */
10083 && code
!= TYPENAME_TYPE
10084 && code
!= TEMPLATE_TYPE_PARM
10085 && code
!= IDENTIFIER_NODE
10086 && code
!= FUNCTION_TYPE
10087 && code
!= METHOD_TYPE
)
10088 type
= tsubst (type
, args
, complain
, in_decl
);
10089 if (type
== error_mark_node
)
10090 return error_mark_node
;
10096 case ENUMERAL_TYPE
:
10097 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
10098 /*entering_scope=*/0);
10101 case IDENTIFIER_NODE
:
10112 if (t
== integer_type_node
)
10115 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
10116 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
10120 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
10122 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
10123 /*integral_constant_expression_p=*/false);
10125 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10127 if (TREE_CODE (max
) == NOP_EXPR
10128 && TREE_SIDE_EFFECTS (omax
)
10129 && !TREE_TYPE (max
))
10130 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
10132 max
= mark_rvalue_use (max
);
10133 max
= fold_decl_constant_value (max
);
10135 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10136 with TREE_SIDE_EFFECTS that indicates this is not an integral
10137 constant expression. */
10138 if (processing_template_decl
10139 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
10141 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
10142 TREE_SIDE_EFFECTS (max
) = 1;
10145 if (TREE_CODE (max
) != INTEGER_CST
10146 && !at_function_scope_p ()
10147 && !TREE_SIDE_EFFECTS (max
)
10148 && !value_dependent_expression_p (max
))
10150 if (complain
& tf_error
)
10151 error ("array bound is not an integer constant");
10152 return error_mark_node
;
10157 Type deduction may fail for any of the following
10160 Attempting to create an array with a size that is
10161 zero or negative. */
10162 if (integer_zerop (max
) && !(complain
& tf_error
))
10163 /* We must fail if performing argument deduction (as
10164 indicated by the state of complain), so that
10165 another substitution can be found. */
10166 return error_mark_node
;
10167 else if (TREE_CODE (max
) == INTEGER_CST
10168 && INT_CST_LT (max
, integer_zero_node
))
10170 if (complain
& tf_error
)
10171 error ("creating array with negative size (%qE)", max
);
10173 return error_mark_node
;
10176 return compute_array_index_type (NULL_TREE
, max
);
10179 case TEMPLATE_TYPE_PARM
:
10180 case TEMPLATE_TEMPLATE_PARM
:
10181 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10182 case TEMPLATE_PARM_INDEX
:
10187 tree arg
= NULL_TREE
;
10191 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
10192 template_parm_level_and_index (t
, &level
, &idx
);
10194 levels
= TMPL_ARGS_DEPTH (args
);
10195 if (level
<= levels
)
10197 arg
= TMPL_ARG (args
, level
, idx
);
10199 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
10200 /* See through ARGUMENT_PACK_SELECT arguments. */
10201 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
10204 if (arg
== error_mark_node
)
10205 return error_mark_node
;
10206 else if (arg
!= NULL_TREE
)
10208 if (ARGUMENT_PACK_P (arg
))
10209 /* If ARG is an argument pack, we don't actually want to
10210 perform a substitution here, because substitutions
10211 for argument packs are only done
10212 element-by-element. We can get to this point when
10213 substituting the type of a non-type template
10214 parameter pack, when that type actually contains
10215 template parameter packs from an outer template, e.g.,
10217 template<typename... Types> struct A {
10218 template<Types... Values> struct B { };
10222 if (code
== TEMPLATE_TYPE_PARM
)
10225 gcc_assert (TYPE_P (arg
));
10227 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
10229 return cp_build_qualified_type_real
10230 (arg
, quals
, complain
| tf_ignore_bad_quals
);
10232 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
10234 /* We are processing a type constructed from a
10235 template template parameter. */
10236 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
10237 args
, complain
, in_decl
);
10238 if (argvec
== error_mark_node
)
10239 return error_mark_node
;
10241 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10242 are resolving nested-types in the signature of a
10243 member function templates. Otherwise ARG is a
10244 TEMPLATE_DECL and is the real template to be
10246 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
10247 arg
= TYPE_NAME (arg
);
10249 r
= lookup_template_class (arg
,
10251 DECL_CONTEXT (arg
),
10252 /*entering_scope=*/0,
10254 return cp_build_qualified_type_real
10255 (r
, cp_type_quals (t
), complain
);
10258 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
10259 return unshare_expr (arg
);
10263 /* This can happen during the attempted tsubst'ing in
10264 unify. This means that we don't yet have any information
10265 about the template parameter in question. */
10268 /* If we get here, we must have been looking at a parm for a
10269 more deeply nested template. Make a new version of this
10270 template parameter, but with a lower level. */
10273 case TEMPLATE_TYPE_PARM
:
10274 case TEMPLATE_TEMPLATE_PARM
:
10275 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10276 if (cp_type_quals (t
))
10278 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
10279 r
= cp_build_qualified_type_real
10280 (r
, cp_type_quals (t
),
10281 complain
| (code
== TEMPLATE_TYPE_PARM
10282 ? tf_ignore_bad_quals
: 0));
10287 TEMPLATE_TYPE_PARM_INDEX (r
)
10288 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
10289 r
, levels
, args
, complain
);
10290 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
10291 TYPE_MAIN_VARIANT (r
) = r
;
10292 TYPE_POINTER_TO (r
) = NULL_TREE
;
10293 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
10295 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
10296 /* We have reduced the level of the template
10297 template parameter, but not the levels of its
10298 template parameters, so canonical_type_parameter
10299 will not be able to find the canonical template
10300 template parameter for this level. Thus, we
10301 require structural equality checking to compare
10302 TEMPLATE_TEMPLATE_PARMs. */
10303 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10304 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
10305 SET_TYPE_STRUCTURAL_EQUALITY (r
);
10307 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
10309 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
10311 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
10312 complain
, in_decl
);
10313 if (argvec
== error_mark_node
)
10314 return error_mark_node
;
10316 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
10317 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
10322 case TEMPLATE_PARM_INDEX
:
10323 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
10327 gcc_unreachable ();
10335 tree purpose
, value
, chain
;
10337 if (t
== void_list_node
)
10340 purpose
= TREE_PURPOSE (t
);
10343 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
10344 if (purpose
== error_mark_node
)
10345 return error_mark_node
;
10347 value
= TREE_VALUE (t
);
10350 value
= tsubst (value
, args
, complain
, in_decl
);
10351 if (value
== error_mark_node
)
10352 return error_mark_node
;
10354 chain
= TREE_CHAIN (t
);
10355 if (chain
&& chain
!= void_type_node
)
10357 chain
= tsubst (chain
, args
, complain
, in_decl
);
10358 if (chain
== error_mark_node
)
10359 return error_mark_node
;
10361 if (purpose
== TREE_PURPOSE (t
)
10362 && value
== TREE_VALUE (t
)
10363 && chain
== TREE_CHAIN (t
))
10365 return hash_tree_cons (purpose
, value
, chain
);
10369 /* We should never be tsubsting a binfo. */
10370 gcc_unreachable ();
10373 /* A vector of template arguments. */
10374 gcc_assert (!type
);
10375 return tsubst_template_args (t
, args
, complain
, in_decl
);
10378 case REFERENCE_TYPE
:
10380 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
10385 Type deduction may fail for any of the following
10388 -- Attempting to create a pointer to reference type.
10389 -- Attempting to create a reference to a reference type or
10390 a reference to void.
10392 Core issue 106 says that creating a reference to a reference
10393 during instantiation is no longer a cause for failure. We
10394 only enforce this check in strict C++98 mode. */
10395 if ((TREE_CODE (type
) == REFERENCE_TYPE
10396 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
10397 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
10399 static location_t last_loc
;
10401 /* We keep track of the last time we issued this error
10402 message to avoid spewing a ton of messages during a
10403 single bad template instantiation. */
10404 if (complain
& tf_error
10405 && last_loc
!= input_location
)
10407 if (TREE_CODE (type
) == VOID_TYPE
)
10408 error ("forming reference to void");
10409 else if (code
== POINTER_TYPE
)
10410 error ("forming pointer to reference type %qT", type
);
10412 error ("forming reference to reference type %qT", type
);
10413 last_loc
= input_location
;
10416 return error_mark_node
;
10418 else if (code
== POINTER_TYPE
)
10420 r
= build_pointer_type (type
);
10421 if (TREE_CODE (type
) == METHOD_TYPE
)
10422 r
= build_ptrmemfunc_type (r
);
10424 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
10425 /* In C++0x, during template argument substitution, when there is an
10426 attempt to create a reference to a reference type, reference
10427 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
10429 "If a template-argument for a template-parameter T names a type
10430 that is a reference to a type A, an attempt to create the type
10431 'lvalue reference to cv T' creates the type 'lvalue reference to
10432 A,' while an attempt to create the type type rvalue reference to
10433 cv T' creates the type T"
10435 r
= cp_build_reference_type
10437 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
10439 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
10440 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
10442 if (r
!= error_mark_node
)
10443 /* Will this ever be needed for TYPE_..._TO values? */
10450 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
10451 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
10455 Type deduction may fail for any of the following
10458 -- Attempting to create "pointer to member of T" when T
10459 is not a class type. */
10460 if (complain
& tf_error
)
10461 error ("creating pointer to member of non-class type %qT", r
);
10462 return error_mark_node
;
10464 if (TREE_CODE (type
) == REFERENCE_TYPE
)
10466 if (complain
& tf_error
)
10467 error ("creating pointer to member reference type %qT", type
);
10468 return error_mark_node
;
10470 if (TREE_CODE (type
) == VOID_TYPE
)
10472 if (complain
& tf_error
)
10473 error ("creating pointer to member of type void");
10474 return error_mark_node
;
10476 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
10477 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10479 /* The type of the implicit object parameter gets its
10480 cv-qualifiers from the FUNCTION_TYPE. */
10482 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
10483 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
10484 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
10488 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
10492 case FUNCTION_TYPE
:
10497 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
10498 if (fntype
== error_mark_node
)
10499 return error_mark_node
;
10501 /* Substitute the exception specification. */
10502 specs
= tsubst_exception_specification (t
, args
, complain
,
10504 if (specs
== error_mark_node
)
10505 return error_mark_node
;
10507 fntype
= build_exception_variant (fntype
, specs
);
10512 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
10513 if (domain
== error_mark_node
)
10514 return error_mark_node
;
10516 /* As an optimization, we avoid regenerating the array type if
10517 it will obviously be the same as T. */
10518 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
10521 /* These checks should match the ones in grokdeclarator.
10525 The deduction may fail for any of the following reasons:
10527 -- Attempting to create an array with an element type that
10528 is void, a function type, or a reference type, or [DR337]
10529 an abstract class type. */
10530 if (TREE_CODE (type
) == VOID_TYPE
10531 || TREE_CODE (type
) == FUNCTION_TYPE
10532 || TREE_CODE (type
) == REFERENCE_TYPE
)
10534 if (complain
& tf_error
)
10535 error ("creating array of %qT", type
);
10536 return error_mark_node
;
10538 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
10540 if (complain
& tf_error
)
10541 error ("creating array of %qT, which is an abstract class type",
10543 return error_mark_node
;
10546 r
= build_cplus_array_type (type
, domain
);
10548 if (TYPE_USER_ALIGN (t
))
10550 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
10551 TYPE_USER_ALIGN (r
) = 1;
10557 case TYPENAME_TYPE
:
10559 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
10560 in_decl
, /*entering_scope=*/1);
10561 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
10562 complain
, in_decl
);
10564 if (ctx
== error_mark_node
|| f
== error_mark_node
)
10565 return error_mark_node
;
10567 if (!MAYBE_CLASS_TYPE_P (ctx
))
10569 if (complain
& tf_error
)
10570 error ("%qT is not a class, struct, or union type", ctx
);
10571 return error_mark_node
;
10573 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
10575 /* Normally, make_typename_type does not require that the CTX
10576 have complete type in order to allow things like:
10578 template <class T> struct S { typename S<T>::X Y; };
10580 But, such constructs have already been resolved by this
10581 point, so here CTX really should have complete type, unless
10582 it's a partial instantiation. */
10583 if (!(complain
& tf_no_class_instantiations
))
10584 ctx
= complete_type (ctx
);
10585 if (!COMPLETE_TYPE_P (ctx
))
10587 if (complain
& tf_error
)
10588 cxx_incomplete_type_error (NULL_TREE
, ctx
);
10589 return error_mark_node
;
10593 f
= make_typename_type (ctx
, f
, typename_type
,
10594 (complain
& tf_error
) | tf_keep_type_decl
);
10595 if (f
== error_mark_node
)
10597 if (TREE_CODE (f
) == TYPE_DECL
)
10599 complain
|= tf_ignore_bad_quals
;
10603 if (TREE_CODE (f
) != TYPENAME_TYPE
)
10605 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
10606 error ("%qT resolves to %qT, which is not an enumeration type",
10608 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
10609 error ("%qT resolves to %qT, which is is not a class type",
10613 return cp_build_qualified_type_real
10614 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
10617 case UNBOUND_CLASS_TEMPLATE
:
10619 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
10620 in_decl
, /*entering_scope=*/1);
10621 tree name
= TYPE_IDENTIFIER (t
);
10622 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
10624 if (ctx
== error_mark_node
|| name
== error_mark_node
)
10625 return error_mark_node
;
10628 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
10629 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
10636 ++cp_unevaluated_operand
;
10637 ++c_inhibit_evaluation_warnings
;
10639 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
10641 /*integral_constant_expression_p=*/false);
10643 --cp_unevaluated_operand
;
10644 --c_inhibit_evaluation_warnings
;
10646 type
= finish_typeof (type
);
10647 return cp_build_qualified_type_real (type
,
10649 | cp_type_quals (type
),
10653 case DECLTYPE_TYPE
:
10657 ++cp_unevaluated_operand
;
10658 ++c_inhibit_evaluation_warnings
;
10660 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
10662 /*integral_constant_expression_p=*/false);
10664 --cp_unevaluated_operand
;
10665 --c_inhibit_evaluation_warnings
;
10667 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
10668 type
= lambda_capture_field_type (type
);
10669 else if (DECLTYPE_FOR_LAMBDA_RETURN (t
))
10670 type
= lambda_return_type (type
);
10672 type
= finish_decltype_type
10673 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
));
10674 return cp_build_qualified_type_real (type
,
10676 | cp_type_quals (type
),
10680 case TYPE_ARGUMENT_PACK
:
10681 case NONTYPE_ARGUMENT_PACK
:
10683 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
10685 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
10689 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
10691 /* For template nontype argument packs, also substitute into
10693 if (code
== NONTYPE_ARGUMENT_PACK
)
10694 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10712 /* We should use one of the expression tsubsts for these codes. */
10713 gcc_unreachable ();
10716 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
10717 return error_mark_node
;
10721 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10722 type of the expression on the left-hand side of the "." or "->"
10726 tsubst_baselink (tree baselink
, tree object_type
,
10727 tree args
, tsubst_flags_t complain
, tree in_decl
)
10730 tree qualifying_scope
;
10733 tree template_args
= 0;
10734 bool template_id_p
= false;
10736 /* A baselink indicates a function from a base class. Both the
10737 BASELINK_ACCESS_BINFO and the base class referenced may
10738 indicate bases of the template class, rather than the
10739 instantiated class. In addition, lookups that were not
10740 ambiguous before may be ambiguous now. Therefore, we perform
10741 the lookup again. */
10742 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
10743 qualifying_scope
= tsubst (qualifying_scope
, args
,
10744 complain
, in_decl
);
10745 fns
= BASELINK_FUNCTIONS (baselink
);
10746 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
10747 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
10749 template_id_p
= true;
10750 template_args
= TREE_OPERAND (fns
, 1);
10751 fns
= TREE_OPERAND (fns
, 0);
10753 template_args
= tsubst_template_args (template_args
, args
,
10754 complain
, in_decl
);
10756 name
= DECL_NAME (get_first_fn (fns
));
10757 if (IDENTIFIER_TYPENAME_P (name
))
10758 name
= mangle_conv_op_name_for_type (optype
);
10759 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
10761 return error_mark_node
;
10763 /* If lookup found a single function, mark it as used at this
10764 point. (If it lookup found multiple functions the one selected
10765 later by overload resolution will be marked as used at that
10767 if (BASELINK_P (baselink
))
10768 fns
= BASELINK_FUNCTIONS (baselink
);
10769 if (!template_id_p
&& !really_overloaded_fn (fns
))
10770 mark_used (OVL_CURRENT (fns
));
10772 /* Add back the template arguments, if present. */
10773 if (BASELINK_P (baselink
) && template_id_p
)
10774 BASELINK_FUNCTIONS (baselink
)
10775 = build_nt (TEMPLATE_ID_EXPR
,
10776 BASELINK_FUNCTIONS (baselink
),
10778 /* Update the conversion operator type. */
10779 BASELINK_OPTYPE (baselink
) = optype
;
10782 object_type
= current_class_type
;
10783 return adjust_result_of_qualified_name_lookup (baselink
,
10788 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10789 true if the qualified-id will be a postfix-expression in-and-of
10790 itself; false if more of the postfix-expression follows the
10791 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10795 tsubst_qualified_id (tree qualified_id
, tree args
,
10796 tsubst_flags_t complain
, tree in_decl
,
10797 bool done
, bool address_p
)
10803 tree template_args
;
10805 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
10807 /* Figure out what name to look up. */
10808 name
= TREE_OPERAND (qualified_id
, 1);
10809 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
10811 is_template
= true;
10812 template_args
= TREE_OPERAND (name
, 1);
10814 template_args
= tsubst_template_args (template_args
, args
,
10815 complain
, in_decl
);
10816 name
= TREE_OPERAND (name
, 0);
10820 is_template
= false;
10821 template_args
= NULL_TREE
;
10824 /* Substitute into the qualifying scope. When there are no ARGS, we
10825 are just trying to simplify a non-dependent expression. In that
10826 case the qualifying scope may be dependent, and, in any case,
10827 substituting will not help. */
10828 scope
= TREE_OPERAND (qualified_id
, 0);
10831 scope
= tsubst (scope
, args
, complain
, in_decl
);
10832 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
10837 if (dependent_scope_p (scope
))
10838 return build_qualified_name (NULL_TREE
, scope
, expr
,
10839 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
10841 if (!BASELINK_P (name
) && !DECL_P (expr
))
10843 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
10845 /* A BIT_NOT_EXPR is used to represent a destructor. */
10846 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
10848 error ("qualifying type %qT does not match destructor name ~%qT",
10849 scope
, TREE_OPERAND (expr
, 0));
10850 expr
= error_mark_node
;
10853 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
10854 /*is_type_p=*/0, false);
10857 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
10858 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
10859 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
10861 if (complain
& tf_error
)
10863 error ("dependent-name %qE is parsed as a non-type, but "
10864 "instantiation yields a type", qualified_id
);
10865 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
10867 return error_mark_node
;
10873 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
10875 /* Remember that there was a reference to this entity. */
10879 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
10881 if (complain
& tf_error
)
10882 qualified_name_lookup_error (scope
,
10883 TREE_OPERAND (qualified_id
, 1),
10884 expr
, input_location
);
10885 return error_mark_node
;
10889 expr
= lookup_template_function (expr
, template_args
);
10891 if (expr
== error_mark_node
&& complain
& tf_error
)
10892 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
10893 expr
, input_location
);
10894 else if (TYPE_P (scope
))
10896 expr
= (adjust_result_of_qualified_name_lookup
10897 (expr
, scope
, current_class_type
));
10898 expr
= (finish_qualified_id_expr
10899 (scope
, expr
, done
, address_p
,
10900 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
10901 /*template_arg_p=*/false));
10904 /* Expressions do not generally have reference type. */
10905 if (TREE_CODE (expr
) != SCOPE_REF
10906 /* However, if we're about to form a pointer-to-member, we just
10907 want the referenced member referenced. */
10908 && TREE_CODE (expr
) != OFFSET_REF
)
10909 expr
= convert_from_reference (expr
);
10914 /* Like tsubst, but deals with expressions. This function just replaces
10915 template parms; to finish processing the resultant expression, use
10919 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10921 enum tree_code code
;
10924 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
10927 code
= TREE_CODE (t
);
10932 r
= retrieve_local_specialization (t
);
10937 /* This can happen for a parameter name used later in a function
10938 declaration (such as in a late-specified return type). Just
10939 make a dummy decl, since it's only used for its type. */
10940 gcc_assert (cp_unevaluated_operand
!= 0);
10941 /* We copy T because want to tsubst the PARM_DECL only,
10942 not the following PARM_DECLs that are chained to T. */
10944 r
= tsubst_decl (c
, args
, complain
);
10945 /* Give it the template pattern as its context; its true context
10946 hasn't been instantiated yet and this is good enough for
10948 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
10951 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
10952 r
= ARGUMENT_PACK_SELECT_ARG (r
);
10961 if (DECL_TEMPLATE_PARM_P (t
))
10962 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
10963 /* There is no need to substitute into namespace-scope
10965 if (DECL_NAMESPACE_SCOPE_P (t
))
10967 /* If ARGS is NULL, then T is known to be non-dependent. */
10968 if (args
== NULL_TREE
)
10969 return integral_constant_value (t
);
10971 /* Unfortunately, we cannot just call lookup_name here.
10974 template <int I> int f() {
10976 struct S { void g() { E e = a; } };
10979 When we instantiate f<7>::S::g(), say, lookup_name is not
10980 clever enough to find f<7>::a. */
10982 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
10983 /*entering_scope=*/0);
10985 for (v
= TYPE_VALUES (enum_type
);
10987 v
= TREE_CHAIN (v
))
10988 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
10989 return TREE_VALUE (v
);
10991 /* We didn't find the name. That should never happen; if
10992 name-lookup found it during preliminary parsing, we
10993 should find it again here during instantiation. */
10994 gcc_unreachable ();
10999 if (DECL_CONTEXT (t
))
11003 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11004 /*entering_scope=*/1);
11005 if (ctx
!= DECL_CONTEXT (t
))
11007 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
11010 if (complain
& tf_error
)
11011 error ("using invalid field %qD", t
);
11012 return error_mark_node
;
11021 case FUNCTION_DECL
:
11022 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
11023 || local_variable_p (t
))
11024 t
= tsubst (t
, args
, complain
, in_decl
);
11029 /* An OVERLOAD will always be a non-dependent overload set; an
11030 overload set from function scope will just be represented with an
11031 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11032 gcc_assert (!uses_template_parms (t
));
11036 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
11038 case TEMPLATE_DECL
:
11039 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11040 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
11041 args
, complain
, in_decl
);
11042 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
11043 return tsubst (t
, args
, complain
, in_decl
);
11044 else if (DECL_CLASS_SCOPE_P (t
)
11045 && uses_template_parms (DECL_CONTEXT (t
)))
11047 /* Template template argument like the following example need
11050 template <template <class> class TT> struct C {};
11051 template <class T> struct D {
11052 template <class U> struct E {};
11057 We are processing the template argument `E' in #1 for
11058 the template instantiation #2. Originally, `E' is a
11059 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11060 have to substitute this with one having context `D<int>'. */
11062 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
11063 return lookup_field (context
, DECL_NAME(t
), 0, false);
11066 /* Ordinary template template argument. */
11070 case REINTERPRET_CAST_EXPR
:
11071 case CONST_CAST_EXPR
:
11072 case STATIC_CAST_EXPR
:
11073 case DYNAMIC_CAST_EXPR
:
11076 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11077 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11080 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
11082 /* We only want to compute the number of arguments. */
11083 tree expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
11084 complain
, in_decl
);
11087 if (TREE_CODE (expanded
) == TREE_VEC
)
11088 len
= TREE_VEC_LENGTH (expanded
);
11090 if (expanded
== error_mark_node
)
11091 return error_mark_node
;
11092 else if (PACK_EXPANSION_P (expanded
)
11093 || (TREE_CODE (expanded
) == TREE_VEC
11095 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
11097 if (TREE_CODE (expanded
) == TREE_VEC
)
11098 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
11100 if (TYPE_P (expanded
))
11101 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
11102 complain
& tf_error
);
11104 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
11105 complain
& tf_error
);
11108 return build_int_cst (size_type_node
, len
);
11114 case TRUTH_NOT_EXPR
:
11117 case UNARY_PLUS_EXPR
: /* Unary + */
11119 case AT_ENCODE_EXPR
:
11123 case REALPART_EXPR
:
11124 case IMAGPART_EXPR
:
11126 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11127 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11129 case COMPONENT_REF
:
11134 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
11135 name
= TREE_OPERAND (t
, 1);
11136 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
11138 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11139 complain
, in_decl
);
11140 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11142 else if (TREE_CODE (name
) == SCOPE_REF
11143 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
11145 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11146 complain
, in_decl
);
11147 name
= TREE_OPERAND (name
, 1);
11148 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11149 complain
, in_decl
);
11150 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11151 name
= build_qualified_name (/*type=*/NULL_TREE
,
11153 /*template_p=*/false);
11155 else if (TREE_CODE (name
) == BASELINK
)
11156 name
= tsubst_baselink (name
,
11157 non_reference (TREE_TYPE (object
)),
11161 name
= tsubst_copy (name
, args
, complain
, in_decl
);
11162 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
11168 case TRUNC_DIV_EXPR
:
11169 case CEIL_DIV_EXPR
:
11170 case FLOOR_DIV_EXPR
:
11171 case ROUND_DIV_EXPR
:
11172 case EXACT_DIV_EXPR
:
11176 case TRUNC_MOD_EXPR
:
11177 case FLOOR_MOD_EXPR
:
11178 case TRUTH_ANDIF_EXPR
:
11179 case TRUTH_ORIF_EXPR
:
11180 case TRUTH_AND_EXPR
:
11181 case TRUTH_OR_EXPR
:
11194 case COMPOUND_EXPR
:
11197 case PREDECREMENT_EXPR
:
11198 case PREINCREMENT_EXPR
:
11199 case POSTDECREMENT_EXPR
:
11200 case POSTINCREMENT_EXPR
:
11202 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11203 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11206 return build_qualified_name (/*type=*/NULL_TREE
,
11207 tsubst_copy (TREE_OPERAND (t
, 0),
11208 args
, complain
, in_decl
),
11209 tsubst_copy (TREE_OPERAND (t
, 1),
11210 args
, complain
, in_decl
),
11211 QUALIFIED_NAME_IS_TEMPLATE (t
));
11216 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11217 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11218 NULL_TREE
, NULL_TREE
);
11222 int n
= VL_EXP_OPERAND_LENGTH (t
);
11223 tree result
= build_vl_exp (CALL_EXPR
, n
);
11225 for (i
= 0; i
< n
; i
++)
11226 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
11227 complain
, in_decl
);
11233 case PSEUDO_DTOR_EXPR
:
11236 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11237 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11238 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11239 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
11246 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11247 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11248 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
11249 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
11256 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11257 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11258 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
11259 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
11263 case TEMPLATE_ID_EXPR
:
11265 /* Substituted template arguments */
11266 tree fn
= TREE_OPERAND (t
, 0);
11267 tree targs
= TREE_OPERAND (t
, 1);
11269 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
11271 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
11273 return lookup_template_function (fn
, targs
);
11278 tree purpose
, value
, chain
;
11280 if (t
== void_list_node
)
11283 purpose
= TREE_PURPOSE (t
);
11285 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
11286 value
= TREE_VALUE (t
);
11288 value
= tsubst_copy (value
, args
, complain
, in_decl
);
11289 chain
= TREE_CHAIN (t
);
11290 if (chain
&& chain
!= void_type_node
)
11291 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
11292 if (purpose
== TREE_PURPOSE (t
)
11293 && value
== TREE_VALUE (t
)
11294 && chain
== TREE_CHAIN (t
))
11296 return tree_cons (purpose
, value
, chain
);
11301 case ENUMERAL_TYPE
:
11303 case TEMPLATE_TYPE_PARM
:
11304 case TEMPLATE_TEMPLATE_PARM
:
11305 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11306 case TEMPLATE_PARM_INDEX
:
11308 case REFERENCE_TYPE
:
11310 case FUNCTION_TYPE
:
11313 case TYPENAME_TYPE
:
11314 case UNBOUND_CLASS_TEMPLATE
:
11316 case DECLTYPE_TYPE
:
11318 return tsubst (t
, args
, complain
, in_decl
);
11320 case IDENTIFIER_NODE
:
11321 if (IDENTIFIER_TYPENAME_P (t
))
11323 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11324 return mangle_conv_op_name_for_type (new_type
);
11330 /* This is handled by tsubst_copy_and_build. */
11331 gcc_unreachable ();
11334 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
11336 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
11338 case CLEANUP_POINT_EXPR
:
11339 /* We shouldn't have built any of these during initial template
11340 generation. Instead, they should be built during instantiation
11341 in response to the saved STMT_IS_FULL_EXPR_P setting. */
11342 gcc_unreachable ();
11346 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11347 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11348 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11349 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
11350 mark_used (TREE_OPERAND (r
, 1));
11353 case EXPR_PACK_EXPANSION
:
11354 error ("invalid use of pack expansion expression");
11355 return error_mark_node
;
11357 case NONTYPE_ARGUMENT_PACK
:
11358 error ("use %<...%> to expand argument pack");
11359 return error_mark_node
;
11365 /* Instantiate any typedefs in the type. */
11366 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11367 r
= fold_convert (type
, t
);
11368 gcc_assert (TREE_CODE (r
) == code
);
11373 /* These can sometimes show up in a partial instantiation, but never
11374 involve template parms. */
11375 gcc_assert (!uses_template_parms (t
));
11379 gcc_unreachable ();
11383 /* Like tsubst_copy, but specifically for OpenMP clauses. */
11386 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
11389 tree new_clauses
= NULL
, nc
, oc
;
11391 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
11393 nc
= copy_node (oc
);
11394 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
11397 switch (OMP_CLAUSE_CODE (nc
))
11399 case OMP_CLAUSE_LASTPRIVATE
:
11400 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
11402 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
11403 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
11404 in_decl
, /*integral_constant_expression_p=*/false);
11405 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
11406 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
11409 case OMP_CLAUSE_PRIVATE
:
11410 case OMP_CLAUSE_SHARED
:
11411 case OMP_CLAUSE_FIRSTPRIVATE
:
11412 case OMP_CLAUSE_REDUCTION
:
11413 case OMP_CLAUSE_COPYIN
:
11414 case OMP_CLAUSE_COPYPRIVATE
:
11415 case OMP_CLAUSE_IF
:
11416 case OMP_CLAUSE_NUM_THREADS
:
11417 case OMP_CLAUSE_SCHEDULE
:
11418 case OMP_CLAUSE_COLLAPSE
:
11419 OMP_CLAUSE_OPERAND (nc
, 0)
11420 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
11421 in_decl
, /*integral_constant_expression_p=*/false);
11423 case OMP_CLAUSE_NOWAIT
:
11424 case OMP_CLAUSE_ORDERED
:
11425 case OMP_CLAUSE_DEFAULT
:
11426 case OMP_CLAUSE_UNTIED
:
11429 gcc_unreachable ();
11433 return finish_omp_clauses (nreverse (new_clauses
));
11436 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
11439 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
11442 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
11444 tree purpose
, value
, chain
;
11449 if (TREE_CODE (t
) != TREE_LIST
)
11450 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
11451 /*function_p=*/false,
11452 /*integral_constant_expression_p=*/false);
11454 if (t
== void_list_node
)
11457 purpose
= TREE_PURPOSE (t
);
11459 purpose
= RECUR (purpose
);
11460 value
= TREE_VALUE (t
);
11461 if (value
&& TREE_CODE (value
) != LABEL_DECL
)
11462 value
= RECUR (value
);
11463 chain
= TREE_CHAIN (t
);
11464 if (chain
&& chain
!= void_type_node
)
11465 chain
= RECUR (chain
);
11466 return tree_cons (purpose
, value
, chain
);
11470 /* Substitute one OMP_FOR iterator. */
11473 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
11474 tree condv
, tree incrv
, tree
*clauses
,
11475 tree args
, tsubst_flags_t complain
, tree in_decl
,
11476 bool integral_constant_expression_p
)
11478 #define RECUR(NODE) \
11479 tsubst_expr ((NODE), args, complain, in_decl, \
11480 integral_constant_expression_p)
11481 tree decl
, init
, cond
, incr
, auto_node
;
11483 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
11484 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
11485 decl
= RECUR (TREE_OPERAND (init
, 0));
11486 init
= TREE_OPERAND (init
, 1);
11487 auto_node
= type_uses_auto (TREE_TYPE (decl
));
11488 if (auto_node
&& init
)
11490 tree init_expr
= init
;
11491 if (TREE_CODE (init_expr
) == DECL_EXPR
)
11492 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
11493 init_expr
= RECUR (init_expr
);
11495 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
11497 gcc_assert (!type_dependent_expression_p (decl
));
11499 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
11501 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
11502 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11503 if (TREE_CODE (incr
) == MODIFY_EXPR
)
11504 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
11505 RECUR (TREE_OPERAND (incr
, 1)),
11508 incr
= RECUR (incr
);
11509 TREE_VEC_ELT (declv
, i
) = decl
;
11510 TREE_VEC_ELT (initv
, i
) = init
;
11511 TREE_VEC_ELT (condv
, i
) = cond
;
11512 TREE_VEC_ELT (incrv
, i
) = incr
;
11516 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
11519 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
11521 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
11522 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
11523 && OMP_CLAUSE_DECL (c
) == decl
)
11525 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
11526 && OMP_CLAUSE_DECL (c
) == decl
)
11527 error ("iteration variable %qD should not be firstprivate", decl
);
11528 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
11529 && OMP_CLAUSE_DECL (c
) == decl
)
11530 error ("iteration variable %qD should not be reduction", decl
);
11534 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
11535 OMP_CLAUSE_DECL (c
) = decl
;
11536 c
= finish_omp_clauses (c
);
11539 OMP_CLAUSE_CHAIN (c
) = *clauses
;
11544 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
11545 if (COMPARISON_CLASS_P (cond
))
11546 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
11547 RECUR (TREE_OPERAND (cond
, 0)),
11548 RECUR (TREE_OPERAND (cond
, 1)));
11550 cond
= RECUR (cond
);
11551 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
11552 switch (TREE_CODE (incr
))
11554 case PREINCREMENT_EXPR
:
11555 case PREDECREMENT_EXPR
:
11556 case POSTINCREMENT_EXPR
:
11557 case POSTDECREMENT_EXPR
:
11558 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
11559 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
11562 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11563 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11565 tree rhs
= TREE_OPERAND (incr
, 1);
11566 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11567 RECUR (TREE_OPERAND (incr
, 0)),
11568 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11569 RECUR (TREE_OPERAND (rhs
, 0)),
11570 RECUR (TREE_OPERAND (rhs
, 1))));
11573 incr
= RECUR (incr
);
11576 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
11577 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
11579 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
11580 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
11581 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
11582 TREE_TYPE (decl
), lhs
,
11583 RECUR (TREE_OPERAND (incr
, 2))));
11585 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
11586 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
11587 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
11589 tree rhs
= TREE_OPERAND (incr
, 2);
11590 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
11591 RECUR (TREE_OPERAND (incr
, 0)),
11592 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
11593 RECUR (TREE_OPERAND (rhs
, 0)),
11594 RECUR (TREE_OPERAND (rhs
, 1))));
11597 incr
= RECUR (incr
);
11600 incr
= RECUR (incr
);
11604 TREE_VEC_ELT (declv
, i
) = decl
;
11605 TREE_VEC_ELT (initv
, i
) = init
;
11606 TREE_VEC_ELT (condv
, i
) = cond
;
11607 TREE_VEC_ELT (incrv
, i
) = incr
;
11611 /* Like tsubst_copy for expressions, etc. but also does semantic
11615 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
11616 bool integral_constant_expression_p
)
11618 #define RECUR(NODE) \
11619 tsubst_expr ((NODE), args, complain, in_decl, \
11620 integral_constant_expression_p)
11624 if (t
== NULL_TREE
|| t
== error_mark_node
)
11627 if (EXPR_HAS_LOCATION (t
))
11628 input_location
= EXPR_LOCATION (t
);
11629 if (STATEMENT_CODE_P (TREE_CODE (t
)))
11630 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
11632 switch (TREE_CODE (t
))
11634 case STATEMENT_LIST
:
11636 tree_stmt_iterator i
;
11637 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
11638 RECUR (tsi_stmt (i
));
11642 case CTOR_INITIALIZER
:
11643 finish_mem_initializers (tsubst_initializer_list
11644 (TREE_OPERAND (t
, 0), args
));
11648 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
11652 tmp
= RECUR (EXPR_STMT_EXPR (t
));
11653 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
11654 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
11656 finish_expr_stmt (tmp
);
11660 do_using_directive (USING_STMT_NAMESPACE (t
));
11668 decl
= DECL_EXPR_DECL (t
);
11669 if (TREE_CODE (decl
) == LABEL_DECL
)
11670 finish_label_decl (DECL_NAME (decl
));
11671 else if (TREE_CODE (decl
) == USING_DECL
)
11673 tree scope
= USING_DECL_SCOPE (decl
);
11674 tree name
= DECL_NAME (decl
);
11677 scope
= tsubst (scope
, args
, complain
, in_decl
);
11678 decl
= lookup_qualified_name (scope
, name
,
11679 /*is_type_p=*/false,
11680 /*complain=*/false);
11681 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
11682 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
11684 do_local_using_decl (decl
, scope
, name
);
11688 init
= DECL_INITIAL (decl
);
11689 decl
= tsubst (decl
, args
, complain
, in_decl
);
11690 if (decl
!= error_mark_node
)
11692 /* By marking the declaration as instantiated, we avoid
11693 trying to instantiate it. Since instantiate_decl can't
11694 handle local variables, and since we've already done
11695 all that needs to be done, that's the right thing to
11697 if (TREE_CODE (decl
) == VAR_DECL
)
11698 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
11699 if (TREE_CODE (decl
) == VAR_DECL
11700 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
11701 /* Anonymous aggregates are a special case. */
11702 finish_anon_union (decl
);
11705 maybe_push_decl (decl
);
11706 if (TREE_CODE (decl
) == VAR_DECL
11707 && DECL_PRETTY_FUNCTION_P (decl
))
11709 /* For __PRETTY_FUNCTION__ we have to adjust the
11711 const char *const name
11712 = cxx_printable_name (current_function_decl
, 2);
11713 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
11717 tree t
= RECUR (init
);
11721 /* If we had an initializer but it
11722 instantiated to nothing,
11723 value-initialize the object. This will
11724 only occur when the initializer was a
11725 pack expansion where the parameter packs
11726 used in that expansion were of length
11728 init
= build_value_init (TREE_TYPE (decl
),
11730 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
11731 init
= get_target_expr (init
);
11737 cp_finish_decl (decl
, init
, false, NULL_TREE
, 0);
11742 /* A DECL_EXPR can also be used as an expression, in the condition
11743 clause of an if/for/while construct. */
11748 stmt
= begin_for_stmt ();
11749 RECUR (FOR_INIT_STMT (t
));
11750 finish_for_init_stmt (stmt
);
11751 tmp
= RECUR (FOR_COND (t
));
11752 finish_for_cond (tmp
, stmt
);
11753 tmp
= RECUR (FOR_EXPR (t
));
11754 finish_for_expr (tmp
, stmt
);
11755 RECUR (FOR_BODY (t
));
11756 finish_for_stmt (stmt
);
11759 case RANGE_FOR_STMT
:
11762 stmt
= begin_for_stmt ();
11763 decl
= RANGE_FOR_DECL (t
);
11764 decl
= tsubst (decl
, args
, complain
, in_decl
);
11765 maybe_push_decl (decl
);
11766 expr
= RECUR (RANGE_FOR_EXPR (t
));
11767 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
11768 RECUR (RANGE_FOR_BODY (t
));
11769 finish_for_stmt (stmt
);
11774 stmt
= begin_while_stmt ();
11775 tmp
= RECUR (WHILE_COND (t
));
11776 finish_while_stmt_cond (tmp
, stmt
);
11777 RECUR (WHILE_BODY (t
));
11778 finish_while_stmt (stmt
);
11782 stmt
= begin_do_stmt ();
11783 RECUR (DO_BODY (t
));
11784 finish_do_body (stmt
);
11785 tmp
= RECUR (DO_COND (t
));
11786 finish_do_stmt (tmp
, stmt
);
11790 stmt
= begin_if_stmt ();
11791 tmp
= RECUR (IF_COND (t
));
11792 finish_if_stmt_cond (tmp
, stmt
);
11793 RECUR (THEN_CLAUSE (t
));
11794 finish_then_clause (stmt
);
11796 if (ELSE_CLAUSE (t
))
11798 begin_else_clause (stmt
);
11799 RECUR (ELSE_CLAUSE (t
));
11800 finish_else_clause (stmt
);
11803 finish_if_stmt (stmt
);
11807 if (BIND_EXPR_BODY_BLOCK (t
))
11808 stmt
= begin_function_body ();
11810 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
11811 ? BCS_TRY_BLOCK
: 0);
11813 RECUR (BIND_EXPR_BODY (t
));
11815 if (BIND_EXPR_BODY_BLOCK (t
))
11816 finish_function_body (stmt
);
11818 finish_compound_stmt (stmt
);
11822 finish_break_stmt ();
11825 case CONTINUE_STMT
:
11826 finish_continue_stmt ();
11830 stmt
= begin_switch_stmt ();
11831 tmp
= RECUR (SWITCH_STMT_COND (t
));
11832 finish_switch_cond (tmp
, stmt
);
11833 RECUR (SWITCH_STMT_BODY (t
));
11834 finish_switch_stmt (stmt
);
11837 case CASE_LABEL_EXPR
:
11838 finish_case_label (EXPR_LOCATION (t
),
11839 RECUR (CASE_LOW (t
)),
11840 RECUR (CASE_HIGH (t
)));
11845 tree decl
= LABEL_EXPR_LABEL (t
);
11848 label
= finish_label_stmt (DECL_NAME (decl
));
11849 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
11850 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
11855 tmp
= GOTO_DESTINATION (t
);
11856 if (TREE_CODE (tmp
) != LABEL_DECL
)
11857 /* Computed goto's must be tsubst'd into. On the other hand,
11858 non-computed gotos must not be; the identifier in question
11859 will have no binding. */
11862 tmp
= DECL_NAME (tmp
);
11863 finish_goto_stmt (tmp
);
11867 tmp
= finish_asm_stmt
11868 (ASM_VOLATILE_P (t
),
11869 RECUR (ASM_STRING (t
)),
11870 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
11871 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
11872 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
11873 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
11875 tree asm_expr
= tmp
;
11876 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
11877 asm_expr
= TREE_OPERAND (asm_expr
, 0);
11878 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
11885 stmt
= begin_try_block ();
11886 RECUR (TRY_STMTS (t
));
11887 finish_cleanup_try_block (stmt
);
11888 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
11892 tree compound_stmt
= NULL_TREE
;
11894 if (FN_TRY_BLOCK_P (t
))
11895 stmt
= begin_function_try_block (&compound_stmt
);
11897 stmt
= begin_try_block ();
11899 RECUR (TRY_STMTS (t
));
11901 if (FN_TRY_BLOCK_P (t
))
11902 finish_function_try_block (stmt
);
11904 finish_try_block (stmt
);
11906 RECUR (TRY_HANDLERS (t
));
11907 if (FN_TRY_BLOCK_P (t
))
11908 finish_function_handler_sequence (stmt
, compound_stmt
);
11910 finish_handler_sequence (stmt
);
11916 tree decl
= HANDLER_PARMS (t
);
11920 decl
= tsubst (decl
, args
, complain
, in_decl
);
11921 /* Prevent instantiate_decl from trying to instantiate
11922 this variable. We've already done all that needs to be
11924 if (decl
!= error_mark_node
)
11925 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
11927 stmt
= begin_handler ();
11928 finish_handler_parms (decl
, stmt
);
11929 RECUR (HANDLER_BODY (t
));
11930 finish_handler (stmt
);
11935 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
11938 case STATIC_ASSERT
:
11941 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
11944 /*integral_constant_expression_p=*/true);
11945 finish_static_assert (condition
,
11946 STATIC_ASSERT_MESSAGE (t
),
11947 STATIC_ASSERT_SOURCE_LOCATION (t
),
11948 /*member_p=*/false);
11953 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
11954 args
, complain
, in_decl
);
11955 stmt
= begin_omp_parallel ();
11956 RECUR (OMP_PARALLEL_BODY (t
));
11957 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
11958 = OMP_PARALLEL_COMBINED (t
);
11962 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
11963 args
, complain
, in_decl
);
11964 stmt
= begin_omp_task ();
11965 RECUR (OMP_TASK_BODY (t
));
11966 finish_omp_task (tmp
, stmt
);
11971 tree clauses
, body
, pre_body
;
11972 tree declv
, initv
, condv
, incrv
;
11975 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
11976 args
, complain
, in_decl
);
11977 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11978 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11979 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11980 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
11982 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
11983 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
11984 &clauses
, args
, complain
, in_decl
,
11985 integral_constant_expression_p
);
11987 stmt
= begin_omp_structured_block ();
11989 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
11990 if (TREE_VEC_ELT (initv
, i
) == NULL
11991 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
11992 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
11993 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
11995 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
11996 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
11997 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
12001 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
12002 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
12003 gcc_assert (init
!= NULL
);
12004 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
12005 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
12007 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
12010 pre_body
= push_stmt_list ();
12011 RECUR (OMP_FOR_PRE_BODY (t
));
12012 pre_body
= pop_stmt_list (pre_body
);
12014 body
= push_stmt_list ();
12015 RECUR (OMP_FOR_BODY (t
));
12016 body
= pop_stmt_list (body
);
12018 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
12019 body
, pre_body
, clauses
);
12021 add_stmt (finish_omp_structured_block (stmt
));
12027 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
12028 stmt
= push_stmt_list ();
12029 RECUR (OMP_BODY (t
));
12030 stmt
= pop_stmt_list (stmt
);
12033 OMP_BODY (t
) = stmt
;
12034 OMP_CLAUSES (t
) = tmp
;
12042 stmt
= push_stmt_list ();
12043 RECUR (OMP_BODY (t
));
12044 stmt
= pop_stmt_list (stmt
);
12047 OMP_BODY (t
) = stmt
;
12052 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
12054 tree op1
= TREE_OPERAND (t
, 1);
12055 tree lhs
= RECUR (TREE_OPERAND (op1
, 0));
12056 tree rhs
= RECUR (TREE_OPERAND (op1
, 1));
12057 finish_omp_atomic (TREE_CODE (op1
), lhs
, rhs
);
12061 case EXPR_PACK_EXPANSION
:
12062 error ("invalid use of pack expansion expression");
12063 return error_mark_node
;
12065 case NONTYPE_ARGUMENT_PACK
:
12066 error ("use %<...%> to expand argument pack");
12067 return error_mark_node
;
12070 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
12072 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12073 /*function_p=*/false,
12074 integral_constant_expression_p
);
12081 /* T is a postfix-expression that is not being used in a function
12082 call. Return the substituted version of T. */
12085 tsubst_non_call_postfix_expression (tree t
, tree args
,
12086 tsubst_flags_t complain
,
12089 if (TREE_CODE (t
) == SCOPE_REF
)
12090 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
12091 /*done=*/false, /*address_p=*/false);
12093 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12094 /*function_p=*/false,
12095 /*integral_constant_expression_p=*/false);
12100 /* Like tsubst but deals with expressions and performs semantic
12101 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12104 tsubst_copy_and_build (tree t
,
12106 tsubst_flags_t complain
,
12109 bool integral_constant_expression_p
)
12111 #define RECUR(NODE) \
12112 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12113 /*function_p=*/false, \
12114 integral_constant_expression_p)
12118 if (t
== NULL_TREE
|| t
== error_mark_node
)
12121 switch (TREE_CODE (t
))
12125 /* Fall through. */
12126 case IDENTIFIER_NODE
:
12130 bool non_integral_constant_expression_p
;
12131 const char *error_msg
;
12133 if (IDENTIFIER_TYPENAME_P (t
))
12135 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12136 t
= mangle_conv_op_name_for_type (new_type
);
12139 /* Look up the name. */
12140 decl
= lookup_name (t
);
12142 /* By convention, expressions use ERROR_MARK_NODE to indicate
12143 failure, not NULL_TREE. */
12144 if (decl
== NULL_TREE
)
12145 decl
= error_mark_node
;
12147 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
12149 integral_constant_expression_p
,
12150 /*allow_non_integral_constant_expression_p=*/false,
12151 &non_integral_constant_expression_p
,
12152 /*template_p=*/false,
12154 /*address_p=*/false,
12155 /*template_arg_p=*/false,
12160 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
12161 decl
= unqualified_name_lookup_error (decl
);
12165 case TEMPLATE_ID_EXPR
:
12168 tree templ
= RECUR (TREE_OPERAND (t
, 0));
12169 tree targs
= TREE_OPERAND (t
, 1);
12172 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12174 if (TREE_CODE (templ
) == COMPONENT_REF
)
12176 object
= TREE_OPERAND (templ
, 0);
12177 templ
= TREE_OPERAND (templ
, 1);
12180 object
= NULL_TREE
;
12181 templ
= lookup_template_function (templ
, targs
);
12184 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
12185 object
, templ
, NULL_TREE
);
12187 return baselink_for_fns (templ
);
12192 tree r
= RECUR (TREE_OPERAND (t
, 0));
12194 if (REFERENCE_REF_P (t
))
12196 /* A type conversion to reference type will be enclosed in
12197 such an indirect ref, but the substitution of the cast
12198 will have also added such an indirect ref. */
12199 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
12200 r
= convert_from_reference (r
);
12203 r
= build_x_indirect_ref (r
, RO_UNARY_STAR
, complain
);
12209 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12210 RECUR (TREE_OPERAND (t
, 0)));
12213 case REINTERPRET_CAST_EXPR
:
12214 case CONST_CAST_EXPR
:
12215 case DYNAMIC_CAST_EXPR
:
12216 case STATIC_CAST_EXPR
:
12221 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12222 if (integral_constant_expression_p
12223 && !cast_valid_in_integral_constant_expression_p (type
))
12225 if (complain
& tf_error
)
12226 error ("a cast to a type other than an integral or "
12227 "enumeration type cannot appear in a constant-expression");
12228 return error_mark_node
;
12231 op
= RECUR (TREE_OPERAND (t
, 0));
12233 switch (TREE_CODE (t
))
12236 return build_functional_cast (type
, op
, complain
);
12237 case REINTERPRET_CAST_EXPR
:
12238 return build_reinterpret_cast (type
, op
, complain
);
12239 case CONST_CAST_EXPR
:
12240 return build_const_cast (type
, op
, complain
);
12241 case DYNAMIC_CAST_EXPR
:
12242 return build_dynamic_cast (type
, op
, complain
);
12243 case STATIC_CAST_EXPR
:
12244 return build_static_cast (type
, op
, complain
);
12246 gcc_unreachable ();
12250 case POSTDECREMENT_EXPR
:
12251 case POSTINCREMENT_EXPR
:
12252 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12253 args
, complain
, in_decl
);
12254 return build_x_unary_op (TREE_CODE (t
), op1
, complain
);
12256 case PREDECREMENT_EXPR
:
12257 case PREINCREMENT_EXPR
:
12261 case TRUTH_NOT_EXPR
:
12262 case UNARY_PLUS_EXPR
: /* Unary + */
12263 case REALPART_EXPR
:
12264 case IMAGPART_EXPR
:
12265 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)),
12269 op1
= TREE_OPERAND (t
, 0);
12270 if (TREE_CODE (op1
) == LABEL_DECL
)
12271 return finish_label_address_expr (DECL_NAME (op1
),
12272 EXPR_LOCATION (op1
));
12273 if (TREE_CODE (op1
) == SCOPE_REF
)
12274 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
12275 /*done=*/true, /*address_p=*/true);
12277 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
12279 return build_x_unary_op (ADDR_EXPR
, op1
, complain
);
12284 case TRUNC_DIV_EXPR
:
12285 case CEIL_DIV_EXPR
:
12286 case FLOOR_DIV_EXPR
:
12287 case ROUND_DIV_EXPR
:
12288 case EXACT_DIV_EXPR
:
12292 case TRUNC_MOD_EXPR
:
12293 case FLOOR_MOD_EXPR
:
12294 case TRUTH_ANDIF_EXPR
:
12295 case TRUTH_ORIF_EXPR
:
12296 case TRUTH_AND_EXPR
:
12297 case TRUTH_OR_EXPR
:
12312 return build_x_binary_op
12314 RECUR (TREE_OPERAND (t
, 0)),
12315 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
12317 : TREE_CODE (TREE_OPERAND (t
, 0))),
12318 RECUR (TREE_OPERAND (t
, 1)),
12319 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
12321 : TREE_CODE (TREE_OPERAND (t
, 1))),
12322 /*overloaded_p=*/NULL
,
12326 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
12327 /*address_p=*/false);
12329 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12330 args
, complain
, in_decl
);
12331 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
12334 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12335 return tsubst_copy (t
, args
, complain
, in_decl
);
12339 op1
= TREE_OPERAND (t
, 0);
12342 /* When there are no ARGS, we are trying to evaluate a
12343 non-dependent expression from the parser. Trying to do
12344 the substitutions may not work. */
12346 op1
= TREE_TYPE (op1
);
12350 ++cp_unevaluated_operand
;
12351 ++c_inhibit_evaluation_warnings
;
12352 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12353 /*function_p=*/false,
12354 /*integral_constant_expression_p=*/false);
12355 --cp_unevaluated_operand
;
12356 --c_inhibit_evaluation_warnings
;
12359 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
12360 complain
& tf_error
);
12362 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
12363 complain
& tf_error
);
12365 case AT_ENCODE_EXPR
:
12367 op1
= TREE_OPERAND (t
, 0);
12368 ++cp_unevaluated_operand
;
12369 ++c_inhibit_evaluation_warnings
;
12370 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12371 /*function_p=*/false,
12372 /*integral_constant_expression_p=*/false);
12373 --cp_unevaluated_operand
;
12374 --c_inhibit_evaluation_warnings
;
12375 return objc_build_encode_expr (op1
);
12378 case NOEXCEPT_EXPR
:
12379 op1
= TREE_OPERAND (t
, 0);
12380 ++cp_unevaluated_operand
;
12381 ++c_inhibit_evaluation_warnings
;
12382 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
12383 /*function_p=*/false,
12384 /*integral_constant_expression_p=*/false);
12385 --cp_unevaluated_operand
;
12386 --c_inhibit_evaluation_warnings
;
12387 return finish_noexcept_expr (op1
, complain
);
12391 tree r
= build_x_modify_expr
12392 (RECUR (TREE_OPERAND (t
, 0)),
12393 TREE_CODE (TREE_OPERAND (t
, 1)),
12394 RECUR (TREE_OPERAND (t
, 2)),
12396 /* TREE_NO_WARNING must be set if either the expression was
12397 parenthesized or it uses an operator such as >>= rather
12398 than plain assignment. In the former case, it was already
12399 set and must be copied. In the latter case,
12400 build_x_modify_expr sets it and it must not be reset
12402 if (TREE_NO_WARNING (t
))
12403 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12408 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12409 args
, complain
, in_decl
);
12410 /* Remember that there was a reference to this entity. */
12413 return build_x_arrow (op1
);
12417 tree placement
= RECUR (TREE_OPERAND (t
, 0));
12418 tree init
= RECUR (TREE_OPERAND (t
, 3));
12419 VEC(tree
,gc
) *placement_vec
;
12420 VEC(tree
,gc
) *init_vec
;
12423 if (placement
== NULL_TREE
)
12424 placement_vec
= NULL
;
12427 placement_vec
= make_tree_vector ();
12428 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
12429 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
12432 /* If there was an initializer in the original tree, but it
12433 instantiated to an empty list, then we should pass a
12434 non-NULL empty vector to tell build_new that it was an
12435 empty initializer() rather than no initializer. This can
12436 only happen when the initializer is a pack expansion whose
12437 parameter packs are of length zero. */
12438 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
12442 init_vec
= make_tree_vector ();
12443 if (init
== void_zero_node
)
12444 gcc_assert (init_vec
!= NULL
);
12447 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
12448 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
12452 ret
= build_new (&placement_vec
,
12453 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12454 RECUR (TREE_OPERAND (t
, 2)),
12456 NEW_EXPR_USE_GLOBAL (t
),
12459 if (placement_vec
!= NULL
)
12460 release_tree_vector (placement_vec
);
12461 if (init_vec
!= NULL
)
12462 release_tree_vector (init_vec
);
12468 return delete_sanity
12469 (RECUR (TREE_OPERAND (t
, 0)),
12470 RECUR (TREE_OPERAND (t
, 1)),
12471 DELETE_EXPR_USE_VEC (t
),
12472 DELETE_EXPR_USE_GLOBAL (t
));
12474 case COMPOUND_EXPR
:
12475 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
12476 RECUR (TREE_OPERAND (t
, 1)),
12482 VEC(tree
,gc
) *call_args
;
12483 unsigned int nargs
, i
;
12488 function
= CALL_EXPR_FN (t
);
12489 /* When we parsed the expression, we determined whether or
12490 not Koenig lookup should be performed. */
12491 koenig_p
= KOENIG_LOOKUP_P (t
);
12492 if (TREE_CODE (function
) == SCOPE_REF
)
12494 qualified_p
= true;
12495 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
12497 /*address_p=*/false);
12501 if (TREE_CODE (function
) == COMPONENT_REF
)
12503 tree op
= TREE_OPERAND (function
, 1);
12505 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
12506 || (BASELINK_P (op
)
12507 && BASELINK_QUALIFIED_P (op
)));
12510 qualified_p
= false;
12512 function
= tsubst_copy_and_build (function
, args
, complain
,
12515 integral_constant_expression_p
);
12517 if (BASELINK_P (function
))
12518 qualified_p
= true;
12521 nargs
= call_expr_nargs (t
);
12522 call_args
= make_tree_vector ();
12523 for (i
= 0; i
< nargs
; ++i
)
12525 tree arg
= CALL_EXPR_ARG (t
, i
);
12527 if (!PACK_EXPANSION_P (arg
))
12528 VEC_safe_push (tree
, gc
, call_args
,
12529 RECUR (CALL_EXPR_ARG (t
, i
)));
12532 /* Expand the pack expansion and push each entry onto
12534 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
12535 if (TREE_CODE (arg
) == TREE_VEC
)
12537 unsigned int len
, j
;
12539 len
= TREE_VEC_LENGTH (arg
);
12540 for (j
= 0; j
< len
; ++j
)
12542 tree value
= TREE_VEC_ELT (arg
, j
);
12543 if (value
!= NULL_TREE
)
12544 value
= convert_from_reference (value
);
12545 VEC_safe_push (tree
, gc
, call_args
, value
);
12550 /* A partial substitution. Add one entry. */
12551 VEC_safe_push (tree
, gc
, call_args
, arg
);
12556 /* We do not perform argument-dependent lookup if normal
12557 lookup finds a non-function, in accordance with the
12558 expected resolution of DR 218. */
12560 && ((is_overloaded_fn (function
)
12561 /* If lookup found a member function, the Koenig lookup is
12562 not appropriate, even if an unqualified-name was used
12563 to denote the function. */
12564 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
12565 || TREE_CODE (function
) == IDENTIFIER_NODE
)
12566 /* Only do this when substitution turns a dependent call
12567 into a non-dependent call. */
12568 && type_dependent_expression_p_push (t
)
12569 && !any_type_dependent_arguments_p (call_args
))
12570 function
= perform_koenig_lookup (function
, call_args
, false);
12572 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
12574 unqualified_name_lookup_error (function
);
12575 release_tree_vector (call_args
);
12576 return error_mark_node
;
12579 /* Remember that there was a reference to this entity. */
12580 if (DECL_P (function
))
12581 mark_used (function
);
12583 if (TREE_CODE (function
) == OFFSET_REF
)
12584 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
12585 else if (TREE_CODE (function
) == COMPONENT_REF
)
12587 tree instance
= TREE_OPERAND (function
, 0);
12588 tree fn
= TREE_OPERAND (function
, 1);
12590 if (processing_template_decl
12591 && (type_dependent_expression_p (instance
)
12592 || (!BASELINK_P (fn
)
12593 && TREE_CODE (fn
) != FIELD_DECL
)
12594 || type_dependent_expression_p (fn
)
12595 || any_type_dependent_arguments_p (call_args
)))
12596 ret
= build_nt_call_vec (function
, call_args
);
12597 else if (!BASELINK_P (fn
))
12598 ret
= finish_call_expr (function
, &call_args
,
12599 /*disallow_virtual=*/false,
12600 /*koenig_p=*/false,
12603 ret
= (build_new_method_call
12605 &call_args
, NULL_TREE
,
12606 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
12611 ret
= finish_call_expr (function
, &call_args
,
12612 /*disallow_virtual=*/qualified_p
,
12616 release_tree_vector (call_args
);
12622 return build_x_conditional_expr
12623 (RECUR (TREE_OPERAND (t
, 0)),
12624 RECUR (TREE_OPERAND (t
, 1)),
12625 RECUR (TREE_OPERAND (t
, 2)),
12628 case PSEUDO_DTOR_EXPR
:
12629 return finish_pseudo_destructor_expr
12630 (RECUR (TREE_OPERAND (t
, 0)),
12631 RECUR (TREE_OPERAND (t
, 1)),
12632 RECUR (TREE_OPERAND (t
, 2)));
12636 tree purpose
, value
, chain
;
12638 if (t
== void_list_node
)
12641 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
12642 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
12644 /* We have pack expansions, so expand those and
12645 create a new list out of it. */
12646 tree purposevec
= NULL_TREE
;
12647 tree valuevec
= NULL_TREE
;
12651 /* Expand the argument expressions. */
12652 if (TREE_PURPOSE (t
))
12653 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
12654 complain
, in_decl
);
12655 if (TREE_VALUE (t
))
12656 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
12657 complain
, in_decl
);
12659 /* Build the rest of the list. */
12660 chain
= TREE_CHAIN (t
);
12661 if (chain
&& chain
!= void_type_node
)
12662 chain
= RECUR (chain
);
12664 /* Determine the number of arguments. */
12665 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
12667 len
= TREE_VEC_LENGTH (purposevec
);
12668 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
12670 else if (TREE_CODE (valuevec
) == TREE_VEC
)
12671 len
= TREE_VEC_LENGTH (valuevec
);
12674 /* Since we only performed a partial substitution into
12675 the argument pack, we only return a single list
12677 if (purposevec
== TREE_PURPOSE (t
)
12678 && valuevec
== TREE_VALUE (t
)
12679 && chain
== TREE_CHAIN (t
))
12682 return tree_cons (purposevec
, valuevec
, chain
);
12685 /* Convert the argument vectors into a TREE_LIST */
12689 /* Grab the Ith values. */
12691 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
12694 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
12697 /* Build the list (backwards). */
12698 chain
= tree_cons (purpose
, value
, chain
);
12704 purpose
= TREE_PURPOSE (t
);
12706 purpose
= RECUR (purpose
);
12707 value
= TREE_VALUE (t
);
12709 value
= RECUR (value
);
12710 chain
= TREE_CHAIN (t
);
12711 if (chain
&& chain
!= void_type_node
)
12712 chain
= RECUR (chain
);
12713 if (purpose
== TREE_PURPOSE (t
)
12714 && value
== TREE_VALUE (t
)
12715 && chain
== TREE_CHAIN (t
))
12717 return tree_cons (purpose
, value
, chain
);
12720 case COMPONENT_REF
:
12726 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
12727 args
, complain
, in_decl
);
12728 /* Remember that there was a reference to this entity. */
12729 if (DECL_P (object
))
12730 mark_used (object
);
12731 object_type
= TREE_TYPE (object
);
12733 member
= TREE_OPERAND (t
, 1);
12734 if (BASELINK_P (member
))
12735 member
= tsubst_baselink (member
,
12736 non_reference (TREE_TYPE (object
)),
12737 args
, complain
, in_decl
);
12739 member
= tsubst_copy (member
, args
, complain
, in_decl
);
12740 if (member
== error_mark_node
)
12741 return error_mark_node
;
12743 if (object_type
&& !CLASS_TYPE_P (object_type
))
12745 if (SCALAR_TYPE_P (object_type
))
12747 tree s
= NULL_TREE
;
12748 tree dtor
= member
;
12750 if (TREE_CODE (dtor
) == SCOPE_REF
)
12752 s
= TREE_OPERAND (dtor
, 0);
12753 dtor
= TREE_OPERAND (dtor
, 1);
12755 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
12757 dtor
= TREE_OPERAND (dtor
, 0);
12759 return finish_pseudo_destructor_expr (object
, s
, dtor
);
12763 else if (TREE_CODE (member
) == SCOPE_REF
12764 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
12769 /* Lookup the template functions now that we know what the
12771 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
12772 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
12773 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
12774 /*is_type_p=*/false,
12775 /*complain=*/false);
12776 if (BASELINK_P (member
))
12778 BASELINK_FUNCTIONS (member
)
12779 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
12781 member
= (adjust_result_of_qualified_name_lookup
12782 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
12787 qualified_name_lookup_error (object_type
, tmpl
, member
,
12789 return error_mark_node
;
12792 else if (TREE_CODE (member
) == SCOPE_REF
12793 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
12794 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
12796 if (complain
& tf_error
)
12798 if (TYPE_P (TREE_OPERAND (member
, 0)))
12799 error ("%qT is not a class or namespace",
12800 TREE_OPERAND (member
, 0));
12802 error ("%qD is not a class or namespace",
12803 TREE_OPERAND (member
, 0));
12805 return error_mark_node
;
12807 else if (TREE_CODE (member
) == FIELD_DECL
)
12808 return finish_non_static_data_member (member
, object
, NULL_TREE
);
12810 return finish_class_member_access_expr (object
, member
,
12811 /*template_p=*/false,
12817 (RECUR (TREE_OPERAND (t
, 0)));
12821 VEC(constructor_elt
,gc
) *n
;
12822 constructor_elt
*ce
;
12823 unsigned HOST_WIDE_INT idx
;
12824 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12825 bool process_index_p
;
12827 bool need_copy_p
= false;
12830 if (type
== error_mark_node
)
12831 return error_mark_node
;
12833 /* digest_init will do the wrong thing if we let it. */
12834 if (type
&& TYPE_PTRMEMFUNC_P (type
))
12837 /* We do not want to process the index of aggregate
12838 initializers as they are identifier nodes which will be
12839 looked up by digest_init. */
12840 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
12842 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
12843 newlen
= VEC_length (constructor_elt
, n
);
12844 FOR_EACH_VEC_ELT (constructor_elt
, n
, idx
, ce
)
12846 if (ce
->index
&& process_index_p
)
12847 ce
->index
= RECUR (ce
->index
);
12849 if (PACK_EXPANSION_P (ce
->value
))
12851 /* Substitute into the pack expansion. */
12852 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
12855 if (ce
->value
== error_mark_node
)
12857 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
12858 /* Just move the argument into place. */
12859 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
12862 /* Update the length of the final CONSTRUCTOR
12863 arguments vector, and note that we will need to
12865 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
12866 need_copy_p
= true;
12870 ce
->value
= RECUR (ce
->value
);
12875 VEC(constructor_elt
,gc
) *old_n
= n
;
12877 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
12878 FOR_EACH_VEC_ELT (constructor_elt
, old_n
, idx
, ce
)
12880 if (TREE_CODE (ce
->value
) == TREE_VEC
)
12882 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
12883 for (i
= 0; i
< len
; ++i
)
12884 CONSTRUCTOR_APPEND_ELT (n
, 0,
12885 TREE_VEC_ELT (ce
->value
, i
));
12888 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
12892 r
= build_constructor (init_list_type_node
, n
);
12893 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
12895 if (TREE_HAS_CONSTRUCTOR (t
))
12896 return finish_compound_literal (type
, r
);
12903 tree operand_0
= TREE_OPERAND (t
, 0);
12904 if (TYPE_P (operand_0
))
12906 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
12907 return get_typeid (operand_0
);
12911 operand_0
= RECUR (operand_0
);
12912 return build_typeid (operand_0
);
12923 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
12925 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
12926 /* If the original type was a reference, we'll be wrapped in
12927 the appropriate INDIRECT_REF. */
12928 r
= convert_from_reference (r
);
12933 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
12934 tsubst_copy (TREE_TYPE (t
), args
, complain
,
12937 case OFFSETOF_EXPR
:
12938 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
12942 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
12943 complain
, in_decl
);
12945 tree type2
= TRAIT_EXPR_TYPE2 (t
);
12947 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
12949 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
12954 tree old_stmt_expr
= cur_stmt_expr
;
12955 tree stmt_expr
= begin_stmt_expr ();
12957 cur_stmt_expr
= stmt_expr
;
12958 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
12959 integral_constant_expression_p
);
12960 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
12961 cur_stmt_expr
= old_stmt_expr
;
12963 /* If the resulting list of expression statement is empty,
12964 fold it further into void_zero_node. */
12965 if (empty_expr_stmt_p (stmt_expr
))
12966 stmt_expr
= void_zero_node
;
12972 t
= tsubst_copy (t
, args
, complain
, in_decl
);
12973 /* As in finish_id_expression, we resolve enumeration constants
12974 to their underlying values. */
12975 if (TREE_CODE (t
) == CONST_DECL
)
12977 used_types_insert (TREE_TYPE (t
));
12978 return DECL_INITIAL (t
);
12984 tree r
= build_lambda_expr ();
12986 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
12987 TREE_TYPE (r
) = type
;
12988 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
12990 LAMBDA_EXPR_LOCATION (r
)
12991 = LAMBDA_EXPR_LOCATION (t
);
12992 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
12993 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
12994 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
12995 LAMBDA_EXPR_DISCRIMINATOR (r
)
12996 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
12997 LAMBDA_EXPR_CAPTURE_LIST (r
)
12998 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
12999 LAMBDA_EXPR_THIS_CAPTURE (r
)
13000 = RECUR (LAMBDA_EXPR_THIS_CAPTURE (t
));
13001 LAMBDA_EXPR_EXTRA_SCOPE (r
)
13002 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t
));
13004 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
13005 determine_visibility (TYPE_NAME (type
));
13006 /* Now that we know visibility, instantiate the type so we have a
13007 declaration of the op() for later calls to lambda_function. */
13008 complete_type (type
);
13010 type
= tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
13012 apply_lambda_return_type (r
, type
);
13014 return build_lambda_object (r
);
13018 /* Handle Objective-C++ constructs, if appropriate. */
13021 = objcp_tsubst_copy_and_build (t
, args
, complain
,
13022 in_decl
, /*function_p=*/false);
13026 return tsubst_copy (t
, args
, complain
, in_decl
);
13032 /* Verify that the instantiated ARGS are valid. For type arguments,
13033 make sure that the type's linkage is ok. For non-type arguments,
13034 make sure they are constants if they are integral or enumerations.
13035 Emit an error under control of COMPLAIN, and return TRUE on error. */
13038 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
13040 if (ARGUMENT_PACK_P (t
))
13042 tree vec
= ARGUMENT_PACK_ARGS (t
);
13043 int len
= TREE_VEC_LENGTH (vec
);
13044 bool result
= false;
13047 for (i
= 0; i
< len
; ++i
)
13048 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
13052 else if (TYPE_P (t
))
13054 /* [basic.link]: A name with no linkage (notably, the name
13055 of a class or enumeration declared in a local scope)
13056 shall not be used to declare an entity with linkage.
13057 This implies that names with no linkage cannot be used as
13060 DR 757 relaxes this restriction for C++0x. */
13061 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
13062 : no_linkage_check (t
, /*relaxed_p=*/false));
13066 /* DR 488 makes use of a type with no linkage cause
13067 type deduction to fail. */
13068 if (complain
& tf_error
)
13070 if (TYPE_ANONYMOUS_P (nt
))
13071 error ("%qT is/uses anonymous type", t
);
13073 error ("template argument for %qD uses local type %qT",
13078 /* In order to avoid all sorts of complications, we do not
13079 allow variably-modified types as template arguments. */
13080 else if (variably_modified_type_p (t
, NULL_TREE
))
13082 if (complain
& tf_error
)
13083 error ("%qT is a variably modified type", t
);
13087 /* A non-type argument of integral or enumerated type must be a
13089 else if (TREE_TYPE (t
)
13090 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
13091 && !TREE_CONSTANT (t
))
13093 if (complain
& tf_error
)
13094 error ("integral expression %qE is not constant", t
);
13101 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
13103 int ix
, len
= DECL_NTPARMS (tmpl
);
13104 bool result
= false;
13106 for (ix
= 0; ix
!= len
; ix
++)
13108 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
13111 if (result
&& (complain
& tf_error
))
13112 error (" trying to instantiate %qD", tmpl
);
13116 /* Instantiate the indicated variable or function template TMPL with
13117 the template arguments in TARG_PTR. */
13120 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
13122 tree targ_ptr
= orig_args
;
13126 HOST_WIDE_INT saved_processing_template_decl
;
13128 if (tmpl
== error_mark_node
)
13129 return error_mark_node
;
13131 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
13133 /* If this function is a clone, handle it specially. */
13134 if (DECL_CLONED_FUNCTION_P (tmpl
))
13139 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
13140 DECL_CLONED_FUNCTION. */
13141 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
13142 targ_ptr
, complain
);
13143 if (spec
== error_mark_node
)
13144 return error_mark_node
;
13146 /* Look for the clone. */
13147 FOR_EACH_CLONE (clone
, spec
)
13148 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
13150 /* We should always have found the clone by now. */
13151 gcc_unreachable ();
13155 /* Check to see if we already have this specialization. */
13156 gen_tmpl
= most_general_template (tmpl
);
13157 if (tmpl
!= gen_tmpl
)
13158 /* The TMPL is a partial instantiation. To get a full set of
13159 arguments we must add the arguments used to perform the
13160 partial instantiation. */
13161 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
13164 /* It would be nice to avoid hashing here and then again in tsubst_decl,
13165 but it doesn't seem to be on the hot path. */
13166 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
13168 gcc_assert (tmpl
== gen_tmpl
13169 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
13171 || fndecl
== NULL_TREE
);
13173 if (spec
!= NULL_TREE
)
13176 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
13178 return error_mark_node
;
13180 /* We are building a FUNCTION_DECL, during which the access of its
13181 parameters and return types have to be checked. However this
13182 FUNCTION_DECL which is the desired context for access checking
13183 is not built yet. We solve this chicken-and-egg problem by
13184 deferring all checks until we have the FUNCTION_DECL. */
13185 push_deferring_access_checks (dk_deferred
);
13187 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
13188 (because, for example, we have encountered a non-dependent
13189 function call in the body of a template function and must now
13190 determine which of several overloaded functions will be called),
13191 within the instantiation itself we are not processing a
13193 saved_processing_template_decl
= processing_template_decl
;
13194 processing_template_decl
= 0;
13195 /* Substitute template parameters to obtain the specialization. */
13196 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
13197 targ_ptr
, complain
, gen_tmpl
);
13198 processing_template_decl
= saved_processing_template_decl
;
13199 if (fndecl
== error_mark_node
)
13200 return error_mark_node
;
13202 /* Now we know the specialization, compute access previously
13204 push_access_scope (fndecl
);
13206 /* Some typedefs referenced from within the template code need to be access
13207 checked at template instantiation time, i.e now. These types were
13208 added to the template at parsing time. Let's get those and perfom
13209 the acces checks then. */
13210 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl
), targ_ptr
);
13211 perform_deferred_access_checks ();
13212 pop_access_scope (fndecl
);
13213 pop_deferring_access_checks ();
13215 /* The DECL_TI_TEMPLATE should always be the immediate parent
13216 template, not the most general template. */
13217 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
13219 /* If we've just instantiated the main entry point for a function,
13220 instantiate all the alternate entry points as well. We do this
13221 by cloning the instantiation of the main entry point, not by
13222 instantiating the template clones. */
13223 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
13224 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
13229 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
13230 NARGS elements of the arguments that are being used when calling
13231 it. TARGS is a vector into which the deduced template arguments
13234 Return zero for success, 2 for an incomplete match that doesn't resolve
13235 all the types, and 1 for complete failure. An error message will be
13236 printed only for an incomplete match.
13238 If FN is a conversion operator, or we are trying to produce a specific
13239 specialization, RETURN_TYPE is the return type desired.
13241 The EXPLICIT_TARGS are explicit template arguments provided via a
13244 The parameter STRICT is one of:
13247 We are deducing arguments for a function call, as in
13248 [temp.deduct.call].
13251 We are deducing arguments for a conversion function, as in
13252 [temp.deduct.conv].
13255 We are deducing arguments when doing an explicit instantiation
13256 as in [temp.explicit], when determining an explicit specialization
13257 as in [temp.expl.spec], or when taking the address of a function
13258 template, as in [temp.deduct.funcaddr]. */
13261 fn_type_unification (tree fn
,
13262 tree explicit_targs
,
13265 unsigned int nargs
,
13267 unification_kind_t strict
,
13273 bool incomplete_argument_packs_p
= false;
13275 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
13277 fntype
= TREE_TYPE (fn
);
13278 if (explicit_targs
)
13282 The specified template arguments must match the template
13283 parameters in kind (i.e., type, nontype, template), and there
13284 must not be more arguments than there are parameters;
13285 otherwise type deduction fails.
13287 Nontype arguments must match the types of the corresponding
13288 nontype template parameters, or must be convertible to the
13289 types of the corresponding nontype parameters as specified in
13290 _temp.arg.nontype_, otherwise type deduction fails.
13292 All references in the function type of the function template
13293 to the corresponding template parameters are replaced by the
13294 specified template argument values. If a substitution in a
13295 template parameter or in the function type of the function
13296 template results in an invalid type, type deduction fails. */
13297 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
13298 int i
, len
= TREE_VEC_LENGTH (tparms
);
13299 tree converted_args
;
13300 bool incomplete
= false;
13302 if (explicit_targs
== error_mark_node
)
13306 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
, tf_none
,
13307 /*require_all_args=*/false,
13308 /*use_default_args=*/false));
13309 if (converted_args
== error_mark_node
)
13312 /* Substitute the explicit args into the function type. This is
13313 necessary so that, for instance, explicitly declared function
13314 arguments can match null pointed constants. If we were given
13315 an incomplete set of explicit args, we must not do semantic
13316 processing during substitution as we could create partial
13318 for (i
= 0; i
< len
; i
++)
13320 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13321 bool parameter_pack
= false;
13323 /* Dig out the actual parm. */
13324 if (TREE_CODE (parm
) == TYPE_DECL
13325 || TREE_CODE (parm
) == TEMPLATE_DECL
)
13327 parm
= TREE_TYPE (parm
);
13328 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
13330 else if (TREE_CODE (parm
) == PARM_DECL
)
13332 parm
= DECL_INITIAL (parm
);
13333 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
13336 if (parameter_pack
)
13340 template_parm_level_and_index (parm
, &level
, &idx
);
13342 /* Mark the argument pack as "incomplete". We could
13343 still deduce more arguments during unification. */
13344 targ
= TMPL_ARG (converted_args
, level
, idx
);
13347 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
13348 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
13349 = ARGUMENT_PACK_ARGS (targ
);
13352 /* We have some incomplete argument packs. */
13353 incomplete_argument_packs_p
= true;
13357 if (incomplete_argument_packs_p
)
13358 /* Any substitution is guaranteed to be incomplete if there
13359 are incomplete argument packs, because we can still deduce
13363 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
13365 processing_template_decl
+= incomplete
;
13366 fntype
= tsubst (fntype
, converted_args
, tf_none
, NULL_TREE
);
13367 processing_template_decl
-= incomplete
;
13369 if (fntype
== error_mark_node
)
13372 /* Place the explicitly specified arguments in TARGS. */
13373 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
13374 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
13377 /* Never do unification on the 'this' parameter. */
13378 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
13384 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
13385 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
13386 new_args
[0] = return_type
;
13387 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
13392 /* We allow incomplete unification without an error message here
13393 because the standard doesn't seem to explicitly prohibit it. Our
13394 callers must be ready to deal with unification failures in any
13396 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
13397 targs
, parms
, args
, nargs
, /*subr=*/0,
13400 if (result
== 0 && incomplete_argument_packs_p
)
13402 int i
, len
= NUM_TMPL_ARGS (targs
);
13404 /* Clear the "incomplete" flags on all argument packs. */
13405 for (i
= 0; i
< len
; i
++)
13407 tree arg
= TREE_VEC_ELT (targs
, i
);
13408 if (ARGUMENT_PACK_P (arg
))
13410 ARGUMENT_PACK_INCOMPLETE_P (arg
) = 0;
13411 ARGUMENT_PACK_EXPLICIT_ARGS (arg
) = NULL_TREE
;
13416 /* Now that we have bindings for all of the template arguments,
13417 ensure that the arguments deduced for the template template
13418 parameters have compatible template parameter lists. We cannot
13419 check this property before we have deduced all template
13420 arguments, because the template parameter types of a template
13421 template parameter might depend on prior template parameters
13422 deduced after the template template parameter. The following
13423 ill-formed example illustrates this issue:
13425 template<typename T, template<T> class C> void f(C<5>, T);
13427 template<int N> struct X {};
13430 f(X<5>(), 5l); // error: template argument deduction fails
13433 The template parameter list of 'C' depends on the template type
13434 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
13435 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
13436 time that we deduce 'C'. */
13438 && !template_template_parm_bindings_ok_p
13439 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
13443 /* All is well so far. Now, check:
13447 When all template arguments have been deduced, all uses of
13448 template parameters in nondeduced contexts are replaced with
13449 the corresponding deduced argument values. If the
13450 substitution results in an invalid type, as described above,
13451 type deduction fails. */
13453 tree substed
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
13454 if (substed
== error_mark_node
)
13457 /* If we're looking for an exact match, check that what we got
13458 is indeed an exact match. It might not be if some template
13459 parameters are used in non-deduced contexts. */
13460 if (strict
== DEDUCE_EXACT
)
13465 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
13467 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
13468 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
13469 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
13477 /* Adjust types before performing type deduction, as described in
13478 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
13479 sections are symmetric. PARM is the type of a function parameter
13480 or the return type of the conversion function. ARG is the type of
13481 the argument passed to the call, or the type of the value
13482 initialized with the result of the conversion function.
13483 ARG_EXPR is the original argument expression, which may be null. */
13486 maybe_adjust_types_for_deduction (unification_kind_t strict
,
13500 /* Swap PARM and ARG throughout the remainder of this
13501 function; the handling is precisely symmetric since PARM
13502 will initialize ARG rather than vice versa. */
13510 /* Core issue #873: Do the DR606 thing (see below) for these cases,
13511 too, but here handle it by stripping the reference from PARM
13512 rather than by adding it to ARG. */
13513 if (TREE_CODE (*parm
) == REFERENCE_TYPE
13514 && TYPE_REF_IS_RVALUE (*parm
)
13515 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
13516 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
13517 && TREE_CODE (*arg
) == REFERENCE_TYPE
13518 && !TYPE_REF_IS_RVALUE (*arg
))
13519 *parm
= TREE_TYPE (*parm
);
13520 /* Nothing else to do in this case. */
13524 gcc_unreachable ();
13527 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
13529 /* [temp.deduct.call]
13531 If P is not a reference type:
13533 --If A is an array type, the pointer type produced by the
13534 array-to-pointer standard conversion (_conv.array_) is
13535 used in place of A for type deduction; otherwise,
13537 --If A is a function type, the pointer type produced by
13538 the function-to-pointer standard conversion
13539 (_conv.func_) is used in place of A for type deduction;
13542 --If A is a cv-qualified type, the top level
13543 cv-qualifiers of A's type are ignored for type
13545 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
13546 *arg
= build_pointer_type (TREE_TYPE (*arg
));
13547 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
13548 *arg
= build_pointer_type (*arg
);
13550 *arg
= TYPE_MAIN_VARIANT (*arg
);
13553 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
13554 of the form T&&, where T is a template parameter, and the argument
13555 is an lvalue, T is deduced as A& */
13556 if (TREE_CODE (*parm
) == REFERENCE_TYPE
13557 && TYPE_REF_IS_RVALUE (*parm
)
13558 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
13559 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
13560 && arg_expr
&& real_lvalue_p (arg_expr
))
13561 *arg
= build_reference_type (*arg
);
13563 /* [temp.deduct.call]
13565 If P is a cv-qualified type, the top level cv-qualifiers
13566 of P's type are ignored for type deduction. If P is a
13567 reference type, the type referred to by P is used for
13569 *parm
= TYPE_MAIN_VARIANT (*parm
);
13570 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
13572 *parm
= TREE_TYPE (*parm
);
13573 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
13576 /* DR 322. For conversion deduction, remove a reference type on parm
13577 too (which has been swapped into ARG). */
13578 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
13579 *arg
= TREE_TYPE (*arg
);
13584 /* Most parms like fn_type_unification.
13586 If SUBR is 1, we're being called recursively (to unify the
13587 arguments of a function or method parameter of a function
13591 type_unification_real (tree tparms
,
13595 unsigned int xnargs
,
13597 unification_kind_t strict
,
13600 tree parm
, arg
, arg_expr
;
13602 int ntparms
= TREE_VEC_LENGTH (tparms
);
13604 int saw_undeduced
= 0;
13607 unsigned int nargs
;
13610 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
13611 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
13612 gcc_assert (ntparms
> 0);
13614 /* Reset the number of non-defaulted template arguments contained
13616 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
13621 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
13622 | UNIFY_ALLOW_DERIVED
);
13626 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
13630 sub_strict
= UNIFY_ALLOW_NONE
;
13634 gcc_unreachable ();
13643 while (parms
&& parms
!= void_list_node
13646 if (TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
13649 parm
= TREE_VALUE (parms
);
13650 parms
= TREE_CHAIN (parms
);
13655 if (arg
== error_mark_node
)
13657 if (arg
== unknown_type_node
)
13658 /* We can't deduce anything from this, but we might get all the
13659 template args from other function args. */
13662 /* Conversions will be performed on a function argument that
13663 corresponds with a function parameter that contains only
13664 non-deducible template parameters and explicitly specified
13665 template parameters. */
13666 if (!uses_template_parms (parm
))
13671 type
= TREE_TYPE (arg
);
13675 if (same_type_p (parm
, type
))
13677 if (strict
!= DEDUCE_EXACT
13678 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
13687 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
13688 if (type_unknown_p (arg
))
13690 /* [temp.deduct.type]
13692 A template-argument can be deduced from a pointer to
13693 function or pointer to member function argument if
13694 the set of overloaded functions does not contain
13695 function templates and at most one of a set of
13696 overloaded functions provides a unique match. */
13697 if (resolve_overloaded_unification
13698 (tparms
, targs
, parm
, arg
, strict
, sub_strict
))
13704 arg
= unlowered_expr_type (arg
);
13705 if (arg
== error_mark_node
)
13710 int arg_strict
= sub_strict
;
13713 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
13716 if (arg
== init_list_type_node
&& arg_expr
)
13718 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
13725 && parms
!= void_list_node
13726 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
13728 /* Unify the remaining arguments with the pack expansion type. */
13730 tree parmvec
= make_tree_vec (1);
13732 /* Allocate a TREE_VEC and copy in all of the arguments */
13733 argvec
= make_tree_vec (nargs
- ia
);
13734 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
13735 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
13737 /* Copy the parameter into parmvec. */
13738 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
13739 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
13740 /*call_args_p=*/true, /*subr=*/subr
))
13743 /* Advance to the end of the list of parameters. */
13744 parms
= TREE_CHAIN (parms
);
13747 /* Fail if we've reached the end of the parm list, and more args
13748 are present, and the parm list isn't variadic. */
13749 if (ia
< nargs
&& parms
== void_list_node
)
13751 /* Fail if parms are left and they don't have default values. */
13752 if (parms
&& parms
!= void_list_node
13753 && TREE_PURPOSE (parms
) == NULL_TREE
)
13757 for (i
= 0; i
< ntparms
; i
++)
13758 if (!TREE_VEC_ELT (targs
, i
))
13762 if (TREE_VEC_ELT (tparms
, i
) == error_mark_node
)
13765 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13767 /* If this is an undeduced nontype parameter that depends on
13768 a type parameter, try another pass; its type may have been
13769 deduced from a later argument than the one from which
13770 this parameter can be deduced. */
13771 if (TREE_CODE (tparm
) == PARM_DECL
13772 && uses_template_parms (TREE_TYPE (tparm
))
13773 && !saw_undeduced
++)
13776 /* Core issue #226 (C++0x) [temp.deduct]:
13778 If a template argument has not been deduced, its
13779 default template argument, if any, is used.
13781 When we are in C++98 mode, TREE_PURPOSE will either
13782 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13783 to explicitly check cxx_dialect here. */
13784 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
13786 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
13787 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
13788 arg
= tsubst_template_arg (arg
, targs
, tf_none
, NULL_TREE
);
13789 arg
= convert_template_argument (parm
, arg
, targs
, tf_none
,
13791 if (arg
== error_mark_node
)
13795 TREE_VEC_ELT (targs
, i
) = arg
;
13796 /* The position of the first default template argument,
13797 is also the number of non-defaulted arguments in TARGS.
13799 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
13800 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
13805 /* If the type parameter is a parameter pack, then it will
13806 be deduced to an empty parameter pack. */
13807 if (template_parameter_pack_p (tparm
))
13811 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
13813 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
13814 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
13815 TREE_CONSTANT (arg
) = 1;
13818 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
13820 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
13822 TREE_VEC_ELT (targs
, i
) = arg
;
13828 #ifdef ENABLE_CHECKING
13829 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
13830 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
13836 /* Subroutine of type_unification_real. Args are like the variables
13837 at the call site. ARG is an overloaded function (or template-id);
13838 we try deducing template args from each of the overloads, and if
13839 only one succeeds, we go with that. Modifies TARGS and returns
13840 true on success. */
13843 resolve_overloaded_unification (tree tparms
,
13847 unification_kind_t strict
,
13850 tree tempargs
= copy_node (targs
);
13852 tree goodfn
= NULL_TREE
;
13855 if (TREE_CODE (arg
) == ADDR_EXPR
)
13857 arg
= TREE_OPERAND (arg
, 0);
13863 if (TREE_CODE (arg
) == COMPONENT_REF
)
13864 /* Handle `&x' where `x' is some static or non-static member
13866 arg
= TREE_OPERAND (arg
, 1);
13868 if (TREE_CODE (arg
) == OFFSET_REF
)
13869 arg
= TREE_OPERAND (arg
, 1);
13871 /* Strip baselink information. */
13872 if (BASELINK_P (arg
))
13873 arg
= BASELINK_FUNCTIONS (arg
);
13875 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
13877 /* If we got some explicit template args, we need to plug them into
13878 the affected templates before we try to unify, in case the
13879 explicit args will completely resolve the templates in question. */
13881 tree expl_subargs
= TREE_OPERAND (arg
, 1);
13882 arg
= TREE_OPERAND (arg
, 0);
13884 for (; arg
; arg
= OVL_NEXT (arg
))
13886 tree fn
= OVL_CURRENT (arg
);
13887 tree subargs
, elem
;
13889 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
13892 ++processing_template_decl
;
13893 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
13894 expl_subargs
, /*check_ret=*/false);
13897 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
13898 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
13899 elem
, strict
, sub_strict
, addr_p
)
13900 && (!goodfn
|| !decls_match (goodfn
, elem
)))
13906 --processing_template_decl
;
13909 else if (TREE_CODE (arg
) != OVERLOAD
13910 && TREE_CODE (arg
) != FUNCTION_DECL
)
13911 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13912 -- but the deduction does not succeed because the expression is
13913 not just the function on its own. */
13916 for (; arg
; arg
= OVL_NEXT (arg
))
13917 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
13918 TREE_TYPE (OVL_CURRENT (arg
)),
13919 strict
, sub_strict
, addr_p
)
13920 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
13922 goodfn
= OVL_CURRENT (arg
);
13926 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13927 to function or pointer to member function argument if the set of
13928 overloaded functions does not contain function templates and at most
13929 one of a set of overloaded functions provides a unique match.
13931 So if we found multiple possibilities, we return success but don't
13932 deduce anything. */
13936 int i
= TREE_VEC_LENGTH (targs
);
13938 if (TREE_VEC_ELT (tempargs
, i
))
13939 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
13947 /* Core DR 115: In contexts where deduction is done and fails, or in
13948 contexts where deduction is not done, if a template argument list is
13949 specified and it, along with any default template arguments, identifies
13950 a single function template specialization, then the template-id is an
13951 lvalue for the function template specialization. */
13954 resolve_nondeduced_context (tree orig_expr
)
13956 tree expr
, offset
, baselink
;
13959 if (!type_unknown_p (orig_expr
))
13964 offset
= NULL_TREE
;
13965 baselink
= NULL_TREE
;
13967 if (TREE_CODE (expr
) == ADDR_EXPR
)
13969 expr
= TREE_OPERAND (expr
, 0);
13972 if (TREE_CODE (expr
) == OFFSET_REF
)
13975 expr
= TREE_OPERAND (expr
, 1);
13977 if (TREE_CODE (expr
) == BASELINK
)
13980 expr
= BASELINK_FUNCTIONS (expr
);
13983 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
13986 tree goodfn
= NULL_TREE
;
13988 /* If we got some explicit template args, we need to plug them into
13989 the affected templates before we try to unify, in case the
13990 explicit args will completely resolve the templates in question. */
13992 tree expl_subargs
= TREE_OPERAND (expr
, 1);
13993 tree arg
= TREE_OPERAND (expr
, 0);
13994 tree badfn
= NULL_TREE
;
13995 tree badargs
= NULL_TREE
;
13997 for (; arg
; arg
= OVL_NEXT (arg
))
13999 tree fn
= OVL_CURRENT (arg
);
14000 tree subargs
, elem
;
14002 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
14005 ++processing_template_decl
;
14006 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
14007 expl_subargs
, /*check_ret=*/false);
14008 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
14010 elem
= instantiate_template (fn
, subargs
, tf_none
);
14011 if (elem
== error_mark_node
)
14016 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
14022 --processing_template_decl
;
14028 expr
= build_baselink (BASELINK_BINFO (baselink
),
14029 BASELINK_ACCESS_BINFO (baselink
),
14030 expr
, BASELINK_OPTYPE (baselink
));
14032 expr
= build2 (OFFSET_REF
, TREE_TYPE (expr
),
14033 TREE_OPERAND (offset
, 0), expr
);
14035 expr
= build_address (expr
);
14038 else if (good
== 0 && badargs
)
14039 /* There were no good options and at least one bad one, so let the
14040 user know what the problem is. */
14041 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
14046 /* Subroutine of resolve_overloaded_unification; does deduction for a single
14047 overload. Fills TARGS with any deduced arguments, or error_mark_node if
14048 different overloads deduce different arguments for a given parm.
14049 ADDR_P is true if the expression for which deduction is being
14050 performed was of the form "& fn" rather than simply "fn".
14052 Returns 1 on success. */
14055 try_one_overload (tree tparms
,
14060 unification_kind_t strict
,
14068 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14069 to function or pointer to member function argument if the set of
14070 overloaded functions does not contain function templates and at most
14071 one of a set of overloaded functions provides a unique match.
14073 So if this is a template, just return success. */
14075 if (uses_template_parms (arg
))
14078 if (TREE_CODE (arg
) == METHOD_TYPE
)
14079 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
14081 arg
= build_pointer_type (arg
);
14083 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
14085 /* We don't copy orig_targs for this because if we have already deduced
14086 some template args from previous args, unify would complain when we
14087 try to deduce a template parameter for the same argument, even though
14088 there isn't really a conflict. */
14089 nargs
= TREE_VEC_LENGTH (targs
);
14090 tempargs
= make_tree_vec (nargs
);
14092 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
) != 0)
14095 /* First make sure we didn't deduce anything that conflicts with
14096 explicitly specified args. */
14097 for (i
= nargs
; i
--; )
14099 tree elt
= TREE_VEC_ELT (tempargs
, i
);
14100 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
14104 else if (uses_template_parms (elt
))
14105 /* Since we're unifying against ourselves, we will fill in
14106 template args used in the function parm list with our own
14107 template parms. Discard them. */
14108 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
14109 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
14113 for (i
= nargs
; i
--; )
14115 tree elt
= TREE_VEC_ELT (tempargs
, i
);
14118 TREE_VEC_ELT (targs
, i
) = elt
;
14124 /* PARM is a template class (perhaps with unbound template
14125 parameters). ARG is a fully instantiated type. If ARG can be
14126 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
14127 TARGS are as for unify. */
14130 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
)
14132 tree copy_of_targs
;
14134 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
14135 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
14136 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
14139 /* We need to make a new template argument vector for the call to
14140 unify. If we used TARGS, we'd clutter it up with the result of
14141 the attempted unification, even if this class didn't work out.
14142 We also don't want to commit ourselves to all the unifications
14143 we've already done, since unification is supposed to be done on
14144 an argument-by-argument basis. In other words, consider the
14145 following pathological case:
14147 template <int I, int J, int K>
14150 template <int I, int J>
14151 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
14153 template <int I, int J, int K>
14154 void f(S<I, J, K>, S<I, I, I>);
14163 Now, by the time we consider the unification involving `s2', we
14164 already know that we must have `f<0, 0, 0>'. But, even though
14165 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
14166 because there are two ways to unify base classes of S<0, 1, 2>
14167 with S<I, I, I>. If we kept the already deduced knowledge, we
14168 would reject the possibility I=1. */
14169 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
14171 /* If unification failed, we're done. */
14172 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
14173 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
))
14179 /* Given a template type PARM and a class type ARG, find the unique
14180 base type in ARG that is an instance of PARM. We do not examine
14181 ARG itself; only its base-classes. If there is not exactly one
14182 appropriate base class, return NULL_TREE. PARM may be the type of
14183 a partial specialization, as well as a plain template type. Used
14187 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
)
14189 tree rval
= NULL_TREE
;
14192 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
14194 binfo
= TYPE_BINFO (complete_type (arg
));
14196 /* The type could not be completed. */
14199 /* Walk in inheritance graph order. The search order is not
14200 important, and this avoids multiple walks of virtual bases. */
14201 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
14203 tree r
= try_class_unification (tparms
, targs
, parm
, BINFO_TYPE (binfo
));
14207 /* If there is more than one satisfactory baseclass, then:
14211 If they yield more than one possible deduced A, the type
14215 if (rval
&& !same_type_p (r
, rval
))
14225 /* Returns the level of DECL, which declares a template parameter. */
14228 template_decl_level (tree decl
)
14230 switch (TREE_CODE (decl
))
14233 case TEMPLATE_DECL
:
14234 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
14237 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
14240 gcc_unreachable ();
14245 /* Decide whether ARG can be unified with PARM, considering only the
14246 cv-qualifiers of each type, given STRICT as documented for unify.
14247 Returns nonzero iff the unification is OK on that basis. */
14250 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
14252 int arg_quals
= cp_type_quals (arg
);
14253 int parm_quals
= cp_type_quals (parm
);
14255 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14256 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
14258 /* Although a CVR qualifier is ignored when being applied to a
14259 substituted template parameter ([8.3.2]/1 for example), that
14260 does not allow us to unify "const T" with "int&" because both
14261 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
14262 It is ok when we're allowing additional CV qualifiers
14263 at the outer level [14.8.2.1]/3,1st bullet. */
14264 if ((TREE_CODE (arg
) == REFERENCE_TYPE
14265 || TREE_CODE (arg
) == FUNCTION_TYPE
14266 || TREE_CODE (arg
) == METHOD_TYPE
)
14267 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
14270 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
14271 && (parm_quals
& TYPE_QUAL_RESTRICT
))
14275 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
14276 && (arg_quals
& parm_quals
) != parm_quals
)
14279 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
14280 && (parm_quals
& arg_quals
) != arg_quals
)
14286 /* Determines the LEVEL and INDEX for the template parameter PARM. */
14288 template_parm_level_and_index (tree parm
, int* level
, int* index
)
14290 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14291 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14292 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14294 *index
= TEMPLATE_TYPE_IDX (parm
);
14295 *level
= TEMPLATE_TYPE_LEVEL (parm
);
14299 *index
= TEMPLATE_PARM_IDX (parm
);
14300 *level
= TEMPLATE_PARM_LEVEL (parm
);
14304 /* Unifies the remaining arguments in PACKED_ARGS with the pack
14305 expansion at the end of PACKED_PARMS. Returns 0 if the type
14306 deduction succeeds, 1 otherwise. STRICT is the same as in
14307 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
14308 call argument list. We'll need to adjust the arguments to make them
14309 types. SUBR tells us if this is from a recursive call to
14310 type_unification_real. */
14312 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
14313 tree packed_args
, int strict
, bool call_args_p
,
14317 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
14318 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
14319 tree pack
, packs
= NULL_TREE
;
14320 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
14321 int len
= TREE_VEC_LENGTH (packed_args
);
14323 /* Determine the parameter packs we will be deducing from the
14324 pattern, and record their current deductions. */
14325 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
14326 pack
; pack
= TREE_CHAIN (pack
))
14328 tree parm_pack
= TREE_VALUE (pack
);
14331 /* Determine the index and level of this parameter pack. */
14332 template_parm_level_and_index (parm_pack
, &level
, &idx
);
14334 /* Keep track of the parameter packs and their corresponding
14336 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
14337 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
14340 /* Loop through all of the arguments that have not yet been
14341 unified and unify each with the pattern. */
14342 for (i
= start
; i
< len
; i
++)
14344 tree parm
= pattern
;
14346 /* For each parameter pack, clear out the deduced value so that
14347 we can deduce it again. */
14348 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14351 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14353 TMPL_ARG (targs
, level
, idx
) = NULL_TREE
;
14356 /* Unify the pattern with the current argument. */
14358 tree arg
= TREE_VEC_ELT (packed_args
, i
);
14359 tree arg_expr
= NULL_TREE
;
14360 int arg_strict
= strict
;
14361 bool skip_arg_p
= false;
14367 /* This mirrors what we do in type_unification_real. */
14371 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
14372 | UNIFY_ALLOW_MORE_CV_QUAL
14373 | UNIFY_ALLOW_DERIVED
);
14377 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
14381 sub_strict
= UNIFY_ALLOW_NONE
;
14385 gcc_unreachable ();
14390 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
14391 if (type_unknown_p (arg
))
14393 /* [temp.deduct.type] A template-argument can be
14394 deduced from a pointer to function or pointer
14395 to member function argument if the set of
14396 overloaded functions does not contain function
14397 templates and at most one of a set of
14398 overloaded functions provides a unique
14401 if (resolve_overloaded_unification
14402 (tparms
, targs
, parm
, arg
,
14403 (unification_kind_t
) strict
,
14413 arg
= unlowered_expr_type (arg
);
14414 if (arg
== error_mark_node
)
14419 arg_strict
= sub_strict
;
14423 maybe_adjust_types_for_deduction ((unification_kind_t
) strict
,
14424 &parm
, &arg
, arg_expr
);
14429 /* For deduction from an init-list we need the actual list. */
14430 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
14432 if (unify (tparms
, targs
, parm
, arg
, arg_strict
))
14437 /* For each parameter pack, collect the deduced value. */
14438 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14441 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14443 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
14444 TMPL_ARG (targs
, level
, idx
);
14448 /* Verify that the results of unification with the parameter packs
14449 produce results consistent with what we've seen before, and make
14450 the deduced argument packs available. */
14451 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
14453 tree old_pack
= TREE_VALUE (pack
);
14454 tree new_args
= TREE_TYPE (pack
);
14455 int i
, len
= TREE_VEC_LENGTH (new_args
);
14457 bool nondeduced_p
= false;
14459 /* By default keep the original deduced argument pack.
14460 If necessary, more specific code is going to update the
14461 resulting deduced argument later down in this function. */
14462 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
14463 TMPL_ARG (targs
, level
, idx
) = old_pack
;
14465 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
14466 actually deduce anything. */
14467 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
14468 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
14469 nondeduced_p
= true;
14473 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
14475 /* Prepend the explicit arguments onto NEW_ARGS. */
14476 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
14477 tree old_args
= new_args
;
14478 int i
, explicit_len
= TREE_VEC_LENGTH (explicit_args
);
14479 int len
= explicit_len
+ TREE_VEC_LENGTH (old_args
);
14481 /* Copy the explicit arguments. */
14482 new_args
= make_tree_vec (len
);
14483 for (i
= 0; i
< explicit_len
; i
++)
14484 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (explicit_args
, i
);
14486 /* Copy the deduced arguments. */
14487 for (; i
< len
; i
++)
14488 TREE_VEC_ELT (new_args
, i
) =
14489 TREE_VEC_ELT (old_args
, i
- explicit_len
);
14495 /* Build the deduced *_ARGUMENT_PACK. */
14496 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
14498 result
= make_node (NONTYPE_ARGUMENT_PACK
);
14499 TREE_TYPE (result
) =
14500 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
14501 TREE_CONSTANT (result
) = 1;
14504 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
14506 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
14508 /* Note the deduced argument packs for this parameter
14510 TMPL_ARG (targs
, level
, idx
) = result
;
14512 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
14513 && (ARGUMENT_PACK_ARGS (old_pack
)
14514 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
14516 /* We only had the explicitly-provided arguments before, but
14517 now we have a complete set of arguments. */
14518 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
14520 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
14521 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
14522 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
14524 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack
),
14526 /* Inconsistent unification of this parameter pack. */
14533 /* Deduce the value of template parameters. TPARMS is the (innermost)
14534 set of template parameters to a template. TARGS is the bindings
14535 for those template parameters, as determined thus far; TARGS may
14536 include template arguments for outer levels of template parameters
14537 as well. PARM is a parameter to a template function, or a
14538 subcomponent of that parameter; ARG is the corresponding argument.
14539 This function attempts to match PARM with ARG in a manner
14540 consistent with the existing assignments in TARGS. If more values
14541 are deduced, then TARGS is updated.
14543 Returns 0 if the type deduction succeeds, 1 otherwise. The
14544 parameter STRICT is a bitwise or of the following flags:
14547 Require an exact match between PARM and ARG.
14548 UNIFY_ALLOW_MORE_CV_QUAL:
14549 Allow the deduced ARG to be more cv-qualified (by qualification
14550 conversion) than ARG.
14551 UNIFY_ALLOW_LESS_CV_QUAL:
14552 Allow the deduced ARG to be less cv-qualified than ARG.
14553 UNIFY_ALLOW_DERIVED:
14554 Allow the deduced ARG to be a template base class of ARG,
14555 or a pointer to a template base class of the type pointed to by
14557 UNIFY_ALLOW_INTEGER:
14558 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
14559 case for more information.
14560 UNIFY_ALLOW_OUTER_LEVEL:
14561 This is the outermost level of a deduction. Used to determine validity
14562 of qualification conversions. A valid qualification conversion must
14563 have const qualified pointers leading up to the inner type which
14564 requires additional CV quals, except at the outer level, where const
14565 is not required [conv.qual]. It would be normal to set this flag in
14566 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
14567 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
14568 This is the outermost level of a deduction, and PARM can be more CV
14569 qualified at this point.
14570 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
14571 This is the outermost level of a deduction, and PARM can be less CV
14572 qualified at this point. */
14575 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
)
14580 int strict_in
= strict
;
14582 /* I don't think this will do the right thing with respect to types.
14583 But the only case I've seen it in so far has been array bounds, where
14584 signedness is the only information lost, and I think that will be
14586 while (TREE_CODE (parm
) == NOP_EXPR
)
14587 parm
= TREE_OPERAND (parm
, 0);
14589 if (arg
== error_mark_node
)
14591 if (arg
== unknown_type_node
14592 || arg
== init_list_type_node
)
14593 /* We can't deduce anything from this, but we might get all the
14594 template args from other function args. */
14597 /* If PARM uses template parameters, then we can't bail out here,
14598 even if ARG == PARM, since we won't record unifications for the
14599 template parameters. We might need them if we're trying to
14600 figure out which of two things is more specialized. */
14601 if (arg
== parm
&& !uses_template_parms (parm
))
14604 /* Handle init lists early, so the rest of the function can assume
14605 we're dealing with a type. */
14606 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
14610 tree orig_parm
= parm
;
14612 /* Replace T with std::initializer_list<T> for deduction. */
14613 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14614 && flag_deduce_init_list
)
14615 parm
= listify (parm
);
14617 if (!is_std_init_list (parm
))
14618 /* We can only deduce from an initializer list argument if the
14619 parameter is std::initializer_list; otherwise this is a
14620 non-deduced context. */
14623 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
14625 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
14627 int elt_strict
= strict
;
14629 if (elt
== error_mark_node
)
14632 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
14634 tree type
= TREE_TYPE (elt
);
14635 /* It should only be possible to get here for a call. */
14636 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
14637 elt_strict
|= maybe_adjust_types_for_deduction
14638 (DEDUCE_CALL
, &elttype
, &type
, elt
);
14642 if (unify (tparms
, targs
, elttype
, elt
, elt_strict
))
14646 /* If the std::initializer_list<T> deduction worked, replace the
14647 deduced A with std::initializer_list<A>. */
14648 if (orig_parm
!= parm
)
14650 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
14651 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
14652 targ
= listify (targ
);
14653 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
14658 /* Immediately reject some pairs that won't unify because of
14659 cv-qualification mismatches. */
14660 if (TREE_CODE (arg
) == TREE_CODE (parm
)
14662 /* It is the elements of the array which hold the cv quals of an array
14663 type, and the elements might be template type parms. We'll check
14664 when we recurse. */
14665 && TREE_CODE (arg
) != ARRAY_TYPE
14666 /* We check the cv-qualifiers when unifying with template type
14667 parameters below. We want to allow ARG `const T' to unify with
14668 PARM `T' for example, when computing which of two templates
14669 is more specialized, for example. */
14670 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
14671 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
14674 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
14675 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
14676 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
14677 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
14678 strict
&= ~UNIFY_ALLOW_DERIVED
;
14679 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
14680 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
14682 switch (TREE_CODE (parm
))
14684 case TYPENAME_TYPE
:
14686 case UNBOUND_CLASS_TEMPLATE
:
14687 /* In a type which contains a nested-name-specifier, template
14688 argument values cannot be deduced for template parameters used
14689 within the nested-name-specifier. */
14692 case TEMPLATE_TYPE_PARM
:
14693 case TEMPLATE_TEMPLATE_PARM
:
14694 case BOUND_TEMPLATE_TEMPLATE_PARM
:
14695 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
14696 if (tparm
== error_mark_node
)
14699 if (TEMPLATE_TYPE_LEVEL (parm
)
14700 != template_decl_level (tparm
))
14701 /* The PARM is not one we're trying to unify. Just check
14702 to see if it matches ARG. */
14703 return (TREE_CODE (arg
) == TREE_CODE (parm
)
14704 && same_type_p (parm
, arg
)) ? 0 : 1;
14705 idx
= TEMPLATE_TYPE_IDX (parm
);
14706 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
14707 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
14709 /* Check for mixed types and values. */
14710 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
14711 && TREE_CODE (tparm
) != TYPE_DECL
)
14712 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14713 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
14716 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14718 /* ARG must be constructed from a template class or a template
14719 template parameter. */
14720 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
14721 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
14725 tree parmvec
= TYPE_TI_ARGS (parm
);
14726 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
14728 = DECL_INNERMOST_TEMPLATE_PARMS
14729 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
14731 int parm_variadic_p
= 0;
14733 /* The resolution to DR150 makes clear that default
14734 arguments for an N-argument may not be used to bind T
14735 to a template template parameter with fewer than N
14736 parameters. It is not safe to permit the binding of
14737 default arguments as an extension, as that may change
14738 the meaning of a conforming program. Consider:
14740 struct Dense { static const unsigned int dim = 1; };
14742 template <template <typename> class View,
14744 void operator+(float, View<Block> const&);
14746 template <typename Block,
14747 unsigned int Dim = Block::dim>
14748 struct Lvalue_proxy { operator float() const; };
14752 Lvalue_proxy<Dense> p;
14757 Here, if Lvalue_proxy is permitted to bind to View, then
14758 the global operator+ will be used; if they are not, the
14759 Lvalue_proxy will be converted to float. */
14760 if (coerce_template_parms (parm_parms
,
14762 TYPE_TI_TEMPLATE (parm
),
14764 /*require_all_args=*/true,
14765 /*use_default_args=*/false)
14766 == error_mark_node
)
14769 /* Deduce arguments T, i from TT<T> or TT<i>.
14770 We check each element of PARMVEC and ARGVEC individually
14771 rather than the whole TREE_VEC since they can have
14772 different number of elements. */
14774 parmvec
= expand_template_argument_pack (parmvec
);
14775 argvec
= expand_template_argument_pack (argvec
);
14777 len
= TREE_VEC_LENGTH (parmvec
);
14779 /* Check if the parameters end in a pack, making them
14782 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
14783 parm_variadic_p
= 1;
14785 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
14788 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
14790 if (unify (tparms
, targs
,
14791 TREE_VEC_ELT (parmvec
, i
),
14792 TREE_VEC_ELT (argvec
, i
),
14797 if (parm_variadic_p
14798 && unify_pack_expansion (tparms
, targs
,
14801 /*call_args_p=*/false,
14805 arg
= TYPE_TI_TEMPLATE (arg
);
14807 /* Fall through to deduce template name. */
14810 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
14811 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
14813 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
14815 /* Simple cases: Value already set, does match or doesn't. */
14816 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
14823 /* If PARM is `const T' and ARG is only `int', we don't have
14824 a match unless we are allowing additional qualification.
14825 If ARG is `const int' and PARM is just `T' that's OK;
14826 that binds `const int' to `T'. */
14827 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
14831 /* Consider the case where ARG is `const volatile int' and
14832 PARM is `const T'. Then, T should be `volatile int'. */
14833 arg
= cp_build_qualified_type_real
14834 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
14835 if (arg
== error_mark_node
)
14838 /* Simple cases: Value already set, does match or doesn't. */
14839 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
14844 /* Make sure that ARG is not a variable-sized array. (Note
14845 that were talking about variable-sized arrays (like
14846 `int[n]'), rather than arrays of unknown size (like
14847 `int[]').) We'll get very confused by such a type since
14848 the bound of the array will not be computable in an
14849 instantiation. Besides, such types are not allowed in
14850 ISO C++, so we can do as we please here. */
14851 if (variably_modified_type_p (arg
, NULL_TREE
))
14854 /* Strip typedefs as in convert_template_argument. */
14855 arg
= strip_typedefs (arg
);
14858 /* If ARG is a parameter pack or an expansion, we cannot unify
14859 against it unless PARM is also a parameter pack. */
14860 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
14861 && !template_parameter_pack_p (parm
))
14864 /* If the argument deduction results is a METHOD_TYPE,
14865 then there is a problem.
14866 METHOD_TYPE doesn't map to any real C++ type the result of
14867 the deduction can not be of that type. */
14868 if (TREE_CODE (arg
) == METHOD_TYPE
)
14871 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
14874 case TEMPLATE_PARM_INDEX
:
14875 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
14876 if (tparm
== error_mark_node
)
14879 if (TEMPLATE_PARM_LEVEL (parm
)
14880 != template_decl_level (tparm
))
14881 /* The PARM is not one we're trying to unify. Just check
14882 to see if it matches ARG. */
14883 return !(TREE_CODE (arg
) == TREE_CODE (parm
)
14884 && cp_tree_equal (parm
, arg
));
14886 idx
= TEMPLATE_PARM_IDX (parm
);
14887 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
14890 return !cp_tree_equal (targ
, arg
);
14892 /* [temp.deduct.type] If, in the declaration of a function template
14893 with a non-type template-parameter, the non-type
14894 template-parameter is used in an expression in the function
14895 parameter-list and, if the corresponding template-argument is
14896 deduced, the template-argument type shall match the type of the
14897 template-parameter exactly, except that a template-argument
14898 deduced from an array bound may be of any integral type.
14899 The non-type parameter might use already deduced type parameters. */
14900 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
14901 if (!TREE_TYPE (arg
))
14902 /* Template-parameter dependent expression. Just accept it for now.
14903 It will later be processed in convert_template_argument. */
14905 else if (same_type_p (TREE_TYPE (arg
), tparm
))
14907 else if ((strict
& UNIFY_ALLOW_INTEGER
)
14908 && (TREE_CODE (tparm
) == INTEGER_TYPE
14909 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
14910 /* Convert the ARG to the type of PARM; the deduced non-type
14911 template argument must exactly match the types of the
14912 corresponding parameter. */
14913 arg
= fold (build_nop (tparm
, arg
));
14914 else if (uses_template_parms (tparm
))
14915 /* We haven't deduced the type of this parameter yet. Try again
14921 /* If ARG is a parameter pack or an expansion, we cannot unify
14922 against it unless PARM is also a parameter pack. */
14923 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
14924 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
14927 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
14932 /* A pointer-to-member constant can be unified only with
14933 another constant. */
14934 if (TREE_CODE (arg
) != PTRMEM_CST
)
14937 /* Just unify the class member. It would be useless (and possibly
14938 wrong, depending on the strict flags) to unify also
14939 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14940 arg refer to the same variable, even if through different
14941 classes. For instance:
14943 struct A { int x; };
14946 Unification of &A::x and &B::x must succeed. */
14947 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
14948 PTRMEM_CST_MEMBER (arg
), strict
);
14953 if (TREE_CODE (arg
) != POINTER_TYPE
)
14956 /* [temp.deduct.call]
14958 A can be another pointer or pointer to member type that can
14959 be converted to the deduced A via a qualification
14960 conversion (_conv.qual_).
14962 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14963 This will allow for additional cv-qualification of the
14964 pointed-to types if appropriate. */
14966 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
14967 /* The derived-to-base conversion only persists through one
14968 level of pointers. */
14969 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
14971 return unify (tparms
, targs
, TREE_TYPE (parm
),
14972 TREE_TYPE (arg
), strict
);
14975 case REFERENCE_TYPE
:
14976 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
14978 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
14979 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
14982 if (TREE_CODE (arg
) != ARRAY_TYPE
)
14984 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
14985 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
14987 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
14994 /* Our representation of array types uses "N - 1" as the
14995 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
14996 not an integer constant. We cannot unify arbitrarily
14997 complex expressions, so we eliminate the MINUS_EXPRs
14999 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
15000 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
15003 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
15004 parm_max
= TREE_OPERAND (parm_max
, 0);
15006 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
15007 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
15010 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
15011 trying to unify the type of a variable with the type
15012 of a template parameter. For example:
15014 template <unsigned int N>
15015 void f (char (&) [N]);
15022 Here, the type of the ARG will be "int [g(i)]", and
15023 may be a SAVE_EXPR, etc. */
15024 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
15026 arg_max
= TREE_OPERAND (arg_max
, 0);
15029 /* If only one of the bounds used a MINUS_EXPR, compensate
15030 by adding one to the other bound. */
15031 if (parm_cst
&& !arg_cst
)
15032 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
15036 else if (arg_cst
&& !parm_cst
)
15037 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
15042 if (unify (tparms
, targs
, parm_max
, arg_max
, UNIFY_ALLOW_INTEGER
))
15045 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
15046 strict
& UNIFY_ALLOW_MORE_CV_QUAL
);
15053 case ENUMERAL_TYPE
:
15055 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15058 /* We have already checked cv-qualification at the top of the
15060 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
15063 /* As far as unification is concerned, this wins. Later checks
15064 will invalidate it if necessary. */
15067 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
15068 /* Type INTEGER_CST can come from ordinary constant template args. */
15070 while (TREE_CODE (arg
) == NOP_EXPR
)
15071 arg
= TREE_OPERAND (arg
, 0);
15073 if (TREE_CODE (arg
) != INTEGER_CST
)
15075 return !tree_int_cst_equal (parm
, arg
);
15080 if (TREE_CODE (arg
) != TREE_VEC
)
15082 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
15084 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
15085 if (unify (tparms
, targs
,
15086 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
15094 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15097 if (TYPE_PTRMEMFUNC_P (parm
))
15099 if (!TYPE_PTRMEMFUNC_P (arg
))
15102 return unify (tparms
, targs
,
15103 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
15104 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
15108 if (CLASSTYPE_TEMPLATE_INFO (parm
))
15110 tree t
= NULL_TREE
;
15112 if (strict_in
& UNIFY_ALLOW_DERIVED
)
15114 /* First, we try to unify the PARM and ARG directly. */
15115 t
= try_class_unification (tparms
, targs
,
15120 /* Fallback to the special case allowed in
15121 [temp.deduct.call]:
15123 If P is a class, and P has the form
15124 template-id, then A can be a derived class of
15125 the deduced A. Likewise, if P is a pointer to
15126 a class of the form template-id, A can be a
15127 pointer to a derived class pointed to by the
15129 t
= get_template_base (tparms
, targs
, parm
, arg
);
15135 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
15136 && (CLASSTYPE_TI_TEMPLATE (parm
)
15137 == CLASSTYPE_TI_TEMPLATE (arg
)))
15138 /* Perhaps PARM is something like S<U> and ARG is S<int>.
15139 Then, we should unify `int' and `U'. */
15142 /* There's no chance of unification succeeding. */
15145 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
15146 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
);
15148 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
15153 case FUNCTION_TYPE
:
15155 unsigned int nargs
;
15160 if (TREE_CODE (arg
) != TREE_CODE (parm
))
15163 /* CV qualifications for methods can never be deduced, they must
15164 match exactly. We need to check them explicitly here,
15165 because type_unification_real treats them as any other
15166 cv-qualified parameter. */
15167 if (TREE_CODE (parm
) == METHOD_TYPE
15168 && (!check_cv_quals_for_unify
15170 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg
))),
15171 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm
))))))
15174 if (unify (tparms
, targs
, TREE_TYPE (parm
),
15175 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
))
15178 nargs
= list_length (TYPE_ARG_TYPES (arg
));
15179 args
= XALLOCAVEC (tree
, nargs
);
15180 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
15181 a
!= NULL_TREE
&& a
!= void_list_node
;
15182 a
= TREE_CHAIN (a
), ++i
)
15183 args
[i
] = TREE_VALUE (a
);
15186 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
15187 args
, nargs
, 1, DEDUCE_EXACT
,
15192 /* Unify a pointer to member with a pointer to member function, which
15193 deduces the type of the member as a function type. */
15194 if (TYPE_PTRMEMFUNC_P (arg
))
15199 /* Check top-level cv qualifiers */
15200 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
15203 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
15204 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
), UNIFY_ALLOW_NONE
))
15207 /* Determine the type of the function we are unifying against. */
15208 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
15210 build_function_type (TREE_TYPE (method_type
),
15211 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
15213 /* Extract the cv-qualifiers of the member function from the
15214 implicit object parameter and place them on the function
15215 type to be restored later. */
15216 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
15217 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
);
15220 if (TREE_CODE (arg
) != OFFSET_TYPE
)
15222 if (unify (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
15223 TYPE_OFFSET_BASETYPE (arg
), UNIFY_ALLOW_NONE
))
15225 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
15229 if (DECL_TEMPLATE_PARM_P (parm
))
15230 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
);
15231 if (arg
!= integral_constant_value (parm
))
15236 case TEMPLATE_DECL
:
15237 /* Matched cases are handled by the ARG == PARM test above. */
15241 /* A non-type template parameter that is a variable should be a
15242 an integral constant, in which case, it whould have been
15243 folded into its (constant) value. So we should not be getting
15244 a variable here. */
15245 gcc_unreachable ();
15247 case TYPE_ARGUMENT_PACK
:
15248 case NONTYPE_ARGUMENT_PACK
:
15250 tree packed_parms
= ARGUMENT_PACK_ARGS (parm
);
15251 tree packed_args
= ARGUMENT_PACK_ARGS (arg
);
15252 int i
, len
= TREE_VEC_LENGTH (packed_parms
);
15253 int argslen
= TREE_VEC_LENGTH (packed_args
);
15254 int parm_variadic_p
= 0;
15256 for (i
= 0; i
< len
; ++i
)
15258 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms
, i
)))
15261 /* We can unify against something with a trailing
15263 parm_variadic_p
= 1;
15265 /* Since there is something following the pack
15266 expansion, we cannot unify this template argument
15273 /* If we don't have enough arguments to satisfy the parameters
15274 (not counting the pack expression at the end), or we have
15275 too many arguments for a parameter list that doesn't end in
15276 a pack expression, we can't unify. */
15277 if (argslen
< (len
- parm_variadic_p
)
15278 || (argslen
> len
&& !parm_variadic_p
))
15281 /* Unify all of the parameters that precede the (optional)
15282 pack expression. */
15283 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
15285 if (unify (tparms
, targs
, TREE_VEC_ELT (packed_parms
, i
),
15286 TREE_VEC_ELT (packed_args
, i
), strict
))
15290 if (parm_variadic_p
)
15291 return unify_pack_expansion (tparms
, targs
,
15292 packed_parms
, packed_args
,
15293 strict
, /*call_args_p=*/false,
15301 case DECLTYPE_TYPE
:
15302 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
15307 /* Unification fails if we hit an error node. */
15311 gcc_assert (EXPR_P (parm
));
15313 /* We must be looking at an expression. This can happen with
15317 void foo(S<I>, S<I + 2>);
15319 This is a "nondeduced context":
15323 The nondeduced contexts are:
15325 --A type that is a template-id in which one or more of
15326 the template-arguments is an expression that references
15327 a template-parameter.
15329 In these cases, we assume deduction succeeded, but don't
15330 actually infer any unifications. */
15332 if (!uses_template_parms (parm
)
15333 && !template_args_equal (parm
, arg
))
15340 /* Note that DECL can be defined in this translation unit, if
15344 mark_definable (tree decl
)
15347 DECL_NOT_REALLY_EXTERN (decl
) = 1;
15348 FOR_EACH_CLONE (clone
, decl
)
15349 DECL_NOT_REALLY_EXTERN (clone
) = 1;
15352 /* Called if RESULT is explicitly instantiated, or is a member of an
15353 explicitly instantiated class. */
15356 mark_decl_instantiated (tree result
, int extern_p
)
15358 SET_DECL_EXPLICIT_INSTANTIATION (result
);
15360 /* If this entity has already been written out, it's too late to
15361 make any modifications. */
15362 if (TREE_ASM_WRITTEN (result
))
15365 if (TREE_CODE (result
) != FUNCTION_DECL
)
15366 /* The TREE_PUBLIC flag for function declarations will have been
15367 set correctly by tsubst. */
15368 TREE_PUBLIC (result
) = 1;
15370 /* This might have been set by an earlier implicit instantiation. */
15371 DECL_COMDAT (result
) = 0;
15374 DECL_NOT_REALLY_EXTERN (result
) = 0;
15377 mark_definable (result
);
15378 /* Always make artificials weak. */
15379 if (DECL_ARTIFICIAL (result
) && flag_weak
)
15380 comdat_linkage (result
);
15381 /* For WIN32 we also want to put explicit instantiations in
15382 linkonce sections. */
15383 else if (TREE_PUBLIC (result
))
15384 maybe_make_one_only (result
);
15387 /* If EXTERN_P, then this function will not be emitted -- unless
15388 followed by an explicit instantiation, at which point its linkage
15389 will be adjusted. If !EXTERN_P, then this function will be
15390 emitted here. In neither circumstance do we want
15391 import_export_decl to adjust the linkage. */
15392 DECL_INTERFACE_KNOWN (result
) = 1;
15395 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
15396 important template arguments. If any are missing, we check whether
15397 they're important by using error_mark_node for substituting into any
15398 args that were used for partial ordering (the ones between ARGS and END)
15399 and seeing if it bubbles up. */
15402 check_undeduced_parms (tree targs
, tree args
, tree end
)
15404 bool found
= false;
15406 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
15407 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
15410 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
15414 for (; args
!= end
; args
= TREE_CHAIN (args
))
15416 tree substed
= tsubst (TREE_VALUE (args
), targs
, tf_none
, NULL_TREE
);
15417 if (substed
== error_mark_node
)
15424 /* Given two function templates PAT1 and PAT2, return:
15426 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
15427 -1 if PAT2 is more specialized than PAT1.
15428 0 if neither is more specialized.
15430 LEN indicates the number of parameters we should consider
15431 (defaulted parameters should not be considered).
15433 The 1998 std underspecified function template partial ordering, and
15434 DR214 addresses the issue. We take pairs of arguments, one from
15435 each of the templates, and deduce them against each other. One of
15436 the templates will be more specialized if all the *other*
15437 template's arguments deduce against its arguments and at least one
15438 of its arguments *does* *not* deduce against the other template's
15439 corresponding argument. Deduction is done as for class templates.
15440 The arguments used in deduction have reference and top level cv
15441 qualifiers removed. Iff both arguments were originally reference
15442 types *and* deduction succeeds in both directions, the template
15443 with the more cv-qualified argument wins for that pairing (if
15444 neither is more cv-qualified, they both are equal). Unlike regular
15445 deduction, after all the arguments have been deduced in this way,
15446 we do *not* verify the deduced template argument values can be
15447 substituted into non-deduced contexts.
15449 The logic can be a bit confusing here, because we look at deduce1 and
15450 targs1 to see if pat2 is at least as specialized, and vice versa; if we
15451 can find template arguments for pat1 to make arg1 look like arg2, that
15452 means that arg2 is at least as specialized as arg1. */
15455 more_specialized_fn (tree pat1
, tree pat2
, int len
)
15457 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
15458 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
15459 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
15460 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
15461 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
15462 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
15463 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
15464 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
15465 tree origs1
, origs2
;
15466 bool lose1
= false;
15467 bool lose2
= false;
15469 /* Remove the this parameter from non-static member functions. If
15470 one is a non-static member function and the other is not a static
15471 member function, remove the first parameter from that function
15472 also. This situation occurs for operator functions where we
15473 locate both a member function (with this pointer) and non-member
15474 operator (with explicit first operand). */
15475 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
15477 len
--; /* LEN is the number of significant arguments for DECL1 */
15478 args1
= TREE_CHAIN (args1
);
15479 if (!DECL_STATIC_FUNCTION_P (decl2
))
15480 args2
= TREE_CHAIN (args2
);
15482 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
15484 args2
= TREE_CHAIN (args2
);
15485 if (!DECL_STATIC_FUNCTION_P (decl1
))
15488 args1
= TREE_CHAIN (args1
);
15492 /* If only one is a conversion operator, they are unordered. */
15493 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
15496 /* Consider the return type for a conversion function */
15497 if (DECL_CONV_FN_P (decl1
))
15499 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
15500 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
15504 processing_template_decl
++;
15510 /* Stop when an ellipsis is seen. */
15511 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
15513 tree arg1
= TREE_VALUE (args1
);
15514 tree arg2
= TREE_VALUE (args2
);
15515 int deduce1
, deduce2
;
15519 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
15520 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15522 /* When both arguments are pack expansions, we need only
15523 unify the patterns themselves. */
15524 arg1
= PACK_EXPANSION_PATTERN (arg1
);
15525 arg2
= PACK_EXPANSION_PATTERN (arg2
);
15527 /* This is the last comparison we need to do. */
15531 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
15533 arg1
= TREE_TYPE (arg1
);
15534 quals1
= cp_type_quals (arg1
);
15537 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
15539 arg2
= TREE_TYPE (arg2
);
15540 quals2
= cp_type_quals (arg2
);
15543 if ((quals1
< 0) != (quals2
< 0))
15545 /* Only of the args is a reference, see if we should apply
15546 array/function pointer decay to it. This is not part of
15547 DR214, but is, IMHO, consistent with the deduction rules
15548 for the function call itself, and with our earlier
15549 implementation of the underspecified partial ordering
15550 rules. (nathan). */
15553 switch (TREE_CODE (arg1
))
15556 arg1
= TREE_TYPE (arg1
);
15558 case FUNCTION_TYPE
:
15559 arg1
= build_pointer_type (arg1
);
15568 switch (TREE_CODE (arg2
))
15571 arg2
= TREE_TYPE (arg2
);
15573 case FUNCTION_TYPE
:
15574 arg2
= build_pointer_type (arg2
);
15583 arg1
= TYPE_MAIN_VARIANT (arg1
);
15584 arg2
= TYPE_MAIN_VARIANT (arg2
);
15586 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
15588 int i
, len2
= list_length (args2
);
15589 tree parmvec
= make_tree_vec (1);
15590 tree argvec
= make_tree_vec (len2
);
15593 /* Setup the parameter vector, which contains only ARG1. */
15594 TREE_VEC_ELT (parmvec
, 0) = arg1
;
15596 /* Setup the argument vector, which contains the remaining
15598 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
15599 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
15601 deduce1
= !unify_pack_expansion (tparms1
, targs1
, parmvec
,
15602 argvec
, UNIFY_ALLOW_NONE
,
15603 /*call_args_p=*/false,
15606 /* We cannot deduce in the other direction, because ARG1 is
15607 a pack expansion but ARG2 is not. */
15610 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15612 int i
, len1
= list_length (args1
);
15613 tree parmvec
= make_tree_vec (1);
15614 tree argvec
= make_tree_vec (len1
);
15617 /* Setup the parameter vector, which contains only ARG1. */
15618 TREE_VEC_ELT (parmvec
, 0) = arg2
;
15620 /* Setup the argument vector, which contains the remaining
15622 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
15623 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
15625 deduce2
= !unify_pack_expansion (tparms2
, targs2
, parmvec
,
15626 argvec
, UNIFY_ALLOW_NONE
,
15627 /*call_args_p=*/false,
15630 /* We cannot deduce in the other direction, because ARG2 is
15631 a pack expansion but ARG1 is not.*/
15637 /* The normal case, where neither argument is a pack
15639 deduce1
= !unify (tparms1
, targs1
, arg1
, arg2
, UNIFY_ALLOW_NONE
);
15640 deduce2
= !unify (tparms2
, targs2
, arg2
, arg1
, UNIFY_ALLOW_NONE
);
15643 /* If we couldn't deduce arguments for tparms1 to make arg1 match
15644 arg2, then arg2 is not as specialized as arg1. */
15650 /* "If, for a given type, deduction succeeds in both directions
15651 (i.e., the types are identical after the transformations above)
15652 and if the type from the argument template is more cv-qualified
15653 than the type from the parameter template (as described above)
15654 that type is considered to be more specialized than the other. If
15655 neither type is more cv-qualified than the other then neither type
15656 is more specialized than the other." */
15658 if (deduce1
&& deduce2
15659 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
15661 if ((quals1
& quals2
) == quals2
)
15663 if ((quals1
& quals2
) == quals1
)
15667 if (lose1
&& lose2
)
15668 /* We've failed to deduce something in either direction.
15669 These must be unordered. */
15672 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
15673 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
15674 /* We have already processed all of the arguments in our
15675 handing of the pack expansion type. */
15678 args1
= TREE_CHAIN (args1
);
15679 args2
= TREE_CHAIN (args2
);
15682 /* "In most cases, all template parameters must have values in order for
15683 deduction to succeed, but for partial ordering purposes a template
15684 parameter may remain without a value provided it is not used in the
15685 types being used for partial ordering."
15687 Thus, if we are missing any of the targs1 we need to substitute into
15688 origs1, then pat2 is not as specialized as pat1. This can happen when
15689 there is a nondeduced context. */
15690 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
15692 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
15695 processing_template_decl
--;
15697 /* All things being equal, if the next argument is a pack expansion
15698 for one function but not for the other, prefer the
15699 non-variadic function. FIXME this is bogus; see c++/41958. */
15701 && args1
&& TREE_VALUE (args1
)
15702 && args2
&& TREE_VALUE (args2
))
15704 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
15705 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
15708 if (lose1
== lose2
)
15716 /* Determine which of two partial specializations is more specialized.
15718 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
15719 to the first partial specialization. The TREE_VALUE is the
15720 innermost set of template parameters for the partial
15721 specialization. PAT2 is similar, but for the second template.
15723 Return 1 if the first partial specialization is more specialized;
15724 -1 if the second is more specialized; 0 if neither is more
15727 See [temp.class.order] for information about determining which of
15728 two templates is more specialized. */
15731 more_specialized_class (tree pat1
, tree pat2
)
15736 bool any_deductions
= false;
15738 tmpl1
= TREE_TYPE (pat1
);
15739 tmpl2
= TREE_TYPE (pat2
);
15741 /* Just like what happens for functions, if we are ordering between
15742 different class template specializations, we may encounter dependent
15743 types in the arguments, and we need our dependency check functions
15744 to behave correctly. */
15745 ++processing_template_decl
;
15746 targs
= get_class_bindings (TREE_VALUE (pat1
),
15747 CLASSTYPE_TI_ARGS (tmpl1
),
15748 CLASSTYPE_TI_ARGS (tmpl2
));
15752 any_deductions
= true;
15755 targs
= get_class_bindings (TREE_VALUE (pat2
),
15756 CLASSTYPE_TI_ARGS (tmpl2
),
15757 CLASSTYPE_TI_ARGS (tmpl1
));
15761 any_deductions
= true;
15763 --processing_template_decl
;
15765 /* In the case of a tie where at least one of the class templates
15766 has a parameter pack at the end, the template with the most
15767 non-packed parameters wins. */
15770 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
15771 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
15773 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
15774 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
15775 int len1
= TREE_VEC_LENGTH (args1
);
15776 int len2
= TREE_VEC_LENGTH (args2
);
15778 /* We don't count the pack expansion at the end. */
15779 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
15781 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
15786 else if (len1
< len2
)
15793 /* Return the template arguments that will produce the function signature
15794 DECL from the function template FN, with the explicit template
15795 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
15796 also match. Return NULL_TREE if no satisfactory arguments could be
15800 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
15802 int ntparms
= DECL_NTPARMS (fn
);
15803 tree targs
= make_tree_vec (ntparms
);
15805 tree decl_arg_types
;
15807 unsigned int nargs
, ix
;
15810 /* Substitute the explicit template arguments into the type of DECL.
15811 The call to fn_type_unification will handle substitution into the
15813 decl_type
= TREE_TYPE (decl
);
15814 if (explicit_args
&& uses_template_parms (decl_type
))
15817 tree converted_args
;
15819 if (DECL_TEMPLATE_INFO (decl
))
15820 tmpl
= DECL_TI_TEMPLATE (decl
);
15822 /* We can get here for some invalid specializations. */
15826 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
15827 explicit_args
, NULL_TREE
,
15829 /*require_all_args=*/false,
15830 /*use_default_args=*/false);
15831 if (converted_args
== error_mark_node
)
15834 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
15835 if (decl_type
== error_mark_node
)
15839 /* Never do unification on the 'this' parameter. */
15840 decl_arg_types
= skip_artificial_parms_for (decl
,
15841 TYPE_ARG_TYPES (decl_type
));
15843 nargs
= list_length (decl_arg_types
);
15844 args
= XALLOCAVEC (tree
, nargs
);
15845 for (arg
= decl_arg_types
, ix
= 0;
15846 arg
!= NULL_TREE
&& arg
!= void_list_node
;
15847 arg
= TREE_CHAIN (arg
), ++ix
)
15848 args
[ix
] = TREE_VALUE (arg
);
15850 if (fn_type_unification (fn
, explicit_args
, targs
,
15852 (check_rettype
|| DECL_CONV_FN_P (fn
)
15853 ? TREE_TYPE (decl_type
) : NULL_TREE
),
15854 DEDUCE_EXACT
, LOOKUP_NORMAL
))
15860 /* Return the innermost template arguments that, when applied to a
15861 template specialization whose innermost template parameters are
15862 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
15865 For example, suppose we have:
15867 template <class T, class U> struct S {};
15868 template <class T> struct S<T*, int> {};
15870 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
15871 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
15872 int}. The resulting vector will be {double}, indicating that `T'
15873 is bound to `double'. */
15876 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
15878 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
15880 tree innermost_deduced_args
;
15882 innermost_deduced_args
= make_tree_vec (ntparms
);
15883 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
15885 deduced_args
= copy_node (args
);
15886 SET_TMPL_ARGS_LEVEL (deduced_args
,
15887 TMPL_ARGS_DEPTH (deduced_args
),
15888 innermost_deduced_args
);
15891 deduced_args
= innermost_deduced_args
;
15893 if (unify (tparms
, deduced_args
,
15894 INNERMOST_TEMPLATE_ARGS (spec_args
),
15895 INNERMOST_TEMPLATE_ARGS (args
),
15899 for (i
= 0; i
< ntparms
; ++i
)
15900 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
15903 /* Verify that nondeduced template arguments agree with the type
15904 obtained from argument deduction.
15908 struct A { typedef int X; };
15909 template <class T, class U> struct C {};
15910 template <class T> struct C<T, typename T::X> {};
15912 Then with the instantiation `C<A, int>', we can deduce that
15913 `T' is `A' but unify () does not check whether `typename T::X'
15915 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
15916 if (spec_args
== error_mark_node
15917 /* We only need to check the innermost arguments; the other
15918 arguments will always agree. */
15919 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
15920 INNERMOST_TEMPLATE_ARGS (args
)))
15923 /* Now that we have bindings for all of the template arguments,
15924 ensure that the arguments deduced for the template template
15925 parameters have compatible template parameter lists. See the use
15926 of template_template_parm_bindings_ok_p in fn_type_unification
15927 for more information. */
15928 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
15931 return deduced_args
;
15934 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
15935 Return the TREE_LIST node with the most specialized template, if
15936 any. If there is no most specialized template, the error_mark_node
15939 Note that this function does not look at, or modify, the
15940 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
15941 returned is one of the elements of INSTANTIATIONS, callers may
15942 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
15943 and retrieve it from the value returned. */
15946 most_specialized_instantiation (tree templates
)
15950 ++processing_template_decl
;
15953 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
15957 if (get_bindings (TREE_VALUE (champ
),
15958 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
15959 NULL_TREE
, /*check_ret=*/false))
15962 if (get_bindings (TREE_VALUE (fn
),
15963 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
15964 NULL_TREE
, /*check_ret=*/false))
15971 /* Equally specialized, move to next function. If there
15972 is no next function, nothing's most specialized. */
15973 fn
= TREE_CHAIN (fn
);
15981 /* Now verify that champ is better than everything earlier in the
15982 instantiation list. */
15983 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
15984 if (get_bindings (TREE_VALUE (champ
),
15985 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
15986 NULL_TREE
, /*check_ret=*/false)
15987 || !get_bindings (TREE_VALUE (fn
),
15988 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
15989 NULL_TREE
, /*check_ret=*/false))
15995 processing_template_decl
--;
15998 return error_mark_node
;
16003 /* If DECL is a specialization of some template, return the most
16004 general such template. Otherwise, returns NULL_TREE.
16006 For example, given:
16008 template <class T> struct S { template <class U> void f(U); };
16010 if TMPL is `template <class U> void S<int>::f(U)' this will return
16011 the full template. This function will not trace past partial
16012 specializations, however. For example, given in addition:
16014 template <class T> struct S<T*> { template <class U> void f(U); };
16016 if TMPL is `template <class U> void S<int*>::f(U)' this will return
16017 `template <class T> template <class U> S<T*>::f(U)'. */
16020 most_general_template (tree decl
)
16022 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
16023 an immediate specialization. */
16024 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16026 if (DECL_TEMPLATE_INFO (decl
)) {
16027 decl
= DECL_TI_TEMPLATE (decl
);
16029 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
16030 template friend. */
16031 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
16037 /* Look for more and more general templates. */
16038 while (DECL_TEMPLATE_INFO (decl
))
16040 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
16041 (See cp-tree.h for details.) */
16042 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
16045 if (CLASS_TYPE_P (TREE_TYPE (decl
))
16046 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
16049 /* Stop if we run into an explicitly specialized class template. */
16050 if (!DECL_NAMESPACE_SCOPE_P (decl
)
16051 && DECL_CONTEXT (decl
)
16052 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
16055 decl
= DECL_TI_TEMPLATE (decl
);
16061 /* Return the most specialized of the class template partial
16062 specializations of TMPL which can produce TYPE, a specialization of
16063 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
16064 a _TYPE node corresponding to the partial specialization, while the
16065 TREE_PURPOSE is the set of template arguments that must be
16066 substituted into the TREE_TYPE in order to generate TYPE.
16068 If the choice of partial specialization is ambiguous, a diagnostic
16069 is issued, and the error_mark_node is returned. If there are no
16070 partial specializations of TMPL matching TYPE, then NULL_TREE is
16074 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
16076 tree list
= NULL_TREE
;
16082 tree outer_args
= NULL_TREE
;
16084 tmpl
= most_general_template (tmpl
);
16085 args
= CLASSTYPE_TI_ARGS (type
);
16087 /* For determining which partial specialization to use, only the
16088 innermost args are interesting. */
16089 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
16091 outer_args
= strip_innermost_template_args (args
, 1);
16092 args
= INNERMOST_TEMPLATE_ARGS (args
);
16095 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
16097 tree partial_spec_args
;
16099 tree parms
= TREE_VALUE (t
);
16101 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
16103 ++processing_template_decl
;
16109 /* Discard the outer levels of args, and then substitute in the
16110 template args from the enclosing class. */
16111 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
16112 partial_spec_args
= tsubst_template_args
16113 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
16115 /* PARMS already refers to just the innermost parms, but the
16116 template parms in partial_spec_args had their levels lowered
16117 by tsubst, so we need to do the same for the parm list. We
16118 can't just tsubst the TREE_VEC itself, as tsubst wants to
16119 treat a TREE_VEC as an argument vector. */
16120 parms
= copy_node (parms
);
16121 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
16122 TREE_VEC_ELT (parms
, i
) =
16123 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
16127 partial_spec_args
=
16128 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
16129 add_to_template_args (outer_args
,
16130 partial_spec_args
),
16132 /*require_all_args=*/true,
16133 /*use_default_args=*/true);
16135 --processing_template_decl
;
16137 if (partial_spec_args
== error_mark_node
)
16138 return error_mark_node
;
16140 spec_args
= get_class_bindings (parms
,
16146 spec_args
= add_to_template_args (outer_args
, spec_args
);
16147 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
16148 TREE_TYPE (list
) = TREE_TYPE (t
);
16155 ambiguous_p
= false;
16158 t
= TREE_CHAIN (t
);
16159 for (; t
; t
= TREE_CHAIN (t
))
16161 fate
= more_specialized_class (champ
, t
);
16168 t
= TREE_CHAIN (t
);
16171 ambiguous_p
= true;
16180 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
16182 fate
= more_specialized_class (champ
, t
);
16185 ambiguous_p
= true;
16193 char *spaces
= NULL
;
16194 if (!(complain
& tf_error
))
16195 return error_mark_node
;
16196 error ("ambiguous class template instantiation for %q#T", type
);
16197 str
= TREE_CHAIN (list
) ? _("candidates are:") : _("candidate is:");
16198 for (t
= list
; t
; t
= TREE_CHAIN (t
))
16200 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
16201 spaces
= spaces
? spaces
: get_spaces (str
);
16204 return error_mark_node
;
16210 /* Explicitly instantiate DECL. */
16213 do_decl_instantiation (tree decl
, tree storage
)
16215 tree result
= NULL_TREE
;
16218 if (!decl
|| decl
== error_mark_node
)
16219 /* An error occurred, for which grokdeclarator has already issued
16220 an appropriate message. */
16222 else if (! DECL_LANG_SPECIFIC (decl
))
16224 error ("explicit instantiation of non-template %q#D", decl
);
16227 else if (TREE_CODE (decl
) == VAR_DECL
)
16229 /* There is an asymmetry here in the way VAR_DECLs and
16230 FUNCTION_DECLs are handled by grokdeclarator. In the case of
16231 the latter, the DECL we get back will be marked as a
16232 template instantiation, and the appropriate
16233 DECL_TEMPLATE_INFO will be set up. This does not happen for
16234 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
16235 should handle VAR_DECLs as it currently handles
16237 if (!DECL_CLASS_SCOPE_P (decl
))
16239 error ("%qD is not a static data member of a class template", decl
);
16242 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
16243 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
16245 error ("no matching template for %qD found", decl
);
16248 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
16250 error ("type %qT for explicit instantiation %qD does not match "
16251 "declared type %qT", TREE_TYPE (result
), decl
,
16256 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
16258 error ("explicit instantiation of %q#D", decl
);
16264 /* Check for various error cases. Note that if the explicit
16265 instantiation is valid the RESULT will currently be marked as an
16266 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
16267 until we get here. */
16269 if (DECL_TEMPLATE_SPECIALIZATION (result
))
16271 /* DR 259 [temp.spec].
16273 Both an explicit instantiation and a declaration of an explicit
16274 specialization shall not appear in a program unless the explicit
16275 instantiation follows a declaration of the explicit specialization.
16277 For a given set of template parameters, if an explicit
16278 instantiation of a template appears after a declaration of an
16279 explicit specialization for that template, the explicit
16280 instantiation has no effect. */
16283 else if (DECL_EXPLICIT_INSTANTIATION (result
))
16287 No program shall explicitly instantiate any template more
16290 We check DECL_NOT_REALLY_EXTERN so as not to complain when
16291 the first instantiation was `extern' and the second is not,
16292 and EXTERN_P for the opposite case. */
16293 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
16294 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
16295 /* If an "extern" explicit instantiation follows an ordinary
16296 explicit instantiation, the template is instantiated. */
16300 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
16302 error ("no matching template for %qD found", result
);
16305 else if (!DECL_TEMPLATE_INFO (result
))
16307 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
16311 if (storage
== NULL_TREE
)
16313 else if (storage
== ridpointers
[(int) RID_EXTERN
])
16315 if (!in_system_header
&& (cxx_dialect
== cxx98
))
16316 pedwarn (input_location
, OPT_pedantic
,
16317 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
16322 error ("storage class %qD applied to template instantiation", storage
);
16324 check_explicit_instantiation_namespace (result
);
16325 mark_decl_instantiated (result
, extern_p
);
16327 instantiate_decl (result
, /*defer_ok=*/1,
16328 /*expl_inst_class_mem_p=*/false);
16332 mark_class_instantiated (tree t
, int extern_p
)
16334 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
16335 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
16336 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
16337 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
16340 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
16341 rest_of_type_compilation (t
, 1);
16345 /* Called from do_type_instantiation through binding_table_foreach to
16346 do recursive instantiation for the type bound in ENTRY. */
16348 bt_instantiate_type_proc (binding_entry entry
, void *data
)
16350 tree storage
= *(tree
*) data
;
16352 if (MAYBE_CLASS_TYPE_P (entry
->type
)
16353 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
16354 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
16357 /* Called from do_type_instantiation to instantiate a member
16358 (a member function or a static member variable) of an
16359 explicitly instantiated class template. */
16361 instantiate_class_member (tree decl
, int extern_p
)
16363 mark_decl_instantiated (decl
, extern_p
);
16365 instantiate_decl (decl
, /*defer_ok=*/1,
16366 /*expl_inst_class_mem_p=*/true);
16369 /* Perform an explicit instantiation of template class T. STORAGE, if
16370 non-null, is the RID for extern, inline or static. COMPLAIN is
16371 nonzero if this is called from the parser, zero if called recursively,
16372 since the standard is unclear (as detailed below). */
16375 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
16380 int previous_instantiation_extern_p
= 0;
16382 if (TREE_CODE (t
) == TYPE_DECL
)
16385 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
16387 error ("explicit instantiation of non-template type %qT", t
);
16393 if (!COMPLETE_TYPE_P (t
))
16395 if (complain
& tf_error
)
16396 error ("explicit instantiation of %q#T before definition of template",
16401 if (storage
!= NULL_TREE
)
16403 if (!in_system_header
)
16405 if (storage
== ridpointers
[(int) RID_EXTERN
])
16407 if (cxx_dialect
== cxx98
)
16408 pedwarn (input_location
, OPT_pedantic
,
16409 "ISO C++ 1998 forbids the use of %<extern%> on "
16410 "explicit instantiations");
16413 pedwarn (input_location
, OPT_pedantic
,
16414 "ISO C++ forbids the use of %qE"
16415 " on explicit instantiations", storage
);
16418 if (storage
== ridpointers
[(int) RID_INLINE
])
16420 else if (storage
== ridpointers
[(int) RID_EXTERN
])
16422 else if (storage
== ridpointers
[(int) RID_STATIC
])
16426 error ("storage class %qD applied to template instantiation",
16432 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
16434 /* DR 259 [temp.spec].
16436 Both an explicit instantiation and a declaration of an explicit
16437 specialization shall not appear in a program unless the explicit
16438 instantiation follows a declaration of the explicit specialization.
16440 For a given set of template parameters, if an explicit
16441 instantiation of a template appears after a declaration of an
16442 explicit specialization for that template, the explicit
16443 instantiation has no effect. */
16446 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
16450 No program shall explicitly instantiate any template more
16453 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
16454 instantiation was `extern'. If EXTERN_P then the second is.
16455 These cases are OK. */
16456 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
16458 if (!previous_instantiation_extern_p
&& !extern_p
16459 && (complain
& tf_error
))
16460 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
16462 /* If we've already instantiated the template, just return now. */
16463 if (!CLASSTYPE_INTERFACE_ONLY (t
))
16467 check_explicit_instantiation_namespace (TYPE_NAME (t
));
16468 mark_class_instantiated (t
, extern_p
);
16476 /* In contrast to implicit instantiation, where only the
16477 declarations, and not the definitions, of members are
16478 instantiated, we have here:
16482 The explicit instantiation of a class template specialization
16483 implies the instantiation of all of its members not
16484 previously explicitly specialized in the translation unit
16485 containing the explicit instantiation.
16487 Of course, we can't instantiate member template classes, since
16488 we don't have any arguments for them. Note that the standard
16489 is unclear on whether the instantiation of the members are
16490 *explicit* instantiations or not. However, the most natural
16491 interpretation is that it should be an explicit instantiation. */
16494 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
16495 if (TREE_CODE (tmp
) == FUNCTION_DECL
16496 && DECL_TEMPLATE_INSTANTIATION (tmp
))
16497 instantiate_class_member (tmp
, extern_p
);
16499 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
16500 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
16501 instantiate_class_member (tmp
, extern_p
);
16503 if (CLASSTYPE_NESTED_UTDS (t
))
16504 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
16505 bt_instantiate_type_proc
, &storage
);
16509 /* Given a function DECL, which is a specialization of TMPL, modify
16510 DECL to be a re-instantiation of TMPL with the same template
16511 arguments. TMPL should be the template into which tsubst'ing
16512 should occur for DECL, not the most general template.
16514 One reason for doing this is a scenario like this:
16517 void f(const T&, int i);
16519 void g() { f(3, 7); }
16522 void f(const T& t, const int i) { }
16524 Note that when the template is first instantiated, with
16525 instantiate_template, the resulting DECL will have no name for the
16526 first parameter, and the wrong type for the second. So, when we go
16527 to instantiate the DECL, we regenerate it. */
16530 regenerate_decl_from_template (tree decl
, tree tmpl
)
16532 /* The arguments used to instantiate DECL, from the most general
16537 args
= DECL_TI_ARGS (decl
);
16538 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
16540 /* Make sure that we can see identifiers, and compute access
16542 push_access_scope (decl
);
16544 if (TREE_CODE (decl
) == FUNCTION_DECL
)
16552 args_depth
= TMPL_ARGS_DEPTH (args
);
16553 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
16554 if (args_depth
> parms_depth
)
16555 args
= get_innermost_template_args (args
, parms_depth
);
16557 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
16558 args
, tf_error
, NULL_TREE
);
16560 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
16563 /* Merge parameter declarations. */
16564 decl_parm
= skip_artificial_parms_for (decl
,
16565 DECL_ARGUMENTS (decl
));
16567 = skip_artificial_parms_for (code_pattern
,
16568 DECL_ARGUMENTS (code_pattern
));
16569 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
16574 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
16575 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
16576 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
16578 parm_type
= type_decays_to (parm_type
);
16579 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
16580 TREE_TYPE (decl_parm
) = parm_type
;
16581 attributes
= DECL_ATTRIBUTES (pattern_parm
);
16582 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
16584 DECL_ATTRIBUTES (decl_parm
) = attributes
;
16585 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
16587 decl_parm
= DECL_CHAIN (decl_parm
);
16588 pattern_parm
= DECL_CHAIN (pattern_parm
);
16590 /* Merge any parameters that match with the function parameter
16592 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
16595 tree expanded_types
;
16596 /* Expand the TYPE_PACK_EXPANSION that provides the types for
16597 the parameters in this function parameter pack. */
16598 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
16599 args
, tf_error
, NULL_TREE
);
16600 len
= TREE_VEC_LENGTH (expanded_types
);
16601 for (i
= 0; i
< len
; i
++)
16606 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
16607 /* Rename the parameter to include the index. */
16608 DECL_NAME (decl_parm
) =
16609 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
16610 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
16611 parm_type
= type_decays_to (parm_type
);
16612 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
16613 TREE_TYPE (decl_parm
) = parm_type
;
16614 attributes
= DECL_ATTRIBUTES (pattern_parm
);
16615 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
16617 DECL_ATTRIBUTES (decl_parm
) = attributes
;
16618 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
16620 decl_parm
= DECL_CHAIN (decl_parm
);
16623 /* Merge additional specifiers from the CODE_PATTERN. */
16624 if (DECL_DECLARED_INLINE_P (code_pattern
)
16625 && !DECL_DECLARED_INLINE_P (decl
))
16626 DECL_DECLARED_INLINE_P (decl
) = 1;
16628 else if (TREE_CODE (decl
) == VAR_DECL
)
16630 DECL_INITIAL (decl
) =
16631 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
16632 tf_error
, DECL_TI_TEMPLATE (decl
),
16633 /*integral_constant_expression_p=*/false);
16634 if (VAR_HAD_UNKNOWN_BOUND (decl
))
16635 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
16636 tf_error
, DECL_TI_TEMPLATE (decl
));
16639 gcc_unreachable ();
16641 pop_access_scope (decl
);
16644 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
16645 substituted to get DECL. */
16648 template_for_substitution (tree decl
)
16650 tree tmpl
= DECL_TI_TEMPLATE (decl
);
16652 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
16653 for the instantiation. This is not always the most general
16654 template. Consider, for example:
16657 struct S { template <class U> void f();
16658 template <> void f<int>(); };
16660 and an instantiation of S<double>::f<int>. We want TD to be the
16661 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
16662 while (/* An instantiation cannot have a definition, so we need a
16663 more general template. */
16664 DECL_TEMPLATE_INSTANTIATION (tmpl
)
16665 /* We must also deal with friend templates. Given:
16667 template <class T> struct S {
16668 template <class U> friend void f() {};
16671 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
16672 so far as the language is concerned, but that's still
16673 where we get the pattern for the instantiation from. On
16674 other hand, if the definition comes outside the class, say:
16676 template <class T> struct S {
16677 template <class U> friend void f();
16679 template <class U> friend void f() {}
16681 we don't need to look any further. That's what the check for
16682 DECL_INITIAL is for. */
16683 || (TREE_CODE (decl
) == FUNCTION_DECL
16684 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
16685 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
16687 /* The present template, TD, should not be a definition. If it
16688 were a definition, we should be using it! Note that we
16689 cannot restructure the loop to just keep going until we find
16690 a template with a definition, since that might go too far if
16691 a specialization was declared, but not defined. */
16692 gcc_assert (TREE_CODE (decl
) != VAR_DECL
16693 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
16695 /* Fetch the more general template. */
16696 tmpl
= DECL_TI_TEMPLATE (tmpl
);
16702 /* Returns true if we need to instantiate this template instance even if we
16703 know we aren't going to emit it.. */
16706 always_instantiate_p (tree decl
)
16708 /* We always instantiate inline functions so that we can inline them. An
16709 explicit instantiation declaration prohibits implicit instantiation of
16710 non-inline functions. With high levels of optimization, we would
16711 normally inline non-inline functions -- but we're not allowed to do
16712 that for "extern template" functions. Therefore, we check
16713 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
16714 return ((TREE_CODE (decl
) == FUNCTION_DECL
16715 && DECL_DECLARED_INLINE_P (decl
))
16716 /* And we need to instantiate static data members so that
16717 their initializers are available in integral constant
16719 || (TREE_CODE (decl
) == VAR_DECL
16720 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl
)));
16723 /* Produce the definition of D, a _DECL generated from a template. If
16724 DEFER_OK is nonzero, then we don't have to actually do the
16725 instantiation now; we just have to do it sometime. Normally it is
16726 an error if this is an explicit instantiation but D is undefined.
16727 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
16728 explicitly instantiated class template. */
16731 instantiate_decl (tree d
, int defer_ok
,
16732 bool expl_inst_class_mem_p
)
16734 tree tmpl
= DECL_TI_TEMPLATE (d
);
16741 bool pattern_defined
;
16743 location_t saved_loc
= input_location
;
16746 /* This function should only be used to instantiate templates for
16747 functions and static member variables. */
16748 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
16749 || TREE_CODE (d
) == VAR_DECL
);
16751 /* Variables are never deferred; if instantiation is required, they
16752 are instantiated right away. That allows for better code in the
16753 case that an expression refers to the value of the variable --
16754 if the variable has a constant value the referring expression can
16755 take advantage of that fact. */
16756 if (TREE_CODE (d
) == VAR_DECL
)
16759 /* Don't instantiate cloned functions. Instead, instantiate the
16760 functions they cloned. */
16761 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
16762 d
= DECL_CLONED_FUNCTION (d
);
16764 if (DECL_TEMPLATE_INSTANTIATED (d
)
16765 || DECL_TEMPLATE_SPECIALIZATION (d
))
16766 /* D has already been instantiated or explicitly specialized, so
16767 there's nothing for us to do here.
16769 It might seem reasonable to check whether or not D is an explicit
16770 instantiation, and, if so, stop here. But when an explicit
16771 instantiation is deferred until the end of the compilation,
16772 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
16773 the instantiation. */
16776 /* Check to see whether we know that this template will be
16777 instantiated in some other file, as with "extern template"
16779 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
16781 /* In general, we do not instantiate such templates. */
16782 if (external_p
&& !always_instantiate_p (d
))
16785 gen_tmpl
= most_general_template (tmpl
);
16786 gen_args
= DECL_TI_ARGS (d
);
16788 if (tmpl
!= gen_tmpl
)
16789 /* We should already have the extra args. */
16790 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
16791 == TMPL_ARGS_DEPTH (gen_args
));
16792 /* And what's in the hash table should match D. */
16793 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
16794 || spec
== NULL_TREE
);
16796 /* This needs to happen before any tsubsting. */
16797 if (! push_tinst_level (d
))
16800 timevar_push (TV_PARSE
);
16802 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
16803 for the instantiation. */
16804 td
= template_for_substitution (d
);
16805 code_pattern
= DECL_TEMPLATE_RESULT (td
);
16807 /* We should never be trying to instantiate a member of a class
16808 template or partial specialization. */
16809 gcc_assert (d
!= code_pattern
);
16811 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
16812 || DECL_TEMPLATE_SPECIALIZATION (td
))
16813 /* In the case of a friend template whose definition is provided
16814 outside the class, we may have too many arguments. Drop the
16815 ones we don't need. The same is true for specializations. */
16816 args
= get_innermost_template_args
16817 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
16821 if (TREE_CODE (d
) == FUNCTION_DECL
)
16822 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
);
16824 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
16826 /* We may be in the middle of deferred access check. Disable it now. */
16827 push_deferring_access_checks (dk_no_deferred
);
16829 /* Unless an explicit instantiation directive has already determined
16830 the linkage of D, remember that a definition is available for
16832 if (pattern_defined
16833 && !DECL_INTERFACE_KNOWN (d
)
16834 && !DECL_NOT_REALLY_EXTERN (d
))
16835 mark_definable (d
);
16837 input_location
= DECL_SOURCE_LOCATION (d
);
16839 /* If D is a member of an explicitly instantiated class template,
16840 and no definition is available, treat it like an implicit
16842 if (!pattern_defined
&& expl_inst_class_mem_p
16843 && DECL_EXPLICIT_INSTANTIATION (d
))
16845 DECL_NOT_REALLY_EXTERN (d
) = 0;
16846 DECL_INTERFACE_KNOWN (d
) = 0;
16847 SET_DECL_IMPLICIT_INSTANTIATION (d
);
16850 /* Recheck the substitutions to obtain any warning messages
16851 about ignoring cv qualifiers. Don't do this for artificial decls,
16852 as it breaks the context-sensitive substitution for lambda op(). */
16853 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
16855 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
16856 tree type
= TREE_TYPE (gen
);
16858 /* Make sure that we can see identifiers, and compute access
16859 correctly. D is already the target FUNCTION_DECL with the
16861 push_access_scope (d
);
16863 if (TREE_CODE (gen
) == FUNCTION_DECL
)
16865 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
16866 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
16868 /* Don't simply tsubst the function type, as that will give
16869 duplicate warnings about poor parameter qualifications.
16870 The function arguments are the same as the decl_arguments
16871 without the top level cv qualifiers. */
16872 type
= TREE_TYPE (type
);
16874 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
16876 pop_access_scope (d
);
16879 /* Defer all other templates, unless we have been explicitly
16880 forbidden from doing so. */
16881 if (/* If there is no definition, we cannot instantiate the
16884 /* If it's OK to postpone instantiation, do so. */
16886 /* If this is a static data member that will be defined
16887 elsewhere, we don't want to instantiate the entire data
16888 member, but we do want to instantiate the initializer so that
16889 we can substitute that elsewhere. */
16890 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
16892 /* The definition of the static data member is now required so
16893 we must substitute the initializer. */
16894 if (TREE_CODE (d
) == VAR_DECL
16895 && !DECL_INITIAL (d
)
16896 && DECL_INITIAL (code_pattern
))
16901 ns
= decl_namespace_context (d
);
16902 push_nested_namespace (ns
);
16903 push_nested_class (DECL_CONTEXT (d
));
16904 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
16906 tf_warning_or_error
, NULL_TREE
,
16907 /*integral_constant_expression_p=*/false);
16908 cp_finish_decl (d
, init
, /*init_const_expr_p=*/false,
16909 /*asmspec_tree=*/NULL_TREE
,
16910 LOOKUP_ONLYCONVERTING
);
16911 pop_nested_class ();
16912 pop_nested_namespace (ns
);
16915 /* We restore the source position here because it's used by
16916 add_pending_template. */
16917 input_location
= saved_loc
;
16919 if (at_eof
&& !pattern_defined
16920 && DECL_EXPLICIT_INSTANTIATION (d
)
16921 && DECL_NOT_REALLY_EXTERN (d
))
16924 The definition of a non-exported function template, a
16925 non-exported member function template, or a non-exported
16926 member function or static data member of a class template
16927 shall be present in every translation unit in which it is
16928 explicitly instantiated. */
16929 permerror (input_location
, "explicit instantiation of %qD "
16930 "but no definition available", d
);
16932 /* ??? Historically, we have instantiated inline functions, even
16933 when marked as "extern template". */
16934 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
16935 add_pending_template (d
);
16938 /* Tell the repository that D is available in this translation unit
16939 -- and see if it is supposed to be instantiated here. */
16940 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
16942 /* In a PCH file, despite the fact that the repository hasn't
16943 requested instantiation in the PCH it is still possible that
16944 an instantiation will be required in a file that includes the
16947 add_pending_template (d
);
16948 /* Instantiate inline functions so that the inliner can do its
16949 job, even though we'll not be emitting a copy of this
16951 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
16955 need_push
= !cfun
|| !global_bindings_p ();
16957 push_to_top_level ();
16959 /* Mark D as instantiated so that recursive calls to
16960 instantiate_decl do not try to instantiate it again. */
16961 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
16963 /* Regenerate the declaration in case the template has been modified
16964 by a subsequent redeclaration. */
16965 regenerate_decl_from_template (d
, td
);
16967 /* We already set the file and line above. Reset them now in case
16968 they changed as a result of calling regenerate_decl_from_template. */
16969 input_location
= DECL_SOURCE_LOCATION (d
);
16971 if (TREE_CODE (d
) == VAR_DECL
)
16975 /* Clear out DECL_RTL; whatever was there before may not be right
16976 since we've reset the type of the declaration. */
16977 SET_DECL_RTL (d
, NULL
);
16978 DECL_IN_AGGR_P (d
) = 0;
16980 /* The initializer is placed in DECL_INITIAL by
16981 regenerate_decl_from_template. Pull it out so that
16982 cp_finish_decl can process it. */
16983 init
= DECL_INITIAL (d
);
16984 DECL_INITIAL (d
) = NULL_TREE
;
16985 DECL_INITIALIZED_P (d
) = 0;
16987 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
16988 initializer. That function will defer actual emission until
16989 we have a chance to determine linkage. */
16990 DECL_EXTERNAL (d
) = 0;
16992 /* Enter the scope of D so that access-checking works correctly. */
16993 push_nested_class (DECL_CONTEXT (d
));
16994 cp_finish_decl (d
, init
, false, NULL_TREE
, 0);
16995 pop_nested_class ();
16997 else if (TREE_CODE (d
) == FUNCTION_DECL
)
16999 htab_t saved_local_specializations
;
17004 /* Save away the current list, in case we are instantiating one
17005 template from within the body of another. */
17006 saved_local_specializations
= local_specializations
;
17008 /* Set up the list of local specializations. */
17009 local_specializations
= htab_create (37,
17010 hash_local_specialization
,
17011 eq_local_specializations
,
17014 /* Set up context. */
17015 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
17017 /* Create substitution entries for the parameters. */
17018 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
17019 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
17020 spec_parm
= DECL_ARGUMENTS (d
);
17021 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
17023 register_local_specialization (spec_parm
, tmpl_parm
);
17024 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
17025 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
17027 while (tmpl_parm
&& !FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
17029 register_local_specialization (spec_parm
, tmpl_parm
);
17030 tmpl_parm
= DECL_CHAIN (tmpl_parm
);
17031 spec_parm
= DECL_CHAIN (spec_parm
);
17033 if (tmpl_parm
&& FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
17035 /* Register the (value) argument pack as a specialization of
17036 TMPL_PARM, then move on. */
17037 tree argpack
= make_fnparm_pack (spec_parm
);
17038 register_local_specialization (argpack
, tmpl_parm
);
17039 tmpl_parm
= DECL_CHAIN (tmpl_parm
);
17040 spec_parm
= NULL_TREE
;
17042 gcc_assert (!spec_parm
);
17044 /* Substitute into the body of the function. */
17045 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
17046 tf_warning_or_error
, tmpl
,
17047 /*integral_constant_expression_p=*/false);
17049 /* Set the current input_location to the end of the function
17050 so that finish_function knows where we are. */
17051 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
17053 /* We don't need the local specializations any more. */
17054 htab_delete (local_specializations
);
17055 local_specializations
= saved_local_specializations
;
17057 /* Finish the function. */
17058 d
= finish_function (0);
17059 expand_or_defer_fn (d
);
17062 /* We're not deferring instantiation any more. */
17063 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
17066 pop_from_top_level ();
17069 input_location
= saved_loc
;
17070 pop_deferring_access_checks ();
17071 pop_tinst_level ();
17073 timevar_pop (TV_PARSE
);
17078 /* Run through the list of templates that we wish we could
17079 instantiate, and instantiate any we can. RETRIES is the
17080 number of times we retry pending template instantiation. */
17083 instantiate_pending_templates (int retries
)
17086 location_t saved_loc
= input_location
;
17088 /* Instantiating templates may trigger vtable generation. This in turn
17089 may require further template instantiations. We place a limit here
17090 to avoid infinite loop. */
17091 if (pending_templates
&& retries
>= max_tinst_depth
)
17093 tree decl
= pending_templates
->tinst
->decl
;
17095 error ("template instantiation depth exceeds maximum of %d"
17096 " instantiating %q+D, possibly from virtual table generation"
17097 " (use -ftemplate-depth= to increase the maximum)",
17098 max_tinst_depth
, decl
);
17099 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17100 /* Pretend that we defined it. */
17101 DECL_INITIAL (decl
) = error_mark_node
;
17107 struct pending_template
**t
= &pending_templates
;
17108 struct pending_template
*last
= NULL
;
17112 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
17113 bool complete
= false;
17115 if (TYPE_P (instantiation
))
17119 if (!COMPLETE_TYPE_P (instantiation
))
17121 instantiate_class_template (instantiation
);
17122 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
17123 for (fn
= TYPE_METHODS (instantiation
);
17125 fn
= TREE_CHAIN (fn
))
17126 if (! DECL_ARTIFICIAL (fn
))
17127 instantiate_decl (fn
,
17129 /*expl_inst_class_mem_p=*/false);
17130 if (COMPLETE_TYPE_P (instantiation
))
17134 complete
= COMPLETE_TYPE_P (instantiation
);
17138 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
17139 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
17142 = instantiate_decl (instantiation
,
17144 /*expl_inst_class_mem_p=*/false);
17145 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
17149 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
17150 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
17154 /* If INSTANTIATION has been instantiated, then we don't
17155 need to consider it again in the future. */
17163 current_tinst_level
= NULL
;
17165 last_pending_template
= last
;
17167 while (reconsider
);
17169 input_location
= saved_loc
;
17172 /* Substitute ARGVEC into T, which is a list of initializers for
17173 either base class or a non-static data member. The TREE_PURPOSEs
17174 are DECLs, and the TREE_VALUEs are the initializer values. Used by
17175 instantiate_decl. */
17178 tsubst_initializer_list (tree t
, tree argvec
)
17180 tree inits
= NULL_TREE
;
17182 for (; t
; t
= TREE_CHAIN (t
))
17186 tree expanded_bases
= NULL_TREE
;
17187 tree expanded_arguments
= NULL_TREE
;
17190 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
17195 /* Expand the base class expansion type into separate base
17197 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
17198 tf_warning_or_error
,
17200 if (expanded_bases
== error_mark_node
)
17203 /* We'll be building separate TREE_LISTs of arguments for
17205 len
= TREE_VEC_LENGTH (expanded_bases
);
17206 expanded_arguments
= make_tree_vec (len
);
17207 for (i
= 0; i
< len
; i
++)
17208 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
17210 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
17211 expand each argument in the TREE_VALUE of t. */
17212 expr
= make_node (EXPR_PACK_EXPANSION
);
17213 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
17214 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
17216 if (TREE_VALUE (t
) == void_type_node
)
17217 /* VOID_TYPE_NODE is used to indicate
17218 value-initialization. */
17220 for (i
= 0; i
< len
; i
++)
17221 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
17225 /* Substitute parameter packs into each argument in the
17227 in_base_initializer
= 1;
17228 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
17230 tree expanded_exprs
;
17232 /* Expand the argument. */
17233 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
17235 = tsubst_pack_expansion (expr
, argvec
,
17236 tf_warning_or_error
,
17238 if (expanded_exprs
== error_mark_node
)
17241 /* Prepend each of the expanded expressions to the
17242 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
17243 for (i
= 0; i
< len
; i
++)
17245 TREE_VEC_ELT (expanded_arguments
, i
) =
17246 tree_cons (NULL_TREE
,
17247 TREE_VEC_ELT (expanded_exprs
, i
),
17248 TREE_VEC_ELT (expanded_arguments
, i
));
17251 in_base_initializer
= 0;
17253 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
17254 since we built them backwards. */
17255 for (i
= 0; i
< len
; i
++)
17257 TREE_VEC_ELT (expanded_arguments
, i
) =
17258 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
17263 for (i
= 0; i
< len
; ++i
)
17265 if (expanded_bases
)
17267 decl
= TREE_VEC_ELT (expanded_bases
, i
);
17268 decl
= expand_member_init (decl
);
17269 init
= TREE_VEC_ELT (expanded_arguments
, i
);
17273 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
17274 tf_warning_or_error
, NULL_TREE
);
17276 decl
= expand_member_init (decl
);
17277 if (decl
&& !DECL_P (decl
))
17278 in_base_initializer
= 1;
17280 init
= TREE_VALUE (t
);
17281 if (init
!= void_type_node
)
17282 init
= tsubst_expr (init
, argvec
,
17283 tf_warning_or_error
, NULL_TREE
,
17284 /*integral_constant_expression_p=*/false);
17285 in_base_initializer
= 0;
17290 init
= build_tree_list (decl
, init
);
17291 TREE_CHAIN (init
) = inits
;
17299 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
17302 set_current_access_from_decl (tree decl
)
17304 if (TREE_PRIVATE (decl
))
17305 current_access_specifier
= access_private_node
;
17306 else if (TREE_PROTECTED (decl
))
17307 current_access_specifier
= access_protected_node
;
17309 current_access_specifier
= access_public_node
;
17312 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
17313 is the instantiation (which should have been created with
17314 start_enum) and ARGS are the template arguments to use. */
17317 tsubst_enum (tree tag
, tree newtag
, tree args
)
17321 if (SCOPED_ENUM_P (newtag
))
17322 begin_scope (sk_scoped_enum
, newtag
);
17324 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
17329 decl
= TREE_VALUE (e
);
17330 /* Note that in a template enum, the TREE_VALUE is the
17331 CONST_DECL, not the corresponding INTEGER_CST. */
17332 value
= tsubst_expr (DECL_INITIAL (decl
),
17333 args
, tf_warning_or_error
, NULL_TREE
,
17334 /*integral_constant_expression_p=*/true);
17336 /* Give this enumeration constant the correct access. */
17337 set_current_access_from_decl (decl
);
17339 /* Actually build the enumerator itself. */
17341 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
17344 if (SCOPED_ENUM_P (newtag
))
17347 finish_enum_value_list (newtag
);
17348 finish_enum (newtag
);
17350 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
17351 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
17354 /* DECL is a FUNCTION_DECL that is a template specialization. Return
17355 its type -- but without substituting the innermost set of template
17356 arguments. So, innermost set of template parameters will appear in
17360 get_mostly_instantiated_function_type (tree decl
)
17368 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
17369 targs
= DECL_TI_ARGS (decl
);
17370 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
17371 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
17373 /* There should be as many levels of arguments as there are levels
17375 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
17377 fn_type
= TREE_TYPE (tmpl
);
17379 if (parm_depth
== 1)
17380 /* No substitution is necessary. */
17384 int i
, save_access_control
;
17387 /* Replace the innermost level of the TARGS with NULL_TREEs to
17388 let tsubst know not to substitute for those parameters. */
17389 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
17390 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
17391 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
17392 TMPL_ARGS_LEVEL (targs
, i
));
17393 SET_TMPL_ARGS_LEVEL (partial_args
,
17394 TMPL_ARGS_DEPTH (targs
),
17395 make_tree_vec (DECL_NTPARMS (tmpl
)));
17397 /* Disable access control as this function is used only during
17399 save_access_control
= flag_access_control
;
17400 flag_access_control
= 0;
17402 ++processing_template_decl
;
17403 /* Now, do the (partial) substitution to figure out the
17404 appropriate function type. */
17405 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
17406 --processing_template_decl
;
17408 /* Substitute into the template parameters to obtain the real
17409 innermost set of parameters. This step is important if the
17410 innermost set of template parameters contains value
17411 parameters whose types depend on outer template parameters. */
17412 TREE_VEC_LENGTH (partial_args
)--;
17413 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
17415 flag_access_control
= save_access_control
;
17421 /* Return truthvalue if we're processing a template different from
17422 the last one involved in diagnostics. */
17424 problematic_instantiation_changed (void)
17426 return last_template_error_tick
!= tinst_level_tick
;
17429 /* Remember current template involved in diagnostics. */
17431 record_last_problematic_instantiation (void)
17433 last_template_error_tick
= tinst_level_tick
;
17436 struct tinst_level
*
17437 current_instantiation (void)
17439 return current_tinst_level
;
17442 /* [temp.param] Check that template non-type parm TYPE is of an allowable
17443 type. Return zero for ok, nonzero for disallowed. Issue error and
17444 warning messages under control of COMPLAIN. */
17447 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
17449 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
17451 else if (POINTER_TYPE_P (type
))
17453 else if (TYPE_PTR_TO_MEMBER_P (type
))
17455 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
17457 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
17460 if (complain
& tf_error
)
17461 error ("%q#T is not a valid type for a template constant parameter", type
);
17465 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
17466 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
17469 dependent_type_p_r (tree type
)
17475 A type is dependent if it is:
17477 -- a template parameter. Template template parameters are types
17478 for us (since TYPE_P holds true for them) so we handle
17480 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
17481 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
17483 /* -- a qualified-id with a nested-name-specifier which contains a
17484 class-name that names a dependent type or whose unqualified-id
17485 names a dependent type. */
17486 if (TREE_CODE (type
) == TYPENAME_TYPE
)
17488 /* -- a cv-qualified type where the cv-unqualified type is
17490 type
= TYPE_MAIN_VARIANT (type
);
17491 /* -- a compound type constructed from any dependent type. */
17492 if (TYPE_PTR_TO_MEMBER_P (type
))
17493 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
17494 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
17496 else if (TREE_CODE (type
) == POINTER_TYPE
17497 || TREE_CODE (type
) == REFERENCE_TYPE
)
17498 return dependent_type_p (TREE_TYPE (type
));
17499 else if (TREE_CODE (type
) == FUNCTION_TYPE
17500 || TREE_CODE (type
) == METHOD_TYPE
)
17504 if (dependent_type_p (TREE_TYPE (type
)))
17506 for (arg_type
= TYPE_ARG_TYPES (type
);
17508 arg_type
= TREE_CHAIN (arg_type
))
17509 if (dependent_type_p (TREE_VALUE (arg_type
)))
17513 /* -- an array type constructed from any dependent type or whose
17514 size is specified by a constant expression that is
17517 We checked for type- and value-dependence of the bounds in
17518 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
17519 if (TREE_CODE (type
) == ARRAY_TYPE
)
17521 if (TYPE_DOMAIN (type
)
17522 && dependent_type_p (TYPE_DOMAIN (type
)))
17524 return dependent_type_p (TREE_TYPE (type
));
17527 /* -- a template-id in which either the template name is a template
17529 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
17531 /* ... or any of the template arguments is a dependent type or
17532 an expression that is type-dependent or value-dependent. */
17533 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
17534 && (any_dependent_template_arguments_p
17535 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
17538 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
17539 argument of the `typeof' expression is not type-dependent, then
17540 it should already been have resolved. */
17541 if (TREE_CODE (type
) == TYPEOF_TYPE
17542 || TREE_CODE (type
) == DECLTYPE_TYPE
)
17545 /* A template argument pack is dependent if any of its packed
17547 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
17549 tree args
= ARGUMENT_PACK_ARGS (type
);
17550 int i
, len
= TREE_VEC_LENGTH (args
);
17551 for (i
= 0; i
< len
; ++i
)
17552 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
17556 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
17557 be template parameters. */
17558 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
17561 /* The standard does not specifically mention types that are local
17562 to template functions or local classes, but they should be
17563 considered dependent too. For example:
17565 template <int I> void f() {
17570 The size of `E' cannot be known until the value of `I' has been
17571 determined. Therefore, `E' must be considered dependent. */
17572 scope
= TYPE_CONTEXT (type
);
17573 if (scope
&& TYPE_P (scope
))
17574 return dependent_type_p (scope
);
17575 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
17576 return type_dependent_expression_p (scope
);
17578 /* Other types are non-dependent. */
17582 /* Returns TRUE if TYPE is dependent, in the sense of
17583 [temp.dep.type]. */
17586 dependent_type_p (tree type
)
17588 /* If there are no template parameters in scope, then there can't be
17589 any dependent types. */
17590 if (!processing_template_decl
)
17592 /* If we are not processing a template, then nobody should be
17593 providing us with a dependent type. */
17595 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
17599 /* If the type is NULL, we have not computed a type for the entity
17600 in question; in that case, the type is dependent. */
17604 /* Erroneous types can be considered non-dependent. */
17605 if (type
== error_mark_node
)
17608 /* If we have not already computed the appropriate value for TYPE,
17610 if (!TYPE_DEPENDENT_P_VALID (type
))
17612 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
17613 TYPE_DEPENDENT_P_VALID (type
) = 1;
17616 return TYPE_DEPENDENT_P (type
);
17619 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
17620 lookup. In other words, a dependent type that is not the current
17624 dependent_scope_p (tree scope
)
17626 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
17627 && !currently_open_class (scope
));
17630 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
17631 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
17635 value_dependent_expression_p (tree expression
)
17637 if (!processing_template_decl
)
17640 /* A name declared with a dependent type. */
17641 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
17644 switch (TREE_CODE (expression
))
17646 case IDENTIFIER_NODE
:
17647 /* A name that has not been looked up -- must be dependent. */
17650 case TEMPLATE_PARM_INDEX
:
17651 /* A non-type template parm. */
17655 /* A non-type template parm. */
17656 if (DECL_TEMPLATE_PARM_P (expression
))
17658 return value_dependent_expression_p (DECL_INITIAL (expression
));
17661 /* A constant with integral or enumeration type and is initialized
17662 with an expression that is value-dependent. */
17663 if (DECL_INITIAL (expression
)
17664 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression
))
17665 && value_dependent_expression_p (DECL_INITIAL (expression
)))
17669 case DYNAMIC_CAST_EXPR
:
17670 case STATIC_CAST_EXPR
:
17671 case CONST_CAST_EXPR
:
17672 case REINTERPRET_CAST_EXPR
:
17674 /* These expressions are value-dependent if the type to which
17675 the cast occurs is dependent or the expression being casted
17676 is value-dependent. */
17678 tree type
= TREE_TYPE (expression
);
17680 if (dependent_type_p (type
))
17683 /* A functional cast has a list of operands. */
17684 expression
= TREE_OPERAND (expression
, 0);
17687 /* If there are no operands, it must be an expression such
17688 as "int()". This should not happen for aggregate types
17689 because it would form non-constant expressions. */
17690 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
17695 if (TREE_CODE (expression
) == TREE_LIST
)
17696 return any_value_dependent_elements_p (expression
);
17698 return value_dependent_expression_p (expression
);
17703 /* A `sizeof' expression is value-dependent if the operand is
17704 type-dependent or is a pack expansion. */
17705 expression
= TREE_OPERAND (expression
, 0);
17706 if (PACK_EXPANSION_P (expression
))
17708 else if (TYPE_P (expression
))
17709 return dependent_type_p (expression
);
17710 return type_dependent_expression_p (expression
);
17712 case AT_ENCODE_EXPR
:
17713 /* An 'encode' expression is value-dependent if the operand is
17715 expression
= TREE_OPERAND (expression
, 0);
17716 return dependent_type_p (expression
);
17718 case NOEXCEPT_EXPR
:
17719 expression
= TREE_OPERAND (expression
, 0);
17720 /* FIXME why check value-dependency? */
17721 return (type_dependent_expression_p (expression
)
17722 || value_dependent_expression_p (expression
));
17726 tree name
= TREE_OPERAND (expression
, 1);
17727 return value_dependent_expression_p (name
);
17730 case COMPONENT_REF
:
17731 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
17732 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
17735 /* A CALL_EXPR may appear in a constant expression if it is a
17736 call to a builtin function, e.g., __builtin_constant_p. All
17737 such calls are value-dependent. */
17740 case NONTYPE_ARGUMENT_PACK
:
17741 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
17742 is value-dependent. */
17744 tree values
= ARGUMENT_PACK_ARGS (expression
);
17745 int i
, len
= TREE_VEC_LENGTH (values
);
17747 for (i
= 0; i
< len
; ++i
)
17748 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
17756 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
17757 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
17758 || (type2
? dependent_type_p (type2
) : false));
17762 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
17763 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
17767 tree op
= TREE_OPERAND (expression
, 0);
17768 return (value_dependent_expression_p (op
)
17769 || has_value_dependent_address (op
));
17773 /* A constant expression is value-dependent if any subexpression is
17774 value-dependent. */
17775 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
17777 case tcc_reference
:
17779 return (value_dependent_expression_p
17780 (TREE_OPERAND (expression
, 0)));
17782 case tcc_comparison
:
17784 return ((value_dependent_expression_p
17785 (TREE_OPERAND (expression
, 0)))
17786 || (value_dependent_expression_p
17787 (TREE_OPERAND (expression
, 1))));
17789 case tcc_expression
:
17793 for (i
= 0; i
< TREE_OPERAND_LENGTH (expression
); ++i
)
17794 /* In some cases, some of the operands may be missing.
17795 (For example, in the case of PREDECREMENT_EXPR, the
17796 amount to increment by may be missing.) That doesn't
17797 make the expression dependent. */
17798 if (TREE_OPERAND (expression
, i
)
17799 && (value_dependent_expression_p
17800 (TREE_OPERAND (expression
, i
))))
17810 /* The expression is not value-dependent. */
17814 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
17815 [temp.dep.expr]. */
17818 type_dependent_expression_p (tree expression
)
17820 if (!processing_template_decl
)
17823 if (expression
== error_mark_node
)
17826 /* An unresolved name is always dependent. */
17827 if (TREE_CODE (expression
) == IDENTIFIER_NODE
17828 || TREE_CODE (expression
) == USING_DECL
)
17831 /* Some expression forms are never type-dependent. */
17832 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
17833 || TREE_CODE (expression
) == SIZEOF_EXPR
17834 || TREE_CODE (expression
) == ALIGNOF_EXPR
17835 || TREE_CODE (expression
) == AT_ENCODE_EXPR
17836 || TREE_CODE (expression
) == NOEXCEPT_EXPR
17837 || TREE_CODE (expression
) == TRAIT_EXPR
17838 || TREE_CODE (expression
) == TYPEID_EXPR
17839 || TREE_CODE (expression
) == DELETE_EXPR
17840 || TREE_CODE (expression
) == VEC_DELETE_EXPR
17841 || TREE_CODE (expression
) == THROW_EXPR
)
17844 /* The types of these expressions depends only on the type to which
17845 the cast occurs. */
17846 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
17847 || TREE_CODE (expression
) == STATIC_CAST_EXPR
17848 || TREE_CODE (expression
) == CONST_CAST_EXPR
17849 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
17850 || TREE_CODE (expression
) == CAST_EXPR
)
17851 return dependent_type_p (TREE_TYPE (expression
));
17853 /* The types of these expressions depends only on the type created
17854 by the expression. */
17855 if (TREE_CODE (expression
) == NEW_EXPR
17856 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
17858 /* For NEW_EXPR tree nodes created inside a template, either
17859 the object type itself or a TREE_LIST may appear as the
17861 tree type
= TREE_OPERAND (expression
, 1);
17862 if (TREE_CODE (type
) == TREE_LIST
)
17863 /* This is an array type. We need to check array dimensions
17865 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
17866 || value_dependent_expression_p
17867 (TREE_OPERAND (TREE_VALUE (type
), 1));
17869 return dependent_type_p (type
);
17872 if (TREE_CODE (expression
) == SCOPE_REF
)
17874 tree scope
= TREE_OPERAND (expression
, 0);
17875 tree name
= TREE_OPERAND (expression
, 1);
17877 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
17878 contains an identifier associated by name lookup with one or more
17879 declarations declared with a dependent type, or...a
17880 nested-name-specifier or qualified-id that names a member of an
17881 unknown specialization. */
17882 return (type_dependent_expression_p (name
)
17883 || dependent_scope_p (scope
));
17886 if (TREE_CODE (expression
) == FUNCTION_DECL
17887 && DECL_LANG_SPECIFIC (expression
)
17888 && DECL_TEMPLATE_INFO (expression
)
17889 && (any_dependent_template_arguments_p
17890 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
17893 if (TREE_CODE (expression
) == TEMPLATE_DECL
17894 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
17897 if (TREE_CODE (expression
) == STMT_EXPR
)
17898 expression
= stmt_expr_value_expr (expression
);
17900 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
17905 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
17907 if (type_dependent_expression_p (elt
))
17913 /* A static data member of the current instantiation with incomplete
17914 array type is type-dependent, as the definition and specializations
17915 can have different bounds. */
17916 if (TREE_CODE (expression
) == VAR_DECL
17917 && DECL_CLASS_SCOPE_P (expression
)
17918 && dependent_type_p (DECL_CONTEXT (expression
))
17919 && VAR_HAD_UNKNOWN_BOUND (expression
))
17922 if (TREE_TYPE (expression
) == unknown_type_node
)
17924 if (TREE_CODE (expression
) == ADDR_EXPR
)
17925 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
17926 if (TREE_CODE (expression
) == COMPONENT_REF
17927 || TREE_CODE (expression
) == OFFSET_REF
)
17929 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
17931 expression
= TREE_OPERAND (expression
, 1);
17932 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
17935 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
17936 if (TREE_CODE (expression
) == SCOPE_REF
)
17939 if (TREE_CODE (expression
) == BASELINK
)
17940 expression
= BASELINK_FUNCTIONS (expression
);
17942 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
17944 if (any_dependent_template_arguments_p
17945 (TREE_OPERAND (expression
, 1)))
17947 expression
= TREE_OPERAND (expression
, 0);
17949 gcc_assert (TREE_CODE (expression
) == OVERLOAD
17950 || TREE_CODE (expression
) == FUNCTION_DECL
);
17954 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
17956 expression
= OVL_NEXT (expression
);
17961 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
17963 return (dependent_type_p (TREE_TYPE (expression
)));
17966 /* Like type_dependent_expression_p, but it also works while not processing
17967 a template definition, i.e. during substitution or mangling. */
17970 type_dependent_expression_p_push (tree expr
)
17973 ++processing_template_decl
;
17974 b
= type_dependent_expression_p (expr
);
17975 --processing_template_decl
;
17979 /* Returns TRUE if ARGS contains a type-dependent expression. */
17982 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
17987 FOR_EACH_VEC_ELT (tree
, args
, i
, arg
)
17989 if (type_dependent_expression_p (arg
))
17995 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
17996 expressions) contains any value-dependent expressions. */
17999 any_value_dependent_elements_p (const_tree list
)
18001 for (; list
; list
= TREE_CHAIN (list
))
18002 if (value_dependent_expression_p (TREE_VALUE (list
)))
18008 /* Returns TRUE if the ARG (a template argument) is dependent. */
18011 dependent_template_arg_p (tree arg
)
18013 if (!processing_template_decl
)
18016 if (TREE_CODE (arg
) == TEMPLATE_DECL
18017 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
18018 return dependent_template_p (arg
);
18019 else if (ARGUMENT_PACK_P (arg
))
18021 tree args
= ARGUMENT_PACK_ARGS (arg
);
18022 int i
, len
= TREE_VEC_LENGTH (args
);
18023 for (i
= 0; i
< len
; ++i
)
18025 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
18031 else if (TYPE_P (arg
))
18032 return dependent_type_p (arg
);
18034 return (type_dependent_expression_p (arg
)
18035 || value_dependent_expression_p (arg
));
18038 /* Returns true if ARGS (a collection of template arguments) contains
18039 any types that require structural equality testing. */
18042 any_template_arguments_need_structural_equality_p (tree args
)
18049 if (args
== error_mark_node
)
18052 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
18054 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
18055 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
18057 tree arg
= TREE_VEC_ELT (level
, j
);
18058 tree packed_args
= NULL_TREE
;
18061 if (ARGUMENT_PACK_P (arg
))
18063 /* Look inside the argument pack. */
18064 packed_args
= ARGUMENT_PACK_ARGS (arg
);
18065 len
= TREE_VEC_LENGTH (packed_args
);
18068 for (k
= 0; k
< len
; ++k
)
18071 arg
= TREE_VEC_ELT (packed_args
, k
);
18073 if (error_operand_p (arg
))
18075 else if (TREE_CODE (arg
) == TEMPLATE_DECL
18076 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
18078 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
18080 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
18081 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
18090 /* Returns true if ARGS (a collection of template arguments) contains
18091 any dependent arguments. */
18094 any_dependent_template_arguments_p (const_tree args
)
18101 if (args
== error_mark_node
)
18104 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
18106 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
18107 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
18108 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
18115 /* Returns TRUE if the template TMPL is dependent. */
18118 dependent_template_p (tree tmpl
)
18120 if (TREE_CODE (tmpl
) == OVERLOAD
)
18124 if (dependent_template_p (OVL_FUNCTION (tmpl
)))
18126 tmpl
= OVL_CHAIN (tmpl
);
18131 /* Template template parameters are dependent. */
18132 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
18133 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
18135 /* So are names that have not been looked up. */
18136 if (TREE_CODE (tmpl
) == SCOPE_REF
18137 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
18139 /* So are member templates of dependent classes. */
18140 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
18141 return dependent_type_p (DECL_CONTEXT (tmpl
));
18145 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
18148 dependent_template_id_p (tree tmpl
, tree args
)
18150 return (dependent_template_p (tmpl
)
18151 || any_dependent_template_arguments_p (args
));
18154 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
18158 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
18162 if (!processing_template_decl
)
18165 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
18167 tree decl
= TREE_VEC_ELT (declv
, i
);
18168 tree init
= TREE_VEC_ELT (initv
, i
);
18169 tree cond
= TREE_VEC_ELT (condv
, i
);
18170 tree incr
= TREE_VEC_ELT (incrv
, i
);
18172 if (type_dependent_expression_p (decl
))
18175 if (init
&& type_dependent_expression_p (init
))
18178 if (type_dependent_expression_p (cond
))
18181 if (COMPARISON_CLASS_P (cond
)
18182 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
18183 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
18186 if (TREE_CODE (incr
) == MODOP_EXPR
)
18188 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
18189 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
18192 else if (type_dependent_expression_p (incr
))
18194 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
18196 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
18198 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
18200 tree t
= TREE_OPERAND (incr
, 1);
18201 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
18202 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
18211 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
18212 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
18213 no such TYPE can be found. Note that this function peers inside
18214 uninstantiated templates and therefore should be used only in
18215 extremely limited situations. ONLY_CURRENT_P restricts this
18216 peering to the currently open classes hierarchy (which is required
18217 when comparing types). */
18220 resolve_typename_type (tree type
, bool only_current_p
)
18229 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
18231 scope
= TYPE_CONTEXT (type
);
18232 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
18233 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
18234 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
18235 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
18236 identifier of the TYPENAME_TYPE anymore.
18237 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
18238 TYPENAME_TYPE instead, we avoid messing up with a possible
18239 typedef variant case. */
18240 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
18242 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
18243 it first before we can figure out what NAME refers to. */
18244 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
18245 scope
= resolve_typename_type (scope
, only_current_p
);
18246 /* If we don't know what SCOPE refers to, then we cannot resolve the
18248 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
18250 /* If the SCOPE is a template type parameter, we have no way of
18251 resolving the name. */
18252 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
18254 /* If the SCOPE is not the current instantiation, there's no reason
18255 to look inside it. */
18256 if (only_current_p
&& !currently_open_class (scope
))
18258 /* If this is a typedef, we don't want to look inside (c++/11987). */
18259 if (typedef_variant_p (type
))
18261 /* If SCOPE isn't the template itself, it will not have a valid
18262 TYPE_FIELDS list. */
18263 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
18264 /* scope is either the template itself or a compatible instantiation
18265 like X<T>, so look up the name in the original template. */
18266 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
18268 /* scope is a partial instantiation, so we can't do the lookup or we
18269 will lose the template arguments. */
18271 /* Enter the SCOPE so that name lookup will be resolved as if we
18272 were in the class definition. In particular, SCOPE will no
18273 longer be considered a dependent type. */
18274 pushed_scope
= push_scope (scope
);
18275 /* Look up the declaration. */
18276 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
18278 result
= NULL_TREE
;
18280 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
18281 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
18284 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
18285 && TREE_CODE (decl
) == TYPE_DECL
)
18287 result
= TREE_TYPE (decl
);
18288 if (result
== error_mark_node
)
18289 result
= NULL_TREE
;
18291 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
18292 && DECL_CLASS_TEMPLATE_P (decl
))
18296 /* Obtain the template and the arguments. */
18297 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
18298 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
18299 /* Instantiate the template. */
18300 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
18301 /*entering_scope=*/0,
18302 tf_error
| tf_user
);
18303 if (result
== error_mark_node
)
18304 result
= NULL_TREE
;
18307 /* Leave the SCOPE. */
18309 pop_scope (pushed_scope
);
18311 /* If we failed to resolve it, return the original typename. */
18315 /* If lookup found a typename type, resolve that too. */
18316 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
18318 /* Ill-formed programs can cause infinite recursion here, so we
18319 must catch that. */
18320 TYPENAME_IS_RESOLVING_P (type
) = 1;
18321 result
= resolve_typename_type (result
, only_current_p
);
18322 TYPENAME_IS_RESOLVING_P (type
) = 0;
18325 /* Qualify the resulting type. */
18326 quals
= cp_type_quals (type
);
18328 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
18333 /* EXPR is an expression which is not type-dependent. Return a proxy
18334 for EXPR that can be used to compute the types of larger
18335 expressions containing EXPR. */
18338 build_non_dependent_expr (tree expr
)
18342 /* Preserve null pointer constants so that the type of things like
18343 "p == 0" where "p" is a pointer can be determined. */
18344 if (null_ptr_cst_p (expr
))
18346 /* Preserve OVERLOADs; the functions must be available to resolve
18349 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
18350 inner_expr
= stmt_expr_value_expr (inner_expr
);
18351 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
18352 inner_expr
= TREE_OPERAND (inner_expr
, 0);
18353 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
18354 inner_expr
= TREE_OPERAND (inner_expr
, 1);
18355 if (is_overloaded_fn (inner_expr
)
18356 || TREE_CODE (inner_expr
) == OFFSET_REF
)
18358 /* There is no need to return a proxy for a variable. */
18359 if (TREE_CODE (expr
) == VAR_DECL
)
18361 /* Preserve string constants; conversions from string constants to
18362 "char *" are allowed, even though normally a "const char *"
18363 cannot be used to initialize a "char *". */
18364 if (TREE_CODE (expr
) == STRING_CST
)
18366 /* Preserve arithmetic constants, as an optimization -- there is no
18367 reason to create a new node. */
18368 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
18370 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
18371 There is at least one place where we want to know that a
18372 particular expression is a throw-expression: when checking a ?:
18373 expression, there are special rules if the second or third
18374 argument is a throw-expression. */
18375 if (TREE_CODE (expr
) == THROW_EXPR
)
18378 if (TREE_CODE (expr
) == COND_EXPR
)
18379 return build3 (COND_EXPR
,
18381 TREE_OPERAND (expr
, 0),
18382 (TREE_OPERAND (expr
, 1)
18383 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
18384 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
18385 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
18386 if (TREE_CODE (expr
) == COMPOUND_EXPR
18387 && !COMPOUND_EXPR_OVERLOADED (expr
))
18388 return build2 (COMPOUND_EXPR
,
18390 TREE_OPERAND (expr
, 0),
18391 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
18393 /* If the type is unknown, it can't really be non-dependent */
18394 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
18396 /* Otherwise, build a NON_DEPENDENT_EXPR.
18398 REFERENCE_TYPEs are not stripped for expressions in templates
18399 because doing so would play havoc with mangling. Consider, for
18402 template <typename T> void f<T& g>() { g(); }
18404 In the body of "f", the expression for "g" will have
18405 REFERENCE_TYPE, even though the standard says that it should
18406 not. The reason is that we must preserve the syntactic form of
18407 the expression so that mangling (say) "f<g>" inside the body of
18408 "f" works out correctly. Therefore, the REFERENCE_TYPE is
18410 return build1 (NON_DEPENDENT_EXPR
, non_reference (TREE_TYPE (expr
)), expr
);
18413 /* ARGS is a vector of expressions as arguments to a function call.
18414 Replace the arguments with equivalent non-dependent expressions.
18415 This modifies ARGS in place. */
18418 make_args_non_dependent (VEC(tree
,gc
) *args
)
18423 FOR_EACH_VEC_ELT (tree
, args
, ix
, arg
)
18425 tree newarg
= build_non_dependent_expr (arg
);
18427 VEC_replace (tree
, args
, ix
, newarg
);
18431 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
18432 with a level one deeper than the actual template parms. */
18437 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
18438 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
18439 TYPE_DECL
, get_identifier ("auto"), au
);
18440 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
18441 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
18442 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
18443 TYPE_NAME (au
), NULL_TREE
);
18444 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
18445 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
18446 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
18451 /* Given type ARG, return std::initializer_list<ARG>. */
18456 tree std_init_list
= namespace_binding
18457 (get_identifier ("initializer_list"), std_node
);
18459 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
18461 error ("deducing from brace-enclosed initializer list requires "
18462 "#include <initializer_list>");
18463 return error_mark_node
;
18465 argvec
= make_tree_vec (1);
18466 TREE_VEC_ELT (argvec
, 0) = arg
;
18467 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
18468 NULL_TREE
, 0, tf_warning_or_error
);
18471 /* Replace auto in TYPE with std::initializer_list<auto>. */
18474 listify_autos (tree type
, tree auto_node
)
18476 tree init_auto
= listify (auto_node
);
18477 tree argvec
= make_tree_vec (1);
18478 TREE_VEC_ELT (argvec
, 0) = init_auto
;
18479 if (processing_template_decl
)
18480 argvec
= add_to_template_args (current_template_args (), argvec
);
18481 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
18484 /* walk_tree helper for do_auto_deduction. */
18487 contains_auto_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
18490 /* Is this a variable with the type we're looking for? */
18492 && TREE_TYPE (*tp
) == type
)
18498 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
18499 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
18502 do_auto_deduction (tree type
, tree init
, tree auto_node
)
18504 tree parms
, tparms
, targs
;
18509 /* The name of the object being declared shall not appear in the
18510 initializer expression. */
18511 decl
= cp_walk_tree_without_duplicates (&init
, contains_auto_r
, type
);
18514 error ("variable %q#D with %<auto%> type used in its own "
18515 "initializer", decl
);
18516 return error_mark_node
;
18519 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
18520 with either a new invented type template parameter U or, if the
18521 initializer is a braced-init-list (8.5.4), with
18522 std::initializer_list<U>. */
18523 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
18524 type
= listify_autos (type
, auto_node
);
18526 parms
= build_tree_list (NULL_TREE
, type
);
18528 tparms
= make_tree_vec (1);
18529 targs
= make_tree_vec (1);
18530 TREE_VEC_ELT (tparms
, 0)
18531 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
18532 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
18533 DEDUCE_CALL
, LOOKUP_NORMAL
);
18536 error ("unable to deduce %qT from %qE", type
, init
);
18537 return error_mark_node
;
18540 /* If the list of declarators contains more than one declarator, the type
18541 of each declared variable is determined as described above. If the
18542 type deduced for the template parameter U is not the same in each
18543 deduction, the program is ill-formed. */
18544 if (TREE_TYPE (auto_node
)
18545 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
18547 error ("inconsistent deduction for %qT: %qT and then %qT",
18548 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
18549 return error_mark_node
;
18551 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
18553 if (processing_template_decl
)
18554 targs
= add_to_template_args (current_template_args (), targs
);
18555 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
18558 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
18562 splice_late_return_type (tree type
, tree late_return_type
)
18566 if (late_return_type
== NULL_TREE
)
18568 argvec
= make_tree_vec (1);
18569 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
18570 if (processing_template_decl
)
18571 argvec
= add_to_template_args (current_template_args (), argvec
);
18572 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
18575 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
18578 is_auto (const_tree type
)
18580 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18581 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
18587 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
18588 appear as a type-specifier for the declaration in question, we don't
18589 have to look through the whole type. */
18592 type_uses_auto (tree type
)
18594 enum tree_code code
;
18595 if (is_auto (type
))
18598 code
= TREE_CODE (type
);
18600 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
18601 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
18602 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
18603 return type_uses_auto (TREE_TYPE (type
));
18605 if (TYPE_PTRMEMFUNC_P (type
))
18606 return type_uses_auto (TREE_TYPE (TREE_TYPE
18607 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
18612 /* For a given template T, return the vector of typedefs referenced
18613 in T for which access check is needed at T instantiation time.
18614 T is either a FUNCTION_DECL or a RECORD_TYPE.
18615 Those typedefs were added to T by the function
18616 append_type_to_template_for_access_check. */
18618 VEC(qualified_typedef_usage_t
,gc
)*
18619 get_types_needing_access_check (tree t
)
18622 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
18624 if (!t
|| t
== error_mark_node
)
18627 if (!(ti
= get_template_info (t
)))
18630 if (CLASS_TYPE_P (t
)
18631 || TREE_CODE (t
) == FUNCTION_DECL
)
18633 if (!TI_TEMPLATE (ti
))
18636 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
18642 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
18643 tied to T. That list of typedefs will be access checked at
18644 T instantiation time.
18645 T is either a FUNCTION_DECL or a RECORD_TYPE.
18646 TYPE_DECL is a TYPE_DECL node representing a typedef.
18647 SCOPE is the scope through which TYPE_DECL is accessed.
18648 LOCATION is the location of the usage point of TYPE_DECL.
18650 This function is a subroutine of
18651 append_type_to_template_for_access_check. */
18654 append_type_to_template_for_access_check_1 (tree t
,
18657 location_t location
)
18659 qualified_typedef_usage_t typedef_usage
;
18662 if (!t
|| t
== error_mark_node
)
18665 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
18666 || CLASS_TYPE_P (t
))
18668 && TREE_CODE (type_decl
) == TYPE_DECL
18671 if (!(ti
= get_template_info (t
)))
18674 gcc_assert (TI_TEMPLATE (ti
));
18676 typedef_usage
.typedef_decl
= type_decl
;
18677 typedef_usage
.context
= scope
;
18678 typedef_usage
.locus
= location
;
18680 VEC_safe_push (qualified_typedef_usage_t
, gc
,
18681 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
18685 /* Append TYPE_DECL to the template TEMPL.
18686 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
18687 At TEMPL instanciation time, TYPE_DECL will be checked to see
18688 if it can be accessed through SCOPE.
18689 LOCATION is the location of the usage point of TYPE_DECL.
18691 e.g. consider the following code snippet:
18698 template<class U> struct S
18700 C::myint mi; // <-- usage point of the typedef C::myint
18705 At S<char> instantiation time, we need to check the access of C::myint
18706 In other words, we need to check the access of the myint typedef through
18707 the C scope. For that purpose, this function will add the myint typedef
18708 and the scope C through which its being accessed to a list of typedefs
18709 tied to the template S. That list will be walked at template instantiation
18710 time and access check performed on each typedefs it contains.
18711 Note that this particular code snippet should yield an error because
18712 myint is private to C. */
18715 append_type_to_template_for_access_check (tree templ
,
18718 location_t location
)
18720 qualified_typedef_usage_t
*iter
;
18723 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
18725 /* Make sure we don't append the type to the template twice. */
18726 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
18727 get_types_needing_access_check (templ
),
18729 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
18732 append_type_to_template_for_access_check_1 (templ
, type_decl
,
18736 /* Set up the hash tables for template instantiations. */
18739 init_template_processing (void)
18741 decl_specializations
= htab_create_ggc (37,
18742 hash_specialization
,
18743 eq_specializations
,
18745 type_specializations
= htab_create_ggc (37,
18746 hash_specialization
,
18747 eq_specializations
,
18751 /* Print stats about the template hash tables for -fstats. */
18754 print_template_statistics (void)
18756 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
18757 "%f collisions\n", (long) htab_size (decl_specializations
),
18758 (long) htab_elements (decl_specializations
),
18759 htab_collisions (decl_specializations
));
18760 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
18761 "%f collisions\n", (long) htab_size (type_specializations
),
18762 (long) htab_elements (type_specializations
),
18763 htab_collisions (type_specializations
));
18766 #include "gt-cp-pt.h"