1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t
) (tree
, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
{
57 struct pending_template
*next
;
58 struct tinst_level
*tinst
;
61 static GTY(()) struct pending_template
*pending_templates
;
62 static GTY(()) struct pending_template
*last_pending_template
;
64 int processing_template_parmlist
;
65 static int template_header_count
;
67 static GTY(()) tree saved_trees
;
68 static VEC(int,heap
) *inline_parm_levels
;
70 static GTY(()) struct tinst_level
*current_tinst_level
;
72 static GTY(()) tree saved_access_scope
;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr
;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
82 static htab_t local_specializations
;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry
)))
92 htab_t decl_specializations
;
94 static GTY ((param_is (spec_entry
)))
95 htab_t type_specializations
;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 enum template_base_result
{
114 tbr_ambiguous_baseclass
,
118 static void push_access_scope (tree
);
119 static void pop_access_scope (tree
);
120 static void push_deduction_access_scope (tree
);
121 static void pop_deduction_access_scope (tree
);
122 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
123 unification_kind_t
, int,
125 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
126 unification_kind_t
, int, bool, bool);
127 static int unify (tree
, tree
, tree
, tree
, int, bool);
128 static void add_pending_template (tree
);
129 static tree
reopen_tinst_level (struct tinst_level
*);
130 static tree
tsubst_initializer_list (tree
, tree
);
131 static tree
get_class_bindings (tree
, tree
, tree
);
132 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
134 static void tsubst_enum (tree
, tree
, tree
);
135 static tree
add_to_template_args (tree
, tree
);
136 static tree
add_outermost_template_args (tree
, tree
);
137 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
138 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
140 static int type_unification_real (tree
, tree
, tree
, const tree
*,
141 unsigned int, int, unification_kind_t
, int,
143 static void note_template_header (int);
144 static tree
convert_nontype_argument_function (tree
, tree
);
145 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
146 static tree
convert_template_argument (tree
, tree
, tree
,
147 tsubst_flags_t
, int, tree
);
148 static int for_each_template_parm (tree
, tree_fn_t
, void*,
149 struct pointer_set_t
*, bool);
150 static tree
expand_template_argument_pack (tree
);
151 static tree
build_template_parm_index (int, int, int, int, tree
, tree
);
152 static bool inline_needs_template_parms (tree
);
153 static void push_inline_template_parms_recursive (tree
, int);
154 static tree
retrieve_local_specialization (tree
);
155 static void register_local_specialization (tree
, tree
);
156 static hashval_t
hash_specialization (const void *p
);
157 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
158 static int mark_template_parm (tree
, void *);
159 static int template_parm_this_level_p (tree
, void *);
160 static tree
tsubst_friend_function (tree
, tree
);
161 static tree
tsubst_friend_class (tree
, tree
);
162 static int can_complete_type_without_circularity (tree
);
163 static tree
get_bindings (tree
, tree
, tree
, bool);
164 static int template_decl_level (tree
);
165 static int check_cv_quals_for_unify (int, tree
, tree
);
166 static void template_parm_level_and_index (tree
, int*, int*);
167 static int unify_pack_expansion (tree
, tree
, tree
,
168 tree
, int, bool, bool, bool);
169 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
170 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
171 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
172 static void regenerate_decl_from_template (tree
, tree
);
173 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
174 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
175 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
176 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
177 static bool check_specialization_scope (void);
178 static tree
process_partial_specialization (tree
);
179 static void set_current_access_from_decl (tree
);
180 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
182 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
183 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
185 static bool template_template_parm_bindings_ok_p (tree
, tree
);
186 static int template_args_equal (tree
, tree
);
187 static void tsubst_default_arguments (tree
);
188 static tree
for_each_template_parm_r (tree
*, int *, void *);
189 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
190 static void copy_default_args_to_explicit_spec (tree
);
191 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
192 static int eq_local_specializations (const void *, const void *);
193 static bool dependent_template_arg_p (tree
);
194 static bool any_template_arguments_need_structural_equality_p (tree
);
195 static bool dependent_type_p_r (tree
);
196 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
197 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
198 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
199 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
200 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
201 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
203 static tree
listify (tree
);
204 static tree
listify_autos (tree
, tree
);
205 static tree
template_parm_to_arg (tree t
);
206 static tree
current_template_args (void);
207 static tree
fixup_template_type_parm_type (tree
, int);
208 static tree
fixup_template_parm_index (tree
, tree
, int);
209 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
211 /* Make the current scope suitable for access checking when we are
212 processing T. T can be FUNCTION_DECL for instantiated function
213 template, or VAR_DECL for static member variable (need by
214 instantiate_decl). */
217 push_access_scope (tree t
)
219 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
220 || TREE_CODE (t
) == VAR_DECL
);
222 if (DECL_FRIEND_CONTEXT (t
))
223 push_nested_class (DECL_FRIEND_CONTEXT (t
));
224 else if (DECL_CLASS_SCOPE_P (t
))
225 push_nested_class (DECL_CONTEXT (t
));
227 push_to_top_level ();
229 if (TREE_CODE (t
) == FUNCTION_DECL
)
231 saved_access_scope
= tree_cons
232 (NULL_TREE
, current_function_decl
, saved_access_scope
);
233 current_function_decl
= t
;
237 /* Restore the scope set up by push_access_scope. T is the node we
241 pop_access_scope (tree t
)
243 if (TREE_CODE (t
) == FUNCTION_DECL
)
245 current_function_decl
= TREE_VALUE (saved_access_scope
);
246 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
249 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
252 pop_from_top_level ();
255 /* Do any processing required when DECL (a member template
256 declaration) is finished. Returns the TEMPLATE_DECL corresponding
257 to DECL, unless it is a specialization, in which case the DECL
258 itself is returned. */
261 finish_member_template_decl (tree decl
)
263 if (decl
== error_mark_node
)
264 return error_mark_node
;
266 gcc_assert (DECL_P (decl
));
268 if (TREE_CODE (decl
) == TYPE_DECL
)
272 type
= TREE_TYPE (decl
);
273 if (type
== error_mark_node
)
274 return error_mark_node
;
275 if (MAYBE_CLASS_TYPE_P (type
)
276 && CLASSTYPE_TEMPLATE_INFO (type
)
277 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
279 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
280 check_member_template (tmpl
);
285 else if (TREE_CODE (decl
) == FIELD_DECL
)
286 error ("data member %qD cannot be a member template", decl
);
287 else if (DECL_TEMPLATE_INFO (decl
))
289 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
291 check_member_template (DECL_TI_TEMPLATE (decl
));
292 return DECL_TI_TEMPLATE (decl
);
298 error ("invalid member template declaration %qD", decl
);
300 return error_mark_node
;
303 /* Create a template info node. */
306 build_template_info (tree template_decl
, tree template_args
)
308 tree result
= make_node (TEMPLATE_INFO
);
309 TI_TEMPLATE (result
) = template_decl
;
310 TI_ARGS (result
) = template_args
;
314 /* Return the template info node corresponding to T, whatever T is. */
317 get_template_info (const_tree t
)
319 tree tinfo
= NULL_TREE
;
321 if (!t
|| t
== error_mark_node
)
324 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
325 tinfo
= DECL_TEMPLATE_INFO (t
);
327 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
330 if (TAGGED_TYPE_P (t
))
331 tinfo
= TYPE_TEMPLATE_INFO (t
);
332 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
333 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
338 /* Returns the template nesting level of the indicated class TYPE.
348 A<T>::B<U> has depth two, while A<T> has depth one.
349 Both A<T>::B<int> and A<int>::B<U> have depth one, if
350 they are instantiations, not specializations.
352 This function is guaranteed to return 0 if passed NULL_TREE so
353 that, for example, `template_class_depth (current_class_type)' is
357 template_class_depth (tree type
)
362 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
363 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
364 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
366 tree tinfo
= get_template_info (type
);
368 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
369 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
376 /* Subroutine of maybe_begin_member_template_processing.
377 Returns true if processing DECL needs us to push template parms. */
380 inline_needs_template_parms (tree decl
)
382 if (! DECL_TEMPLATE_INFO (decl
))
385 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
386 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
389 /* Subroutine of maybe_begin_member_template_processing.
390 Push the template parms in PARMS, starting from LEVELS steps into the
391 chain, and ending at the beginning, since template parms are listed
395 push_inline_template_parms_recursive (tree parmlist
, int levels
)
397 tree parms
= TREE_VALUE (parmlist
);
401 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
403 ++processing_template_decl
;
404 current_template_parms
405 = tree_cons (size_int (processing_template_decl
),
406 parms
, current_template_parms
);
407 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
409 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
411 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
413 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
415 if (parm
== error_mark_node
)
418 gcc_assert (DECL_P (parm
));
420 switch (TREE_CODE (parm
))
429 /* Make a CONST_DECL as is done in process_template_parm.
430 It is ugly that we recreate this here; the original
431 version built in process_template_parm is no longer
433 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
434 CONST_DECL
, DECL_NAME (parm
),
436 DECL_ARTIFICIAL (decl
) = 1;
437 TREE_CONSTANT (decl
) = 1;
438 TREE_READONLY (decl
) = 1;
439 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
440 SET_DECL_TEMPLATE_PARM_P (decl
);
451 /* Restore the template parameter context for a member template or
452 a friend template defined in a class definition. */
455 maybe_begin_member_template_processing (tree decl
)
460 if (inline_needs_template_parms (decl
))
462 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
463 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
465 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
468 parms
= TREE_CHAIN (parms
);
471 push_inline_template_parms_recursive (parms
, levels
);
474 /* Remember how many levels of template parameters we pushed so that
475 we can pop them later. */
476 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
479 /* Undo the effects of maybe_begin_member_template_processing. */
482 maybe_end_member_template_processing (void)
487 if (VEC_length (int, inline_parm_levels
) == 0)
490 last
= VEC_pop (int, inline_parm_levels
);
491 for (i
= 0; i
< last
; ++i
)
493 --processing_template_decl
;
494 current_template_parms
= TREE_CHAIN (current_template_parms
);
499 /* Return a new template argument vector which contains all of ARGS,
500 but has as its innermost set of arguments the EXTRA_ARGS. */
503 add_to_template_args (tree args
, tree extra_args
)
510 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
513 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
514 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
516 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
517 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
519 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
520 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
525 /* Like add_to_template_args, but only the outermost ARGS are added to
526 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
527 (EXTRA_ARGS) levels are added. This function is used to combine
528 the template arguments from a partial instantiation with the
529 template arguments used to attain the full instantiation from the
530 partial instantiation. */
533 add_outermost_template_args (tree args
, tree extra_args
)
537 /* If there are more levels of EXTRA_ARGS than there are ARGS,
538 something very fishy is going on. */
539 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
541 /* If *all* the new arguments will be the EXTRA_ARGS, just return
543 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
546 /* For the moment, we make ARGS look like it contains fewer levels. */
547 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
549 new_args
= add_to_template_args (args
, extra_args
);
551 /* Now, we restore ARGS to its full dimensions. */
552 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
557 /* Return the N levels of innermost template arguments from the ARGS. */
560 get_innermost_template_args (tree args
, int n
)
568 /* If N is 1, just return the innermost set of template arguments. */
570 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
572 /* If we're not removing anything, just return the arguments we were
574 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
575 gcc_assert (extra_levels
>= 0);
576 if (extra_levels
== 0)
579 /* Make a new set of arguments, not containing the outer arguments. */
580 new_args
= make_tree_vec (n
);
581 for (i
= 1; i
<= n
; ++i
)
582 SET_TMPL_ARGS_LEVEL (new_args
, i
,
583 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
588 /* The inverse of get_innermost_template_args: Return all but the innermost
589 EXTRA_LEVELS levels of template arguments from the ARGS. */
592 strip_innermost_template_args (tree args
, int extra_levels
)
595 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
600 /* If N is 1, just return the outermost set of template arguments. */
602 return TMPL_ARGS_LEVEL (args
, 1);
604 /* If we're not removing anything, just return the arguments we were
606 gcc_assert (extra_levels
>= 0);
607 if (extra_levels
== 0)
610 /* Make a new set of arguments, not containing the inner arguments. */
611 new_args
= make_tree_vec (n
);
612 for (i
= 1; i
<= n
; ++i
)
613 SET_TMPL_ARGS_LEVEL (new_args
, i
,
614 TMPL_ARGS_LEVEL (args
, i
));
619 /* We've got a template header coming up; push to a new level for storing
623 begin_template_parm_list (void)
625 /* We use a non-tag-transparent scope here, which causes pushtag to
626 put tags in this scope, rather than in the enclosing class or
627 namespace scope. This is the right thing, since we want
628 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
629 global template class, push_template_decl handles putting the
630 TEMPLATE_DECL into top-level scope. For a nested template class,
633 template <class T> struct S1 {
634 template <class T> struct S2 {};
637 pushtag contains special code to call pushdecl_with_scope on the
638 TEMPLATE_DECL for S2. */
639 begin_scope (sk_template_parms
, NULL
);
640 ++processing_template_decl
;
641 ++processing_template_parmlist
;
642 note_template_header (0);
645 /* This routine is called when a specialization is declared. If it is
646 invalid to declare a specialization here, an error is reported and
647 false is returned, otherwise this routine will return true. */
650 check_specialization_scope (void)
652 tree scope
= current_scope ();
656 An explicit specialization shall be declared in the namespace of
657 which the template is a member, or, for member templates, in the
658 namespace of which the enclosing class or enclosing class
659 template is a member. An explicit specialization of a member
660 function, member class or static data member of a class template
661 shall be declared in the namespace of which the class template
663 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
665 error ("explicit specialization in non-namespace scope %qD", scope
);
671 In an explicit specialization declaration for a member of a class
672 template or a member template that appears in namespace scope,
673 the member template and some of its enclosing class templates may
674 remain unspecialized, except that the declaration shall not
675 explicitly specialize a class member template if its enclosing
676 class templates are not explicitly specialized as well. */
677 if (current_template_parms
)
679 error ("enclosing class templates are not explicitly specialized");
686 /* We've just seen template <>. */
689 begin_specialization (void)
691 begin_scope (sk_template_spec
, NULL
);
692 note_template_header (1);
693 return check_specialization_scope ();
696 /* Called at then end of processing a declaration preceded by
700 end_specialization (void)
703 reset_specialization ();
706 /* Any template <>'s that we have seen thus far are not referring to a
707 function specialization. */
710 reset_specialization (void)
712 processing_specialization
= 0;
713 template_header_count
= 0;
716 /* We've just seen a template header. If SPECIALIZATION is nonzero,
717 it was of the form template <>. */
720 note_template_header (int specialization
)
722 processing_specialization
= specialization
;
723 template_header_count
++;
726 /* We're beginning an explicit instantiation. */
729 begin_explicit_instantiation (void)
731 gcc_assert (!processing_explicit_instantiation
);
732 processing_explicit_instantiation
= true;
737 end_explicit_instantiation (void)
739 gcc_assert (processing_explicit_instantiation
);
740 processing_explicit_instantiation
= false;
743 /* An explicit specialization or partial specialization TMPL is being
744 declared. Check that the namespace in which the specialization is
745 occurring is permissible. Returns false iff it is invalid to
746 specialize TMPL in the current namespace. */
749 check_specialization_namespace (tree tmpl
)
751 tree tpl_ns
= decl_namespace_context (tmpl
);
755 An explicit specialization shall be declared in the namespace of
756 which the template is a member, or, for member templates, in the
757 namespace of which the enclosing class or enclosing class
758 template is a member. An explicit specialization of a member
759 function, member class or static data member of a class template
760 shall be declared in the namespace of which the class template is
762 if (current_scope() != DECL_CONTEXT (tmpl
)
763 && !at_namespace_scope_p ())
765 error ("specialization of %qD must appear at namespace scope", tmpl
);
768 if (is_associated_namespace (current_namespace
, tpl_ns
))
769 /* Same or super-using namespace. */
773 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
774 permerror (input_location
, " from definition of %q+#D", tmpl
);
779 /* SPEC is an explicit instantiation. Check that it is valid to
780 perform this explicit instantiation in the current namespace. */
783 check_explicit_instantiation_namespace (tree spec
)
787 /* DR 275: An explicit instantiation shall appear in an enclosing
788 namespace of its template. */
789 ns
= decl_namespace_context (spec
);
790 if (!is_ancestor (current_namespace
, ns
))
791 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
792 "(which does not enclose namespace %qD)",
793 spec
, current_namespace
, ns
);
796 /* The TYPE is being declared. If it is a template type, that means it
797 is a partial specialization. Do appropriate error-checking. */
800 maybe_process_partial_specialization (tree type
)
804 if (type
== error_mark_node
)
805 return error_mark_node
;
807 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
809 error ("name of class shadows template template parameter %qD",
811 return error_mark_node
;
814 context
= TYPE_CONTEXT (type
);
816 if (CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
818 /* This is for ordinary explicit specialization and partial
819 specialization of a template class such as:
821 template <> class C<int>;
825 template <class T> class C<T*>;
827 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
829 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
830 && !COMPLETE_TYPE_P (type
))
832 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
833 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
834 if (processing_template_decl
)
836 if (push_template_decl (TYPE_MAIN_DECL (type
))
838 return error_mark_node
;
841 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
842 error ("specialization of %qT after instantiation", type
);
844 else if (CLASS_TYPE_P (type
)
845 && !CLASSTYPE_USE_TEMPLATE (type
)
846 && CLASSTYPE_TEMPLATE_INFO (type
)
847 && context
&& CLASS_TYPE_P (context
)
848 && CLASSTYPE_TEMPLATE_INFO (context
))
850 /* This is for an explicit specialization of member class
851 template according to [temp.expl.spec/18]:
853 template <> template <class U> class C<int>::D;
855 The context `C<int>' must be an implicit instantiation.
856 Otherwise this is just a member class template declared
859 template <> class C<int> { template <class U> class D; };
860 template <> template <class U> class C<int>::D;
862 In the first case, `C<int>::D' is a specialization of `C<T>::D'
863 while in the second case, `C<int>::D' is a primary template
864 and `C<T>::D' may not exist. */
866 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
867 && !COMPLETE_TYPE_P (type
))
870 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
872 if (current_namespace
873 != decl_namespace_context (tmpl
))
875 permerror (input_location
, "specializing %q#T in different namespace", type
);
876 permerror (input_location
, " from definition of %q+#D", tmpl
);
879 /* Check for invalid specialization after instantiation:
881 template <> template <> class C<int>::D<int>;
882 template <> template <class U> class C<int>::D; */
884 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
885 t
; t
= TREE_CHAIN (t
))
887 tree inst
= TREE_VALUE (t
);
888 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
890 /* We already have a full specialization of this partial
891 instantiation. Reassign it to the new member
892 specialization template. */
896 elt
.tmpl
= most_general_template (tmpl
);
897 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
900 htab_remove_elt (type_specializations
, &elt
);
903 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
905 slot
= (spec_entry
**)
906 htab_find_slot (type_specializations
, &elt
, INSERT
);
907 *slot
= ggc_alloc_spec_entry ();
910 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
911 /* But if we've had an implicit instantiation, that's a
912 problem ([temp.expl.spec]/6). */
913 error ("specialization %qT after instantiation %qT",
917 /* Mark TYPE as a specialization. And as a result, we only
918 have one level of template argument for the innermost
920 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
921 CLASSTYPE_TI_ARGS (type
)
922 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
925 else if (processing_specialization
)
927 /* Someday C++0x may allow for enum template specialization. */
928 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
929 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
930 pedwarn (input_location
, OPT_pedantic
, "template specialization "
931 "of %qD not allowed by ISO C++", type
);
934 error ("explicit specialization of non-template %qT", type
);
935 return error_mark_node
;
942 /* Returns nonzero if we can optimize the retrieval of specializations
943 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
944 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
947 optimize_specialization_lookup_p (tree tmpl
)
949 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
950 && DECL_CLASS_SCOPE_P (tmpl
)
951 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
953 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
954 /* The optimized lookup depends on the fact that the
955 template arguments for the member function template apply
956 purely to the containing class, which is not true if the
957 containing class is an explicit or partial
959 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
960 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
961 && !DECL_CONV_FN_P (tmpl
)
962 /* It is possible to have a template that is not a member
963 template and is not a member of a template class:
965 template <typename T>
966 struct S { friend A::f(); };
968 Here, the friend function is a template, but the context does
969 not have template information. The optimized lookup relies
970 on having ARGS be the template arguments for both the class
971 and the function template. */
972 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
975 /* Retrieve the specialization (in the sense of [temp.spec] - a
976 specialization is either an instantiation or an explicit
977 specialization) of TMPL for the given template ARGS. If there is
978 no such specialization, return NULL_TREE. The ARGS are a vector of
979 arguments, or a vector of vectors of arguments, in the case of
980 templates with more than one level of parameters.
982 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
983 then we search for a partial specialization matching ARGS. This
984 parameter is ignored if TMPL is not a class template. */
987 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
989 if (args
== error_mark_node
)
992 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
994 /* There should be as many levels of arguments as there are
995 levels of parameters. */
996 gcc_assert (TMPL_ARGS_DEPTH (args
)
997 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
999 if (optimize_specialization_lookup_p (tmpl
))
1001 tree class_template
;
1002 tree class_specialization
;
1003 VEC(tree
,gc
) *methods
;
1007 /* The template arguments actually apply to the containing
1008 class. Find the class specialization with those
1010 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1011 class_specialization
1012 = retrieve_specialization (class_template
, args
, 0);
1013 if (!class_specialization
)
1015 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1016 for the specialization. */
1017 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1020 /* Iterate through the methods with the indicated name, looking
1021 for the one that has an instance of TMPL. */
1022 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1023 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
1025 tree fn
= OVL_CURRENT (fns
);
1026 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1027 /* using-declarations can add base methods to the method vec,
1028 and we don't want those here. */
1029 && DECL_CONTEXT (fn
) == class_specialization
)
1038 htab_t specializations
;
1042 elt
.spec
= NULL_TREE
;
1044 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1045 specializations
= type_specializations
;
1047 specializations
= decl_specializations
;
1050 hash
= hash_specialization (&elt
);
1051 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1059 /* Like retrieve_specialization, but for local declarations. */
1062 retrieve_local_specialization (tree tmpl
)
1066 if (local_specializations
== NULL
)
1069 spec
= (tree
) htab_find_with_hash (local_specializations
, tmpl
,
1070 htab_hash_pointer (tmpl
));
1071 return spec
? TREE_PURPOSE (spec
) : NULL_TREE
;
1074 /* Returns nonzero iff DECL is a specialization of TMPL. */
1077 is_specialization_of (tree decl
, tree tmpl
)
1081 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1085 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1091 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1093 for (t
= TREE_TYPE (decl
);
1095 t
= CLASSTYPE_USE_TEMPLATE (t
)
1096 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1097 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1104 /* Returns nonzero iff DECL is a specialization of friend declaration
1105 FRIEND_DECL according to [temp.friend]. */
1108 is_specialization_of_friend (tree decl
, tree friend_decl
)
1110 bool need_template
= true;
1113 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1114 || TREE_CODE (decl
) == TYPE_DECL
);
1116 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1117 of a template class, we want to check if DECL is a specialization
1119 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1120 && DECL_TEMPLATE_INFO (friend_decl
)
1121 && !DECL_USE_TEMPLATE (friend_decl
))
1123 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1124 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1125 need_template
= false;
1127 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1128 && !PRIMARY_TEMPLATE_P (friend_decl
))
1129 need_template
= false;
1131 /* There is nothing to do if this is not a template friend. */
1132 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1135 if (is_specialization_of (decl
, friend_decl
))
1139 A member of a class template may be declared to be a friend of a
1140 non-template class. In this case, the corresponding member of
1141 every specialization of the class template is a friend of the
1142 class granting friendship.
1144 For example, given a template friend declaration
1146 template <class T> friend void A<T>::f();
1148 the member function below is considered a friend
1150 template <> struct A<int> {
1154 For this type of template friend, TEMPLATE_DEPTH below will be
1155 nonzero. To determine if DECL is a friend of FRIEND, we first
1156 check if the enclosing class is a specialization of another. */
1158 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1160 && DECL_CLASS_SCOPE_P (decl
)
1161 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1162 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1164 /* Next, we check the members themselves. In order to handle
1165 a few tricky cases, such as when FRIEND_DECL's are
1167 template <class T> friend void A<T>::g(T t);
1168 template <class T> template <T t> friend void A<T>::h();
1172 void A<int>::g(int);
1173 template <int> void A<int>::h();
1175 we need to figure out ARGS, the template arguments from
1176 the context of DECL. This is required for template substitution
1177 of `T' in the function parameter of `g' and template parameter
1178 of `h' in the above examples. Here ARGS corresponds to `int'. */
1180 tree context
= DECL_CONTEXT (decl
);
1181 tree args
= NULL_TREE
;
1182 int current_depth
= 0;
1184 while (current_depth
< template_depth
)
1186 if (CLASSTYPE_TEMPLATE_INFO (context
))
1188 if (current_depth
== 0)
1189 args
= TYPE_TI_ARGS (context
);
1191 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1194 context
= TYPE_CONTEXT (context
);
1197 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1202 tree friend_args_type
;
1203 tree decl_args_type
;
1205 /* Make sure that both DECL and FRIEND_DECL are templates or
1207 is_template
= DECL_TEMPLATE_INFO (decl
)
1208 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1209 if (need_template
^ is_template
)
1211 else if (is_template
)
1213 /* If both are templates, check template parameter list. */
1215 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1217 if (!comp_template_parms
1218 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1222 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1225 decl_type
= TREE_TYPE (decl
);
1227 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1228 tf_none
, NULL_TREE
);
1229 if (friend_type
== error_mark_node
)
1232 /* Check if return types match. */
1233 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1236 /* Check if function parameter types match, ignoring the
1237 `this' parameter. */
1238 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1239 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1240 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1241 friend_args_type
= TREE_CHAIN (friend_args_type
);
1242 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1243 decl_args_type
= TREE_CHAIN (decl_args_type
);
1245 return compparms (decl_args_type
, friend_args_type
);
1249 /* DECL is a TYPE_DECL */
1251 tree decl_type
= TREE_TYPE (decl
);
1253 /* Make sure that both DECL and FRIEND_DECL are templates or
1256 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1257 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1259 if (need_template
^ is_template
)
1261 else if (is_template
)
1264 /* If both are templates, check the name of the two
1265 TEMPLATE_DECL's first because is_friend didn't. */
1266 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1267 != DECL_NAME (friend_decl
))
1270 /* Now check template parameter list. */
1272 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1274 return comp_template_parms
1275 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1279 return (DECL_NAME (decl
)
1280 == DECL_NAME (friend_decl
));
1286 /* Register the specialization SPEC as a specialization of TMPL with
1287 the indicated ARGS. IS_FRIEND indicates whether the specialization
1288 is actually just a friend declaration. Returns SPEC, or an
1289 equivalent prior declaration, if available. */
1292 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1296 spec_entry
**slot
= NULL
;
1299 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1301 if (TREE_CODE (spec
) == FUNCTION_DECL
1302 && uses_template_parms (DECL_TI_ARGS (spec
)))
1303 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1304 register it; we want the corresponding TEMPLATE_DECL instead.
1305 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1306 the more obvious `uses_template_parms (spec)' to avoid problems
1307 with default function arguments. In particular, given
1308 something like this:
1310 template <class T> void f(T t1, T t = T())
1312 the default argument expression is not substituted for in an
1313 instantiation unless and until it is actually needed. */
1316 if (optimize_specialization_lookup_p (tmpl
))
1317 /* We don't put these specializations in the hash table, but we might
1318 want to give an error about a mismatch. */
1319 fn
= retrieve_specialization (tmpl
, args
, 0);
1327 hash
= hash_specialization (&elt
);
1329 slot
= (spec_entry
**)
1330 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1337 /* We can sometimes try to re-register a specialization that we've
1338 already got. In particular, regenerate_decl_from_template calls
1339 duplicate_decls which will update the specialization list. But,
1340 we'll still get called again here anyhow. It's more convenient
1341 to simply allow this than to try to prevent it. */
1344 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1346 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1348 if (DECL_ODR_USED (fn
)
1349 || DECL_EXPLICIT_INSTANTIATION (fn
))
1351 error ("specialization of %qD after instantiation",
1353 return error_mark_node
;
1358 /* This situation should occur only if the first
1359 specialization is an implicit instantiation, the
1360 second is an explicit specialization, and the
1361 implicit instantiation has not yet been used. That
1362 situation can occur if we have implicitly
1363 instantiated a member function and then specialized
1366 We can also wind up here if a friend declaration that
1367 looked like an instantiation turns out to be a
1370 template <class T> void foo(T);
1371 class S { friend void foo<>(int) };
1372 template <> void foo(int);
1374 We transform the existing DECL in place so that any
1375 pointers to it become pointers to the updated
1378 If there was a definition for the template, but not
1379 for the specialization, we want this to look as if
1380 there were no definition, and vice versa. */
1381 DECL_INITIAL (fn
) = NULL_TREE
;
1382 duplicate_decls (spec
, fn
, is_friend
);
1383 /* The call to duplicate_decls will have applied
1386 An explicit specialization of a function template
1387 is inline only if it is explicitly declared to be,
1388 and independently of whether its function template
1391 to the primary function; now copy the inline bits to
1392 the various clones. */
1393 FOR_EACH_CLONE (clone
, fn
)
1395 DECL_DECLARED_INLINE_P (clone
)
1396 = DECL_DECLARED_INLINE_P (fn
);
1397 DECL_SOURCE_LOCATION (clone
)
1398 = DECL_SOURCE_LOCATION (fn
);
1400 check_specialization_namespace (fn
);
1405 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1407 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1408 /* Dup decl failed, but this is a new definition. Set the
1409 line number so any errors match this new
1411 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1417 return duplicate_decls (spec
, fn
, is_friend
);
1419 /* A specialization must be declared in the same namespace as the
1420 template it is specializing. */
1421 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1422 && !check_specialization_namespace (tmpl
))
1423 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1425 if (!optimize_specialization_lookup_p (tmpl
))
1427 gcc_assert (tmpl
&& args
&& spec
);
1428 *slot
= ggc_alloc_spec_entry ();
1430 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1431 && PRIMARY_TEMPLATE_P (tmpl
)
1432 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1433 /* TMPL is a forward declaration of a template function; keep a list
1434 of all specializations in case we need to reassign them to a friend
1435 template later in tsubst_friend_function. */
1436 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1437 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1443 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1444 TMPL and ARGS members, ignores SPEC. */
1447 eq_specializations (const void *p1
, const void *p2
)
1449 const spec_entry
*e1
= (const spec_entry
*)p1
;
1450 const spec_entry
*e2
= (const spec_entry
*)p2
;
1452 return (e1
->tmpl
== e2
->tmpl
1453 && comp_template_args (e1
->args
, e2
->args
));
1456 /* Returns a hash for a template TMPL and template arguments ARGS. */
1459 hash_tmpl_and_args (tree tmpl
, tree args
)
1461 hashval_t val
= DECL_UID (tmpl
);
1462 return iterative_hash_template_arg (args
, val
);
1465 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1469 hash_specialization (const void *p
)
1471 const spec_entry
*e
= (const spec_entry
*)p
;
1472 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1475 /* Recursively calculate a hash value for a template argument ARG, for use
1476 in the hash tables of template specializations. */
1479 iterative_hash_template_arg (tree arg
, hashval_t val
)
1481 unsigned HOST_WIDE_INT i
;
1482 enum tree_code code
;
1485 if (arg
== NULL_TREE
)
1486 return iterative_hash_object (arg
, val
);
1491 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1492 /* We can get one of these when re-hashing a previous entry in the middle
1493 of substituting into a pack expansion. Just look through it. */
1494 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1496 code
= TREE_CODE (arg
);
1497 tclass
= TREE_CODE_CLASS (code
);
1499 val
= iterative_hash_object (code
, val
);
1506 case IDENTIFIER_NODE
:
1507 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1511 int i
, len
= TREE_VEC_LENGTH (arg
);
1512 for (i
= 0; i
< len
; ++i
)
1513 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1517 case TYPE_PACK_EXPANSION
:
1518 case EXPR_PACK_EXPANSION
:
1519 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1521 case TYPE_ARGUMENT_PACK
:
1522 case NONTYPE_ARGUMENT_PACK
:
1523 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1526 for (; arg
; arg
= TREE_CHAIN (arg
))
1527 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1531 for (; arg
; arg
= OVL_NEXT (arg
))
1532 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1538 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1540 val
= iterative_hash_template_arg (field
, val
);
1541 val
= iterative_hash_template_arg (value
, val
);
1547 if (!DECL_ARTIFICIAL (arg
))
1549 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1550 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1552 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1555 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1558 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1559 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1561 case TEMPLATE_PARM_INDEX
:
1562 val
= iterative_hash_template_arg
1563 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1564 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1565 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1568 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1569 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1570 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1573 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1575 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1579 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1580 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1581 val
= iterative_hash_object (code
, val
);
1582 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1585 /* A lambda can't appear in a template arg, but don't crash on
1587 gcc_assert (seen_error ());
1591 case STATIC_CAST_EXPR
:
1592 case REINTERPRET_CAST_EXPR
:
1593 case CONST_CAST_EXPR
:
1594 case DYNAMIC_CAST_EXPR
:
1596 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1597 /* Now hash operands as usual. */
1607 if (TYPE_CANONICAL (arg
))
1608 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1610 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1611 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1612 /* Otherwise just compare the types during lookup. */
1615 case tcc_declaration
:
1617 return iterative_hash_expr (arg
, val
);
1620 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1622 unsigned n
= cp_tree_operand_length (arg
);
1623 for (i
= 0; i
< n
; ++i
)
1624 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1632 /* Unregister the specialization SPEC as a specialization of TMPL.
1633 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1634 if the SPEC was listed as a specialization of TMPL.
1636 Note that SPEC has been ggc_freed, so we can't look inside it. */
1639 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1644 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1645 elt
.args
= TI_ARGS (tinfo
);
1646 elt
.spec
= NULL_TREE
;
1648 slot
= (spec_entry
**) htab_find_slot (decl_specializations
, &elt
, INSERT
);
1651 gcc_assert ((*slot
)->spec
== spec
|| (*slot
)->spec
== new_spec
);
1652 gcc_assert (new_spec
!= NULL_TREE
);
1653 (*slot
)->spec
= new_spec
;
1660 /* Compare an entry in the local specializations hash table P1 (which
1661 is really a pointer to a TREE_LIST) with P2 (which is really a
1665 eq_local_specializations (const void *p1
, const void *p2
)
1667 return TREE_VALUE ((const_tree
) p1
) == (const_tree
) p2
;
1670 /* Hash P1, an entry in the local specializations table. */
1673 hash_local_specialization (const void* p1
)
1675 return htab_hash_pointer (TREE_VALUE ((const_tree
) p1
));
1678 /* Like register_specialization, but for local declarations. We are
1679 registering SPEC, an instantiation of TMPL. */
1682 register_local_specialization (tree spec
, tree tmpl
)
1686 slot
= htab_find_slot_with_hash (local_specializations
, tmpl
,
1687 htab_hash_pointer (tmpl
), INSERT
);
1688 *slot
= build_tree_list (spec
, tmpl
);
1691 /* TYPE is a class type. Returns true if TYPE is an explicitly
1692 specialized class. */
1695 explicit_class_specialization_p (tree type
)
1697 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1699 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1702 /* Print the list of functions at FNS, going through all the overloads
1703 for each element of the list. Alternatively, FNS can not be a
1704 TREE_LIST, in which case it will be printed together with all the
1707 MORE and *STR should respectively be FALSE and NULL when the function
1708 is called from the outside. They are used internally on recursive
1709 calls. print_candidates manages the two parameters and leaves NULL
1710 in *STR when it ends. */
1713 print_candidates_1 (tree fns
, bool more
, const char **str
)
1716 char *spaces
= NULL
;
1718 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1719 if (TREE_CODE (fn
) == TREE_LIST
)
1721 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1722 print_candidates_1 (TREE_VALUE (fn2
),
1723 TREE_CHAIN (fn2
) || more
, str
);
1729 /* Pick the prefix string. */
1730 if (!more
&& !OVL_NEXT (fns
))
1732 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1736 *str
= _("candidates are:");
1737 spaces
= get_spaces (*str
);
1739 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1740 *str
= spaces
? spaces
: *str
;
1750 /* Print the list of candidate FNS in an error message. FNS can also
1751 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1754 print_candidates (tree fns
)
1756 const char *str
= NULL
;
1757 print_candidates_1 (fns
, false, &str
);
1758 gcc_assert (str
== NULL
);
1761 /* Returns the template (one of the functions given by TEMPLATE_ID)
1762 which can be specialized to match the indicated DECL with the
1763 explicit template args given in TEMPLATE_ID. The DECL may be
1764 NULL_TREE if none is available. In that case, the functions in
1765 TEMPLATE_ID are non-members.
1767 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1768 specialization of a member template.
1770 The TEMPLATE_COUNT is the number of references to qualifying
1771 template classes that appeared in the name of the function. See
1772 check_explicit_specialization for a more accurate description.
1774 TSK indicates what kind of template declaration (if any) is being
1775 declared. TSK_TEMPLATE indicates that the declaration given by
1776 DECL, though a FUNCTION_DECL, has template parameters, and is
1777 therefore a template function.
1779 The template args (those explicitly specified and those deduced)
1780 are output in a newly created vector *TARGS_OUT.
1782 If it is impossible to determine the result, an error message is
1783 issued. The error_mark_node is returned to indicate failure. */
1786 determine_specialization (tree template_id
,
1789 int need_member_template
,
1795 tree explicit_targs
;
1796 tree candidates
= NULL_TREE
;
1797 /* A TREE_LIST of templates of which DECL may be a specialization.
1798 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1799 corresponding TREE_PURPOSE is the set of template arguments that,
1800 when used to instantiate the template, would produce a function
1801 with the signature of DECL. */
1802 tree templates
= NULL_TREE
;
1804 cp_binding_level
*b
;
1806 *targs_out
= NULL_TREE
;
1808 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1809 return error_mark_node
;
1811 fns
= TREE_OPERAND (template_id
, 0);
1812 explicit_targs
= TREE_OPERAND (template_id
, 1);
1814 if (fns
== error_mark_node
)
1815 return error_mark_node
;
1817 /* Check for baselinks. */
1818 if (BASELINK_P (fns
))
1819 fns
= BASELINK_FUNCTIONS (fns
);
1821 if (!is_overloaded_fn (fns
))
1823 error ("%qD is not a function template", fns
);
1824 return error_mark_node
;
1827 /* Count the number of template headers specified for this
1830 for (b
= current_binding_level
;
1831 b
->kind
== sk_template_parms
;
1835 for (; fns
; fns
= OVL_NEXT (fns
))
1837 tree fn
= OVL_CURRENT (fns
);
1839 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1841 tree decl_arg_types
;
1844 /* In case of explicit specialization, we need to check if
1845 the number of template headers appearing in the specialization
1846 is correct. This is usually done in check_explicit_specialization,
1847 but the check done there cannot be exhaustive when specializing
1848 member functions. Consider the following code:
1850 template <> void A<int>::f(int);
1851 template <> template <> void A<int>::f(int);
1853 Assuming that A<int> is not itself an explicit specialization
1854 already, the first line specializes "f" which is a non-template
1855 member function, whilst the second line specializes "f" which
1856 is a template member function. So both lines are syntactically
1857 correct, and check_explicit_specialization does not reject
1860 Here, we can do better, as we are matching the specialization
1861 against the declarations. We count the number of template
1862 headers, and we check if they match TEMPLATE_COUNT + 1
1863 (TEMPLATE_COUNT is the number of qualifying template classes,
1864 plus there must be another header for the member template
1867 Notice that if header_count is zero, this is not a
1868 specialization but rather a template instantiation, so there
1869 is no check we can perform here. */
1870 if (header_count
&& header_count
!= template_count
+ 1)
1873 /* Check that the number of template arguments at the
1874 innermost level for DECL is the same as for FN. */
1875 if (current_binding_level
->kind
== sk_template_parms
1876 && !current_binding_level
->explicit_spec_p
1877 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1878 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1879 (current_template_parms
))))
1882 /* DECL might be a specialization of FN. */
1883 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1884 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1886 /* For a non-static member function, we need to make sure
1887 that the const qualification is the same. Since
1888 get_bindings does not try to merge the "this" parameter,
1889 we must do the comparison explicitly. */
1890 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1891 && !same_type_p (TREE_VALUE (fn_arg_types
),
1892 TREE_VALUE (decl_arg_types
)))
1895 /* Skip the "this" parameter and, for constructors of
1896 classes with virtual bases, the VTT parameter. A
1897 full specialization of a constructor will have a VTT
1898 parameter, but a template never will. */
1900 = skip_artificial_parms_for (decl
, decl_arg_types
);
1902 = skip_artificial_parms_for (fn
, fn_arg_types
);
1904 /* Check that the number of function parameters matches.
1906 template <class T> void f(int i = 0);
1907 template <> void f<int>();
1908 The specialization f<int> is invalid but is not caught
1909 by get_bindings below. */
1910 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1913 /* Function templates cannot be specializations; there are
1914 no partial specializations of functions. Therefore, if
1915 the type of DECL does not match FN, there is no
1917 if (tsk
== tsk_template
)
1919 if (compparms (fn_arg_types
, decl_arg_types
))
1920 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1924 /* See whether this function might be a specialization of this
1926 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1929 /* We cannot deduce template arguments that when used to
1930 specialize TMPL will produce DECL. */
1933 /* Save this template, and the arguments deduced. */
1934 templates
= tree_cons (targs
, fn
, templates
);
1936 else if (need_member_template
)
1937 /* FN is an ordinary member function, and we need a
1938 specialization of a member template. */
1940 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1941 /* We can get IDENTIFIER_NODEs here in certain erroneous
1944 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1945 /* This is just an ordinary non-member function. Nothing can
1946 be a specialization of that. */
1948 else if (DECL_ARTIFICIAL (fn
))
1949 /* Cannot specialize functions that are created implicitly. */
1953 tree decl_arg_types
;
1955 /* This is an ordinary member function. However, since
1956 we're here, we can assume it's enclosing class is a
1957 template class. For example,
1959 template <typename T> struct S { void f(); };
1960 template <> void S<int>::f() {}
1962 Here, S<int>::f is a non-template, but S<int> is a
1963 template class. If FN has the same type as DECL, we
1964 might be in business. */
1966 if (!DECL_TEMPLATE_INFO (fn
))
1967 /* Its enclosing class is an explicit specialization
1968 of a template class. This is not a candidate. */
1971 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1972 TREE_TYPE (TREE_TYPE (fn
))))
1973 /* The return types differ. */
1976 /* Adjust the type of DECL in case FN is a static member. */
1977 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1978 if (DECL_STATIC_FUNCTION_P (fn
)
1979 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1980 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1982 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1985 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1989 if (templates
&& TREE_CHAIN (templates
))
1995 It is possible for a specialization with a given function
1996 signature to be instantiated from more than one function
1997 template. In such cases, explicit specification of the
1998 template arguments must be used to uniquely identify the
1999 function template specialization being specialized.
2001 Note that here, there's no suggestion that we're supposed to
2002 determine which of the candidate templates is most
2003 specialized. However, we, also have:
2007 Partial ordering of overloaded function template
2008 declarations is used in the following contexts to select
2009 the function template to which a function template
2010 specialization refers:
2012 -- when an explicit specialization refers to a function
2015 So, we do use the partial ordering rules, at least for now.
2016 This extension can only serve to make invalid programs valid,
2017 so it's safe. And, there is strong anecdotal evidence that
2018 the committee intended the partial ordering rules to apply;
2019 the EDG front end has that behavior, and John Spicer claims
2020 that the committee simply forgot to delete the wording in
2021 [temp.expl.spec]. */
2022 tree tmpl
= most_specialized_instantiation (templates
);
2023 if (tmpl
!= error_mark_node
)
2026 TREE_CHAIN (templates
) = NULL_TREE
;
2030 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2032 error ("template-id %qD for %q+D does not match any template "
2033 "declaration", template_id
, decl
);
2034 if (header_count
&& header_count
!= template_count
+ 1)
2035 inform (input_location
, "saw %d %<template<>%>, need %d for "
2036 "specializing a member function template",
2037 header_count
, template_count
+ 1);
2038 return error_mark_node
;
2040 else if ((templates
&& TREE_CHAIN (templates
))
2041 || (candidates
&& TREE_CHAIN (candidates
))
2042 || (templates
&& candidates
))
2044 error ("ambiguous template specialization %qD for %q+D",
2046 candidates
= chainon (candidates
, templates
);
2047 print_candidates (candidates
);
2048 return error_mark_node
;
2051 /* We have one, and exactly one, match. */
2054 tree fn
= TREE_VALUE (candidates
);
2055 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2056 /* DECL is a re-declaration or partial instantiation of a template
2058 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2060 /* It was a specialization of an ordinary member function in a
2062 return DECL_TI_TEMPLATE (fn
);
2065 /* It was a specialization of a template. */
2066 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2067 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2069 *targs_out
= copy_node (targs
);
2070 SET_TMPL_ARGS_LEVEL (*targs_out
,
2071 TMPL_ARGS_DEPTH (*targs_out
),
2072 TREE_PURPOSE (templates
));
2075 *targs_out
= TREE_PURPOSE (templates
);
2076 return TREE_VALUE (templates
);
2079 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2080 but with the default argument values filled in from those in the
2084 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2087 tree new_spec_types
;
2092 if (spec_types
== void_list_node
)
2093 return void_list_node
;
2095 /* Substitute into the rest of the list. */
2097 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2098 TREE_CHAIN (tmpl_types
));
2100 /* Add the default argument for this parameter. */
2101 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2102 TREE_VALUE (spec_types
),
2106 /* DECL is an explicit specialization. Replicate default arguments
2107 from the template it specializes. (That way, code like:
2109 template <class T> void f(T = 3);
2110 template <> void f(double);
2113 works, as required.) An alternative approach would be to look up
2114 the correct default arguments at the call-site, but this approach
2115 is consistent with how implicit instantiations are handled. */
2118 copy_default_args_to_explicit_spec (tree decl
)
2123 tree new_spec_types
;
2127 tree object_type
= NULL_TREE
;
2128 tree in_charge
= NULL_TREE
;
2129 tree vtt
= NULL_TREE
;
2131 /* See if there's anything we need to do. */
2132 tmpl
= DECL_TI_TEMPLATE (decl
);
2133 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2134 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2135 if (TREE_PURPOSE (t
))
2140 old_type
= TREE_TYPE (decl
);
2141 spec_types
= TYPE_ARG_TYPES (old_type
);
2143 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2145 /* Remove the this pointer, but remember the object's type for
2147 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2148 spec_types
= TREE_CHAIN (spec_types
);
2149 tmpl_types
= TREE_CHAIN (tmpl_types
);
2151 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2153 /* DECL may contain more parameters than TMPL due to the extra
2154 in-charge parameter in constructors and destructors. */
2155 in_charge
= spec_types
;
2156 spec_types
= TREE_CHAIN (spec_types
);
2158 if (DECL_HAS_VTT_PARM_P (decl
))
2161 spec_types
= TREE_CHAIN (spec_types
);
2165 /* Compute the merged default arguments. */
2167 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2169 /* Compute the new FUNCTION_TYPE. */
2173 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2178 /* Put the in-charge parameter back. */
2179 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2180 TREE_VALUE (in_charge
),
2183 new_type
= build_method_type_directly (object_type
,
2184 TREE_TYPE (old_type
),
2188 new_type
= build_function_type (TREE_TYPE (old_type
),
2190 new_type
= cp_build_type_attribute_variant (new_type
,
2191 TYPE_ATTRIBUTES (old_type
));
2192 new_type
= build_exception_variant (new_type
,
2193 TYPE_RAISES_EXCEPTIONS (old_type
));
2194 TREE_TYPE (decl
) = new_type
;
2197 /* Check to see if the function just declared, as indicated in
2198 DECLARATOR, and in DECL, is a specialization of a function
2199 template. We may also discover that the declaration is an explicit
2200 instantiation at this point.
2202 Returns DECL, or an equivalent declaration that should be used
2203 instead if all goes well. Issues an error message if something is
2204 amiss. Returns error_mark_node if the error is not easily
2207 FLAGS is a bitmask consisting of the following flags:
2209 2: The function has a definition.
2210 4: The function is a friend.
2212 The TEMPLATE_COUNT is the number of references to qualifying
2213 template classes that appeared in the name of the function. For
2216 template <class T> struct S { void f(); };
2219 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2220 classes are not counted in the TEMPLATE_COUNT, so that in
2222 template <class T> struct S {};
2223 template <> struct S<int> { void f(); }
2224 template <> void S<int>::f();
2226 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2227 invalid; there should be no template <>.)
2229 If the function is a specialization, it is marked as such via
2230 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2231 is set up correctly, and it is added to the list of specializations
2232 for that template. */
2235 check_explicit_specialization (tree declarator
,
2240 int have_def
= flags
& 2;
2241 int is_friend
= flags
& 4;
2242 int specialization
= 0;
2243 int explicit_instantiation
= 0;
2244 int member_specialization
= 0;
2245 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2246 tree dname
= DECL_NAME (decl
);
2251 if (!processing_specialization
)
2254 tsk
= tsk_excessive_parms
;
2257 tsk
= current_tmpl_spec_kind (template_count
);
2262 if (processing_specialization
)
2265 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2267 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2270 /* This could be something like:
2272 template <class T> void f(T);
2273 class S { friend void f<>(int); } */
2277 /* This case handles bogus declarations like template <>
2278 template <class T> void f<int>(); */
2280 error ("template-id %qD in declaration of primary template",
2287 case tsk_invalid_member_spec
:
2288 /* The error has already been reported in
2289 check_specialization_scope. */
2290 return error_mark_node
;
2292 case tsk_invalid_expl_inst
:
2293 error ("template parameter list used in explicit instantiation");
2299 error ("definition provided for explicit instantiation");
2301 explicit_instantiation
= 1;
2304 case tsk_excessive_parms
:
2305 case tsk_insufficient_parms
:
2306 if (tsk
== tsk_excessive_parms
)
2307 error ("too many template parameter lists in declaration of %qD",
2309 else if (template_header_count
)
2310 error("too few template parameter lists in declaration of %qD", decl
);
2312 error("explicit specialization of %qD must be introduced by "
2313 "%<template <>%>", decl
);
2317 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2319 member_specialization
= 1;
2325 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2327 /* This case handles bogus declarations like template <>
2328 template <class T> void f<int>(); */
2330 if (uses_template_parms (declarator
))
2331 error ("function template partial specialization %qD "
2332 "is not allowed", declarator
);
2334 error ("template-id %qD in declaration of primary template",
2339 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2340 /* This is a specialization of a member template, without
2341 specialization the containing class. Something like:
2343 template <class T> struct S {
2344 template <class U> void f (U);
2346 template <> template <class U> void S<int>::f(U) {}
2348 That's a specialization -- but of the entire template. */
2356 if (specialization
|| member_specialization
)
2358 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2359 for (; t
; t
= TREE_CHAIN (t
))
2360 if (TREE_PURPOSE (t
))
2362 permerror (input_location
,
2363 "default argument specified in explicit specialization");
2368 if (specialization
|| member_specialization
|| explicit_instantiation
)
2370 tree tmpl
= NULL_TREE
;
2371 tree targs
= NULL_TREE
;
2373 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2374 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2378 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2383 /* If there is no class context, the explicit instantiation
2384 must be at namespace scope. */
2385 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2387 /* Find the namespace binding, using the declaration
2389 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2391 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2393 error ("%qD is not a template function", dname
);
2394 fns
= error_mark_node
;
2398 tree fn
= OVL_CURRENT (fns
);
2399 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2400 CP_DECL_CONTEXT (fn
)))
2401 error ("%qD is not declared in %qD",
2402 decl
, current_namespace
);
2406 declarator
= lookup_template_function (fns
, NULL_TREE
);
2409 if (declarator
== error_mark_node
)
2410 return error_mark_node
;
2412 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2414 if (!explicit_instantiation
)
2415 /* A specialization in class scope. This is invalid,
2416 but the error will already have been flagged by
2417 check_specialization_scope. */
2418 return error_mark_node
;
2421 /* It's not valid to write an explicit instantiation in
2424 class C { template void f(); }
2426 This case is caught by the parser. However, on
2429 template class C { void f(); };
2431 (which is invalid) we can get here. The error will be
2438 else if (ctype
!= NULL_TREE
2439 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2442 /* Find the list of functions in ctype that have the same
2443 name as the declared function. */
2444 tree name
= TREE_OPERAND (declarator
, 0);
2445 tree fns
= NULL_TREE
;
2448 if (constructor_name_p (name
, ctype
))
2450 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2452 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2453 : !CLASSTYPE_DESTRUCTORS (ctype
))
2455 /* From [temp.expl.spec]:
2457 If such an explicit specialization for the member
2458 of a class template names an implicitly-declared
2459 special member function (clause _special_), the
2460 program is ill-formed.
2462 Similar language is found in [temp.explicit]. */
2463 error ("specialization of implicitly-declared special member function");
2464 return error_mark_node
;
2467 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2470 if (!DECL_CONV_FN_P (decl
))
2472 idx
= lookup_fnfields_1 (ctype
, name
);
2474 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2478 VEC(tree
,gc
) *methods
;
2481 /* For a type-conversion operator, we cannot do a
2482 name-based lookup. We might be looking for `operator
2483 int' which will be a specialization of `operator T'.
2484 So, we find *all* the conversion operators, and then
2485 select from them. */
2488 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2490 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2491 VEC_iterate (tree
, methods
, idx
, ovl
);
2494 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2495 /* There are no more conversion functions. */
2498 /* Glue all these conversion functions together
2499 with those we already have. */
2500 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2501 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2505 if (fns
== NULL_TREE
)
2507 error ("no member function %qD declared in %qT", name
, ctype
);
2508 return error_mark_node
;
2511 TREE_OPERAND (declarator
, 0) = fns
;
2514 /* Figure out what exactly is being specialized at this point.
2515 Note that for an explicit instantiation, even one for a
2516 member function, we cannot tell apriori whether the
2517 instantiation is for a member template, or just a member
2518 function of a template class. Even if a member template is
2519 being instantiated, the member template arguments may be
2520 elided if they can be deduced from the rest of the
2522 tmpl
= determine_specialization (declarator
, decl
,
2524 member_specialization
,
2528 if (!tmpl
|| tmpl
== error_mark_node
)
2529 /* We couldn't figure out what this declaration was
2531 return error_mark_node
;
2534 tree gen_tmpl
= most_general_template (tmpl
);
2536 if (explicit_instantiation
)
2538 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2539 is done by do_decl_instantiation later. */
2541 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2542 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2544 if (arg_depth
> parm_depth
)
2546 /* If TMPL is not the most general template (for
2547 example, if TMPL is a friend template that is
2548 injected into namespace scope), then there will
2549 be too many levels of TARGS. Remove some of them
2554 new_targs
= make_tree_vec (parm_depth
);
2555 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2556 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2557 = TREE_VEC_ELT (targs
, i
);
2561 return instantiate_template (tmpl
, targs
, tf_error
);
2564 /* If we thought that the DECL was a member function, but it
2565 turns out to be specializing a static member function,
2566 make DECL a static member function as well. */
2567 if (DECL_STATIC_FUNCTION_P (tmpl
)
2568 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2569 revert_static_member_fn (decl
);
2571 /* If this is a specialization of a member template of a
2572 template class, we want to return the TEMPLATE_DECL, not
2573 the specialization of it. */
2574 if (tsk
== tsk_template
)
2576 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2577 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2578 DECL_INITIAL (result
) = NULL_TREE
;
2582 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2583 DECL_SOURCE_LOCATION (result
)
2584 = DECL_SOURCE_LOCATION (decl
);
2585 /* We want to use the argument list specified in the
2586 definition, not in the original declaration. */
2587 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2588 for (parm
= DECL_ARGUMENTS (result
); parm
;
2589 parm
= DECL_CHAIN (parm
))
2590 DECL_CONTEXT (parm
) = result
;
2592 return register_specialization (tmpl
, gen_tmpl
, targs
,
2596 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2597 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2599 /* Inherit default function arguments from the template
2600 DECL is specializing. */
2601 copy_default_args_to_explicit_spec (decl
);
2603 /* This specialization has the same protection as the
2604 template it specializes. */
2605 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2606 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2608 /* 7.1.1-1 [dcl.stc]
2610 A storage-class-specifier shall not be specified in an
2611 explicit specialization...
2613 The parser rejects these, so unless action is taken here,
2614 explicit function specializations will always appear with
2617 The action recommended by the C++ CWG in response to C++
2618 defect report 605 is to make the storage class and linkage
2619 of the explicit specialization match the templated function:
2621 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2623 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2625 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2626 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2628 /* This specialization has the same linkage and visibility as
2629 the function template it specializes. */
2630 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2631 if (! TREE_PUBLIC (decl
))
2633 DECL_INTERFACE_KNOWN (decl
) = 1;
2634 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2636 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2637 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2639 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2640 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2644 /* If DECL is a friend declaration, declared using an
2645 unqualified name, the namespace associated with DECL may
2646 have been set incorrectly. For example, in:
2648 template <typename T> void f(T);
2650 struct S { friend void f<int>(int); }
2653 we will have set the DECL_CONTEXT for the friend
2654 declaration to N, rather than to the global namespace. */
2655 if (DECL_NAMESPACE_SCOPE_P (decl
))
2656 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2658 if (is_friend
&& !have_def
)
2659 /* This is not really a declaration of a specialization.
2660 It's just the name of an instantiation. But, it's not
2661 a request for an instantiation, either. */
2662 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2663 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2664 /* This is indeed a specialization. In case of constructors
2665 and destructors, we need in-charge and not-in-charge
2666 versions in V3 ABI. */
2667 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2669 /* Register this specialization so that we can find it
2671 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2678 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2679 parameters. These are represented in the same format used for
2680 DECL_TEMPLATE_PARMS. */
2683 comp_template_parms (const_tree parms1
, const_tree parms2
)
2688 if (parms1
== parms2
)
2691 for (p1
= parms1
, p2
= parms2
;
2692 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2693 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2695 tree t1
= TREE_VALUE (p1
);
2696 tree t2
= TREE_VALUE (p2
);
2699 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2700 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2702 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2705 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2707 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2708 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2710 /* If either of the template parameters are invalid, assume
2711 they match for the sake of error recovery. */
2712 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2715 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2718 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2719 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2720 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2722 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2727 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2728 /* One set of parameters has more parameters lists than the
2735 /* Determine whether PARM is a parameter pack. */
2738 template_parameter_pack_p (const_tree parm
)
2740 /* Determine if we have a non-type template parameter pack. */
2741 if (TREE_CODE (parm
) == PARM_DECL
)
2742 return (DECL_TEMPLATE_PARM_P (parm
)
2743 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2745 /* If this is a list of template parameters, we could get a
2746 TYPE_DECL or a TEMPLATE_DECL. */
2747 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2748 parm
= TREE_TYPE (parm
);
2750 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2751 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2752 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2755 /* Determine if T is a function parameter pack. */
2758 function_parameter_pack_p (const_tree t
)
2760 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2761 return FUNCTION_PARAMETER_PACK_P (t
);
2765 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2766 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2769 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2771 if (! primary_func_tmpl_inst
2772 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2773 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2776 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2779 /* Return true iff the function parameter PARAM_DECL was expanded
2780 from the function parameter pack PACK. */
2783 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2785 if (DECL_ARTIFICIAL (param_decl
)
2786 || !function_parameter_pack_p (pack
))
2789 /* The parameter pack and its pack arguments have the same
2791 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2794 /* Determine whether ARGS describes a variadic template args list,
2795 i.e., one that is terminated by a template argument pack. */
2798 template_args_variadic_p (tree args
)
2803 if (args
== NULL_TREE
)
2806 args
= INNERMOST_TEMPLATE_ARGS (args
);
2807 nargs
= TREE_VEC_LENGTH (args
);
2812 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2814 return ARGUMENT_PACK_P (last_parm
);
2817 /* Generate a new name for the parameter pack name NAME (an
2818 IDENTIFIER_NODE) that incorporates its */
2821 make_ith_pack_parameter_name (tree name
, int i
)
2823 /* Munge the name to include the parameter index. */
2824 #define NUMBUF_LEN 128
2825 char numbuf
[NUMBUF_LEN
];
2829 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2830 newname_len
= IDENTIFIER_LENGTH (name
)
2831 + strlen (numbuf
) + 2;
2832 newname
= (char*)alloca (newname_len
);
2833 snprintf (newname
, newname_len
,
2834 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2835 return get_identifier (newname
);
2838 /* Return true if T is a primary function
2839 or class template instantiation. */
2842 primary_template_instantiation_p (const_tree t
)
2847 if (TREE_CODE (t
) == FUNCTION_DECL
)
2848 return DECL_LANG_SPECIFIC (t
)
2849 && DECL_TEMPLATE_INSTANTIATION (t
)
2850 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2851 else if (CLASS_TYPE_P (t
))
2852 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2853 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2857 /* Return true if PARM is a template template parameter. */
2860 template_template_parameter_p (const_tree parm
)
2862 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2865 /* Return the template parameters of T if T is a
2866 primary template instantiation, NULL otherwise. */
2869 get_primary_template_innermost_parameters (const_tree t
)
2871 tree parms
= NULL
, template_info
= NULL
;
2873 if ((template_info
= get_template_info (t
))
2874 && primary_template_instantiation_p (t
))
2875 parms
= INNERMOST_TEMPLATE_PARMS
2876 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2881 /* Return the template parameters of the LEVELth level from the full list
2882 of template parameters PARMS. */
2885 get_template_parms_at_level (tree parms
, int level
)
2889 || TREE_CODE (parms
) != TREE_LIST
2890 || level
> TMPL_PARMS_DEPTH (parms
))
2893 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2894 if (TMPL_PARMS_DEPTH (p
) == level
)
2900 /* Returns the template arguments of T if T is a template instantiation,
2904 get_template_innermost_arguments (const_tree t
)
2906 tree args
= NULL
, template_info
= NULL
;
2908 if ((template_info
= get_template_info (t
))
2909 && TI_ARGS (template_info
))
2910 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2915 /* Return the argument pack elements of T if T is a template argument pack,
2919 get_template_argument_pack_elems (const_tree t
)
2921 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2922 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2925 return ARGUMENT_PACK_ARGS (t
);
2928 /* Structure used to track the progress of find_parameter_packs_r. */
2929 struct find_parameter_pack_data
2931 /* TREE_LIST that will contain all of the parameter packs found by
2933 tree
* parameter_packs
;
2935 /* Set of AST nodes that have been visited by the traversal. */
2936 struct pointer_set_t
*visited
;
2939 /* Identifies all of the argument packs that occur in a template
2940 argument and appends them to the TREE_LIST inside DATA, which is a
2941 find_parameter_pack_data structure. This is a subroutine of
2942 make_pack_expansion and uses_parameter_packs. */
2944 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2947 struct find_parameter_pack_data
* ppd
=
2948 (struct find_parameter_pack_data
*)data
;
2949 bool parameter_pack_p
= false;
2951 /* Identify whether this is a parameter pack or not. */
2952 switch (TREE_CODE (t
))
2954 case TEMPLATE_PARM_INDEX
:
2955 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2956 parameter_pack_p
= true;
2959 case TEMPLATE_TYPE_PARM
:
2960 case TEMPLATE_TEMPLATE_PARM
:
2961 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2962 parameter_pack_p
= true;
2966 if (FUNCTION_PARAMETER_PACK_P (t
))
2968 /* We don't want to walk into the type of a PARM_DECL,
2969 because we don't want to see the type parameter pack. */
2971 parameter_pack_p
= true;
2976 /* Not a parameter pack. */
2980 if (parameter_pack_p
)
2982 /* Add this parameter pack to the list. */
2983 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
2987 cp_walk_tree (&TYPE_CONTEXT (t
),
2988 &find_parameter_packs_r
, ppd
, ppd
->visited
);
2990 /* This switch statement will return immediately if we don't find a
2992 switch (TREE_CODE (t
))
2994 case TEMPLATE_PARM_INDEX
:
2997 case BOUND_TEMPLATE_TEMPLATE_PARM
:
2998 /* Check the template itself. */
2999 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3000 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3001 /* Check the template arguments. */
3002 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3007 case TEMPLATE_TYPE_PARM
:
3008 case TEMPLATE_TEMPLATE_PARM
:
3015 if (TYPE_PTRMEMFUNC_P (t
))
3021 if (TYPE_TEMPLATE_INFO (t
))
3022 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3023 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3030 cp_walk_tree (&TREE_TYPE (t
),
3031 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3035 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3040 case TYPE_PACK_EXPANSION
:
3041 case EXPR_PACK_EXPANSION
:
3046 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3051 case IDENTIFIER_NODE
:
3052 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3064 /* Determines if the expression or type T uses any parameter packs. */
3066 uses_parameter_packs (tree t
)
3068 tree parameter_packs
= NULL_TREE
;
3069 struct find_parameter_pack_data ppd
;
3070 ppd
.parameter_packs
= ¶meter_packs
;
3071 ppd
.visited
= pointer_set_create ();
3072 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3073 pointer_set_destroy (ppd
.visited
);
3074 return parameter_packs
!= NULL_TREE
;
3077 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3078 representation a base-class initializer into a parameter pack
3079 expansion. If all goes well, the resulting node will be an
3080 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3083 make_pack_expansion (tree arg
)
3086 tree parameter_packs
= NULL_TREE
;
3087 bool for_types
= false;
3088 struct find_parameter_pack_data ppd
;
3090 if (!arg
|| arg
== error_mark_node
)
3093 if (TREE_CODE (arg
) == TREE_LIST
)
3095 /* The only time we will see a TREE_LIST here is for a base
3096 class initializer. In this case, the TREE_PURPOSE will be a
3097 _TYPE node (representing the base class expansion we're
3098 initializing) and the TREE_VALUE will be a TREE_LIST
3099 containing the initialization arguments.
3101 The resulting expansion looks somewhat different from most
3102 expansions. Rather than returning just one _EXPANSION, we
3103 return a TREE_LIST whose TREE_PURPOSE is a
3104 TYPE_PACK_EXPANSION containing the bases that will be
3105 initialized. The TREE_VALUE will be identical to the
3106 original TREE_VALUE, which is a list of arguments that will
3107 be passed to each base. We do not introduce any new pack
3108 expansion nodes into the TREE_VALUE (although it is possible
3109 that some already exist), because the TREE_PURPOSE and
3110 TREE_VALUE all need to be expanded together with the same
3111 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3112 resulting TREE_PURPOSE will mention the parameter packs in
3113 both the bases and the arguments to the bases. */
3116 tree parameter_packs
= NULL_TREE
;
3118 /* Determine which parameter packs will be used by the base
3120 ppd
.visited
= pointer_set_create ();
3121 ppd
.parameter_packs
= ¶meter_packs
;
3122 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3125 if (parameter_packs
== NULL_TREE
)
3127 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3128 pointer_set_destroy (ppd
.visited
);
3129 return error_mark_node
;
3132 if (TREE_VALUE (arg
) != void_type_node
)
3134 /* Collect the sets of parameter packs used in each of the
3135 initialization arguments. */
3136 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3138 /* Determine which parameter packs will be expanded in this
3140 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3145 pointer_set_destroy (ppd
.visited
);
3147 /* Create the pack expansion type for the base type. */
3148 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3149 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3150 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3152 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3153 they will rarely be compared to anything. */
3154 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3156 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3159 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3162 /* Build the PACK_EXPANSION_* node. */
3164 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3165 : make_node (EXPR_PACK_EXPANSION
);
3166 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3167 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3169 /* Propagate type and const-expression information. */
3170 TREE_TYPE (result
) = TREE_TYPE (arg
);
3171 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3174 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3175 they will rarely be compared to anything. */
3176 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3178 /* Determine which parameter packs will be expanded. */
3179 ppd
.parameter_packs
= ¶meter_packs
;
3180 ppd
.visited
= pointer_set_create ();
3181 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3182 pointer_set_destroy (ppd
.visited
);
3184 /* Make sure we found some parameter packs. */
3185 if (parameter_packs
== NULL_TREE
)
3188 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3190 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3191 return error_mark_node
;
3193 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3198 /* Checks T for any "bare" parameter packs, which have not yet been
3199 expanded, and issues an error if any are found. This operation can
3200 only be done on full expressions or types (e.g., an expression
3201 statement, "if" condition, etc.), because we could have expressions like:
3203 foo(f(g(h(args)))...)
3205 where "args" is a parameter pack. check_for_bare_parameter_packs
3206 should not be called for the subexpressions args, h(args),
3207 g(h(args)), or f(g(h(args))), because we would produce erroneous
3210 Returns TRUE and emits an error if there were bare parameter packs,
3211 returns FALSE otherwise. */
3213 check_for_bare_parameter_packs (tree t
)
3215 tree parameter_packs
= NULL_TREE
;
3216 struct find_parameter_pack_data ppd
;
3218 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3221 if (TREE_CODE (t
) == TYPE_DECL
)
3224 ppd
.parameter_packs
= ¶meter_packs
;
3225 ppd
.visited
= pointer_set_create ();
3226 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3227 pointer_set_destroy (ppd
.visited
);
3229 if (parameter_packs
)
3231 error ("parameter packs not expanded with %<...%>:");
3232 while (parameter_packs
)
3234 tree pack
= TREE_VALUE (parameter_packs
);
3235 tree name
= NULL_TREE
;
3237 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3238 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3239 name
= TYPE_NAME (pack
);
3240 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3241 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3243 name
= DECL_NAME (pack
);
3246 inform (input_location
, " %qD", name
);
3248 inform (input_location
, " <anonymous>");
3250 parameter_packs
= TREE_CHAIN (parameter_packs
);
3259 /* Expand any parameter packs that occur in the template arguments in
3262 expand_template_argument_pack (tree args
)
3264 tree result_args
= NULL_TREE
;
3265 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3266 int num_result_args
= -1;
3267 int non_default_args_count
= -1;
3269 /* First, determine if we need to expand anything, and the number of
3270 slots we'll need. */
3271 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3273 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3274 if (arg
== NULL_TREE
)
3276 if (ARGUMENT_PACK_P (arg
))
3278 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3279 if (num_result_args
< 0)
3280 num_result_args
= in_arg
+ num_packed
;
3282 num_result_args
+= num_packed
;
3286 if (num_result_args
>= 0)
3291 /* If no expansion is necessary, we're done. */
3292 if (num_result_args
< 0)
3295 /* Expand arguments. */
3296 result_args
= make_tree_vec (num_result_args
);
3297 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3298 non_default_args_count
=
3299 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3300 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3302 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3303 if (ARGUMENT_PACK_P (arg
))
3305 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3306 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3307 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3308 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3309 if (non_default_args_count
> 0)
3310 non_default_args_count
+= num_packed
;
3314 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3318 if (non_default_args_count
>= 0)
3319 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3323 /* Checks if DECL shadows a template parameter.
3325 [temp.local]: A template-parameter shall not be redeclared within its
3326 scope (including nested scopes).
3328 Emits an error and returns TRUE if the DECL shadows a parameter,
3329 returns FALSE otherwise. */
3332 check_template_shadow (tree decl
)
3336 /* If we're not in a template, we can't possibly shadow a template
3338 if (!current_template_parms
)
3341 /* Figure out what we're shadowing. */
3342 if (TREE_CODE (decl
) == OVERLOAD
)
3343 decl
= OVL_CURRENT (decl
);
3344 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3346 /* If there's no previous binding for this name, we're not shadowing
3347 anything, let alone a template parameter. */
3351 /* If we're not shadowing a template parameter, we're done. Note
3352 that OLDDECL might be an OVERLOAD (or perhaps even an
3353 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3355 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3358 /* We check for decl != olddecl to avoid bogus errors for using a
3359 name inside a class. We check TPFI to avoid duplicate errors for
3360 inline member templates. */
3362 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3365 error ("declaration of %q+#D", decl
);
3366 error (" shadows template parm %q+#D", olddecl
);
3370 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3371 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3372 template parameters. */
3375 build_template_parm_index (int index
,
3382 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3383 TEMPLATE_PARM_IDX (t
) = index
;
3384 TEMPLATE_PARM_LEVEL (t
) = level
;
3385 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3386 TEMPLATE_PARM_NUM_SIBLINGS (t
) = num_siblings
;
3387 TEMPLATE_PARM_DECL (t
) = decl
;
3388 TREE_TYPE (t
) = type
;
3389 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3390 TREE_READONLY (t
) = TREE_READONLY (decl
);
3395 /* Find the canonical type parameter for the given template type
3396 parameter. Returns the canonical type parameter, which may be TYPE
3397 if no such parameter existed. */
3400 canonical_type_parameter (tree type
)
3403 int idx
= TEMPLATE_TYPE_IDX (type
);
3404 if (!canonical_template_parms
)
3405 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3407 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3408 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3410 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3411 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3412 list
= TREE_CHAIN (list
);
3415 return TREE_VALUE (list
);
3418 VEC_replace(tree
, canonical_template_parms
, idx
,
3419 tree_cons (NULL_TREE
, type
,
3420 VEC_index (tree
, canonical_template_parms
, idx
)));
3425 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3426 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3427 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3428 new one is created. */
3431 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3432 tsubst_flags_t complain
)
3434 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3435 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3436 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3437 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3439 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3442 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3443 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3444 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3445 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3446 DECL_ARTIFICIAL (decl
) = 1;
3447 SET_DECL_TEMPLATE_PARM_P (decl
);
3449 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3450 TEMPLATE_PARM_LEVEL (index
) - levels
,
3451 TEMPLATE_PARM_ORIG_LEVEL (index
),
3452 TEMPLATE_PARM_NUM_SIBLINGS (index
),
3454 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3455 TEMPLATE_PARM_PARAMETER_PACK (t
)
3456 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3458 /* Template template parameters need this. */
3459 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3460 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3461 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3465 return TEMPLATE_PARM_DESCENDANTS (index
);
3468 /* Process information from new template parameter PARM and append it
3469 to the LIST being built. This new parameter is a non-type
3470 parameter iff IS_NON_TYPE is true. This new parameter is a
3471 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3472 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3473 parameter list PARM belongs to. This is used used to create a
3474 proper canonical type for the type of PARM that is to be created,
3475 iff PARM is a type. If the size is not known, this parameter shall
3479 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3480 bool is_non_type
, bool is_parameter_pack
,
3481 unsigned num_template_parms
)
3488 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3489 defval
= TREE_PURPOSE (parm
);
3493 tree p
= tree_last (list
);
3495 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3498 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3499 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3501 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3511 parm
= TREE_VALUE (parm
);
3513 SET_DECL_TEMPLATE_PARM_P (parm
);
3515 if (TREE_TYPE (parm
) == error_mark_node
)
3517 err_parm_list
= build_tree_list (defval
, parm
);
3518 TREE_VALUE (err_parm_list
) = error_mark_node
;
3519 return chainon (list
, err_parm_list
);
3525 The top-level cv-qualifiers on the template-parameter are
3526 ignored when determining its type. */
3527 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3528 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3530 err_parm_list
= build_tree_list (defval
, parm
);
3531 TREE_VALUE (err_parm_list
) = error_mark_node
;
3532 return chainon (list
, err_parm_list
);
3535 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3537 /* This template parameter is not a parameter pack, but it
3538 should be. Complain about "bare" parameter packs. */
3539 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3541 /* Recover by calling this a parameter pack. */
3542 is_parameter_pack
= true;
3546 /* A template parameter is not modifiable. */
3547 TREE_CONSTANT (parm
) = 1;
3548 TREE_READONLY (parm
) = 1;
3549 decl
= build_decl (parm_loc
,
3550 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3551 TREE_CONSTANT (decl
) = 1;
3552 TREE_READONLY (decl
) = 1;
3553 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3554 = build_template_parm_index (idx
, processing_template_decl
,
3555 processing_template_decl
,
3557 decl
, TREE_TYPE (parm
));
3559 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3560 = is_parameter_pack
;
3565 parm
= TREE_VALUE (TREE_VALUE (parm
));
3567 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3569 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3570 /* This is for distinguishing between real templates and template
3571 template parameters */
3572 TREE_TYPE (parm
) = t
;
3573 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3578 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3579 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3580 decl
= build_decl (parm_loc
,
3581 TYPE_DECL
, parm
, t
);
3584 TYPE_NAME (t
) = decl
;
3585 TYPE_STUB_DECL (t
) = decl
;
3587 TEMPLATE_TYPE_PARM_INDEX (t
)
3588 = build_template_parm_index (idx
, processing_template_decl
,
3589 processing_template_decl
,
3591 decl
, TREE_TYPE (parm
));
3592 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3593 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3595 DECL_ARTIFICIAL (decl
) = 1;
3596 SET_DECL_TEMPLATE_PARM_P (decl
);
3598 parm
= build_tree_list (defval
, parm
);
3599 return chainon (list
, parm
);
3602 /* The end of a template parameter list has been reached. Process the
3603 tree list into a parameter vector, converting each parameter into a more
3604 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3608 end_template_parm_list (tree parms
)
3612 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3614 current_template_parms
3615 = tree_cons (size_int (processing_template_decl
),
3616 saved_parmlist
, current_template_parms
);
3618 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3620 next
= TREE_CHAIN (parm
);
3621 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3622 TREE_CHAIN (parm
) = NULL_TREE
;
3625 --processing_template_parmlist
;
3627 return saved_parmlist
;
3630 /* Create a new type almost identical to TYPE but which has the
3631 following differences:
3633 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3634 template sibling parameters of T.
3636 2/ T has a new canonical type that matches the new number
3639 3/ From now on, T is going to be what lookups referring to the
3640 name of TYPE will return. No lookup should return TYPE anymore.
3642 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3644 This is a subroutine of fixup_template_parms. */
3647 fixup_template_type_parm_type (tree type
, int num_parms
)
3649 tree orig_idx
= TEMPLATE_TYPE_PARM_INDEX (type
), idx
;
3651 /* This is the decl which name is inserted into the symbol table for
3652 the template parm type. So whenever we lookup the type name, this
3653 is the DECL we get. */
3656 /* Do not fix up the type twice. */
3657 if (orig_idx
&& TEMPLATE_PARM_NUM_SIBLINGS (orig_idx
) != 0)
3660 t
= copy_type (type
);
3661 decl
= TYPE_NAME (t
);
3663 TYPE_MAIN_VARIANT (t
) = t
;
3664 TYPE_NEXT_VARIANT (t
)= NULL_TREE
;
3665 TYPE_POINTER_TO (t
) = 0;
3666 TYPE_REFERENCE_TO (t
) = 0;
3668 idx
= build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx
),
3669 TEMPLATE_PARM_LEVEL (orig_idx
),
3670 TEMPLATE_PARM_ORIG_LEVEL (orig_idx
),
3673 TEMPLATE_PARM_DESCENDANTS (idx
) = TEMPLATE_PARM_DESCENDANTS (orig_idx
);
3674 TEMPLATE_PARM_PARAMETER_PACK (idx
) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx
);
3675 TEMPLATE_TYPE_PARM_INDEX (t
) = idx
;
3677 TYPE_STUB_DECL (t
) = decl
;
3678 TEMPLATE_TYPE_DECL (t
) = decl
;
3679 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
3680 TREE_TYPE (DECL_TEMPLATE_RESULT (decl
)) = t
;
3682 /* Update the type associated to the type name stored in the symbol
3683 table. Now, whenever the type name is looked up, the resulting
3684 type is properly fixed up. */
3685 TREE_TYPE (decl
) = t
;
3687 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3692 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3693 identical to I, but that is fixed up as to:
3695 1/ carry the number of sibling parms (NUM_PARMS) of the template
3696 parm represented by I.
3698 2/ replace all references to template parm types declared before I
3699 (in the same template parm list as I) by references to template
3700 parm types contained in ARGS. ARGS should contain the list of
3701 template parms that have been fixed up so far, in a form suitable
3702 to be passed to tsubst.
3704 This is a subroutine of fixup_template_parms. */
3707 fixup_template_parm_index (tree i
, tree args
, int num_parms
)
3709 tree index
, decl
, type
;
3712 || TREE_CODE (i
) != TEMPLATE_PARM_INDEX
3713 /* Do not fix up the index twice. */
3714 || (TEMPLATE_PARM_NUM_SIBLINGS (i
) != 0))
3717 decl
= TEMPLATE_PARM_DECL (i
);
3718 type
= TREE_TYPE (decl
);
3720 index
= build_template_parm_index (TEMPLATE_PARM_IDX (i
),
3721 TEMPLATE_PARM_LEVEL (i
),
3722 TEMPLATE_PARM_ORIG_LEVEL (i
),
3726 TEMPLATE_PARM_DESCENDANTS (index
) = TEMPLATE_PARM_DESCENDANTS (i
);
3727 TEMPLATE_PARM_PARAMETER_PACK (index
) = TEMPLATE_PARM_PARAMETER_PACK (i
);
3729 type
= tsubst (type
, args
, tf_none
, NULL_TREE
);
3731 TREE_TYPE (decl
) = type
;
3732 TREE_TYPE (index
) = type
;
3738 This is a subroutine of fixup_template_parms.
3740 It computes the canonical type of the type of the template
3741 parameter PARM_DESC and update all references to that type so that
3742 they use the newly computed canonical type. No access check is
3743 performed during the fixup. PARM_DESC is a TREE_LIST which
3744 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3745 default argument of the template parm if any. IDX is the index of
3746 the template parameter, starting at 0. NUM_PARMS is the number of
3747 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3748 TREE_VEC containing the full set of template parameters in a form
3749 suitable to be passed to substs functions as their ARGS
3750 argument. This is what current_template_args returns for a given
3751 template. The innermost vector of args in ARGLIST is the set of
3752 template parms that have been fixed up so far. This function adds
3753 the fixed up parameter into that vector. */
3756 fixup_template_parm (tree parm_desc
,
3761 tree parm
= TREE_VALUE (parm_desc
);
3762 tree fixedup_args
= INNERMOST_TEMPLATE_ARGS (arglist
);
3764 push_deferring_access_checks (dk_no_check
);
3766 if (TREE_CODE (parm
) == TYPE_DECL
)
3768 /* PARM is a template type parameter. Fix up its type, add
3769 the fixed-up template parm to the vector of fixed-up
3770 template parms so far, and substitute the fixed-up
3771 template parms into the default argument of this
3774 fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3775 TREE_TYPE (parm
) = t
;
3777 TREE_VEC_ELT (fixedup_args
, idx
) = template_parm_to_arg (parm_desc
);
3779 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3781 /* PARM is a template template parameter. This is going to
3783 tree tparms
, targs
, innermost_args
, t
;
3786 /* First, fix up the parms of the template template parm
3787 because the parms are involved in defining the new canonical
3788 type of the template template parm. */
3790 /* So we need to substitute the template parm types that have
3791 been fixed up so far into the template parms of this template
3792 template parm. E.g, consider this:
3794 template<class T, template<T u> class TT> class S;
3796 In this case we want to substitute T into the
3797 template parameters of TT.
3799 So let's walk the template parms of PARM here, and
3800 tsubst ARGLIST into into each of the template
3803 /* For this substitution we need to build the full set of
3804 template parameters and use that as arguments for the
3805 tsubsting function. */
3806 tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
3808 /* This will contain the innermost parms of PARM into which
3809 we have substituted so far. */
3810 innermost_args
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
3811 targs
= add_to_template_args (arglist
, innermost_args
);
3812 for (j
= 0; j
< TREE_VEC_LENGTH (tparms
); ++j
)
3816 parameter
= TREE_VEC_ELT (tparms
, j
);
3818 /* INNERMOST_ARGS needs to have at least the same number
3819 of elements as the index PARAMETER, ortherwise
3820 tsubsting into PARAMETER will result in partially
3821 instantiating it, reducing its tempate parm
3822 level. Let's tactically fill INNERMOST_ARGS for that
3824 TREE_VEC_ELT (innermost_args
, j
) =
3825 template_parm_to_arg (parameter
);
3827 fixup_template_parm (parameter
, j
,
3828 TREE_VEC_LENGTH (tparms
),
3832 /* Now fix up the type of the template template parm. */
3834 t
= fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3835 TREE_TYPE (parm
) = t
;
3837 TREE_VEC_ELT (fixedup_args
, idx
) =
3838 template_parm_to_arg (parm_desc
);
3840 else if (TREE_CODE (parm
) == PARM_DECL
)
3842 /* PARM is a non-type template parameter. We need to:
3844 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3845 proper number of sibling parameters.
3847 * Make lookups of the template parameter return a reference
3848 to the fixed-up index. No lookup should return references
3849 to the former index anymore.
3851 * Substitute the template parms that got fixed up so far
3853 * into the type of PARM. */
3855 tree index
= DECL_INITIAL (parm
);
3857 /* PUSHED_DECL is the decl added to the symbol table with
3858 the name of the parameter. E,g:
3860 template<class T, T u> //#0
3861 auto my_function(T t) -> decltype(u); //#1
3863 Here, when looking up u at //#1, we get the decl of u
3864 resulting from the declaration in #0. This is what
3865 PUSHED_DECL is. We need to replace the reference to the
3866 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3867 fixed-up TEMPLATE_PARM_INDEX. */
3868 tree pushed_decl
= TEMPLATE_PARM_DECL (index
);
3870 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3871 fixup the type of PUSHED_DECL as well and luckily
3872 fixup_template_parm_index does it for us too. */
3873 tree fixed_up_index
=
3874 fixup_template_parm_index (index
, arglist
, num_parms
);
3876 DECL_INITIAL (pushed_decl
) = DECL_INITIAL (parm
) = fixed_up_index
;
3878 /* Add this fixed up PARM to the template parms we've fixed
3879 up so far and use that to substitute the fixed-up
3880 template parms into the type of PARM. */
3881 TREE_VEC_ELT (fixedup_args
, idx
) =
3882 template_parm_to_arg (parm_desc
);
3883 TREE_TYPE (parm
) = tsubst (TREE_TYPE (parm
), arglist
,
3884 tf_none
, NULL_TREE
);
3887 TREE_PURPOSE (parm_desc
) =
3888 tsubst_template_arg (TREE_PURPOSE (parm_desc
),
3889 arglist
, tf_none
, parm
);
3891 pop_deferring_access_checks ();
3894 /* Walk the current template parms and properly compute the canonical
3895 types of the dependent types created during
3896 cp_parser_template_parameter_list. */
3899 fixup_template_parms (void)
3906 parameter_vec
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3907 if (parameter_vec
== NULL_TREE
)
3910 num_parms
= TREE_VEC_LENGTH (parameter_vec
);
3912 /* This vector contains the current innermost template parms that
3913 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3914 to be passed to tsubst* functions as their ARGS argument. */
3915 fixedup_args
= make_tree_vec (num_parms
);
3917 /* This vector contains the full set of template parms in a form
3918 suitable to be passed to substs functions as their ARGS
3920 arglist
= current_template_args ();
3921 arglist
= add_outermost_template_args (arglist
, fixedup_args
);
3923 /* Let's do the proper fixup now. */
3924 for (i
= 0; i
< num_parms
; ++i
)
3925 fixup_template_parm (TREE_VEC_ELT (parameter_vec
, i
),
3926 i
, num_parms
, arglist
);
3929 /* end_template_decl is called after a template declaration is seen. */
3932 end_template_decl (void)
3934 reset_specialization ();
3936 if (! processing_template_decl
)
3939 /* This matches the pushlevel in begin_template_parm_list. */
3942 --processing_template_decl
;
3943 current_template_parms
= TREE_CHAIN (current_template_parms
);
3946 /* Takes a TREE_LIST representing a template parameter and convert it
3947 into an argument suitable to be passed to the type substitution
3948 functions. Note that If the TREE_LIST contains an error_mark
3949 node, the returned argument is error_mark_node. */
3952 template_parm_to_arg (tree t
)
3956 || TREE_CODE (t
) != TREE_LIST
)
3959 if (error_operand_p (TREE_VALUE (t
)))
3960 return error_mark_node
;
3964 if (TREE_CODE (t
) == TYPE_DECL
3965 || TREE_CODE (t
) == TEMPLATE_DECL
)
3969 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3971 /* Turn this argument into a TYPE_ARGUMENT_PACK
3972 with a single element, which expands T. */
3973 tree vec
= make_tree_vec (1);
3974 #ifdef ENABLE_CHECKING
3975 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3976 (vec
, TREE_VEC_LENGTH (vec
));
3978 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3980 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3981 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3986 t
= DECL_INITIAL (t
);
3988 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3990 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3991 with a single element, which expands T. */
3992 tree vec
= make_tree_vec (1);
3993 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3994 #ifdef ENABLE_CHECKING
3995 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3996 (vec
, TREE_VEC_LENGTH (vec
));
3998 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4000 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4001 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4002 TREE_TYPE (t
) = type
;
4008 /* Within the declaration of a template, return all levels of template
4009 parameters that apply. The template parameters are represented as
4010 a TREE_VEC, in the form documented in cp-tree.h for template
4014 current_template_args (void)
4017 tree args
= NULL_TREE
;
4018 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
4021 /* If there is only one level of template parameters, we do not
4022 create a TREE_VEC of TREE_VECs. Instead, we return a single
4023 TREE_VEC containing the arguments. */
4025 args
= make_tree_vec (length
);
4027 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
4029 tree a
= copy_node (TREE_VALUE (header
));
4032 TREE_TYPE (a
) = NULL_TREE
;
4033 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4034 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4036 #ifdef ENABLE_CHECKING
4037 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4041 TREE_VEC_ELT (args
, --l
) = a
;
4046 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
4047 /* This can happen for template parms of a template template
4050 template<template<class T, class U> class TT> struct S;
4052 Consider the level of the parms of TT; T and U both have
4053 level 2; TT has no template parm of level 1. So in this case
4054 the first element of full_template_args is NULL_TREE. If we
4055 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4056 of 2. This will make tsubst wrongly consider that T and U
4057 have level 1. Instead, let's create a dummy vector as the
4058 first element of full_template_args so that TMPL_ARG_DEPTH
4059 returns the correct depth for args. */
4060 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
4064 /* Update the declared TYPE by doing any lookups which were thought to be
4065 dependent, but are not now that we know the SCOPE of the declarator. */
4068 maybe_update_decl_type (tree orig_type
, tree scope
)
4070 tree type
= orig_type
;
4072 if (type
== NULL_TREE
)
4075 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4076 type
= TREE_TYPE (type
);
4078 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4079 && dependent_type_p (type
)
4080 /* Don't bother building up the args in this case. */
4081 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4083 /* tsubst in the args corresponding to the template parameters,
4084 including auto if present. Most things will be unchanged, but
4085 make_typename_type and tsubst_qualified_id will resolve
4086 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4087 tree args
= current_template_args ();
4088 tree auto_node
= type_uses_auto (type
);
4092 tree auto_vec
= make_tree_vec (1);
4093 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4094 args
= add_to_template_args (args
, auto_vec
);
4096 pushed
= push_scope (scope
);
4097 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4102 if (type
== error_mark_node
)
4105 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4107 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4110 type
= TYPE_NAME (type
);
4115 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4116 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4117 a member template. Used by push_template_decl below. */
4120 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4122 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4123 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4124 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4125 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4126 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4131 struct template_parm_data
4133 /* The level of the template parameters we are currently
4137 /* The index of the specialization argument we are currently
4141 /* An array whose size is the number of template parameters. The
4142 elements are nonzero if the parameter has been used in any one
4143 of the arguments processed so far. */
4146 /* An array whose size is the number of template arguments. The
4147 elements are nonzero if the argument makes use of template
4148 parameters of this level. */
4149 int* arg_uses_template_parms
;
4152 /* Subroutine of push_template_decl used to see if each template
4153 parameter in a partial specialization is used in the explicit
4154 argument list. If T is of the LEVEL given in DATA (which is
4155 treated as a template_parm_data*), then DATA->PARMS is marked
4159 mark_template_parm (tree t
, void* data
)
4163 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4165 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4167 level
= TEMPLATE_PARM_LEVEL (t
);
4168 idx
= TEMPLATE_PARM_IDX (t
);
4172 level
= TEMPLATE_TYPE_LEVEL (t
);
4173 idx
= TEMPLATE_TYPE_IDX (t
);
4176 if (level
== tpd
->level
)
4178 tpd
->parms
[idx
] = 1;
4179 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4182 /* Return zero so that for_each_template_parm will continue the
4183 traversal of the tree; we want to mark *every* template parm. */
4187 /* Process the partial specialization DECL. */
4190 process_partial_specialization (tree decl
)
4192 tree type
= TREE_TYPE (decl
);
4193 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4194 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4195 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4196 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4199 int nargs
= TREE_VEC_LENGTH (inner_args
);
4202 bool did_error_intro
= false;
4203 struct template_parm_data tpd
;
4204 struct template_parm_data tpd2
;
4206 gcc_assert (current_template_parms
);
4208 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4209 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4211 /* We check that each of the template parameters given in the
4212 partial specialization is used in the argument list to the
4213 specialization. For example:
4215 template <class T> struct S;
4216 template <class T> struct S<T*>;
4218 The second declaration is OK because `T*' uses the template
4219 parameter T, whereas
4221 template <class T> struct S<int>;
4223 is no good. Even trickier is:
4234 The S2<T> declaration is actually invalid; it is a
4235 full-specialization. Of course,
4238 struct S2<T (*)(U)>;
4240 or some such would have been OK. */
4241 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4242 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4243 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4245 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4246 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4247 for (i
= 0; i
< nargs
; ++i
)
4249 tpd
.current_arg
= i
;
4250 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4251 &mark_template_parm
,
4254 /*include_nondeduced_p=*/false);
4256 for (i
= 0; i
< ntparms
; ++i
)
4257 if (tpd
.parms
[i
] == 0)
4259 /* One of the template parms was not used in the
4261 if (!did_error_intro
)
4263 error ("template parameters not used in partial specialization:");
4264 did_error_intro
= true;
4267 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4270 if (did_error_intro
)
4271 return error_mark_node
;
4273 /* [temp.class.spec]
4275 The argument list of the specialization shall not be identical to
4276 the implicit argument list of the primary template. */
4277 if (comp_template_args
4279 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4281 error ("partial specialization %qT does not specialize any template arguments", type
);
4283 /* [temp.class.spec]
4285 A partially specialized non-type argument expression shall not
4286 involve template parameters of the partial specialization except
4287 when the argument expression is a simple identifier.
4289 The type of a template parameter corresponding to a specialized
4290 non-type argument shall not be dependent on a parameter of the
4293 Also, we verify that pack expansions only occur at the
4294 end of the argument list. */
4295 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4297 for (i
= 0; i
< nargs
; ++i
)
4299 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4300 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4301 tree packed_args
= NULL_TREE
;
4304 if (ARGUMENT_PACK_P (arg
))
4306 /* Extract the arguments from the argument pack. We'll be
4307 iterating over these in the following loop. */
4308 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4309 len
= TREE_VEC_LENGTH (packed_args
);
4312 for (j
= 0; j
< len
; j
++)
4315 /* Get the Jth argument in the parameter pack. */
4316 arg
= TREE_VEC_ELT (packed_args
, j
);
4318 if (PACK_EXPANSION_P (arg
))
4320 /* Pack expansions must come at the end of the
4322 if ((packed_args
&& j
< len
- 1)
4323 || (!packed_args
&& i
< nargs
- 1))
4325 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4326 error ("parameter pack argument %qE must be at the "
4327 "end of the template argument list", arg
);
4329 error ("parameter pack argument %qT must be at the "
4330 "end of the template argument list", arg
);
4334 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4335 /* We only care about the pattern. */
4336 arg
= PACK_EXPANSION_PATTERN (arg
);
4338 if (/* These first two lines are the `non-type' bit. */
4340 && TREE_CODE (arg
) != TEMPLATE_DECL
4341 /* This next line is the `argument expression is not just a
4342 simple identifier' condition and also the `specialized
4343 non-type argument' bit. */
4344 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4346 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4347 || (packed_args
&& uses_template_parms (arg
)))
4348 error ("template argument %qE involves template parameter(s)",
4352 /* Look at the corresponding template parameter,
4353 marking which template parameters its type depends
4355 tree type
= TREE_TYPE (parm
);
4359 /* We haven't yet initialized TPD2. Do so now. */
4360 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4361 /* The number of parameters here is the number in the
4362 main template, which, as checked in the assertion
4364 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4366 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4369 /* Mark the template parameters. But this time, we're
4370 looking for the template parameters of the main
4371 template, not in the specialization. */
4372 tpd2
.current_arg
= i
;
4373 tpd2
.arg_uses_template_parms
[i
] = 0;
4374 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4375 for_each_template_parm (type
,
4376 &mark_template_parm
,
4379 /*include_nondeduced_p=*/false);
4381 if (tpd2
.arg_uses_template_parms
[i
])
4383 /* The type depended on some template parameters.
4384 If they are fully specialized in the
4385 specialization, that's OK. */
4388 for (j
= 0; j
< nargs
; ++j
)
4389 if (tpd2
.parms
[j
] != 0
4390 && tpd
.arg_uses_template_parms
[j
])
4393 error_n (input_location
, count
,
4394 "type %qT of template argument %qE depends "
4395 "on a template parameter",
4396 "type %qT of template argument %qE depends "
4397 "on template parameters",
4406 /* We should only get here once. */
4407 gcc_assert (!COMPLETE_TYPE_P (type
));
4409 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4410 = tree_cons (specargs
, inner_parms
,
4411 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4412 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4414 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4415 inst
= TREE_CHAIN (inst
))
4417 tree inst_type
= TREE_VALUE (inst
);
4418 if (COMPLETE_TYPE_P (inst_type
)
4419 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4421 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4422 if (spec
&& TREE_TYPE (spec
) == type
)
4423 permerror (input_location
,
4424 "partial specialization of %qT after instantiation "
4425 "of %qT", type
, inst_type
);
4432 /* Check that a template declaration's use of default arguments and
4433 parameter packs is not invalid. Here, PARMS are the template
4434 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4435 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4439 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4440 declaration (but not a definition); 1 indicates a declaration, 2
4441 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4442 emitted for extraneous default arguments.
4444 Returns TRUE if there were no errors found, FALSE otherwise. */
4447 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
4448 int is_partial
, int is_friend_decl
)
4451 int last_level_to_check
;
4453 bool no_errors
= true;
4457 A default template-argument shall not be specified in a
4458 function template declaration or a function template definition, nor
4459 in the template-parameter-list of the definition of a member of a
4462 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4463 /* You can't have a function template declaration in a local
4464 scope, nor you can you define a member of a class template in a
4468 if (current_class_type
4469 && !TYPE_BEING_DEFINED (current_class_type
)
4470 && DECL_LANG_SPECIFIC (decl
)
4471 && DECL_DECLARES_FUNCTION_P (decl
)
4472 /* If this is either a friend defined in the scope of the class
4473 or a member function. */
4474 && (DECL_FUNCTION_MEMBER_P (decl
)
4475 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4476 : DECL_FRIEND_CONTEXT (decl
)
4477 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4479 /* And, if it was a member function, it really was defined in
4480 the scope of the class. */
4481 && (!DECL_FUNCTION_MEMBER_P (decl
)
4482 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4483 /* We already checked these parameters when the template was
4484 declared, so there's no need to do it again now. This function
4485 was defined in class scope, but we're processing it's body now
4486 that the class is complete. */
4489 /* Core issue 226 (C++0x only): the following only applies to class
4491 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4495 If a template-parameter has a default template-argument, all
4496 subsequent template-parameters shall have a default
4497 template-argument supplied. */
4498 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4500 tree inner_parms
= TREE_VALUE (parm_level
);
4501 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4502 int seen_def_arg_p
= 0;
4505 for (i
= 0; i
< ntparms
; ++i
)
4507 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4509 if (parm
== error_mark_node
)
4512 if (TREE_PURPOSE (parm
))
4514 else if (seen_def_arg_p
4515 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4517 error ("no default argument for %qD", TREE_VALUE (parm
));
4518 /* For better subsequent error-recovery, we indicate that
4519 there should have been a default argument. */
4520 TREE_PURPOSE (parm
) = error_mark_node
;
4526 /* Don't complain about an enclosing partial
4528 && parm_level
== parms
4529 && TREE_CODE (decl
) == TYPE_DECL
4531 && template_parameter_pack_p (TREE_VALUE (parm
)))
4533 /* A primary class template can only have one
4534 parameter pack, at the end of the template
4537 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4538 error ("parameter pack %qE must be at the end of the"
4539 " template parameter list", TREE_VALUE (parm
));
4541 error ("parameter pack %qT must be at the end of the"
4542 " template parameter list",
4543 TREE_TYPE (TREE_VALUE (parm
)));
4545 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4553 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4557 /* For an ordinary class template, default template arguments are
4558 allowed at the innermost level, e.g.:
4559 template <class T = int>
4561 but, in a partial specialization, they're not allowed even
4562 there, as we have in [temp.class.spec]:
4564 The template parameter list of a specialization shall not
4565 contain default template argument values.
4567 So, for a partial specialization, or for a function template
4568 (in C++98/C++03), we look at all of them. */
4571 /* But, for a primary class template that is not a partial
4572 specialization we look at all template parameters except the
4574 parms
= TREE_CHAIN (parms
);
4576 /* Figure out what error message to issue. */
4577 if (is_friend_decl
== 2)
4578 msg
= G_("default template arguments may not be used in function template "
4579 "friend re-declaration");
4580 else if (is_friend_decl
)
4581 msg
= G_("default template arguments may not be used in function template "
4582 "friend declarations");
4583 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4584 msg
= G_("default template arguments may not be used in function templates "
4585 "without -std=c++0x or -std=gnu++0x");
4586 else if (is_partial
)
4587 msg
= G_("default template arguments may not be used in "
4588 "partial specializations");
4590 msg
= G_("default argument for template parameter for class enclosing %qD");
4592 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4593 /* If we're inside a class definition, there's no need to
4594 examine the parameters to the class itself. On the one
4595 hand, they will be checked when the class is defined, and,
4596 on the other, default arguments are valid in things like:
4597 template <class T = double>
4598 struct S { template <class U> void f(U); };
4599 Here the default argument for `S' has no bearing on the
4600 declaration of `f'. */
4601 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4603 /* Check everything. */
4604 last_level_to_check
= 0;
4606 for (parm_level
= parms
;
4607 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4608 parm_level
= TREE_CHAIN (parm_level
))
4610 tree inner_parms
= TREE_VALUE (parm_level
);
4614 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4615 for (i
= 0; i
< ntparms
; ++i
)
4617 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4620 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4625 if (is_friend_decl
== 2)
4632 /* Clear out the default argument so that we are not
4634 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4638 /* At this point, if we're still interested in issuing messages,
4639 they must apply to classes surrounding the object declared. */
4641 msg
= G_("default argument for template parameter for class "
4648 /* Worker for push_template_decl_real, called via
4649 for_each_template_parm. DATA is really an int, indicating the
4650 level of the parameters we are interested in. If T is a template
4651 parameter of that level, return nonzero. */
4654 template_parm_this_level_p (tree t
, void* data
)
4656 int this_level
= *(int *)data
;
4659 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4660 level
= TEMPLATE_PARM_LEVEL (t
);
4662 level
= TEMPLATE_TYPE_LEVEL (t
);
4663 return level
== this_level
;
4666 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4667 parameters given by current_template_args, or reuses a
4668 previously existing one, if appropriate. Returns the DECL, or an
4669 equivalent one, if it is replaced via a call to duplicate_decls.
4671 If IS_FRIEND is true, DECL is a friend declaration. */
4674 push_template_decl_real (tree decl
, bool is_friend
)
4682 int new_template_p
= 0;
4683 /* True if the template is a member template, in the sense of
4685 bool member_template_p
= false;
4687 if (decl
== error_mark_node
|| !current_template_parms
)
4688 return error_mark_node
;
4690 /* See if this is a partial specialization. */
4691 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4692 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4693 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4695 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4699 /* For a friend, we want the context of the friend function, not
4700 the type of which it is a friend. */
4701 ctx
= CP_DECL_CONTEXT (decl
);
4702 else if (CP_DECL_CONTEXT (decl
)
4703 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4704 /* In the case of a virtual function, we want the class in which
4706 ctx
= CP_DECL_CONTEXT (decl
);
4708 /* Otherwise, if we're currently defining some class, the DECL
4709 is assumed to be a member of the class. */
4710 ctx
= current_scope ();
4712 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4715 if (!DECL_CONTEXT (decl
))
4716 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4718 /* See if this is a primary template. */
4719 if (is_friend
&& ctx
)
4720 /* A friend template that specifies a class context, i.e.
4721 template <typename T> friend void A<T>::f();
4725 primary
= template_parm_scope_p ();
4729 if (DECL_CLASS_SCOPE_P (decl
))
4730 member_template_p
= true;
4731 if (TREE_CODE (decl
) == TYPE_DECL
4732 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4734 error ("template class without a name");
4735 return error_mark_node
;
4737 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4739 if (DECL_DESTRUCTOR_P (decl
))
4743 A destructor shall not be a member template. */
4744 error ("destructor %qD declared as member template", decl
);
4745 return error_mark_node
;
4747 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4748 && (!prototype_p (TREE_TYPE (decl
))
4749 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4750 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4751 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4752 == void_list_node
)))
4754 /* [basic.stc.dynamic.allocation]
4756 An allocation function can be a function
4757 template. ... Template allocation functions shall
4758 have two or more parameters. */
4759 error ("invalid template declaration of %qD", decl
);
4760 return error_mark_node
;
4763 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4764 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4768 error ("template declaration of %q#D", decl
);
4769 return error_mark_node
;
4773 /* Check to see that the rules regarding the use of default
4774 arguments are not being violated. */
4775 check_default_tmpl_args (decl
, current_template_parms
,
4776 primary
, is_partial
, /*is_friend_decl=*/0);
4778 /* Ensure that there are no parameter packs in the type of this
4779 declaration that have not been expanded. */
4780 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4782 /* Check each of the arguments individually to see if there are
4783 any bare parameter packs. */
4784 tree type
= TREE_TYPE (decl
);
4785 tree arg
= DECL_ARGUMENTS (decl
);
4786 tree argtype
= TYPE_ARG_TYPES (type
);
4788 while (arg
&& argtype
)
4790 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4791 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4793 /* This is a PARM_DECL that contains unexpanded parameter
4794 packs. We have already complained about this in the
4795 check_for_bare_parameter_packs call, so just replace
4796 these types with ERROR_MARK_NODE. */
4797 TREE_TYPE (arg
) = error_mark_node
;
4798 TREE_VALUE (argtype
) = error_mark_node
;
4801 arg
= DECL_CHAIN (arg
);
4802 argtype
= TREE_CHAIN (argtype
);
4805 /* Check for bare parameter packs in the return type and the
4806 exception specifiers. */
4807 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4808 /* Errors were already issued, set return type to int
4809 as the frontend doesn't expect error_mark_node as
4811 TREE_TYPE (type
) = integer_type_node
;
4812 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4813 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4815 else if (check_for_bare_parameter_packs (TREE_TYPE (decl
)))
4817 TREE_TYPE (decl
) = error_mark_node
;
4818 return error_mark_node
;
4822 return process_partial_specialization (decl
);
4824 args
= current_template_args ();
4827 || TREE_CODE (ctx
) == FUNCTION_DECL
4828 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4829 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4831 if (DECL_LANG_SPECIFIC (decl
)
4832 && DECL_TEMPLATE_INFO (decl
)
4833 && DECL_TI_TEMPLATE (decl
))
4834 tmpl
= DECL_TI_TEMPLATE (decl
);
4835 /* If DECL is a TYPE_DECL for a class-template, then there won't
4836 be DECL_LANG_SPECIFIC. The information equivalent to
4837 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4838 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4839 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4840 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4842 /* Since a template declaration already existed for this
4843 class-type, we must be redeclaring it here. Make sure
4844 that the redeclaration is valid. */
4845 redeclare_class_template (TREE_TYPE (decl
),
4846 current_template_parms
);
4847 /* We don't need to create a new TEMPLATE_DECL; just use the
4848 one we already had. */
4849 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4853 tmpl
= build_template_decl (decl
, current_template_parms
,
4857 if (DECL_LANG_SPECIFIC (decl
)
4858 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4860 /* A specialization of a member template of a template
4862 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4863 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4864 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4870 tree a
, t
, current
, parms
;
4872 tree tinfo
= get_template_info (decl
);
4876 error ("template definition of non-template %q#D", decl
);
4877 return error_mark_node
;
4880 tmpl
= TI_TEMPLATE (tinfo
);
4882 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4883 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4884 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4885 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4889 /* The declaration is a specialization of a member
4890 template, declared outside the class. Therefore, the
4891 innermost template arguments will be NULL, so we
4892 replace them with the arguments determined by the
4893 earlier call to check_explicit_specialization. */
4894 args
= DECL_TI_ARGS (decl
);
4897 = build_template_decl (decl
, current_template_parms
,
4899 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4900 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4901 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4902 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4903 DECL_TEMPLATE_INFO (new_tmpl
)
4904 = build_template_info (tmpl
, args
);
4906 register_specialization (new_tmpl
,
4907 most_general_template (tmpl
),
4913 /* Make sure the template headers we got make sense. */
4915 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4916 i
= TMPL_PARMS_DEPTH (parms
);
4917 if (TMPL_ARGS_DEPTH (args
) != i
)
4919 error ("expected %d levels of template parms for %q#D, got %d",
4920 i
, decl
, TMPL_ARGS_DEPTH (args
));
4923 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4925 a
= TMPL_ARGS_LEVEL (args
, i
);
4926 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4928 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4930 if (current
== decl
)
4931 error ("got %d template parameters for %q#D",
4932 TREE_VEC_LENGTH (a
), decl
);
4934 error ("got %d template parameters for %q#T",
4935 TREE_VEC_LENGTH (a
), current
);
4936 error (" but %d required", TREE_VEC_LENGTH (t
));
4937 return error_mark_node
;
4940 if (current
== decl
)
4942 else if (current
== NULL_TREE
)
4943 /* Can happen in erroneous input. */
4946 current
= (TYPE_P (current
)
4947 ? TYPE_CONTEXT (current
)
4948 : DECL_CONTEXT (current
));
4951 /* Check that the parms are used in the appropriate qualifying scopes
4952 in the declarator. */
4953 if (!comp_template_args
4955 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4958 template arguments to %qD do not match original template %qD",
4959 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4960 if (!uses_template_parms (TI_ARGS (tinfo
)))
4961 inform (input_location
, "use template<> for an explicit specialization");
4962 /* Avoid crash in import_export_decl. */
4963 DECL_INTERFACE_KNOWN (decl
) = 1;
4964 return error_mark_node
;
4968 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4969 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4971 /* Push template declarations for global functions and types. Note
4972 that we do not try to push a global template friend declared in a
4973 template class; such a thing may well depend on the template
4974 parameters of the class. */
4975 if (new_template_p
&& !ctx
4976 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4978 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4979 if (tmpl
== error_mark_node
)
4980 return error_mark_node
;
4982 /* Hide template friend classes that haven't been declared yet. */
4983 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4985 DECL_ANTICIPATED (tmpl
) = 1;
4986 DECL_FRIEND_P (tmpl
) = 1;
4992 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4995 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4996 if (DECL_CONV_FN_P (tmpl
))
4998 int depth
= TMPL_PARMS_DEPTH (parms
);
5000 /* It is a conversion operator. See if the type converted to
5001 depends on innermost template operands. */
5003 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5005 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5008 /* Give template template parms a DECL_CONTEXT of the template
5009 for which they are a parameter. */
5010 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5011 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5013 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5014 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5015 DECL_CONTEXT (parm
) = tmpl
;
5019 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5020 back to its most general template. If TMPL is a specialization,
5021 ARGS may only have the innermost set of arguments. Add the missing
5022 argument levels if necessary. */
5023 if (DECL_TEMPLATE_INFO (tmpl
))
5024 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5026 info
= build_template_info (tmpl
, args
);
5028 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5029 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5030 else if (DECL_LANG_SPECIFIC (decl
))
5031 DECL_TEMPLATE_INFO (decl
) = info
;
5033 return DECL_TEMPLATE_RESULT (tmpl
);
5037 push_template_decl (tree decl
)
5039 return push_template_decl_real (decl
, false);
5042 /* Called when a class template TYPE is redeclared with the indicated
5043 template PARMS, e.g.:
5045 template <class T> struct S;
5046 template <class T> struct S {}; */
5049 redeclare_class_template (tree type
, tree parms
)
5055 if (!TYPE_TEMPLATE_INFO (type
))
5057 error ("%qT is not a template type", type
);
5061 tmpl
= TYPE_TI_TEMPLATE (type
);
5062 if (!PRIMARY_TEMPLATE_P (tmpl
))
5063 /* The type is nested in some template class. Nothing to worry
5064 about here; there are no new template parameters for the nested
5070 error ("template specifiers not specified in declaration of %qD",
5075 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5076 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5078 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5080 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5081 "redeclared with %d template parameter",
5082 "redeclared with %d template parameters",
5083 TREE_VEC_LENGTH (parms
));
5084 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5085 "previous declaration %q+D used %d template parameter",
5086 "previous declaration %q+D used %d template parameters",
5087 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5091 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5098 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5099 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5102 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5103 if (tmpl_parm
== error_mark_node
)
5106 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5107 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5108 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5110 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5112 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5113 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5114 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5115 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5116 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5117 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5118 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5119 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5120 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5122 error ("template parameter %q+#D", tmpl_parm
);
5123 error ("redeclared here as %q#D", parm
);
5127 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5129 /* We have in [temp.param]:
5131 A template-parameter may not be given default arguments
5132 by two different declarations in the same scope. */
5133 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5134 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5135 "original definition appeared here");
5139 if (parm_default
!= NULL_TREE
)
5140 /* Update the previous template parameters (which are the ones
5141 that will really count) with the new default value. */
5142 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5143 else if (tmpl_default
!= NULL_TREE
)
5144 /* Update the new parameters, too; they'll be used as the
5145 parameters for any members. */
5146 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5152 /* Simplify EXPR if it is a non-dependent expression. Returns the
5153 (possibly simplified) expression. */
5156 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5158 if (expr
== NULL_TREE
)
5161 /* If we're in a template, but EXPR isn't value dependent, simplify
5162 it. We're supposed to treat:
5164 template <typename T> void f(T[1 + 1]);
5165 template <typename T> void f(T[2]);
5167 as two declarations of the same function, for example. */
5168 if (processing_template_decl
5169 && !type_dependent_expression_p (expr
)
5170 && potential_constant_expression (expr
)
5171 && !value_dependent_expression_p (expr
))
5173 HOST_WIDE_INT saved_processing_template_decl
;
5175 saved_processing_template_decl
= processing_template_decl
;
5176 processing_template_decl
= 0;
5177 expr
= tsubst_copy_and_build (expr
,
5180 /*in_decl=*/NULL_TREE
,
5181 /*function_p=*/false,
5182 /*integral_constant_expression_p=*/true);
5183 processing_template_decl
= saved_processing_template_decl
;
5189 fold_non_dependent_expr (tree expr
)
5191 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5194 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5195 must be a function or a pointer-to-function type, as specified
5196 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5197 and check that the resulting function has external linkage. */
5200 convert_nontype_argument_function (tree type
, tree expr
)
5205 fn
= instantiate_type (type
, fns
, tf_none
);
5206 if (fn
== error_mark_node
)
5207 return error_mark_node
;
5210 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5211 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5212 if (TREE_CODE (fn_no_ptr
) == BASELINK
)
5213 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5215 /* [temp.arg.nontype]/1
5217 A template-argument for a non-type, non-template template-parameter
5220 -- the address of an object or function with external linkage. */
5221 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr
))
5223 error ("%qE is not a valid template argument for type %qT "
5224 "because function %qD has not external linkage",
5225 expr
, type
, fn_no_ptr
);
5232 /* Subroutine of convert_nontype_argument.
5233 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5234 Emit an error otherwise. */
5237 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5238 tsubst_flags_t complain
)
5241 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5243 if (complain
& tf_error
)
5245 error ("%qE is not a valid template argument for type %qT",
5247 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5252 /* Returns TRUE iff the address of OP is value-dependent.
5254 14.6.2.4 [temp.dep.temp]:
5255 A non-integral non-type template-argument is dependent if its type is
5256 dependent or it has either of the following forms
5259 and contains a nested-name-specifier which specifies a class-name that
5260 names a dependent type.
5262 We generalize this to just say that the address of a member of a
5263 dependent class is value-dependent; the above doesn't cover the
5264 address of a static data member named with an unqualified-id. */
5267 has_value_dependent_address (tree op
)
5269 /* We could use get_inner_reference here, but there's no need;
5270 this is only relevant for template non-type arguments, which
5271 can only be expressed as &id-expression. */
5274 tree ctx
= CP_DECL_CONTEXT (op
);
5275 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5282 /* The next set of functions are used for providing helpful explanatory
5283 diagnostics for failed overload resolution. Their messages should be
5284 indented by two spaces for consistency with the messages in
5288 unify_success (bool explain_p ATTRIBUTE_UNUSED
)
5294 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5297 inform (input_location
,
5298 " couldn't deduce template parameter %qD", parm
);
5303 unify_invalid (bool explain_p ATTRIBUTE_UNUSED
)
5309 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5312 inform (input_location
,
5313 " types %qT and %qT have incompatible cv-qualifiers",
5319 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5322 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5327 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5330 inform (input_location
,
5331 " template parameter %qD is not a parameter pack, but "
5338 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5341 inform (input_location
,
5342 " template argument %qE does not match "
5343 "pointer-to-member constant %qE",
5349 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5352 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5357 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5360 inform (input_location
,
5361 " inconsistent parameter pack deduction with %qT and %qT",
5367 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5370 inform (input_location
,
5371 " deduced conflicting types for parameter %qT (%qT and %qT)",
5372 parm
, first
, second
);
5377 unify_vla_arg (bool explain_p
, tree arg
)
5380 inform (input_location
,
5381 " variable-sized array type %qT is not "
5382 "a valid template argument",
5388 unify_method_type_error (bool explain_p
, tree arg
)
5391 inform (input_location
,
5392 " member function type %qT is not a valid template argument",
5398 unify_arity (bool explain_p
, int have
, int wanted
)
5401 inform_n (input_location
, wanted
,
5402 " candidate expects %d argument, %d provided",
5403 " candidate expects %d arguments, %d provided",
5409 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5411 return unify_arity (explain_p
, have
, wanted
);
5415 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5417 return unify_arity (explain_p
, have
, wanted
);
5421 unify_arg_conversion (bool explain_p
, tree to_type
,
5422 tree from_type
, tree arg
)
5425 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5426 arg
, from_type
, to_type
);
5431 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5432 tree parm
, tree arg
)
5437 case tbr_ambiguous_baseclass
:
5438 inform (input_location
, " %qT is an ambiguous base class of %qT",
5442 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5449 unify_inconsistent_template_template_parameters (bool explain_p
)
5452 inform (input_location
,
5453 " template parameters of a template template argument are "
5454 "inconsistent with other deduced template arguments");
5459 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5462 inform (input_location
,
5463 " can't deduce a template for %qT from non-template type %qT",
5469 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5472 inform (input_location
,
5473 " template argument %qE does not match %qD", arg
, parm
);
5478 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5481 inform (input_location
,
5482 " could not resolve address from overloaded function %qE",
5487 /* Attempt to convert the non-type template parameter EXPR to the
5488 indicated TYPE. If the conversion is successful, return the
5489 converted value. If the conversion is unsuccessful, return
5490 NULL_TREE if we issued an error message, or error_mark_node if we
5491 did not. We issue error messages for out-and-out bad template
5492 parameters, but not simply because the conversion failed, since we
5493 might be just trying to do argument deduction. Both TYPE and EXPR
5494 must be non-dependent.
5496 The conversion follows the special rules described in
5497 [temp.arg.nontype], and it is much more strict than an implicit
5500 This function is called twice for each template argument (see
5501 lookup_template_class for a more accurate description of this
5502 problem). This means that we need to handle expressions which
5503 are not valid in a C++ source, but can be created from the
5504 first call (for instance, casts to perform conversions). These
5505 hacks can go away after we fix the double coercion problem. */
5508 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5512 /* Detect immediately string literals as invalid non-type argument.
5513 This special-case is not needed for correctness (we would easily
5514 catch this later), but only to provide better diagnostic for this
5515 common user mistake. As suggested by DR 100, we do not mention
5516 linkage issues in the diagnostic as this is not the point. */
5517 /* FIXME we're making this OK. */
5518 if (TREE_CODE (expr
) == STRING_CST
)
5520 if (complain
& tf_error
)
5521 error ("%qE is not a valid template argument for type %qT "
5522 "because string literals can never be used in this context",
5527 /* Add the ADDR_EXPR now for the benefit of
5528 value_dependent_expression_p. */
5529 if (TYPE_PTROBV_P (type
)
5530 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5531 expr
= decay_conversion (expr
);
5533 /* If we are in a template, EXPR may be non-dependent, but still
5534 have a syntactic, rather than semantic, form. For example, EXPR
5535 might be a SCOPE_REF, rather than the VAR_DECL to which the
5536 SCOPE_REF refers. Preserving the qualifying scope is necessary
5537 so that access checking can be performed when the template is
5538 instantiated -- but here we need the resolved form so that we can
5539 convert the argument. */
5540 if (TYPE_REF_OBJ_P (type
)
5541 && has_value_dependent_address (expr
))
5542 /* If we want the address and it's value-dependent, don't fold. */;
5543 else if (!type_unknown_p (expr
))
5544 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5545 if (error_operand_p (expr
))
5546 return error_mark_node
;
5547 expr_type
= TREE_TYPE (expr
);
5548 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5549 expr
= mark_lvalue_use (expr
);
5551 expr
= mark_rvalue_use (expr
);
5553 /* HACK: Due to double coercion, we can get a
5554 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5555 which is the tree that we built on the first call (see
5556 below when coercing to reference to object or to reference to
5557 function). We just strip everything and get to the arg.
5558 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5560 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5562 tree probe_type
, probe
= expr
;
5563 if (REFERENCE_REF_P (probe
))
5564 probe
= TREE_OPERAND (probe
, 0);
5565 probe_type
= TREE_TYPE (probe
);
5566 if (TREE_CODE (probe
) == NOP_EXPR
)
5568 /* ??? Maybe we could use convert_from_reference here, but we
5569 would need to relax its constraints because the NOP_EXPR
5570 could actually change the type to something more cv-qualified,
5571 and this is not folded by convert_from_reference. */
5572 tree addr
= TREE_OPERAND (probe
, 0);
5573 gcc_assert (TREE_CODE (probe_type
) == REFERENCE_TYPE
);
5574 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5575 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5576 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5577 (TREE_TYPE (probe_type
),
5578 TREE_TYPE (TREE_TYPE (addr
))));
5580 expr
= TREE_OPERAND (addr
, 0);
5581 expr_type
= TREE_TYPE (expr
);
5585 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5586 parameter is a pointer to object, through decay and
5587 qualification conversion. Let's strip everything. */
5588 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5591 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5592 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5593 /* Skip the ADDR_EXPR only if it is part of the decay for
5594 an array. Otherwise, it is part of the original argument
5595 in the source code. */
5596 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5597 expr
= TREE_OPERAND (expr
, 0);
5598 expr_type
= TREE_TYPE (expr
);
5601 /* [temp.arg.nontype]/5, bullet 1
5603 For a non-type template-parameter of integral or enumeration type,
5604 integral promotions (_conv.prom_) and integral conversions
5605 (_conv.integral_) are applied. */
5606 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5608 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5609 t
= maybe_constant_value (t
);
5610 if (t
!= error_mark_node
)
5613 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5614 return error_mark_node
;
5616 /* Notice that there are constant expressions like '4 % 0' which
5617 do not fold into integer constants. */
5618 if (TREE_CODE (expr
) != INTEGER_CST
)
5620 if (complain
& tf_error
)
5622 int errs
= errorcount
, warns
= warningcount
;
5623 expr
= cxx_constant_value (expr
);
5624 if (errorcount
> errs
|| warningcount
> warns
)
5625 inform (EXPR_LOC_OR_HERE (expr
),
5626 "in template argument for type %qT ", type
);
5627 if (expr
== error_mark_node
)
5629 /* else cxx_constant_value complained but gave us
5630 a real constant, so go ahead. */
5631 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5637 /* [temp.arg.nontype]/5, bullet 2
5639 For a non-type template-parameter of type pointer to object,
5640 qualification conversions (_conv.qual_) and the array-to-pointer
5641 conversion (_conv.array_) are applied. */
5642 else if (TYPE_PTROBV_P (type
))
5644 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5646 A template-argument for a non-type, non-template template-parameter
5647 shall be one of: [...]
5649 -- the name of a non-type template-parameter;
5650 -- the address of an object or function with external linkage, [...]
5651 expressed as "& id-expression" where the & is optional if the name
5652 refers to a function or array, or if the corresponding
5653 template-parameter is a reference.
5655 Here, we do not care about functions, as they are invalid anyway
5656 for a parameter of type pointer-to-object. */
5658 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5659 /* Non-type template parameters are OK. */
5661 else if (TREE_CODE (expr
) != ADDR_EXPR
5662 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5664 if (TREE_CODE (expr
) == VAR_DECL
)
5666 error ("%qD is not a valid template argument "
5667 "because %qD is a variable, not the address of "
5672 /* Other values, like integer constants, might be valid
5673 non-type arguments of some other type. */
5674 return error_mark_node
;
5680 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5681 ? TREE_OPERAND (expr
, 0) : expr
);
5682 if (TREE_CODE (decl
) != VAR_DECL
)
5684 error ("%qE is not a valid template argument of type %qT "
5685 "because %qE is not a variable",
5689 else if (!DECL_EXTERNAL_LINKAGE_P (decl
))
5691 error ("%qE is not a valid template argument of type %qT "
5692 "because %qD does not have external linkage",
5698 expr
= decay_conversion (expr
);
5699 if (expr
== error_mark_node
)
5700 return error_mark_node
;
5702 expr
= perform_qualification_conversions (type
, expr
);
5703 if (expr
== error_mark_node
)
5704 return error_mark_node
;
5706 /* [temp.arg.nontype]/5, bullet 3
5708 For a non-type template-parameter of type reference to object, no
5709 conversions apply. The type referred to by the reference may be more
5710 cv-qualified than the (otherwise identical) type of the
5711 template-argument. The template-parameter is bound directly to the
5712 template-argument, which must be an lvalue. */
5713 else if (TYPE_REF_OBJ_P (type
))
5715 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5717 return error_mark_node
;
5719 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5721 error ("%qE is not a valid template argument for type %qT "
5722 "because of conflicts in cv-qualification", expr
, type
);
5726 if (!real_lvalue_p (expr
))
5728 error ("%qE is not a valid template argument for type %qT "
5729 "because it is not an lvalue", expr
, type
);
5733 /* [temp.arg.nontype]/1
5735 A template-argument for a non-type, non-template template-parameter
5736 shall be one of: [...]
5738 -- the address of an object or function with external linkage. */
5739 if (TREE_CODE (expr
) == INDIRECT_REF
5740 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5742 expr
= TREE_OPERAND (expr
, 0);
5745 error ("%q#D is not a valid template argument for type %qT "
5746 "because a reference variable does not have a constant "
5747 "address", expr
, type
);
5754 error ("%qE is not a valid template argument for type %qT "
5755 "because it is not an object with external linkage",
5760 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5762 error ("%qE is not a valid template argument for type %qT "
5763 "because object %qD has not external linkage",
5768 expr
= build_nop (type
, build_address (expr
));
5770 /* [temp.arg.nontype]/5, bullet 4
5772 For a non-type template-parameter of type pointer to function, only
5773 the function-to-pointer conversion (_conv.func_) is applied. If the
5774 template-argument represents a set of overloaded functions (or a
5775 pointer to such), the matching function is selected from the set
5777 else if (TYPE_PTRFN_P (type
))
5779 /* If the argument is a template-id, we might not have enough
5780 context information to decay the pointer. */
5781 if (!type_unknown_p (expr_type
))
5783 expr
= decay_conversion (expr
);
5784 if (expr
== error_mark_node
)
5785 return error_mark_node
;
5788 expr
= convert_nontype_argument_function (type
, expr
);
5789 if (!expr
|| expr
== error_mark_node
)
5792 if (TREE_CODE (expr
) != ADDR_EXPR
)
5794 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5795 error ("it must be the address of a function with external linkage");
5799 /* [temp.arg.nontype]/5, bullet 5
5801 For a non-type template-parameter of type reference to function, no
5802 conversions apply. If the template-argument represents a set of
5803 overloaded functions, the matching function is selected from the set
5805 else if (TYPE_REFFN_P (type
))
5807 if (TREE_CODE (expr
) == ADDR_EXPR
)
5809 error ("%qE is not a valid template argument for type %qT "
5810 "because it is a pointer", expr
, type
);
5811 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5815 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5816 if (!expr
|| expr
== error_mark_node
)
5819 expr
= build_nop (type
, build_address (expr
));
5821 /* [temp.arg.nontype]/5, bullet 6
5823 For a non-type template-parameter of type pointer to member function,
5824 no conversions apply. If the template-argument represents a set of
5825 overloaded member functions, the matching member function is selected
5826 from the set (_over.over_). */
5827 else if (TYPE_PTRMEMFUNC_P (type
))
5829 expr
= instantiate_type (type
, expr
, tf_none
);
5830 if (expr
== error_mark_node
)
5831 return error_mark_node
;
5833 /* [temp.arg.nontype] bullet 1 says the pointer to member
5834 expression must be a pointer-to-member constant. */
5835 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5836 return error_mark_node
;
5838 /* There is no way to disable standard conversions in
5839 resolve_address_of_overloaded_function (called by
5840 instantiate_type). It is possible that the call succeeded by
5841 converting &B::I to &D::I (where B is a base of D), so we need
5842 to reject this conversion here.
5844 Actually, even if there was a way to disable standard conversions,
5845 it would still be better to reject them here so that we can
5846 provide a superior diagnostic. */
5847 if (!same_type_p (TREE_TYPE (expr
), type
))
5849 error ("%qE is not a valid template argument for type %qT "
5850 "because it is of type %qT", expr
, type
,
5852 /* If we are just one standard conversion off, explain. */
5853 if (can_convert (type
, TREE_TYPE (expr
)))
5854 inform (input_location
,
5855 "standard conversions are not allowed in this context");
5859 /* [temp.arg.nontype]/5, bullet 7
5861 For a non-type template-parameter of type pointer to data member,
5862 qualification conversions (_conv.qual_) are applied. */
5863 else if (TYPE_PTRMEM_P (type
))
5865 /* [temp.arg.nontype] bullet 1 says the pointer to member
5866 expression must be a pointer-to-member constant. */
5867 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5868 return error_mark_node
;
5870 expr
= perform_qualification_conversions (type
, expr
);
5871 if (expr
== error_mark_node
)
5874 /* A template non-type parameter must be one of the above. */
5878 /* Sanity check: did we actually convert the argument to the
5880 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5881 (type
, TREE_TYPE (expr
)));
5885 /* Subroutine of coerce_template_template_parms, which returns 1 if
5886 PARM_PARM and ARG_PARM match using the rule for the template
5887 parameters of template template parameters. Both PARM and ARG are
5888 template parameters; the rest of the arguments are the same as for
5889 coerce_template_template_parms.
5892 coerce_template_template_parm (tree parm
,
5894 tsubst_flags_t complain
,
5898 if (arg
== NULL_TREE
|| arg
== error_mark_node
5899 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5902 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5905 switch (TREE_CODE (parm
))
5908 /* We encounter instantiations of templates like
5909 template <template <template <class> class> class TT>
5912 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5913 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5915 if (!coerce_template_template_parms
5916 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5922 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5923 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5924 /* Argument is a parameter pack but parameter is not. */
5929 /* The tsubst call is used to handle cases such as
5931 template <int> class C {};
5932 template <class T, template <T> class TT> class D {};
5935 i.e. the parameter list of TT depends on earlier parameters. */
5936 if (!uses_template_parms (TREE_TYPE (arg
))
5938 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5942 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5943 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5944 /* Argument is a parameter pack but parameter is not. */
5957 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5958 template template parameters. Both PARM_PARMS and ARG_PARMS are
5959 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5962 Consider the example:
5963 template <class T> class A;
5964 template<template <class U> class TT> class B;
5966 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5967 the parameters to A, and OUTER_ARGS contains A. */
5970 coerce_template_template_parms (tree parm_parms
,
5972 tsubst_flags_t complain
,
5976 int nparms
, nargs
, i
;
5980 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5981 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5983 nparms
= TREE_VEC_LENGTH (parm_parms
);
5984 nargs
= TREE_VEC_LENGTH (arg_parms
);
5986 /* Determine whether we have a parameter pack at the end of the
5987 template template parameter's template parameter list. */
5988 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5990 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5992 if (parm
== error_mark_node
)
5995 switch (TREE_CODE (parm
))
5999 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6004 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6014 && !(variadic_p
&& nargs
>= nparms
- 1))
6017 /* Check all of the template parameters except the parameter pack at
6018 the end (if any). */
6019 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6021 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6022 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6025 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6026 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6028 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6036 /* Check each of the template parameters in the template
6037 argument against the template parameter pack at the end of
6038 the template template parameter. */
6039 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6042 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6044 for (; i
< nargs
; ++i
)
6046 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6049 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6051 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6060 /* Verifies that the deduced template arguments (in TARGS) for the
6061 template template parameters (in TPARMS) represent valid bindings,
6062 by comparing the template parameter list of each template argument
6063 to the template parameter list of its corresponding template
6064 template parameter, in accordance with DR150. This
6065 routine can only be called after all template arguments have been
6066 deduced. It will return TRUE if all of the template template
6067 parameter bindings are okay, FALSE otherwise. */
6069 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6071 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6074 /* We're dealing with template parms in this process. */
6075 ++processing_template_decl
;
6077 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6079 for (i
= 0; i
< ntparms
; ++i
)
6081 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6082 tree targ
= TREE_VEC_ELT (targs
, i
);
6084 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6086 tree packed_args
= NULL_TREE
;
6089 if (ARGUMENT_PACK_P (targ
))
6091 /* Look inside the argument pack. */
6092 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6093 len
= TREE_VEC_LENGTH (packed_args
);
6096 for (idx
= 0; idx
< len
; ++idx
)
6098 tree targ_parms
= NULL_TREE
;
6101 /* Extract the next argument from the argument
6103 targ
= TREE_VEC_ELT (packed_args
, idx
);
6105 if (PACK_EXPANSION_P (targ
))
6106 /* Look at the pattern of the pack expansion. */
6107 targ
= PACK_EXPANSION_PATTERN (targ
);
6109 /* Extract the template parameters from the template
6111 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6112 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6113 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6114 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6116 /* Verify that we can coerce the template template
6117 parameters from the template argument to the template
6118 parameter. This requires an exact match. */
6120 && !coerce_template_template_parms
6121 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6136 --processing_template_decl
;
6140 /* Since type attributes aren't mangled, we need to strip them from
6141 template type arguments. */
6144 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6147 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6149 mv
= TYPE_MAIN_VARIANT (arg
);
6150 arg
= strip_typedefs (arg
);
6151 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6152 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6154 if (complain
& tf_warning
)
6155 warning (0, "ignoring attributes on template argument %qT", arg
);
6156 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6157 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6162 /* Convert the indicated template ARG as necessary to match the
6163 indicated template PARM. Returns the converted ARG, or
6164 error_mark_node if the conversion was unsuccessful. Error and
6165 warning messages are issued under control of COMPLAIN. This
6166 conversion is for the Ith parameter in the parameter list. ARGS is
6167 the full set of template arguments deduced so far. */
6170 convert_template_argument (tree parm
,
6173 tsubst_flags_t complain
,
6179 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6181 if (TREE_CODE (arg
) == TREE_LIST
6182 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6184 /* The template argument was the name of some
6185 member function. That's usually
6186 invalid, but static members are OK. In any
6187 case, grab the underlying fields/functions
6188 and issue an error later if required. */
6189 orig_arg
= TREE_VALUE (arg
);
6190 TREE_TYPE (arg
) = unknown_type_node
;
6195 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6196 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6197 || requires_tmpl_type
);
6199 /* When determining whether an argument pack expansion is a template,
6200 look at the pattern. */
6201 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6202 arg
= PACK_EXPANSION_PATTERN (arg
);
6204 /* Deal with an injected-class-name used as a template template arg. */
6205 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6207 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6208 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6210 if (cxx_dialect
>= cxx0x
)
6211 /* OK under DR 1004. */;
6212 else if (complain
& tf_warning_or_error
)
6213 pedwarn (input_location
, OPT_pedantic
, "injected-class-name %qD"
6214 " used as template template argument", TYPE_NAME (arg
));
6215 else if (flag_pedantic_errors
)
6223 ((TREE_CODE (arg
) == TEMPLATE_DECL
6224 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6225 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6226 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6229 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6230 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6231 arg
= TYPE_STUB_DECL (arg
);
6233 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6235 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6236 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6238 permerror (input_location
, "to refer to a type member of a template parameter, "
6239 "use %<typename %E%>", orig_arg
);
6241 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6242 TREE_OPERAND (arg
, 1),
6244 complain
& tf_error
);
6248 if (is_type
!= requires_type
)
6252 if (complain
& tf_error
)
6254 error ("type/value mismatch at argument %d in template "
6255 "parameter list for %qD",
6258 error (" expected a constant of type %qT, got %qT",
6260 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6261 else if (requires_tmpl_type
)
6262 error (" expected a class template, got %qE", orig_arg
);
6264 error (" expected a type, got %qE", orig_arg
);
6267 return error_mark_node
;
6269 if (is_tmpl_type
^ requires_tmpl_type
)
6271 if (in_decl
&& (complain
& tf_error
))
6273 error ("type/value mismatch at argument %d in template "
6274 "parameter list for %qD",
6277 error (" expected a type, got %qT", DECL_NAME (arg
));
6279 error (" expected a class template, got %qT", orig_arg
);
6281 return error_mark_node
;
6286 if (requires_tmpl_type
)
6288 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6289 /* The number of argument required is not known yet.
6290 Just accept it for now. */
6291 val
= TREE_TYPE (arg
);
6294 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6297 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6299 if (coerce_template_template_parms (parmparm
, argparm
,
6305 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6307 if (val
!= error_mark_node
)
6309 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6310 val
= TREE_TYPE (val
);
6311 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6312 val
= make_pack_expansion (val
);
6317 if (in_decl
&& (complain
& tf_error
))
6319 error ("type/value mismatch at argument %d in "
6320 "template parameter list for %qD",
6322 error (" expected a template of type %qD, got %qT",
6326 val
= error_mark_node
;
6332 /* We only form one instance of each template specialization.
6333 Therefore, if we use a non-canonical variant (i.e., a
6334 typedef), any future messages referring to the type will use
6335 the typedef, which is confusing if those future uses do not
6336 themselves also use the typedef. */
6338 val
= canonicalize_type_argument (val
, complain
);
6342 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6344 if (invalid_nontype_parm_type_p (t
, complain
))
6345 return error_mark_node
;
6347 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6349 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6353 /* Not sure if this is reachable, but it doesn't hurt
6355 error ("type mismatch in nontype parameter pack");
6356 val
= error_mark_node
;
6359 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6360 /* We used to call digest_init here. However, digest_init
6361 will report errors, which we don't want when complain
6362 is zero. More importantly, digest_init will try too
6363 hard to convert things: for example, `0' should not be
6364 converted to pointer type at this point according to
6365 the standard. Accepting this is not merely an
6366 extension, since deciding whether or not these
6367 conversions can occur is part of determining which
6368 function template to call, or whether a given explicit
6369 argument specification is valid. */
6370 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6374 if (val
== NULL_TREE
)
6375 val
= error_mark_node
;
6376 else if (val
== error_mark_node
&& (complain
& tf_error
))
6377 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6379 if (TREE_CODE (val
) == SCOPE_REF
)
6381 /* Strip typedefs from the SCOPE_REF. */
6382 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6383 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6385 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6386 QUALIFIED_NAME_IS_TEMPLATE (val
));
6393 /* Coerces the remaining template arguments in INNER_ARGS (from
6394 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6395 Returns the coerced argument pack. PARM_IDX is the position of this
6396 parameter in the template parameter list. ARGS is the original
6397 template argument list. */
6399 coerce_template_parameter_pack (tree parms
,
6407 tsubst_flags_t complain
)
6409 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6410 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6413 tree packed_types
= NULL_TREE
;
6415 if (arg_idx
> nargs
)
6418 packed_args
= make_tree_vec (nargs
- arg_idx
);
6420 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6421 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6423 /* When the template parameter is a non-type template
6424 parameter pack whose type uses parameter packs, we need
6425 to look at each of the template arguments
6426 separately. Build a vector of the types for these
6427 non-type template parameters in PACKED_TYPES. */
6429 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6430 packed_types
= tsubst_pack_expansion (expansion
, args
,
6433 if (packed_types
== error_mark_node
)
6434 return error_mark_node
;
6436 /* Check that we have the right number of arguments. */
6438 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6439 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6442 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6443 error ("wrong number of template arguments (%d, should be %d)",
6444 nargs
, needed_parms
);
6445 return error_mark_node
;
6448 /* If we aren't able to check the actual arguments now
6449 (because they haven't been expanded yet), we can at least
6450 verify that all of the types used for the non-type
6451 template parameter pack are, in fact, valid for non-type
6452 template parameters. */
6454 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6456 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6457 for (j
= 0; j
< len
; ++j
)
6459 tree t
= TREE_VEC_ELT (packed_types
, j
);
6460 if (invalid_nontype_parm_type_p (t
, complain
))
6461 return error_mark_node
;
6466 /* Convert the remaining arguments, which will be a part of the
6467 parameter pack "parm". */
6468 for (; arg_idx
< nargs
; ++arg_idx
)
6470 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6471 tree actual_parm
= TREE_VALUE (parm
);
6473 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6475 /* When we have a vector of types (corresponding to the
6476 non-type template parameter pack that uses parameter
6477 packs in its type, as mention above), and the
6478 argument is not an expansion (which expands to a
6479 currently unknown number of arguments), clone the
6480 parm and give it the next type in PACKED_TYPES. */
6481 actual_parm
= copy_node (actual_parm
);
6482 TREE_TYPE (actual_parm
) =
6483 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6486 if (arg
!= error_mark_node
)
6487 arg
= convert_template_argument (actual_parm
,
6488 arg
, new_args
, complain
, parm_idx
,
6490 if (arg
== error_mark_node
)
6492 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6495 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6496 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6497 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6500 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6501 TREE_TYPE (argument_pack
)
6502 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6503 TREE_CONSTANT (argument_pack
) = 1;
6506 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6507 #ifdef ENABLE_CHECKING
6508 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6509 TREE_VEC_LENGTH (packed_args
));
6511 return argument_pack
;
6514 /* Convert all template arguments to their appropriate types, and
6515 return a vector containing the innermost resulting template
6516 arguments. If any error occurs, return error_mark_node. Error and
6517 warning messages are issued under control of COMPLAIN.
6519 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6520 for arguments not specified in ARGS. Otherwise, if
6521 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6522 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6523 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6527 coerce_template_parms (tree parms
,
6530 tsubst_flags_t complain
,
6531 bool require_all_args
,
6532 bool use_default_args
)
6534 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6537 tree new_inner_args
;
6538 int saved_unevaluated_operand
;
6539 int saved_inhibit_evaluation_warnings
;
6541 /* When used as a boolean value, indicates whether this is a
6542 variadic template parameter list. Since it's an int, we can also
6543 subtract it from nparms to get the number of non-variadic
6546 int post_variadic_parms
= 0;
6548 if (args
== error_mark_node
)
6549 return error_mark_node
;
6551 nparms
= TREE_VEC_LENGTH (parms
);
6553 /* Determine if there are any parameter packs. */
6554 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6556 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6558 ++post_variadic_parms
;
6559 if (template_parameter_pack_p (tparm
))
6563 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6564 /* If there are no parameters that follow a parameter pack, we need to
6565 expand any argument packs so that we can deduce a parameter pack from
6566 some non-packed args followed by an argument pack, as in variadic85.C.
6567 If there are such parameters, we need to leave argument packs intact
6568 so the arguments are assigned properly. This can happen when dealing
6569 with a nested class inside a partial specialization of a class
6570 template, as in variadic92.C, or when deducing a template parameter pack
6571 from a sub-declarator, as in variadic114.C. */
6572 if (!post_variadic_parms
)
6573 inner_args
= expand_template_argument_pack (inner_args
);
6575 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6576 if ((nargs
> nparms
&& !variadic_p
)
6577 || (nargs
< nparms
- variadic_p
6579 && (!use_default_args
6580 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6581 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6583 if (complain
& tf_error
)
6587 nparms
-= variadic_p
;
6588 error ("wrong number of template arguments "
6589 "(%d, should be %d or more)", nargs
, nparms
);
6592 error ("wrong number of template arguments "
6593 "(%d, should be %d)", nargs
, nparms
);
6596 error ("provided for %q+D", in_decl
);
6599 return error_mark_node
;
6602 /* We need to evaluate the template arguments, even though this
6603 template-id may be nested within a "sizeof". */
6604 saved_unevaluated_operand
= cp_unevaluated_operand
;
6605 cp_unevaluated_operand
= 0;
6606 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6607 c_inhibit_evaluation_warnings
= 0;
6608 new_inner_args
= make_tree_vec (nparms
);
6609 new_args
= add_outermost_template_args (args
, new_inner_args
);
6610 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6615 /* Get the Ith template parameter. */
6616 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6618 if (parm
== error_mark_node
)
6620 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6624 /* Calculate the next argument. */
6625 if (arg_idx
< nargs
)
6626 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6630 if (template_parameter_pack_p (TREE_VALUE (parm
))
6631 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6633 /* All remaining arguments will be placed in the
6634 template parameter pack PARM. */
6635 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6636 inner_args
, arg_idx
,
6640 /* Store this argument. */
6641 if (arg
== error_mark_node
)
6643 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6645 /* We are done with all of the arguments. */
6652 if (PACK_EXPANSION_P (arg
))
6654 if (complain
& tf_error
)
6656 /* FIXME this restriction was removed by N2555; see
6658 /* If ARG is a pack expansion, but PARM is not a
6659 template parameter pack (if it were, we would have
6660 handled it above), we're trying to expand into a
6661 fixed-length argument list. */
6662 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
6663 sorry ("cannot expand %<%E%> into a fixed-length "
6664 "argument list", arg
);
6666 sorry ("cannot expand %<%T%> into a fixed-length "
6667 "argument list", arg
);
6672 else if (require_all_args
)
6674 /* There must be a default arg in this case. */
6675 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6677 /* The position of the first default template argument,
6678 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6680 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6681 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6686 if (arg
== error_mark_node
)
6688 if (complain
& tf_error
)
6689 error ("template argument %d is invalid", arg_idx
+ 1);
6692 /* This only occurs if there was an error in the template
6693 parameter list itself (which we would already have
6694 reported) that we are trying to recover from, e.g., a class
6695 template with a parameter list such as
6696 template<typename..., typename>. */
6699 arg
= convert_template_argument (TREE_VALUE (parm
),
6700 arg
, new_args
, complain
,
6703 if (arg
== error_mark_node
)
6705 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6707 cp_unevaluated_operand
= saved_unevaluated_operand
;
6708 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6711 return error_mark_node
;
6713 #ifdef ENABLE_CHECKING
6714 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6715 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6716 TREE_VEC_LENGTH (new_inner_args
));
6719 return new_inner_args
;
6722 /* Returns 1 if template args OT and NT are equivalent. */
6725 template_args_equal (tree ot
, tree nt
)
6729 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6732 if (TREE_CODE (nt
) == TREE_VEC
)
6733 /* For member templates */
6734 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6735 else if (PACK_EXPANSION_P (ot
))
6736 return PACK_EXPANSION_P (nt
)
6737 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6738 PACK_EXPANSION_PATTERN (nt
));
6739 else if (ARGUMENT_PACK_P (ot
))
6744 if (!ARGUMENT_PACK_P (nt
))
6747 opack
= ARGUMENT_PACK_ARGS (ot
);
6748 npack
= ARGUMENT_PACK_ARGS (nt
);
6749 len
= TREE_VEC_LENGTH (opack
);
6750 if (TREE_VEC_LENGTH (npack
) != len
)
6752 for (i
= 0; i
< len
; ++i
)
6753 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6754 TREE_VEC_ELT (npack
, i
)))
6758 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6760 /* We get here probably because we are in the middle of substituting
6761 into the pattern of a pack expansion. In that case the
6762 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6763 interested in. So we want to use the initial pack argument for
6765 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6766 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6767 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6768 return template_args_equal (ot
, nt
);
6770 else if (TYPE_P (nt
))
6771 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6772 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6775 return cp_tree_equal (ot
, nt
);
6778 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6779 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6780 NEWARG_PTR with the offending arguments if they are non-NULL. */
6783 comp_template_args_with_info (tree oldargs
, tree newargs
,
6784 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6788 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6791 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6793 tree nt
= TREE_VEC_ELT (newargs
, i
);
6794 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6796 if (! template_args_equal (ot
, nt
))
6798 if (oldarg_ptr
!= NULL
)
6800 if (newarg_ptr
!= NULL
)
6808 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6809 of template arguments. Returns 0 otherwise. */
6812 comp_template_args (tree oldargs
, tree newargs
)
6814 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
6818 add_pending_template (tree d
)
6820 tree ti
= (TYPE_P (d
)
6821 ? CLASSTYPE_TEMPLATE_INFO (d
)
6822 : DECL_TEMPLATE_INFO (d
));
6823 struct pending_template
*pt
;
6826 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6829 /* We are called both from instantiate_decl, where we've already had a
6830 tinst_level pushed, and instantiate_template, where we haven't.
6832 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6835 push_tinst_level (d
);
6837 pt
= ggc_alloc_pending_template ();
6839 pt
->tinst
= current_tinst_level
;
6840 if (last_pending_template
)
6841 last_pending_template
->next
= pt
;
6843 pending_templates
= pt
;
6845 last_pending_template
= pt
;
6847 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6854 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6855 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6856 documentation for TEMPLATE_ID_EXPR. */
6859 lookup_template_function (tree fns
, tree arglist
)
6863 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6864 return error_mark_node
;
6866 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6868 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
6870 error ("%q#D is not a function template", fns
);
6871 return error_mark_node
;
6874 if (BASELINK_P (fns
))
6876 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6878 BASELINK_FUNCTIONS (fns
),
6883 type
= TREE_TYPE (fns
);
6884 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6885 type
= unknown_type_node
;
6887 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6890 /* Within the scope of a template class S<T>, the name S gets bound
6891 (in build_self_reference) to a TYPE_DECL for the class, not a
6892 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6893 or one of its enclosing classes, and that type is a template,
6894 return the associated TEMPLATE_DECL. Otherwise, the original
6897 Also handle the case when DECL is a TREE_LIST of ambiguous
6898 injected-class-names from different bases. */
6901 maybe_get_template_decl_from_type_decl (tree decl
)
6903 if (decl
== NULL_TREE
)
6906 /* DR 176: A lookup that finds an injected-class-name (10.2
6907 [class.member.lookup]) can result in an ambiguity in certain cases
6908 (for example, if it is found in more than one base class). If all of
6909 the injected-class-names that are found refer to specializations of
6910 the same class template, and if the name is followed by a
6911 template-argument-list, the reference refers to the class template
6912 itself and not a specialization thereof, and is not ambiguous. */
6913 if (TREE_CODE (decl
) == TREE_LIST
)
6915 tree t
, tmpl
= NULL_TREE
;
6916 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6918 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6921 else if (tmpl
!= elt
)
6924 if (tmpl
&& t
== NULL_TREE
)
6930 return (decl
!= NULL_TREE
6931 && DECL_SELF_REFERENCE_P (decl
)
6932 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6933 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
6936 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6937 parameters, find the desired type.
6939 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6941 IN_DECL, if non-NULL, is the template declaration we are trying to
6944 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6945 the class we are looking up.
6947 Issue error and warning messages under control of COMPLAIN.
6949 If the template class is really a local class in a template
6950 function, then the FUNCTION_CONTEXT is the function in which it is
6953 ??? Note that this function is currently called *twice* for each
6954 template-id: the first time from the parser, while creating the
6955 incomplete type (finish_template_type), and the second type during the
6956 real instantiation (instantiate_template_class). This is surely something
6957 that we want to avoid. It also causes some problems with argument
6958 coercion (see convert_nontype_argument for more information on this). */
6961 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
6962 int entering_scope
, tsubst_flags_t complain
)
6964 tree templ
= NULL_TREE
, parmlist
;
6971 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
6973 tree value
= innermost_non_namespace_value (d1
);
6974 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
6979 push_decl_namespace (context
);
6980 templ
= lookup_name (d1
);
6981 templ
= maybe_get_template_decl_from_type_decl (templ
);
6983 pop_decl_namespace ();
6986 context
= DECL_CONTEXT (templ
);
6988 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
6990 tree type
= TREE_TYPE (d1
);
6992 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6993 an implicit typename for the second A. Deal with it. */
6994 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
6995 type
= TREE_TYPE (type
);
6997 if (CLASSTYPE_TEMPLATE_INFO (type
))
6999 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7000 d1
= DECL_NAME (templ
);
7003 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7004 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7006 templ
= TYPE_TI_TEMPLATE (d1
);
7007 d1
= DECL_NAME (templ
);
7009 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7010 && DECL_TEMPLATE_RESULT (d1
)
7011 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7014 d1
= DECL_NAME (templ
);
7015 context
= DECL_CONTEXT (templ
);
7018 /* Issue an error message if we didn't find a template. */
7021 if (complain
& tf_error
)
7022 error ("%qT is not a template", d1
);
7023 return error_mark_node
;
7026 if (TREE_CODE (templ
) != TEMPLATE_DECL
7027 /* Make sure it's a user visible template, if it was named by
7029 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7030 && !PRIMARY_TEMPLATE_P (templ
)))
7032 if (complain
& tf_error
)
7034 error ("non-template type %qT used as a template", d1
);
7036 error ("for template declaration %q+D", in_decl
);
7038 return error_mark_node
;
7041 complain
&= ~tf_user
;
7043 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7045 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7046 template arguments */
7052 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7054 /* Consider an example where a template template parameter declared as
7056 template <class T, class U = std::allocator<T> > class TT
7058 The template parameter level of T and U are one level larger than
7059 of TT. To proper process the default argument of U, say when an
7060 instantiation `TT<int>' is seen, we need to build the full
7061 arguments containing {int} as the innermost level. Outer levels,
7062 available when not appearing as default template argument, can be
7063 obtained from the arguments of the enclosing template.
7065 Suppose that TT is later substituted with std::vector. The above
7066 instantiation is `TT<int, std::allocator<T> >' with TT at
7067 level 1, and T at level 2, while the template arguments at level 1
7068 becomes {std::vector} and the inner level 2 is {int}. */
7070 outer
= DECL_CONTEXT (templ
);
7072 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7073 else if (current_template_parms
)
7074 /* This is an argument of the current template, so we haven't set
7075 DECL_CONTEXT yet. */
7076 outer
= current_template_args ();
7079 arglist
= add_to_template_args (outer
, arglist
);
7081 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7083 /*require_all_args=*/true,
7084 /*use_default_args=*/true);
7085 if (arglist2
== error_mark_node
7086 || (!uses_template_parms (arglist2
)
7087 && check_instantiated_args (templ
, arglist2
, complain
)))
7088 return error_mark_node
;
7090 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7095 tree template_type
= TREE_TYPE (templ
);
7098 tree found
= NULL_TREE
;
7101 int is_dependent_type
;
7102 int use_partial_inst_tmpl
= false;
7104 gen_tmpl
= most_general_template (templ
);
7105 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7106 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7107 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7109 if (arg_depth
== 1 && parm_depth
> 1)
7111 /* We've been given an incomplete set of template arguments.
7114 template <class T> struct S1 {
7115 template <class U> struct S2 {};
7116 template <class U> struct S2<U*> {};
7119 we will be called with an ARGLIST of `U*', but the
7120 TEMPLATE will be `template <class T> template
7121 <class U> struct S1<T>::S2'. We must fill in the missing
7124 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7126 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7129 /* Now we should have enough arguments. */
7130 gcc_assert (parm_depth
== arg_depth
);
7132 /* From here on, we're only interested in the most general
7135 /* Calculate the BOUND_ARGS. These will be the args that are
7136 actually tsubst'd into the definition to create the
7140 /* We have multiple levels of arguments to coerce, at once. */
7142 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7144 tree bound_args
= make_tree_vec (parm_depth
);
7146 for (i
= saved_depth
,
7147 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7148 i
> 0 && t
!= NULL_TREE
;
7149 --i
, t
= TREE_CHAIN (t
))
7152 if (i
== saved_depth
)
7153 a
= coerce_template_parms (TREE_VALUE (t
),
7156 /*require_all_args=*/true,
7157 /*use_default_args=*/true);
7159 /* Outer levels should have already been coerced. */
7160 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7162 /* Don't process further if one of the levels fails. */
7163 if (a
== error_mark_node
)
7165 /* Restore the ARGLIST to its full size. */
7166 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7167 return error_mark_node
;
7170 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7172 /* We temporarily reduce the length of the ARGLIST so
7173 that coerce_template_parms will see only the arguments
7174 corresponding to the template parameters it is
7176 TREE_VEC_LENGTH (arglist
)--;
7179 /* Restore the ARGLIST to its full size. */
7180 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7182 arglist
= bound_args
;
7186 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7187 INNERMOST_TEMPLATE_ARGS (arglist
),
7190 /*require_all_args=*/true,
7191 /*use_default_args=*/true);
7193 if (arglist
== error_mark_node
)
7194 /* We were unable to bind the arguments. */
7195 return error_mark_node
;
7197 /* In the scope of a template class, explicit references to the
7198 template class refer to the type of the template, not any
7199 instantiation of it. For example, in:
7201 template <class T> class C { void f(C<T>); }
7203 the `C<T>' is just the same as `C'. Outside of the
7204 class, however, such a reference is an instantiation. */
7206 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7207 || currently_open_class (template_type
))
7208 /* comp_template_args is expensive, check it last. */
7209 && comp_template_args (TYPE_TI_ARGS (template_type
),
7211 return template_type
;
7213 /* If we already have this specialization, return it. */
7214 elt
.tmpl
= gen_tmpl
;
7216 hash
= hash_specialization (&elt
);
7217 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7223 is_dependent_type
= uses_template_parms (arglist
);
7225 /* If the deduced arguments are invalid, then the binding
7227 if (!is_dependent_type
7228 && check_instantiated_args (gen_tmpl
,
7229 INNERMOST_TEMPLATE_ARGS (arglist
),
7231 return error_mark_node
;
7233 if (!is_dependent_type
7234 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7235 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7236 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7238 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7239 DECL_NAME (gen_tmpl
),
7240 /*tag_scope=*/ts_global
);
7244 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7247 context
= global_namespace
;
7249 /* Create the type. */
7250 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7252 if (!is_dependent_type
)
7254 set_current_access_from_decl (TYPE_NAME (template_type
));
7255 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7256 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7257 arglist
, complain
, in_decl
),
7258 SCOPED_ENUM_P (template_type
), NULL
);
7262 /* We don't want to call start_enum for this type, since
7263 the values for the enumeration constants may involve
7264 template parameters. And, no one should be interested
7265 in the enumeration constants for such a type. */
7266 t
= cxx_make_type (ENUMERAL_TYPE
);
7267 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7269 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7270 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7271 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7275 t
= make_class_type (TREE_CODE (template_type
));
7276 CLASSTYPE_DECLARED_CLASS (t
)
7277 = CLASSTYPE_DECLARED_CLASS (template_type
);
7278 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7279 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7281 /* A local class. Make sure the decl gets registered properly. */
7282 if (context
== current_function_decl
)
7283 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7285 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7286 /* This instantiation is another name for the primary
7287 template type. Set the TYPE_CANONICAL field
7289 TYPE_CANONICAL (t
) = template_type
;
7290 else if (any_template_arguments_need_structural_equality_p (arglist
))
7291 /* Some of the template arguments require structural
7292 equality testing, so this template class requires
7293 structural equality testing. */
7294 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7297 /* If we called start_enum or pushtag above, this information
7298 will already be set up. */
7301 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7303 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7304 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7305 DECL_SOURCE_LOCATION (type_decl
)
7306 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7309 type_decl
= TYPE_NAME (t
);
7311 TREE_PRIVATE (type_decl
)
7312 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
7313 TREE_PROTECTED (type_decl
)
7314 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
7315 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7317 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7318 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7321 /* Let's consider the explicit specialization of a member
7322 of a class template specialization that is implicitely instantiated,
7327 template<class U> struct M {}; //#0
7332 struct S<int>::M<char> //#1
7336 [temp.expl.spec]/4 says this is valid.
7338 In this case, when we write:
7341 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7344 When we encounter #1, we want to store the partial instantiation
7345 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7347 For all cases other than this "explicit specialization of member of a
7348 class template", we just want to store the most general template into
7349 the CLASSTYPE_TI_TEMPLATE of M.
7351 This case of "explicit specialization of member of a class template"
7353 1/ the enclosing class is an instantiation of, and therefore not
7354 the same as, the context of the most general template, and
7355 2/ we aren't looking at the partial instantiation itself, i.e.
7356 the innermost arguments are not the same as the innermost parms of
7357 the most general template.
7359 So it's only when 1/ and 2/ happens that we want to use the partial
7360 instantiation of the member template in lieu of its most general
7363 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7364 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7365 /* the enclosing class must be an instantiation... */
7366 && CLASS_TYPE_P (context
)
7367 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7369 tree partial_inst_args
;
7370 TREE_VEC_LENGTH (arglist
)--;
7371 ++processing_template_decl
;
7373 tsubst (INNERMOST_TEMPLATE_ARGS
7374 (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7375 arglist
, complain
, NULL_TREE
);
7376 --processing_template_decl
;
7377 TREE_VEC_LENGTH (arglist
)++;
7378 use_partial_inst_tmpl
=
7379 /*...and we must not be looking at the partial instantiation
7381 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7385 if (!use_partial_inst_tmpl
)
7386 /* This case is easy; there are no member templates involved. */
7390 /* This is a full instantiation of a member template. Find
7391 the partial instantiation of which this is an instance. */
7393 /* Temporarily reduce by one the number of levels in the ARGLIST
7394 so as to avoid comparing the last set of arguments. */
7395 TREE_VEC_LENGTH (arglist
)--;
7396 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7397 TREE_VEC_LENGTH (arglist
)++;
7398 found
= CLASSTYPE_TI_TEMPLATE (found
);
7401 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7404 slot
= (spec_entry
**) htab_find_slot_with_hash (type_specializations
,
7405 &elt
, hash
, INSERT
);
7406 *slot
= ggc_alloc_spec_entry ();
7409 /* Note this use of the partial instantiation so we can check it
7410 later in maybe_process_partial_specialization. */
7411 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7412 = tree_cons (arglist
, t
,
7413 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7415 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7416 /* Now that the type has been registered on the instantiations
7417 list, we set up the enumerators. Because the enumeration
7418 constants may involve the enumeration type itself, we make
7419 sure to register the type first, and then create the
7420 constants. That way, doing tsubst_expr for the enumeration
7421 constants won't result in recursive calls here; we'll find
7422 the instantiation and exit above. */
7423 tsubst_enum (template_type
, t
, arglist
);
7425 if (is_dependent_type
)
7426 /* If the type makes use of template parameters, the
7427 code that generates debugging information will crash. */
7428 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7430 /* Possibly limit visibility based on template args. */
7431 TREE_PUBLIC (type_decl
) = 1;
7432 determine_visibility (type_decl
);
7438 /* Wrapper for lookup_template_class_1. */
7441 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7442 int entering_scope
, tsubst_flags_t complain
)
7445 timevar_push (TV_TEMPLATE_INST
);
7446 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7447 entering_scope
, complain
);
7448 timevar_pop (TV_TEMPLATE_INST
);
7456 /* True when we should also visit template parameters that occur in
7457 non-deduced contexts. */
7458 bool include_nondeduced_p
;
7459 struct pointer_set_t
*visited
;
7462 /* Called from for_each_template_parm via walk_tree. */
7465 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7468 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7469 tree_fn_t fn
= pfd
->fn
;
7470 void *data
= pfd
->data
;
7473 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7474 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7475 pfd
->include_nondeduced_p
))
7476 return error_mark_node
;
7478 switch (TREE_CODE (t
))
7481 if (TYPE_PTRMEMFUNC_P (t
))
7487 if (!TYPE_TEMPLATE_INFO (t
))
7489 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7490 fn
, data
, pfd
->visited
,
7491 pfd
->include_nondeduced_p
))
7492 return error_mark_node
;
7496 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7497 fn
, data
, pfd
->visited
,
7498 pfd
->include_nondeduced_p
)
7499 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7500 fn
, data
, pfd
->visited
,
7501 pfd
->include_nondeduced_p
))
7502 return error_mark_node
;
7506 /* Since we're not going to walk subtrees, we have to do this
7508 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7509 pfd
->visited
, pfd
->include_nondeduced_p
))
7510 return error_mark_node
;
7514 /* Check the return type. */
7515 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7516 pfd
->include_nondeduced_p
))
7517 return error_mark_node
;
7519 /* Check the parameter types. Since default arguments are not
7520 instantiated until they are needed, the TYPE_ARG_TYPES may
7521 contain expressions that involve template parameters. But,
7522 no-one should be looking at them yet. And, once they're
7523 instantiated, they don't contain template parameters, so
7524 there's no point in looking at them then, either. */
7528 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7529 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7530 pfd
->visited
, pfd
->include_nondeduced_p
))
7531 return error_mark_node
;
7533 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7534 want walk_tree walking into them itself. */
7540 case UNDERLYING_TYPE
:
7541 if (pfd
->include_nondeduced_p
7542 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7544 pfd
->include_nondeduced_p
))
7545 return error_mark_node
;
7550 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7551 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7552 pfd
->visited
, pfd
->include_nondeduced_p
))
7553 return error_mark_node
;
7558 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7559 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7560 pfd
->visited
, pfd
->include_nondeduced_p
))
7561 return error_mark_node
;
7562 if (DECL_CONTEXT (t
)
7563 && pfd
->include_nondeduced_p
7564 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7565 pfd
->visited
, pfd
->include_nondeduced_p
))
7566 return error_mark_node
;
7569 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7570 /* Record template parameters such as `T' inside `TT<T>'. */
7571 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7572 pfd
->include_nondeduced_p
))
7573 return error_mark_node
;
7576 case TEMPLATE_TEMPLATE_PARM
:
7577 case TEMPLATE_TYPE_PARM
:
7578 case TEMPLATE_PARM_INDEX
:
7579 if (fn
&& (*fn
)(t
, data
))
7580 return error_mark_node
;
7582 return error_mark_node
;
7586 /* A template template parameter is encountered. */
7587 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7588 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7589 pfd
->include_nondeduced_p
))
7590 return error_mark_node
;
7592 /* Already substituted template template parameter */
7598 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7600 pfd
->include_nondeduced_p
))
7601 return error_mark_node
;
7605 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7606 && pfd
->include_nondeduced_p
7607 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7608 (TREE_TYPE (t
)), fn
, data
,
7609 pfd
->visited
, pfd
->include_nondeduced_p
))
7610 return error_mark_node
;
7615 /* If there's no type, then this thing must be some expression
7616 involving template parameters. */
7617 if (!fn
&& !TREE_TYPE (t
))
7618 return error_mark_node
;
7623 case REINTERPRET_CAST_EXPR
:
7624 case CONST_CAST_EXPR
:
7625 case STATIC_CAST_EXPR
:
7626 case DYNAMIC_CAST_EXPR
:
7630 case PSEUDO_DTOR_EXPR
:
7632 return error_mark_node
;
7639 /* We didn't find any template parameters we liked. */
7643 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7644 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7645 call FN with the parameter and the DATA.
7646 If FN returns nonzero, the iteration is terminated, and
7647 for_each_template_parm returns 1. Otherwise, the iteration
7648 continues. If FN never returns a nonzero value, the value
7649 returned by for_each_template_parm is 0. If FN is NULL, it is
7650 considered to be the function which always returns 1.
7652 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7653 parameters that occur in non-deduced contexts. When false, only
7654 visits those template parameters that can be deduced. */
7657 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7658 struct pointer_set_t
*visited
,
7659 bool include_nondeduced_p
)
7661 struct pair_fn_data pfd
;
7667 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7669 /* Walk the tree. (Conceptually, we would like to walk without
7670 duplicates, but for_each_template_parm_r recursively calls
7671 for_each_template_parm, so we would need to reorganize a fair
7672 bit to use walk_tree_without_duplicates, so we keep our own
7675 pfd
.visited
= visited
;
7677 pfd
.visited
= pointer_set_create ();
7678 result
= cp_walk_tree (&t
,
7679 for_each_template_parm_r
,
7681 pfd
.visited
) != NULL_TREE
;
7686 pointer_set_destroy (pfd
.visited
);
7693 /* Returns true if T depends on any template parameter. */
7696 uses_template_parms (tree t
)
7699 int saved_processing_template_decl
;
7701 saved_processing_template_decl
= processing_template_decl
;
7702 if (!saved_processing_template_decl
)
7703 processing_template_decl
= 1;
7705 dependent_p
= dependent_type_p (t
);
7706 else if (TREE_CODE (t
) == TREE_VEC
)
7707 dependent_p
= any_dependent_template_arguments_p (t
);
7708 else if (TREE_CODE (t
) == TREE_LIST
)
7709 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7710 || uses_template_parms (TREE_CHAIN (t
)));
7711 else if (TREE_CODE (t
) == TYPE_DECL
)
7712 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7715 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7716 || TREE_CODE (t
) == OVERLOAD
7717 || TREE_CODE (t
) == BASELINK
7718 || TREE_CODE (t
) == IDENTIFIER_NODE
7719 || TREE_CODE (t
) == TRAIT_EXPR
7720 || TREE_CODE (t
) == CONSTRUCTOR
7721 || CONSTANT_CLASS_P (t
))
7722 dependent_p
= (type_dependent_expression_p (t
)
7723 || value_dependent_expression_p (t
));
7726 gcc_assert (t
== error_mark_node
);
7727 dependent_p
= false;
7730 processing_template_decl
= saved_processing_template_decl
;
7735 /* Returns true if T depends on any template parameter with level LEVEL. */
7738 uses_template_parms_level (tree t
, int level
)
7740 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7741 /*include_nondeduced_p=*/true);
7744 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7745 ill-formed translation unit, i.e. a variable or function that isn't
7746 usable in a constant expression. */
7749 neglectable_inst_p (tree d
)
7752 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
7753 : decl_maybe_constant_var_p (d
)));
7756 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7757 neglectable and instantiated from within an erroneous instantiation. */
7760 limit_bad_template_recursion (tree decl
)
7762 struct tinst_level
*lev
= current_tinst_level
;
7763 int errs
= errorcount
+ sorrycount
;
7764 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
7767 for (; lev
; lev
= lev
->next
)
7768 if (neglectable_inst_p (lev
->decl
))
7771 return (lev
&& errs
> lev
->errors
);
7774 static int tinst_depth
;
7775 extern int max_tinst_depth
;
7776 #ifdef GATHER_STATISTICS
7779 static GTY(()) struct tinst_level
*last_error_tinst_level
;
7781 /* We're starting to instantiate D; record the template instantiation context
7782 for diagnostics and to restore it later. */
7785 push_tinst_level (tree d
)
7787 struct tinst_level
*new_level
;
7789 if (tinst_depth
>= max_tinst_depth
)
7791 last_error_tinst_level
= current_tinst_level
;
7792 if (TREE_CODE (d
) == TREE_LIST
)
7793 error ("template instantiation depth exceeds maximum of %d (use "
7794 "-ftemplate-depth= to increase the maximum) substituting %qS",
7795 max_tinst_depth
, d
);
7797 error ("template instantiation depth exceeds maximum of %d (use "
7798 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7799 max_tinst_depth
, d
);
7801 print_instantiation_context ();
7806 /* If the current instantiation caused problems, don't let it instantiate
7807 anything else. Do allow deduction substitution and decls usable in
7808 constant expressions. */
7809 if (limit_bad_template_recursion (d
))
7812 new_level
= ggc_alloc_tinst_level ();
7813 new_level
->decl
= d
;
7814 new_level
->locus
= input_location
;
7815 new_level
->errors
= errorcount
+sorrycount
;
7816 new_level
->in_system_header_p
= in_system_header
;
7817 new_level
->next
= current_tinst_level
;
7818 current_tinst_level
= new_level
;
7821 #ifdef GATHER_STATISTICS
7822 if (tinst_depth
> depth_reached
)
7823 depth_reached
= tinst_depth
;
7829 /* We're done instantiating this template; return to the instantiation
7833 pop_tinst_level (void)
7835 /* Restore the filename and line number stashed away when we started
7836 this instantiation. */
7837 input_location
= current_tinst_level
->locus
;
7838 current_tinst_level
= current_tinst_level
->next
;
7842 /* We're instantiating a deferred template; restore the template
7843 instantiation context in which the instantiation was requested, which
7844 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7847 reopen_tinst_level (struct tinst_level
*level
)
7849 struct tinst_level
*t
;
7852 for (t
= level
; t
; t
= t
->next
)
7855 current_tinst_level
= level
;
7857 if (current_tinst_level
)
7858 current_tinst_level
->errors
= errorcount
+sorrycount
;
7862 /* Returns the TINST_LEVEL which gives the original instantiation
7865 struct tinst_level
*
7866 outermost_tinst_level (void)
7868 struct tinst_level
*level
= current_tinst_level
;
7871 level
= level
->next
;
7875 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
7878 parameter_of_template_p (tree parm
, tree templ
)
7883 if (!parm
|| !templ
)
7886 gcc_assert (DECL_TEMPLATE_PARM_P (parm
));
7887 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
7889 parms
= DECL_TEMPLATE_PARMS (templ
);
7890 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
7892 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
7894 tree p
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
7896 || (DECL_INITIAL (parm
)
7897 && DECL_INITIAL (parm
) == DECL_INITIAL (p
)))
7904 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7905 vector of template arguments, as for tsubst.
7907 Returns an appropriate tsubst'd friend declaration. */
7910 tsubst_friend_function (tree decl
, tree args
)
7914 if (TREE_CODE (decl
) == FUNCTION_DECL
7915 && DECL_TEMPLATE_INSTANTIATION (decl
)
7916 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
7917 /* This was a friend declared with an explicit template
7918 argument list, e.g.:
7922 to indicate that f was a template instantiation, not a new
7923 function declaration. Now, we have to figure out what
7924 instantiation of what template. */
7926 tree template_id
, arglist
, fns
;
7929 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
7931 /* Friend functions are looked up in the containing namespace scope.
7932 We must enter that scope, to avoid finding member functions of the
7933 current class with same name. */
7934 push_nested_namespace (ns
);
7935 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
7936 tf_warning_or_error
, NULL_TREE
,
7937 /*integral_constant_expression_p=*/false);
7938 pop_nested_namespace (ns
);
7939 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
7940 tf_warning_or_error
, NULL_TREE
);
7941 template_id
= lookup_template_function (fns
, arglist
);
7943 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7944 tmpl
= determine_specialization (template_id
, new_friend
,
7946 /*need_member_template=*/0,
7947 TREE_VEC_LENGTH (args
),
7949 return instantiate_template (tmpl
, new_args
, tf_error
);
7952 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7954 /* The NEW_FRIEND will look like an instantiation, to the
7955 compiler, but is not an instantiation from the point of view of
7956 the language. For example, we might have had:
7958 template <class T> struct S {
7959 template <class U> friend void f(T, U);
7962 Then, in S<int>, template <class U> void f(int, U) is not an
7963 instantiation of anything. */
7964 if (new_friend
== error_mark_node
)
7965 return error_mark_node
;
7967 DECL_USE_TEMPLATE (new_friend
) = 0;
7968 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7970 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
7971 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
7972 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
7975 /* The mangled name for the NEW_FRIEND is incorrect. The function
7976 is not a template instantiation and should not be mangled like
7977 one. Therefore, we forget the mangling here; we'll recompute it
7978 later if we need it. */
7979 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
7981 SET_DECL_RTL (new_friend
, NULL
);
7982 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
7985 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
7988 tree new_friend_template_info
;
7989 tree new_friend_result_template_info
;
7991 int new_friend_is_defn
;
7993 /* We must save some information from NEW_FRIEND before calling
7994 duplicate decls since that function will free NEW_FRIEND if
7996 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
7997 new_friend_is_defn
=
7998 (DECL_INITIAL (DECL_TEMPLATE_RESULT
7999 (template_for_substitution (new_friend
)))
8001 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8003 /* This declaration is a `primary' template. */
8004 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8006 new_friend_result_template_info
8007 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8010 new_friend_result_template_info
= NULL_TREE
;
8012 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8013 if (new_friend_is_defn
)
8014 DECL_INITIAL (new_friend
) = error_mark_node
;
8016 /* Inside pushdecl_namespace_level, we will push into the
8017 current namespace. However, the friend function should go
8018 into the namespace of the template. */
8019 ns
= decl_namespace_context (new_friend
);
8020 push_nested_namespace (ns
);
8021 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8022 pop_nested_namespace (ns
);
8024 if (old_decl
== error_mark_node
)
8025 return error_mark_node
;
8027 if (old_decl
!= new_friend
)
8029 /* This new friend declaration matched an existing
8030 declaration. For example, given:
8032 template <class T> void f(T);
8033 template <class U> class C {
8034 template <class T> friend void f(T) {}
8037 the friend declaration actually provides the definition
8038 of `f', once C has been instantiated for some type. So,
8039 old_decl will be the out-of-class template declaration,
8040 while new_friend is the in-class definition.
8042 But, if `f' was called before this point, the
8043 instantiation of `f' will have DECL_TI_ARGS corresponding
8044 to `T' but not to `U', references to which might appear
8045 in the definition of `f'. Previously, the most general
8046 template for an instantiation of `f' was the out-of-class
8047 version; now it is the in-class version. Therefore, we
8048 run through all specialization of `f', adding to their
8049 DECL_TI_ARGS appropriately. In particular, they need a
8050 new set of outer arguments, corresponding to the
8051 arguments for this class instantiation.
8053 The same situation can arise with something like this:
8056 template <class T> class C {
8060 when `C<int>' is instantiated. Now, `f(int)' is defined
8063 if (!new_friend_is_defn
)
8064 /* On the other hand, if the in-class declaration does
8065 *not* provide a definition, then we don't want to alter
8066 existing definitions. We can just leave everything
8071 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8072 tree new_args
= TI_ARGS (new_friend_template_info
);
8074 /* Overwrite whatever template info was there before, if
8075 any, with the new template information pertaining to
8077 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8079 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8081 /* We should have called reregister_specialization in
8083 gcc_assert (retrieve_specialization (new_template
,
8087 /* Instantiate it if the global has already been used. */
8088 if (DECL_ODR_USED (old_decl
))
8089 instantiate_decl (old_decl
, /*defer_ok=*/true,
8090 /*expl_inst_class_mem_p=*/false);
8096 /* Indicate that the old function template is a partial
8098 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8099 = new_friend_result_template_info
;
8101 gcc_assert (new_template
8102 == most_general_template (new_template
));
8103 gcc_assert (new_template
!= old_decl
);
8105 /* Reassign any specializations already in the hash table
8106 to the new more general template, and add the
8107 additional template args. */
8108 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8112 tree spec
= TREE_VALUE (t
);
8115 elt
.tmpl
= old_decl
;
8116 elt
.args
= DECL_TI_ARGS (spec
);
8117 elt
.spec
= NULL_TREE
;
8119 htab_remove_elt (decl_specializations
, &elt
);
8122 = add_outermost_template_args (new_args
,
8123 DECL_TI_ARGS (spec
));
8125 register_specialization
8126 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8129 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8133 /* The information from NEW_FRIEND has been merged into OLD_DECL
8134 by duplicate_decls. */
8135 new_friend
= old_decl
;
8140 tree context
= DECL_CONTEXT (new_friend
);
8144 template <class T> class C {
8145 template <class U> friend void C1<U>::f (); // case 1
8146 friend void C2<T>::f (); // case 2
8148 we only need to make sure CONTEXT is a complete type for
8149 case 2. To distinguish between the two cases, we note that
8150 CONTEXT of case 1 remains dependent type after tsubst while
8151 this isn't true for case 2. */
8152 ++processing_template_decl
;
8153 dependent_p
= dependent_type_p (context
);
8154 --processing_template_decl
;
8157 && !complete_type_or_else (context
, NULL_TREE
))
8158 return error_mark_node
;
8160 if (COMPLETE_TYPE_P (context
))
8162 /* Check to see that the declaration is really present, and,
8163 possibly obtain an improved declaration. */
8164 tree fn
= check_classfn (context
,
8165 new_friend
, NULL_TREE
);
8175 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8176 template arguments, as for tsubst.
8178 Returns an appropriate tsubst'd friend type or error_mark_node on
8182 tsubst_friend_class (tree friend_tmpl
, tree args
)
8188 context
= CP_DECL_CONTEXT (friend_tmpl
);
8190 if (context
!= global_namespace
)
8192 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8193 push_nested_namespace (context
);
8195 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8198 /* Look for a class template declaration. We look for hidden names
8199 because two friend declarations of the same template are the
8200 same. For example, in:
8203 template <typename> friend class F;
8205 template <typename> struct B {
8206 template <typename> friend class F;
8209 both F templates are the same. */
8210 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8211 /*block_p=*/true, 0,
8212 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
8214 /* But, if we don't find one, it might be because we're in a
8215 situation like this:
8223 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8224 for `S<int>', not the TEMPLATE_DECL. */
8225 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8227 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8228 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8231 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8233 /* The friend template has already been declared. Just
8234 check to see that the declarations match, and install any new
8235 default parameters. We must tsubst the default parameters,
8236 of course. We only need the innermost template parameters
8237 because that is all that redeclare_class_template will look
8239 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8240 > TMPL_ARGS_DEPTH (args
))
8243 location_t saved_input_location
;
8244 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8245 args
, tf_warning_or_error
);
8247 saved_input_location
= input_location
;
8248 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8249 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8250 input_location
= saved_input_location
;
8254 friend_type
= TREE_TYPE (tmpl
);
8258 /* The friend template has not already been declared. In this
8259 case, the instantiation of the template class will cause the
8260 injection of this template into the global scope. */
8261 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8262 if (tmpl
== error_mark_node
)
8263 return error_mark_node
;
8265 /* The new TMPL is not an instantiation of anything, so we
8266 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8267 the new type because that is supposed to be the corresponding
8268 template decl, i.e., TMPL. */
8269 DECL_USE_TEMPLATE (tmpl
) = 0;
8270 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8271 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8272 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8273 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8275 /* Inject this template into the global scope. */
8276 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8279 if (context
!= global_namespace
)
8281 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8282 pop_nested_namespace (context
);
8284 pop_nested_class ();
8290 /* Returns zero if TYPE cannot be completed later due to circularity.
8291 Otherwise returns one. */
8294 can_complete_type_without_circularity (tree type
)
8296 if (type
== NULL_TREE
|| type
== error_mark_node
)
8298 else if (COMPLETE_TYPE_P (type
))
8300 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8301 return can_complete_type_without_circularity (TREE_TYPE (type
));
8302 else if (CLASS_TYPE_P (type
)
8303 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8309 /* Apply any attributes which had to be deferred until instantiation
8310 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8311 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8314 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8315 tree args
, tsubst_flags_t complain
, tree in_decl
)
8317 tree last_dep
= NULL_TREE
;
8321 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8322 if (ATTR_IS_DEPENDENT (t
))
8325 attributes
= copy_list (attributes
);
8329 if (DECL_P (*decl_p
))
8331 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8333 p
= &DECL_ATTRIBUTES (*decl_p
);
8336 p
= &TYPE_ATTRIBUTES (*decl_p
);
8340 tree late_attrs
= NULL_TREE
;
8341 tree
*q
= &late_attrs
;
8343 for (*p
= attributes
; *p
; )
8346 if (ATTR_IS_DEPENDENT (t
))
8348 *p
= TREE_CHAIN (t
);
8349 TREE_CHAIN (t
) = NULL_TREE
;
8350 /* If the first attribute argument is an identifier, don't
8351 pass it through tsubst. Attributes like mode, format,
8352 cleanup and several target specific attributes expect it
8355 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8356 && TREE_VALUE (TREE_VALUE (t
))
8357 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8358 == IDENTIFIER_NODE
))
8361 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8363 /*integral_constant_expression_p=*/false);
8364 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8366 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8371 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8372 /*integral_constant_expression_p=*/false);
8374 q
= &TREE_CHAIN (t
);
8377 p
= &TREE_CHAIN (t
);
8380 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8384 /* Perform (or defer) access check for typedefs that were referenced
8385 from within the template TMPL code.
8386 This is a subroutine of instantiate_template and instantiate_class_template.
8387 TMPL is the template to consider and TARGS is the list of arguments of
8391 perform_typedefs_access_check (tree tmpl
, tree targs
)
8393 location_t saved_location
;
8395 qualified_typedef_usage_t
*iter
;
8398 || (!CLASS_TYPE_P (tmpl
)
8399 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8402 saved_location
= input_location
;
8403 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
8404 get_types_needing_access_check (tmpl
),
8407 tree type_decl
= iter
->typedef_decl
;
8408 tree type_scope
= iter
->context
;
8410 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8413 if (uses_template_parms (type_decl
))
8414 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8415 if (uses_template_parms (type_scope
))
8416 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8418 /* Make access check error messages point to the location
8419 of the use of the typedef. */
8420 input_location
= iter
->locus
;
8421 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8422 type_decl
, type_decl
);
8424 input_location
= saved_location
;
8428 instantiate_class_template_1 (tree type
)
8430 tree templ
, args
, pattern
, t
, member
;
8434 unsigned int saved_maximum_field_alignment
;
8436 if (type
== error_mark_node
)
8437 return error_mark_node
;
8439 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8440 || uses_template_parms (type
))
8443 /* Figure out which template is being instantiated. */
8444 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8445 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8447 /* Determine what specialization of the original template to
8449 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8450 if (t
== error_mark_node
)
8452 TYPE_BEING_DEFINED (type
) = 1;
8453 return error_mark_node
;
8457 /* This TYPE is actually an instantiation of a partial
8458 specialization. We replace the innermost set of ARGS with
8459 the arguments appropriate for substitution. For example,
8462 template <class T> struct S {};
8463 template <class T> struct S<T*> {};
8465 and supposing that we are instantiating S<int*>, ARGS will
8466 presently be {int*} -- but we need {int}. */
8467 pattern
= TREE_TYPE (t
);
8468 args
= TREE_PURPOSE (t
);
8472 pattern
= TREE_TYPE (templ
);
8473 args
= CLASSTYPE_TI_ARGS (type
);
8476 /* If the template we're instantiating is incomplete, then clearly
8477 there's nothing we can do. */
8478 if (!COMPLETE_TYPE_P (pattern
))
8481 /* If we've recursively instantiated too many templates, stop. */
8482 if (! push_tinst_level (type
))
8485 /* Now we're really doing the instantiation. Mark the type as in
8486 the process of being defined. */
8487 TYPE_BEING_DEFINED (type
) = 1;
8489 /* We may be in the middle of deferred access check. Disable
8491 push_deferring_access_checks (dk_no_deferred
);
8493 push_to_top_level ();
8494 /* Use #pragma pack from the template context. */
8495 saved_maximum_field_alignment
= maximum_field_alignment
;
8496 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8498 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8500 /* Set the input location to the most specialized template definition.
8501 This is needed if tsubsting causes an error. */
8502 typedecl
= TYPE_MAIN_DECL (pattern
);
8503 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8504 DECL_SOURCE_LOCATION (typedecl
);
8506 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8507 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8508 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8509 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8510 if (ANON_AGGR_TYPE_P (pattern
))
8511 SET_ANON_AGGR_TYPE_P (type
);
8512 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8514 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8515 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8517 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8519 pbinfo
= TYPE_BINFO (pattern
);
8521 /* We should never instantiate a nested class before its enclosing
8522 class; we need to look up the nested class by name before we can
8523 instantiate it, and that lookup should instantiate the enclosing
8525 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8526 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8528 base_list
= NULL_TREE
;
8529 if (BINFO_N_BASE_BINFOS (pbinfo
))
8535 /* We must enter the scope containing the type, as that is where
8536 the accessibility of types named in dependent bases are
8538 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8540 /* Substitute into each of the bases to determine the actual
8542 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8545 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8546 tree expanded_bases
= NULL_TREE
;
8549 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8552 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8553 args
, tf_error
, NULL_TREE
);
8554 if (expanded_bases
== error_mark_node
)
8557 len
= TREE_VEC_LENGTH (expanded_bases
);
8560 for (idx
= 0; idx
< len
; idx
++)
8563 /* Extract the already-expanded base class. */
8564 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8566 /* Substitute to figure out the base class. */
8567 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8570 if (base
== error_mark_node
)
8573 base_list
= tree_cons (access
, base
, base_list
);
8574 if (BINFO_VIRTUAL_P (pbase_binfo
))
8575 TREE_TYPE (base_list
) = integer_type_node
;
8579 /* The list is now in reverse order; correct that. */
8580 base_list
= nreverse (base_list
);
8583 pop_scope (pushed_scope
);
8585 /* Now call xref_basetypes to set up all the base-class
8587 xref_basetypes (type
, base_list
);
8589 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8590 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8591 args
, tf_error
, NULL_TREE
);
8592 fixup_attribute_variants (type
);
8594 /* Now that our base classes are set up, enter the scope of the
8595 class, so that name lookups into base classes, etc. will work
8596 correctly. This is precisely analogous to what we do in
8597 begin_class_definition when defining an ordinary non-template
8598 class, except we also need to push the enclosing classes. */
8599 push_nested_class (type
);
8601 /* Now members are processed in the order of declaration. */
8602 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8603 member
; member
= TREE_CHAIN (member
))
8605 tree t
= TREE_VALUE (member
);
8607 if (TREE_PURPOSE (member
))
8611 /* Build new CLASSTYPE_NESTED_UTDS. */
8614 bool class_template_p
;
8616 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8617 && TYPE_LANG_SPECIFIC (t
)
8618 && CLASSTYPE_IS_TEMPLATE (t
));
8619 /* If the member is a class template, then -- even after
8620 substitution -- there may be dependent types in the
8621 template argument list for the class. We increment
8622 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8623 that function will assume that no types are dependent
8624 when outside of a template. */
8625 if (class_template_p
)
8626 ++processing_template_decl
;
8627 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8628 if (class_template_p
)
8629 --processing_template_decl
;
8630 if (newtag
== error_mark_node
)
8633 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8635 tree name
= TYPE_IDENTIFIER (t
);
8637 if (class_template_p
)
8638 /* Unfortunately, lookup_template_class sets
8639 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8640 instantiation (i.e., for the type of a member
8641 template class nested within a template class.)
8642 This behavior is required for
8643 maybe_process_partial_specialization to work
8644 correctly, but is not accurate in this case;
8645 the TAG is not an instantiation of anything.
8646 (The corresponding TEMPLATE_DECL is an
8647 instantiation, but the TYPE is not.) */
8648 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8650 /* Now, we call pushtag to put this NEWTAG into the scope of
8651 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8652 pushtag calling push_template_decl. We don't have to do
8653 this for enums because it will already have been done in
8656 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8657 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8660 else if (TREE_CODE (t
) == FUNCTION_DECL
8661 || DECL_FUNCTION_TEMPLATE_P (t
))
8663 /* Build new TYPE_METHODS. */
8666 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8667 ++processing_template_decl
;
8668 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8669 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8670 --processing_template_decl
;
8671 set_current_access_from_decl (r
);
8672 finish_member_declaration (r
);
8673 /* Instantiate members marked with attribute used. */
8674 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8679 /* Build new TYPE_FIELDS. */
8680 if (TREE_CODE (t
) == STATIC_ASSERT
)
8683 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8684 tf_warning_or_error
, NULL_TREE
,
8685 /*integral_constant_expression_p=*/true);
8686 finish_static_assert (condition
,
8687 STATIC_ASSERT_MESSAGE (t
),
8688 STATIC_ASSERT_SOURCE_LOCATION (t
),
8691 else if (TREE_CODE (t
) != CONST_DECL
)
8695 /* The file and line for this declaration, to
8696 assist in error message reporting. Since we
8697 called push_tinst_level above, we don't need to
8699 input_location
= DECL_SOURCE_LOCATION (t
);
8701 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8702 ++processing_template_decl
;
8703 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8704 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8705 --processing_template_decl
;
8706 if (TREE_CODE (r
) == VAR_DECL
)
8710 [t]he initialization (and any associated
8711 side-effects) of a static data member does
8712 not occur unless the static data member is
8713 itself used in a way that requires the
8714 definition of the static data member to
8717 Therefore, we do not substitute into the
8718 initialized for the static data member here. */
8719 finish_static_data_member_decl
8722 /*init_const_expr_p=*/false,
8723 /*asmspec_tree=*/NULL_TREE
,
8725 /* Instantiate members marked with attribute used. */
8726 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8729 else if (TREE_CODE (r
) == FIELD_DECL
)
8731 /* Determine whether R has a valid type and can be
8732 completed later. If R is invalid, then it is
8733 replaced by error_mark_node so that it will not be
8734 added to TYPE_FIELDS. */
8735 tree rtype
= TREE_TYPE (r
);
8736 if (can_complete_type_without_circularity (rtype
))
8737 complete_type (rtype
);
8739 if (!COMPLETE_TYPE_P (rtype
))
8741 cxx_incomplete_type_error (r
, rtype
);
8742 r
= error_mark_node
;
8746 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8747 such a thing will already have been added to the field
8748 list by tsubst_enum in finish_member_declaration in the
8749 CLASSTYPE_NESTED_UTDS case above. */
8750 if (!(TREE_CODE (r
) == TYPE_DECL
8751 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8752 && DECL_ARTIFICIAL (r
)))
8754 set_current_access_from_decl (r
);
8755 finish_member_declaration (r
);
8762 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
8764 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8766 tree friend_type
= t
;
8767 bool adjust_processing_template_decl
= false;
8769 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8771 /* template <class T> friend class C; */
8772 friend_type
= tsubst_friend_class (friend_type
, args
);
8773 adjust_processing_template_decl
= true;
8775 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8777 /* template <class T> friend class C::D; */
8778 friend_type
= tsubst (friend_type
, args
,
8779 tf_warning_or_error
, NULL_TREE
);
8780 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8781 friend_type
= TREE_TYPE (friend_type
);
8782 adjust_processing_template_decl
= true;
8784 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
8785 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
8787 /* This could be either
8791 when dependent_type_p is false or
8793 template <class U> friend class T::C;
8796 friend_type
= tsubst (friend_type
, args
,
8797 tf_warning_or_error
, NULL_TREE
);
8798 /* Bump processing_template_decl for correct
8799 dependent_type_p calculation. */
8800 ++processing_template_decl
;
8801 if (dependent_type_p (friend_type
))
8802 adjust_processing_template_decl
= true;
8803 --processing_template_decl
;
8805 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8806 && hidden_name_p (TYPE_NAME (friend_type
)))
8810 where C hasn't been declared yet. Let's lookup name
8811 from namespace scope directly, bypassing any name that
8812 come from dependent base class. */
8813 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8815 /* The call to xref_tag_from_type does injection for friend
8817 push_nested_namespace (ns
);
8819 xref_tag_from_type (friend_type
, NULL_TREE
,
8820 /*tag_scope=*/ts_current
);
8821 pop_nested_namespace (ns
);
8823 else if (uses_template_parms (friend_type
))
8824 /* friend class C<T>; */
8825 friend_type
= tsubst (friend_type
, args
,
8826 tf_warning_or_error
, NULL_TREE
);
8831 where C is already declared or
8833 friend class C<int>;
8835 We don't have to do anything in these cases. */
8837 if (adjust_processing_template_decl
)
8838 /* Trick make_friend_class into realizing that the friend
8839 we're adding is a template, not an ordinary class. It's
8840 important that we use make_friend_class since it will
8841 perform some error-checking and output cross-reference
8843 ++processing_template_decl
;
8845 if (friend_type
!= error_mark_node
)
8846 make_friend_class (type
, friend_type
, /*complain=*/false);
8848 if (adjust_processing_template_decl
)
8849 --processing_template_decl
;
8853 /* Build new DECL_FRIENDLIST. */
8856 /* The file and line for this declaration, to
8857 assist in error message reporting. Since we
8858 called push_tinst_level above, we don't need to
8860 input_location
= DECL_SOURCE_LOCATION (t
);
8862 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8864 ++processing_template_decl
;
8865 push_deferring_access_checks (dk_no_check
);
8868 r
= tsubst_friend_function (t
, args
);
8869 add_friend (type
, r
, /*complain=*/false);
8870 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8872 pop_deferring_access_checks ();
8873 --processing_template_decl
;
8879 if (CLASSTYPE_LAMBDA_EXPR (type
))
8881 tree lambda
= CLASSTYPE_LAMBDA_EXPR (type
);
8882 if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda
))
8884 apply_lambda_return_type (lambda
, void_type_node
);
8885 LAMBDA_EXPR_RETURN_TYPE (lambda
) = NULL_TREE
;
8887 instantiate_decl (lambda_function (type
), false, false);
8888 maybe_add_lambda_conv_op (type
);
8891 /* Set the file and line number information to whatever is given for
8892 the class itself. This puts error messages involving generated
8893 implicit functions at a predictable point, and the same point
8894 that would be used for non-template classes. */
8895 input_location
= DECL_SOURCE_LOCATION (typedecl
);
8897 unreverse_member_declarations (type
);
8898 finish_struct_1 (type
);
8899 TYPE_BEING_DEFINED (type
) = 0;
8901 /* We don't instantiate default arguments for member functions. 14.7.1:
8903 The implicit instantiation of a class template specialization causes
8904 the implicit instantiation of the declarations, but not of the
8905 definitions or default arguments, of the class member functions,
8906 member classes, static data members and member templates.... */
8908 /* Some typedefs referenced from within the template code need to be access
8909 checked at template instantiation time, i.e now. These types were
8910 added to the template at parsing time. Let's get those and perform
8911 the access checks then. */
8912 perform_typedefs_access_check (pattern
, args
);
8913 perform_deferred_access_checks ();
8914 pop_nested_class ();
8915 maximum_field_alignment
= saved_maximum_field_alignment
;
8916 pop_from_top_level ();
8917 pop_deferring_access_checks ();
8920 /* The vtable for a template class can be emitted in any translation
8921 unit in which the class is instantiated. When there is no key
8922 method, however, finish_struct_1 will already have added TYPE to
8923 the keyed_classes list. */
8924 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
8925 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
8930 /* Wrapper for instantiate_class_template_1. */
8933 instantiate_class_template (tree type
)
8936 timevar_push (TV_TEMPLATE_INST
);
8937 ret
= instantiate_class_template_1 (type
);
8938 timevar_pop (TV_TEMPLATE_INST
);
8943 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
8949 else if (TYPE_P (t
))
8950 r
= tsubst (t
, args
, complain
, in_decl
);
8953 if (!(complain
& tf_warning
))
8954 ++c_inhibit_evaluation_warnings
;
8955 r
= tsubst_expr (t
, args
, complain
, in_decl
,
8956 /*integral_constant_expression_p=*/true);
8957 if (!(complain
& tf_warning
))
8958 --c_inhibit_evaluation_warnings
;
8959 /* Preserve the raw-reference nature of T. */
8960 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
8961 && REFERENCE_REF_P (r
))
8962 r
= TREE_OPERAND (r
, 0);
8967 /* Given a function parameter pack TMPL_PARM and some function parameters
8968 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
8969 and set *SPEC_P to point at the next point in the list. */
8972 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
8974 /* Collect all of the extra "packed" parameters into an
8978 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
8979 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
8980 tree spec_parm
= *spec_p
;
8983 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
8985 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
8988 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
8989 parmvec
= make_tree_vec (len
);
8990 parmtypevec
= make_tree_vec (len
);
8991 spec_parm
= *spec_p
;
8992 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
8994 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
8995 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
8998 /* Build the argument packs. */
8999 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9000 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9001 TREE_TYPE (argpack
) = argtypepack
;
9002 *spec_p
= spec_parm
;
9007 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9008 NONTYPE_ARGUMENT_PACK. */
9011 make_fnparm_pack (tree spec_parm
)
9013 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9016 /* Substitute ARGS into T, which is an pack expansion
9017 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9018 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9019 (if only a partial substitution could be performed) or
9020 ERROR_MARK_NODE if there was an error. */
9022 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9026 tree pack
, packs
= NULL_TREE
, unsubstituted_packs
= NULL_TREE
;
9030 htab_t saved_local_specializations
= NULL
;
9032 gcc_assert (PACK_EXPANSION_P (t
));
9033 pattern
= PACK_EXPANSION_PATTERN (t
);
9035 /* Determine the argument packs that will instantiate the parameter
9036 packs used in the expansion expression. While we're at it,
9037 compute the number of arguments to be expanded and make sure it
9039 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9040 pack
= TREE_CHAIN (pack
))
9042 tree parm_pack
= TREE_VALUE (pack
);
9043 tree arg_pack
= NULL_TREE
;
9044 tree orig_arg
= NULL_TREE
;
9046 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9048 if (!cp_unevaluated_operand
)
9049 arg_pack
= retrieve_local_specialization (parm_pack
);
9052 /* We can't rely on local_specializations for a parameter
9053 name used later in a function declaration (such as in a
9054 late-specified return type). Even if it exists, it might
9055 have the wrong value for a recursive call. Just make a
9056 dummy decl, since it's only used for its type. */
9057 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9058 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9059 /* Partial instantiation of the parm_pack, we can't build
9060 up an argument pack yet. */
9061 arg_pack
= NULL_TREE
;
9063 arg_pack
= make_fnparm_pack (arg_pack
);
9068 int level
, idx
, levels
;
9069 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9071 levels
= TMPL_ARGS_DEPTH (args
);
9072 if (level
<= levels
)
9073 arg_pack
= TMPL_ARG (args
, level
, idx
);
9076 orig_arg
= arg_pack
;
9077 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9078 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9080 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9081 /* This can only happen if we forget to expand an argument
9082 pack somewhere else. Just return an error, silently. */
9084 result
= make_tree_vec (1);
9085 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9089 /* For clarity in the comments below let's use the
9090 representation 'argument_pack<elements>' to denote an
9091 argument pack and its elements.
9093 In the 'if' block below, we want to detect cases where
9094 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
9095 check if ARG_PACK is an argument pack which sole element is
9096 the expansion of PARM_PACK. That argument pack is typically
9097 created by template_parm_to_arg when passed a parameter
9100 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
9101 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
9103 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
9104 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
9105 /* So we have an argument_pack<P...>. We want to test if P
9106 is actually PARM_PACK. We will not use cp_tree_equal to
9107 test P and PARM_PACK because during type fixup (by
9108 fixup_template_parm) P can be a pre-fixup version of a
9109 type and PARM_PACK be its post-fixup version.
9110 cp_tree_equal would consider them as different even
9111 though we would want to consider them compatible for our
9112 precise purpose here.
9114 Thus we are going to consider that P and PARM_PACK are
9115 compatible if they have the same DECL. */
9116 if ((/* If ARG_PACK is a type parameter pack named by the
9117 same DECL as parm_pack ... */
9119 && TYPE_P (parm_pack
)
9120 && TYPE_NAME (pattern
) == TYPE_NAME (parm_pack
))
9121 /* ... or if ARG_PACK is a non-type parameter
9122 named by the same DECL as parm_pack ... */
9123 || (TREE_CODE (pattern
) == TEMPLATE_PARM_INDEX
9124 && TREE_CODE (parm_pack
) == PARM_DECL
9125 && TEMPLATE_PARM_DECL (pattern
)
9126 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack
))))
9127 && template_parameter_pack_p (pattern
))
9128 /* ... then the argument pack that the parameter maps to
9129 is just an expansion of the parameter itself, such as
9130 one would find in the implicit typedef of a class
9131 inside the class itself. Consider this parameter
9132 "unsubstituted", so that we will maintain the outer
9134 arg_pack
= NULL_TREE
;
9140 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9142 /* It's all-or-nothing with incomplete argument packs. */
9143 if (incomplete
&& !ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9144 return error_mark_node
;
9146 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9151 else if (len
!= my_len
)
9154 /* We got explicit args for some packs but not others;
9155 do nothing now and try again after deduction. */
9157 if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9158 error ("mismatched argument pack lengths while expanding "
9162 error ("mismatched argument pack lengths while expanding "
9165 return error_mark_node
;
9168 /* Keep track of the parameter packs and their corresponding
9170 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9171 TREE_TYPE (packs
) = orig_arg
;
9174 /* We can't substitute for this parameter pack. */
9175 unsubstituted_packs
= tree_cons (TREE_PURPOSE (pack
),
9177 unsubstituted_packs
);
9180 /* We cannot expand this expansion expression, because we don't have
9181 all of the argument packs we need. Substitute into the pattern
9182 and return a PACK_EXPANSION_*. The caller will need to deal with
9184 if (unsubstituted_packs
)
9187 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9188 new_pat
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9189 /*integral_constant_expression_p=*/false);
9191 new_pat
= tsubst (pattern
, args
, complain
, in_decl
);
9192 return make_pack_expansion (new_pat
);
9195 /* We could not find any argument packs that work. */
9197 return error_mark_node
;
9199 if (cp_unevaluated_operand
)
9201 /* We're in a late-specified return type, so create our own local
9202 specializations table; the current table is either NULL or (in the
9203 case of recursive unification) might have bindings that we don't
9204 want to use or alter. */
9205 saved_local_specializations
= local_specializations
;
9206 local_specializations
= htab_create (37,
9207 hash_local_specialization
,
9208 eq_local_specializations
,
9212 /* For each argument in each argument pack, substitute into the
9214 result
= make_tree_vec (len
+ incomplete
);
9215 for (i
= 0; i
< len
+ incomplete
; ++i
)
9217 /* For parameter pack, change the substitution of the parameter
9218 pack to the ith argument in its argument pack, then expand
9220 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9222 tree parm
= TREE_PURPOSE (pack
);
9224 if (TREE_CODE (parm
) == PARM_DECL
)
9226 /* Select the Ith argument from the pack. */
9227 tree arg
= make_node (ARGUMENT_PACK_SELECT
);
9228 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9229 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
9231 register_local_specialization (arg
, parm
);
9237 template_parm_level_and_index (parm
, &level
, &idx
);
9241 /* Select the Ith argument from the pack. */
9242 value
= make_node (ARGUMENT_PACK_SELECT
);
9243 ARGUMENT_PACK_SELECT_FROM_PACK (value
) = TREE_VALUE (pack
);
9244 ARGUMENT_PACK_SELECT_INDEX (value
) = i
;
9247 /* Update the corresponding argument. */
9248 TMPL_ARG (args
, level
, idx
) = value
;
9252 /* Substitute into the PATTERN with the altered arguments. */
9253 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9254 TREE_VEC_ELT (result
, i
) =
9255 tsubst_expr (pattern
, args
, complain
, in_decl
,
9256 /*integral_constant_expression_p=*/false);
9258 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
9261 /* When we have incomplete argument packs, the last "expanded"
9262 result is itself a pack expansion, which allows us
9263 to deduce more arguments. */
9264 TREE_VEC_ELT (result
, i
) =
9265 make_pack_expansion (TREE_VEC_ELT (result
, i
));
9267 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
9269 result
= error_mark_node
;
9274 /* Update ARGS to restore the substitution from parameter packs to
9275 their argument packs. */
9276 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9278 tree parm
= TREE_PURPOSE (pack
);
9280 if (TREE_CODE (parm
) == PARM_DECL
)
9281 register_local_specialization (TREE_TYPE (pack
), parm
);
9285 template_parm_level_and_index (parm
, &level
, &idx
);
9287 /* Update the corresponding argument. */
9288 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9289 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9292 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9296 if (saved_local_specializations
)
9298 htab_delete (local_specializations
);
9299 local_specializations
= saved_local_specializations
;
9305 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9306 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9307 parameter packs; all parms generated from a function parameter pack will
9308 have the same DECL_PARM_INDEX. */
9311 get_pattern_parm (tree parm
, tree tmpl
)
9313 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9316 if (DECL_ARTIFICIAL (parm
))
9318 for (patparm
= DECL_ARGUMENTS (pattern
);
9319 patparm
; patparm
= DECL_CHAIN (patparm
))
9320 if (DECL_ARTIFICIAL (patparm
)
9321 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9326 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9327 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9328 gcc_assert (DECL_PARM_INDEX (patparm
)
9329 == DECL_PARM_INDEX (parm
));
9335 /* Substitute ARGS into the vector or list of template arguments T. */
9338 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9341 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9344 if (t
== error_mark_node
)
9345 return error_mark_node
;
9347 len
= TREE_VEC_LENGTH (t
);
9348 elts
= XALLOCAVEC (tree
, len
);
9350 for (i
= 0; i
< len
; i
++)
9352 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9355 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9356 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9357 else if (PACK_EXPANSION_P (orig_arg
))
9359 /* Substitute into an expansion expression. */
9360 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9362 if (TREE_CODE (new_arg
) == TREE_VEC
)
9363 /* Add to the expanded length adjustment the number of
9364 expanded arguments. We subtract one from this
9365 measurement, because the argument pack expression
9366 itself is already counted as 1 in
9367 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9368 the argument pack is empty. */
9369 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9371 else if (ARGUMENT_PACK_P (orig_arg
))
9373 /* Substitute into each of the arguments. */
9374 new_arg
= TYPE_P (orig_arg
)
9375 ? cxx_make_type (TREE_CODE (orig_arg
))
9376 : make_node (TREE_CODE (orig_arg
));
9378 SET_ARGUMENT_PACK_ARGS (
9380 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9381 args
, complain
, in_decl
));
9383 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9384 new_arg
= error_mark_node
;
9386 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9387 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9389 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9391 if (TREE_TYPE (new_arg
) == error_mark_node
)
9392 new_arg
= error_mark_node
;
9396 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9398 if (new_arg
== error_mark_node
)
9399 return error_mark_node
;
9402 if (new_arg
!= orig_arg
)
9409 /* Make space for the expanded arguments coming from template
9411 t
= make_tree_vec (len
+ expanded_len_adjust
);
9412 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9413 arguments for a member template.
9414 In that case each TREE_VEC in ORIG_T represents a level of template
9415 arguments, and ORIG_T won't carry any non defaulted argument count.
9416 It will rather be the nested TREE_VECs that will carry one.
9417 In other words, ORIG_T carries a non defaulted argument count only
9418 if it doesn't contain any nested TREE_VEC. */
9419 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9421 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9422 count
+= expanded_len_adjust
;
9423 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9425 for (i
= 0, out
= 0; i
< len
; i
++)
9427 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9428 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9429 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9433 /* Now expand the template argument pack "in place". */
9434 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9435 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9439 TREE_VEC_ELT (t
, out
) = elts
[i
];
9447 /* Return the result of substituting ARGS into the template parameters
9448 given by PARMS. If there are m levels of ARGS and m + n levels of
9449 PARMS, then the result will contain n levels of PARMS. For
9450 example, if PARMS is `template <class T> template <class U>
9451 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9452 result will be `template <int*, double, class V>'. */
9455 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9460 /* When substituting into a template, we must set
9461 PROCESSING_TEMPLATE_DECL as the template parameters may be
9462 dependent if they are based on one-another, and the dependency
9463 predicates are short-circuit outside of templates. */
9464 ++processing_template_decl
;
9466 for (new_parms
= &r
;
9467 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9468 new_parms
= &(TREE_CHAIN (*new_parms
)),
9469 parms
= TREE_CHAIN (parms
))
9472 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9475 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9479 if (parms
== error_mark_node
)
9482 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9484 if (tuple
== error_mark_node
)
9487 TREE_VEC_ELT (new_vec
, i
) =
9488 tsubst_template_parm (tuple
, args
, complain
);
9492 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9493 - TMPL_ARGS_DEPTH (args
)),
9494 new_vec
, NULL_TREE
);
9497 --processing_template_decl
;
9502 /* Return the result of substituting ARGS into one template parameter
9503 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9504 parameter and which TREE_PURPOSE is the default argument of the
9505 template parameter. */
9508 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9510 tree default_value
, parm_decl
;
9512 if (args
== NULL_TREE
9514 || t
== error_mark_node
)
9517 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9519 default_value
= TREE_PURPOSE (t
);
9520 parm_decl
= TREE_VALUE (t
);
9522 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9523 if (TREE_CODE (parm_decl
) == PARM_DECL
9524 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9525 parm_decl
= error_mark_node
;
9526 default_value
= tsubst_template_arg (default_value
, args
,
9527 complain
, NULL_TREE
);
9529 return build_tree_list (default_value
, parm_decl
);
9532 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9533 type T. If T is not an aggregate or enumeration type, it is
9534 handled as if by tsubst. IN_DECL is as for tsubst. If
9535 ENTERING_SCOPE is nonzero, T is the context for a template which
9536 we are presently tsubst'ing. Return the substituted value. */
9539 tsubst_aggr_type (tree t
,
9541 tsubst_flags_t complain
,
9548 switch (TREE_CODE (t
))
9551 if (TYPE_PTRMEMFUNC_P (t
))
9552 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9554 /* Else fall through. */
9557 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9562 int saved_unevaluated_operand
;
9563 int saved_inhibit_evaluation_warnings
;
9565 /* In "sizeof(X<I>)" we need to evaluate "I". */
9566 saved_unevaluated_operand
= cp_unevaluated_operand
;
9567 cp_unevaluated_operand
= 0;
9568 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9569 c_inhibit_evaluation_warnings
= 0;
9571 /* First, determine the context for the type we are looking
9573 context
= TYPE_CONTEXT (t
);
9576 context
= tsubst_aggr_type (context
, args
, complain
,
9577 in_decl
, /*entering_scope=*/1);
9578 /* If context is a nested class inside a class template,
9579 it may still need to be instantiated (c++/33959). */
9580 if (TYPE_P (context
))
9581 context
= complete_type (context
);
9584 /* Then, figure out what arguments are appropriate for the
9585 type we are trying to find. For example, given:
9587 template <class T> struct S;
9588 template <class T, class U> void f(T, U) { S<U> su; }
9590 and supposing that we are instantiating f<int, double>,
9591 then our ARGS will be {int, double}, but, when looking up
9592 S we only want {double}. */
9593 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9595 if (argvec
== error_mark_node
)
9596 r
= error_mark_node
;
9599 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9600 entering_scope
, complain
);
9601 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9604 cp_unevaluated_operand
= saved_unevaluated_operand
;
9605 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9610 /* This is not a template type, so there's nothing to do. */
9614 return tsubst (t
, args
, complain
, in_decl
);
9618 /* Substitute into the default argument ARG (a default argument for
9619 FN), which has the indicated TYPE. */
9622 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9624 tree saved_class_ptr
= NULL_TREE
;
9625 tree saved_class_ref
= NULL_TREE
;
9627 /* This can happen in invalid code. */
9628 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9631 /* This default argument came from a template. Instantiate the
9632 default argument here, not in tsubst. In the case of
9641 we must be careful to do name lookup in the scope of S<T>,
9642 rather than in the current class. */
9643 push_access_scope (fn
);
9644 /* The "this" pointer is not valid in a default argument. */
9647 saved_class_ptr
= current_class_ptr
;
9648 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9649 saved_class_ref
= current_class_ref
;
9650 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9653 push_deferring_access_checks(dk_no_deferred
);
9654 /* The default argument expression may cause implicitly defined
9655 member functions to be synthesized, which will result in garbage
9656 collection. We must treat this situation as if we were within
9657 the body of function so as to avoid collecting live data on the
9660 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9661 tf_warning_or_error
, NULL_TREE
,
9662 /*integral_constant_expression_p=*/false);
9664 pop_deferring_access_checks();
9666 /* Restore the "this" pointer. */
9669 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9670 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9673 /* Make sure the default argument is reasonable. */
9674 arg
= check_default_argument (type
, arg
);
9676 pop_access_scope (fn
);
9681 /* Substitute into all the default arguments for FN. */
9684 tsubst_default_arguments (tree fn
)
9689 tmpl_args
= DECL_TI_ARGS (fn
);
9691 /* If this function is not yet instantiated, we certainly don't need
9692 its default arguments. */
9693 if (uses_template_parms (tmpl_args
))
9695 /* Don't do this again for clones. */
9696 if (DECL_CLONED_FUNCTION_P (fn
))
9699 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9701 arg
= TREE_CHAIN (arg
))
9702 if (TREE_PURPOSE (arg
))
9703 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9705 TREE_PURPOSE (arg
));
9708 /* Substitute the ARGS into the T, which is a _DECL. Return the
9709 result of the substitution. Issue error and warning messages under
9710 control of COMPLAIN. */
9713 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9715 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9716 location_t saved_loc
;
9721 /* Set the filename and linenumber to improve error-reporting. */
9722 saved_loc
= input_location
;
9723 input_location
= DECL_SOURCE_LOCATION (t
);
9725 switch (TREE_CODE (t
))
9729 /* We can get here when processing a member function template,
9730 member class template, or template template parameter. */
9731 tree decl
= DECL_TEMPLATE_RESULT (t
);
9736 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9738 /* Template template parameter is treated here. */
9739 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9740 if (new_type
== error_mark_node
)
9741 RETURN (error_mark_node
);
9744 DECL_CHAIN (r
) = NULL_TREE
;
9745 TREE_TYPE (r
) = new_type
;
9746 DECL_TEMPLATE_RESULT (r
)
9747 = build_decl (DECL_SOURCE_LOCATION (decl
),
9748 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9749 DECL_TEMPLATE_PARMS (r
)
9750 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9752 TYPE_NAME (new_type
) = r
;
9756 /* We might already have an instance of this template.
9757 The ARGS are for the surrounding class type, so the
9758 full args contain the tsubst'd args for the context,
9759 plus the innermost args from the template decl. */
9760 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9761 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9762 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9763 /* Because this is a template, the arguments will still be
9764 dependent, even after substitution. If
9765 PROCESSING_TEMPLATE_DECL is not set, the dependency
9766 predicates will short-circuit. */
9767 ++processing_template_decl
;
9768 full_args
= tsubst_template_args (tmpl_args
, args
,
9770 --processing_template_decl
;
9771 if (full_args
== error_mark_node
)
9772 RETURN (error_mark_node
);
9774 /* If this is a default template template argument,
9775 tsubst might not have changed anything. */
9776 if (full_args
== tmpl_args
)
9779 hash
= hash_tmpl_and_args (t
, full_args
);
9780 spec
= retrieve_specialization (t
, full_args
, hash
);
9781 if (spec
!= NULL_TREE
)
9787 /* Make a new template decl. It will be similar to the
9788 original, but will record the current template arguments.
9789 We also create a new function declaration, which is just
9790 like the old one, but points to this new template, rather
9791 than the old one. */
9793 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
9794 DECL_CHAIN (r
) = NULL_TREE
;
9796 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
9798 if (TREE_CODE (decl
) == TYPE_DECL
)
9801 ++processing_template_decl
;
9802 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9803 --processing_template_decl
;
9804 if (new_type
== error_mark_node
)
9805 RETURN (error_mark_node
);
9807 TREE_TYPE (r
) = new_type
;
9808 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
9809 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
9810 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
9811 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
9816 ++processing_template_decl
;
9817 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
9818 --processing_template_decl
;
9819 if (new_decl
== error_mark_node
)
9820 RETURN (error_mark_node
);
9822 DECL_TEMPLATE_RESULT (r
) = new_decl
;
9823 DECL_TI_TEMPLATE (new_decl
) = r
;
9824 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
9825 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
9826 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
9829 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9830 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
9831 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
9833 /* The template parameters for this new template are all the
9834 template parameters for the old template, except the
9835 outermost level of parameters. */
9836 DECL_TEMPLATE_PARMS (r
)
9837 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9840 if (PRIMARY_TEMPLATE_P (t
))
9841 DECL_PRIMARY_TEMPLATE (r
) = r
;
9843 if (TREE_CODE (decl
) != TYPE_DECL
)
9844 /* Record this non-type partial instantiation. */
9845 register_specialization (r
, t
,
9846 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
9854 tree argvec
= NULL_TREE
;
9862 /* Nobody should be tsubst'ing into non-template functions. */
9863 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
9865 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
9870 /* If T is not dependent, just return it. We have to
9871 increment PROCESSING_TEMPLATE_DECL because
9872 value_dependent_expression_p assumes that nothing is
9873 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9874 ++processing_template_decl
;
9875 dependent_p
= value_dependent_expression_p (t
);
9876 --processing_template_decl
;
9880 /* Calculate the most general template of which R is a
9881 specialization, and the complete set of arguments used to
9883 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
9884 argvec
= tsubst_template_args (DECL_TI_ARGS
9885 (DECL_TEMPLATE_RESULT
9886 (DECL_TI_TEMPLATE (t
))),
9887 args
, complain
, in_decl
);
9888 if (argvec
== error_mark_node
)
9889 RETURN (error_mark_node
);
9891 /* Check to see if we already have this specialization. */
9892 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9893 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9901 /* We can see more levels of arguments than parameters if
9902 there was a specialization of a member template, like
9905 template <class T> struct S { template <class U> void f(); }
9906 template <> template <class U> void S<int>::f(U);
9908 Here, we'll be substituting into the specialization,
9909 because that's where we can find the code we actually
9910 want to generate, but we'll have enough arguments for
9911 the most general template.
9913 We also deal with the peculiar case:
9915 template <class T> struct S {
9916 template <class U> friend void f();
9918 template <class U> void f() {}
9920 template void f<double>();
9922 Here, the ARGS for the instantiation of will be {int,
9923 double}. But, we only need as many ARGS as there are
9924 levels of template parameters in CODE_PATTERN. We are
9925 careful not to get fooled into reducing the ARGS in
9928 template <class T> struct S { template <class U> void f(U); }
9929 template <class T> template <> void S<T>::f(int) {}
9931 which we can spot because the pattern will be a
9932 specialization in this case. */
9933 args_depth
= TMPL_ARGS_DEPTH (args
);
9935 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
9936 if (args_depth
> parms_depth
9937 && !DECL_TEMPLATE_SPECIALIZATION (t
))
9938 args
= get_innermost_template_args (args
, parms_depth
);
9942 /* This special case arises when we have something like this:
9944 template <class T> struct S {
9945 friend void f<int>(int, double);
9948 Here, the DECL_TI_TEMPLATE for the friend declaration
9949 will be an IDENTIFIER_NODE. We are being called from
9950 tsubst_friend_function, and we want only to create a
9951 new decl (R) with appropriate types so that we can call
9952 determine_specialization. */
9953 gen_tmpl
= NULL_TREE
;
9956 if (DECL_CLASS_SCOPE_P (t
))
9958 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
9962 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
9963 complain
, t
, /*entering_scope=*/1);
9968 ctx
= DECL_CONTEXT (t
);
9970 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9971 if (type
== error_mark_node
)
9972 RETURN (error_mark_node
);
9974 /* We do NOT check for matching decls pushed separately at this
9975 point, as they may not represent instantiations of this
9976 template, and in any case are considered separate under the
9979 DECL_USE_TEMPLATE (r
) = 0;
9980 TREE_TYPE (r
) = type
;
9981 /* Clear out the mangled name and RTL for the instantiation. */
9982 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
9983 SET_DECL_RTL (r
, NULL
);
9984 /* Leave DECL_INITIAL set on deleted instantiations. */
9985 if (!DECL_DELETED_FN (r
))
9986 DECL_INITIAL (r
) = NULL_TREE
;
9987 DECL_CONTEXT (r
) = ctx
;
9989 if (member
&& DECL_CONV_FN_P (r
))
9990 /* Type-conversion operator. Reconstruct the name, in
9991 case it's the name of one of the template's parameters. */
9992 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
9994 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
9996 DECL_RESULT (r
) = NULL_TREE
;
9998 TREE_STATIC (r
) = 0;
9999 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10000 DECL_EXTERNAL (r
) = 1;
10001 /* If this is an instantiation of a function with internal
10002 linkage, we already know what object file linkage will be
10003 assigned to the instantiation. */
10004 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10005 DECL_DEFER_OUTPUT (r
) = 0;
10006 DECL_CHAIN (r
) = NULL_TREE
;
10007 DECL_PENDING_INLINE_INFO (r
) = 0;
10008 DECL_PENDING_INLINE_P (r
) = 0;
10009 DECL_SAVED_TREE (r
) = NULL_TREE
;
10010 DECL_STRUCT_FUNCTION (r
) = NULL
;
10012 /* We'll re-clone as appropriate in instantiate_template. */
10013 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10015 /* If we aren't complaining now, return on error before we register
10016 the specialization so that we'll complain eventually. */
10017 if ((complain
& tf_error
) == 0
10018 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10019 && !grok_op_properties (r
, /*complain=*/false))
10020 RETURN (error_mark_node
);
10022 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10023 this in the special friend case mentioned above where
10024 GEN_TMPL is NULL. */
10027 DECL_TEMPLATE_INFO (r
)
10028 = build_template_info (gen_tmpl
, argvec
);
10029 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10030 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10032 /* We're not supposed to instantiate default arguments
10033 until they are called, for a template. But, for a
10036 template <class T> void f ()
10037 { extern void g(int i = T()); }
10039 we should do the substitution when the template is
10040 instantiated. We handle the member function case in
10041 instantiate_class_template since the default arguments
10042 might refer to other members of the class. */
10044 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10045 && !uses_template_parms (argvec
))
10046 tsubst_default_arguments (r
);
10049 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10051 /* Copy the list of befriending classes. */
10052 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10054 friends
= &TREE_CHAIN (*friends
))
10056 *friends
= copy_node (*friends
);
10057 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10062 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10064 maybe_retrofit_in_chrg (r
);
10065 if (DECL_CONSTRUCTOR_P (r
))
10066 grok_ctor_properties (ctx
, r
);
10067 /* If this is an instantiation of a member template, clone it.
10068 If it isn't, that'll be handled by
10069 clone_constructors_and_destructors. */
10070 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10071 clone_function_decl (r
, /*update_method_vec_p=*/0);
10073 else if ((complain
& tf_error
) != 0
10074 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10075 && !grok_op_properties (r
, /*complain=*/true))
10076 RETURN (error_mark_node
);
10078 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10079 SET_DECL_FRIEND_CONTEXT (r
,
10080 tsubst (DECL_FRIEND_CONTEXT (t
),
10081 args
, complain
, in_decl
));
10083 /* Possibly limit visibility based on template args. */
10084 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10085 if (DECL_VISIBILITY_SPECIFIED (t
))
10087 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10088 DECL_ATTRIBUTES (r
)
10089 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10091 determine_visibility (r
);
10092 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10093 && !processing_template_decl
)
10094 defaulted_late_check (r
);
10096 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10097 args
, complain
, in_decl
);
10103 tree type
= NULL_TREE
;
10105 tree expanded_types
= NULL_TREE
;
10106 tree prev_r
= NULL_TREE
;
10107 tree first_r
= NULL_TREE
;
10109 if (FUNCTION_PARAMETER_PACK_P (t
))
10111 /* If there is a local specialization that isn't a
10112 parameter pack, it means that we're doing a "simple"
10113 substitution from inside tsubst_pack_expansion. Just
10114 return the local specialization (which will be a single
10116 tree spec
= retrieve_local_specialization (t
);
10118 && TREE_CODE (spec
) == PARM_DECL
10119 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10122 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10123 the parameters in this function parameter pack. */
10124 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10125 complain
, in_decl
);
10126 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10128 len
= TREE_VEC_LENGTH (expanded_types
);
10130 /* Zero-length parameter packs are boring. Just substitute
10133 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10138 /* All we did was update the type. Make a note of that. */
10139 type
= expanded_types
;
10140 expanded_types
= NULL_TREE
;
10144 /* Loop through all of the parameter's we'll build. When T is
10145 a function parameter pack, LEN is the number of expanded
10146 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10148 for (i
= 0; i
< len
; ++i
)
10152 if (DECL_TEMPLATE_PARM_P (t
))
10153 SET_DECL_TEMPLATE_PARM_P (r
);
10155 if (expanded_types
)
10156 /* We're on the Ith parameter of the function parameter
10159 /* An argument of a function parameter pack is not a parameter
10161 FUNCTION_PARAMETER_PACK_P (r
) = false;
10163 /* Get the Ith type. */
10164 type
= TREE_VEC_ELT (expanded_types
, i
);
10167 /* Rename the parameter to include the index. */
10169 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10172 /* We're dealing with a normal parameter. */
10173 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10175 type
= type_decays_to (type
);
10176 TREE_TYPE (r
) = type
;
10177 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10179 if (DECL_INITIAL (r
))
10181 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10182 DECL_INITIAL (r
) = TREE_TYPE (r
);
10184 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10185 complain
, in_decl
);
10188 DECL_CONTEXT (r
) = NULL_TREE
;
10190 if (!DECL_TEMPLATE_PARM_P (r
))
10191 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10193 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10194 args
, complain
, in_decl
);
10196 /* Keep track of the first new parameter we
10197 generate. That's what will be returned to the
10202 /* Build a proper chain of parameters when substituting
10203 into a function parameter pack. */
10205 DECL_CHAIN (prev_r
) = r
;
10208 if (DECL_CHAIN (t
))
10209 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10210 complain
, DECL_CHAIN (t
));
10212 /* FIRST_R contains the start of the chain we've built. */
10222 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10223 if (type
== error_mark_node
)
10224 RETURN (error_mark_node
);
10225 TREE_TYPE (r
) = type
;
10226 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10228 /* DECL_INITIAL gives the number of bits in a bit-field. */
10230 = tsubst_expr (DECL_INITIAL (t
), args
,
10232 /*integral_constant_expression_p=*/true);
10233 /* We don't have to set DECL_CONTEXT here; it is set by
10234 finish_member_declaration. */
10235 DECL_CHAIN (r
) = NULL_TREE
;
10236 if (VOID_TYPE_P (type
))
10237 error ("instantiation of %q+D as type %qT", r
, type
);
10239 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10240 args
, complain
, in_decl
);
10245 /* We reach here only for member using decls. */
10246 if (DECL_DEPENDENT_P (t
))
10248 r
= do_class_using_decl
10249 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
10250 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
10252 r
= error_mark_node
;
10255 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10256 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10262 DECL_CHAIN (r
) = NULL_TREE
;
10269 tree argvec
= NULL_TREE
;
10270 tree gen_tmpl
= NULL_TREE
;
10272 tree tmpl
= NULL_TREE
;
10274 tree type
= NULL_TREE
;
10277 if (TREE_CODE (t
) == TYPE_DECL
10278 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10280 /* If this is the canonical decl, we don't have to
10281 mess with instantiations, and often we can't (for
10282 typename, template type parms and such). Note that
10283 TYPE_NAME is not correct for the above test if
10284 we've copied the type for a typedef. */
10285 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10286 if (type
== error_mark_node
)
10287 RETURN (error_mark_node
);
10288 r
= TYPE_NAME (type
);
10292 /* Check to see if we already have the specialization we
10295 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10297 /* T is a static data member or namespace-scope entity.
10298 We have to substitute into namespace-scope variables
10299 (even though such entities are never templates) because
10302 template <class T> void f() { extern T t; }
10304 where the entity referenced is not known until
10305 instantiation time. */
10307 ctx
= DECL_CONTEXT (t
);
10308 if (DECL_CLASS_SCOPE_P (t
))
10310 ctx
= tsubst_aggr_type (ctx
, args
,
10312 in_decl
, /*entering_scope=*/1);
10313 /* If CTX is unchanged, then T is in fact the
10314 specialization we want. That situation occurs when
10315 referencing a static data member within in its own
10316 class. We can use pointer equality, rather than
10317 same_type_p, because DECL_CONTEXT is always
10319 if (ctx
== DECL_CONTEXT (t
))
10325 tmpl
= DECL_TI_TEMPLATE (t
);
10326 gen_tmpl
= most_general_template (tmpl
);
10327 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10328 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10329 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10334 /* A local variable. */
10336 /* Subsequent calls to pushdecl will fill this in. */
10338 spec
= retrieve_local_specialization (t
);
10340 /* If we already have the specialization we need, there is
10341 nothing more to do. */
10348 /* Create a new node for the specialization we need. */
10350 if (type
== NULL_TREE
)
10352 if (is_typedef_decl (t
))
10353 type
= DECL_ORIGINAL_TYPE (t
);
10355 type
= TREE_TYPE (t
);
10356 if (TREE_CODE (t
) == VAR_DECL
&& VAR_HAD_UNKNOWN_BOUND (t
))
10357 type
= strip_array_domain (type
);
10358 type
= tsubst (type
, args
, complain
, in_decl
);
10360 if (TREE_CODE (r
) == VAR_DECL
)
10362 /* Even if the original location is out of scope, the
10363 newly substituted one is not. */
10364 DECL_DEAD_FOR_LOCAL (r
) = 0;
10365 DECL_INITIALIZED_P (r
) = 0;
10366 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10367 if (type
== error_mark_node
)
10368 RETURN (error_mark_node
);
10369 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10371 /* It may seem that this case cannot occur, since:
10376 declares a function, not a variable. However:
10379 template <typename T> void g() { T t; }
10380 template void g<f>();
10382 is an attempt to declare a variable with function
10384 error ("variable %qD has function type",
10385 /* R is not yet sufficiently initialized, so we
10386 just use its name. */
10388 RETURN (error_mark_node
);
10390 type
= complete_type (type
);
10391 /* Wait until cp_finish_decl to set this again, to handle
10392 circular dependency (template/instantiate6.C). */
10393 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10394 type
= check_var_type (DECL_NAME (r
), type
);
10396 if (DECL_HAS_VALUE_EXPR_P (t
))
10398 tree ve
= DECL_VALUE_EXPR (t
);
10399 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10400 /*constant_expression_p=*/false);
10401 if (REFERENCE_REF_P (ve
))
10403 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10404 ve
= TREE_OPERAND (ve
, 0);
10406 SET_DECL_VALUE_EXPR (r
, ve
);
10409 else if (DECL_SELF_REFERENCE_P (t
))
10410 SET_DECL_SELF_REFERENCE_P (r
);
10411 TREE_TYPE (r
) = type
;
10412 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10413 DECL_CONTEXT (r
) = ctx
;
10414 /* Clear out the mangled name and RTL for the instantiation. */
10415 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10416 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10417 SET_DECL_RTL (r
, NULL
);
10418 /* The initializer must not be expanded until it is required;
10419 see [temp.inst]. */
10420 DECL_INITIAL (r
) = NULL_TREE
;
10421 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10422 SET_DECL_RTL (r
, NULL
);
10423 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10424 if (TREE_CODE (r
) == VAR_DECL
)
10426 /* Possibly limit visibility based on template args. */
10427 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10428 if (DECL_VISIBILITY_SPECIFIED (t
))
10430 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10431 DECL_ATTRIBUTES (r
)
10432 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10434 determine_visibility (r
);
10439 /* A static data member declaration is always marked
10440 external when it is declared in-class, even if an
10441 initializer is present. We mimic the non-template
10442 processing here. */
10443 DECL_EXTERNAL (r
) = 1;
10445 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10446 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10447 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10449 else if (cp_unevaluated_operand
)
10451 /* We're substituting this var in a decltype outside of its
10452 scope, such as for a lambda return type. Don't add it to
10453 local_specializations, do perform auto deduction. */
10454 tree auto_node
= type_uses_auto (type
);
10458 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10459 /*constant_expression_p=*/false);
10460 init
= resolve_nondeduced_context (init
);
10461 TREE_TYPE (r
) = type
10462 = do_auto_deduction (type
, init
, auto_node
);
10466 register_local_specialization (r
, t
);
10468 DECL_CHAIN (r
) = NULL_TREE
;
10470 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10472 args
, complain
, in_decl
);
10474 /* Preserve a typedef that names a type. */
10475 if (is_typedef_decl (r
))
10477 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10478 set_underlying_type (r
);
10481 layout_decl (r
, 0);
10486 gcc_unreachable ();
10491 /* Restore the file and line information. */
10492 input_location
= saved_loc
;
10497 /* Substitute into the ARG_TYPES of a function type. */
10500 tsubst_arg_types (tree arg_types
,
10502 tsubst_flags_t complain
,
10505 tree remaining_arg_types
;
10506 tree type
= NULL_TREE
;
10508 tree expanded_args
= NULL_TREE
;
10511 if (!arg_types
|| arg_types
== void_list_node
)
10514 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10515 args
, complain
, in_decl
);
10516 if (remaining_arg_types
== error_mark_node
)
10517 return error_mark_node
;
10519 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10521 /* For a pack expansion, perform substitution on the
10522 entire expression. Later on, we'll handle the arguments
10524 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10525 args
, complain
, in_decl
);
10527 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10528 /* So that we'll spin through the parameters, one by one. */
10529 i
= TREE_VEC_LENGTH (expanded_args
);
10532 /* We only partially substituted into the parameter
10533 pack. Our type is TYPE_PACK_EXPANSION. */
10534 type
= expanded_args
;
10535 expanded_args
= NULL_TREE
;
10543 type
= TREE_VEC_ELT (expanded_args
, i
);
10545 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10547 if (type
== error_mark_node
)
10548 return error_mark_node
;
10549 if (VOID_TYPE_P (type
))
10551 if (complain
& tf_error
)
10553 error ("invalid parameter type %qT", type
);
10555 error ("in declaration %q+D", in_decl
);
10557 return error_mark_node
;
10560 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10561 top-level qualifiers as required. */
10562 type
= cv_unqualified (type_decays_to (type
));
10564 /* We do not substitute into default arguments here. The standard
10565 mandates that they be instantiated only when needed, which is
10566 done in build_over_call. */
10567 default_arg
= TREE_PURPOSE (arg_types
);
10569 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10571 /* We've instantiated a template before its default arguments
10572 have been parsed. This can happen for a nested template
10573 class, and is not an error unless we require the default
10574 argument in a call of this function. */
10575 remaining_arg_types
=
10576 tree_cons (default_arg
, type
, remaining_arg_types
);
10577 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
10578 remaining_arg_types
);
10581 remaining_arg_types
=
10582 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10585 return remaining_arg_types
;
10588 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10589 *not* handle the exception-specification for FNTYPE, because the
10590 initial substitution of explicitly provided template parameters
10591 during argument deduction forbids substitution into the
10592 exception-specification:
10596 All references in the function type of the function template to the
10597 corresponding template parameters are replaced by the specified tem-
10598 plate argument values. If a substitution in a template parameter or
10599 in the function type of the function template results in an invalid
10600 type, type deduction fails. [Note: The equivalent substitution in
10601 exception specifications is done only when the function is instanti-
10602 ated, at which point a program is ill-formed if the substitution
10603 results in an invalid type.] */
10606 tsubst_function_type (tree t
,
10608 tsubst_flags_t complain
,
10615 /* The TYPE_CONTEXT is not used for function/method types. */
10616 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10618 /* Substitute the return type. */
10619 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10620 if (return_type
== error_mark_node
)
10621 return error_mark_node
;
10622 /* The standard does not presently indicate that creation of a
10623 function type with an invalid return type is a deduction failure.
10624 However, that is clearly analogous to creating an array of "void"
10625 or a reference to a reference. This is core issue #486. */
10626 if (TREE_CODE (return_type
) == ARRAY_TYPE
10627 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10629 if (complain
& tf_error
)
10631 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10632 error ("function returning an array");
10634 error ("function returning a function");
10636 return error_mark_node
;
10639 /* Substitute the argument types. */
10640 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
10641 complain
, in_decl
);
10642 if (arg_types
== error_mark_node
)
10643 return error_mark_node
;
10645 /* Construct a new type node and return it. */
10646 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10648 fntype
= build_function_type (return_type
, arg_types
);
10649 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10653 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10654 if (! MAYBE_CLASS_TYPE_P (r
))
10658 Type deduction may fail for any of the following
10661 -- Attempting to create "pointer to member of T" when T
10662 is not a class type. */
10663 if (complain
& tf_error
)
10664 error ("creating pointer to member function of non-class type %qT",
10666 return error_mark_node
;
10669 fntype
= build_method_type_directly (r
, return_type
,
10670 TREE_CHAIN (arg_types
));
10672 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10677 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10678 ARGS into that specification, and return the substituted
10679 specification. If there is no specification, return NULL_TREE. */
10682 tsubst_exception_specification (tree fntype
,
10684 tsubst_flags_t complain
,
10691 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10692 new_specs
= NULL_TREE
;
10693 if (specs
&& TREE_PURPOSE (specs
))
10695 /* A noexcept-specifier. */
10696 tree expr
= TREE_PURPOSE (specs
);
10697 if (expr
== boolean_true_node
|| expr
== boolean_false_node
)
10701 /* Defer instantiation of noexcept-specifiers to avoid
10702 excessive instantiations (c++/49107). */
10703 new_specs
= make_node (DEFERRED_NOEXCEPT
);
10704 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
10706 /* We already partially instantiated this member template,
10707 so combine the new args with the old. */
10708 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
10709 = DEFERRED_NOEXCEPT_PATTERN (expr
);
10710 DEFERRED_NOEXCEPT_ARGS (new_specs
)
10711 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
10715 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
10716 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
10720 new_specs
= tsubst_copy_and_build
10721 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
10722 /*integral_constant_expression_p=*/true);
10723 new_specs
= build_noexcept_spec (new_specs
, complain
);
10727 if (! TREE_VALUE (specs
))
10734 tree expanded_specs
= NULL_TREE
;
10736 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
10738 /* Expand the pack expansion type. */
10739 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
10743 if (expanded_specs
== error_mark_node
)
10744 return error_mark_node
;
10745 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
10746 len
= TREE_VEC_LENGTH (expanded_specs
);
10749 /* We're substituting into a member template, so
10750 we got a TYPE_PACK_EXPANSION back. Add that
10751 expansion and move on. */
10752 gcc_assert (TREE_CODE (expanded_specs
)
10753 == TYPE_PACK_EXPANSION
);
10754 new_specs
= add_exception_specifier (new_specs
,
10757 specs
= TREE_CHAIN (specs
);
10762 for (i
= 0; i
< len
; ++i
)
10764 if (expanded_specs
)
10765 spec
= TREE_VEC_ELT (expanded_specs
, i
);
10767 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
10768 if (spec
== error_mark_node
)
10770 new_specs
= add_exception_specifier (new_specs
, spec
,
10774 specs
= TREE_CHAIN (specs
);
10780 /* Take the tree structure T and replace template parameters used
10781 therein with the argument vector ARGS. IN_DECL is an associated
10782 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10783 Issue error and warning messages under control of COMPLAIN. Note
10784 that we must be relatively non-tolerant of extensions here, in
10785 order to preserve conformance; if we allow substitutions that
10786 should not be allowed, we may allow argument deductions that should
10787 not succeed, and therefore report ambiguous overload situations
10788 where there are none. In theory, we could allow the substitution,
10789 but indicate that it should have failed, and allow our caller to
10790 make sure that the right thing happens, but we don't try to do this
10793 This function is used for dealing with types, decls and the like;
10794 for expressions, use tsubst_expr or tsubst_copy. */
10797 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10799 enum tree_code code
;
10802 if (t
== NULL_TREE
|| t
== error_mark_node
10803 || t
== integer_type_node
10804 || t
== void_type_node
10805 || t
== char_type_node
10806 || t
== unknown_type_node
10807 || TREE_CODE (t
) == NAMESPACE_DECL
10808 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
10812 return tsubst_decl (t
, args
, complain
);
10814 if (args
== NULL_TREE
)
10817 code
= TREE_CODE (t
);
10819 if (code
== IDENTIFIER_NODE
)
10820 type
= IDENTIFIER_TYPE_VALUE (t
);
10822 type
= TREE_TYPE (t
);
10824 gcc_assert (type
!= unknown_type_node
);
10826 /* Reuse typedefs. We need to do this to handle dependent attributes,
10827 such as attribute aligned. */
10829 && typedef_variant_p (t
))
10831 tree decl
= TYPE_NAME (t
);
10833 if (DECL_CLASS_SCOPE_P (decl
)
10834 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10835 && uses_template_parms (DECL_CONTEXT (decl
)))
10837 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10838 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
10839 r
= retrieve_specialization (tmpl
, gen_args
, 0);
10841 else if (DECL_FUNCTION_SCOPE_P (decl
)
10842 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10843 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
10844 r
= retrieve_local_specialization (decl
);
10846 /* The typedef is from a non-template context. */
10852 r
= cp_build_qualified_type_real
10853 (r
, cp_type_quals (t
) | cp_type_quals (r
),
10854 complain
| tf_ignore_bad_quals
);
10857 /* Else we must be instantiating the typedef, so fall through. */
10861 && code
!= TYPENAME_TYPE
10862 && code
!= TEMPLATE_TYPE_PARM
10863 && code
!= IDENTIFIER_NODE
10864 && code
!= FUNCTION_TYPE
10865 && code
!= METHOD_TYPE
)
10866 type
= tsubst (type
, args
, complain
, in_decl
);
10867 if (type
== error_mark_node
)
10868 return error_mark_node
;
10874 case ENUMERAL_TYPE
:
10875 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
10876 /*entering_scope=*/0);
10879 case IDENTIFIER_NODE
:
10890 if (t
== integer_type_node
)
10893 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
10894 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
10898 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
10900 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
10901 /*integral_constant_expression_p=*/false);
10903 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
10905 if (TREE_CODE (max
) == NOP_EXPR
10906 && TREE_SIDE_EFFECTS (omax
)
10907 && !TREE_TYPE (max
))
10908 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
10910 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
10911 with TREE_SIDE_EFFECTS that indicates this is not an integral
10912 constant expression. */
10913 if (processing_template_decl
10914 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
10916 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
10917 TREE_SIDE_EFFECTS (max
) = 1;
10920 return compute_array_index_type (NULL_TREE
, max
, complain
);
10923 case TEMPLATE_TYPE_PARM
:
10924 case TEMPLATE_TEMPLATE_PARM
:
10925 case BOUND_TEMPLATE_TEMPLATE_PARM
:
10926 case TEMPLATE_PARM_INDEX
:
10931 tree arg
= NULL_TREE
;
10935 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
10936 template_parm_level_and_index (t
, &level
, &idx
);
10938 levels
= TMPL_ARGS_DEPTH (args
);
10939 if (level
<= levels
)
10941 arg
= TMPL_ARG (args
, level
, idx
);
10943 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
10944 /* See through ARGUMENT_PACK_SELECT arguments. */
10945 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
10948 if (arg
== error_mark_node
)
10949 return error_mark_node
;
10950 else if (arg
!= NULL_TREE
)
10952 if (ARGUMENT_PACK_P (arg
))
10953 /* If ARG is an argument pack, we don't actually want to
10954 perform a substitution here, because substitutions
10955 for argument packs are only done
10956 element-by-element. We can get to this point when
10957 substituting the type of a non-type template
10958 parameter pack, when that type actually contains
10959 template parameter packs from an outer template, e.g.,
10961 template<typename... Types> struct A {
10962 template<Types... Values> struct B { };
10966 if (code
== TEMPLATE_TYPE_PARM
)
10969 gcc_assert (TYPE_P (arg
));
10971 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
10973 return cp_build_qualified_type_real
10974 (arg
, quals
, complain
| tf_ignore_bad_quals
);
10976 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
10978 /* We are processing a type constructed from a
10979 template template parameter. */
10980 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
10981 args
, complain
, in_decl
);
10982 if (argvec
== error_mark_node
)
10983 return error_mark_node
;
10985 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
10986 are resolving nested-types in the signature of a
10987 member function templates. Otherwise ARG is a
10988 TEMPLATE_DECL and is the real template to be
10990 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
10991 arg
= TYPE_NAME (arg
);
10993 r
= lookup_template_class (arg
,
10995 DECL_CONTEXT (arg
),
10996 /*entering_scope=*/0,
10998 return cp_build_qualified_type_real
10999 (r
, cp_type_quals (t
), complain
);
11002 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11003 return convert_from_reference (unshare_expr (arg
));
11007 /* This can happen during the attempted tsubst'ing in
11008 unify. This means that we don't yet have any information
11009 about the template parameter in question. */
11012 /* If we get here, we must have been looking at a parm for a
11013 more deeply nested template. Make a new version of this
11014 template parameter, but with a lower level. */
11017 case TEMPLATE_TYPE_PARM
:
11018 case TEMPLATE_TEMPLATE_PARM
:
11019 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11020 if (cp_type_quals (t
))
11022 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11023 r
= cp_build_qualified_type_real
11024 (r
, cp_type_quals (t
),
11025 complain
| (code
== TEMPLATE_TYPE_PARM
11026 ? tf_ignore_bad_quals
: 0));
11031 TEMPLATE_TYPE_PARM_INDEX (r
)
11032 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11033 r
, levels
, args
, complain
);
11034 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11035 TYPE_MAIN_VARIANT (r
) = r
;
11036 TYPE_POINTER_TO (r
) = NULL_TREE
;
11037 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11039 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11040 /* We have reduced the level of the template
11041 template parameter, but not the levels of its
11042 template parameters, so canonical_type_parameter
11043 will not be able to find the canonical template
11044 template parameter for this level. Thus, we
11045 require structural equality checking to compare
11046 TEMPLATE_TEMPLATE_PARMs. */
11047 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11048 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11049 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11051 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11053 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11055 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11056 complain
, in_decl
);
11057 if (argvec
== error_mark_node
)
11058 return error_mark_node
;
11060 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11061 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11066 case TEMPLATE_PARM_INDEX
:
11067 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11071 gcc_unreachable ();
11079 tree purpose
, value
, chain
;
11081 if (t
== void_list_node
)
11084 purpose
= TREE_PURPOSE (t
);
11087 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11088 if (purpose
== error_mark_node
)
11089 return error_mark_node
;
11091 value
= TREE_VALUE (t
);
11094 value
= tsubst (value
, args
, complain
, in_decl
);
11095 if (value
== error_mark_node
)
11096 return error_mark_node
;
11098 chain
= TREE_CHAIN (t
);
11099 if (chain
&& chain
!= void_type_node
)
11101 chain
= tsubst (chain
, args
, complain
, in_decl
);
11102 if (chain
== error_mark_node
)
11103 return error_mark_node
;
11105 if (purpose
== TREE_PURPOSE (t
)
11106 && value
== TREE_VALUE (t
)
11107 && chain
== TREE_CHAIN (t
))
11109 return hash_tree_cons (purpose
, value
, chain
);
11113 /* We should never be tsubsting a binfo. */
11114 gcc_unreachable ();
11117 /* A vector of template arguments. */
11118 gcc_assert (!type
);
11119 return tsubst_template_args (t
, args
, complain
, in_decl
);
11122 case REFERENCE_TYPE
:
11124 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11129 Type deduction may fail for any of the following
11132 -- Attempting to create a pointer to reference type.
11133 -- Attempting to create a reference to a reference type or
11134 a reference to void.
11136 Core issue 106 says that creating a reference to a reference
11137 during instantiation is no longer a cause for failure. We
11138 only enforce this check in strict C++98 mode. */
11139 if ((TREE_CODE (type
) == REFERENCE_TYPE
11140 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11141 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11143 static location_t last_loc
;
11145 /* We keep track of the last time we issued this error
11146 message to avoid spewing a ton of messages during a
11147 single bad template instantiation. */
11148 if (complain
& tf_error
11149 && last_loc
!= input_location
)
11151 if (TREE_CODE (type
) == VOID_TYPE
)
11152 error ("forming reference to void");
11153 else if (code
== POINTER_TYPE
)
11154 error ("forming pointer to reference type %qT", type
);
11156 error ("forming reference to reference type %qT", type
);
11157 last_loc
= input_location
;
11160 return error_mark_node
;
11162 else if (code
== POINTER_TYPE
)
11164 r
= build_pointer_type (type
);
11165 if (TREE_CODE (type
) == METHOD_TYPE
)
11166 r
= build_ptrmemfunc_type (r
);
11168 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11169 /* In C++0x, during template argument substitution, when there is an
11170 attempt to create a reference to a reference type, reference
11171 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11173 "If a template-argument for a template-parameter T names a type
11174 that is a reference to a type A, an attempt to create the type
11175 'lvalue reference to cv T' creates the type 'lvalue reference to
11176 A,' while an attempt to create the type type rvalue reference to
11177 cv T' creates the type T"
11179 r
= cp_build_reference_type
11181 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11183 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11184 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11186 if (r
!= error_mark_node
)
11187 /* Will this ever be needed for TYPE_..._TO values? */
11194 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11195 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11199 Type deduction may fail for any of the following
11202 -- Attempting to create "pointer to member of T" when T
11203 is not a class type. */
11204 if (complain
& tf_error
)
11205 error ("creating pointer to member of non-class type %qT", r
);
11206 return error_mark_node
;
11208 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11210 if (complain
& tf_error
)
11211 error ("creating pointer to member reference type %qT", type
);
11212 return error_mark_node
;
11214 if (TREE_CODE (type
) == VOID_TYPE
)
11216 if (complain
& tf_error
)
11217 error ("creating pointer to member of type void");
11218 return error_mark_node
;
11220 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11221 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11223 /* The type of the implicit object parameter gets its
11224 cv-qualifiers from the FUNCTION_TYPE. */
11226 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11227 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11228 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11232 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11236 case FUNCTION_TYPE
:
11241 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11242 if (fntype
== error_mark_node
)
11243 return error_mark_node
;
11245 /* Substitute the exception specification. */
11246 specs
= tsubst_exception_specification (t
, args
, complain
,
11247 in_decl
, /*defer_ok*/true);
11248 if (specs
== error_mark_node
)
11249 return error_mark_node
;
11251 fntype
= build_exception_variant (fntype
, specs
);
11256 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11257 if (domain
== error_mark_node
)
11258 return error_mark_node
;
11260 /* As an optimization, we avoid regenerating the array type if
11261 it will obviously be the same as T. */
11262 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11265 /* These checks should match the ones in grokdeclarator.
11269 The deduction may fail for any of the following reasons:
11271 -- Attempting to create an array with an element type that
11272 is void, a function type, or a reference type, or [DR337]
11273 an abstract class type. */
11274 if (TREE_CODE (type
) == VOID_TYPE
11275 || TREE_CODE (type
) == FUNCTION_TYPE
11276 || TREE_CODE (type
) == REFERENCE_TYPE
)
11278 if (complain
& tf_error
)
11279 error ("creating array of %qT", type
);
11280 return error_mark_node
;
11282 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
11284 if (complain
& tf_error
)
11285 error ("creating array of %qT, which is an abstract class type",
11287 return error_mark_node
;
11290 r
= build_cplus_array_type (type
, domain
);
11292 if (TYPE_USER_ALIGN (t
))
11294 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11295 TYPE_USER_ALIGN (r
) = 1;
11301 case TYPENAME_TYPE
:
11303 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11304 in_decl
, /*entering_scope=*/1);
11305 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11306 complain
, in_decl
);
11308 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11309 return error_mark_node
;
11311 if (!MAYBE_CLASS_TYPE_P (ctx
))
11313 if (complain
& tf_error
)
11314 error ("%qT is not a class, struct, or union type", ctx
);
11315 return error_mark_node
;
11317 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11319 /* Normally, make_typename_type does not require that the CTX
11320 have complete type in order to allow things like:
11322 template <class T> struct S { typename S<T>::X Y; };
11324 But, such constructs have already been resolved by this
11325 point, so here CTX really should have complete type, unless
11326 it's a partial instantiation. */
11327 ctx
= complete_type (ctx
);
11328 if (!COMPLETE_TYPE_P (ctx
))
11330 if (complain
& tf_error
)
11331 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11332 return error_mark_node
;
11336 f
= make_typename_type (ctx
, f
, typename_type
,
11337 (complain
& tf_error
) | tf_keep_type_decl
);
11338 if (f
== error_mark_node
)
11340 if (TREE_CODE (f
) == TYPE_DECL
)
11342 complain
|= tf_ignore_bad_quals
;
11346 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11348 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11350 if (complain
& tf_error
)
11351 error ("%qT resolves to %qT, which is not an enumeration type",
11354 return error_mark_node
;
11356 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11358 if (complain
& tf_error
)
11359 error ("%qT resolves to %qT, which is is not a class type",
11362 return error_mark_node
;
11366 return cp_build_qualified_type_real
11367 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11370 case UNBOUND_CLASS_TEMPLATE
:
11372 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11373 in_decl
, /*entering_scope=*/1);
11374 tree name
= TYPE_IDENTIFIER (t
);
11375 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11377 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11378 return error_mark_node
;
11381 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11382 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11389 ++cp_unevaluated_operand
;
11390 ++c_inhibit_evaluation_warnings
;
11392 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11394 /*integral_constant_expression_p=*/false);
11396 --cp_unevaluated_operand
;
11397 --c_inhibit_evaluation_warnings
;
11399 type
= finish_typeof (type
);
11400 return cp_build_qualified_type_real (type
,
11402 | cp_type_quals (type
),
11406 case DECLTYPE_TYPE
:
11410 ++cp_unevaluated_operand
;
11411 ++c_inhibit_evaluation_warnings
;
11413 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11415 /*integral_constant_expression_p=*/false);
11417 --cp_unevaluated_operand
;
11418 --c_inhibit_evaluation_warnings
;
11420 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11421 type
= lambda_capture_field_type (type
);
11422 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11423 type
= lambda_proxy_type (type
);
11425 type
= finish_decltype_type
11426 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11427 return cp_build_qualified_type_real (type
,
11429 | cp_type_quals (type
),
11433 case UNDERLYING_TYPE
:
11435 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11436 complain
, in_decl
);
11437 return finish_underlying_type (type
);
11440 case TYPE_ARGUMENT_PACK
:
11441 case NONTYPE_ARGUMENT_PACK
:
11443 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11445 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11449 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11451 /* For template nontype argument packs, also substitute into
11453 if (code
== NONTYPE_ARGUMENT_PACK
)
11454 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11472 /* We should use one of the expression tsubsts for these codes. */
11473 gcc_unreachable ();
11476 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11477 return error_mark_node
;
11481 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11482 type of the expression on the left-hand side of the "." or "->"
11486 tsubst_baselink (tree baselink
, tree object_type
,
11487 tree args
, tsubst_flags_t complain
, tree in_decl
)
11490 tree qualifying_scope
;
11493 tree template_args
= 0;
11494 bool template_id_p
= false;
11496 /* A baselink indicates a function from a base class. Both the
11497 BASELINK_ACCESS_BINFO and the base class referenced may
11498 indicate bases of the template class, rather than the
11499 instantiated class. In addition, lookups that were not
11500 ambiguous before may be ambiguous now. Therefore, we perform
11501 the lookup again. */
11502 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11503 qualifying_scope
= tsubst (qualifying_scope
, args
,
11504 complain
, in_decl
);
11505 fns
= BASELINK_FUNCTIONS (baselink
);
11506 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11507 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11509 template_id_p
= true;
11510 template_args
= TREE_OPERAND (fns
, 1);
11511 fns
= TREE_OPERAND (fns
, 0);
11513 template_args
= tsubst_template_args (template_args
, args
,
11514 complain
, in_decl
);
11516 name
= DECL_NAME (get_first_fn (fns
));
11517 if (IDENTIFIER_TYPENAME_P (name
))
11518 name
= mangle_conv_op_name_for_type (optype
);
11519 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11521 return error_mark_node
;
11523 /* If lookup found a single function, mark it as used at this
11524 point. (If it lookup found multiple functions the one selected
11525 later by overload resolution will be marked as used at that
11527 if (BASELINK_P (baselink
))
11528 fns
= BASELINK_FUNCTIONS (baselink
);
11529 if (!template_id_p
&& !really_overloaded_fn (fns
))
11530 mark_used (OVL_CURRENT (fns
));
11532 /* Add back the template arguments, if present. */
11533 if (BASELINK_P (baselink
) && template_id_p
)
11534 BASELINK_FUNCTIONS (baselink
)
11535 = build_nt (TEMPLATE_ID_EXPR
,
11536 BASELINK_FUNCTIONS (baselink
),
11538 /* Update the conversion operator type. */
11539 BASELINK_OPTYPE (baselink
) = optype
;
11542 object_type
= current_class_type
;
11543 return adjust_result_of_qualified_name_lookup (baselink
,
11548 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11549 true if the qualified-id will be a postfix-expression in-and-of
11550 itself; false if more of the postfix-expression follows the
11551 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11555 tsubst_qualified_id (tree qualified_id
, tree args
,
11556 tsubst_flags_t complain
, tree in_decl
,
11557 bool done
, bool address_p
)
11563 tree template_args
;
11565 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11567 /* Figure out what name to look up. */
11568 name
= TREE_OPERAND (qualified_id
, 1);
11569 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11571 is_template
= true;
11572 template_args
= TREE_OPERAND (name
, 1);
11574 template_args
= tsubst_template_args (template_args
, args
,
11575 complain
, in_decl
);
11576 name
= TREE_OPERAND (name
, 0);
11580 is_template
= false;
11581 template_args
= NULL_TREE
;
11584 /* Substitute into the qualifying scope. When there are no ARGS, we
11585 are just trying to simplify a non-dependent expression. In that
11586 case the qualifying scope may be dependent, and, in any case,
11587 substituting will not help. */
11588 scope
= TREE_OPERAND (qualified_id
, 0);
11591 scope
= tsubst (scope
, args
, complain
, in_decl
);
11592 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11597 if (dependent_scope_p (scope
))
11600 expr
= build_min_nt (TEMPLATE_ID_EXPR
, expr
, template_args
);
11601 return build_qualified_name (NULL_TREE
, scope
, expr
,
11602 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11605 if (!BASELINK_P (name
) && !DECL_P (expr
))
11607 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11609 /* A BIT_NOT_EXPR is used to represent a destructor. */
11610 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11612 error ("qualifying type %qT does not match destructor name ~%qT",
11613 scope
, TREE_OPERAND (expr
, 0));
11614 expr
= error_mark_node
;
11617 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11618 /*is_type_p=*/0, false);
11621 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11622 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11623 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11625 if (complain
& tf_error
)
11627 error ("dependent-name %qE is parsed as a non-type, but "
11628 "instantiation yields a type", qualified_id
);
11629 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11631 return error_mark_node
;
11637 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11639 /* Remember that there was a reference to this entity. */
11643 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
11645 if (complain
& tf_error
)
11646 qualified_name_lookup_error (scope
,
11647 TREE_OPERAND (qualified_id
, 1),
11648 expr
, input_location
);
11649 return error_mark_node
;
11653 expr
= lookup_template_function (expr
, template_args
);
11655 if (expr
== error_mark_node
&& complain
& tf_error
)
11656 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
11657 expr
, input_location
);
11658 else if (TYPE_P (scope
))
11660 expr
= (adjust_result_of_qualified_name_lookup
11661 (expr
, scope
, current_class_type
));
11662 expr
= (finish_qualified_id_expr
11663 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
11664 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
11665 /*template_arg_p=*/false));
11668 /* Expressions do not generally have reference type. */
11669 if (TREE_CODE (expr
) != SCOPE_REF
11670 /* However, if we're about to form a pointer-to-member, we just
11671 want the referenced member referenced. */
11672 && TREE_CODE (expr
) != OFFSET_REF
)
11673 expr
= convert_from_reference (expr
);
11678 /* Like tsubst, but deals with expressions. This function just replaces
11679 template parms; to finish processing the resultant expression, use
11683 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11685 enum tree_code code
;
11688 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
11691 code
= TREE_CODE (t
);
11696 r
= retrieve_local_specialization (t
);
11701 /* This can happen for a parameter name used later in a function
11702 declaration (such as in a late-specified return type). Just
11703 make a dummy decl, since it's only used for its type. */
11704 gcc_assert (cp_unevaluated_operand
!= 0);
11705 /* We copy T because want to tsubst the PARM_DECL only,
11706 not the following PARM_DECLs that are chained to T. */
11708 r
= tsubst_decl (c
, args
, complain
);
11709 /* Give it the template pattern as its context; its true context
11710 hasn't been instantiated yet and this is good enough for
11712 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
11715 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
11716 r
= ARGUMENT_PACK_SELECT_ARG (r
);
11725 if (DECL_TEMPLATE_PARM_P (t
))
11726 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
11727 /* There is no need to substitute into namespace-scope
11729 if (DECL_NAMESPACE_SCOPE_P (t
))
11731 /* If ARGS is NULL, then T is known to be non-dependent. */
11732 if (args
== NULL_TREE
)
11733 return integral_constant_value (t
);
11735 /* Unfortunately, we cannot just call lookup_name here.
11738 template <int I> int f() {
11740 struct S { void g() { E e = a; } };
11743 When we instantiate f<7>::S::g(), say, lookup_name is not
11744 clever enough to find f<7>::a. */
11746 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
11747 /*entering_scope=*/0);
11749 for (v
= TYPE_VALUES (enum_type
);
11751 v
= TREE_CHAIN (v
))
11752 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
11753 return TREE_VALUE (v
);
11755 /* We didn't find the name. That should never happen; if
11756 name-lookup found it during preliminary parsing, we
11757 should find it again here during instantiation. */
11758 gcc_unreachable ();
11763 if (DECL_CONTEXT (t
))
11767 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11768 /*entering_scope=*/1);
11769 if (ctx
!= DECL_CONTEXT (t
))
11771 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
11774 if (complain
& tf_error
)
11775 error ("using invalid field %qD", t
);
11776 return error_mark_node
;
11785 case FUNCTION_DECL
:
11786 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
11787 || local_variable_p (t
))
11788 t
= tsubst (t
, args
, complain
, in_decl
);
11793 /* An OVERLOAD will always be a non-dependent overload set; an
11794 overload set from function scope will just be represented with an
11795 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11796 gcc_assert (!uses_template_parms (t
));
11800 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
11802 case TEMPLATE_DECL
:
11803 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11804 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
11805 args
, complain
, in_decl
);
11806 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
11807 return tsubst (t
, args
, complain
, in_decl
);
11808 else if (DECL_CLASS_SCOPE_P (t
)
11809 && uses_template_parms (DECL_CONTEXT (t
)))
11811 /* Template template argument like the following example need
11814 template <template <class> class TT> struct C {};
11815 template <class T> struct D {
11816 template <class U> struct E {};
11821 We are processing the template argument `E' in #1 for
11822 the template instantiation #2. Originally, `E' is a
11823 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
11824 have to substitute this with one having context `D<int>'. */
11826 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
11827 return lookup_field (context
, DECL_NAME(t
), 0, false);
11830 /* Ordinary template template argument. */
11834 case REINTERPRET_CAST_EXPR
:
11835 case CONST_CAST_EXPR
:
11836 case STATIC_CAST_EXPR
:
11837 case DYNAMIC_CAST_EXPR
:
11840 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11841 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11844 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
11850 ++cp_unevaluated_operand
;
11851 ++c_inhibit_evaluation_warnings
;
11852 /* We only want to compute the number of arguments. */
11853 expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
11854 complain
, in_decl
);
11855 --cp_unevaluated_operand
;
11856 --c_inhibit_evaluation_warnings
;
11858 if (TREE_CODE (expanded
) == TREE_VEC
)
11859 len
= TREE_VEC_LENGTH (expanded
);
11861 if (expanded
== error_mark_node
)
11862 return error_mark_node
;
11863 else if (PACK_EXPANSION_P (expanded
)
11864 || (TREE_CODE (expanded
) == TREE_VEC
11866 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
11868 if (TREE_CODE (expanded
) == TREE_VEC
)
11869 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
11871 if (TYPE_P (expanded
))
11872 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
11873 complain
& tf_error
);
11875 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
11876 complain
& tf_error
);
11879 return build_int_cst (size_type_node
, len
);
11885 case TRUTH_NOT_EXPR
:
11888 case UNARY_PLUS_EXPR
: /* Unary + */
11890 case AT_ENCODE_EXPR
:
11894 case REALPART_EXPR
:
11895 case IMAGPART_EXPR
:
11897 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
11898 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
11900 case COMPONENT_REF
:
11905 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
11906 name
= TREE_OPERAND (t
, 1);
11907 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
11909 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11910 complain
, in_decl
);
11911 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11913 else if (TREE_CODE (name
) == SCOPE_REF
11914 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
11916 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11917 complain
, in_decl
);
11918 name
= TREE_OPERAND (name
, 1);
11919 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
11920 complain
, in_decl
);
11921 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
11922 name
= build_qualified_name (/*type=*/NULL_TREE
,
11924 /*template_p=*/false);
11926 else if (TREE_CODE (name
) == BASELINK
)
11927 name
= tsubst_baselink (name
,
11928 non_reference (TREE_TYPE (object
)),
11932 name
= tsubst_copy (name
, args
, complain
, in_decl
);
11933 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
11939 case TRUNC_DIV_EXPR
:
11940 case CEIL_DIV_EXPR
:
11941 case FLOOR_DIV_EXPR
:
11942 case ROUND_DIV_EXPR
:
11943 case EXACT_DIV_EXPR
:
11947 case TRUNC_MOD_EXPR
:
11948 case FLOOR_MOD_EXPR
:
11949 case TRUTH_ANDIF_EXPR
:
11950 case TRUTH_ORIF_EXPR
:
11951 case TRUTH_AND_EXPR
:
11952 case TRUTH_OR_EXPR
:
11965 case COMPOUND_EXPR
:
11968 case PREDECREMENT_EXPR
:
11969 case PREINCREMENT_EXPR
:
11970 case POSTDECREMENT_EXPR
:
11971 case POSTINCREMENT_EXPR
:
11973 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11974 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
11977 return build_qualified_name (/*type=*/NULL_TREE
,
11978 tsubst_copy (TREE_OPERAND (t
, 0),
11979 args
, complain
, in_decl
),
11980 tsubst_copy (TREE_OPERAND (t
, 1),
11981 args
, complain
, in_decl
),
11982 QUALIFIED_NAME_IS_TEMPLATE (t
));
11987 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
11988 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
11989 NULL_TREE
, NULL_TREE
);
11993 int n
= VL_EXP_OPERAND_LENGTH (t
);
11994 tree result
= build_vl_exp (CALL_EXPR
, n
);
11996 for (i
= 0; i
< n
; i
++)
11997 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
11998 complain
, in_decl
);
12004 case PSEUDO_DTOR_EXPR
:
12007 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12008 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12009 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12010 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12017 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12018 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12019 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12020 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12027 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12028 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12029 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12030 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12034 case TEMPLATE_ID_EXPR
:
12036 /* Substituted template arguments */
12037 tree fn
= TREE_OPERAND (t
, 0);
12038 tree targs
= TREE_OPERAND (t
, 1);
12040 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12042 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12044 return lookup_template_function (fn
, targs
);
12049 tree purpose
, value
, chain
;
12051 if (t
== void_list_node
)
12054 purpose
= TREE_PURPOSE (t
);
12056 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12057 value
= TREE_VALUE (t
);
12059 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12060 chain
= TREE_CHAIN (t
);
12061 if (chain
&& chain
!= void_type_node
)
12062 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12063 if (purpose
== TREE_PURPOSE (t
)
12064 && value
== TREE_VALUE (t
)
12065 && chain
== TREE_CHAIN (t
))
12067 return tree_cons (purpose
, value
, chain
);
12072 case ENUMERAL_TYPE
:
12074 case TEMPLATE_TYPE_PARM
:
12075 case TEMPLATE_TEMPLATE_PARM
:
12076 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12077 case TEMPLATE_PARM_INDEX
:
12079 case REFERENCE_TYPE
:
12081 case FUNCTION_TYPE
:
12084 case TYPENAME_TYPE
:
12085 case UNBOUND_CLASS_TEMPLATE
:
12087 case DECLTYPE_TYPE
:
12089 return tsubst (t
, args
, complain
, in_decl
);
12091 case IDENTIFIER_NODE
:
12092 if (IDENTIFIER_TYPENAME_P (t
))
12094 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12095 return mangle_conv_op_name_for_type (new_type
);
12101 /* This is handled by tsubst_copy_and_build. */
12102 gcc_unreachable ();
12105 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12107 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12109 case CLEANUP_POINT_EXPR
:
12110 /* We shouldn't have built any of these during initial template
12111 generation. Instead, they should be built during instantiation
12112 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12113 gcc_unreachable ();
12117 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12118 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12119 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12120 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12121 mark_used (TREE_OPERAND (r
, 1));
12124 case EXPR_PACK_EXPANSION
:
12125 error ("invalid use of pack expansion expression");
12126 return error_mark_node
;
12128 case NONTYPE_ARGUMENT_PACK
:
12129 error ("use %<...%> to expand argument pack");
12130 return error_mark_node
;
12137 /* Instantiate any typedefs in the type. */
12138 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12139 r
= fold_convert (type
, t
);
12140 gcc_assert (TREE_CODE (r
) == code
);
12145 /* These can sometimes show up in a partial instantiation, but never
12146 involve template parms. */
12147 gcc_assert (!uses_template_parms (t
));
12151 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12152 gcc_checking_assert (false);
12157 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12160 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12163 tree new_clauses
= NULL
, nc
, oc
;
12165 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12167 nc
= copy_node (oc
);
12168 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12171 switch (OMP_CLAUSE_CODE (nc
))
12173 case OMP_CLAUSE_LASTPRIVATE
:
12174 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12176 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12177 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12178 in_decl
, /*integral_constant_expression_p=*/false);
12179 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12180 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12183 case OMP_CLAUSE_PRIVATE
:
12184 case OMP_CLAUSE_SHARED
:
12185 case OMP_CLAUSE_FIRSTPRIVATE
:
12186 case OMP_CLAUSE_REDUCTION
:
12187 case OMP_CLAUSE_COPYIN
:
12188 case OMP_CLAUSE_COPYPRIVATE
:
12189 case OMP_CLAUSE_IF
:
12190 case OMP_CLAUSE_NUM_THREADS
:
12191 case OMP_CLAUSE_SCHEDULE
:
12192 case OMP_CLAUSE_COLLAPSE
:
12193 case OMP_CLAUSE_FINAL
:
12194 OMP_CLAUSE_OPERAND (nc
, 0)
12195 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12196 in_decl
, /*integral_constant_expression_p=*/false);
12198 case OMP_CLAUSE_NOWAIT
:
12199 case OMP_CLAUSE_ORDERED
:
12200 case OMP_CLAUSE_DEFAULT
:
12201 case OMP_CLAUSE_UNTIED
:
12202 case OMP_CLAUSE_MERGEABLE
:
12205 gcc_unreachable ();
12209 return finish_omp_clauses (nreverse (new_clauses
));
12212 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12215 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12218 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12220 tree purpose
, value
, chain
;
12225 if (TREE_CODE (t
) != TREE_LIST
)
12226 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12227 /*function_p=*/false,
12228 /*integral_constant_expression_p=*/false);
12230 if (t
== void_list_node
)
12233 purpose
= TREE_PURPOSE (t
);
12235 purpose
= RECUR (purpose
);
12236 value
= TREE_VALUE (t
);
12237 if (value
&& TREE_CODE (value
) != LABEL_DECL
)
12238 value
= RECUR (value
);
12239 chain
= TREE_CHAIN (t
);
12240 if (chain
&& chain
!= void_type_node
)
12241 chain
= RECUR (chain
);
12242 return tree_cons (purpose
, value
, chain
);
12246 /* Substitute one OMP_FOR iterator. */
12249 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12250 tree condv
, tree incrv
, tree
*clauses
,
12251 tree args
, tsubst_flags_t complain
, tree in_decl
,
12252 bool integral_constant_expression_p
)
12254 #define RECUR(NODE) \
12255 tsubst_expr ((NODE), args, complain, in_decl, \
12256 integral_constant_expression_p)
12257 tree decl
, init
, cond
, incr
, auto_node
;
12259 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12260 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12261 decl
= RECUR (TREE_OPERAND (init
, 0));
12262 init
= TREE_OPERAND (init
, 1);
12263 auto_node
= type_uses_auto (TREE_TYPE (decl
));
12264 if (auto_node
&& init
)
12266 tree init_expr
= init
;
12267 if (TREE_CODE (init_expr
) == DECL_EXPR
)
12268 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
12269 init_expr
= RECUR (init_expr
);
12271 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
12273 gcc_assert (!type_dependent_expression_p (decl
));
12275 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12277 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12278 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12279 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12280 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12281 RECUR (TREE_OPERAND (incr
, 1)),
12284 incr
= RECUR (incr
);
12285 TREE_VEC_ELT (declv
, i
) = decl
;
12286 TREE_VEC_ELT (initv
, i
) = init
;
12287 TREE_VEC_ELT (condv
, i
) = cond
;
12288 TREE_VEC_ELT (incrv
, i
) = incr
;
12292 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
12295 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12297 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12298 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12299 && OMP_CLAUSE_DECL (c
) == decl
)
12301 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12302 && OMP_CLAUSE_DECL (c
) == decl
)
12303 error ("iteration variable %qD should not be firstprivate", decl
);
12304 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12305 && OMP_CLAUSE_DECL (c
) == decl
)
12306 error ("iteration variable %qD should not be reduction", decl
);
12310 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12311 OMP_CLAUSE_DECL (c
) = decl
;
12312 c
= finish_omp_clauses (c
);
12315 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12320 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12321 if (COMPARISON_CLASS_P (cond
))
12322 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12323 RECUR (TREE_OPERAND (cond
, 0)),
12324 RECUR (TREE_OPERAND (cond
, 1)));
12326 cond
= RECUR (cond
);
12327 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12328 switch (TREE_CODE (incr
))
12330 case PREINCREMENT_EXPR
:
12331 case PREDECREMENT_EXPR
:
12332 case POSTINCREMENT_EXPR
:
12333 case POSTDECREMENT_EXPR
:
12334 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12335 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12338 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12339 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12341 tree rhs
= TREE_OPERAND (incr
, 1);
12342 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12343 RECUR (TREE_OPERAND (incr
, 0)),
12344 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12345 RECUR (TREE_OPERAND (rhs
, 0)),
12346 RECUR (TREE_OPERAND (rhs
, 1))));
12349 incr
= RECUR (incr
);
12352 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12353 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12355 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12356 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12357 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12358 TREE_TYPE (decl
), lhs
,
12359 RECUR (TREE_OPERAND (incr
, 2))));
12361 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12362 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12363 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12365 tree rhs
= TREE_OPERAND (incr
, 2);
12366 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12367 RECUR (TREE_OPERAND (incr
, 0)),
12368 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12369 RECUR (TREE_OPERAND (rhs
, 0)),
12370 RECUR (TREE_OPERAND (rhs
, 1))));
12373 incr
= RECUR (incr
);
12376 incr
= RECUR (incr
);
12380 TREE_VEC_ELT (declv
, i
) = decl
;
12381 TREE_VEC_ELT (initv
, i
) = init
;
12382 TREE_VEC_ELT (condv
, i
) = cond
;
12383 TREE_VEC_ELT (incrv
, i
) = incr
;
12387 /* Like tsubst_copy for expressions, etc. but also does semantic
12391 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12392 bool integral_constant_expression_p
)
12394 #define RECUR(NODE) \
12395 tsubst_expr ((NODE), args, complain, in_decl, \
12396 integral_constant_expression_p)
12400 if (t
== NULL_TREE
|| t
== error_mark_node
)
12403 if (EXPR_HAS_LOCATION (t
))
12404 input_location
= EXPR_LOCATION (t
);
12405 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12406 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12408 switch (TREE_CODE (t
))
12410 case STATEMENT_LIST
:
12412 tree_stmt_iterator i
;
12413 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12414 RECUR (tsi_stmt (i
));
12418 case CTOR_INITIALIZER
:
12419 finish_mem_initializers (tsubst_initializer_list
12420 (TREE_OPERAND (t
, 0), args
));
12424 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12428 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12429 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12430 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12432 finish_expr_stmt (tmp
);
12436 do_using_directive (USING_STMT_NAMESPACE (t
));
12441 tree decl
, pattern_decl
;
12444 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12445 if (TREE_CODE (decl
) == LABEL_DECL
)
12446 finish_label_decl (DECL_NAME (decl
));
12447 else if (TREE_CODE (decl
) == USING_DECL
)
12449 tree scope
= USING_DECL_SCOPE (decl
);
12450 tree name
= DECL_NAME (decl
);
12453 scope
= tsubst (scope
, args
, complain
, in_decl
);
12454 decl
= lookup_qualified_name (scope
, name
,
12455 /*is_type_p=*/false,
12456 /*complain=*/false);
12457 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12458 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12460 do_local_using_decl (decl
, scope
, name
);
12464 init
= DECL_INITIAL (decl
);
12465 decl
= tsubst (decl
, args
, complain
, in_decl
);
12466 if (decl
!= error_mark_node
)
12468 /* By marking the declaration as instantiated, we avoid
12469 trying to instantiate it. Since instantiate_decl can't
12470 handle local variables, and since we've already done
12471 all that needs to be done, that's the right thing to
12473 if (TREE_CODE (decl
) == VAR_DECL
)
12474 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12475 if (TREE_CODE (decl
) == VAR_DECL
12476 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12477 /* Anonymous aggregates are a special case. */
12478 finish_anon_union (decl
);
12481 int const_init
= false;
12482 maybe_push_decl (decl
);
12483 if (TREE_CODE (decl
) == VAR_DECL
12484 && DECL_PRETTY_FUNCTION_P (decl
))
12486 /* For __PRETTY_FUNCTION__ we have to adjust the
12488 const char *const name
12489 = cxx_printable_name (current_function_decl
, 2);
12490 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12494 tree t
= RECUR (init
);
12498 /* If we had an initializer but it
12499 instantiated to nothing,
12500 value-initialize the object. This will
12501 only occur when the initializer was a
12502 pack expansion where the parameter packs
12503 used in that expansion were of length
12505 init
= build_value_init (TREE_TYPE (decl
),
12507 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12508 init
= get_target_expr_sfinae (init
, complain
);
12514 if (TREE_CODE (decl
) == VAR_DECL
)
12515 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12517 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12522 /* A DECL_EXPR can also be used as an expression, in the condition
12523 clause of an if/for/while construct. */
12528 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12529 RECUR (FOR_INIT_STMT (t
));
12530 finish_for_init_stmt (stmt
);
12531 tmp
= RECUR (FOR_COND (t
));
12532 finish_for_cond (tmp
, stmt
);
12533 tmp
= RECUR (FOR_EXPR (t
));
12534 finish_for_expr (tmp
, stmt
);
12535 RECUR (FOR_BODY (t
));
12536 finish_for_stmt (stmt
);
12539 case RANGE_FOR_STMT
:
12542 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12543 decl
= RANGE_FOR_DECL (t
);
12544 decl
= tsubst (decl
, args
, complain
, in_decl
);
12545 maybe_push_decl (decl
);
12546 expr
= RECUR (RANGE_FOR_EXPR (t
));
12547 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12548 RECUR (RANGE_FOR_BODY (t
));
12549 finish_for_stmt (stmt
);
12554 stmt
= begin_while_stmt ();
12555 tmp
= RECUR (WHILE_COND (t
));
12556 finish_while_stmt_cond (tmp
, stmt
);
12557 RECUR (WHILE_BODY (t
));
12558 finish_while_stmt (stmt
);
12562 stmt
= begin_do_stmt ();
12563 RECUR (DO_BODY (t
));
12564 finish_do_body (stmt
);
12565 tmp
= RECUR (DO_COND (t
));
12566 finish_do_stmt (tmp
, stmt
);
12570 stmt
= begin_if_stmt ();
12571 tmp
= RECUR (IF_COND (t
));
12572 finish_if_stmt_cond (tmp
, stmt
);
12573 RECUR (THEN_CLAUSE (t
));
12574 finish_then_clause (stmt
);
12576 if (ELSE_CLAUSE (t
))
12578 begin_else_clause (stmt
);
12579 RECUR (ELSE_CLAUSE (t
));
12580 finish_else_clause (stmt
);
12583 finish_if_stmt (stmt
);
12587 if (BIND_EXPR_BODY_BLOCK (t
))
12588 stmt
= begin_function_body ();
12590 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12591 ? BCS_TRY_BLOCK
: 0);
12593 RECUR (BIND_EXPR_BODY (t
));
12595 if (BIND_EXPR_BODY_BLOCK (t
))
12596 finish_function_body (stmt
);
12598 finish_compound_stmt (stmt
);
12602 finish_break_stmt ();
12605 case CONTINUE_STMT
:
12606 finish_continue_stmt ();
12610 stmt
= begin_switch_stmt ();
12611 tmp
= RECUR (SWITCH_STMT_COND (t
));
12612 finish_switch_cond (tmp
, stmt
);
12613 RECUR (SWITCH_STMT_BODY (t
));
12614 finish_switch_stmt (stmt
);
12617 case CASE_LABEL_EXPR
:
12618 finish_case_label (EXPR_LOCATION (t
),
12619 RECUR (CASE_LOW (t
)),
12620 RECUR (CASE_HIGH (t
)));
12625 tree decl
= LABEL_EXPR_LABEL (t
);
12628 label
= finish_label_stmt (DECL_NAME (decl
));
12629 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
12630 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
12635 tmp
= GOTO_DESTINATION (t
);
12636 if (TREE_CODE (tmp
) != LABEL_DECL
)
12637 /* Computed goto's must be tsubst'd into. On the other hand,
12638 non-computed gotos must not be; the identifier in question
12639 will have no binding. */
12642 tmp
= DECL_NAME (tmp
);
12643 finish_goto_stmt (tmp
);
12647 tmp
= finish_asm_stmt
12648 (ASM_VOLATILE_P (t
),
12649 RECUR (ASM_STRING (t
)),
12650 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
12651 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
12652 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
12653 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
12655 tree asm_expr
= tmp
;
12656 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
12657 asm_expr
= TREE_OPERAND (asm_expr
, 0);
12658 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
12665 stmt
= begin_try_block ();
12666 RECUR (TRY_STMTS (t
));
12667 finish_cleanup_try_block (stmt
);
12668 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
12672 tree compound_stmt
= NULL_TREE
;
12674 if (FN_TRY_BLOCK_P (t
))
12675 stmt
= begin_function_try_block (&compound_stmt
);
12677 stmt
= begin_try_block ();
12679 RECUR (TRY_STMTS (t
));
12681 if (FN_TRY_BLOCK_P (t
))
12682 finish_function_try_block (stmt
);
12684 finish_try_block (stmt
);
12686 RECUR (TRY_HANDLERS (t
));
12687 if (FN_TRY_BLOCK_P (t
))
12688 finish_function_handler_sequence (stmt
, compound_stmt
);
12690 finish_handler_sequence (stmt
);
12696 tree decl
= HANDLER_PARMS (t
);
12700 decl
= tsubst (decl
, args
, complain
, in_decl
);
12701 /* Prevent instantiate_decl from trying to instantiate
12702 this variable. We've already done all that needs to be
12704 if (decl
!= error_mark_node
)
12705 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12707 stmt
= begin_handler ();
12708 finish_handler_parms (decl
, stmt
);
12709 RECUR (HANDLER_BODY (t
));
12710 finish_handler (stmt
);
12715 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
12718 case STATIC_ASSERT
:
12721 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
12724 /*integral_constant_expression_p=*/true);
12725 finish_static_assert (condition
,
12726 STATIC_ASSERT_MESSAGE (t
),
12727 STATIC_ASSERT_SOURCE_LOCATION (t
),
12728 /*member_p=*/false);
12733 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
12734 args
, complain
, in_decl
);
12735 stmt
= begin_omp_parallel ();
12736 RECUR (OMP_PARALLEL_BODY (t
));
12737 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
12738 = OMP_PARALLEL_COMBINED (t
);
12742 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
12743 args
, complain
, in_decl
);
12744 stmt
= begin_omp_task ();
12745 RECUR (OMP_TASK_BODY (t
));
12746 finish_omp_task (tmp
, stmt
);
12751 tree clauses
, body
, pre_body
;
12752 tree declv
, initv
, condv
, incrv
;
12755 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
12756 args
, complain
, in_decl
);
12757 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12758 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12759 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12760 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12762 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
12763 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
12764 &clauses
, args
, complain
, in_decl
,
12765 integral_constant_expression_p
);
12767 stmt
= begin_omp_structured_block ();
12769 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
12770 if (TREE_VEC_ELT (initv
, i
) == NULL
12771 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
12772 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
12773 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
12775 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
12776 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
12777 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
12781 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
12782 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
12783 gcc_assert (init
!= NULL
);
12784 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
12785 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
12787 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
12790 pre_body
= push_stmt_list ();
12791 RECUR (OMP_FOR_PRE_BODY (t
));
12792 pre_body
= pop_stmt_list (pre_body
);
12794 body
= push_stmt_list ();
12795 RECUR (OMP_FOR_BODY (t
));
12796 body
= pop_stmt_list (body
);
12798 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
12799 body
, pre_body
, clauses
);
12801 add_stmt (finish_omp_structured_block (stmt
));
12807 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
12808 stmt
= push_stmt_list ();
12809 RECUR (OMP_BODY (t
));
12810 stmt
= pop_stmt_list (stmt
);
12813 OMP_BODY (t
) = stmt
;
12814 OMP_CLAUSES (t
) = tmp
;
12822 stmt
= push_stmt_list ();
12823 RECUR (OMP_BODY (t
));
12824 stmt
= pop_stmt_list (stmt
);
12827 OMP_BODY (t
) = stmt
;
12832 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
12833 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
12835 tree op1
= TREE_OPERAND (t
, 1);
12836 tree rhs1
= NULL_TREE
;
12838 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
12840 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
12841 op1
= TREE_OPERAND (op1
, 1);
12843 lhs
= RECUR (TREE_OPERAND (op1
, 0));
12844 rhs
= RECUR (TREE_OPERAND (op1
, 1));
12845 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
12846 NULL_TREE
, NULL_TREE
, rhs1
);
12850 tree op1
= TREE_OPERAND (t
, 1);
12851 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
12852 tree rhs1
= NULL_TREE
;
12853 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
12854 enum tree_code opcode
= NOP_EXPR
;
12855 if (code
== OMP_ATOMIC_READ
)
12857 v
= RECUR (TREE_OPERAND (op1
, 0));
12858 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
12860 else if (code
== OMP_ATOMIC_CAPTURE_OLD
12861 || code
== OMP_ATOMIC_CAPTURE_NEW
)
12863 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
12864 v
= RECUR (TREE_OPERAND (op1
, 0));
12865 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
12866 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
12868 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
12869 op11
= TREE_OPERAND (op11
, 1);
12871 lhs
= RECUR (TREE_OPERAND (op11
, 0));
12872 rhs
= RECUR (TREE_OPERAND (op11
, 1));
12873 opcode
= TREE_CODE (op11
);
12878 lhs
= RECUR (TREE_OPERAND (op1
, 0));
12879 rhs
= RECUR (TREE_OPERAND (op1
, 1));
12881 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
12885 case EXPR_PACK_EXPANSION
:
12886 error ("invalid use of pack expansion expression");
12887 return error_mark_node
;
12889 case NONTYPE_ARGUMENT_PACK
:
12890 error ("use %<...%> to expand argument pack");
12891 return error_mark_node
;
12894 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
12896 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12897 /*function_p=*/false,
12898 integral_constant_expression_p
);
12905 /* T is a postfix-expression that is not being used in a function
12906 call. Return the substituted version of T. */
12909 tsubst_non_call_postfix_expression (tree t
, tree args
,
12910 tsubst_flags_t complain
,
12913 if (TREE_CODE (t
) == SCOPE_REF
)
12914 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
12915 /*done=*/false, /*address_p=*/false);
12917 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12918 /*function_p=*/false,
12919 /*integral_constant_expression_p=*/false);
12924 /* Like tsubst but deals with expressions and performs semantic
12925 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
12928 tsubst_copy_and_build (tree t
,
12930 tsubst_flags_t complain
,
12933 bool integral_constant_expression_p
)
12935 #define RECUR(NODE) \
12936 tsubst_copy_and_build (NODE, args, complain, in_decl, \
12937 /*function_p=*/false, \
12938 integral_constant_expression_p)
12942 if (t
== NULL_TREE
|| t
== error_mark_node
)
12945 switch (TREE_CODE (t
))
12949 /* Fall through. */
12950 case IDENTIFIER_NODE
:
12954 bool non_integral_constant_expression_p
;
12955 const char *error_msg
;
12957 if (IDENTIFIER_TYPENAME_P (t
))
12959 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12960 t
= mangle_conv_op_name_for_type (new_type
);
12963 /* Look up the name. */
12964 decl
= lookup_name (t
);
12966 /* By convention, expressions use ERROR_MARK_NODE to indicate
12967 failure, not NULL_TREE. */
12968 if (decl
== NULL_TREE
)
12969 decl
= error_mark_node
;
12971 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
12973 integral_constant_expression_p
,
12974 /*allow_non_integral_constant_expression_p=*/false,
12975 &non_integral_constant_expression_p
,
12976 /*template_p=*/false,
12978 /*address_p=*/false,
12979 /*template_arg_p=*/false,
12984 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
12985 decl
= unqualified_name_lookup_error (decl
);
12989 case TEMPLATE_ID_EXPR
:
12992 tree templ
= RECUR (TREE_OPERAND (t
, 0));
12993 tree targs
= TREE_OPERAND (t
, 1);
12996 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12998 if (TREE_CODE (templ
) == COMPONENT_REF
)
13000 object
= TREE_OPERAND (templ
, 0);
13001 templ
= TREE_OPERAND (templ
, 1);
13004 object
= NULL_TREE
;
13005 templ
= lookup_template_function (templ
, targs
);
13008 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13009 object
, templ
, NULL_TREE
);
13011 return baselink_for_fns (templ
);
13016 tree r
= RECUR (TREE_OPERAND (t
, 0));
13018 if (REFERENCE_REF_P (t
))
13020 /* A type conversion to reference type will be enclosed in
13021 such an indirect ref, but the substitution of the cast
13022 will have also added such an indirect ref. */
13023 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13024 r
= convert_from_reference (r
);
13027 r
= build_x_indirect_ref (r
, RO_UNARY_STAR
, complain
);
13033 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13034 RECUR (TREE_OPERAND (t
, 0)));
13037 case REINTERPRET_CAST_EXPR
:
13038 case CONST_CAST_EXPR
:
13039 case DYNAMIC_CAST_EXPR
:
13040 case STATIC_CAST_EXPR
:
13045 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13046 if (integral_constant_expression_p
13047 && !cast_valid_in_integral_constant_expression_p (type
))
13049 if (complain
& tf_error
)
13050 error ("a cast to a type other than an integral or "
13051 "enumeration type cannot appear in a constant-expression");
13052 return error_mark_node
;
13055 op
= RECUR (TREE_OPERAND (t
, 0));
13057 switch (TREE_CODE (t
))
13060 return build_functional_cast (type
, op
, complain
);
13061 case REINTERPRET_CAST_EXPR
:
13062 return build_reinterpret_cast (type
, op
, complain
);
13063 case CONST_CAST_EXPR
:
13064 return build_const_cast (type
, op
, complain
);
13065 case DYNAMIC_CAST_EXPR
:
13066 return build_dynamic_cast (type
, op
, complain
);
13067 case STATIC_CAST_EXPR
:
13068 return build_static_cast (type
, op
, complain
);
13070 gcc_unreachable ();
13074 case POSTDECREMENT_EXPR
:
13075 case POSTINCREMENT_EXPR
:
13076 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13077 args
, complain
, in_decl
);
13078 return build_x_unary_op (TREE_CODE (t
), op1
, complain
);
13080 case PREDECREMENT_EXPR
:
13081 case PREINCREMENT_EXPR
:
13085 case TRUTH_NOT_EXPR
:
13086 case UNARY_PLUS_EXPR
: /* Unary + */
13087 case REALPART_EXPR
:
13088 case IMAGPART_EXPR
:
13089 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)),
13093 op1
= TREE_OPERAND (t
, 0);
13094 if (TREE_CODE (op1
) == LABEL_DECL
)
13095 return finish_label_address_expr (DECL_NAME (op1
),
13096 EXPR_LOCATION (op1
));
13097 if (TREE_CODE (op1
) == SCOPE_REF
)
13098 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13099 /*done=*/true, /*address_p=*/true);
13101 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13103 return build_x_unary_op (ADDR_EXPR
, op1
, complain
);
13108 case TRUNC_DIV_EXPR
:
13109 case CEIL_DIV_EXPR
:
13110 case FLOOR_DIV_EXPR
:
13111 case ROUND_DIV_EXPR
:
13112 case EXACT_DIV_EXPR
:
13116 case TRUNC_MOD_EXPR
:
13117 case FLOOR_MOD_EXPR
:
13118 case TRUTH_ANDIF_EXPR
:
13119 case TRUTH_ORIF_EXPR
:
13120 case TRUTH_AND_EXPR
:
13121 case TRUTH_OR_EXPR
:
13136 return build_x_binary_op
13138 RECUR (TREE_OPERAND (t
, 0)),
13139 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13141 : TREE_CODE (TREE_OPERAND (t
, 0))),
13142 RECUR (TREE_OPERAND (t
, 1)),
13143 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13145 : TREE_CODE (TREE_OPERAND (t
, 1))),
13150 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13151 /*address_p=*/false);
13153 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13154 args
, complain
, in_decl
);
13155 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
13158 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13159 return tsubst_copy (t
, args
, complain
, in_decl
);
13163 op1
= TREE_OPERAND (t
, 0);
13166 /* When there are no ARGS, we are trying to evaluate a
13167 non-dependent expression from the parser. Trying to do
13168 the substitutions may not work. */
13170 op1
= TREE_TYPE (op1
);
13174 ++cp_unevaluated_operand
;
13175 ++c_inhibit_evaluation_warnings
;
13176 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13177 /*function_p=*/false,
13178 /*integral_constant_expression_p=*/false);
13179 --cp_unevaluated_operand
;
13180 --c_inhibit_evaluation_warnings
;
13183 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13184 complain
& tf_error
);
13186 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13187 complain
& tf_error
);
13189 case AT_ENCODE_EXPR
:
13191 op1
= TREE_OPERAND (t
, 0);
13192 ++cp_unevaluated_operand
;
13193 ++c_inhibit_evaluation_warnings
;
13194 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13195 /*function_p=*/false,
13196 /*integral_constant_expression_p=*/false);
13197 --cp_unevaluated_operand
;
13198 --c_inhibit_evaluation_warnings
;
13199 return objc_build_encode_expr (op1
);
13202 case NOEXCEPT_EXPR
:
13203 op1
= TREE_OPERAND (t
, 0);
13204 ++cp_unevaluated_operand
;
13205 ++c_inhibit_evaluation_warnings
;
13206 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13207 /*function_p=*/false,
13208 /*integral_constant_expression_p=*/false);
13209 --cp_unevaluated_operand
;
13210 --c_inhibit_evaluation_warnings
;
13211 return finish_noexcept_expr (op1
, complain
);
13215 tree r
= build_x_modify_expr
13216 (RECUR (TREE_OPERAND (t
, 0)),
13217 TREE_CODE (TREE_OPERAND (t
, 1)),
13218 RECUR (TREE_OPERAND (t
, 2)),
13220 /* TREE_NO_WARNING must be set if either the expression was
13221 parenthesized or it uses an operator such as >>= rather
13222 than plain assignment. In the former case, it was already
13223 set and must be copied. In the latter case,
13224 build_x_modify_expr sets it and it must not be reset
13226 if (TREE_NO_WARNING (t
))
13227 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13232 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13233 args
, complain
, in_decl
);
13234 /* Remember that there was a reference to this entity. */
13237 return build_x_arrow (op1
);
13241 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13242 tree init
= RECUR (TREE_OPERAND (t
, 3));
13243 VEC(tree
,gc
) *placement_vec
;
13244 VEC(tree
,gc
) *init_vec
;
13247 if (placement
== NULL_TREE
)
13248 placement_vec
= NULL
;
13251 placement_vec
= make_tree_vector ();
13252 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13253 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
13256 /* If there was an initializer in the original tree, but it
13257 instantiated to an empty list, then we should pass a
13258 non-NULL empty vector to tell build_new that it was an
13259 empty initializer() rather than no initializer. This can
13260 only happen when the initializer is a pack expansion whose
13261 parameter packs are of length zero. */
13262 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13266 init_vec
= make_tree_vector ();
13267 if (init
== void_zero_node
)
13268 gcc_assert (init_vec
!= NULL
);
13271 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13272 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
13276 ret
= build_new (&placement_vec
,
13277 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13278 RECUR (TREE_OPERAND (t
, 2)),
13280 NEW_EXPR_USE_GLOBAL (t
),
13283 if (placement_vec
!= NULL
)
13284 release_tree_vector (placement_vec
);
13285 if (init_vec
!= NULL
)
13286 release_tree_vector (init_vec
);
13292 return delete_sanity
13293 (RECUR (TREE_OPERAND (t
, 0)),
13294 RECUR (TREE_OPERAND (t
, 1)),
13295 DELETE_EXPR_USE_VEC (t
),
13296 DELETE_EXPR_USE_GLOBAL (t
),
13299 case COMPOUND_EXPR
:
13300 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
13301 RECUR (TREE_OPERAND (t
, 1)),
13307 VEC(tree
,gc
) *call_args
;
13308 unsigned int nargs
, i
;
13313 function
= CALL_EXPR_FN (t
);
13314 /* When we parsed the expression, we determined whether or
13315 not Koenig lookup should be performed. */
13316 koenig_p
= KOENIG_LOOKUP_P (t
);
13317 if (TREE_CODE (function
) == SCOPE_REF
)
13319 qualified_p
= true;
13320 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13322 /*address_p=*/false);
13324 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13326 /* Do nothing; calling tsubst_copy_and_build on an identifier
13327 would incorrectly perform unqualified lookup again.
13329 Note that we can also have an IDENTIFIER_NODE if the earlier
13330 unqualified lookup found a member function; in that case
13331 koenig_p will be false and we do want to do the lookup
13332 again to find the instantiated member function.
13334 FIXME but doing that causes c++/15272, so we need to stop
13335 using IDENTIFIER_NODE in that situation. */
13336 qualified_p
= false;
13340 if (TREE_CODE (function
) == COMPONENT_REF
)
13342 tree op
= TREE_OPERAND (function
, 1);
13344 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13345 || (BASELINK_P (op
)
13346 && BASELINK_QUALIFIED_P (op
)));
13349 qualified_p
= false;
13351 function
= tsubst_copy_and_build (function
, args
, complain
,
13354 integral_constant_expression_p
);
13356 if (BASELINK_P (function
))
13357 qualified_p
= true;
13360 nargs
= call_expr_nargs (t
);
13361 call_args
= make_tree_vector ();
13362 for (i
= 0; i
< nargs
; ++i
)
13364 tree arg
= CALL_EXPR_ARG (t
, i
);
13366 if (!PACK_EXPANSION_P (arg
))
13367 VEC_safe_push (tree
, gc
, call_args
,
13368 RECUR (CALL_EXPR_ARG (t
, i
)));
13371 /* Expand the pack expansion and push each entry onto
13373 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13374 if (TREE_CODE (arg
) == TREE_VEC
)
13376 unsigned int len
, j
;
13378 len
= TREE_VEC_LENGTH (arg
);
13379 for (j
= 0; j
< len
; ++j
)
13381 tree value
= TREE_VEC_ELT (arg
, j
);
13382 if (value
!= NULL_TREE
)
13383 value
= convert_from_reference (value
);
13384 VEC_safe_push (tree
, gc
, call_args
, value
);
13389 /* A partial substitution. Add one entry. */
13390 VEC_safe_push (tree
, gc
, call_args
, arg
);
13395 /* We do not perform argument-dependent lookup if normal
13396 lookup finds a non-function, in accordance with the
13397 expected resolution of DR 218. */
13399 && ((is_overloaded_fn (function
)
13400 /* If lookup found a member function, the Koenig lookup is
13401 not appropriate, even if an unqualified-name was used
13402 to denote the function. */
13403 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13404 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13405 /* Only do this when substitution turns a dependent call
13406 into a non-dependent call. */
13407 && type_dependent_expression_p_push (t
)
13408 && !any_type_dependent_arguments_p (call_args
))
13409 function
= perform_koenig_lookup (function
, call_args
, false,
13412 if (TREE_CODE (function
) == IDENTIFIER_NODE
13413 && !any_type_dependent_arguments_p (call_args
))
13415 if (koenig_p
&& (complain
& tf_warning_or_error
))
13417 /* For backwards compatibility and good diagnostics, try
13418 the unqualified lookup again if we aren't in SFINAE
13420 tree unq
= (tsubst_copy_and_build
13421 (function
, args
, complain
, in_decl
, true,
13422 integral_constant_expression_p
));
13423 if (unq
!= function
)
13426 if (TREE_CODE (fn
) == COMPONENT_REF
)
13427 fn
= TREE_OPERAND (fn
, 1);
13428 if (is_overloaded_fn (fn
))
13429 fn
= get_first_fn (fn
);
13430 permerror (EXPR_LOC_OR_HERE (t
),
13431 "%qD was not declared in this scope, "
13432 "and no declarations were found by "
13433 "argument-dependent lookup at the point "
13434 "of instantiation", function
);
13435 if (DECL_CLASS_SCOPE_P (fn
))
13437 inform (EXPR_LOC_OR_HERE (t
),
13438 "declarations in dependent base %qT are "
13439 "not found by unqualified lookup",
13440 DECL_CLASS_CONTEXT (fn
));
13441 if (current_class_ptr
)
13442 inform (EXPR_LOC_OR_HERE (t
),
13443 "use %<this->%D%> instead", function
);
13445 inform (EXPR_LOC_OR_HERE (t
),
13446 "use %<%T::%D%> instead",
13447 current_class_name
, function
);
13450 inform (0, "%q+D declared here, later in the "
13451 "translation unit", fn
);
13455 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13457 unqualified_name_lookup_error (function
);
13458 release_tree_vector (call_args
);
13459 return error_mark_node
;
13463 /* Remember that there was a reference to this entity. */
13464 if (DECL_P (function
))
13465 mark_used (function
);
13467 if (TREE_CODE (function
) == OFFSET_REF
)
13468 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
13469 else if (TREE_CODE (function
) == COMPONENT_REF
)
13471 tree instance
= TREE_OPERAND (function
, 0);
13472 tree fn
= TREE_OPERAND (function
, 1);
13474 if (processing_template_decl
13475 && (type_dependent_expression_p (instance
)
13476 || (!BASELINK_P (fn
)
13477 && TREE_CODE (fn
) != FIELD_DECL
)
13478 || type_dependent_expression_p (fn
)
13479 || any_type_dependent_arguments_p (call_args
)))
13480 ret
= build_nt_call_vec (function
, call_args
);
13481 else if (!BASELINK_P (fn
))
13482 ret
= finish_call_expr (function
, &call_args
,
13483 /*disallow_virtual=*/false,
13484 /*koenig_p=*/false,
13487 ret
= (build_new_method_call
13489 &call_args
, NULL_TREE
,
13490 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
13495 ret
= finish_call_expr (function
, &call_args
,
13496 /*disallow_virtual=*/qualified_p
,
13500 release_tree_vector (call_args
);
13506 return build_x_conditional_expr
13507 (RECUR (TREE_OPERAND (t
, 0)),
13508 RECUR (TREE_OPERAND (t
, 1)),
13509 RECUR (TREE_OPERAND (t
, 2)),
13512 case PSEUDO_DTOR_EXPR
:
13513 return finish_pseudo_destructor_expr
13514 (RECUR (TREE_OPERAND (t
, 0)),
13515 RECUR (TREE_OPERAND (t
, 1)),
13516 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
13520 tree purpose
, value
, chain
;
13522 if (t
== void_list_node
)
13525 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
13526 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
13528 /* We have pack expansions, so expand those and
13529 create a new list out of it. */
13530 tree purposevec
= NULL_TREE
;
13531 tree valuevec
= NULL_TREE
;
13535 /* Expand the argument expressions. */
13536 if (TREE_PURPOSE (t
))
13537 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
13538 complain
, in_decl
);
13539 if (TREE_VALUE (t
))
13540 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
13541 complain
, in_decl
);
13543 /* Build the rest of the list. */
13544 chain
= TREE_CHAIN (t
);
13545 if (chain
&& chain
!= void_type_node
)
13546 chain
= RECUR (chain
);
13548 /* Determine the number of arguments. */
13549 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
13551 len
= TREE_VEC_LENGTH (purposevec
);
13552 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
13554 else if (TREE_CODE (valuevec
) == TREE_VEC
)
13555 len
= TREE_VEC_LENGTH (valuevec
);
13558 /* Since we only performed a partial substitution into
13559 the argument pack, we only return a single list
13561 if (purposevec
== TREE_PURPOSE (t
)
13562 && valuevec
== TREE_VALUE (t
)
13563 && chain
== TREE_CHAIN (t
))
13566 return tree_cons (purposevec
, valuevec
, chain
);
13569 /* Convert the argument vectors into a TREE_LIST */
13573 /* Grab the Ith values. */
13575 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
13578 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
13581 /* Build the list (backwards). */
13582 chain
= tree_cons (purpose
, value
, chain
);
13588 purpose
= TREE_PURPOSE (t
);
13590 purpose
= RECUR (purpose
);
13591 value
= TREE_VALUE (t
);
13593 value
= RECUR (value
);
13594 chain
= TREE_CHAIN (t
);
13595 if (chain
&& chain
!= void_type_node
)
13596 chain
= RECUR (chain
);
13597 if (purpose
== TREE_PURPOSE (t
)
13598 && value
== TREE_VALUE (t
)
13599 && chain
== TREE_CHAIN (t
))
13601 return tree_cons (purpose
, value
, chain
);
13604 case COMPONENT_REF
:
13610 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13611 args
, complain
, in_decl
);
13612 /* Remember that there was a reference to this entity. */
13613 if (DECL_P (object
))
13614 mark_used (object
);
13615 object_type
= TREE_TYPE (object
);
13617 member
= TREE_OPERAND (t
, 1);
13618 if (BASELINK_P (member
))
13619 member
= tsubst_baselink (member
,
13620 non_reference (TREE_TYPE (object
)),
13621 args
, complain
, in_decl
);
13623 member
= tsubst_copy (member
, args
, complain
, in_decl
);
13624 if (member
== error_mark_node
)
13625 return error_mark_node
;
13627 if (object_type
&& !CLASS_TYPE_P (object_type
))
13629 if (SCALAR_TYPE_P (object_type
))
13631 tree s
= NULL_TREE
;
13632 tree dtor
= member
;
13634 if (TREE_CODE (dtor
) == SCOPE_REF
)
13636 s
= TREE_OPERAND (dtor
, 0);
13637 dtor
= TREE_OPERAND (dtor
, 1);
13639 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
13641 dtor
= TREE_OPERAND (dtor
, 0);
13643 return finish_pseudo_destructor_expr (object
, s
, dtor
);
13647 else if (TREE_CODE (member
) == SCOPE_REF
13648 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
13653 /* Lookup the template functions now that we know what the
13655 tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
13656 args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
13657 member
= lookup_qualified_name (TREE_OPERAND (member
, 0), tmpl
,
13658 /*is_type_p=*/false,
13659 /*complain=*/false);
13660 if (BASELINK_P (member
))
13662 BASELINK_FUNCTIONS (member
)
13663 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
13665 member
= (adjust_result_of_qualified_name_lookup
13666 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
13671 qualified_name_lookup_error (object_type
, tmpl
, member
,
13673 return error_mark_node
;
13676 else if (TREE_CODE (member
) == SCOPE_REF
13677 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
13678 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
13680 if (complain
& tf_error
)
13682 if (TYPE_P (TREE_OPERAND (member
, 0)))
13683 error ("%qT is not a class or namespace",
13684 TREE_OPERAND (member
, 0));
13686 error ("%qD is not a class or namespace",
13687 TREE_OPERAND (member
, 0));
13689 return error_mark_node
;
13691 else if (TREE_CODE (member
) == FIELD_DECL
)
13692 return finish_non_static_data_member (member
, object
, NULL_TREE
);
13694 return finish_class_member_access_expr (object
, member
,
13695 /*template_p=*/false,
13701 (RECUR (TREE_OPERAND (t
, 0)));
13705 VEC(constructor_elt
,gc
) *n
;
13706 constructor_elt
*ce
;
13707 unsigned HOST_WIDE_INT idx
;
13708 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13709 bool process_index_p
;
13711 bool need_copy_p
= false;
13714 if (type
== error_mark_node
)
13715 return error_mark_node
;
13717 /* digest_init will do the wrong thing if we let it. */
13718 if (type
&& TYPE_PTRMEMFUNC_P (type
))
13721 /* We do not want to process the index of aggregate
13722 initializers as they are identifier nodes which will be
13723 looked up by digest_init. */
13724 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
13726 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
13727 newlen
= VEC_length (constructor_elt
, n
);
13728 FOR_EACH_VEC_ELT (constructor_elt
, n
, idx
, ce
)
13730 if (ce
->index
&& process_index_p
)
13731 ce
->index
= RECUR (ce
->index
);
13733 if (PACK_EXPANSION_P (ce
->value
))
13735 /* Substitute into the pack expansion. */
13736 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
13739 if (ce
->value
== error_mark_node
13740 || PACK_EXPANSION_P (ce
->value
))
13742 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
13743 /* Just move the argument into place. */
13744 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
13747 /* Update the length of the final CONSTRUCTOR
13748 arguments vector, and note that we will need to
13750 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
13751 need_copy_p
= true;
13755 ce
->value
= RECUR (ce
->value
);
13760 VEC(constructor_elt
,gc
) *old_n
= n
;
13762 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
13763 FOR_EACH_VEC_ELT (constructor_elt
, old_n
, idx
, ce
)
13765 if (TREE_CODE (ce
->value
) == TREE_VEC
)
13767 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
13768 for (i
= 0; i
< len
; ++i
)
13769 CONSTRUCTOR_APPEND_ELT (n
, 0,
13770 TREE_VEC_ELT (ce
->value
, i
));
13773 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
13777 r
= build_constructor (init_list_type_node
, n
);
13778 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
13780 if (TREE_HAS_CONSTRUCTOR (t
))
13781 return finish_compound_literal (type
, r
, complain
);
13783 TREE_TYPE (r
) = type
;
13789 tree operand_0
= TREE_OPERAND (t
, 0);
13790 if (TYPE_P (operand_0
))
13792 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
13793 return get_typeid (operand_0
);
13797 operand_0
= RECUR (operand_0
);
13798 return build_typeid (operand_0
);
13809 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
13811 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
13812 /* If the original type was a reference, we'll be wrapped in
13813 the appropriate INDIRECT_REF. */
13814 r
= convert_from_reference (r
);
13819 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
13820 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
13822 case OFFSETOF_EXPR
:
13823 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
13827 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
13828 complain
, in_decl
);
13830 tree type2
= TRAIT_EXPR_TYPE2 (t
);
13832 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
13834 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
13839 tree old_stmt_expr
= cur_stmt_expr
;
13840 tree stmt_expr
= begin_stmt_expr ();
13842 cur_stmt_expr
= stmt_expr
;
13843 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
13844 integral_constant_expression_p
);
13845 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
13846 cur_stmt_expr
= old_stmt_expr
;
13848 /* If the resulting list of expression statement is empty,
13849 fold it further into void_zero_node. */
13850 if (empty_expr_stmt_p (stmt_expr
))
13851 stmt_expr
= void_zero_node
;
13857 t
= tsubst_copy (t
, args
, complain
, in_decl
);
13858 /* As in finish_id_expression, we resolve enumeration constants
13859 to their underlying values. */
13860 if (TREE_CODE (t
) == CONST_DECL
)
13862 used_types_insert (TREE_TYPE (t
));
13863 return DECL_INITIAL (t
);
13869 tree r
= build_lambda_expr ();
13871 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13872 TREE_TYPE (r
) = type
;
13873 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
13875 LAMBDA_EXPR_LOCATION (r
)
13876 = LAMBDA_EXPR_LOCATION (t
);
13877 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
13878 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
13879 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
13880 LAMBDA_EXPR_DISCRIMINATOR (r
)
13881 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
13882 LAMBDA_EXPR_EXTRA_SCOPE (r
)
13883 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t
));
13884 if (LAMBDA_EXPR_RETURN_TYPE (t
) == dependent_lambda_return_type_node
)
13886 LAMBDA_EXPR_RETURN_TYPE (r
) = dependent_lambda_return_type_node
;
13887 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (r
) = true;
13890 LAMBDA_EXPR_RETURN_TYPE (r
)
13891 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
13893 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
13894 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
13896 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
13897 determine_visibility (TYPE_NAME (type
));
13898 /* Now that we know visibility, instantiate the type so we have a
13899 declaration of the op() for later calls to lambda_function. */
13900 complete_type (type
);
13902 /* The capture list refers to closure members, so this needs to
13903 wait until after we finish instantiating the type. */
13904 LAMBDA_EXPR_CAPTURE_LIST (r
)
13905 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
13907 return build_lambda_object (r
);
13911 /* We can get here for a constant initializer of non-dependent type.
13912 FIXME stop folding in cp_parser_initializer_clause. */
13913 gcc_assert (TREE_CONSTANT (t
));
13915 tree r
= get_target_expr (RECUR (TARGET_EXPR_INITIAL (t
)));
13916 TREE_CONSTANT (r
) = true;
13921 /* Handle Objective-C++ constructs, if appropriate. */
13924 = objcp_tsubst_copy_and_build (t
, args
, complain
,
13925 in_decl
, /*function_p=*/false);
13929 return tsubst_copy (t
, args
, complain
, in_decl
);
13935 /* Verify that the instantiated ARGS are valid. For type arguments,
13936 make sure that the type's linkage is ok. For non-type arguments,
13937 make sure they are constants if they are integral or enumerations.
13938 Emit an error under control of COMPLAIN, and return TRUE on error. */
13941 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
13943 if (ARGUMENT_PACK_P (t
))
13945 tree vec
= ARGUMENT_PACK_ARGS (t
);
13946 int len
= TREE_VEC_LENGTH (vec
);
13947 bool result
= false;
13950 for (i
= 0; i
< len
; ++i
)
13951 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
13955 else if (TYPE_P (t
))
13957 /* [basic.link]: A name with no linkage (notably, the name
13958 of a class or enumeration declared in a local scope)
13959 shall not be used to declare an entity with linkage.
13960 This implies that names with no linkage cannot be used as
13963 DR 757 relaxes this restriction for C++0x. */
13964 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
13965 : no_linkage_check (t
, /*relaxed_p=*/false));
13969 /* DR 488 makes use of a type with no linkage cause
13970 type deduction to fail. */
13971 if (complain
& tf_error
)
13973 if (TYPE_ANONYMOUS_P (nt
))
13974 error ("%qT is/uses anonymous type", t
);
13976 error ("template argument for %qD uses local type %qT",
13981 /* In order to avoid all sorts of complications, we do not
13982 allow variably-modified types as template arguments. */
13983 else if (variably_modified_type_p (t
, NULL_TREE
))
13985 if (complain
& tf_error
)
13986 error ("%qT is a variably modified type", t
);
13990 /* A non-type argument of integral or enumerated type must be a
13992 else if (TREE_TYPE (t
)
13993 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
13994 && !TREE_CONSTANT (t
))
13996 if (complain
& tf_error
)
13997 error ("integral expression %qE is not constant", t
);
14004 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14006 int ix
, len
= DECL_NTPARMS (tmpl
);
14007 bool result
= false;
14009 for (ix
= 0; ix
!= len
; ix
++)
14011 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14014 if (result
&& (complain
& tf_error
))
14015 error (" trying to instantiate %qD", tmpl
);
14019 /* In C++0x, it's possible to have a function template whose type depends
14020 on itself recursively. This is most obvious with decltype, but can also
14021 occur with enumeration scope (c++/48969). So we need to catch infinite
14022 recursion and reject the substitution at deduction time; this function
14023 will return error_mark_node for any repeated substitution.
14025 This also catches excessive recursion such as when f<N> depends on
14026 f<N-1> across all integers, and returns error_mark_node for all the
14027 substitutions back up to the initial one.
14029 This is, of course, not reentrant. */
14032 deduction_tsubst_fntype (tree fn
, tree targs
, tsubst_flags_t complain
)
14034 static bool excessive_deduction_depth
;
14035 static int deduction_depth
;
14036 struct pending_template
*old_last_pend
= last_pending_template
;
14037 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14039 tree fntype
= TREE_TYPE (fn
);
14043 if (excessive_deduction_depth
)
14044 return error_mark_node
;
14046 tinst
= build_tree_list (fn
, targs
);
14047 if (!push_tinst_level (tinst
))
14049 excessive_deduction_depth
= true;
14051 return error_mark_node
;
14054 input_location
= DECL_SOURCE_LOCATION (fn
);
14056 push_deduction_access_scope (fn
);
14057 r
= tsubst (fntype
, targs
, complain
, NULL_TREE
);
14058 pop_deduction_access_scope (fn
);
14061 if (excessive_deduction_depth
)
14063 r
= error_mark_node
;
14064 if (deduction_depth
== 0)
14065 /* Reset once we're all the way out. */
14066 excessive_deduction_depth
= false;
14069 pop_tinst_level ();
14070 /* We can't free this if a pending_template entry or last_error_tinst_level
14071 is pointing at it. */
14072 if (last_pending_template
== old_last_pend
14073 && last_error_tinst_level
== old_error_tinst
)
14078 /* Instantiate the indicated variable or function template TMPL with
14079 the template arguments in TARG_PTR. */
14082 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14084 tree targ_ptr
= orig_args
;
14088 HOST_WIDE_INT saved_processing_template_decl
;
14090 if (tmpl
== error_mark_node
)
14091 return error_mark_node
;
14093 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14095 /* If this function is a clone, handle it specially. */
14096 if (DECL_CLONED_FUNCTION_P (tmpl
))
14101 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14102 DECL_CLONED_FUNCTION. */
14103 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14104 targ_ptr
, complain
);
14105 if (spec
== error_mark_node
)
14106 return error_mark_node
;
14108 /* Look for the clone. */
14109 FOR_EACH_CLONE (clone
, spec
)
14110 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14112 /* We should always have found the clone by now. */
14113 gcc_unreachable ();
14117 /* Check to see if we already have this specialization. */
14118 gen_tmpl
= most_general_template (tmpl
);
14119 if (tmpl
!= gen_tmpl
)
14120 /* The TMPL is a partial instantiation. To get a full set of
14121 arguments we must add the arguments used to perform the
14122 partial instantiation. */
14123 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14126 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14127 but it doesn't seem to be on the hot path. */
14128 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14130 gcc_assert (tmpl
== gen_tmpl
14131 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14133 || fndecl
== NULL_TREE
);
14135 if (spec
!= NULL_TREE
)
14138 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14140 return error_mark_node
;
14142 /* We are building a FUNCTION_DECL, during which the access of its
14143 parameters and return types have to be checked. However this
14144 FUNCTION_DECL which is the desired context for access checking
14145 is not built yet. We solve this chicken-and-egg problem by
14146 deferring all checks until we have the FUNCTION_DECL. */
14147 push_deferring_access_checks (dk_deferred
);
14149 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
14150 (because, for example, we have encountered a non-dependent
14151 function call in the body of a template function and must now
14152 determine which of several overloaded functions will be called),
14153 within the instantiation itself we are not processing a
14155 saved_processing_template_decl
= processing_template_decl
;
14156 processing_template_decl
= 0;
14157 /* Substitute template parameters to obtain the specialization. */
14158 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14159 targ_ptr
, complain
, gen_tmpl
);
14160 processing_template_decl
= saved_processing_template_decl
;
14161 if (fndecl
== error_mark_node
)
14162 return error_mark_node
;
14164 /* Now we know the specialization, compute access previously
14166 push_access_scope (fndecl
);
14168 /* Some typedefs referenced from within the template code need to be access
14169 checked at template instantiation time, i.e now. These types were
14170 added to the template at parsing time. Let's get those and perfom
14171 the acces checks then. */
14172 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl
), targ_ptr
);
14173 perform_deferred_access_checks ();
14174 pop_access_scope (fndecl
);
14175 pop_deferring_access_checks ();
14177 /* The DECL_TI_TEMPLATE should always be the immediate parent
14178 template, not the most general template. */
14179 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14181 /* If we've just instantiated the main entry point for a function,
14182 instantiate all the alternate entry points as well. We do this
14183 by cloning the instantiation of the main entry point, not by
14184 instantiating the template clones. */
14185 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14186 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14191 /* Wrapper for instantiate_template_1. */
14194 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14197 timevar_push (TV_TEMPLATE_INST
);
14198 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14199 timevar_pop (TV_TEMPLATE_INST
);
14203 /* We're going to do deduction substitution on the type of TMPL, a function
14204 template. In C++11 mode, push into that access scope. In C++03 mode,
14205 disable access checking. */
14208 push_deduction_access_scope (tree tmpl
)
14210 if (cxx_dialect
>= cxx0x
)
14212 int ptd
= processing_template_decl
;
14213 push_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
14214 /* Preserve processing_template_decl across push_to_top_level. */
14215 if (ptd
&& !processing_template_decl
)
14216 ++processing_template_decl
;
14219 push_deferring_access_checks (dk_no_check
);
14222 /* And pop back out. */
14225 pop_deduction_access_scope (tree tmpl
)
14227 if (cxx_dialect
>= cxx0x
)
14228 pop_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
14230 pop_deferring_access_checks ();
14233 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14234 NARGS elements of the arguments that are being used when calling
14235 it. TARGS is a vector into which the deduced template arguments
14238 Return zero for success, 2 for an incomplete match that doesn't resolve
14239 all the types, and 1 for complete failure. An error message will be
14240 printed only for an incomplete match.
14242 If FN is a conversion operator, or we are trying to produce a specific
14243 specialization, RETURN_TYPE is the return type desired.
14245 The EXPLICIT_TARGS are explicit template arguments provided via a
14248 The parameter STRICT is one of:
14251 We are deducing arguments for a function call, as in
14252 [temp.deduct.call].
14255 We are deducing arguments for a conversion function, as in
14256 [temp.deduct.conv].
14259 We are deducing arguments when doing an explicit instantiation
14260 as in [temp.explicit], when determining an explicit specialization
14261 as in [temp.expl.spec], or when taking the address of a function
14262 template, as in [temp.deduct.funcaddr]. */
14265 fn_type_unification (tree fn
,
14266 tree explicit_targs
,
14269 unsigned int nargs
,
14271 unification_kind_t strict
,
14278 bool incomplete_argument_packs_p
= false;
14280 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14282 fntype
= TREE_TYPE (fn
);
14283 if (explicit_targs
)
14287 The specified template arguments must match the template
14288 parameters in kind (i.e., type, nontype, template), and there
14289 must not be more arguments than there are parameters;
14290 otherwise type deduction fails.
14292 Nontype arguments must match the types of the corresponding
14293 nontype template parameters, or must be convertible to the
14294 types of the corresponding nontype parameters as specified in
14295 _temp.arg.nontype_, otherwise type deduction fails.
14297 All references in the function type of the function template
14298 to the corresponding template parameters are replaced by the
14299 specified template argument values. If a substitution in a
14300 template parameter or in the function type of the function
14301 template results in an invalid type, type deduction fails. */
14302 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14303 int i
, len
= TREE_VEC_LENGTH (tparms
);
14304 tree converted_args
;
14305 bool incomplete
= false;
14307 if (explicit_targs
== error_mark_node
)
14308 return unify_invalid (explain_p
);
14311 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14313 ? tf_warning_or_error
14315 /*require_all_args=*/false,
14316 /*use_default_args=*/false));
14317 if (converted_args
== error_mark_node
)
14320 /* Substitute the explicit args into the function type. This is
14321 necessary so that, for instance, explicitly declared function
14322 arguments can match null pointed constants. If we were given
14323 an incomplete set of explicit args, we must not do semantic
14324 processing during substitution as we could create partial
14326 for (i
= 0; i
< len
; i
++)
14328 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14329 bool parameter_pack
= false;
14331 /* Dig out the actual parm. */
14332 if (TREE_CODE (parm
) == TYPE_DECL
14333 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14335 parm
= TREE_TYPE (parm
);
14336 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14338 else if (TREE_CODE (parm
) == PARM_DECL
)
14340 parm
= DECL_INITIAL (parm
);
14341 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14344 if (parameter_pack
)
14348 template_parm_level_and_index (parm
, &level
, &idx
);
14350 /* Mark the argument pack as "incomplete". We could
14351 still deduce more arguments during unification.
14352 We remove this mark in type_unification_real. */
14353 targ
= TMPL_ARG (converted_args
, level
, idx
);
14356 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
14357 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
14358 = ARGUMENT_PACK_ARGS (targ
);
14361 /* We have some incomplete argument packs. */
14362 incomplete_argument_packs_p
= true;
14366 if (incomplete_argument_packs_p
)
14367 /* Any substitution is guaranteed to be incomplete if there
14368 are incomplete argument packs, because we can still deduce
14372 incomplete
= NUM_TMPL_ARGS (explicit_targs
) != NUM_TMPL_ARGS (targs
);
14374 processing_template_decl
+= incomplete
;
14375 fntype
= deduction_tsubst_fntype (fn
, converted_args
,
14377 ? tf_warning_or_error
14379 processing_template_decl
-= incomplete
;
14381 if (fntype
== error_mark_node
)
14384 /* Place the explicitly specified arguments in TARGS. */
14385 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
14386 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
14389 /* Never do unification on the 'this' parameter. */
14390 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
14396 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
14397 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
14398 new_args
[0] = return_type
;
14399 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
14404 /* We allow incomplete unification without an error message here
14405 because the standard doesn't seem to explicitly prohibit it. Our
14406 callers must be ready to deal with unification failures in any
14408 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
14409 targs
, parms
, args
, nargs
, /*subr=*/0,
14410 strict
, flags
, explain_p
);
14412 /* Now that we have bindings for all of the template arguments,
14413 ensure that the arguments deduced for the template template
14414 parameters have compatible template parameter lists. We cannot
14415 check this property before we have deduced all template
14416 arguments, because the template parameter types of a template
14417 template parameter might depend on prior template parameters
14418 deduced after the template template parameter. The following
14419 ill-formed example illustrates this issue:
14421 template<typename T, template<T> class C> void f(C<5>, T);
14423 template<int N> struct X {};
14426 f(X<5>(), 5l); // error: template argument deduction fails
14429 The template parameter list of 'C' depends on the template type
14430 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14431 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14432 time that we deduce 'C'. */
14434 && !template_template_parm_bindings_ok_p
14435 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
14436 return unify_inconsistent_template_template_parameters (explain_p
);
14439 /* All is well so far. Now, check:
14443 When all template arguments have been deduced, all uses of
14444 template parameters in nondeduced contexts are replaced with
14445 the corresponding deduced argument values. If the
14446 substitution results in an invalid type, as described above,
14447 type deduction fails. */
14449 tree substed
= deduction_tsubst_fntype (fn
, targs
,
14451 ? tf_warning_or_error
14453 if (substed
== error_mark_node
)
14456 /* If we're looking for an exact match, check that what we got
14457 is indeed an exact match. It might not be if some template
14458 parameters are used in non-deduced contexts. */
14459 if (strict
== DEDUCE_EXACT
)
14464 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
14466 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
14467 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
14468 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
14469 return unify_type_mismatch (explain_p
, args
[i
],
14470 TREE_VALUE (sarg
));
14477 /* Adjust types before performing type deduction, as described in
14478 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14479 sections are symmetric. PARM is the type of a function parameter
14480 or the return type of the conversion function. ARG is the type of
14481 the argument passed to the call, or the type of the value
14482 initialized with the result of the conversion function.
14483 ARG_EXPR is the original argument expression, which may be null. */
14486 maybe_adjust_types_for_deduction (unification_kind_t strict
,
14500 /* Swap PARM and ARG throughout the remainder of this
14501 function; the handling is precisely symmetric since PARM
14502 will initialize ARG rather than vice versa. */
14510 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14511 too, but here handle it by stripping the reference from PARM
14512 rather than by adding it to ARG. */
14513 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14514 && TYPE_REF_IS_RVALUE (*parm
)
14515 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14516 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
14517 && TREE_CODE (*arg
) == REFERENCE_TYPE
14518 && !TYPE_REF_IS_RVALUE (*arg
))
14519 *parm
= TREE_TYPE (*parm
);
14520 /* Nothing else to do in this case. */
14524 gcc_unreachable ();
14527 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
14529 /* [temp.deduct.call]
14531 If P is not a reference type:
14533 --If A is an array type, the pointer type produced by the
14534 array-to-pointer standard conversion (_conv.array_) is
14535 used in place of A for type deduction; otherwise,
14537 --If A is a function type, the pointer type produced by
14538 the function-to-pointer standard conversion
14539 (_conv.func_) is used in place of A for type deduction;
14542 --If A is a cv-qualified type, the top level
14543 cv-qualifiers of A's type are ignored for type
14545 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
14546 *arg
= build_pointer_type (TREE_TYPE (*arg
));
14547 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
14548 *arg
= build_pointer_type (*arg
);
14550 *arg
= TYPE_MAIN_VARIANT (*arg
);
14553 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
14554 of the form T&&, where T is a template parameter, and the argument
14555 is an lvalue, T is deduced as A& */
14556 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14557 && TYPE_REF_IS_RVALUE (*parm
)
14558 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14559 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
14560 && (arg_expr
? real_lvalue_p (arg_expr
)
14561 /* try_one_overload doesn't provide an arg_expr, but
14562 functions are always lvalues. */
14563 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
14564 *arg
= build_reference_type (*arg
);
14566 /* [temp.deduct.call]
14568 If P is a cv-qualified type, the top level cv-qualifiers
14569 of P's type are ignored for type deduction. If P is a
14570 reference type, the type referred to by P is used for
14572 *parm
= TYPE_MAIN_VARIANT (*parm
);
14573 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
14575 *parm
= TREE_TYPE (*parm
);
14576 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
14579 /* DR 322. For conversion deduction, remove a reference type on parm
14580 too (which has been swapped into ARG). */
14581 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
14582 *arg
= TREE_TYPE (*arg
);
14587 /* Most parms like fn_type_unification.
14589 If SUBR is 1, we're being called recursively (to unify the
14590 arguments of a function or method parameter of a function
14594 type_unification_real (tree tparms
,
14598 unsigned int xnargs
,
14600 unification_kind_t strict
,
14604 tree parm
, arg
, arg_expr
;
14606 int ntparms
= TREE_VEC_LENGTH (tparms
);
14608 int saw_undeduced
= 0;
14611 unsigned int nargs
;
14614 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
14615 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
14616 gcc_assert (ntparms
> 0);
14618 /* Reset the number of non-defaulted template arguments contained
14620 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
14625 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
| UNIFY_ALLOW_MORE_CV_QUAL
14626 | UNIFY_ALLOW_DERIVED
);
14630 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
14634 sub_strict
= UNIFY_ALLOW_NONE
;
14638 gcc_unreachable ();
14647 while (parms
&& parms
!= void_list_node
14650 parm
= TREE_VALUE (parms
);
14652 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
14653 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
14654 /* For a function parameter pack that occurs at the end of the
14655 parameter-declaration-list, the type A of each remaining
14656 argument of the call is compared with the type P of the
14657 declarator-id of the function parameter pack. */
14660 parms
= TREE_CHAIN (parms
);
14662 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
14663 /* For a function parameter pack that does not occur at the
14664 end of the parameter-declaration-list, the type of the
14665 parameter pack is a non-deduced context. */
14672 if (arg
== error_mark_node
)
14673 return unify_invalid (explain_p
);
14674 if (arg
== unknown_type_node
)
14675 /* We can't deduce anything from this, but we might get all the
14676 template args from other function args. */
14679 /* Conversions will be performed on a function argument that
14680 corresponds with a function parameter that contains only
14681 non-deducible template parameters and explicitly specified
14682 template parameters. */
14683 if (!uses_template_parms (parm
))
14688 type
= TREE_TYPE (arg
);
14692 if (same_type_p (parm
, type
))
14694 if (strict
!= DEDUCE_EXACT
14695 && can_convert_arg (parm
, type
, TYPE_P (arg
) ? NULL_TREE
: arg
,
14699 if (strict
== DEDUCE_EXACT
)
14700 return unify_type_mismatch (explain_p
, parm
, arg
);
14702 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
14707 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
14708 if (type_unknown_p (arg
))
14710 /* [temp.deduct.type]
14712 A template-argument can be deduced from a pointer to
14713 function or pointer to member function argument if
14714 the set of overloaded functions does not contain
14715 function templates and at most one of a set of
14716 overloaded functions provides a unique match. */
14717 if (resolve_overloaded_unification
14718 (tparms
, targs
, parm
, arg
, strict
, sub_strict
, explain_p
))
14721 return unify_overload_resolution_failure (explain_p
, arg
);
14724 arg
= unlowered_expr_type (arg
);
14725 if (arg
== error_mark_node
)
14726 return unify_invalid (explain_p
);
14730 int arg_strict
= sub_strict
;
14733 arg_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
,
14736 if (arg
== init_list_type_node
&& arg_expr
)
14738 if (unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
))
14739 /* If unification failed, the recursive call will have updated
14740 UI appropriately. */
14747 && parms
!= void_list_node
14748 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
14750 /* Unify the remaining arguments with the pack expansion type. */
14752 tree parmvec
= make_tree_vec (1);
14754 /* Allocate a TREE_VEC and copy in all of the arguments */
14755 argvec
= make_tree_vec (nargs
- ia
);
14756 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
14757 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
14759 /* Copy the parameter into parmvec. */
14760 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
14761 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
14762 /*call_args_p=*/true, /*subr=*/subr
, explain_p
))
14765 /* Advance to the end of the list of parameters. */
14766 parms
= TREE_CHAIN (parms
);
14769 /* Fail if we've reached the end of the parm list, and more args
14770 are present, and the parm list isn't variadic. */
14771 if (ia
< nargs
&& parms
== void_list_node
)
14772 return unify_too_many_arguments (explain_p
, nargs
, ia
);
14773 /* Fail if parms are left and they don't have default values. */
14774 if (parms
&& parms
!= void_list_node
14775 && TREE_PURPOSE (parms
) == NULL_TREE
)
14777 unsigned int count
= nargs
;
14779 while (p
&& p
!= void_list_node
)
14782 p
= TREE_CHAIN (p
);
14784 return unify_too_few_arguments (explain_p
, ia
, count
);
14789 /* Check to see if we need another pass before we start clearing
14790 ARGUMENT_PACK_INCOMPLETE_P. */
14791 for (i
= 0; i
< ntparms
; i
++)
14793 tree targ
= TREE_VEC_ELT (targs
, i
);
14794 tree tparm
= TREE_VEC_ELT (tparms
, i
);
14796 if (targ
|| tparm
== error_mark_node
)
14798 tparm
= TREE_VALUE (tparm
);
14800 /* If this is an undeduced nontype parameter that depends on
14801 a type parameter, try another pass; its type may have been
14802 deduced from a later argument than the one from which
14803 this parameter can be deduced. */
14804 if (TREE_CODE (tparm
) == PARM_DECL
14805 && uses_template_parms (TREE_TYPE (tparm
))
14806 && !saw_undeduced
++)
14810 for (i
= 0; i
< ntparms
; i
++)
14812 tree targ
= TREE_VEC_ELT (targs
, i
);
14813 tree tparm
= TREE_VEC_ELT (tparms
, i
);
14815 /* Clear the "incomplete" flags on all argument packs now so that
14816 substituting them into later default arguments works. */
14817 if (targ
&& ARGUMENT_PACK_P (targ
))
14819 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
14820 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
14823 if (targ
|| tparm
== error_mark_node
)
14825 tparm
= TREE_VALUE (tparm
);
14827 /* Core issue #226 (C++0x) [temp.deduct]:
14829 If a template argument has not been deduced, its
14830 default template argument, if any, is used.
14832 When we are in C++98 mode, TREE_PURPOSE will either
14833 be NULL_TREE or ERROR_MARK_NODE, so we do not need
14834 to explicitly check cxx_dialect here. */
14835 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
14837 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14838 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
14839 arg
= tsubst_template_arg (arg
, targs
, tf_none
, NULL_TREE
);
14840 arg
= convert_template_argument (parm
, arg
, targs
,
14842 ? tf_warning_or_error
14845 if (arg
== error_mark_node
)
14849 TREE_VEC_ELT (targs
, i
) = arg
;
14850 /* The position of the first default template argument,
14851 is also the number of non-defaulted arguments in TARGS.
14853 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
14854 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
14859 /* If the type parameter is a parameter pack, then it will
14860 be deduced to an empty parameter pack. */
14861 if (template_parameter_pack_p (tparm
))
14865 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
14867 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
14868 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
14869 TREE_CONSTANT (arg
) = 1;
14872 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
14874 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
14876 TREE_VEC_ELT (targs
, i
) = arg
;
14880 return unify_parameter_deduction_failure (explain_p
, tparm
);
14883 #ifdef ENABLE_CHECKING
14884 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
14885 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
14888 return unify_success (explain_p
);
14891 /* Subroutine of type_unification_real. Args are like the variables
14892 at the call site. ARG is an overloaded function (or template-id);
14893 we try deducing template args from each of the overloads, and if
14894 only one succeeds, we go with that. Modifies TARGS and returns
14895 true on success. */
14898 resolve_overloaded_unification (tree tparms
,
14902 unification_kind_t strict
,
14906 tree tempargs
= copy_node (targs
);
14908 tree goodfn
= NULL_TREE
;
14911 if (TREE_CODE (arg
) == ADDR_EXPR
)
14913 arg
= TREE_OPERAND (arg
, 0);
14919 if (TREE_CODE (arg
) == COMPONENT_REF
)
14920 /* Handle `&x' where `x' is some static or non-static member
14922 arg
= TREE_OPERAND (arg
, 1);
14924 if (TREE_CODE (arg
) == OFFSET_REF
)
14925 arg
= TREE_OPERAND (arg
, 1);
14927 /* Strip baselink information. */
14928 if (BASELINK_P (arg
))
14929 arg
= BASELINK_FUNCTIONS (arg
);
14931 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
14933 /* If we got some explicit template args, we need to plug them into
14934 the affected templates before we try to unify, in case the
14935 explicit args will completely resolve the templates in question. */
14938 tree expl_subargs
= TREE_OPERAND (arg
, 1);
14939 arg
= TREE_OPERAND (arg
, 0);
14941 for (; arg
; arg
= OVL_NEXT (arg
))
14943 tree fn
= OVL_CURRENT (arg
);
14944 tree subargs
, elem
;
14946 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
14949 ++processing_template_decl
;
14950 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
14951 expl_subargs
, /*check_ret=*/false);
14952 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
14954 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
14955 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
14956 elem
, strict
, sub_strict
, addr_p
, explain_p
)
14957 && (!goodfn
|| !decls_match (goodfn
, elem
)))
14965 --processing_template_decl
;
14967 /* If no templates (or more than one) are fully resolved by the
14968 explicit arguments, this template-id is a non-deduced context; it
14969 could still be OK if we deduce all template arguments for the
14970 enclosing call through other arguments. */
14974 else if (TREE_CODE (arg
) != OVERLOAD
14975 && TREE_CODE (arg
) != FUNCTION_DECL
)
14976 /* If ARG is, for example, "(0, &f)" then its type will be unknown
14977 -- but the deduction does not succeed because the expression is
14978 not just the function on its own. */
14981 for (; arg
; arg
= OVL_NEXT (arg
))
14982 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
14983 TREE_TYPE (OVL_CURRENT (arg
)),
14984 strict
, sub_strict
, addr_p
, explain_p
)
14985 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
14987 goodfn
= OVL_CURRENT (arg
);
14991 /* [temp.deduct.type] A template-argument can be deduced from a pointer
14992 to function or pointer to member function argument if the set of
14993 overloaded functions does not contain function templates and at most
14994 one of a set of overloaded functions provides a unique match.
14996 So if we found multiple possibilities, we return success but don't
14997 deduce anything. */
15001 int i
= TREE_VEC_LENGTH (targs
);
15003 if (TREE_VEC_ELT (tempargs
, i
))
15004 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15012 /* Core DR 115: In contexts where deduction is done and fails, or in
15013 contexts where deduction is not done, if a template argument list is
15014 specified and it, along with any default template arguments, identifies
15015 a single function template specialization, then the template-id is an
15016 lvalue for the function template specialization. */
15019 resolve_nondeduced_context (tree orig_expr
)
15021 tree expr
, offset
, baselink
;
15024 if (!type_unknown_p (orig_expr
))
15029 offset
= NULL_TREE
;
15030 baselink
= NULL_TREE
;
15032 if (TREE_CODE (expr
) == ADDR_EXPR
)
15034 expr
= TREE_OPERAND (expr
, 0);
15037 if (TREE_CODE (expr
) == OFFSET_REF
)
15040 expr
= TREE_OPERAND (expr
, 1);
15042 if (TREE_CODE (expr
) == BASELINK
)
15045 expr
= BASELINK_FUNCTIONS (expr
);
15048 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15051 tree goodfn
= NULL_TREE
;
15053 /* If we got some explicit template args, we need to plug them into
15054 the affected templates before we try to unify, in case the
15055 explicit args will completely resolve the templates in question. */
15057 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15058 tree arg
= TREE_OPERAND (expr
, 0);
15059 tree badfn
= NULL_TREE
;
15060 tree badargs
= NULL_TREE
;
15062 for (; arg
; arg
= OVL_NEXT (arg
))
15064 tree fn
= OVL_CURRENT (arg
);
15065 tree subargs
, elem
;
15067 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15070 ++processing_template_decl
;
15071 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
15072 expl_subargs
, /*check_ret=*/false);
15073 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
15075 elem
= instantiate_template (fn
, subargs
, tf_none
);
15076 if (elem
== error_mark_node
)
15081 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15087 --processing_template_decl
;
15091 mark_used (goodfn
);
15094 expr
= build_baselink (BASELINK_BINFO (baselink
),
15095 BASELINK_ACCESS_BINFO (baselink
),
15096 expr
, BASELINK_OPTYPE (baselink
));
15100 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15101 expr
= build_offset_ref (base
, expr
, addr
);
15104 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15107 else if (good
== 0 && badargs
)
15108 /* There were no good options and at least one bad one, so let the
15109 user know what the problem is. */
15110 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15115 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15116 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15117 different overloads deduce different arguments for a given parm.
15118 ADDR_P is true if the expression for which deduction is being
15119 performed was of the form "& fn" rather than simply "fn".
15121 Returns 1 on success. */
15124 try_one_overload (tree tparms
,
15129 unification_kind_t strict
,
15138 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15139 to function or pointer to member function argument if the set of
15140 overloaded functions does not contain function templates and at most
15141 one of a set of overloaded functions provides a unique match.
15143 So if this is a template, just return success. */
15145 if (uses_template_parms (arg
))
15148 if (TREE_CODE (arg
) == METHOD_TYPE
)
15149 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15151 arg
= build_pointer_type (arg
);
15153 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15155 /* We don't copy orig_targs for this because if we have already deduced
15156 some template args from previous args, unify would complain when we
15157 try to deduce a template parameter for the same argument, even though
15158 there isn't really a conflict. */
15159 nargs
= TREE_VEC_LENGTH (targs
);
15160 tempargs
= make_tree_vec (nargs
);
15162 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15165 /* First make sure we didn't deduce anything that conflicts with
15166 explicitly specified args. */
15167 for (i
= nargs
; i
--; )
15169 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15170 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15174 else if (uses_template_parms (elt
))
15175 /* Since we're unifying against ourselves, we will fill in
15176 template args used in the function parm list with our own
15177 template parms. Discard them. */
15178 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15179 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15183 for (i
= nargs
; i
--; )
15185 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15188 TREE_VEC_ELT (targs
, i
) = elt
;
15194 /* PARM is a template class (perhaps with unbound template
15195 parameters). ARG is a fully instantiated type. If ARG can be
15196 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15197 TARGS are as for unify. */
15200 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15203 tree copy_of_targs
;
15205 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15206 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15207 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15210 /* We need to make a new template argument vector for the call to
15211 unify. If we used TARGS, we'd clutter it up with the result of
15212 the attempted unification, even if this class didn't work out.
15213 We also don't want to commit ourselves to all the unifications
15214 we've already done, since unification is supposed to be done on
15215 an argument-by-argument basis. In other words, consider the
15216 following pathological case:
15218 template <int I, int J, int K>
15221 template <int I, int J>
15222 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15224 template <int I, int J, int K>
15225 void f(S<I, J, K>, S<I, I, I>);
15234 Now, by the time we consider the unification involving `s2', we
15235 already know that we must have `f<0, 0, 0>'. But, even though
15236 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15237 because there are two ways to unify base classes of S<0, 1, 2>
15238 with S<I, I, I>. If we kept the already deduced knowledge, we
15239 would reject the possibility I=1. */
15240 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15242 /* If unification failed, we're done. */
15243 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15244 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15250 /* Given a template type PARM and a class type ARG, find the unique
15251 base type in ARG that is an instance of PARM. We do not examine
15252 ARG itself; only its base-classes. If there is not exactly one
15253 appropriate base class, return NULL_TREE. PARM may be the type of
15254 a partial specialization, as well as a plain template type. Used
15257 static enum template_base_result
15258 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15259 bool explain_p
, tree
*result
)
15261 tree rval
= NULL_TREE
;
15264 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15266 binfo
= TYPE_BINFO (complete_type (arg
));
15269 /* The type could not be completed. */
15270 *result
= NULL_TREE
;
15271 return tbr_incomplete_type
;
15274 /* Walk in inheritance graph order. The search order is not
15275 important, and this avoids multiple walks of virtual bases. */
15276 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15278 tree r
= try_class_unification (tparms
, targs
, parm
,
15279 BINFO_TYPE (binfo
), explain_p
);
15283 /* If there is more than one satisfactory baseclass, then:
15287 If they yield more than one possible deduced A, the type
15291 if (rval
&& !same_type_p (r
, rval
))
15293 *result
= NULL_TREE
;
15294 return tbr_ambiguous_baseclass
;
15302 return tbr_success
;
15305 /* Returns the level of DECL, which declares a template parameter. */
15308 template_decl_level (tree decl
)
15310 switch (TREE_CODE (decl
))
15313 case TEMPLATE_DECL
:
15314 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
15317 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
15320 gcc_unreachable ();
15325 /* Decide whether ARG can be unified with PARM, considering only the
15326 cv-qualifiers of each type, given STRICT as documented for unify.
15327 Returns nonzero iff the unification is OK on that basis. */
15330 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
15332 int arg_quals
= cp_type_quals (arg
);
15333 int parm_quals
= cp_type_quals (parm
);
15335 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15336 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15338 /* Although a CVR qualifier is ignored when being applied to a
15339 substituted template parameter ([8.3.2]/1 for example), that
15340 does not allow us to unify "const T" with "int&" because both
15341 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15342 It is ok when we're allowing additional CV qualifiers
15343 at the outer level [14.8.2.1]/3,1st bullet. */
15344 if ((TREE_CODE (arg
) == REFERENCE_TYPE
15345 || TREE_CODE (arg
) == FUNCTION_TYPE
15346 || TREE_CODE (arg
) == METHOD_TYPE
)
15347 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
15350 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
15351 && (parm_quals
& TYPE_QUAL_RESTRICT
))
15355 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15356 && (arg_quals
& parm_quals
) != parm_quals
)
15359 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
15360 && (parm_quals
& arg_quals
) != arg_quals
)
15366 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15368 template_parm_level_and_index (tree parm
, int* level
, int* index
)
15370 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15371 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15372 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15374 *index
= TEMPLATE_TYPE_IDX (parm
);
15375 *level
= TEMPLATE_TYPE_LEVEL (parm
);
15379 *index
= TEMPLATE_PARM_IDX (parm
);
15380 *level
= TEMPLATE_PARM_LEVEL (parm
);
15384 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15386 if (unify (TP, TA, P, A, S, EP)) \
15390 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15391 expansion at the end of PACKED_PARMS. Returns 0 if the type
15392 deduction succeeds, 1 otherwise. STRICT is the same as in
15393 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15394 call argument list. We'll need to adjust the arguments to make them
15395 types. SUBR tells us if this is from a recursive call to
15396 type_unification_real. */
15398 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
15399 tree packed_args
, int strict
, bool call_args_p
,
15400 bool subr
, bool explain_p
)
15403 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
15404 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
15405 tree pack
, packs
= NULL_TREE
;
15406 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
15407 int len
= TREE_VEC_LENGTH (packed_args
);
15409 /* Determine the parameter packs we will be deducing from the
15410 pattern, and record their current deductions. */
15411 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
15412 pack
; pack
= TREE_CHAIN (pack
))
15414 tree parm_pack
= TREE_VALUE (pack
);
15417 /* Determine the index and level of this parameter pack. */
15418 template_parm_level_and_index (parm_pack
, &level
, &idx
);
15420 /* Keep track of the parameter packs and their corresponding
15422 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
15423 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
15426 /* Loop through all of the arguments that have not yet been
15427 unified and unify each with the pattern. */
15428 for (i
= start
; i
< len
; i
++)
15430 tree parm
= pattern
;
15432 /* For each parameter pack, clear out the deduced value so that
15433 we can deduce it again. */
15434 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15437 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15439 TMPL_ARG (targs
, level
, idx
) = NULL_TREE
;
15442 /* Unify the pattern with the current argument. */
15444 tree arg
= TREE_VEC_ELT (packed_args
, i
);
15445 tree arg_expr
= NULL_TREE
;
15446 int arg_strict
= strict
;
15452 /* This mirrors what we do in type_unification_real. */
15456 sub_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15457 | UNIFY_ALLOW_MORE_CV_QUAL
15458 | UNIFY_ALLOW_DERIVED
);
15462 sub_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15466 sub_strict
= UNIFY_ALLOW_NONE
;
15470 gcc_unreachable ();
15475 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15476 if (type_unknown_p (arg
))
15478 /* [temp.deduct.type] A template-argument can be
15479 deduced from a pointer to function or pointer
15480 to member function argument if the set of
15481 overloaded functions does not contain function
15482 templates and at most one of a set of
15483 overloaded functions provides a unique
15486 if (resolve_overloaded_unification
15487 (tparms
, targs
, parm
, arg
,
15488 (unification_kind_t
) strict
,
15489 sub_strict
, explain_p
))
15491 return unify_overload_resolution_failure (explain_p
, arg
);
15495 arg
= unlowered_expr_type (arg
);
15496 if (arg
== error_mark_node
)
15497 return unify_invalid (explain_p
);
15500 arg_strict
= sub_strict
;
15504 maybe_adjust_types_for_deduction ((unification_kind_t
) strict
,
15505 &parm
, &arg
, arg_expr
);
15508 /* For deduction from an init-list we need the actual list. */
15509 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15511 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm
, arg
, arg_strict
,
15516 /* For each parameter pack, collect the deduced value. */
15517 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15520 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15522 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
15523 TMPL_ARG (targs
, level
, idx
);
15527 /* Verify that the results of unification with the parameter packs
15528 produce results consistent with what we've seen before, and make
15529 the deduced argument packs available. */
15530 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15532 tree old_pack
= TREE_VALUE (pack
);
15533 tree new_args
= TREE_TYPE (pack
);
15534 int i
, len
= TREE_VEC_LENGTH (new_args
);
15536 bool nondeduced_p
= false;
15538 /* By default keep the original deduced argument pack.
15539 If necessary, more specific code is going to update the
15540 resulting deduced argument later down in this function. */
15541 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15542 TMPL_ARG (targs
, level
, idx
) = old_pack
;
15544 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
15545 actually deduce anything. */
15546 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
15547 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
15548 nondeduced_p
= true;
15552 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
15554 /* Prepend the explicit arguments onto NEW_ARGS. */
15555 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
15556 tree old_args
= new_args
;
15557 int i
, explicit_len
= TREE_VEC_LENGTH (explicit_args
);
15558 int len
= explicit_len
+ TREE_VEC_LENGTH (old_args
);
15560 /* Copy the explicit arguments. */
15561 new_args
= make_tree_vec (len
);
15562 for (i
= 0; i
< explicit_len
; i
++)
15563 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (explicit_args
, i
);
15565 /* Copy the deduced arguments. */
15566 for (; i
< len
; i
++)
15567 TREE_VEC_ELT (new_args
, i
) =
15568 TREE_VEC_ELT (old_args
, i
- explicit_len
);
15574 /* Build the deduced *_ARGUMENT_PACK. */
15575 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
15577 result
= make_node (NONTYPE_ARGUMENT_PACK
);
15578 TREE_TYPE (result
) =
15579 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
15580 TREE_CONSTANT (result
) = 1;
15583 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15585 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
15587 /* Note the deduced argument packs for this parameter
15589 TMPL_ARG (targs
, level
, idx
) = result
;
15591 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
15592 && (ARGUMENT_PACK_ARGS (old_pack
)
15593 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
15595 /* We only had the explicitly-provided arguments before, but
15596 now we have a complete set of arguments. */
15597 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
15599 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
15600 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
15601 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
15605 tree bad_old_arg
, bad_new_arg
;
15606 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
15608 if (!comp_template_args_with_info (old_args
, new_args
,
15609 &bad_old_arg
, &bad_new_arg
))
15610 /* Inconsistent unification of this parameter pack. */
15611 return unify_parameter_pack_inconsistent (explain_p
,
15617 return unify_success (explain_p
);
15620 /* Deduce the value of template parameters. TPARMS is the (innermost)
15621 set of template parameters to a template. TARGS is the bindings
15622 for those template parameters, as determined thus far; TARGS may
15623 include template arguments for outer levels of template parameters
15624 as well. PARM is a parameter to a template function, or a
15625 subcomponent of that parameter; ARG is the corresponding argument.
15626 This function attempts to match PARM with ARG in a manner
15627 consistent with the existing assignments in TARGS. If more values
15628 are deduced, then TARGS is updated.
15630 Returns 0 if the type deduction succeeds, 1 otherwise. The
15631 parameter STRICT is a bitwise or of the following flags:
15634 Require an exact match between PARM and ARG.
15635 UNIFY_ALLOW_MORE_CV_QUAL:
15636 Allow the deduced ARG to be more cv-qualified (by qualification
15637 conversion) than ARG.
15638 UNIFY_ALLOW_LESS_CV_QUAL:
15639 Allow the deduced ARG to be less cv-qualified than ARG.
15640 UNIFY_ALLOW_DERIVED:
15641 Allow the deduced ARG to be a template base class of ARG,
15642 or a pointer to a template base class of the type pointed to by
15644 UNIFY_ALLOW_INTEGER:
15645 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
15646 case for more information.
15647 UNIFY_ALLOW_OUTER_LEVEL:
15648 This is the outermost level of a deduction. Used to determine validity
15649 of qualification conversions. A valid qualification conversion must
15650 have const qualified pointers leading up to the inner type which
15651 requires additional CV quals, except at the outer level, where const
15652 is not required [conv.qual]. It would be normal to set this flag in
15653 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
15654 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
15655 This is the outermost level of a deduction, and PARM can be more CV
15656 qualified at this point.
15657 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
15658 This is the outermost level of a deduction, and PARM can be less CV
15659 qualified at this point. */
15662 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
15668 int strict_in
= strict
;
15670 /* I don't think this will do the right thing with respect to types.
15671 But the only case I've seen it in so far has been array bounds, where
15672 signedness is the only information lost, and I think that will be
15674 while (TREE_CODE (parm
) == NOP_EXPR
)
15675 parm
= TREE_OPERAND (parm
, 0);
15677 if (arg
== error_mark_node
)
15678 return unify_invalid (explain_p
);
15679 if (arg
== unknown_type_node
15680 || arg
== init_list_type_node
)
15681 /* We can't deduce anything from this, but we might get all the
15682 template args from other function args. */
15683 return unify_success (explain_p
);
15685 /* If PARM uses template parameters, then we can't bail out here,
15686 even if ARG == PARM, since we won't record unifications for the
15687 template parameters. We might need them if we're trying to
15688 figure out which of two things is more specialized. */
15689 if (arg
== parm
&& !uses_template_parms (parm
))
15690 return unify_success (explain_p
);
15692 /* Handle init lists early, so the rest of the function can assume
15693 we're dealing with a type. */
15694 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
15698 tree orig_parm
= parm
;
15700 /* Replace T with std::initializer_list<T> for deduction. */
15701 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15702 && flag_deduce_init_list
)
15703 parm
= listify (parm
);
15705 if (!is_std_init_list (parm
))
15706 /* We can only deduce from an initializer list argument if the
15707 parameter is std::initializer_list; otherwise this is a
15708 non-deduced context. */
15709 return unify_success (explain_p
);
15711 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
15713 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
15715 int elt_strict
= strict
;
15717 if (elt
== error_mark_node
)
15718 return unify_invalid (explain_p
);
15720 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
15722 tree type
= TREE_TYPE (elt
);
15723 /* It should only be possible to get here for a call. */
15724 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
15725 elt_strict
|= maybe_adjust_types_for_deduction
15726 (DEDUCE_CALL
, &elttype
, &type
, elt
);
15730 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
15734 /* If the std::initializer_list<T> deduction worked, replace the
15735 deduced A with std::initializer_list<A>. */
15736 if (orig_parm
!= parm
)
15738 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
15739 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15740 targ
= listify (targ
);
15741 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
15743 return unify_success (explain_p
);
15746 /* Immediately reject some pairs that won't unify because of
15747 cv-qualification mismatches. */
15748 if (TREE_CODE (arg
) == TREE_CODE (parm
)
15750 /* It is the elements of the array which hold the cv quals of an array
15751 type, and the elements might be template type parms. We'll check
15752 when we recurse. */
15753 && TREE_CODE (arg
) != ARRAY_TYPE
15754 /* We check the cv-qualifiers when unifying with template type
15755 parameters below. We want to allow ARG `const T' to unify with
15756 PARM `T' for example, when computing which of two templates
15757 is more specialized, for example. */
15758 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
15759 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
15760 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
15762 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
15763 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
15764 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
15765 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
15766 strict
&= ~UNIFY_ALLOW_DERIVED
;
15767 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15768 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
15770 switch (TREE_CODE (parm
))
15772 case TYPENAME_TYPE
:
15774 case UNBOUND_CLASS_TEMPLATE
:
15775 /* In a type which contains a nested-name-specifier, template
15776 argument values cannot be deduced for template parameters used
15777 within the nested-name-specifier. */
15778 return unify_success (explain_p
);
15780 case TEMPLATE_TYPE_PARM
:
15781 case TEMPLATE_TEMPLATE_PARM
:
15782 case BOUND_TEMPLATE_TEMPLATE_PARM
:
15783 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
15784 if (tparm
== error_mark_node
)
15785 return unify_invalid (explain_p
);
15787 if (TEMPLATE_TYPE_LEVEL (parm
)
15788 != template_decl_level (tparm
))
15789 /* The PARM is not one we're trying to unify. Just check
15790 to see if it matches ARG. */
15792 if (TREE_CODE (arg
) == TREE_CODE (parm
)
15793 && same_type_p (parm
, arg
))
15794 return unify_success (explain_p
);
15796 return unify_type_mismatch (explain_p
, parm
, arg
);
15798 idx
= TEMPLATE_TYPE_IDX (parm
);
15799 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15800 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
15802 /* Check for mixed types and values. */
15803 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15804 && TREE_CODE (tparm
) != TYPE_DECL
)
15805 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15806 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
15807 gcc_unreachable ();
15809 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15811 /* ARG must be constructed from a template class or a template
15812 template parameter. */
15813 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
15814 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
15815 return unify_template_deduction_failure (explain_p
, parm
, arg
);
15818 tree parmvec
= TYPE_TI_ARGS (parm
);
15819 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
15820 tree full_argvec
= add_to_template_args (targs
, argvec
);
15822 = DECL_INNERMOST_TEMPLATE_PARMS
15823 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
15825 int parm_variadic_p
= 0;
15827 /* The resolution to DR150 makes clear that default
15828 arguments for an N-argument may not be used to bind T
15829 to a template template parameter with fewer than N
15830 parameters. It is not safe to permit the binding of
15831 default arguments as an extension, as that may change
15832 the meaning of a conforming program. Consider:
15834 struct Dense { static const unsigned int dim = 1; };
15836 template <template <typename> class View,
15838 void operator+(float, View<Block> const&);
15840 template <typename Block,
15841 unsigned int Dim = Block::dim>
15842 struct Lvalue_proxy { operator float() const; };
15846 Lvalue_proxy<Dense> p;
15851 Here, if Lvalue_proxy is permitted to bind to View, then
15852 the global operator+ will be used; if they are not, the
15853 Lvalue_proxy will be converted to float. */
15854 if (coerce_template_parms (parm_parms
,
15856 TYPE_TI_TEMPLATE (parm
),
15858 ? tf_warning_or_error
15860 /*require_all_args=*/true,
15861 /*use_default_args=*/false)
15862 == error_mark_node
)
15865 /* Deduce arguments T, i from TT<T> or TT<i>.
15866 We check each element of PARMVEC and ARGVEC individually
15867 rather than the whole TREE_VEC since they can have
15868 different number of elements. */
15870 parmvec
= expand_template_argument_pack (parmvec
);
15871 argvec
= expand_template_argument_pack (argvec
);
15873 len
= TREE_VEC_LENGTH (parmvec
);
15875 /* Check if the parameters end in a pack, making them
15878 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
15879 parm_variadic_p
= 1;
15881 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
15882 return unify_too_few_arguments (explain_p
,
15883 TREE_VEC_LENGTH (argvec
), len
);
15885 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
15887 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
15888 TREE_VEC_ELT (parmvec
, i
),
15889 TREE_VEC_ELT (argvec
, i
),
15890 UNIFY_ALLOW_NONE
, explain_p
);
15893 if (parm_variadic_p
15894 && unify_pack_expansion (tparms
, targs
,
15897 /*call_args_p=*/false,
15898 /*subr=*/false, explain_p
))
15901 arg
= TYPE_TI_TEMPLATE (arg
);
15903 /* Fall through to deduce template name. */
15906 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15907 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15909 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
15911 /* Simple cases: Value already set, does match or doesn't. */
15912 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
15913 return unify_success (explain_p
);
15915 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
15919 /* If PARM is `const T' and ARG is only `int', we don't have
15920 a match unless we are allowing additional qualification.
15921 If ARG is `const int' and PARM is just `T' that's OK;
15922 that binds `const int' to `T'. */
15923 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
15925 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
15927 /* Consider the case where ARG is `const volatile int' and
15928 PARM is `const T'. Then, T should be `volatile int'. */
15929 arg
= cp_build_qualified_type_real
15930 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
15931 if (arg
== error_mark_node
)
15932 return unify_invalid (explain_p
);
15934 /* Simple cases: Value already set, does match or doesn't. */
15935 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
15936 return unify_success (explain_p
);
15938 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
15940 /* Make sure that ARG is not a variable-sized array. (Note
15941 that were talking about variable-sized arrays (like
15942 `int[n]'), rather than arrays of unknown size (like
15943 `int[]').) We'll get very confused by such a type since
15944 the bound of the array is not constant, and therefore
15945 not mangleable. Besides, such types are not allowed in
15946 ISO C++, so we can do as we please here. We do allow
15947 them for 'auto' deduction, since that isn't ABI-exposed. */
15948 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
15949 return unify_vla_arg (explain_p
, arg
);
15951 /* Strip typedefs as in convert_template_argument. */
15952 arg
= canonicalize_type_argument (arg
, tf_none
);
15955 /* If ARG is a parameter pack or an expansion, we cannot unify
15956 against it unless PARM is also a parameter pack. */
15957 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
15958 && !template_parameter_pack_p (parm
))
15959 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
15961 /* If the argument deduction results is a METHOD_TYPE,
15962 then there is a problem.
15963 METHOD_TYPE doesn't map to any real C++ type the result of
15964 the deduction can not be of that type. */
15965 if (TREE_CODE (arg
) == METHOD_TYPE
)
15966 return unify_method_type_error (explain_p
, arg
);
15968 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
15969 return unify_success (explain_p
);
15971 case TEMPLATE_PARM_INDEX
:
15972 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
15973 if (tparm
== error_mark_node
)
15974 return unify_invalid (explain_p
);
15976 if (TEMPLATE_PARM_LEVEL (parm
)
15977 != template_decl_level (tparm
))
15979 /* The PARM is not one we're trying to unify. Just check
15980 to see if it matches ARG. */
15981 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
15982 && cp_tree_equal (parm
, arg
));
15984 unify_expression_unequal (explain_p
, parm
, arg
);
15988 idx
= TEMPLATE_PARM_IDX (parm
);
15989 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
15993 int x
= !cp_tree_equal (targ
, arg
);
15995 unify_inconsistency (explain_p
, parm
, targ
, arg
);
15999 /* [temp.deduct.type] If, in the declaration of a function template
16000 with a non-type template-parameter, the non-type
16001 template-parameter is used in an expression in the function
16002 parameter-list and, if the corresponding template-argument is
16003 deduced, the template-argument type shall match the type of the
16004 template-parameter exactly, except that a template-argument
16005 deduced from an array bound may be of any integral type.
16006 The non-type parameter might use already deduced type parameters. */
16007 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16008 if (!TREE_TYPE (arg
))
16009 /* Template-parameter dependent expression. Just accept it for now.
16010 It will later be processed in convert_template_argument. */
16012 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16014 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16015 && (TREE_CODE (tparm
) == INTEGER_TYPE
16016 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16017 /* Convert the ARG to the type of PARM; the deduced non-type
16018 template argument must exactly match the types of the
16019 corresponding parameter. */
16020 arg
= fold (build_nop (tparm
, arg
));
16021 else if (uses_template_parms (tparm
))
16022 /* We haven't deduced the type of this parameter yet. Try again
16024 return unify_success (explain_p
);
16026 return unify_type_mismatch (explain_p
, tparm
, arg
);
16028 /* If ARG is a parameter pack or an expansion, we cannot unify
16029 against it unless PARM is also a parameter pack. */
16030 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16031 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16032 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16034 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16035 return unify_success (explain_p
);
16039 /* A pointer-to-member constant can be unified only with
16040 another constant. */
16041 if (TREE_CODE (arg
) != PTRMEM_CST
)
16042 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16044 /* Just unify the class member. It would be useless (and possibly
16045 wrong, depending on the strict flags) to unify also
16046 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16047 arg refer to the same variable, even if through different
16048 classes. For instance:
16050 struct A { int x; };
16053 Unification of &A::x and &B::x must succeed. */
16054 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16055 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16060 if (TREE_CODE (arg
) != POINTER_TYPE
)
16061 return unify_type_mismatch (explain_p
, parm
, arg
);
16063 /* [temp.deduct.call]
16065 A can be another pointer or pointer to member type that can
16066 be converted to the deduced A via a qualification
16067 conversion (_conv.qual_).
16069 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16070 This will allow for additional cv-qualification of the
16071 pointed-to types if appropriate. */
16073 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16074 /* The derived-to-base conversion only persists through one
16075 level of pointers. */
16076 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16078 return unify (tparms
, targs
, TREE_TYPE (parm
),
16079 TREE_TYPE (arg
), strict
, explain_p
);
16082 case REFERENCE_TYPE
:
16083 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16084 return unify_type_mismatch (explain_p
, parm
, arg
);
16085 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16086 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16089 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16090 return unify_type_mismatch (explain_p
, parm
, arg
);
16091 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16092 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16093 return unify_type_mismatch (explain_p
, parm
, arg
);
16094 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16101 /* Our representation of array types uses "N - 1" as the
16102 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16103 not an integer constant. We cannot unify arbitrarily
16104 complex expressions, so we eliminate the MINUS_EXPRs
16106 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16107 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16110 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16111 parm_max
= TREE_OPERAND (parm_max
, 0);
16113 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16114 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16117 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16118 trying to unify the type of a variable with the type
16119 of a template parameter. For example:
16121 template <unsigned int N>
16122 void f (char (&) [N]);
16129 Here, the type of the ARG will be "int [g(i)]", and
16130 may be a SAVE_EXPR, etc. */
16131 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16132 return unify_vla_arg (explain_p
, arg
);
16133 arg_max
= TREE_OPERAND (arg_max
, 0);
16136 /* If only one of the bounds used a MINUS_EXPR, compensate
16137 by adding one to the other bound. */
16138 if (parm_cst
&& !arg_cst
)
16139 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16143 else if (arg_cst
&& !parm_cst
)
16144 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16149 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16150 UNIFY_ALLOW_INTEGER
, explain_p
);
16152 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16153 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16160 case ENUMERAL_TYPE
:
16162 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16163 return unify_type_mismatch (explain_p
, parm
, arg
);
16165 /* We have already checked cv-qualification at the top of the
16167 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16168 return unify_type_mismatch (explain_p
, parm
, arg
);
16170 /* As far as unification is concerned, this wins. Later checks
16171 will invalidate it if necessary. */
16172 return unify_success (explain_p
);
16174 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16175 /* Type INTEGER_CST can come from ordinary constant template args. */
16177 while (TREE_CODE (arg
) == NOP_EXPR
)
16178 arg
= TREE_OPERAND (arg
, 0);
16180 if (TREE_CODE (arg
) != INTEGER_CST
)
16181 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16182 return (tree_int_cst_equal (parm
, arg
)
16183 ? unify_success (explain_p
)
16184 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16189 if (TREE_CODE (arg
) != TREE_VEC
)
16190 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16191 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
16192 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
),
16193 TREE_VEC_LENGTH (parm
));
16194 for (i
= 0; i
< TREE_VEC_LENGTH (parm
); ++i
)
16195 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16196 TREE_VEC_ELT (parm
, i
),
16197 TREE_VEC_ELT (arg
, i
),
16198 UNIFY_ALLOW_NONE
, explain_p
);
16199 return unify_success (explain_p
);
16204 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16205 return unify_type_mismatch (explain_p
, parm
, arg
);
16207 if (TYPE_PTRMEMFUNC_P (parm
))
16209 if (!TYPE_PTRMEMFUNC_P (arg
))
16210 return unify_type_mismatch (explain_p
, parm
, arg
);
16212 return unify (tparms
, targs
,
16213 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16214 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16215 strict
, explain_p
);
16218 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16220 tree t
= NULL_TREE
;
16222 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16224 /* First, we try to unify the PARM and ARG directly. */
16225 t
= try_class_unification (tparms
, targs
,
16226 parm
, arg
, explain_p
);
16230 /* Fallback to the special case allowed in
16231 [temp.deduct.call]:
16233 If P is a class, and P has the form
16234 template-id, then A can be a derived class of
16235 the deduced A. Likewise, if P is a pointer to
16236 a class of the form template-id, A can be a
16237 pointer to a derived class pointed to by the
16239 enum template_base_result r
;
16240 r
= get_template_base (tparms
, targs
, parm
, arg
,
16244 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16247 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16248 && (CLASSTYPE_TI_TEMPLATE (parm
)
16249 == CLASSTYPE_TI_TEMPLATE (arg
)))
16250 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16251 Then, we should unify `int' and `U'. */
16254 /* There's no chance of unification succeeding. */
16255 return unify_type_mismatch (explain_p
, parm
, arg
);
16257 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16258 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16260 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16261 return unify_type_mismatch (explain_p
, parm
, arg
);
16262 return unify_success (explain_p
);
16265 case FUNCTION_TYPE
:
16267 unsigned int nargs
;
16272 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16273 return unify_type_mismatch (explain_p
, parm
, arg
);
16275 /* CV qualifications for methods can never be deduced, they must
16276 match exactly. We need to check them explicitly here,
16277 because type_unification_real treats them as any other
16278 cv-qualified parameter. */
16279 if (TREE_CODE (parm
) == METHOD_TYPE
16280 && (!check_cv_quals_for_unify
16282 class_of_this_parm (arg
),
16283 class_of_this_parm (parm
))))
16284 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16286 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
16287 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
16289 nargs
= list_length (TYPE_ARG_TYPES (arg
));
16290 args
= XALLOCAVEC (tree
, nargs
);
16291 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
16292 a
!= NULL_TREE
&& a
!= void_list_node
;
16293 a
= TREE_CHAIN (a
), ++i
)
16294 args
[i
] = TREE_VALUE (a
);
16297 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
16298 args
, nargs
, 1, DEDUCE_EXACT
,
16299 LOOKUP_NORMAL
, explain_p
);
16303 /* Unify a pointer to member with a pointer to member function, which
16304 deduces the type of the member as a function type. */
16305 if (TYPE_PTRMEMFUNC_P (arg
))
16310 /* Check top-level cv qualifiers */
16311 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
16312 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16314 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16315 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
16316 UNIFY_ALLOW_NONE
, explain_p
);
16318 /* Determine the type of the function we are unifying against. */
16319 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
16321 build_function_type (TREE_TYPE (method_type
),
16322 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
16324 /* Extract the cv-qualifiers of the member function from the
16325 implicit object parameter and place them on the function
16326 type to be restored later. */
16327 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
16328 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
16331 if (TREE_CODE (arg
) != OFFSET_TYPE
)
16332 return unify_type_mismatch (explain_p
, parm
, arg
);
16333 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16334 TYPE_OFFSET_BASETYPE (arg
),
16335 UNIFY_ALLOW_NONE
, explain_p
);
16336 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16337 strict
, explain_p
);
16340 if (DECL_TEMPLATE_PARM_P (parm
))
16341 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
16342 if (arg
!= integral_constant_value (parm
))
16343 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16344 return unify_success (explain_p
);
16347 case TEMPLATE_DECL
:
16348 /* Matched cases are handled by the ARG == PARM test above. */
16349 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16352 /* A non-type template parameter that is a variable should be a
16353 an integral constant, in which case, it whould have been
16354 folded into its (constant) value. So we should not be getting
16355 a variable here. */
16356 gcc_unreachable ();
16358 case TYPE_ARGUMENT_PACK
:
16359 case NONTYPE_ARGUMENT_PACK
:
16361 tree packed_parms
= ARGUMENT_PACK_ARGS (parm
);
16362 tree packed_args
= ARGUMENT_PACK_ARGS (arg
);
16363 int i
, len
= TREE_VEC_LENGTH (packed_parms
);
16364 int argslen
= TREE_VEC_LENGTH (packed_args
);
16365 int parm_variadic_p
= 0;
16367 for (i
= 0; i
< len
; ++i
)
16369 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms
, i
)))
16372 /* We can unify against something with a trailing
16374 parm_variadic_p
= 1;
16376 /* Since there is something following the pack
16377 expansion, we cannot unify this template argument
16379 return unify_success (explain_p
);
16384 /* If we don't have enough arguments to satisfy the parameters
16385 (not counting the pack expression at the end), or we have
16386 too many arguments for a parameter list that doesn't end in
16387 a pack expression, we can't unify. */
16388 if (argslen
< (len
- parm_variadic_p
))
16389 return unify_too_few_arguments (explain_p
, argslen
, len
);
16390 if (argslen
> len
&& !parm_variadic_p
)
16391 return unify_too_many_arguments (explain_p
, argslen
, len
);
16393 /* Unify all of the parameters that precede the (optional)
16394 pack expression. */
16395 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16397 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16398 TREE_VEC_ELT (packed_parms
, i
),
16399 TREE_VEC_ELT (packed_args
, i
),
16400 strict
, explain_p
);
16403 if (parm_variadic_p
)
16404 return unify_pack_expansion (tparms
, targs
,
16405 packed_parms
, packed_args
,
16406 strict
, /*call_args_p=*/false,
16407 /*subr=*/false, explain_p
);
16408 return unify_success (explain_p
);
16414 case DECLTYPE_TYPE
:
16415 case UNDERLYING_TYPE
:
16416 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16417 or UNDERLYING_TYPE nodes. */
16418 return unify_success (explain_p
);
16421 /* Unification fails if we hit an error node. */
16422 return unify_invalid (explain_p
);
16425 /* An unresolved overload is a nondeduced context. */
16426 if (type_unknown_p (parm
))
16427 return unify_success (explain_p
);
16428 gcc_assert (EXPR_P (parm
));
16430 /* We must be looking at an expression. This can happen with
16434 void foo(S<I>, S<I + 2>);
16436 This is a "nondeduced context":
16440 The nondeduced contexts are:
16442 --A type that is a template-id in which one or more of
16443 the template-arguments is an expression that references
16444 a template-parameter.
16446 In these cases, we assume deduction succeeded, but don't
16447 actually infer any unifications. */
16449 if (!uses_template_parms (parm
)
16450 && !template_args_equal (parm
, arg
))
16451 return unify_expression_unequal (explain_p
, parm
, arg
);
16453 return unify_success (explain_p
);
16456 #undef RECUR_AND_CHECK_FAILURE
16458 /* Note that DECL can be defined in this translation unit, if
16462 mark_definable (tree decl
)
16465 DECL_NOT_REALLY_EXTERN (decl
) = 1;
16466 FOR_EACH_CLONE (clone
, decl
)
16467 DECL_NOT_REALLY_EXTERN (clone
) = 1;
16470 /* Called if RESULT is explicitly instantiated, or is a member of an
16471 explicitly instantiated class. */
16474 mark_decl_instantiated (tree result
, int extern_p
)
16476 SET_DECL_EXPLICIT_INSTANTIATION (result
);
16478 /* If this entity has already been written out, it's too late to
16479 make any modifications. */
16480 if (TREE_ASM_WRITTEN (result
))
16483 if (TREE_CODE (result
) != FUNCTION_DECL
)
16484 /* The TREE_PUBLIC flag for function declarations will have been
16485 set correctly by tsubst. */
16486 TREE_PUBLIC (result
) = 1;
16488 /* This might have been set by an earlier implicit instantiation. */
16489 DECL_COMDAT (result
) = 0;
16492 DECL_NOT_REALLY_EXTERN (result
) = 0;
16495 mark_definable (result
);
16496 /* Always make artificials weak. */
16497 if (DECL_ARTIFICIAL (result
) && flag_weak
)
16498 comdat_linkage (result
);
16499 /* For WIN32 we also want to put explicit instantiations in
16500 linkonce sections. */
16501 else if (TREE_PUBLIC (result
))
16502 maybe_make_one_only (result
);
16505 /* If EXTERN_P, then this function will not be emitted -- unless
16506 followed by an explicit instantiation, at which point its linkage
16507 will be adjusted. If !EXTERN_P, then this function will be
16508 emitted here. In neither circumstance do we want
16509 import_export_decl to adjust the linkage. */
16510 DECL_INTERFACE_KNOWN (result
) = 1;
16513 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16514 important template arguments. If any are missing, we check whether
16515 they're important by using error_mark_node for substituting into any
16516 args that were used for partial ordering (the ones between ARGS and END)
16517 and seeing if it bubbles up. */
16520 check_undeduced_parms (tree targs
, tree args
, tree end
)
16522 bool found
= false;
16524 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
16525 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
16528 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
16532 for (; args
!= end
; args
= TREE_CHAIN (args
))
16534 tree substed
= tsubst (TREE_VALUE (args
), targs
, tf_none
, NULL_TREE
);
16535 if (substed
== error_mark_node
)
16542 /* Given two function templates PAT1 and PAT2, return:
16544 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
16545 -1 if PAT2 is more specialized than PAT1.
16546 0 if neither is more specialized.
16548 LEN indicates the number of parameters we should consider
16549 (defaulted parameters should not be considered).
16551 The 1998 std underspecified function template partial ordering, and
16552 DR214 addresses the issue. We take pairs of arguments, one from
16553 each of the templates, and deduce them against each other. One of
16554 the templates will be more specialized if all the *other*
16555 template's arguments deduce against its arguments and at least one
16556 of its arguments *does* *not* deduce against the other template's
16557 corresponding argument. Deduction is done as for class templates.
16558 The arguments used in deduction have reference and top level cv
16559 qualifiers removed. Iff both arguments were originally reference
16560 types *and* deduction succeeds in both directions, the template
16561 with the more cv-qualified argument wins for that pairing (if
16562 neither is more cv-qualified, they both are equal). Unlike regular
16563 deduction, after all the arguments have been deduced in this way,
16564 we do *not* verify the deduced template argument values can be
16565 substituted into non-deduced contexts.
16567 The logic can be a bit confusing here, because we look at deduce1 and
16568 targs1 to see if pat2 is at least as specialized, and vice versa; if we
16569 can find template arguments for pat1 to make arg1 look like arg2, that
16570 means that arg2 is at least as specialized as arg1. */
16573 more_specialized_fn (tree pat1
, tree pat2
, int len
)
16575 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
16576 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
16577 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
16578 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
16579 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
16580 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
16581 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
16582 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
16583 tree origs1
, origs2
;
16584 bool lose1
= false;
16585 bool lose2
= false;
16587 /* Remove the this parameter from non-static member functions. If
16588 one is a non-static member function and the other is not a static
16589 member function, remove the first parameter from that function
16590 also. This situation occurs for operator functions where we
16591 locate both a member function (with this pointer) and non-member
16592 operator (with explicit first operand). */
16593 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
16595 len
--; /* LEN is the number of significant arguments for DECL1 */
16596 args1
= TREE_CHAIN (args1
);
16597 if (!DECL_STATIC_FUNCTION_P (decl2
))
16598 args2
= TREE_CHAIN (args2
);
16600 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
16602 args2
= TREE_CHAIN (args2
);
16603 if (!DECL_STATIC_FUNCTION_P (decl1
))
16606 args1
= TREE_CHAIN (args1
);
16610 /* If only one is a conversion operator, they are unordered. */
16611 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
16614 /* Consider the return type for a conversion function */
16615 if (DECL_CONV_FN_P (decl1
))
16617 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
16618 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
16622 processing_template_decl
++;
16628 /* Stop when an ellipsis is seen. */
16629 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
16631 tree arg1
= TREE_VALUE (args1
);
16632 tree arg2
= TREE_VALUE (args2
);
16633 int deduce1
, deduce2
;
16637 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
16638 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16640 /* When both arguments are pack expansions, we need only
16641 unify the patterns themselves. */
16642 arg1
= PACK_EXPANSION_PATTERN (arg1
);
16643 arg2
= PACK_EXPANSION_PATTERN (arg2
);
16645 /* This is the last comparison we need to do. */
16649 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
16651 arg1
= TREE_TYPE (arg1
);
16652 quals1
= cp_type_quals (arg1
);
16655 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
16657 arg2
= TREE_TYPE (arg2
);
16658 quals2
= cp_type_quals (arg2
);
16661 if ((quals1
< 0) != (quals2
< 0))
16663 /* Only of the args is a reference, see if we should apply
16664 array/function pointer decay to it. This is not part of
16665 DR214, but is, IMHO, consistent with the deduction rules
16666 for the function call itself, and with our earlier
16667 implementation of the underspecified partial ordering
16668 rules. (nathan). */
16671 switch (TREE_CODE (arg1
))
16674 arg1
= TREE_TYPE (arg1
);
16676 case FUNCTION_TYPE
:
16677 arg1
= build_pointer_type (arg1
);
16686 switch (TREE_CODE (arg2
))
16689 arg2
= TREE_TYPE (arg2
);
16691 case FUNCTION_TYPE
:
16692 arg2
= build_pointer_type (arg2
);
16701 arg1
= TYPE_MAIN_VARIANT (arg1
);
16702 arg2
= TYPE_MAIN_VARIANT (arg2
);
16704 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
16706 int i
, len2
= list_length (args2
);
16707 tree parmvec
= make_tree_vec (1);
16708 tree argvec
= make_tree_vec (len2
);
16711 /* Setup the parameter vector, which contains only ARG1. */
16712 TREE_VEC_ELT (parmvec
, 0) = arg1
;
16714 /* Setup the argument vector, which contains the remaining
16716 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
16717 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
16719 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
16720 argvec
, UNIFY_ALLOW_NONE
,
16721 /*call_args_p=*/false,
16722 /*subr=*/0, /*explain_p=*/false)
16725 /* We cannot deduce in the other direction, because ARG1 is
16726 a pack expansion but ARG2 is not. */
16729 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16731 int i
, len1
= list_length (args1
);
16732 tree parmvec
= make_tree_vec (1);
16733 tree argvec
= make_tree_vec (len1
);
16736 /* Setup the parameter vector, which contains only ARG1. */
16737 TREE_VEC_ELT (parmvec
, 0) = arg2
;
16739 /* Setup the argument vector, which contains the remaining
16741 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
16742 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
16744 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
16745 argvec
, UNIFY_ALLOW_NONE
,
16746 /*call_args_p=*/false,
16747 /*subr=*/0, /*explain_p=*/false)
16750 /* We cannot deduce in the other direction, because ARG2 is
16751 a pack expansion but ARG1 is not.*/
16757 /* The normal case, where neither argument is a pack
16759 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
16760 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
16762 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
16763 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
16767 /* If we couldn't deduce arguments for tparms1 to make arg1 match
16768 arg2, then arg2 is not as specialized as arg1. */
16774 /* "If, for a given type, deduction succeeds in both directions
16775 (i.e., the types are identical after the transformations above)
16776 and if the type from the argument template is more cv-qualified
16777 than the type from the parameter template (as described above)
16778 that type is considered to be more specialized than the other. If
16779 neither type is more cv-qualified than the other then neither type
16780 is more specialized than the other." */
16782 if (deduce1
&& deduce2
16783 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
16785 if ((quals1
& quals2
) == quals2
)
16787 if ((quals1
& quals2
) == quals1
)
16791 if (lose1
&& lose2
)
16792 /* We've failed to deduce something in either direction.
16793 These must be unordered. */
16796 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
16797 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
16798 /* We have already processed all of the arguments in our
16799 handing of the pack expansion type. */
16802 args1
= TREE_CHAIN (args1
);
16803 args2
= TREE_CHAIN (args2
);
16806 /* "In most cases, all template parameters must have values in order for
16807 deduction to succeed, but for partial ordering purposes a template
16808 parameter may remain without a value provided it is not used in the
16809 types being used for partial ordering."
16811 Thus, if we are missing any of the targs1 we need to substitute into
16812 origs1, then pat2 is not as specialized as pat1. This can happen when
16813 there is a nondeduced context. */
16814 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
16816 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
16819 processing_template_decl
--;
16821 /* All things being equal, if the next argument is a pack expansion
16822 for one function but not for the other, prefer the
16823 non-variadic function. FIXME this is bogus; see c++/41958. */
16825 && args1
&& TREE_VALUE (args1
)
16826 && args2
&& TREE_VALUE (args2
))
16828 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
16829 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
16832 if (lose1
== lose2
)
16840 /* Determine which of two partial specializations is more specialized.
16842 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
16843 to the first partial specialization. The TREE_VALUE is the
16844 innermost set of template parameters for the partial
16845 specialization. PAT2 is similar, but for the second template.
16847 Return 1 if the first partial specialization is more specialized;
16848 -1 if the second is more specialized; 0 if neither is more
16851 See [temp.class.order] for information about determining which of
16852 two templates is more specialized. */
16855 more_specialized_class (tree pat1
, tree pat2
)
16860 bool any_deductions
= false;
16862 tmpl1
= TREE_TYPE (pat1
);
16863 tmpl2
= TREE_TYPE (pat2
);
16865 /* Just like what happens for functions, if we are ordering between
16866 different class template specializations, we may encounter dependent
16867 types in the arguments, and we need our dependency check functions
16868 to behave correctly. */
16869 ++processing_template_decl
;
16870 targs
= get_class_bindings (TREE_VALUE (pat1
),
16871 CLASSTYPE_TI_ARGS (tmpl1
),
16872 CLASSTYPE_TI_ARGS (tmpl2
));
16876 any_deductions
= true;
16879 targs
= get_class_bindings (TREE_VALUE (pat2
),
16880 CLASSTYPE_TI_ARGS (tmpl2
),
16881 CLASSTYPE_TI_ARGS (tmpl1
));
16885 any_deductions
= true;
16887 --processing_template_decl
;
16889 /* In the case of a tie where at least one of the class templates
16890 has a parameter pack at the end, the template with the most
16891 non-packed parameters wins. */
16894 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
16895 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
16897 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
16898 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
16899 int len1
= TREE_VEC_LENGTH (args1
);
16900 int len2
= TREE_VEC_LENGTH (args2
);
16902 /* We don't count the pack expansion at the end. */
16903 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
16905 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
16910 else if (len1
< len2
)
16917 /* Return the template arguments that will produce the function signature
16918 DECL from the function template FN, with the explicit template
16919 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
16920 also match. Return NULL_TREE if no satisfactory arguments could be
16924 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
16926 int ntparms
= DECL_NTPARMS (fn
);
16927 tree targs
= make_tree_vec (ntparms
);
16929 tree decl_arg_types
;
16931 unsigned int nargs
, ix
;
16934 /* Substitute the explicit template arguments into the type of DECL.
16935 The call to fn_type_unification will handle substitution into the
16937 decl_type
= TREE_TYPE (decl
);
16938 if (explicit_args
&& uses_template_parms (decl_type
))
16941 tree converted_args
;
16943 if (DECL_TEMPLATE_INFO (decl
))
16944 tmpl
= DECL_TI_TEMPLATE (decl
);
16946 /* We can get here for some invalid specializations. */
16950 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
16951 explicit_args
, NULL_TREE
,
16953 /*require_all_args=*/false,
16954 /*use_default_args=*/false);
16955 if (converted_args
== error_mark_node
)
16958 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
16959 if (decl_type
== error_mark_node
)
16963 /* Never do unification on the 'this' parameter. */
16964 decl_arg_types
= skip_artificial_parms_for (decl
,
16965 TYPE_ARG_TYPES (decl_type
));
16967 nargs
= list_length (decl_arg_types
);
16968 args
= XALLOCAVEC (tree
, nargs
);
16969 for (arg
= decl_arg_types
, ix
= 0;
16970 arg
!= NULL_TREE
&& arg
!= void_list_node
;
16971 arg
= TREE_CHAIN (arg
), ++ix
)
16972 args
[ix
] = TREE_VALUE (arg
);
16974 if (fn_type_unification (fn
, explicit_args
, targs
,
16976 (check_rettype
|| DECL_CONV_FN_P (fn
)
16977 ? TREE_TYPE (decl_type
) : NULL_TREE
),
16978 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false))
16984 /* Return the innermost template arguments that, when applied to a
16985 template specialization whose innermost template parameters are
16986 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
16989 For example, suppose we have:
16991 template <class T, class U> struct S {};
16992 template <class T> struct S<T*, int> {};
16994 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
16995 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
16996 int}. The resulting vector will be {double}, indicating that `T'
16997 is bound to `double'. */
17000 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
17002 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17004 tree innermost_deduced_args
;
17006 innermost_deduced_args
= make_tree_vec (ntparms
);
17007 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17009 deduced_args
= copy_node (args
);
17010 SET_TMPL_ARGS_LEVEL (deduced_args
,
17011 TMPL_ARGS_DEPTH (deduced_args
),
17012 innermost_deduced_args
);
17015 deduced_args
= innermost_deduced_args
;
17017 if (unify (tparms
, deduced_args
,
17018 INNERMOST_TEMPLATE_ARGS (spec_args
),
17019 INNERMOST_TEMPLATE_ARGS (args
),
17020 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17023 for (i
= 0; i
< ntparms
; ++i
)
17024 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17027 /* Verify that nondeduced template arguments agree with the type
17028 obtained from argument deduction.
17032 struct A { typedef int X; };
17033 template <class T, class U> struct C {};
17034 template <class T> struct C<T, typename T::X> {};
17036 Then with the instantiation `C<A, int>', we can deduce that
17037 `T' is `A' but unify () does not check whether `typename T::X'
17039 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17040 if (spec_args
== error_mark_node
17041 /* We only need to check the innermost arguments; the other
17042 arguments will always agree. */
17043 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17044 INNERMOST_TEMPLATE_ARGS (args
)))
17047 /* Now that we have bindings for all of the template arguments,
17048 ensure that the arguments deduced for the template template
17049 parameters have compatible template parameter lists. See the use
17050 of template_template_parm_bindings_ok_p in fn_type_unification
17051 for more information. */
17052 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17055 return deduced_args
;
17058 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17059 Return the TREE_LIST node with the most specialized template, if
17060 any. If there is no most specialized template, the error_mark_node
17063 Note that this function does not look at, or modify, the
17064 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17065 returned is one of the elements of INSTANTIATIONS, callers may
17066 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17067 and retrieve it from the value returned. */
17070 most_specialized_instantiation (tree templates
)
17074 ++processing_template_decl
;
17077 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17081 if (get_bindings (TREE_VALUE (champ
),
17082 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17083 NULL_TREE
, /*check_ret=*/true))
17086 if (get_bindings (TREE_VALUE (fn
),
17087 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17088 NULL_TREE
, /*check_ret=*/true))
17095 /* Equally specialized, move to next function. If there
17096 is no next function, nothing's most specialized. */
17097 fn
= TREE_CHAIN (fn
);
17105 /* Now verify that champ is better than everything earlier in the
17106 instantiation list. */
17107 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17108 if (get_bindings (TREE_VALUE (champ
),
17109 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17110 NULL_TREE
, /*check_ret=*/true)
17111 || !get_bindings (TREE_VALUE (fn
),
17112 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17113 NULL_TREE
, /*check_ret=*/true))
17119 processing_template_decl
--;
17122 return error_mark_node
;
17127 /* If DECL is a specialization of some template, return the most
17128 general such template. Otherwise, returns NULL_TREE.
17130 For example, given:
17132 template <class T> struct S { template <class U> void f(U); };
17134 if TMPL is `template <class U> void S<int>::f(U)' this will return
17135 the full template. This function will not trace past partial
17136 specializations, however. For example, given in addition:
17138 template <class T> struct S<T*> { template <class U> void f(U); };
17140 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17141 `template <class T> template <class U> S<T*>::f(U)'. */
17144 most_general_template (tree decl
)
17146 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17147 an immediate specialization. */
17148 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17150 if (DECL_TEMPLATE_INFO (decl
)) {
17151 decl
= DECL_TI_TEMPLATE (decl
);
17153 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17154 template friend. */
17155 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17161 /* Look for more and more general templates. */
17162 while (DECL_TEMPLATE_INFO (decl
))
17164 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17165 (See cp-tree.h for details.) */
17166 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17169 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17170 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17173 /* Stop if we run into an explicitly specialized class template. */
17174 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17175 && DECL_CONTEXT (decl
)
17176 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17179 decl
= DECL_TI_TEMPLATE (decl
);
17185 /* Return the most specialized of the class template partial
17186 specializations of TMPL which can produce TYPE, a specialization of
17187 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17188 a _TYPE node corresponding to the partial specialization, while the
17189 TREE_PURPOSE is the set of template arguments that must be
17190 substituted into the TREE_TYPE in order to generate TYPE.
17192 If the choice of partial specialization is ambiguous, a diagnostic
17193 is issued, and the error_mark_node is returned. If there are no
17194 partial specializations of TMPL matching TYPE, then NULL_TREE is
17198 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17200 tree list
= NULL_TREE
;
17206 tree outer_args
= NULL_TREE
;
17208 tmpl
= most_general_template (tmpl
);
17209 args
= CLASSTYPE_TI_ARGS (type
);
17211 /* For determining which partial specialization to use, only the
17212 innermost args are interesting. */
17213 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17215 outer_args
= strip_innermost_template_args (args
, 1);
17216 args
= INNERMOST_TEMPLATE_ARGS (args
);
17219 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17221 tree partial_spec_args
;
17223 tree parms
= TREE_VALUE (t
);
17225 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17227 ++processing_template_decl
;
17233 /* Discard the outer levels of args, and then substitute in the
17234 template args from the enclosing class. */
17235 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17236 partial_spec_args
= tsubst_template_args
17237 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17239 /* PARMS already refers to just the innermost parms, but the
17240 template parms in partial_spec_args had their levels lowered
17241 by tsubst, so we need to do the same for the parm list. We
17242 can't just tsubst the TREE_VEC itself, as tsubst wants to
17243 treat a TREE_VEC as an argument vector. */
17244 parms
= copy_node (parms
);
17245 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17246 TREE_VEC_ELT (parms
, i
) =
17247 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17251 partial_spec_args
=
17252 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17253 add_to_template_args (outer_args
,
17254 partial_spec_args
),
17256 /*require_all_args=*/true,
17257 /*use_default_args=*/true);
17259 --processing_template_decl
;
17261 if (partial_spec_args
== error_mark_node
)
17262 return error_mark_node
;
17264 spec_args
= get_class_bindings (parms
,
17270 spec_args
= add_to_template_args (outer_args
, spec_args
);
17271 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17272 TREE_TYPE (list
) = TREE_TYPE (t
);
17279 ambiguous_p
= false;
17282 t
= TREE_CHAIN (t
);
17283 for (; t
; t
= TREE_CHAIN (t
))
17285 fate
= more_specialized_class (champ
, t
);
17292 t
= TREE_CHAIN (t
);
17295 ambiguous_p
= true;
17304 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17306 fate
= more_specialized_class (champ
, t
);
17309 ambiguous_p
= true;
17317 char *spaces
= NULL
;
17318 if (!(complain
& tf_error
))
17319 return error_mark_node
;
17320 error ("ambiguous class template instantiation for %q#T", type
);
17321 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17322 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17324 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17325 spaces
= spaces
? spaces
: get_spaces (str
);
17328 return error_mark_node
;
17334 /* Explicitly instantiate DECL. */
17337 do_decl_instantiation (tree decl
, tree storage
)
17339 tree result
= NULL_TREE
;
17342 if (!decl
|| decl
== error_mark_node
)
17343 /* An error occurred, for which grokdeclarator has already issued
17344 an appropriate message. */
17346 else if (! DECL_LANG_SPECIFIC (decl
))
17348 error ("explicit instantiation of non-template %q#D", decl
);
17351 else if (TREE_CODE (decl
) == VAR_DECL
)
17353 /* There is an asymmetry here in the way VAR_DECLs and
17354 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17355 the latter, the DECL we get back will be marked as a
17356 template instantiation, and the appropriate
17357 DECL_TEMPLATE_INFO will be set up. This does not happen for
17358 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17359 should handle VAR_DECLs as it currently handles
17361 if (!DECL_CLASS_SCOPE_P (decl
))
17363 error ("%qD is not a static data member of a class template", decl
);
17366 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17367 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17369 error ("no matching template for %qD found", decl
);
17372 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17374 error ("type %qT for explicit instantiation %qD does not match "
17375 "declared type %qT", TREE_TYPE (result
), decl
,
17380 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17382 error ("explicit instantiation of %q#D", decl
);
17388 /* Check for various error cases. Note that if the explicit
17389 instantiation is valid the RESULT will currently be marked as an
17390 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17391 until we get here. */
17393 if (DECL_TEMPLATE_SPECIALIZATION (result
))
17395 /* DR 259 [temp.spec].
17397 Both an explicit instantiation and a declaration of an explicit
17398 specialization shall not appear in a program unless the explicit
17399 instantiation follows a declaration of the explicit specialization.
17401 For a given set of template parameters, if an explicit
17402 instantiation of a template appears after a declaration of an
17403 explicit specialization for that template, the explicit
17404 instantiation has no effect. */
17407 else if (DECL_EXPLICIT_INSTANTIATION (result
))
17411 No program shall explicitly instantiate any template more
17414 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17415 the first instantiation was `extern' and the second is not,
17416 and EXTERN_P for the opposite case. */
17417 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
17418 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
17419 /* If an "extern" explicit instantiation follows an ordinary
17420 explicit instantiation, the template is instantiated. */
17424 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
17426 error ("no matching template for %qD found", result
);
17429 else if (!DECL_TEMPLATE_INFO (result
))
17431 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
17435 if (storage
== NULL_TREE
)
17437 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17439 if (!in_system_header
&& (cxx_dialect
== cxx98
))
17440 pedwarn (input_location
, OPT_pedantic
,
17441 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17446 error ("storage class %qD applied to template instantiation", storage
);
17448 check_explicit_instantiation_namespace (result
);
17449 mark_decl_instantiated (result
, extern_p
);
17451 instantiate_decl (result
, /*defer_ok=*/1,
17452 /*expl_inst_class_mem_p=*/false);
17456 mark_class_instantiated (tree t
, int extern_p
)
17458 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
17459 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
17460 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
17461 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
17464 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
17465 rest_of_type_compilation (t
, 1);
17469 /* Called from do_type_instantiation through binding_table_foreach to
17470 do recursive instantiation for the type bound in ENTRY. */
17472 bt_instantiate_type_proc (binding_entry entry
, void *data
)
17474 tree storage
= *(tree
*) data
;
17476 if (MAYBE_CLASS_TYPE_P (entry
->type
)
17477 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
17478 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
17481 /* Called from do_type_instantiation to instantiate a member
17482 (a member function or a static member variable) of an
17483 explicitly instantiated class template. */
17485 instantiate_class_member (tree decl
, int extern_p
)
17487 mark_decl_instantiated (decl
, extern_p
);
17489 instantiate_decl (decl
, /*defer_ok=*/1,
17490 /*expl_inst_class_mem_p=*/true);
17493 /* Perform an explicit instantiation of template class T. STORAGE, if
17494 non-null, is the RID for extern, inline or static. COMPLAIN is
17495 nonzero if this is called from the parser, zero if called recursively,
17496 since the standard is unclear (as detailed below). */
17499 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
17504 int previous_instantiation_extern_p
= 0;
17506 if (TREE_CODE (t
) == TYPE_DECL
)
17509 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
17511 error ("explicit instantiation of non-template type %qT", t
);
17517 if (!COMPLETE_TYPE_P (t
))
17519 if (complain
& tf_error
)
17520 error ("explicit instantiation of %q#T before definition of template",
17525 if (storage
!= NULL_TREE
)
17527 if (!in_system_header
)
17529 if (storage
== ridpointers
[(int) RID_EXTERN
])
17531 if (cxx_dialect
== cxx98
)
17532 pedwarn (input_location
, OPT_pedantic
,
17533 "ISO C++ 1998 forbids the use of %<extern%> on "
17534 "explicit instantiations");
17537 pedwarn (input_location
, OPT_pedantic
,
17538 "ISO C++ forbids the use of %qE"
17539 " on explicit instantiations", storage
);
17542 if (storage
== ridpointers
[(int) RID_INLINE
])
17544 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17546 else if (storage
== ridpointers
[(int) RID_STATIC
])
17550 error ("storage class %qD applied to template instantiation",
17556 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
17558 /* DR 259 [temp.spec].
17560 Both an explicit instantiation and a declaration of an explicit
17561 specialization shall not appear in a program unless the explicit
17562 instantiation follows a declaration of the explicit specialization.
17564 For a given set of template parameters, if an explicit
17565 instantiation of a template appears after a declaration of an
17566 explicit specialization for that template, the explicit
17567 instantiation has no effect. */
17570 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
17574 No program shall explicitly instantiate any template more
17577 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
17578 instantiation was `extern'. If EXTERN_P then the second is.
17579 These cases are OK. */
17580 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
17582 if (!previous_instantiation_extern_p
&& !extern_p
17583 && (complain
& tf_error
))
17584 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
17586 /* If we've already instantiated the template, just return now. */
17587 if (!CLASSTYPE_INTERFACE_ONLY (t
))
17591 check_explicit_instantiation_namespace (TYPE_NAME (t
));
17592 mark_class_instantiated (t
, extern_p
);
17600 /* In contrast to implicit instantiation, where only the
17601 declarations, and not the definitions, of members are
17602 instantiated, we have here:
17606 The explicit instantiation of a class template specialization
17607 implies the instantiation of all of its members not
17608 previously explicitly specialized in the translation unit
17609 containing the explicit instantiation.
17611 Of course, we can't instantiate member template classes, since
17612 we don't have any arguments for them. Note that the standard
17613 is unclear on whether the instantiation of the members are
17614 *explicit* instantiations or not. However, the most natural
17615 interpretation is that it should be an explicit instantiation. */
17618 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
17619 if (TREE_CODE (tmp
) == FUNCTION_DECL
17620 && DECL_TEMPLATE_INSTANTIATION (tmp
))
17621 instantiate_class_member (tmp
, extern_p
);
17623 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
17624 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
17625 instantiate_class_member (tmp
, extern_p
);
17627 if (CLASSTYPE_NESTED_UTDS (t
))
17628 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
17629 bt_instantiate_type_proc
, &storage
);
17633 /* Given a function DECL, which is a specialization of TMPL, modify
17634 DECL to be a re-instantiation of TMPL with the same template
17635 arguments. TMPL should be the template into which tsubst'ing
17636 should occur for DECL, not the most general template.
17638 One reason for doing this is a scenario like this:
17641 void f(const T&, int i);
17643 void g() { f(3, 7); }
17646 void f(const T& t, const int i) { }
17648 Note that when the template is first instantiated, with
17649 instantiate_template, the resulting DECL will have no name for the
17650 first parameter, and the wrong type for the second. So, when we go
17651 to instantiate the DECL, we regenerate it. */
17654 regenerate_decl_from_template (tree decl
, tree tmpl
)
17656 /* The arguments used to instantiate DECL, from the most general
17661 args
= DECL_TI_ARGS (decl
);
17662 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
17664 /* Make sure that we can see identifiers, and compute access
17666 push_access_scope (decl
);
17668 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17676 args_depth
= TMPL_ARGS_DEPTH (args
);
17677 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
17678 if (args_depth
> parms_depth
)
17679 args
= get_innermost_template_args (args
, parms_depth
);
17681 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
17682 args
, tf_error
, NULL_TREE
,
17683 /*defer_ok*/false);
17685 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
17688 /* Merge parameter declarations. */
17689 decl_parm
= skip_artificial_parms_for (decl
,
17690 DECL_ARGUMENTS (decl
));
17692 = skip_artificial_parms_for (code_pattern
,
17693 DECL_ARGUMENTS (code_pattern
));
17694 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
17699 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
17700 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
17701 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
17703 parm_type
= type_decays_to (parm_type
);
17704 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
17705 TREE_TYPE (decl_parm
) = parm_type
;
17706 attributes
= DECL_ATTRIBUTES (pattern_parm
);
17707 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
17709 DECL_ATTRIBUTES (decl_parm
) = attributes
;
17710 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
17712 decl_parm
= DECL_CHAIN (decl_parm
);
17713 pattern_parm
= DECL_CHAIN (pattern_parm
);
17715 /* Merge any parameters that match with the function parameter
17717 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
17720 tree expanded_types
;
17721 /* Expand the TYPE_PACK_EXPANSION that provides the types for
17722 the parameters in this function parameter pack. */
17723 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
17724 args
, tf_error
, NULL_TREE
);
17725 len
= TREE_VEC_LENGTH (expanded_types
);
17726 for (i
= 0; i
< len
; i
++)
17731 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
17732 /* Rename the parameter to include the index. */
17733 DECL_NAME (decl_parm
) =
17734 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
17735 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
17736 parm_type
= type_decays_to (parm_type
);
17737 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
17738 TREE_TYPE (decl_parm
) = parm_type
;
17739 attributes
= DECL_ATTRIBUTES (pattern_parm
);
17740 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
17742 DECL_ATTRIBUTES (decl_parm
) = attributes
;
17743 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
17745 decl_parm
= DECL_CHAIN (decl_parm
);
17748 /* Merge additional specifiers from the CODE_PATTERN. */
17749 if (DECL_DECLARED_INLINE_P (code_pattern
)
17750 && !DECL_DECLARED_INLINE_P (decl
))
17751 DECL_DECLARED_INLINE_P (decl
) = 1;
17753 else if (TREE_CODE (decl
) == VAR_DECL
)
17755 DECL_INITIAL (decl
) =
17756 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
17757 tf_error
, DECL_TI_TEMPLATE (decl
),
17758 /*integral_constant_expression_p=*/false);
17759 if (VAR_HAD_UNKNOWN_BOUND (decl
))
17760 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
17761 tf_error
, DECL_TI_TEMPLATE (decl
));
17764 gcc_unreachable ();
17766 pop_access_scope (decl
);
17769 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
17770 substituted to get DECL. */
17773 template_for_substitution (tree decl
)
17775 tree tmpl
= DECL_TI_TEMPLATE (decl
);
17777 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
17778 for the instantiation. This is not always the most general
17779 template. Consider, for example:
17782 struct S { template <class U> void f();
17783 template <> void f<int>(); };
17785 and an instantiation of S<double>::f<int>. We want TD to be the
17786 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
17787 while (/* An instantiation cannot have a definition, so we need a
17788 more general template. */
17789 DECL_TEMPLATE_INSTANTIATION (tmpl
)
17790 /* We must also deal with friend templates. Given:
17792 template <class T> struct S {
17793 template <class U> friend void f() {};
17796 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
17797 so far as the language is concerned, but that's still
17798 where we get the pattern for the instantiation from. On
17799 other hand, if the definition comes outside the class, say:
17801 template <class T> struct S {
17802 template <class U> friend void f();
17804 template <class U> friend void f() {}
17806 we don't need to look any further. That's what the check for
17807 DECL_INITIAL is for. */
17808 || (TREE_CODE (decl
) == FUNCTION_DECL
17809 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
17810 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
17812 /* The present template, TD, should not be a definition. If it
17813 were a definition, we should be using it! Note that we
17814 cannot restructure the loop to just keep going until we find
17815 a template with a definition, since that might go too far if
17816 a specialization was declared, but not defined. */
17817 gcc_assert (TREE_CODE (decl
) != VAR_DECL
17818 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
17820 /* Fetch the more general template. */
17821 tmpl
= DECL_TI_TEMPLATE (tmpl
);
17827 /* Returns true if we need to instantiate this template instance even if we
17828 know we aren't going to emit it.. */
17831 always_instantiate_p (tree decl
)
17833 /* We always instantiate inline functions so that we can inline them. An
17834 explicit instantiation declaration prohibits implicit instantiation of
17835 non-inline functions. With high levels of optimization, we would
17836 normally inline non-inline functions -- but we're not allowed to do
17837 that for "extern template" functions. Therefore, we check
17838 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
17839 return ((TREE_CODE (decl
) == FUNCTION_DECL
17840 && DECL_DECLARED_INLINE_P (decl
))
17841 /* And we need to instantiate static data members so that
17842 their initializers are available in integral constant
17844 || (TREE_CODE (decl
) == VAR_DECL
17845 && decl_maybe_constant_var_p (decl
)));
17848 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
17849 instantiate it now, modifying TREE_TYPE (fn). */
17852 maybe_instantiate_noexcept (tree fn
)
17854 tree fntype
, spec
, noex
, clone
;
17856 if (DECL_CLONED_FUNCTION_P (fn
))
17857 fn
= DECL_CLONED_FUNCTION (fn
);
17858 fntype
= TREE_TYPE (fn
);
17859 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
17861 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
17864 noex
= TREE_PURPOSE (spec
);
17866 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
17868 push_tinst_level (fn
);
17869 push_access_scope (fn
);
17870 input_location
= DECL_SOURCE_LOCATION (fn
);
17871 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
17872 DEFERRED_NOEXCEPT_ARGS (noex
),
17873 tf_warning_or_error
, fn
, /*function_p=*/false,
17874 /*integral_constant_expression_p=*/true);
17875 pop_access_scope (fn
);
17876 pop_tinst_level ();
17877 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
17878 if (spec
== error_mark_node
)
17879 spec
= noexcept_false_spec
;
17883 /* This is an implicitly declared function, so NOEX is a list of
17884 other functions to evaluate and merge. */
17886 spec
= noexcept_true_spec
;
17887 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
17889 tree fn
= OVL_CURRENT (elt
);
17891 maybe_instantiate_noexcept (fn
);
17892 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
17893 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
17897 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
17899 FOR_EACH_CLONE (clone
, fn
)
17901 if (TREE_TYPE (clone
) == fntype
)
17902 TREE_TYPE (clone
) = TREE_TYPE (fn
);
17904 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
17908 /* Produce the definition of D, a _DECL generated from a template. If
17909 DEFER_OK is nonzero, then we don't have to actually do the
17910 instantiation now; we just have to do it sometime. Normally it is
17911 an error if this is an explicit instantiation but D is undefined.
17912 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
17913 explicitly instantiated class template. */
17916 instantiate_decl (tree d
, int defer_ok
,
17917 bool expl_inst_class_mem_p
)
17919 tree tmpl
= DECL_TI_TEMPLATE (d
);
17926 bool pattern_defined
;
17928 location_t saved_loc
= input_location
;
17931 /* This function should only be used to instantiate templates for
17932 functions and static member variables. */
17933 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
17934 || TREE_CODE (d
) == VAR_DECL
);
17936 /* Variables are never deferred; if instantiation is required, they
17937 are instantiated right away. That allows for better code in the
17938 case that an expression refers to the value of the variable --
17939 if the variable has a constant value the referring expression can
17940 take advantage of that fact. */
17941 if (TREE_CODE (d
) == VAR_DECL
17942 || DECL_DECLARED_CONSTEXPR_P (d
))
17945 /* Don't instantiate cloned functions. Instead, instantiate the
17946 functions they cloned. */
17947 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
17948 d
= DECL_CLONED_FUNCTION (d
);
17950 if (DECL_TEMPLATE_INSTANTIATED (d
)
17951 || DECL_TEMPLATE_SPECIALIZATION (d
))
17952 /* D has already been instantiated or explicitly specialized, so
17953 there's nothing for us to do here.
17955 It might seem reasonable to check whether or not D is an explicit
17956 instantiation, and, if so, stop here. But when an explicit
17957 instantiation is deferred until the end of the compilation,
17958 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
17959 the instantiation. */
17962 /* Check to see whether we know that this template will be
17963 instantiated in some other file, as with "extern template"
17965 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
17967 /* In general, we do not instantiate such templates. */
17968 if (external_p
&& !always_instantiate_p (d
))
17971 gen_tmpl
= most_general_template (tmpl
);
17972 gen_args
= DECL_TI_ARGS (d
);
17974 if (tmpl
!= gen_tmpl
)
17975 /* We should already have the extra args. */
17976 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
17977 == TMPL_ARGS_DEPTH (gen_args
));
17978 /* And what's in the hash table should match D. */
17979 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
17980 || spec
== NULL_TREE
);
17982 /* This needs to happen before any tsubsting. */
17983 if (! push_tinst_level (d
))
17986 timevar_push (TV_TEMPLATE_INST
);
17988 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
17989 for the instantiation. */
17990 td
= template_for_substitution (d
);
17991 code_pattern
= DECL_TEMPLATE_RESULT (td
);
17993 /* We should never be trying to instantiate a member of a class
17994 template or partial specialization. */
17995 gcc_assert (d
!= code_pattern
);
17997 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
17998 || DECL_TEMPLATE_SPECIALIZATION (td
))
17999 /* In the case of a friend template whose definition is provided
18000 outside the class, we may have too many arguments. Drop the
18001 ones we don't need. The same is true for specializations. */
18002 args
= get_innermost_template_args
18003 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18007 if (TREE_CODE (d
) == FUNCTION_DECL
)
18008 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18009 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18011 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18013 /* We may be in the middle of deferred access check. Disable it now. */
18014 push_deferring_access_checks (dk_no_deferred
);
18016 /* Unless an explicit instantiation directive has already determined
18017 the linkage of D, remember that a definition is available for
18019 if (pattern_defined
18020 && !DECL_INTERFACE_KNOWN (d
)
18021 && !DECL_NOT_REALLY_EXTERN (d
))
18022 mark_definable (d
);
18024 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18025 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18026 input_location
= DECL_SOURCE_LOCATION (d
);
18028 /* If D is a member of an explicitly instantiated class template,
18029 and no definition is available, treat it like an implicit
18031 if (!pattern_defined
&& expl_inst_class_mem_p
18032 && DECL_EXPLICIT_INSTANTIATION (d
))
18034 /* Leave linkage flags alone on instantiations with anonymous
18036 if (TREE_PUBLIC (d
))
18038 DECL_NOT_REALLY_EXTERN (d
) = 0;
18039 DECL_INTERFACE_KNOWN (d
) = 0;
18041 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18044 if (TREE_CODE (d
) == FUNCTION_DECL
)
18045 maybe_instantiate_noexcept (d
);
18047 /* Recheck the substitutions to obtain any warning messages
18048 about ignoring cv qualifiers. Don't do this for artificial decls,
18049 as it breaks the context-sensitive substitution for lambda op(). */
18050 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
18052 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18053 tree type
= TREE_TYPE (gen
);
18055 /* Make sure that we can see identifiers, and compute access
18056 correctly. D is already the target FUNCTION_DECL with the
18058 push_access_scope (d
);
18060 if (TREE_CODE (gen
) == FUNCTION_DECL
)
18062 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
18063 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
18064 d
, /*defer_ok*/true);
18065 /* Don't simply tsubst the function type, as that will give
18066 duplicate warnings about poor parameter qualifications.
18067 The function arguments are the same as the decl_arguments
18068 without the top level cv qualifiers. */
18069 type
= TREE_TYPE (type
);
18071 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
18073 pop_access_scope (d
);
18076 /* Defer all other templates, unless we have been explicitly
18077 forbidden from doing so. */
18078 if (/* If there is no definition, we cannot instantiate the
18081 /* If it's OK to postpone instantiation, do so. */
18083 /* If this is a static data member that will be defined
18084 elsewhere, we don't want to instantiate the entire data
18085 member, but we do want to instantiate the initializer so that
18086 we can substitute that elsewhere. */
18087 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18089 /* The definition of the static data member is now required so
18090 we must substitute the initializer. */
18091 if (TREE_CODE (d
) == VAR_DECL
18092 && !DECL_INITIAL (d
)
18093 && DECL_INITIAL (code_pattern
))
18097 bool const_init
= false;
18099 ns
= decl_namespace_context (d
);
18100 push_nested_namespace (ns
);
18101 push_nested_class (DECL_CONTEXT (d
));
18102 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18104 tf_warning_or_error
, NULL_TREE
,
18105 /*integral_constant_expression_p=*/false);
18106 /* Make sure the initializer is still constant, in case of
18107 circular dependency (template/instantiate6.C). */
18109 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18110 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18111 /*asmspec_tree=*/NULL_TREE
,
18112 LOOKUP_ONLYCONVERTING
);
18113 pop_nested_class ();
18114 pop_nested_namespace (ns
);
18117 /* We restore the source position here because it's used by
18118 add_pending_template. */
18119 input_location
= saved_loc
;
18121 if (at_eof
&& !pattern_defined
18122 && DECL_EXPLICIT_INSTANTIATION (d
)
18123 && DECL_NOT_REALLY_EXTERN (d
))
18126 The definition of a non-exported function template, a
18127 non-exported member function template, or a non-exported
18128 member function or static data member of a class template
18129 shall be present in every translation unit in which it is
18130 explicitly instantiated. */
18131 permerror (input_location
, "explicit instantiation of %qD "
18132 "but no definition available", d
);
18134 /* If we're in unevaluated context, we just wanted to get the
18135 constant value; this isn't an odr use, so don't queue
18136 a full instantiation. */
18137 if (cp_unevaluated_operand
!= 0)
18139 /* ??? Historically, we have instantiated inline functions, even
18140 when marked as "extern template". */
18141 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18142 add_pending_template (d
);
18145 /* Tell the repository that D is available in this translation unit
18146 -- and see if it is supposed to be instantiated here. */
18147 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18149 /* In a PCH file, despite the fact that the repository hasn't
18150 requested instantiation in the PCH it is still possible that
18151 an instantiation will be required in a file that includes the
18154 add_pending_template (d
);
18155 /* Instantiate inline functions so that the inliner can do its
18156 job, even though we'll not be emitting a copy of this
18158 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18162 need_push
= !cfun
|| !global_bindings_p ();
18164 push_to_top_level ();
18166 /* Mark D as instantiated so that recursive calls to
18167 instantiate_decl do not try to instantiate it again. */
18168 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18170 /* Regenerate the declaration in case the template has been modified
18171 by a subsequent redeclaration. */
18172 regenerate_decl_from_template (d
, td
);
18174 /* We already set the file and line above. Reset them now in case
18175 they changed as a result of calling regenerate_decl_from_template. */
18176 input_location
= DECL_SOURCE_LOCATION (d
);
18178 if (TREE_CODE (d
) == VAR_DECL
)
18181 bool const_init
= false;
18183 /* Clear out DECL_RTL; whatever was there before may not be right
18184 since we've reset the type of the declaration. */
18185 SET_DECL_RTL (d
, NULL
);
18186 DECL_IN_AGGR_P (d
) = 0;
18188 /* The initializer is placed in DECL_INITIAL by
18189 regenerate_decl_from_template so we don't need to
18190 push/pop_access_scope again here. Pull it out so that
18191 cp_finish_decl can process it. */
18192 init
= DECL_INITIAL (d
);
18193 DECL_INITIAL (d
) = NULL_TREE
;
18194 DECL_INITIALIZED_P (d
) = 0;
18196 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18197 initializer. That function will defer actual emission until
18198 we have a chance to determine linkage. */
18199 DECL_EXTERNAL (d
) = 0;
18201 /* Enter the scope of D so that access-checking works correctly. */
18202 push_nested_class (DECL_CONTEXT (d
));
18203 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18204 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18205 pop_nested_class ();
18207 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18208 synthesize_method (d
);
18209 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18211 htab_t saved_local_specializations
;
18216 /* Save away the current list, in case we are instantiating one
18217 template from within the body of another. */
18218 saved_local_specializations
= local_specializations
;
18220 /* Set up the list of local specializations. */
18221 local_specializations
= htab_create (37,
18222 hash_local_specialization
,
18223 eq_local_specializations
,
18226 /* Set up context. */
18227 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18229 /* Create substitution entries for the parameters. */
18230 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18231 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18232 spec_parm
= DECL_ARGUMENTS (d
);
18233 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18235 register_local_specialization (spec_parm
, tmpl_parm
);
18236 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18237 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18239 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18241 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18243 register_local_specialization (spec_parm
, tmpl_parm
);
18244 spec_parm
= DECL_CHAIN (spec_parm
);
18248 /* Register the (value) argument pack as a specialization of
18249 TMPL_PARM, then move on. */
18250 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18251 register_local_specialization (argpack
, tmpl_parm
);
18254 gcc_assert (!spec_parm
);
18256 /* Substitute into the body of the function. */
18257 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18258 tf_warning_or_error
, tmpl
,
18259 /*integral_constant_expression_p=*/false);
18261 /* Set the current input_location to the end of the function
18262 so that finish_function knows where we are. */
18263 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18265 /* We don't need the local specializations any more. */
18266 htab_delete (local_specializations
);
18267 local_specializations
= saved_local_specializations
;
18269 /* Finish the function. */
18270 d
= finish_function (0);
18271 expand_or_defer_fn (d
);
18274 /* We're not deferring instantiation any more. */
18275 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18278 pop_from_top_level ();
18281 input_location
= saved_loc
;
18282 pop_deferring_access_checks ();
18283 pop_tinst_level ();
18285 timevar_pop (TV_TEMPLATE_INST
);
18290 /* Run through the list of templates that we wish we could
18291 instantiate, and instantiate any we can. RETRIES is the
18292 number of times we retry pending template instantiation. */
18295 instantiate_pending_templates (int retries
)
18298 location_t saved_loc
= input_location
;
18300 /* Instantiating templates may trigger vtable generation. This in turn
18301 may require further template instantiations. We place a limit here
18302 to avoid infinite loop. */
18303 if (pending_templates
&& retries
>= max_tinst_depth
)
18305 tree decl
= pending_templates
->tinst
->decl
;
18307 error ("template instantiation depth exceeds maximum of %d"
18308 " instantiating %q+D, possibly from virtual table generation"
18309 " (use -ftemplate-depth= to increase the maximum)",
18310 max_tinst_depth
, decl
);
18311 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18312 /* Pretend that we defined it. */
18313 DECL_INITIAL (decl
) = error_mark_node
;
18319 struct pending_template
**t
= &pending_templates
;
18320 struct pending_template
*last
= NULL
;
18324 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18325 bool complete
= false;
18327 if (TYPE_P (instantiation
))
18331 if (!COMPLETE_TYPE_P (instantiation
))
18333 instantiate_class_template (instantiation
);
18334 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18335 for (fn
= TYPE_METHODS (instantiation
);
18337 fn
= TREE_CHAIN (fn
))
18338 if (! DECL_ARTIFICIAL (fn
))
18339 instantiate_decl (fn
,
18341 /*expl_inst_class_mem_p=*/false);
18342 if (COMPLETE_TYPE_P (instantiation
))
18346 complete
= COMPLETE_TYPE_P (instantiation
);
18350 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18351 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18354 = instantiate_decl (instantiation
,
18356 /*expl_inst_class_mem_p=*/false);
18357 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18361 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18362 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18366 /* If INSTANTIATION has been instantiated, then we don't
18367 need to consider it again in the future. */
18375 current_tinst_level
= NULL
;
18377 last_pending_template
= last
;
18379 while (reconsider
);
18381 input_location
= saved_loc
;
18384 /* Substitute ARGVEC into T, which is a list of initializers for
18385 either base class or a non-static data member. The TREE_PURPOSEs
18386 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18387 instantiate_decl. */
18390 tsubst_initializer_list (tree t
, tree argvec
)
18392 tree inits
= NULL_TREE
;
18394 for (; t
; t
= TREE_CHAIN (t
))
18398 tree expanded_bases
= NULL_TREE
;
18399 tree expanded_arguments
= NULL_TREE
;
18402 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
18407 /* Expand the base class expansion type into separate base
18409 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
18410 tf_warning_or_error
,
18412 if (expanded_bases
== error_mark_node
)
18415 /* We'll be building separate TREE_LISTs of arguments for
18417 len
= TREE_VEC_LENGTH (expanded_bases
);
18418 expanded_arguments
= make_tree_vec (len
);
18419 for (i
= 0; i
< len
; i
++)
18420 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
18422 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18423 expand each argument in the TREE_VALUE of t. */
18424 expr
= make_node (EXPR_PACK_EXPANSION
);
18425 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
18426 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
18428 if (TREE_VALUE (t
) == void_type_node
)
18429 /* VOID_TYPE_NODE is used to indicate
18430 value-initialization. */
18432 for (i
= 0; i
< len
; i
++)
18433 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
18437 /* Substitute parameter packs into each argument in the
18439 in_base_initializer
= 1;
18440 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
18442 tree expanded_exprs
;
18444 /* Expand the argument. */
18445 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
18447 = tsubst_pack_expansion (expr
, argvec
,
18448 tf_warning_or_error
,
18450 if (expanded_exprs
== error_mark_node
)
18453 /* Prepend each of the expanded expressions to the
18454 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18455 for (i
= 0; i
< len
; i
++)
18457 TREE_VEC_ELT (expanded_arguments
, i
) =
18458 tree_cons (NULL_TREE
,
18459 TREE_VEC_ELT (expanded_exprs
, i
),
18460 TREE_VEC_ELT (expanded_arguments
, i
));
18463 in_base_initializer
= 0;
18465 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18466 since we built them backwards. */
18467 for (i
= 0; i
< len
; i
++)
18469 TREE_VEC_ELT (expanded_arguments
, i
) =
18470 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
18475 for (i
= 0; i
< len
; ++i
)
18477 if (expanded_bases
)
18479 decl
= TREE_VEC_ELT (expanded_bases
, i
);
18480 decl
= expand_member_init (decl
);
18481 init
= TREE_VEC_ELT (expanded_arguments
, i
);
18485 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
18486 tf_warning_or_error
, NULL_TREE
);
18488 decl
= expand_member_init (decl
);
18489 if (decl
&& !DECL_P (decl
))
18490 in_base_initializer
= 1;
18492 init
= TREE_VALUE (t
);
18493 if (init
!= void_type_node
)
18494 init
= tsubst_expr (init
, argvec
,
18495 tf_warning_or_error
, NULL_TREE
,
18496 /*integral_constant_expression_p=*/false);
18497 in_base_initializer
= 0;
18502 init
= build_tree_list (decl
, init
);
18503 TREE_CHAIN (init
) = inits
;
18511 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18514 set_current_access_from_decl (tree decl
)
18516 if (TREE_PRIVATE (decl
))
18517 current_access_specifier
= access_private_node
;
18518 else if (TREE_PROTECTED (decl
))
18519 current_access_specifier
= access_protected_node
;
18521 current_access_specifier
= access_public_node
;
18524 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18525 is the instantiation (which should have been created with
18526 start_enum) and ARGS are the template arguments to use. */
18529 tsubst_enum (tree tag
, tree newtag
, tree args
)
18533 if (SCOPED_ENUM_P (newtag
))
18534 begin_scope (sk_scoped_enum
, newtag
);
18536 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
18541 decl
= TREE_VALUE (e
);
18542 /* Note that in a template enum, the TREE_VALUE is the
18543 CONST_DECL, not the corresponding INTEGER_CST. */
18544 value
= tsubst_expr (DECL_INITIAL (decl
),
18545 args
, tf_warning_or_error
, NULL_TREE
,
18546 /*integral_constant_expression_p=*/true);
18548 /* Give this enumeration constant the correct access. */
18549 set_current_access_from_decl (decl
);
18551 /* Actually build the enumerator itself. */
18553 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
18556 if (SCOPED_ENUM_P (newtag
))
18559 finish_enum_value_list (newtag
);
18560 finish_enum (newtag
);
18562 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
18563 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
18566 /* DECL is a FUNCTION_DECL that is a template specialization. Return
18567 its type -- but without substituting the innermost set of template
18568 arguments. So, innermost set of template parameters will appear in
18572 get_mostly_instantiated_function_type (tree decl
)
18580 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
18581 targs
= DECL_TI_ARGS (decl
);
18582 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
18583 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
18585 /* There should be as many levels of arguments as there are levels
18587 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
18589 fn_type
= TREE_TYPE (tmpl
);
18591 if (parm_depth
== 1)
18592 /* No substitution is necessary. */
18599 /* Replace the innermost level of the TARGS with NULL_TREEs to
18600 let tsubst know not to substitute for those parameters. */
18601 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
18602 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
18603 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
18604 TMPL_ARGS_LEVEL (targs
, i
));
18605 SET_TMPL_ARGS_LEVEL (partial_args
,
18606 TMPL_ARGS_DEPTH (targs
),
18607 make_tree_vec (DECL_NTPARMS (tmpl
)));
18609 /* Make sure that we can see identifiers, and compute access
18611 push_access_scope (decl
);
18613 ++processing_template_decl
;
18614 /* Now, do the (partial) substitution to figure out the
18615 appropriate function type. */
18616 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
18617 --processing_template_decl
;
18619 /* Substitute into the template parameters to obtain the real
18620 innermost set of parameters. This step is important if the
18621 innermost set of template parameters contains value
18622 parameters whose types depend on outer template parameters. */
18623 TREE_VEC_LENGTH (partial_args
)--;
18624 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
18626 pop_access_scope (decl
);
18632 /* Return truthvalue if we're processing a template different from
18633 the last one involved in diagnostics. */
18635 problematic_instantiation_changed (void)
18637 return current_tinst_level
!= last_error_tinst_level
;
18640 /* Remember current template involved in diagnostics. */
18642 record_last_problematic_instantiation (void)
18644 last_error_tinst_level
= current_tinst_level
;
18647 struct tinst_level
*
18648 current_instantiation (void)
18650 return current_tinst_level
;
18653 /* [temp.param] Check that template non-type parm TYPE is of an allowable
18654 type. Return zero for ok, nonzero for disallowed. Issue error and
18655 warning messages under control of COMPLAIN. */
18658 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
18660 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
18662 else if (POINTER_TYPE_P (type
))
18664 else if (TYPE_PTR_TO_MEMBER_P (type
))
18666 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
18668 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
18670 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
18673 if (complain
& tf_error
)
18674 error ("%q#T is not a valid type for a template constant parameter", type
);
18678 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
18679 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
18682 dependent_type_p_r (tree type
)
18688 A type is dependent if it is:
18690 -- a template parameter. Template template parameters are types
18691 for us (since TYPE_P holds true for them) so we handle
18693 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
18694 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
18696 /* -- a qualified-id with a nested-name-specifier which contains a
18697 class-name that names a dependent type or whose unqualified-id
18698 names a dependent type. */
18699 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18701 /* -- a cv-qualified type where the cv-unqualified type is
18703 type
= TYPE_MAIN_VARIANT (type
);
18704 /* -- a compound type constructed from any dependent type. */
18705 if (TYPE_PTR_TO_MEMBER_P (type
))
18706 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
18707 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
18709 else if (TREE_CODE (type
) == POINTER_TYPE
18710 || TREE_CODE (type
) == REFERENCE_TYPE
)
18711 return dependent_type_p (TREE_TYPE (type
));
18712 else if (TREE_CODE (type
) == FUNCTION_TYPE
18713 || TREE_CODE (type
) == METHOD_TYPE
)
18717 if (dependent_type_p (TREE_TYPE (type
)))
18719 for (arg_type
= TYPE_ARG_TYPES (type
);
18721 arg_type
= TREE_CHAIN (arg_type
))
18722 if (dependent_type_p (TREE_VALUE (arg_type
)))
18726 /* -- an array type constructed from any dependent type or whose
18727 size is specified by a constant expression that is
18730 We checked for type- and value-dependence of the bounds in
18731 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
18732 if (TREE_CODE (type
) == ARRAY_TYPE
)
18734 if (TYPE_DOMAIN (type
)
18735 && dependent_type_p (TYPE_DOMAIN (type
)))
18737 return dependent_type_p (TREE_TYPE (type
));
18740 /* -- a template-id in which either the template name is a template
18742 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
18744 /* ... or any of the template arguments is a dependent type or
18745 an expression that is type-dependent or value-dependent. */
18746 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
18747 && (any_dependent_template_arguments_p
18748 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
18751 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
18752 dependent; if the argument of the `typeof' expression is not
18753 type-dependent, then it should already been have resolved. */
18754 if (TREE_CODE (type
) == TYPEOF_TYPE
18755 || TREE_CODE (type
) == DECLTYPE_TYPE
18756 || TREE_CODE (type
) == UNDERLYING_TYPE
)
18759 /* A template argument pack is dependent if any of its packed
18761 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
18763 tree args
= ARGUMENT_PACK_ARGS (type
);
18764 int i
, len
= TREE_VEC_LENGTH (args
);
18765 for (i
= 0; i
< len
; ++i
)
18766 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
18770 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
18771 be template parameters. */
18772 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
18775 /* The standard does not specifically mention types that are local
18776 to template functions or local classes, but they should be
18777 considered dependent too. For example:
18779 template <int I> void f() {
18784 The size of `E' cannot be known until the value of `I' has been
18785 determined. Therefore, `E' must be considered dependent. */
18786 scope
= TYPE_CONTEXT (type
);
18787 if (scope
&& TYPE_P (scope
))
18788 return dependent_type_p (scope
);
18789 /* Don't use type_dependent_expression_p here, as it can lead
18790 to infinite recursion trying to determine whether a lambda
18791 nested in a lambda is dependent (c++/47687). */
18792 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
18793 && DECL_LANG_SPECIFIC (scope
)
18794 && DECL_TEMPLATE_INFO (scope
)
18795 && (any_dependent_template_arguments_p
18796 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
18799 /* Other types are non-dependent. */
18803 /* Returns TRUE if TYPE is dependent, in the sense of
18804 [temp.dep.type]. Note that a NULL type is considered dependent. */
18807 dependent_type_p (tree type
)
18809 /* If there are no template parameters in scope, then there can't be
18810 any dependent types. */
18811 if (!processing_template_decl
)
18813 /* If we are not processing a template, then nobody should be
18814 providing us with a dependent type. */
18816 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
18820 /* If the type is NULL, we have not computed a type for the entity
18821 in question; in that case, the type is dependent. */
18825 /* Erroneous types can be considered non-dependent. */
18826 if (type
== error_mark_node
)
18829 /* If we have not already computed the appropriate value for TYPE,
18831 if (!TYPE_DEPENDENT_P_VALID (type
))
18833 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
18834 TYPE_DEPENDENT_P_VALID (type
) = 1;
18837 return TYPE_DEPENDENT_P (type
);
18840 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
18841 lookup. In other words, a dependent type that is not the current
18845 dependent_scope_p (tree scope
)
18847 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
18848 && !currently_open_class (scope
));
18851 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
18852 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
18855 /* Note that this predicate is not appropriate for general expressions;
18856 only constant expressions (that satisfy potential_constant_expression)
18857 can be tested for value dependence.
18859 We should really also have a predicate for "instantiation-dependent".
18861 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
18862 (what about instantiation-dependent constant-expressions?)
18863 is_late_template_attribute: defer if instantiation-dependent.
18864 compute_array_index_type: proceed if constant and not t- or v-dependent
18865 if instantiation-dependent, need to remember full expression
18866 uses_template_parms: FIXME - need to audit callers
18867 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
18868 dependent_type_p [array_type]: dependent if index type is dependent
18870 static_assert - instantiation-dependent */
18873 value_dependent_expression_p (tree expression
)
18875 if (!processing_template_decl
)
18878 /* A name declared with a dependent type. */
18879 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
18882 switch (TREE_CODE (expression
))
18884 case IDENTIFIER_NODE
:
18885 /* A name that has not been looked up -- must be dependent. */
18888 case TEMPLATE_PARM_INDEX
:
18889 /* A non-type template parm. */
18893 /* A non-type template parm. */
18894 if (DECL_TEMPLATE_PARM_P (expression
))
18896 return value_dependent_expression_p (DECL_INITIAL (expression
));
18899 /* A constant with literal type and is initialized
18900 with an expression that is value-dependent. */
18901 if (DECL_INITIAL (expression
)
18902 && decl_constant_var_p (expression
)
18903 && value_dependent_expression_p (DECL_INITIAL (expression
)))
18907 case DYNAMIC_CAST_EXPR
:
18908 case STATIC_CAST_EXPR
:
18909 case CONST_CAST_EXPR
:
18910 case REINTERPRET_CAST_EXPR
:
18912 /* These expressions are value-dependent if the type to which
18913 the cast occurs is dependent or the expression being casted
18914 is value-dependent. */
18916 tree type
= TREE_TYPE (expression
);
18918 if (dependent_type_p (type
))
18921 /* A functional cast has a list of operands. */
18922 expression
= TREE_OPERAND (expression
, 0);
18925 /* If there are no operands, it must be an expression such
18926 as "int()". This should not happen for aggregate types
18927 because it would form non-constant expressions. */
18928 gcc_assert (cxx_dialect
>= cxx0x
18929 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
18934 if (TREE_CODE (expression
) == TREE_LIST
)
18935 return any_value_dependent_elements_p (expression
);
18937 return value_dependent_expression_p (expression
);
18943 /* A `sizeof' expression is value-dependent if the operand is
18944 type-dependent or is a pack expansion. */
18945 expression
= TREE_OPERAND (expression
, 0);
18946 if (PACK_EXPANSION_P (expression
))
18948 else if (TYPE_P (expression
))
18949 return dependent_type_p (expression
);
18950 return type_dependent_expression_p (expression
);
18952 case AT_ENCODE_EXPR
:
18953 /* An 'encode' expression is value-dependent if the operand is
18955 expression
= TREE_OPERAND (expression
, 0);
18956 return dependent_type_p (expression
);
18958 case NOEXCEPT_EXPR
:
18959 expression
= TREE_OPERAND (expression
, 0);
18960 return type_dependent_expression_p (expression
);
18964 tree name
= TREE_OPERAND (expression
, 1);
18965 return value_dependent_expression_p (name
);
18968 case COMPONENT_REF
:
18969 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
18970 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
18972 case NONTYPE_ARGUMENT_PACK
:
18973 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
18974 is value-dependent. */
18976 tree values
= ARGUMENT_PACK_ARGS (expression
);
18977 int i
, len
= TREE_VEC_LENGTH (values
);
18979 for (i
= 0; i
< len
; ++i
)
18980 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
18988 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
18989 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
18990 || (type2
? dependent_type_p (type2
) : false));
18994 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
18995 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
18998 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
18999 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19003 tree op
= TREE_OPERAND (expression
, 0);
19004 return (value_dependent_expression_p (op
)
19005 || has_value_dependent_address (op
));
19010 tree fn
= get_callee_fndecl (expression
);
19012 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19014 nargs
= call_expr_nargs (expression
);
19015 for (i
= 0; i
< nargs
; ++i
)
19017 tree op
= CALL_EXPR_ARG (expression
, i
);
19018 /* In a call to a constexpr member function, look through the
19019 implicit ADDR_EXPR on the object argument so that it doesn't
19020 cause the call to be considered value-dependent. We also
19021 look through it in potential_constant_expression. */
19022 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19023 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19024 && TREE_CODE (op
) == ADDR_EXPR
)
19025 op
= TREE_OPERAND (op
, 0);
19026 if (value_dependent_expression_p (op
))
19032 case TEMPLATE_ID_EXPR
:
19033 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19035 return type_dependent_expression_p (expression
);
19041 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19042 if (value_dependent_expression_p (val
))
19048 /* A constant expression is value-dependent if any subexpression is
19049 value-dependent. */
19050 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19052 case tcc_reference
:
19054 case tcc_comparison
:
19056 case tcc_expression
:
19059 int i
, len
= cp_tree_operand_length (expression
);
19061 for (i
= 0; i
< len
; i
++)
19063 tree t
= TREE_OPERAND (expression
, i
);
19065 /* In some cases, some of the operands may be missing.l
19066 (For example, in the case of PREDECREMENT_EXPR, the
19067 amount to increment by may be missing.) That doesn't
19068 make the expression dependent. */
19069 if (t
&& value_dependent_expression_p (t
))
19080 /* The expression is not value-dependent. */
19084 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19085 [temp.dep.expr]. Note that an expression with no type is
19086 considered dependent. Other parts of the compiler arrange for an
19087 expression with type-dependent subexpressions to have no type, so
19088 this function doesn't have to be fully recursive. */
19091 type_dependent_expression_p (tree expression
)
19093 if (!processing_template_decl
)
19096 if (expression
== error_mark_node
)
19099 /* An unresolved name is always dependent. */
19100 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19101 || TREE_CODE (expression
) == USING_DECL
)
19104 /* Some expression forms are never type-dependent. */
19105 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19106 || TREE_CODE (expression
) == SIZEOF_EXPR
19107 || TREE_CODE (expression
) == ALIGNOF_EXPR
19108 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19109 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19110 || TREE_CODE (expression
) == TRAIT_EXPR
19111 || TREE_CODE (expression
) == TYPEID_EXPR
19112 || TREE_CODE (expression
) == DELETE_EXPR
19113 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19114 || TREE_CODE (expression
) == THROW_EXPR
)
19117 /* The types of these expressions depends only on the type to which
19118 the cast occurs. */
19119 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19120 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19121 || TREE_CODE (expression
) == CONST_CAST_EXPR
19122 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19123 || TREE_CODE (expression
) == CAST_EXPR
)
19124 return dependent_type_p (TREE_TYPE (expression
));
19126 /* The types of these expressions depends only on the type created
19127 by the expression. */
19128 if (TREE_CODE (expression
) == NEW_EXPR
19129 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19131 /* For NEW_EXPR tree nodes created inside a template, either
19132 the object type itself or a TREE_LIST may appear as the
19134 tree type
= TREE_OPERAND (expression
, 1);
19135 if (TREE_CODE (type
) == TREE_LIST
)
19136 /* This is an array type. We need to check array dimensions
19138 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19139 || value_dependent_expression_p
19140 (TREE_OPERAND (TREE_VALUE (type
), 1));
19142 return dependent_type_p (type
);
19145 if (TREE_CODE (expression
) == SCOPE_REF
)
19147 tree scope
= TREE_OPERAND (expression
, 0);
19148 tree name
= TREE_OPERAND (expression
, 1);
19150 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19151 contains an identifier associated by name lookup with one or more
19152 declarations declared with a dependent type, or...a
19153 nested-name-specifier or qualified-id that names a member of an
19154 unknown specialization. */
19155 return (type_dependent_expression_p (name
)
19156 || dependent_scope_p (scope
));
19159 if (TREE_CODE (expression
) == FUNCTION_DECL
19160 && DECL_LANG_SPECIFIC (expression
)
19161 && DECL_TEMPLATE_INFO (expression
)
19162 && (any_dependent_template_arguments_p
19163 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19166 if (TREE_CODE (expression
) == TEMPLATE_DECL
19167 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19170 if (TREE_CODE (expression
) == STMT_EXPR
)
19171 expression
= stmt_expr_value_expr (expression
);
19173 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19178 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19180 if (type_dependent_expression_p (elt
))
19186 /* A static data member of the current instantiation with incomplete
19187 array type is type-dependent, as the definition and specializations
19188 can have different bounds. */
19189 if (TREE_CODE (expression
) == VAR_DECL
19190 && DECL_CLASS_SCOPE_P (expression
)
19191 && dependent_type_p (DECL_CONTEXT (expression
))
19192 && VAR_HAD_UNKNOWN_BOUND (expression
))
19195 if (TREE_TYPE (expression
) == unknown_type_node
)
19197 if (TREE_CODE (expression
) == ADDR_EXPR
)
19198 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19199 if (TREE_CODE (expression
) == COMPONENT_REF
19200 || TREE_CODE (expression
) == OFFSET_REF
)
19202 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19204 expression
= TREE_OPERAND (expression
, 1);
19205 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19208 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19209 if (TREE_CODE (expression
) == SCOPE_REF
)
19212 if (TREE_CODE (expression
) == BASELINK
)
19213 expression
= BASELINK_FUNCTIONS (expression
);
19215 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19217 if (any_dependent_template_arguments_p
19218 (TREE_OPERAND (expression
, 1)))
19220 expression
= TREE_OPERAND (expression
, 0);
19222 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19223 || TREE_CODE (expression
) == FUNCTION_DECL
);
19227 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19229 expression
= OVL_NEXT (expression
);
19234 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19236 return (dependent_type_p (TREE_TYPE (expression
)));
19239 /* Like type_dependent_expression_p, but it also works while not processing
19240 a template definition, i.e. during substitution or mangling. */
19243 type_dependent_expression_p_push (tree expr
)
19246 ++processing_template_decl
;
19247 b
= type_dependent_expression_p (expr
);
19248 --processing_template_decl
;
19252 /* Returns TRUE if ARGS contains a type-dependent expression. */
19255 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
19260 FOR_EACH_VEC_ELT (tree
, args
, i
, arg
)
19262 if (type_dependent_expression_p (arg
))
19268 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19269 expressions) contains any type-dependent expressions. */
19272 any_type_dependent_elements_p (const_tree list
)
19274 for (; list
; list
= TREE_CHAIN (list
))
19275 if (value_dependent_expression_p (TREE_VALUE (list
)))
19281 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19282 expressions) contains any value-dependent expressions. */
19285 any_value_dependent_elements_p (const_tree list
)
19287 for (; list
; list
= TREE_CHAIN (list
))
19288 if (value_dependent_expression_p (TREE_VALUE (list
)))
19294 /* Returns TRUE if the ARG (a template argument) is dependent. */
19297 dependent_template_arg_p (tree arg
)
19299 if (!processing_template_decl
)
19302 /* Assume a template argument that was wrongly written by the user
19303 is dependent. This is consistent with what
19304 any_dependent_template_arguments_p [that calls this function]
19306 if (!arg
|| arg
== error_mark_node
)
19309 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
19310 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
19312 if (TREE_CODE (arg
) == TEMPLATE_DECL
19313 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19314 return dependent_template_p (arg
);
19315 else if (ARGUMENT_PACK_P (arg
))
19317 tree args
= ARGUMENT_PACK_ARGS (arg
);
19318 int i
, len
= TREE_VEC_LENGTH (args
);
19319 for (i
= 0; i
< len
; ++i
)
19321 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19327 else if (TYPE_P (arg
))
19328 return dependent_type_p (arg
);
19330 return (type_dependent_expression_p (arg
)
19331 || value_dependent_expression_p (arg
));
19334 /* Returns true if ARGS (a collection of template arguments) contains
19335 any types that require structural equality testing. */
19338 any_template_arguments_need_structural_equality_p (tree args
)
19345 if (args
== error_mark_node
)
19348 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19350 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19351 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19353 tree arg
= TREE_VEC_ELT (level
, j
);
19354 tree packed_args
= NULL_TREE
;
19357 if (ARGUMENT_PACK_P (arg
))
19359 /* Look inside the argument pack. */
19360 packed_args
= ARGUMENT_PACK_ARGS (arg
);
19361 len
= TREE_VEC_LENGTH (packed_args
);
19364 for (k
= 0; k
< len
; ++k
)
19367 arg
= TREE_VEC_ELT (packed_args
, k
);
19369 if (error_operand_p (arg
))
19371 else if (TREE_CODE (arg
) == TEMPLATE_DECL
19372 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19374 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
19376 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
19377 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
19386 /* Returns true if ARGS (a collection of template arguments) contains
19387 any dependent arguments. */
19390 any_dependent_template_arguments_p (const_tree args
)
19397 if (args
== error_mark_node
)
19400 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19402 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19403 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19404 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
19411 /* Returns TRUE if the template TMPL is dependent. */
19414 dependent_template_p (tree tmpl
)
19416 if (TREE_CODE (tmpl
) == OVERLOAD
)
19420 if (dependent_template_p (OVL_CURRENT (tmpl
)))
19422 tmpl
= OVL_NEXT (tmpl
);
19427 /* Template template parameters are dependent. */
19428 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
19429 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
19431 /* So are names that have not been looked up. */
19432 if (TREE_CODE (tmpl
) == SCOPE_REF
19433 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
19435 /* So are member templates of dependent classes. */
19436 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
19437 return dependent_type_p (DECL_CONTEXT (tmpl
));
19441 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19444 dependent_template_id_p (tree tmpl
, tree args
)
19446 return (dependent_template_p (tmpl
)
19447 || any_dependent_template_arguments_p (args
));
19450 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19454 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
19458 if (!processing_template_decl
)
19461 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
19463 tree decl
= TREE_VEC_ELT (declv
, i
);
19464 tree init
= TREE_VEC_ELT (initv
, i
);
19465 tree cond
= TREE_VEC_ELT (condv
, i
);
19466 tree incr
= TREE_VEC_ELT (incrv
, i
);
19468 if (type_dependent_expression_p (decl
))
19471 if (init
&& type_dependent_expression_p (init
))
19474 if (type_dependent_expression_p (cond
))
19477 if (COMPARISON_CLASS_P (cond
)
19478 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
19479 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
19482 if (TREE_CODE (incr
) == MODOP_EXPR
)
19484 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
19485 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
19488 else if (type_dependent_expression_p (incr
))
19490 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
19492 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
19494 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
19496 tree t
= TREE_OPERAND (incr
, 1);
19497 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
19498 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
19507 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
19508 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
19509 no such TYPE can be found. Note that this function peers inside
19510 uninstantiated templates and therefore should be used only in
19511 extremely limited situations. ONLY_CURRENT_P restricts this
19512 peering to the currently open classes hierarchy (which is required
19513 when comparing types). */
19516 resolve_typename_type (tree type
, bool only_current_p
)
19525 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
19527 scope
= TYPE_CONTEXT (type
);
19528 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
19529 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
19530 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
19531 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
19532 identifier of the TYPENAME_TYPE anymore.
19533 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
19534 TYPENAME_TYPE instead, we avoid messing up with a possible
19535 typedef variant case. */
19536 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
19538 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
19539 it first before we can figure out what NAME refers to. */
19540 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
19541 scope
= resolve_typename_type (scope
, only_current_p
);
19542 /* If we don't know what SCOPE refers to, then we cannot resolve the
19544 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
19546 /* If the SCOPE is a template type parameter, we have no way of
19547 resolving the name. */
19548 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
19550 /* If the SCOPE is not the current instantiation, there's no reason
19551 to look inside it. */
19552 if (only_current_p
&& !currently_open_class (scope
))
19554 /* If this is a typedef, we don't want to look inside (c++/11987). */
19555 if (typedef_variant_p (type
))
19557 /* If SCOPE isn't the template itself, it will not have a valid
19558 TYPE_FIELDS list. */
19559 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
19560 /* scope is either the template itself or a compatible instantiation
19561 like X<T>, so look up the name in the original template. */
19562 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
19564 /* scope is a partial instantiation, so we can't do the lookup or we
19565 will lose the template arguments. */
19567 /* Enter the SCOPE so that name lookup will be resolved as if we
19568 were in the class definition. In particular, SCOPE will no
19569 longer be considered a dependent type. */
19570 pushed_scope
= push_scope (scope
);
19571 /* Look up the declaration. */
19572 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true);
19574 result
= NULL_TREE
;
19576 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
19577 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
19580 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
19581 && TREE_CODE (decl
) == TYPE_DECL
)
19583 result
= TREE_TYPE (decl
);
19584 if (result
== error_mark_node
)
19585 result
= NULL_TREE
;
19587 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
19588 && DECL_CLASS_TEMPLATE_P (decl
))
19592 /* Obtain the template and the arguments. */
19593 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
19594 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
19595 /* Instantiate the template. */
19596 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
19597 /*entering_scope=*/0,
19598 tf_error
| tf_user
);
19599 if (result
== error_mark_node
)
19600 result
= NULL_TREE
;
19603 /* Leave the SCOPE. */
19605 pop_scope (pushed_scope
);
19607 /* If we failed to resolve it, return the original typename. */
19611 /* If lookup found a typename type, resolve that too. */
19612 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
19614 /* Ill-formed programs can cause infinite recursion here, so we
19615 must catch that. */
19616 TYPENAME_IS_RESOLVING_P (type
) = 1;
19617 result
= resolve_typename_type (result
, only_current_p
);
19618 TYPENAME_IS_RESOLVING_P (type
) = 0;
19621 /* Qualify the resulting type. */
19622 quals
= cp_type_quals (type
);
19624 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
19629 /* EXPR is an expression which is not type-dependent. Return a proxy
19630 for EXPR that can be used to compute the types of larger
19631 expressions containing EXPR. */
19634 build_non_dependent_expr (tree expr
)
19638 #ifdef ENABLE_CHECKING
19639 /* Try to get a constant value for all non-type-dependent expressions in
19640 order to expose bugs in *_dependent_expression_p and constexpr. */
19641 if (cxx_dialect
>= cxx0x
)
19642 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
19645 /* Preserve OVERLOADs; the functions must be available to resolve
19648 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
19649 inner_expr
= stmt_expr_value_expr (inner_expr
);
19650 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
19651 inner_expr
= TREE_OPERAND (inner_expr
, 0);
19652 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
19653 inner_expr
= TREE_OPERAND (inner_expr
, 1);
19654 if (is_overloaded_fn (inner_expr
)
19655 || TREE_CODE (inner_expr
) == OFFSET_REF
)
19657 /* There is no need to return a proxy for a variable. */
19658 if (TREE_CODE (expr
) == VAR_DECL
)
19660 /* Preserve string constants; conversions from string constants to
19661 "char *" are allowed, even though normally a "const char *"
19662 cannot be used to initialize a "char *". */
19663 if (TREE_CODE (expr
) == STRING_CST
)
19665 /* Preserve arithmetic constants, as an optimization -- there is no
19666 reason to create a new node. */
19667 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
19669 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
19670 There is at least one place where we want to know that a
19671 particular expression is a throw-expression: when checking a ?:
19672 expression, there are special rules if the second or third
19673 argument is a throw-expression. */
19674 if (TREE_CODE (expr
) == THROW_EXPR
)
19677 /* Don't wrap an initializer list, we need to be able to look inside. */
19678 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
19681 if (TREE_CODE (expr
) == COND_EXPR
)
19682 return build3 (COND_EXPR
,
19684 TREE_OPERAND (expr
, 0),
19685 (TREE_OPERAND (expr
, 1)
19686 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
19687 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
19688 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
19689 if (TREE_CODE (expr
) == COMPOUND_EXPR
19690 && !COMPOUND_EXPR_OVERLOADED (expr
))
19691 return build2 (COMPOUND_EXPR
,
19693 TREE_OPERAND (expr
, 0),
19694 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
19696 /* If the type is unknown, it can't really be non-dependent */
19697 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
19699 /* Otherwise, build a NON_DEPENDENT_EXPR. */
19700 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
19703 /* ARGS is a vector of expressions as arguments to a function call.
19704 Replace the arguments with equivalent non-dependent expressions.
19705 This modifies ARGS in place. */
19708 make_args_non_dependent (VEC(tree
,gc
) *args
)
19713 FOR_EACH_VEC_ELT (tree
, args
, ix
, arg
)
19715 tree newarg
= build_non_dependent_expr (arg
);
19717 VEC_replace (tree
, args
, ix
, newarg
);
19721 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
19722 with a level one deeper than the actual template parms. */
19727 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
19728 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
19729 TYPE_DECL
, get_identifier ("auto"), au
);
19730 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
19731 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
19732 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
19733 0, TYPE_NAME (au
), NULL_TREE
);
19734 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
19735 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
19736 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
19741 /* Given type ARG, return std::initializer_list<ARG>. */
19746 tree std_init_list
= namespace_binding
19747 (get_identifier ("initializer_list"), std_node
);
19749 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
19751 error ("deducing from brace-enclosed initializer list requires "
19752 "#include <initializer_list>");
19753 return error_mark_node
;
19755 argvec
= make_tree_vec (1);
19756 TREE_VEC_ELT (argvec
, 0) = arg
;
19757 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
19758 NULL_TREE
, 0, tf_warning_or_error
);
19761 /* Replace auto in TYPE with std::initializer_list<auto>. */
19764 listify_autos (tree type
, tree auto_node
)
19766 tree init_auto
= listify (auto_node
);
19767 tree argvec
= make_tree_vec (1);
19768 TREE_VEC_ELT (argvec
, 0) = init_auto
;
19769 if (processing_template_decl
)
19770 argvec
= add_to_template_args (current_template_args (), argvec
);
19771 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
19774 /* walk_tree helper for do_auto_deduction. */
19777 contains_auto_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
19780 /* Is this a variable with the type we're looking for? */
19782 && TREE_TYPE (*tp
) == type
)
19788 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
19789 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
19792 do_auto_deduction (tree type
, tree init
, tree auto_node
)
19794 tree parms
, tparms
, targs
;
19799 if (processing_template_decl
19800 && (TREE_TYPE (init
) == NULL_TREE
19801 || BRACE_ENCLOSED_INITIALIZER_P (init
)))
19802 /* Not enough information to try this yet. */
19805 /* The name of the object being declared shall not appear in the
19806 initializer expression. */
19807 decl
= cp_walk_tree_without_duplicates (&init
, contains_auto_r
, type
);
19810 error ("variable %q#D with %<auto%> type used in its own "
19811 "initializer", decl
);
19812 return error_mark_node
;
19815 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
19816 with either a new invented type template parameter U or, if the
19817 initializer is a braced-init-list (8.5.4), with
19818 std::initializer_list<U>. */
19819 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
19820 type
= listify_autos (type
, auto_node
);
19822 init
= resolve_nondeduced_context (init
);
19824 parms
= build_tree_list (NULL_TREE
, type
);
19826 tparms
= make_tree_vec (1);
19827 targs
= make_tree_vec (1);
19828 TREE_VEC_ELT (tparms
, 0)
19829 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
19830 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
19831 DEDUCE_CALL
, LOOKUP_NORMAL
,
19832 /*explain_p=*/false);
19835 if (processing_template_decl
)
19836 /* Try again at instantiation time. */
19838 if (type
&& type
!= error_mark_node
)
19839 /* If type is error_mark_node a diagnostic must have been
19840 emitted by now. Also, having a mention to '<type error>'
19841 in the diagnostic is not really useful to the user. */
19842 error ("unable to deduce %qT from %qE", type
, init
);
19843 return error_mark_node
;
19846 /* If the list of declarators contains more than one declarator, the type
19847 of each declared variable is determined as described above. If the
19848 type deduced for the template parameter U is not the same in each
19849 deduction, the program is ill-formed. */
19850 if (TREE_TYPE (auto_node
)
19851 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
19853 error ("inconsistent deduction for %qT: %qT and then %qT",
19854 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
19855 return error_mark_node
;
19857 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
19859 if (processing_template_decl
)
19860 targs
= add_to_template_args (current_template_args (), targs
);
19861 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
19864 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
19868 splice_late_return_type (tree type
, tree late_return_type
)
19872 if (late_return_type
== NULL_TREE
)
19874 argvec
= make_tree_vec (1);
19875 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
19876 if (processing_template_parmlist
)
19877 /* For a late-specified return type in a template type-parameter, we
19878 need to add a dummy argument level for its parmlist. */
19879 argvec
= add_to_template_args
19880 (make_tree_vec (processing_template_parmlist
), argvec
);
19881 if (current_template_parms
)
19882 argvec
= add_to_template_args (current_template_args (), argvec
);
19883 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
19886 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
19889 is_auto (const_tree type
)
19891 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19892 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
19898 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
19899 appear as a type-specifier for the declaration in question, we don't
19900 have to look through the whole type. */
19903 type_uses_auto (tree type
)
19905 enum tree_code code
;
19906 if (is_auto (type
))
19909 code
= TREE_CODE (type
);
19911 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
19912 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
19913 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
19914 return type_uses_auto (TREE_TYPE (type
));
19916 if (TYPE_PTRMEMFUNC_P (type
))
19917 return type_uses_auto (TREE_TYPE (TREE_TYPE
19918 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
19923 /* For a given template T, return the vector of typedefs referenced
19924 in T for which access check is needed at T instantiation time.
19925 T is either a FUNCTION_DECL or a RECORD_TYPE.
19926 Those typedefs were added to T by the function
19927 append_type_to_template_for_access_check. */
19929 VEC(qualified_typedef_usage_t
,gc
)*
19930 get_types_needing_access_check (tree t
)
19933 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
19935 if (!t
|| t
== error_mark_node
)
19938 if (!(ti
= get_template_info (t
)))
19941 if (CLASS_TYPE_P (t
)
19942 || TREE_CODE (t
) == FUNCTION_DECL
)
19944 if (!TI_TEMPLATE (ti
))
19947 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
19953 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
19954 tied to T. That list of typedefs will be access checked at
19955 T instantiation time.
19956 T is either a FUNCTION_DECL or a RECORD_TYPE.
19957 TYPE_DECL is a TYPE_DECL node representing a typedef.
19958 SCOPE is the scope through which TYPE_DECL is accessed.
19959 LOCATION is the location of the usage point of TYPE_DECL.
19961 This function is a subroutine of
19962 append_type_to_template_for_access_check. */
19965 append_type_to_template_for_access_check_1 (tree t
,
19968 location_t location
)
19970 qualified_typedef_usage_t typedef_usage
;
19973 if (!t
|| t
== error_mark_node
)
19976 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
19977 || CLASS_TYPE_P (t
))
19979 && TREE_CODE (type_decl
) == TYPE_DECL
19982 if (!(ti
= get_template_info (t
)))
19985 gcc_assert (TI_TEMPLATE (ti
));
19987 typedef_usage
.typedef_decl
= type_decl
;
19988 typedef_usage
.context
= scope
;
19989 typedef_usage
.locus
= location
;
19991 VEC_safe_push (qualified_typedef_usage_t
, gc
,
19992 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
19996 /* Append TYPE_DECL to the template TEMPL.
19997 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
19998 At TEMPL instanciation time, TYPE_DECL will be checked to see
19999 if it can be accessed through SCOPE.
20000 LOCATION is the location of the usage point of TYPE_DECL.
20002 e.g. consider the following code snippet:
20009 template<class U> struct S
20011 C::myint mi; // <-- usage point of the typedef C::myint
20016 At S<char> instantiation time, we need to check the access of C::myint
20017 In other words, we need to check the access of the myint typedef through
20018 the C scope. For that purpose, this function will add the myint typedef
20019 and the scope C through which its being accessed to a list of typedefs
20020 tied to the template S. That list will be walked at template instantiation
20021 time and access check performed on each typedefs it contains.
20022 Note that this particular code snippet should yield an error because
20023 myint is private to C. */
20026 append_type_to_template_for_access_check (tree templ
,
20029 location_t location
)
20031 qualified_typedef_usage_t
*iter
;
20034 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20036 /* Make sure we don't append the type to the template twice. */
20037 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
20038 get_types_needing_access_check (templ
),
20040 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20043 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20047 /* Set up the hash tables for template instantiations. */
20050 init_template_processing (void)
20052 decl_specializations
= htab_create_ggc (37,
20053 hash_specialization
,
20054 eq_specializations
,
20056 type_specializations
= htab_create_ggc (37,
20057 hash_specialization
,
20058 eq_specializations
,
20062 /* Print stats about the template hash tables for -fstats. */
20065 print_template_statistics (void)
20067 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20068 "%f collisions\n", (long) htab_size (decl_specializations
),
20069 (long) htab_elements (decl_specializations
),
20070 htab_collisions (decl_specializations
));
20071 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20072 "%f collisions\n", (long) htab_size (type_specializations
),
20073 (long) htab_elements (type_specializations
),
20074 htab_collisions (type_specializations
));
20077 #include "gt-cp-pt.h"