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, 2012
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 struct pointer_map_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
, unification_kind_t
, 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 bool dependent_template_arg_p (tree
);
193 static bool any_template_arguments_need_structural_equality_p (tree
);
194 static bool dependent_type_p_r (tree
);
195 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
196 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
198 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
199 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
200 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
202 static tree
listify (tree
);
203 static tree
listify_autos (tree
, tree
);
204 static tree
template_parm_to_arg (tree t
);
205 static bool arg_from_parm_pack_p (tree
, tree
);
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
))
817 /* Consider non-class instantiations of alias templates as
820 && TYPE_TEMPLATE_INFO (type
)
821 && DECL_LANG_SPECIFIC (TYPE_NAME (type
))
822 && DECL_USE_TEMPLATE (TYPE_NAME (type
))))
824 /* This is for ordinary explicit specialization and partial
825 specialization of a template class such as:
827 template <> class C<int>;
831 template <class T> class C<T*>;
833 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
835 if (CLASS_TYPE_P (type
)
836 && CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
837 && !COMPLETE_TYPE_P (type
))
839 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
840 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
841 if (processing_template_decl
)
843 if (push_template_decl (TYPE_MAIN_DECL (type
))
845 return error_mark_node
;
848 else if (CLASS_TYPE_P (type
)
849 && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
850 error ("specialization of %qT after instantiation", type
);
852 if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
854 error ("partial specialization of alias template %qD",
855 TYPE_TI_TEMPLATE (type
));
856 return error_mark_node
;
859 else if (CLASS_TYPE_P (type
)
860 && !CLASSTYPE_USE_TEMPLATE (type
)
861 && CLASSTYPE_TEMPLATE_INFO (type
)
862 && context
&& CLASS_TYPE_P (context
)
863 && CLASSTYPE_TEMPLATE_INFO (context
))
865 /* This is for an explicit specialization of member class
866 template according to [temp.expl.spec/18]:
868 template <> template <class U> class C<int>::D;
870 The context `C<int>' must be an implicit instantiation.
871 Otherwise this is just a member class template declared
874 template <> class C<int> { template <class U> class D; };
875 template <> template <class U> class C<int>::D;
877 In the first case, `C<int>::D' is a specialization of `C<T>::D'
878 while in the second case, `C<int>::D' is a primary template
879 and `C<T>::D' may not exist. */
881 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
882 && !COMPLETE_TYPE_P (type
))
885 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
887 if (current_namespace
888 != decl_namespace_context (tmpl
))
890 permerror (input_location
, "specializing %q#T in different namespace", type
);
891 permerror (input_location
, " from definition of %q+#D", tmpl
);
894 /* Check for invalid specialization after instantiation:
896 template <> template <> class C<int>::D<int>;
897 template <> template <class U> class C<int>::D; */
899 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
900 t
; t
= TREE_CHAIN (t
))
902 tree inst
= TREE_VALUE (t
);
903 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
905 /* We already have a full specialization of this partial
906 instantiation. Reassign it to the new member
907 specialization template. */
912 elt
.tmpl
= most_general_template (tmpl
);
913 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
916 htab_remove_elt (type_specializations
, &elt
);
919 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
921 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
922 entry
= ggc_alloc_spec_entry ();
926 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
927 /* But if we've had an implicit instantiation, that's a
928 problem ([temp.expl.spec]/6). */
929 error ("specialization %qT after instantiation %qT",
933 /* Mark TYPE as a specialization. And as a result, we only
934 have one level of template argument for the innermost
936 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
937 CLASSTYPE_TI_ARGS (type
)
938 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
941 else if (processing_specialization
)
943 /* Someday C++0x may allow for enum template specialization. */
944 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
945 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
946 pedwarn (input_location
, OPT_pedantic
, "template specialization "
947 "of %qD not allowed by ISO C++", type
);
950 error ("explicit specialization of non-template %qT", type
);
951 return error_mark_node
;
958 /* Returns nonzero if we can optimize the retrieval of specializations
959 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
960 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
963 optimize_specialization_lookup_p (tree tmpl
)
965 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
966 && DECL_CLASS_SCOPE_P (tmpl
)
967 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
969 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
970 /* The optimized lookup depends on the fact that the
971 template arguments for the member function template apply
972 purely to the containing class, which is not true if the
973 containing class is an explicit or partial
975 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
976 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
977 && !DECL_CONV_FN_P (tmpl
)
978 /* It is possible to have a template that is not a member
979 template and is not a member of a template class:
981 template <typename T>
982 struct S { friend A::f(); };
984 Here, the friend function is a template, but the context does
985 not have template information. The optimized lookup relies
986 on having ARGS be the template arguments for both the class
987 and the function template. */
988 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
991 /* Retrieve the specialization (in the sense of [temp.spec] - a
992 specialization is either an instantiation or an explicit
993 specialization) of TMPL for the given template ARGS. If there is
994 no such specialization, return NULL_TREE. The ARGS are a vector of
995 arguments, or a vector of vectors of arguments, in the case of
996 templates with more than one level of parameters.
998 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
999 then we search for a partial specialization matching ARGS. This
1000 parameter is ignored if TMPL is not a class template. */
1003 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1005 if (args
== error_mark_node
)
1008 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1010 /* There should be as many levels of arguments as there are
1011 levels of parameters. */
1012 gcc_assert (TMPL_ARGS_DEPTH (args
)
1013 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
1015 if (optimize_specialization_lookup_p (tmpl
))
1017 tree class_template
;
1018 tree class_specialization
;
1019 VEC(tree
,gc
) *methods
;
1023 /* The template arguments actually apply to the containing
1024 class. Find the class specialization with those
1026 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1027 class_specialization
1028 = retrieve_specialization (class_template
, args
, 0);
1029 if (!class_specialization
)
1031 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1032 for the specialization. */
1033 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1036 /* Iterate through the methods with the indicated name, looking
1037 for the one that has an instance of TMPL. */
1038 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1039 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
1041 tree fn
= OVL_CURRENT (fns
);
1042 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1043 /* using-declarations can add base methods to the method vec,
1044 and we don't want those here. */
1045 && DECL_CONTEXT (fn
) == class_specialization
)
1054 htab_t specializations
;
1058 elt
.spec
= NULL_TREE
;
1060 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1061 specializations
= type_specializations
;
1063 specializations
= decl_specializations
;
1066 hash
= hash_specialization (&elt
);
1067 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1075 /* Like retrieve_specialization, but for local declarations. */
1078 retrieve_local_specialization (tree tmpl
)
1082 if (local_specializations
== NULL
)
1085 slot
= pointer_map_contains (local_specializations
, tmpl
);
1086 return slot
? (tree
) *slot
: NULL_TREE
;
1089 /* Returns nonzero iff DECL is a specialization of TMPL. */
1092 is_specialization_of (tree decl
, tree tmpl
)
1096 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1100 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1106 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1108 for (t
= TREE_TYPE (decl
);
1110 t
= CLASSTYPE_USE_TEMPLATE (t
)
1111 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1112 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1119 /* Returns nonzero iff DECL is a specialization of friend declaration
1120 FRIEND_DECL according to [temp.friend]. */
1123 is_specialization_of_friend (tree decl
, tree friend_decl
)
1125 bool need_template
= true;
1128 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1129 || TREE_CODE (decl
) == TYPE_DECL
);
1131 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1132 of a template class, we want to check if DECL is a specialization
1134 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1135 && DECL_TEMPLATE_INFO (friend_decl
)
1136 && !DECL_USE_TEMPLATE (friend_decl
))
1138 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1139 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1140 need_template
= false;
1142 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1143 && !PRIMARY_TEMPLATE_P (friend_decl
))
1144 need_template
= false;
1146 /* There is nothing to do if this is not a template friend. */
1147 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1150 if (is_specialization_of (decl
, friend_decl
))
1154 A member of a class template may be declared to be a friend of a
1155 non-template class. In this case, the corresponding member of
1156 every specialization of the class template is a friend of the
1157 class granting friendship.
1159 For example, given a template friend declaration
1161 template <class T> friend void A<T>::f();
1163 the member function below is considered a friend
1165 template <> struct A<int> {
1169 For this type of template friend, TEMPLATE_DEPTH below will be
1170 nonzero. To determine if DECL is a friend of FRIEND, we first
1171 check if the enclosing class is a specialization of another. */
1173 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1175 && DECL_CLASS_SCOPE_P (decl
)
1176 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1177 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1179 /* Next, we check the members themselves. In order to handle
1180 a few tricky cases, such as when FRIEND_DECL's are
1182 template <class T> friend void A<T>::g(T t);
1183 template <class T> template <T t> friend void A<T>::h();
1187 void A<int>::g(int);
1188 template <int> void A<int>::h();
1190 we need to figure out ARGS, the template arguments from
1191 the context of DECL. This is required for template substitution
1192 of `T' in the function parameter of `g' and template parameter
1193 of `h' in the above examples. Here ARGS corresponds to `int'. */
1195 tree context
= DECL_CONTEXT (decl
);
1196 tree args
= NULL_TREE
;
1197 int current_depth
= 0;
1199 while (current_depth
< template_depth
)
1201 if (CLASSTYPE_TEMPLATE_INFO (context
))
1203 if (current_depth
== 0)
1204 args
= TYPE_TI_ARGS (context
);
1206 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1209 context
= TYPE_CONTEXT (context
);
1212 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1217 tree friend_args_type
;
1218 tree decl_args_type
;
1220 /* Make sure that both DECL and FRIEND_DECL are templates or
1222 is_template
= DECL_TEMPLATE_INFO (decl
)
1223 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1224 if (need_template
^ is_template
)
1226 else if (is_template
)
1228 /* If both are templates, check template parameter list. */
1230 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1232 if (!comp_template_parms
1233 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1237 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1240 decl_type
= TREE_TYPE (decl
);
1242 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1243 tf_none
, NULL_TREE
);
1244 if (friend_type
== error_mark_node
)
1247 /* Check if return types match. */
1248 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1251 /* Check if function parameter types match, ignoring the
1252 `this' parameter. */
1253 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1254 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1255 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1256 friend_args_type
= TREE_CHAIN (friend_args_type
);
1257 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1258 decl_args_type
= TREE_CHAIN (decl_args_type
);
1260 return compparms (decl_args_type
, friend_args_type
);
1264 /* DECL is a TYPE_DECL */
1266 tree decl_type
= TREE_TYPE (decl
);
1268 /* Make sure that both DECL and FRIEND_DECL are templates or
1271 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1272 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1274 if (need_template
^ is_template
)
1276 else if (is_template
)
1279 /* If both are templates, check the name of the two
1280 TEMPLATE_DECL's first because is_friend didn't. */
1281 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1282 != DECL_NAME (friend_decl
))
1285 /* Now check template parameter list. */
1287 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1289 return comp_template_parms
1290 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1294 return (DECL_NAME (decl
)
1295 == DECL_NAME (friend_decl
));
1301 /* Register the specialization SPEC as a specialization of TMPL with
1302 the indicated ARGS. IS_FRIEND indicates whether the specialization
1303 is actually just a friend declaration. Returns SPEC, or an
1304 equivalent prior declaration, if available. */
1307 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1314 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1316 if (TREE_CODE (spec
) == FUNCTION_DECL
1317 && uses_template_parms (DECL_TI_ARGS (spec
)))
1318 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1319 register it; we want the corresponding TEMPLATE_DECL instead.
1320 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1321 the more obvious `uses_template_parms (spec)' to avoid problems
1322 with default function arguments. In particular, given
1323 something like this:
1325 template <class T> void f(T t1, T t = T())
1327 the default argument expression is not substituted for in an
1328 instantiation unless and until it is actually needed. */
1331 if (optimize_specialization_lookup_p (tmpl
))
1332 /* We don't put these specializations in the hash table, but we might
1333 want to give an error about a mismatch. */
1334 fn
= retrieve_specialization (tmpl
, args
, 0);
1342 hash
= hash_specialization (&elt
);
1345 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1347 fn
= ((spec_entry
*) *slot
)->spec
;
1352 /* We can sometimes try to re-register a specialization that we've
1353 already got. In particular, regenerate_decl_from_template calls
1354 duplicate_decls which will update the specialization list. But,
1355 we'll still get called again here anyhow. It's more convenient
1356 to simply allow this than to try to prevent it. */
1359 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1361 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1363 if (DECL_ODR_USED (fn
)
1364 || DECL_EXPLICIT_INSTANTIATION (fn
))
1366 error ("specialization of %qD after instantiation",
1368 return error_mark_node
;
1373 /* This situation should occur only if the first
1374 specialization is an implicit instantiation, the
1375 second is an explicit specialization, and the
1376 implicit instantiation has not yet been used. That
1377 situation can occur if we have implicitly
1378 instantiated a member function and then specialized
1381 We can also wind up here if a friend declaration that
1382 looked like an instantiation turns out to be a
1385 template <class T> void foo(T);
1386 class S { friend void foo<>(int) };
1387 template <> void foo(int);
1389 We transform the existing DECL in place so that any
1390 pointers to it become pointers to the updated
1393 If there was a definition for the template, but not
1394 for the specialization, we want this to look as if
1395 there were no definition, and vice versa. */
1396 DECL_INITIAL (fn
) = NULL_TREE
;
1397 duplicate_decls (spec
, fn
, is_friend
);
1398 /* The call to duplicate_decls will have applied
1401 An explicit specialization of a function template
1402 is inline only if it is explicitly declared to be,
1403 and independently of whether its function template
1406 to the primary function; now copy the inline bits to
1407 the various clones. */
1408 FOR_EACH_CLONE (clone
, fn
)
1410 DECL_DECLARED_INLINE_P (clone
)
1411 = DECL_DECLARED_INLINE_P (fn
);
1412 DECL_SOURCE_LOCATION (clone
)
1413 = DECL_SOURCE_LOCATION (fn
);
1415 check_specialization_namespace (fn
);
1420 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1422 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1423 /* Dup decl failed, but this is a new definition. Set the
1424 line number so any errors match this new
1426 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1432 return duplicate_decls (spec
, fn
, is_friend
);
1434 /* A specialization must be declared in the same namespace as the
1435 template it is specializing. */
1436 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1437 && !check_specialization_namespace (tmpl
))
1438 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1440 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1442 spec_entry
*entry
= ggc_alloc_spec_entry ();
1443 gcc_assert (tmpl
&& args
&& spec
);
1446 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1447 && PRIMARY_TEMPLATE_P (tmpl
)
1448 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1449 /* TMPL is a forward declaration of a template function; keep a list
1450 of all specializations in case we need to reassign them to a friend
1451 template later in tsubst_friend_function. */
1452 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1453 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1459 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1460 TMPL and ARGS members, ignores SPEC. */
1463 eq_specializations (const void *p1
, const void *p2
)
1465 const spec_entry
*e1
= (const spec_entry
*)p1
;
1466 const spec_entry
*e2
= (const spec_entry
*)p2
;
1468 return (e1
->tmpl
== e2
->tmpl
1469 && comp_template_args (e1
->args
, e2
->args
));
1472 /* Returns a hash for a template TMPL and template arguments ARGS. */
1475 hash_tmpl_and_args (tree tmpl
, tree args
)
1477 hashval_t val
= DECL_UID (tmpl
);
1478 return iterative_hash_template_arg (args
, val
);
1481 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1485 hash_specialization (const void *p
)
1487 const spec_entry
*e
= (const spec_entry
*)p
;
1488 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1491 /* Recursively calculate a hash value for a template argument ARG, for use
1492 in the hash tables of template specializations. */
1495 iterative_hash_template_arg (tree arg
, hashval_t val
)
1497 unsigned HOST_WIDE_INT i
;
1498 enum tree_code code
;
1501 if (arg
== NULL_TREE
)
1502 return iterative_hash_object (arg
, val
);
1507 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1508 /* We can get one of these when re-hashing a previous entry in the middle
1509 of substituting into a pack expansion. Just look through it. */
1510 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1512 code
= TREE_CODE (arg
);
1513 tclass
= TREE_CODE_CLASS (code
);
1515 val
= iterative_hash_object (code
, val
);
1522 case IDENTIFIER_NODE
:
1523 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1527 int i
, len
= TREE_VEC_LENGTH (arg
);
1528 for (i
= 0; i
< len
; ++i
)
1529 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1533 case TYPE_PACK_EXPANSION
:
1534 case EXPR_PACK_EXPANSION
:
1535 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1536 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1538 case TYPE_ARGUMENT_PACK
:
1539 case NONTYPE_ARGUMENT_PACK
:
1540 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1543 for (; arg
; arg
= TREE_CHAIN (arg
))
1544 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1548 for (; arg
; arg
= OVL_NEXT (arg
))
1549 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1555 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1557 val
= iterative_hash_template_arg (field
, val
);
1558 val
= iterative_hash_template_arg (value
, val
);
1564 if (!DECL_ARTIFICIAL (arg
))
1566 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1567 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1569 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1572 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1575 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1576 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1578 case TEMPLATE_PARM_INDEX
:
1579 val
= iterative_hash_template_arg
1580 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1581 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1582 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1585 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1586 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1587 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1590 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1592 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1596 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1597 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1598 val
= iterative_hash_object (code
, val
);
1599 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1602 /* A lambda can't appear in a template arg, but don't crash on
1604 gcc_assert (seen_error ());
1608 case IMPLICIT_CONV_EXPR
:
1609 case STATIC_CAST_EXPR
:
1610 case REINTERPRET_CAST_EXPR
:
1611 case CONST_CAST_EXPR
:
1612 case DYNAMIC_CAST_EXPR
:
1614 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1615 /* Now hash operands as usual. */
1625 if (TYPE_CANONICAL (arg
))
1626 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1628 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1629 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1630 /* Otherwise just compare the types during lookup. */
1633 case tcc_declaration
:
1635 return iterative_hash_expr (arg
, val
);
1638 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1640 unsigned n
= cp_tree_operand_length (arg
);
1641 for (i
= 0; i
< n
; ++i
)
1642 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1650 /* Unregister the specialization SPEC as a specialization of TMPL.
1651 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1652 if the SPEC was listed as a specialization of TMPL.
1654 Note that SPEC has been ggc_freed, so we can't look inside it. */
1657 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1662 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1663 elt
.args
= TI_ARGS (tinfo
);
1664 elt
.spec
= NULL_TREE
;
1666 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1669 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1670 gcc_assert (new_spec
!= NULL_TREE
);
1671 entry
->spec
= new_spec
;
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
= pointer_map_insert (local_specializations
, tmpl
);
1690 /* TYPE is a class type. Returns true if TYPE is an explicitly
1691 specialized class. */
1694 explicit_class_specialization_p (tree type
)
1696 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1698 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1701 /* Print the list of functions at FNS, going through all the overloads
1702 for each element of the list. Alternatively, FNS can not be a
1703 TREE_LIST, in which case it will be printed together with all the
1706 MORE and *STR should respectively be FALSE and NULL when the function
1707 is called from the outside. They are used internally on recursive
1708 calls. print_candidates manages the two parameters and leaves NULL
1709 in *STR when it ends. */
1712 print_candidates_1 (tree fns
, bool more
, const char **str
)
1715 char *spaces
= NULL
;
1717 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1718 if (TREE_CODE (fn
) == TREE_LIST
)
1720 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1721 print_candidates_1 (TREE_VALUE (fn2
),
1722 TREE_CHAIN (fn2
) || more
, str
);
1728 /* Pick the prefix string. */
1729 if (!more
&& !OVL_NEXT (fns
))
1731 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1735 *str
= _("candidates are:");
1736 spaces
= get_spaces (*str
);
1738 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1739 *str
= spaces
? spaces
: *str
;
1749 /* Print the list of candidate FNS in an error message. FNS can also
1750 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1753 print_candidates (tree fns
)
1755 const char *str
= NULL
;
1756 print_candidates_1 (fns
, false, &str
);
1757 gcc_assert (str
== NULL
);
1760 /* Returns the template (one of the functions given by TEMPLATE_ID)
1761 which can be specialized to match the indicated DECL with the
1762 explicit template args given in TEMPLATE_ID. The DECL may be
1763 NULL_TREE if none is available. In that case, the functions in
1764 TEMPLATE_ID are non-members.
1766 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1767 specialization of a member template.
1769 The TEMPLATE_COUNT is the number of references to qualifying
1770 template classes that appeared in the name of the function. See
1771 check_explicit_specialization for a more accurate description.
1773 TSK indicates what kind of template declaration (if any) is being
1774 declared. TSK_TEMPLATE indicates that the declaration given by
1775 DECL, though a FUNCTION_DECL, has template parameters, and is
1776 therefore a template function.
1778 The template args (those explicitly specified and those deduced)
1779 are output in a newly created vector *TARGS_OUT.
1781 If it is impossible to determine the result, an error message is
1782 issued. The error_mark_node is returned to indicate failure. */
1785 determine_specialization (tree template_id
,
1788 int need_member_template
,
1794 tree explicit_targs
;
1795 tree candidates
= NULL_TREE
;
1796 /* A TREE_LIST of templates of which DECL may be a specialization.
1797 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1798 corresponding TREE_PURPOSE is the set of template arguments that,
1799 when used to instantiate the template, would produce a function
1800 with the signature of DECL. */
1801 tree templates
= NULL_TREE
;
1803 cp_binding_level
*b
;
1805 *targs_out
= NULL_TREE
;
1807 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1808 return error_mark_node
;
1810 fns
= TREE_OPERAND (template_id
, 0);
1811 explicit_targs
= TREE_OPERAND (template_id
, 1);
1813 if (fns
== error_mark_node
)
1814 return error_mark_node
;
1816 /* Check for baselinks. */
1817 if (BASELINK_P (fns
))
1818 fns
= BASELINK_FUNCTIONS (fns
);
1820 if (!is_overloaded_fn (fns
))
1822 error ("%qD is not a function template", fns
);
1823 return error_mark_node
;
1826 /* Count the number of template headers specified for this
1829 for (b
= current_binding_level
;
1830 b
->kind
== sk_template_parms
;
1834 for (; fns
; fns
= OVL_NEXT (fns
))
1836 tree fn
= OVL_CURRENT (fns
);
1838 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1840 tree decl_arg_types
;
1843 /* In case of explicit specialization, we need to check if
1844 the number of template headers appearing in the specialization
1845 is correct. This is usually done in check_explicit_specialization,
1846 but the check done there cannot be exhaustive when specializing
1847 member functions. Consider the following code:
1849 template <> void A<int>::f(int);
1850 template <> template <> void A<int>::f(int);
1852 Assuming that A<int> is not itself an explicit specialization
1853 already, the first line specializes "f" which is a non-template
1854 member function, whilst the second line specializes "f" which
1855 is a template member function. So both lines are syntactically
1856 correct, and check_explicit_specialization does not reject
1859 Here, we can do better, as we are matching the specialization
1860 against the declarations. We count the number of template
1861 headers, and we check if they match TEMPLATE_COUNT + 1
1862 (TEMPLATE_COUNT is the number of qualifying template classes,
1863 plus there must be another header for the member template
1866 Notice that if header_count is zero, this is not a
1867 specialization but rather a template instantiation, so there
1868 is no check we can perform here. */
1869 if (header_count
&& header_count
!= template_count
+ 1)
1872 /* Check that the number of template arguments at the
1873 innermost level for DECL is the same as for FN. */
1874 if (current_binding_level
->kind
== sk_template_parms
1875 && !current_binding_level
->explicit_spec_p
1876 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1877 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1878 (current_template_parms
))))
1881 /* DECL might be a specialization of FN. */
1882 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1883 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1885 /* For a non-static member function, we need to make sure
1886 that the const qualification is the same. Since
1887 get_bindings does not try to merge the "this" parameter,
1888 we must do the comparison explicitly. */
1889 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1890 && !same_type_p (TREE_VALUE (fn_arg_types
),
1891 TREE_VALUE (decl_arg_types
)))
1894 /* Skip the "this" parameter and, for constructors of
1895 classes with virtual bases, the VTT parameter. A
1896 full specialization of a constructor will have a VTT
1897 parameter, but a template never will. */
1899 = skip_artificial_parms_for (decl
, decl_arg_types
);
1901 = skip_artificial_parms_for (fn
, fn_arg_types
);
1903 /* Check that the number of function parameters matches.
1905 template <class T> void f(int i = 0);
1906 template <> void f<int>();
1907 The specialization f<int> is invalid but is not caught
1908 by get_bindings below. */
1909 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1912 /* Function templates cannot be specializations; there are
1913 no partial specializations of functions. Therefore, if
1914 the type of DECL does not match FN, there is no
1916 if (tsk
== tsk_template
)
1918 if (compparms (fn_arg_types
, decl_arg_types
))
1919 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1923 /* See whether this function might be a specialization of this
1925 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1928 /* We cannot deduce template arguments that when used to
1929 specialize TMPL will produce DECL. */
1932 /* Save this template, and the arguments deduced. */
1933 templates
= tree_cons (targs
, fn
, templates
);
1935 else if (need_member_template
)
1936 /* FN is an ordinary member function, and we need a
1937 specialization of a member template. */
1939 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1940 /* We can get IDENTIFIER_NODEs here in certain erroneous
1943 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1944 /* This is just an ordinary non-member function. Nothing can
1945 be a specialization of that. */
1947 else if (DECL_ARTIFICIAL (fn
))
1948 /* Cannot specialize functions that are created implicitly. */
1952 tree decl_arg_types
;
1954 /* This is an ordinary member function. However, since
1955 we're here, we can assume it's enclosing class is a
1956 template class. For example,
1958 template <typename T> struct S { void f(); };
1959 template <> void S<int>::f() {}
1961 Here, S<int>::f is a non-template, but S<int> is a
1962 template class. If FN has the same type as DECL, we
1963 might be in business. */
1965 if (!DECL_TEMPLATE_INFO (fn
))
1966 /* Its enclosing class is an explicit specialization
1967 of a template class. This is not a candidate. */
1970 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1971 TREE_TYPE (TREE_TYPE (fn
))))
1972 /* The return types differ. */
1975 /* Adjust the type of DECL in case FN is a static member. */
1976 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1977 if (DECL_STATIC_FUNCTION_P (fn
)
1978 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1979 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1981 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1984 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1988 if (templates
&& TREE_CHAIN (templates
))
1994 It is possible for a specialization with a given function
1995 signature to be instantiated from more than one function
1996 template. In such cases, explicit specification of the
1997 template arguments must be used to uniquely identify the
1998 function template specialization being specialized.
2000 Note that here, there's no suggestion that we're supposed to
2001 determine which of the candidate templates is most
2002 specialized. However, we, also have:
2006 Partial ordering of overloaded function template
2007 declarations is used in the following contexts to select
2008 the function template to which a function template
2009 specialization refers:
2011 -- when an explicit specialization refers to a function
2014 So, we do use the partial ordering rules, at least for now.
2015 This extension can only serve to make invalid programs valid,
2016 so it's safe. And, there is strong anecdotal evidence that
2017 the committee intended the partial ordering rules to apply;
2018 the EDG front end has that behavior, and John Spicer claims
2019 that the committee simply forgot to delete the wording in
2020 [temp.expl.spec]. */
2021 tree tmpl
= most_specialized_instantiation (templates
);
2022 if (tmpl
!= error_mark_node
)
2025 TREE_CHAIN (templates
) = NULL_TREE
;
2029 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2031 error ("template-id %qD for %q+D does not match any template "
2032 "declaration", template_id
, decl
);
2033 if (header_count
&& header_count
!= template_count
+ 1)
2034 inform (input_location
, "saw %d %<template<>%>, need %d for "
2035 "specializing a member function template",
2036 header_count
, template_count
+ 1);
2037 return error_mark_node
;
2039 else if ((templates
&& TREE_CHAIN (templates
))
2040 || (candidates
&& TREE_CHAIN (candidates
))
2041 || (templates
&& candidates
))
2043 error ("ambiguous template specialization %qD for %q+D",
2045 candidates
= chainon (candidates
, templates
);
2046 print_candidates (candidates
);
2047 return error_mark_node
;
2050 /* We have one, and exactly one, match. */
2053 tree fn
= TREE_VALUE (candidates
);
2054 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2055 /* DECL is a re-declaration or partial instantiation of a template
2057 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2059 /* It was a specialization of an ordinary member function in a
2061 return DECL_TI_TEMPLATE (fn
);
2064 /* It was a specialization of a template. */
2065 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2066 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2068 *targs_out
= copy_node (targs
);
2069 SET_TMPL_ARGS_LEVEL (*targs_out
,
2070 TMPL_ARGS_DEPTH (*targs_out
),
2071 TREE_PURPOSE (templates
));
2074 *targs_out
= TREE_PURPOSE (templates
);
2075 return TREE_VALUE (templates
);
2078 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2079 but with the default argument values filled in from those in the
2083 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2086 tree new_spec_types
;
2091 if (spec_types
== void_list_node
)
2092 return void_list_node
;
2094 /* Substitute into the rest of the list. */
2096 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2097 TREE_CHAIN (tmpl_types
));
2099 /* Add the default argument for this parameter. */
2100 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2101 TREE_VALUE (spec_types
),
2105 /* DECL is an explicit specialization. Replicate default arguments
2106 from the template it specializes. (That way, code like:
2108 template <class T> void f(T = 3);
2109 template <> void f(double);
2112 works, as required.) An alternative approach would be to look up
2113 the correct default arguments at the call-site, but this approach
2114 is consistent with how implicit instantiations are handled. */
2117 copy_default_args_to_explicit_spec (tree decl
)
2122 tree new_spec_types
;
2126 tree object_type
= NULL_TREE
;
2127 tree in_charge
= NULL_TREE
;
2128 tree vtt
= NULL_TREE
;
2130 /* See if there's anything we need to do. */
2131 tmpl
= DECL_TI_TEMPLATE (decl
);
2132 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2133 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2134 if (TREE_PURPOSE (t
))
2139 old_type
= TREE_TYPE (decl
);
2140 spec_types
= TYPE_ARG_TYPES (old_type
);
2142 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2144 /* Remove the this pointer, but remember the object's type for
2146 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2147 spec_types
= TREE_CHAIN (spec_types
);
2148 tmpl_types
= TREE_CHAIN (tmpl_types
);
2150 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2152 /* DECL may contain more parameters than TMPL due to the extra
2153 in-charge parameter in constructors and destructors. */
2154 in_charge
= spec_types
;
2155 spec_types
= TREE_CHAIN (spec_types
);
2157 if (DECL_HAS_VTT_PARM_P (decl
))
2160 spec_types
= TREE_CHAIN (spec_types
);
2164 /* Compute the merged default arguments. */
2166 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2168 /* Compute the new FUNCTION_TYPE. */
2172 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2177 /* Put the in-charge parameter back. */
2178 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2179 TREE_VALUE (in_charge
),
2182 new_type
= build_method_type_directly (object_type
,
2183 TREE_TYPE (old_type
),
2187 new_type
= build_function_type (TREE_TYPE (old_type
),
2189 new_type
= cp_build_type_attribute_variant (new_type
,
2190 TYPE_ATTRIBUTES (old_type
));
2191 new_type
= build_exception_variant (new_type
,
2192 TYPE_RAISES_EXCEPTIONS (old_type
));
2193 TREE_TYPE (decl
) = new_type
;
2196 /* Check to see if the function just declared, as indicated in
2197 DECLARATOR, and in DECL, is a specialization of a function
2198 template. We may also discover that the declaration is an explicit
2199 instantiation at this point.
2201 Returns DECL, or an equivalent declaration that should be used
2202 instead if all goes well. Issues an error message if something is
2203 amiss. Returns error_mark_node if the error is not easily
2206 FLAGS is a bitmask consisting of the following flags:
2208 2: The function has a definition.
2209 4: The function is a friend.
2211 The TEMPLATE_COUNT is the number of references to qualifying
2212 template classes that appeared in the name of the function. For
2215 template <class T> struct S { void f(); };
2218 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2219 classes are not counted in the TEMPLATE_COUNT, so that in
2221 template <class T> struct S {};
2222 template <> struct S<int> { void f(); }
2223 template <> void S<int>::f();
2225 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2226 invalid; there should be no template <>.)
2228 If the function is a specialization, it is marked as such via
2229 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2230 is set up correctly, and it is added to the list of specializations
2231 for that template. */
2234 check_explicit_specialization (tree declarator
,
2239 int have_def
= flags
& 2;
2240 int is_friend
= flags
& 4;
2241 int specialization
= 0;
2242 int explicit_instantiation
= 0;
2243 int member_specialization
= 0;
2244 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2245 tree dname
= DECL_NAME (decl
);
2250 if (!processing_specialization
)
2253 tsk
= tsk_excessive_parms
;
2256 tsk
= current_tmpl_spec_kind (template_count
);
2261 if (processing_specialization
)
2264 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2266 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2269 /* This could be something like:
2271 template <class T> void f(T);
2272 class S { friend void f<>(int); } */
2276 /* This case handles bogus declarations like template <>
2277 template <class T> void f<int>(); */
2279 error ("template-id %qD in declaration of primary template",
2286 case tsk_invalid_member_spec
:
2287 /* The error has already been reported in
2288 check_specialization_scope. */
2289 return error_mark_node
;
2291 case tsk_invalid_expl_inst
:
2292 error ("template parameter list used in explicit instantiation");
2298 error ("definition provided for explicit instantiation");
2300 explicit_instantiation
= 1;
2303 case tsk_excessive_parms
:
2304 case tsk_insufficient_parms
:
2305 if (tsk
== tsk_excessive_parms
)
2306 error ("too many template parameter lists in declaration of %qD",
2308 else if (template_header_count
)
2309 error("too few template parameter lists in declaration of %qD", decl
);
2311 error("explicit specialization of %qD must be introduced by "
2312 "%<template <>%>", decl
);
2316 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2318 member_specialization
= 1;
2324 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2326 /* This case handles bogus declarations like template <>
2327 template <class T> void f<int>(); */
2329 if (uses_template_parms (declarator
))
2330 error ("function template partial specialization %qD "
2331 "is not allowed", declarator
);
2333 error ("template-id %qD in declaration of primary template",
2338 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2339 /* This is a specialization of a member template, without
2340 specialization the containing class. Something like:
2342 template <class T> struct S {
2343 template <class U> void f (U);
2345 template <> template <class U> void S<int>::f(U) {}
2347 That's a specialization -- but of the entire template. */
2355 if (specialization
|| member_specialization
)
2357 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2358 for (; t
; t
= TREE_CHAIN (t
))
2359 if (TREE_PURPOSE (t
))
2361 permerror (input_location
,
2362 "default argument specified in explicit specialization");
2367 if (specialization
|| member_specialization
|| explicit_instantiation
)
2369 tree tmpl
= NULL_TREE
;
2370 tree targs
= NULL_TREE
;
2372 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2373 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2377 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2382 /* If there is no class context, the explicit instantiation
2383 must be at namespace scope. */
2384 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2386 /* Find the namespace binding, using the declaration
2388 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2390 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2392 error ("%qD is not a template function", dname
);
2393 fns
= error_mark_node
;
2397 tree fn
= OVL_CURRENT (fns
);
2398 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2399 CP_DECL_CONTEXT (fn
)))
2400 error ("%qD is not declared in %qD",
2401 decl
, current_namespace
);
2405 declarator
= lookup_template_function (fns
, NULL_TREE
);
2408 if (declarator
== error_mark_node
)
2409 return error_mark_node
;
2411 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2413 if (!explicit_instantiation
)
2414 /* A specialization in class scope. This is invalid,
2415 but the error will already have been flagged by
2416 check_specialization_scope. */
2417 return error_mark_node
;
2420 /* It's not valid to write an explicit instantiation in
2423 class C { template void f(); }
2425 This case is caught by the parser. However, on
2428 template class C { void f(); };
2430 (which is invalid) we can get here. The error will be
2437 else if (ctype
!= NULL_TREE
2438 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2441 /* Find the list of functions in ctype that have the same
2442 name as the declared function. */
2443 tree name
= TREE_OPERAND (declarator
, 0);
2444 tree fns
= NULL_TREE
;
2447 if (constructor_name_p (name
, ctype
))
2449 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2451 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2452 : !CLASSTYPE_DESTRUCTORS (ctype
))
2454 /* From [temp.expl.spec]:
2456 If such an explicit specialization for the member
2457 of a class template names an implicitly-declared
2458 special member function (clause _special_), the
2459 program is ill-formed.
2461 Similar language is found in [temp.explicit]. */
2462 error ("specialization of implicitly-declared special member function");
2463 return error_mark_node
;
2466 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2469 if (!DECL_CONV_FN_P (decl
))
2471 idx
= lookup_fnfields_1 (ctype
, name
);
2473 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2477 VEC(tree
,gc
) *methods
;
2480 /* For a type-conversion operator, we cannot do a
2481 name-based lookup. We might be looking for `operator
2482 int' which will be a specialization of `operator T'.
2483 So, we find *all* the conversion operators, and then
2484 select from them. */
2487 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2489 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2490 VEC_iterate (tree
, methods
, idx
, ovl
);
2493 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2494 /* There are no more conversion functions. */
2497 /* Glue all these conversion functions together
2498 with those we already have. */
2499 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2500 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2504 if (fns
== NULL_TREE
)
2506 error ("no member function %qD declared in %qT", name
, ctype
);
2507 return error_mark_node
;
2510 TREE_OPERAND (declarator
, 0) = fns
;
2513 /* Figure out what exactly is being specialized at this point.
2514 Note that for an explicit instantiation, even one for a
2515 member function, we cannot tell apriori whether the
2516 instantiation is for a member template, or just a member
2517 function of a template class. Even if a member template is
2518 being instantiated, the member template arguments may be
2519 elided if they can be deduced from the rest of the
2521 tmpl
= determine_specialization (declarator
, decl
,
2523 member_specialization
,
2527 if (!tmpl
|| tmpl
== error_mark_node
)
2528 /* We couldn't figure out what this declaration was
2530 return error_mark_node
;
2533 tree gen_tmpl
= most_general_template (tmpl
);
2535 if (explicit_instantiation
)
2537 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2538 is done by do_decl_instantiation later. */
2540 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2541 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2543 if (arg_depth
> parm_depth
)
2545 /* If TMPL is not the most general template (for
2546 example, if TMPL is a friend template that is
2547 injected into namespace scope), then there will
2548 be too many levels of TARGS. Remove some of them
2553 new_targs
= make_tree_vec (parm_depth
);
2554 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2555 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2556 = TREE_VEC_ELT (targs
, i
);
2560 return instantiate_template (tmpl
, targs
, tf_error
);
2563 /* If we thought that the DECL was a member function, but it
2564 turns out to be specializing a static member function,
2565 make DECL a static member function as well. */
2566 if (DECL_STATIC_FUNCTION_P (tmpl
)
2567 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2568 revert_static_member_fn (decl
);
2570 /* If this is a specialization of a member template of a
2571 template class, we want to return the TEMPLATE_DECL, not
2572 the specialization of it. */
2573 if (tsk
== tsk_template
)
2575 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2576 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2577 DECL_INITIAL (result
) = NULL_TREE
;
2581 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2582 DECL_SOURCE_LOCATION (result
)
2583 = DECL_SOURCE_LOCATION (decl
);
2584 /* We want to use the argument list specified in the
2585 definition, not in the original declaration. */
2586 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2587 for (parm
= DECL_ARGUMENTS (result
); parm
;
2588 parm
= DECL_CHAIN (parm
))
2589 DECL_CONTEXT (parm
) = result
;
2591 return register_specialization (tmpl
, gen_tmpl
, targs
,
2595 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2596 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2598 /* Inherit default function arguments from the template
2599 DECL is specializing. */
2600 copy_default_args_to_explicit_spec (decl
);
2602 /* This specialization has the same protection as the
2603 template it specializes. */
2604 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2605 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2607 /* 7.1.1-1 [dcl.stc]
2609 A storage-class-specifier shall not be specified in an
2610 explicit specialization...
2612 The parser rejects these, so unless action is taken here,
2613 explicit function specializations will always appear with
2616 The action recommended by the C++ CWG in response to C++
2617 defect report 605 is to make the storage class and linkage
2618 of the explicit specialization match the templated function:
2620 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2622 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2624 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2625 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2627 /* This specialization has the same linkage and visibility as
2628 the function template it specializes. */
2629 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2630 if (! TREE_PUBLIC (decl
))
2632 DECL_INTERFACE_KNOWN (decl
) = 1;
2633 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2635 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2636 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2638 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2639 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2643 /* If DECL is a friend declaration, declared using an
2644 unqualified name, the namespace associated with DECL may
2645 have been set incorrectly. For example, in:
2647 template <typename T> void f(T);
2649 struct S { friend void f<int>(int); }
2652 we will have set the DECL_CONTEXT for the friend
2653 declaration to N, rather than to the global namespace. */
2654 if (DECL_NAMESPACE_SCOPE_P (decl
))
2655 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2657 if (is_friend
&& !have_def
)
2658 /* This is not really a declaration of a specialization.
2659 It's just the name of an instantiation. But, it's not
2660 a request for an instantiation, either. */
2661 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2662 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2663 /* This is indeed a specialization. In case of constructors
2664 and destructors, we need in-charge and not-in-charge
2665 versions in V3 ABI. */
2666 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2668 /* Register this specialization so that we can find it
2670 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2677 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2678 parameters. These are represented in the same format used for
2679 DECL_TEMPLATE_PARMS. */
2682 comp_template_parms (const_tree parms1
, const_tree parms2
)
2687 if (parms1
== parms2
)
2690 for (p1
= parms1
, p2
= parms2
;
2691 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2692 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2694 tree t1
= TREE_VALUE (p1
);
2695 tree t2
= TREE_VALUE (p2
);
2698 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2699 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2701 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2704 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2706 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2707 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2709 /* If either of the template parameters are invalid, assume
2710 they match for the sake of error recovery. */
2711 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2714 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2717 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2718 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2719 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2721 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2726 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2727 /* One set of parameters has more parameters lists than the
2734 /* Determine whether PARM is a parameter pack. */
2737 template_parameter_pack_p (const_tree parm
)
2739 /* Determine if we have a non-type template parameter pack. */
2740 if (TREE_CODE (parm
) == PARM_DECL
)
2741 return (DECL_TEMPLATE_PARM_P (parm
)
2742 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2743 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2744 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2746 /* If this is a list of template parameters, we could get a
2747 TYPE_DECL or a TEMPLATE_DECL. */
2748 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2749 parm
= TREE_TYPE (parm
);
2751 /* Otherwise it must be a type template parameter. */
2752 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2753 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2754 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2757 /* Determine if T is a function parameter pack. */
2760 function_parameter_pack_p (const_tree t
)
2762 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2763 return FUNCTION_PARAMETER_PACK_P (t
);
2767 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2768 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2771 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2773 if (! primary_func_tmpl_inst
2774 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2775 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2778 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2781 /* Return true iff the function parameter PARAM_DECL was expanded
2782 from the function parameter pack PACK. */
2785 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2787 if (DECL_ARTIFICIAL (param_decl
)
2788 || !function_parameter_pack_p (pack
))
2791 /* The parameter pack and its pack arguments have the same
2793 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2796 /* Determine whether ARGS describes a variadic template args list,
2797 i.e., one that is terminated by a template argument pack. */
2800 template_args_variadic_p (tree args
)
2805 if (args
== NULL_TREE
)
2808 args
= INNERMOST_TEMPLATE_ARGS (args
);
2809 nargs
= TREE_VEC_LENGTH (args
);
2814 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2816 return ARGUMENT_PACK_P (last_parm
);
2819 /* Generate a new name for the parameter pack name NAME (an
2820 IDENTIFIER_NODE) that incorporates its */
2823 make_ith_pack_parameter_name (tree name
, int i
)
2825 /* Munge the name to include the parameter index. */
2826 #define NUMBUF_LEN 128
2827 char numbuf
[NUMBUF_LEN
];
2831 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2832 newname_len
= IDENTIFIER_LENGTH (name
)
2833 + strlen (numbuf
) + 2;
2834 newname
= (char*)alloca (newname_len
);
2835 snprintf (newname
, newname_len
,
2836 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2837 return get_identifier (newname
);
2840 /* Return true if T is a primary function, class or alias template
2844 primary_template_instantiation_p (const_tree t
)
2849 if (TREE_CODE (t
) == FUNCTION_DECL
)
2850 return DECL_LANG_SPECIFIC (t
)
2851 && DECL_TEMPLATE_INSTANTIATION (t
)
2852 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2853 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2854 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2855 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2857 && TYPE_TEMPLATE_INFO (t
)
2858 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
2859 && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t
)))
2864 /* Return true if PARM is a template template parameter. */
2867 template_template_parameter_p (const_tree parm
)
2869 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2872 /* Return true iff PARM is a DECL representing a type template
2876 template_type_parameter_p (const_tree parm
)
2879 && (TREE_CODE (parm
) == TYPE_DECL
2880 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2881 && DECL_TEMPLATE_PARM_P (parm
));
2884 /* Return the template parameters of T if T is a
2885 primary template instantiation, NULL otherwise. */
2888 get_primary_template_innermost_parameters (const_tree t
)
2890 tree parms
= NULL
, template_info
= NULL
;
2892 if ((template_info
= get_template_info (t
))
2893 && primary_template_instantiation_p (t
))
2894 parms
= INNERMOST_TEMPLATE_PARMS
2895 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2900 /* Return the template parameters of the LEVELth level from the full list
2901 of template parameters PARMS. */
2904 get_template_parms_at_level (tree parms
, int level
)
2908 || TREE_CODE (parms
) != TREE_LIST
2909 || level
> TMPL_PARMS_DEPTH (parms
))
2912 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2913 if (TMPL_PARMS_DEPTH (p
) == level
)
2919 /* Returns the template arguments of T if T is a template instantiation,
2923 get_template_innermost_arguments (const_tree t
)
2925 tree args
= NULL
, template_info
= NULL
;
2927 if ((template_info
= get_template_info (t
))
2928 && TI_ARGS (template_info
))
2929 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2934 /* Return the argument pack elements of T if T is a template argument pack,
2938 get_template_argument_pack_elems (const_tree t
)
2940 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2941 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2944 return ARGUMENT_PACK_ARGS (t
);
2947 /* Structure used to track the progress of find_parameter_packs_r. */
2948 struct find_parameter_pack_data
2950 /* TREE_LIST that will contain all of the parameter packs found by
2952 tree
* parameter_packs
;
2954 /* Set of AST nodes that have been visited by the traversal. */
2955 struct pointer_set_t
*visited
;
2958 /* Identifies all of the argument packs that occur in a template
2959 argument and appends them to the TREE_LIST inside DATA, which is a
2960 find_parameter_pack_data structure. This is a subroutine of
2961 make_pack_expansion and uses_parameter_packs. */
2963 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2966 struct find_parameter_pack_data
* ppd
=
2967 (struct find_parameter_pack_data
*)data
;
2968 bool parameter_pack_p
= false;
2970 /* Handle type aliases/typedefs. */
2973 && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
2974 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2976 if (TYPE_TEMPLATE_INFO (t
))
2977 cp_walk_tree (&TYPE_TI_ARGS (t
),
2978 &find_parameter_packs_r
,
2984 /* Identify whether this is a parameter pack or not. */
2985 switch (TREE_CODE (t
))
2987 case TEMPLATE_PARM_INDEX
:
2988 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2989 parameter_pack_p
= true;
2992 case TEMPLATE_TYPE_PARM
:
2993 t
= TYPE_MAIN_VARIANT (t
);
2994 case TEMPLATE_TEMPLATE_PARM
:
2995 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2996 parameter_pack_p
= true;
3000 if (FUNCTION_PARAMETER_PACK_P (t
))
3002 /* We don't want to walk into the type of a PARM_DECL,
3003 because we don't want to see the type parameter pack. */
3005 parameter_pack_p
= true;
3010 parameter_pack_p
= true;
3013 /* Not a parameter pack. */
3017 if (parameter_pack_p
)
3019 /* Add this parameter pack to the list. */
3020 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3024 cp_walk_tree (&TYPE_CONTEXT (t
),
3025 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3027 /* This switch statement will return immediately if we don't find a
3029 switch (TREE_CODE (t
))
3031 case TEMPLATE_PARM_INDEX
:
3034 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3035 /* Check the template itself. */
3036 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3037 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3038 /* Check the template arguments. */
3039 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3044 case TEMPLATE_TYPE_PARM
:
3045 case TEMPLATE_TEMPLATE_PARM
:
3052 if (TYPE_PTRMEMFUNC_P (t
))
3058 if (TYPE_TEMPLATE_INFO (t
))
3059 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3060 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3067 cp_walk_tree (&TREE_TYPE (t
),
3068 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3072 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3077 case TYPE_PACK_EXPANSION
:
3078 case EXPR_PACK_EXPANSION
:
3083 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3088 case IDENTIFIER_NODE
:
3089 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3101 /* Determines if the expression or type T uses any parameter packs. */
3103 uses_parameter_packs (tree t
)
3105 tree parameter_packs
= NULL_TREE
;
3106 struct find_parameter_pack_data ppd
;
3107 ppd
.parameter_packs
= ¶meter_packs
;
3108 ppd
.visited
= pointer_set_create ();
3109 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3110 pointer_set_destroy (ppd
.visited
);
3111 return parameter_packs
!= NULL_TREE
;
3114 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3115 representation a base-class initializer into a parameter pack
3116 expansion. If all goes well, the resulting node will be an
3117 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3120 make_pack_expansion (tree arg
)
3123 tree parameter_packs
= NULL_TREE
;
3124 bool for_types
= false;
3125 struct find_parameter_pack_data ppd
;
3127 if (!arg
|| arg
== error_mark_node
)
3130 if (TREE_CODE (arg
) == TREE_LIST
)
3132 /* The only time we will see a TREE_LIST here is for a base
3133 class initializer. In this case, the TREE_PURPOSE will be a
3134 _TYPE node (representing the base class expansion we're
3135 initializing) and the TREE_VALUE will be a TREE_LIST
3136 containing the initialization arguments.
3138 The resulting expansion looks somewhat different from most
3139 expansions. Rather than returning just one _EXPANSION, we
3140 return a TREE_LIST whose TREE_PURPOSE is a
3141 TYPE_PACK_EXPANSION containing the bases that will be
3142 initialized. The TREE_VALUE will be identical to the
3143 original TREE_VALUE, which is a list of arguments that will
3144 be passed to each base. We do not introduce any new pack
3145 expansion nodes into the TREE_VALUE (although it is possible
3146 that some already exist), because the TREE_PURPOSE and
3147 TREE_VALUE all need to be expanded together with the same
3148 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3149 resulting TREE_PURPOSE will mention the parameter packs in
3150 both the bases and the arguments to the bases. */
3153 tree parameter_packs
= NULL_TREE
;
3155 /* Determine which parameter packs will be used by the base
3157 ppd
.visited
= pointer_set_create ();
3158 ppd
.parameter_packs
= ¶meter_packs
;
3159 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3162 if (parameter_packs
== NULL_TREE
)
3164 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3165 pointer_set_destroy (ppd
.visited
);
3166 return error_mark_node
;
3169 if (TREE_VALUE (arg
) != void_type_node
)
3171 /* Collect the sets of parameter packs used in each of the
3172 initialization arguments. */
3173 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3175 /* Determine which parameter packs will be expanded in this
3177 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3182 pointer_set_destroy (ppd
.visited
);
3184 /* Create the pack expansion type for the base type. */
3185 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3186 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3187 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3189 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3190 they will rarely be compared to anything. */
3191 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3193 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3196 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3199 /* Build the PACK_EXPANSION_* node. */
3201 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3202 : make_node (EXPR_PACK_EXPANSION
);
3203 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3204 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3206 /* Propagate type and const-expression information. */
3207 TREE_TYPE (result
) = TREE_TYPE (arg
);
3208 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3211 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3212 they will rarely be compared to anything. */
3213 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3215 /* Determine which parameter packs will be expanded. */
3216 ppd
.parameter_packs
= ¶meter_packs
;
3217 ppd
.visited
= pointer_set_create ();
3218 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3219 pointer_set_destroy (ppd
.visited
);
3221 /* Make sure we found some parameter packs. */
3222 if (parameter_packs
== NULL_TREE
)
3225 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3227 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3228 return error_mark_node
;
3230 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3232 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3237 /* Checks T for any "bare" parameter packs, which have not yet been
3238 expanded, and issues an error if any are found. This operation can
3239 only be done on full expressions or types (e.g., an expression
3240 statement, "if" condition, etc.), because we could have expressions like:
3242 foo(f(g(h(args)))...)
3244 where "args" is a parameter pack. check_for_bare_parameter_packs
3245 should not be called for the subexpressions args, h(args),
3246 g(h(args)), or f(g(h(args))), because we would produce erroneous
3249 Returns TRUE and emits an error if there were bare parameter packs,
3250 returns FALSE otherwise. */
3252 check_for_bare_parameter_packs (tree t
)
3254 tree parameter_packs
= NULL_TREE
;
3255 struct find_parameter_pack_data ppd
;
3257 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3260 if (TREE_CODE (t
) == TYPE_DECL
)
3263 ppd
.parameter_packs
= ¶meter_packs
;
3264 ppd
.visited
= pointer_set_create ();
3265 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3266 pointer_set_destroy (ppd
.visited
);
3268 if (parameter_packs
)
3270 error ("parameter packs not expanded with %<...%>:");
3271 while (parameter_packs
)
3273 tree pack
= TREE_VALUE (parameter_packs
);
3274 tree name
= NULL_TREE
;
3276 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3277 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3278 name
= TYPE_NAME (pack
);
3279 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3280 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3282 name
= DECL_NAME (pack
);
3285 inform (input_location
, " %qD", name
);
3287 inform (input_location
, " <anonymous>");
3289 parameter_packs
= TREE_CHAIN (parameter_packs
);
3298 /* Expand any parameter packs that occur in the template arguments in
3301 expand_template_argument_pack (tree args
)
3303 tree result_args
= NULL_TREE
;
3304 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3305 int num_result_args
= -1;
3306 int non_default_args_count
= -1;
3308 /* First, determine if we need to expand anything, and the number of
3309 slots we'll need. */
3310 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3312 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3313 if (arg
== NULL_TREE
)
3315 if (ARGUMENT_PACK_P (arg
))
3317 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3318 if (num_result_args
< 0)
3319 num_result_args
= in_arg
+ num_packed
;
3321 num_result_args
+= num_packed
;
3325 if (num_result_args
>= 0)
3330 /* If no expansion is necessary, we're done. */
3331 if (num_result_args
< 0)
3334 /* Expand arguments. */
3335 result_args
= make_tree_vec (num_result_args
);
3336 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3337 non_default_args_count
=
3338 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3339 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3341 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3342 if (ARGUMENT_PACK_P (arg
))
3344 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3345 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3346 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3347 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3348 if (non_default_args_count
> 0)
3349 non_default_args_count
+= num_packed
;
3353 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3357 if (non_default_args_count
>= 0)
3358 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3362 /* Checks if DECL shadows a template parameter.
3364 [temp.local]: A template-parameter shall not be redeclared within its
3365 scope (including nested scopes).
3367 Emits an error and returns TRUE if the DECL shadows a parameter,
3368 returns FALSE otherwise. */
3371 check_template_shadow (tree decl
)
3375 /* If we're not in a template, we can't possibly shadow a template
3377 if (!current_template_parms
)
3380 /* Figure out what we're shadowing. */
3381 if (TREE_CODE (decl
) == OVERLOAD
)
3382 decl
= OVL_CURRENT (decl
);
3383 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3385 /* If there's no previous binding for this name, we're not shadowing
3386 anything, let alone a template parameter. */
3390 /* If we're not shadowing a template parameter, we're done. Note
3391 that OLDDECL might be an OVERLOAD (or perhaps even an
3392 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3394 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3397 /* We check for decl != olddecl to avoid bogus errors for using a
3398 name inside a class. We check TPFI to avoid duplicate errors for
3399 inline member templates. */
3401 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3404 error ("declaration of %q+#D", decl
);
3405 error (" shadows template parm %q+#D", olddecl
);
3409 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3410 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3411 template parameters. */
3414 build_template_parm_index (int index
,
3421 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3422 TEMPLATE_PARM_IDX (t
) = index
;
3423 TEMPLATE_PARM_LEVEL (t
) = level
;
3424 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3425 TEMPLATE_PARM_NUM_SIBLINGS (t
) = num_siblings
;
3426 TEMPLATE_PARM_DECL (t
) = decl
;
3427 TREE_TYPE (t
) = type
;
3428 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3429 TREE_READONLY (t
) = TREE_READONLY (decl
);
3434 /* Find the canonical type parameter for the given template type
3435 parameter. Returns the canonical type parameter, which may be TYPE
3436 if no such parameter existed. */
3439 canonical_type_parameter (tree type
)
3442 int idx
= TEMPLATE_TYPE_IDX (type
);
3443 if (!canonical_template_parms
)
3444 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3446 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3447 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3449 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3450 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3451 list
= TREE_CHAIN (list
);
3454 return TREE_VALUE (list
);
3457 VEC_replace(tree
, canonical_template_parms
, idx
,
3458 tree_cons (NULL_TREE
, type
,
3459 VEC_index (tree
, canonical_template_parms
, idx
)));
3464 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3465 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3466 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3467 new one is created. */
3470 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3471 tsubst_flags_t complain
)
3473 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3474 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3475 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3476 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3478 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3481 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3482 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3483 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3484 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3485 DECL_ARTIFICIAL (decl
) = 1;
3486 SET_DECL_TEMPLATE_PARM_P (decl
);
3488 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3489 TEMPLATE_PARM_LEVEL (index
) - levels
,
3490 TEMPLATE_PARM_ORIG_LEVEL (index
),
3491 TEMPLATE_PARM_NUM_SIBLINGS (index
),
3493 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3494 TEMPLATE_PARM_PARAMETER_PACK (t
)
3495 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3497 /* Template template parameters need this. */
3498 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3499 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3500 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3504 return TEMPLATE_PARM_DESCENDANTS (index
);
3507 /* Process information from new template parameter PARM and append it
3508 to the LIST being built. This new parameter is a non-type
3509 parameter iff IS_NON_TYPE is true. This new parameter is a
3510 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3511 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3512 parameter list PARM belongs to. This is used used to create a
3513 proper canonical type for the type of PARM that is to be created,
3514 iff PARM is a type. If the size is not known, this parameter shall
3518 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3519 bool is_non_type
, bool is_parameter_pack
,
3520 unsigned num_template_parms
)
3527 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3528 defval
= TREE_PURPOSE (parm
);
3532 tree p
= tree_last (list
);
3534 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3537 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3538 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3540 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3550 parm
= TREE_VALUE (parm
);
3552 SET_DECL_TEMPLATE_PARM_P (parm
);
3554 if (TREE_TYPE (parm
) == error_mark_node
)
3556 err_parm_list
= build_tree_list (defval
, parm
);
3557 TREE_VALUE (err_parm_list
) = error_mark_node
;
3558 return chainon (list
, err_parm_list
);
3564 The top-level cv-qualifiers on the template-parameter are
3565 ignored when determining its type. */
3566 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3567 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3569 err_parm_list
= build_tree_list (defval
, parm
);
3570 TREE_VALUE (err_parm_list
) = error_mark_node
;
3571 return chainon (list
, err_parm_list
);
3574 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3576 /* This template parameter is not a parameter pack, but it
3577 should be. Complain about "bare" parameter packs. */
3578 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3580 /* Recover by calling this a parameter pack. */
3581 is_parameter_pack
= true;
3585 /* A template parameter is not modifiable. */
3586 TREE_CONSTANT (parm
) = 1;
3587 TREE_READONLY (parm
) = 1;
3588 decl
= build_decl (parm_loc
,
3589 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3590 TREE_CONSTANT (decl
) = 1;
3591 TREE_READONLY (decl
) = 1;
3592 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3593 = build_template_parm_index (idx
, processing_template_decl
,
3594 processing_template_decl
,
3596 decl
, TREE_TYPE (parm
));
3598 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3599 = is_parameter_pack
;
3604 parm
= TREE_VALUE (TREE_VALUE (parm
));
3606 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3608 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3609 /* This is for distinguishing between real templates and template
3610 template parameters */
3611 TREE_TYPE (parm
) = t
;
3612 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3617 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3618 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3619 decl
= build_decl (parm_loc
,
3620 TYPE_DECL
, parm
, t
);
3623 TYPE_NAME (t
) = decl
;
3624 TYPE_STUB_DECL (t
) = decl
;
3626 TEMPLATE_TYPE_PARM_INDEX (t
)
3627 = build_template_parm_index (idx
, processing_template_decl
,
3628 processing_template_decl
,
3630 decl
, TREE_TYPE (parm
));
3631 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3632 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3634 DECL_ARTIFICIAL (decl
) = 1;
3635 SET_DECL_TEMPLATE_PARM_P (decl
);
3637 parm
= build_tree_list (defval
, parm
);
3638 return chainon (list
, parm
);
3641 /* The end of a template parameter list has been reached. Process the
3642 tree list into a parameter vector, converting each parameter into a more
3643 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3647 end_template_parm_list (tree parms
)
3651 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3653 current_template_parms
3654 = tree_cons (size_int (processing_template_decl
),
3655 saved_parmlist
, current_template_parms
);
3657 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3659 next
= TREE_CHAIN (parm
);
3660 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3661 TREE_CHAIN (parm
) = NULL_TREE
;
3664 --processing_template_parmlist
;
3666 return saved_parmlist
;
3669 /* Create a new type almost identical to TYPE but which has the
3670 following differences:
3672 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3673 template sibling parameters of T.
3675 2/ T has a new canonical type that matches the new number
3678 3/ From now on, T is going to be what lookups referring to the
3679 name of TYPE will return. No lookup should return TYPE anymore.
3681 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3683 This is a subroutine of fixup_template_parms. */
3686 fixup_template_type_parm_type (tree type
, int num_parms
)
3688 tree orig_idx
= TEMPLATE_TYPE_PARM_INDEX (type
), idx
;
3690 /* This is the decl which name is inserted into the symbol table for
3691 the template parm type. So whenever we lookup the type name, this
3692 is the DECL we get. */
3695 /* Do not fix up the type twice. */
3696 if (orig_idx
&& TEMPLATE_PARM_NUM_SIBLINGS (orig_idx
) != 0)
3699 t
= copy_type (type
);
3700 decl
= TYPE_NAME (t
);
3702 TYPE_MAIN_VARIANT (t
) = t
;
3703 TYPE_NEXT_VARIANT (t
)= NULL_TREE
;
3704 TYPE_POINTER_TO (t
) = 0;
3705 TYPE_REFERENCE_TO (t
) = 0;
3707 idx
= build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx
),
3708 TEMPLATE_PARM_LEVEL (orig_idx
),
3709 TEMPLATE_PARM_ORIG_LEVEL (orig_idx
),
3712 TEMPLATE_PARM_DESCENDANTS (idx
) = TEMPLATE_PARM_DESCENDANTS (orig_idx
);
3713 TEMPLATE_PARM_PARAMETER_PACK (idx
) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx
);
3714 TEMPLATE_TYPE_PARM_INDEX (t
) = idx
;
3716 TYPE_STUB_DECL (t
) = decl
;
3717 TEMPLATE_TYPE_DECL (t
) = decl
;
3718 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
3719 TREE_TYPE (DECL_TEMPLATE_RESULT (decl
)) = t
;
3721 /* Update the type associated to the type name stored in the symbol
3722 table. Now, whenever the type name is looked up, the resulting
3723 type is properly fixed up. */
3724 TREE_TYPE (decl
) = t
;
3726 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3731 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3732 identical to I, but that is fixed up as to:
3734 1/ carry the number of sibling parms (NUM_PARMS) of the template
3735 parm represented by I.
3737 2/ replace all references to template parm types declared before I
3738 (in the same template parm list as I) by references to template
3739 parm types contained in ARGS. ARGS should contain the list of
3740 template parms that have been fixed up so far, in a form suitable
3741 to be passed to tsubst.
3743 This is a subroutine of fixup_template_parms. */
3746 fixup_template_parm_index (tree i
, tree args
, int num_parms
)
3748 tree index
, decl
, type
;
3751 || TREE_CODE (i
) != TEMPLATE_PARM_INDEX
3752 /* Do not fix up the index twice. */
3753 || (TEMPLATE_PARM_NUM_SIBLINGS (i
) != 0))
3756 decl
= TEMPLATE_PARM_DECL (i
);
3757 type
= TREE_TYPE (decl
);
3759 index
= build_template_parm_index (TEMPLATE_PARM_IDX (i
),
3760 TEMPLATE_PARM_LEVEL (i
),
3761 TEMPLATE_PARM_ORIG_LEVEL (i
),
3765 TEMPLATE_PARM_DESCENDANTS (index
) = TEMPLATE_PARM_DESCENDANTS (i
);
3766 TEMPLATE_PARM_PARAMETER_PACK (index
) = TEMPLATE_PARM_PARAMETER_PACK (i
);
3768 type
= tsubst (type
, args
, tf_none
, NULL_TREE
);
3770 TREE_TYPE (decl
) = type
;
3771 TREE_TYPE (index
) = type
;
3777 This is a subroutine of fixup_template_parms.
3779 It computes the canonical type of the type of the template
3780 parameter PARM_DESC and update all references to that type so that
3781 they use the newly computed canonical type. No access check is
3782 performed during the fixup. PARM_DESC is a TREE_LIST which
3783 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3784 default argument of the template parm if any. IDX is the index of
3785 the template parameter, starting at 0. NUM_PARMS is the number of
3786 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3787 TREE_VEC containing the full set of template parameters in a form
3788 suitable to be passed to substs functions as their ARGS
3789 argument. This is what current_template_args returns for a given
3790 template. The innermost vector of args in ARGLIST is the set of
3791 template parms that have been fixed up so far. This function adds
3792 the fixed up parameter into that vector. */
3795 fixup_template_parm (tree parm_desc
,
3800 tree parm
= TREE_VALUE (parm_desc
);
3801 tree fixedup_args
= INNERMOST_TEMPLATE_ARGS (arglist
);
3803 push_deferring_access_checks (dk_no_check
);
3805 if (TREE_CODE (parm
) == TYPE_DECL
)
3807 /* PARM is a template type parameter. Fix up its type, add
3808 the fixed-up template parm to the vector of fixed-up
3809 template parms so far, and substitute the fixed-up
3810 template parms into the default argument of this
3813 fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3814 TREE_TYPE (parm
) = t
;
3816 TREE_VEC_ELT (fixedup_args
, idx
) = template_parm_to_arg (parm_desc
);
3818 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3820 /* PARM is a template template parameter. This is going to
3822 tree tparms
, targs
, innermost_args
, t
;
3825 /* First, fix up the parms of the template template parm
3826 because the parms are involved in defining the new canonical
3827 type of the template template parm. */
3829 /* So we need to substitute the template parm types that have
3830 been fixed up so far into the template parms of this template
3831 template parm. E.g, consider this:
3833 template<class T, template<T u> class TT> class S;
3835 In this case we want to substitute T into the
3836 template parameters of TT.
3838 So let's walk the template parms of PARM here, and
3839 tsubst ARGLIST into into each of the template
3842 /* For this substitution we need to build the full set of
3843 template parameters and use that as arguments for the
3844 tsubsting function. */
3845 tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
3847 /* This will contain the innermost parms of PARM into which
3848 we have substituted so far. */
3849 innermost_args
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
3850 targs
= add_to_template_args (arglist
, innermost_args
);
3851 for (j
= 0; j
< TREE_VEC_LENGTH (tparms
); ++j
)
3855 parameter
= TREE_VEC_ELT (tparms
, j
);
3857 /* INNERMOST_ARGS needs to have at least the same number
3858 of elements as the index PARAMETER, ortherwise
3859 tsubsting into PARAMETER will result in partially
3860 instantiating it, reducing its tempate parm
3861 level. Let's tactically fill INNERMOST_ARGS for that
3863 TREE_VEC_ELT (innermost_args
, j
) =
3864 template_parm_to_arg (parameter
);
3866 fixup_template_parm (parameter
, j
,
3867 TREE_VEC_LENGTH (tparms
),
3871 /* Now fix up the type of the template template parm. */
3873 t
= fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3874 TREE_TYPE (parm
) = t
;
3876 TREE_VEC_ELT (fixedup_args
, idx
) =
3877 template_parm_to_arg (parm_desc
);
3879 else if (TREE_CODE (parm
) == PARM_DECL
)
3881 /* PARM is a non-type template parameter. We need to:
3883 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3884 proper number of sibling parameters.
3886 * Make lookups of the template parameter return a reference
3887 to the fixed-up index. No lookup should return references
3888 to the former index anymore.
3890 * Substitute the template parms that got fixed up so far
3892 * into the type of PARM. */
3894 tree index
= DECL_INITIAL (parm
);
3896 /* PUSHED_DECL is the decl added to the symbol table with
3897 the name of the parameter. E,g:
3899 template<class T, T u> //#0
3900 auto my_function(T t) -> decltype(u); //#1
3902 Here, when looking up u at //#1, we get the decl of u
3903 resulting from the declaration in #0. This is what
3904 PUSHED_DECL is. We need to replace the reference to the
3905 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3906 fixed-up TEMPLATE_PARM_INDEX. */
3907 tree pushed_decl
= TEMPLATE_PARM_DECL (index
);
3909 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3910 fixup the type of PUSHED_DECL as well and luckily
3911 fixup_template_parm_index does it for us too. */
3912 tree fixed_up_index
=
3913 fixup_template_parm_index (index
, arglist
, num_parms
);
3915 DECL_INITIAL (pushed_decl
) = DECL_INITIAL (parm
) = fixed_up_index
;
3917 /* Add this fixed up PARM to the template parms we've fixed
3918 up so far and use that to substitute the fixed-up
3919 template parms into the type of PARM. */
3920 TREE_VEC_ELT (fixedup_args
, idx
) =
3921 template_parm_to_arg (parm_desc
);
3922 TREE_TYPE (parm
) = tsubst (TREE_TYPE (parm
), arglist
,
3923 tf_none
, NULL_TREE
);
3926 TREE_PURPOSE (parm_desc
) =
3927 tsubst_template_arg (TREE_PURPOSE (parm_desc
),
3928 arglist
, tf_none
, parm
);
3930 pop_deferring_access_checks ();
3933 /* Walk the current template parms and properly compute the canonical
3934 types of the dependent types created during
3935 cp_parser_template_parameter_list. */
3938 fixup_template_parms (void)
3945 parameter_vec
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3946 if (parameter_vec
== NULL_TREE
)
3949 num_parms
= TREE_VEC_LENGTH (parameter_vec
);
3951 /* This vector contains the current innermost template parms that
3952 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3953 to be passed to tsubst* functions as their ARGS argument. */
3954 fixedup_args
= make_tree_vec (num_parms
);
3956 /* This vector contains the full set of template parms in a form
3957 suitable to be passed to substs functions as their ARGS
3959 arglist
= current_template_args ();
3960 arglist
= add_outermost_template_args (arglist
, fixedup_args
);
3962 /* Let's do the proper fixup now. */
3963 for (i
= 0; i
< num_parms
; ++i
)
3964 fixup_template_parm (TREE_VEC_ELT (parameter_vec
, i
),
3965 i
, num_parms
, arglist
);
3968 /* end_template_decl is called after a template declaration is seen. */
3971 end_template_decl (void)
3973 reset_specialization ();
3975 if (! processing_template_decl
)
3978 /* This matches the pushlevel in begin_template_parm_list. */
3981 --processing_template_decl
;
3982 current_template_parms
= TREE_CHAIN (current_template_parms
);
3985 /* Takes a TREE_LIST representing a template parameter and convert it
3986 into an argument suitable to be passed to the type substitution
3987 functions. Note that If the TREE_LIST contains an error_mark
3988 node, the returned argument is error_mark_node. */
3991 template_parm_to_arg (tree t
)
3995 || TREE_CODE (t
) != TREE_LIST
)
3998 if (error_operand_p (TREE_VALUE (t
)))
3999 return error_mark_node
;
4003 if (TREE_CODE (t
) == TYPE_DECL
4004 || TREE_CODE (t
) == TEMPLATE_DECL
)
4008 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4010 /* Turn this argument into a TYPE_ARGUMENT_PACK
4011 with a single element, which expands T. */
4012 tree vec
= make_tree_vec (1);
4013 #ifdef ENABLE_CHECKING
4014 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4015 (vec
, TREE_VEC_LENGTH (vec
));
4017 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4019 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4020 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4025 t
= DECL_INITIAL (t
);
4027 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4029 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4030 with a single element, which expands T. */
4031 tree vec
= make_tree_vec (1);
4032 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
4033 #ifdef ENABLE_CHECKING
4034 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4035 (vec
, TREE_VEC_LENGTH (vec
));
4037 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4039 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4040 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4041 TREE_TYPE (t
) = type
;
4047 /* This function returns TRUE if PARM_PACK is a template parameter
4048 pack and if ARG_PACK is what template_parm_to_arg returned when
4049 passed PARM_PACK. */
4052 arg_from_parm_pack_p (tree arg_pack
, tree parm_pack
)
4054 /* For clarity in the comments below let's use the representation
4055 argument_pack<elements>' to denote an argument pack and its
4058 In the 'if' block below, we want to detect cases where
4059 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
4060 check if ARG_PACK is an argument pack which sole element is
4061 the expansion of PARM_PACK. That argument pack is typically
4062 created by template_parm_to_arg when passed a parameter
4066 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
4067 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
4069 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
4070 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
4071 /* So we have an argument_pack<P...>. We want to test if P
4072 is actually PARM_PACK. We will not use cp_tree_equal to
4073 test P and PARM_PACK because during type fixup (by
4074 fixup_template_parm) P can be a pre-fixup version of a
4075 type and PARM_PACK be its post-fixup version.
4076 cp_tree_equal would consider them as different even
4077 though we would want to consider them compatible for our
4078 precise purpose here.
4080 Thus we are going to consider that P and PARM_PACK are
4081 compatible if they have the same DECL. */
4082 if ((/* If ARG_PACK is a type parameter pack named by the
4083 same DECL as parm_pack ... */
4085 && TYPE_P (parm_pack
)
4086 && TYPE_NAME (pattern
) == TYPE_NAME (parm_pack
))
4087 /* ... or if PARM_PACK is a non-type parameter named by the
4088 same DECL as ARG_PACK. Note that PARM_PACK being a
4089 non-type parameter means it's either a PARM_DECL or a
4090 TEMPLATE_PARM_INDEX. */
4091 || (TREE_CODE (pattern
) == TEMPLATE_PARM_INDEX
4092 && ((TREE_CODE (parm_pack
) == PARM_DECL
4093 && (TEMPLATE_PARM_DECL (pattern
)
4094 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack
))))
4095 || (TREE_CODE (parm_pack
) == TEMPLATE_PARM_INDEX
4096 && (TEMPLATE_PARM_DECL (pattern
)
4097 == TEMPLATE_PARM_DECL (parm_pack
))))))
4098 && template_parameter_pack_p (pattern
))
4104 /* Within the declaration of a template, return all levels of template
4105 parameters that apply. The template parameters are represented as
4106 a TREE_VEC, in the form documented in cp-tree.h for template
4110 current_template_args (void)
4113 tree args
= NULL_TREE
;
4114 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
4117 /* If there is only one level of template parameters, we do not
4118 create a TREE_VEC of TREE_VECs. Instead, we return a single
4119 TREE_VEC containing the arguments. */
4121 args
= make_tree_vec (length
);
4123 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
4125 tree a
= copy_node (TREE_VALUE (header
));
4128 TREE_TYPE (a
) = NULL_TREE
;
4129 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4130 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4132 #ifdef ENABLE_CHECKING
4133 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4137 TREE_VEC_ELT (args
, --l
) = a
;
4142 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
4143 /* This can happen for template parms of a template template
4146 template<template<class T, class U> class TT> struct S;
4148 Consider the level of the parms of TT; T and U both have
4149 level 2; TT has no template parm of level 1. So in this case
4150 the first element of full_template_args is NULL_TREE. If we
4151 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4152 of 2. This will make tsubst wrongly consider that T and U
4153 have level 1. Instead, let's create a dummy vector as the
4154 first element of full_template_args so that TMPL_ARG_DEPTH
4155 returns the correct depth for args. */
4156 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
4160 /* Update the declared TYPE by doing any lookups which were thought to be
4161 dependent, but are not now that we know the SCOPE of the declarator. */
4164 maybe_update_decl_type (tree orig_type
, tree scope
)
4166 tree type
= orig_type
;
4168 if (type
== NULL_TREE
)
4171 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4172 type
= TREE_TYPE (type
);
4174 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4175 && dependent_type_p (type
)
4176 /* Don't bother building up the args in this case. */
4177 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4179 /* tsubst in the args corresponding to the template parameters,
4180 including auto if present. Most things will be unchanged, but
4181 make_typename_type and tsubst_qualified_id will resolve
4182 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4183 tree args
= current_template_args ();
4184 tree auto_node
= type_uses_auto (type
);
4188 tree auto_vec
= make_tree_vec (1);
4189 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4190 args
= add_to_template_args (args
, auto_vec
);
4192 pushed
= push_scope (scope
);
4193 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4198 if (type
== error_mark_node
)
4201 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4203 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4206 type
= TYPE_NAME (type
);
4211 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4212 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4213 a member template. Used by push_template_decl below. */
4216 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4218 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4219 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4220 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4221 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4222 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4227 struct template_parm_data
4229 /* The level of the template parameters we are currently
4233 /* The index of the specialization argument we are currently
4237 /* An array whose size is the number of template parameters. The
4238 elements are nonzero if the parameter has been used in any one
4239 of the arguments processed so far. */
4242 /* An array whose size is the number of template arguments. The
4243 elements are nonzero if the argument makes use of template
4244 parameters of this level. */
4245 int* arg_uses_template_parms
;
4248 /* Subroutine of push_template_decl used to see if each template
4249 parameter in a partial specialization is used in the explicit
4250 argument list. If T is of the LEVEL given in DATA (which is
4251 treated as a template_parm_data*), then DATA->PARMS is marked
4255 mark_template_parm (tree t
, void* data
)
4259 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4261 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4263 level
= TEMPLATE_PARM_LEVEL (t
);
4264 idx
= TEMPLATE_PARM_IDX (t
);
4268 level
= TEMPLATE_TYPE_LEVEL (t
);
4269 idx
= TEMPLATE_TYPE_IDX (t
);
4272 if (level
== tpd
->level
)
4274 tpd
->parms
[idx
] = 1;
4275 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4278 /* Return zero so that for_each_template_parm will continue the
4279 traversal of the tree; we want to mark *every* template parm. */
4283 /* Process the partial specialization DECL. */
4286 process_partial_specialization (tree decl
)
4288 tree type
= TREE_TYPE (decl
);
4289 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4290 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4291 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4292 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4295 int nargs
= TREE_VEC_LENGTH (inner_args
);
4298 bool did_error_intro
= false;
4299 struct template_parm_data tpd
;
4300 struct template_parm_data tpd2
;
4302 gcc_assert (current_template_parms
);
4304 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4305 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4307 /* We check that each of the template parameters given in the
4308 partial specialization is used in the argument list to the
4309 specialization. For example:
4311 template <class T> struct S;
4312 template <class T> struct S<T*>;
4314 The second declaration is OK because `T*' uses the template
4315 parameter T, whereas
4317 template <class T> struct S<int>;
4319 is no good. Even trickier is:
4330 The S2<T> declaration is actually invalid; it is a
4331 full-specialization. Of course,
4334 struct S2<T (*)(U)>;
4336 or some such would have been OK. */
4337 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4338 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4339 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4341 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4342 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4343 for (i
= 0; i
< nargs
; ++i
)
4345 tpd
.current_arg
= i
;
4346 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4347 &mark_template_parm
,
4350 /*include_nondeduced_p=*/false);
4352 for (i
= 0; i
< ntparms
; ++i
)
4353 if (tpd
.parms
[i
] == 0)
4355 /* One of the template parms was not used in the
4357 if (!did_error_intro
)
4359 error ("template parameters not used in partial specialization:");
4360 did_error_intro
= true;
4363 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4366 if (did_error_intro
)
4367 return error_mark_node
;
4369 /* [temp.class.spec]
4371 The argument list of the specialization shall not be identical to
4372 the implicit argument list of the primary template. */
4373 if (comp_template_args
4375 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4377 error ("partial specialization %qT does not specialize any template arguments", type
);
4379 /* [temp.class.spec]
4381 A partially specialized non-type argument expression shall not
4382 involve template parameters of the partial specialization except
4383 when the argument expression is a simple identifier.
4385 The type of a template parameter corresponding to a specialized
4386 non-type argument shall not be dependent on a parameter of the
4389 Also, we verify that pack expansions only occur at the
4390 end of the argument list. */
4391 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4393 for (i
= 0; i
< nargs
; ++i
)
4395 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4396 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4397 tree packed_args
= NULL_TREE
;
4400 if (ARGUMENT_PACK_P (arg
))
4402 /* Extract the arguments from the argument pack. We'll be
4403 iterating over these in the following loop. */
4404 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4405 len
= TREE_VEC_LENGTH (packed_args
);
4408 for (j
= 0; j
< len
; j
++)
4411 /* Get the Jth argument in the parameter pack. */
4412 arg
= TREE_VEC_ELT (packed_args
, j
);
4414 if (PACK_EXPANSION_P (arg
))
4416 /* Pack expansions must come at the end of the
4418 if ((packed_args
&& j
< len
- 1)
4419 || (!packed_args
&& i
< nargs
- 1))
4421 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4422 error ("parameter pack argument %qE must be at the "
4423 "end of the template argument list", arg
);
4425 error ("parameter pack argument %qT must be at the "
4426 "end of the template argument list", arg
);
4430 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4431 /* We only care about the pattern. */
4432 arg
= PACK_EXPANSION_PATTERN (arg
);
4434 if (/* These first two lines are the `non-type' bit. */
4436 && TREE_CODE (arg
) != TEMPLATE_DECL
4437 /* This next line is the `argument expression is not just a
4438 simple identifier' condition and also the `specialized
4439 non-type argument' bit. */
4440 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4442 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4443 || (packed_args
&& uses_template_parms (arg
)))
4444 error ("template argument %qE involves template parameter(s)",
4448 /* Look at the corresponding template parameter,
4449 marking which template parameters its type depends
4451 tree type
= TREE_TYPE (parm
);
4455 /* We haven't yet initialized TPD2. Do so now. */
4456 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4457 /* The number of parameters here is the number in the
4458 main template, which, as checked in the assertion
4460 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4462 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4465 /* Mark the template parameters. But this time, we're
4466 looking for the template parameters of the main
4467 template, not in the specialization. */
4468 tpd2
.current_arg
= i
;
4469 tpd2
.arg_uses_template_parms
[i
] = 0;
4470 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4471 for_each_template_parm (type
,
4472 &mark_template_parm
,
4475 /*include_nondeduced_p=*/false);
4477 if (tpd2
.arg_uses_template_parms
[i
])
4479 /* The type depended on some template parameters.
4480 If they are fully specialized in the
4481 specialization, that's OK. */
4484 for (j
= 0; j
< nargs
; ++j
)
4485 if (tpd2
.parms
[j
] != 0
4486 && tpd
.arg_uses_template_parms
[j
])
4489 error_n (input_location
, count
,
4490 "type %qT of template argument %qE depends "
4491 "on a template parameter",
4492 "type %qT of template argument %qE depends "
4493 "on template parameters",
4502 /* We should only get here once. */
4503 gcc_assert (!COMPLETE_TYPE_P (type
));
4505 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4506 = tree_cons (specargs
, inner_parms
,
4507 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4508 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4510 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4511 inst
= TREE_CHAIN (inst
))
4513 tree inst_type
= TREE_VALUE (inst
);
4514 if (COMPLETE_TYPE_P (inst_type
)
4515 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4517 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4518 if (spec
&& TREE_TYPE (spec
) == type
)
4519 permerror (input_location
,
4520 "partial specialization of %qT after instantiation "
4521 "of %qT", type
, inst_type
);
4528 /* Check that a template declaration's use of default arguments and
4529 parameter packs is not invalid. Here, PARMS are the template
4530 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4531 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4535 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4536 declaration (but not a definition); 1 indicates a declaration, 2
4537 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4538 emitted for extraneous default arguments.
4540 Returns TRUE if there were no errors found, FALSE otherwise. */
4543 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
4544 int is_partial
, int is_friend_decl
)
4547 int last_level_to_check
;
4549 bool no_errors
= true;
4553 A default template-argument shall not be specified in a
4554 function template declaration or a function template definition, nor
4555 in the template-parameter-list of the definition of a member of a
4558 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4559 /* You can't have a function template declaration in a local
4560 scope, nor you can you define a member of a class template in a
4564 if (current_class_type
4565 && !TYPE_BEING_DEFINED (current_class_type
)
4566 && DECL_LANG_SPECIFIC (decl
)
4567 && DECL_DECLARES_FUNCTION_P (decl
)
4568 /* If this is either a friend defined in the scope of the class
4569 or a member function. */
4570 && (DECL_FUNCTION_MEMBER_P (decl
)
4571 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4572 : DECL_FRIEND_CONTEXT (decl
)
4573 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4575 /* And, if it was a member function, it really was defined in
4576 the scope of the class. */
4577 && (!DECL_FUNCTION_MEMBER_P (decl
)
4578 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4579 /* We already checked these parameters when the template was
4580 declared, so there's no need to do it again now. This function
4581 was defined in class scope, but we're processing it's body now
4582 that the class is complete. */
4585 /* Core issue 226 (C++0x only): the following only applies to class
4587 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4591 If a template-parameter has a default template-argument, all
4592 subsequent template-parameters shall have a default
4593 template-argument supplied. */
4594 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4596 tree inner_parms
= TREE_VALUE (parm_level
);
4597 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4598 int seen_def_arg_p
= 0;
4601 for (i
= 0; i
< ntparms
; ++i
)
4603 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4605 if (parm
== error_mark_node
)
4608 if (TREE_PURPOSE (parm
))
4610 else if (seen_def_arg_p
4611 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4613 error ("no default argument for %qD", TREE_VALUE (parm
));
4614 /* For better subsequent error-recovery, we indicate that
4615 there should have been a default argument. */
4616 TREE_PURPOSE (parm
) = error_mark_node
;
4622 /* Don't complain about an enclosing partial
4624 && parm_level
== parms
4625 && TREE_CODE (decl
) == TYPE_DECL
4627 && template_parameter_pack_p (TREE_VALUE (parm
)))
4629 /* A primary class template can only have one
4630 parameter pack, at the end of the template
4633 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4634 error ("parameter pack %qE must be at the end of the"
4635 " template parameter list", TREE_VALUE (parm
));
4637 error ("parameter pack %qT must be at the end of the"
4638 " template parameter list",
4639 TREE_TYPE (TREE_VALUE (parm
)));
4641 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4649 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4653 /* For an ordinary class template, default template arguments are
4654 allowed at the innermost level, e.g.:
4655 template <class T = int>
4657 but, in a partial specialization, they're not allowed even
4658 there, as we have in [temp.class.spec]:
4660 The template parameter list of a specialization shall not
4661 contain default template argument values.
4663 So, for a partial specialization, or for a function template
4664 (in C++98/C++03), we look at all of them. */
4667 /* But, for a primary class template that is not a partial
4668 specialization we look at all template parameters except the
4670 parms
= TREE_CHAIN (parms
);
4672 /* Figure out what error message to issue. */
4673 if (is_friend_decl
== 2)
4674 msg
= G_("default template arguments may not be used in function template "
4675 "friend re-declaration");
4676 else if (is_friend_decl
)
4677 msg
= G_("default template arguments may not be used in function template "
4678 "friend declarations");
4679 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4680 msg
= G_("default template arguments may not be used in function templates "
4681 "without -std=c++11 or -std=gnu++11");
4682 else if (is_partial
)
4683 msg
= G_("default template arguments may not be used in "
4684 "partial specializations");
4686 msg
= G_("default argument for template parameter for class enclosing %qD");
4688 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4689 /* If we're inside a class definition, there's no need to
4690 examine the parameters to the class itself. On the one
4691 hand, they will be checked when the class is defined, and,
4692 on the other, default arguments are valid in things like:
4693 template <class T = double>
4694 struct S { template <class U> void f(U); };
4695 Here the default argument for `S' has no bearing on the
4696 declaration of `f'. */
4697 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4699 /* Check everything. */
4700 last_level_to_check
= 0;
4702 for (parm_level
= parms
;
4703 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4704 parm_level
= TREE_CHAIN (parm_level
))
4706 tree inner_parms
= TREE_VALUE (parm_level
);
4710 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4711 for (i
= 0; i
< ntparms
; ++i
)
4713 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4716 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4721 if (is_friend_decl
== 2)
4728 /* Clear out the default argument so that we are not
4730 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4734 /* At this point, if we're still interested in issuing messages,
4735 they must apply to classes surrounding the object declared. */
4737 msg
= G_("default argument for template parameter for class "
4744 /* Worker for push_template_decl_real, called via
4745 for_each_template_parm. DATA is really an int, indicating the
4746 level of the parameters we are interested in. If T is a template
4747 parameter of that level, return nonzero. */
4750 template_parm_this_level_p (tree t
, void* data
)
4752 int this_level
= *(int *)data
;
4755 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4756 level
= TEMPLATE_PARM_LEVEL (t
);
4758 level
= TEMPLATE_TYPE_LEVEL (t
);
4759 return level
== this_level
;
4762 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4763 parameters given by current_template_args, or reuses a
4764 previously existing one, if appropriate. Returns the DECL, or an
4765 equivalent one, if it is replaced via a call to duplicate_decls.
4767 If IS_FRIEND is true, DECL is a friend declaration. */
4770 push_template_decl_real (tree decl
, bool is_friend
)
4778 int new_template_p
= 0;
4779 /* True if the template is a member template, in the sense of
4781 bool member_template_p
= false;
4783 if (decl
== error_mark_node
|| !current_template_parms
)
4784 return error_mark_node
;
4786 /* See if this is a partial specialization. */
4787 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4788 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4789 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4791 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4795 /* For a friend, we want the context of the friend function, not
4796 the type of which it is a friend. */
4797 ctx
= CP_DECL_CONTEXT (decl
);
4798 else if (CP_DECL_CONTEXT (decl
)
4799 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4800 /* In the case of a virtual function, we want the class in which
4802 ctx
= CP_DECL_CONTEXT (decl
);
4804 /* Otherwise, if we're currently defining some class, the DECL
4805 is assumed to be a member of the class. */
4806 ctx
= current_scope ();
4808 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4811 if (!DECL_CONTEXT (decl
))
4812 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4814 /* See if this is a primary template. */
4815 if (is_friend
&& ctx
)
4816 /* A friend template that specifies a class context, i.e.
4817 template <typename T> friend void A<T>::f();
4821 primary
= template_parm_scope_p ();
4825 if (DECL_CLASS_SCOPE_P (decl
))
4826 member_template_p
= true;
4827 if (TREE_CODE (decl
) == TYPE_DECL
4828 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4830 error ("template class without a name");
4831 return error_mark_node
;
4833 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4835 if (DECL_DESTRUCTOR_P (decl
))
4839 A destructor shall not be a member template. */
4840 error ("destructor %qD declared as member template", decl
);
4841 return error_mark_node
;
4843 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4844 && (!prototype_p (TREE_TYPE (decl
))
4845 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4846 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4847 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4848 == void_list_node
)))
4850 /* [basic.stc.dynamic.allocation]
4852 An allocation function can be a function
4853 template. ... Template allocation functions shall
4854 have two or more parameters. */
4855 error ("invalid template declaration of %qD", decl
);
4856 return error_mark_node
;
4859 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4860 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4862 else if (TREE_CODE (decl
) == TYPE_DECL
4863 && TYPE_DECL_ALIAS_P (decl
))
4864 /* alias-declaration */
4865 gcc_assert (!DECL_ARTIFICIAL (decl
));
4868 error ("template declaration of %q#D", decl
);
4869 return error_mark_node
;
4873 /* Check to see that the rules regarding the use of default
4874 arguments are not being violated. */
4875 check_default_tmpl_args (decl
, current_template_parms
,
4876 primary
, is_partial
, /*is_friend_decl=*/0);
4878 /* Ensure that there are no parameter packs in the type of this
4879 declaration that have not been expanded. */
4880 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4882 /* Check each of the arguments individually to see if there are
4883 any bare parameter packs. */
4884 tree type
= TREE_TYPE (decl
);
4885 tree arg
= DECL_ARGUMENTS (decl
);
4886 tree argtype
= TYPE_ARG_TYPES (type
);
4888 while (arg
&& argtype
)
4890 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4891 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4893 /* This is a PARM_DECL that contains unexpanded parameter
4894 packs. We have already complained about this in the
4895 check_for_bare_parameter_packs call, so just replace
4896 these types with ERROR_MARK_NODE. */
4897 TREE_TYPE (arg
) = error_mark_node
;
4898 TREE_VALUE (argtype
) = error_mark_node
;
4901 arg
= DECL_CHAIN (arg
);
4902 argtype
= TREE_CHAIN (argtype
);
4905 /* Check for bare parameter packs in the return type and the
4906 exception specifiers. */
4907 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4908 /* Errors were already issued, set return type to int
4909 as the frontend doesn't expect error_mark_node as
4911 TREE_TYPE (type
) = integer_type_node
;
4912 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4913 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4915 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4916 && TYPE_DECL_ALIAS_P (decl
))
4917 ? DECL_ORIGINAL_TYPE (decl
)
4918 : TREE_TYPE (decl
)))
4920 TREE_TYPE (decl
) = error_mark_node
;
4921 return error_mark_node
;
4925 return process_partial_specialization (decl
);
4927 args
= current_template_args ();
4930 || TREE_CODE (ctx
) == FUNCTION_DECL
4931 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4932 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4934 if (DECL_LANG_SPECIFIC (decl
)
4935 && DECL_TEMPLATE_INFO (decl
)
4936 && DECL_TI_TEMPLATE (decl
))
4937 tmpl
= DECL_TI_TEMPLATE (decl
);
4938 /* If DECL is a TYPE_DECL for a class-template, then there won't
4939 be DECL_LANG_SPECIFIC. The information equivalent to
4940 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4941 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4942 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4943 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4945 /* Since a template declaration already existed for this
4946 class-type, we must be redeclaring it here. Make sure
4947 that the redeclaration is valid. */
4948 redeclare_class_template (TREE_TYPE (decl
),
4949 current_template_parms
);
4950 /* We don't need to create a new TEMPLATE_DECL; just use the
4951 one we already had. */
4952 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4956 tmpl
= build_template_decl (decl
, current_template_parms
,
4960 if (DECL_LANG_SPECIFIC (decl
)
4961 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4963 /* A specialization of a member template of a template
4965 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4966 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4967 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4973 tree a
, t
, current
, parms
;
4975 tree tinfo
= get_template_info (decl
);
4979 error ("template definition of non-template %q#D", decl
);
4980 return error_mark_node
;
4983 tmpl
= TI_TEMPLATE (tinfo
);
4985 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4986 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4987 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4988 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4992 /* The declaration is a specialization of a member
4993 template, declared outside the class. Therefore, the
4994 innermost template arguments will be NULL, so we
4995 replace them with the arguments determined by the
4996 earlier call to check_explicit_specialization. */
4997 args
= DECL_TI_ARGS (decl
);
5000 = build_template_decl (decl
, current_template_parms
,
5002 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5003 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5004 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5005 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5006 DECL_TEMPLATE_INFO (new_tmpl
)
5007 = build_template_info (tmpl
, args
);
5009 register_specialization (new_tmpl
,
5010 most_general_template (tmpl
),
5016 /* Make sure the template headers we got make sense. */
5018 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5019 i
= TMPL_PARMS_DEPTH (parms
);
5020 if (TMPL_ARGS_DEPTH (args
) != i
)
5022 error ("expected %d levels of template parms for %q#D, got %d",
5023 i
, decl
, TMPL_ARGS_DEPTH (args
));
5026 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5028 a
= TMPL_ARGS_LEVEL (args
, i
);
5029 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5031 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5033 if (current
== decl
)
5034 error ("got %d template parameters for %q#D",
5035 TREE_VEC_LENGTH (a
), decl
);
5037 error ("got %d template parameters for %q#T",
5038 TREE_VEC_LENGTH (a
), current
);
5039 error (" but %d required", TREE_VEC_LENGTH (t
));
5040 return error_mark_node
;
5043 if (current
== decl
)
5045 else if (current
== NULL_TREE
)
5046 /* Can happen in erroneous input. */
5049 current
= (TYPE_P (current
)
5050 ? TYPE_CONTEXT (current
)
5051 : DECL_CONTEXT (current
));
5054 /* Check that the parms are used in the appropriate qualifying scopes
5055 in the declarator. */
5056 if (!comp_template_args
5058 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5061 template arguments to %qD do not match original template %qD",
5062 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5063 if (!uses_template_parms (TI_ARGS (tinfo
)))
5064 inform (input_location
, "use template<> for an explicit specialization");
5065 /* Avoid crash in import_export_decl. */
5066 DECL_INTERFACE_KNOWN (decl
) = 1;
5067 return error_mark_node
;
5071 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5072 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5074 /* Push template declarations for global functions and types. Note
5075 that we do not try to push a global template friend declared in a
5076 template class; such a thing may well depend on the template
5077 parameters of the class. */
5078 if (new_template_p
&& !ctx
5079 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5081 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5082 if (tmpl
== error_mark_node
)
5083 return error_mark_node
;
5085 /* Hide template friend classes that haven't been declared yet. */
5086 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5088 DECL_ANTICIPATED (tmpl
) = 1;
5089 DECL_FRIEND_P (tmpl
) = 1;
5095 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5098 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5099 if (DECL_CONV_FN_P (tmpl
))
5101 int depth
= TMPL_PARMS_DEPTH (parms
);
5103 /* It is a conversion operator. See if the type converted to
5104 depends on innermost template operands. */
5106 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5108 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5111 /* Give template template parms a DECL_CONTEXT of the template
5112 for which they are a parameter. */
5113 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5114 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5116 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5117 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5118 DECL_CONTEXT (parm
) = tmpl
;
5122 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5123 back to its most general template. If TMPL is a specialization,
5124 ARGS may only have the innermost set of arguments. Add the missing
5125 argument levels if necessary. */
5126 if (DECL_TEMPLATE_INFO (tmpl
))
5127 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5129 info
= build_template_info (tmpl
, args
);
5131 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5132 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5135 if (primary
&& !DECL_LANG_SPECIFIC (decl
))
5136 retrofit_lang_decl (decl
);
5137 if (DECL_LANG_SPECIFIC (decl
))
5138 DECL_TEMPLATE_INFO (decl
) = info
;
5141 return DECL_TEMPLATE_RESULT (tmpl
);
5145 push_template_decl (tree decl
)
5147 return push_template_decl_real (decl
, false);
5150 /* Called when a class template TYPE is redeclared with the indicated
5151 template PARMS, e.g.:
5153 template <class T> struct S;
5154 template <class T> struct S {}; */
5157 redeclare_class_template (tree type
, tree parms
)
5163 if (!TYPE_TEMPLATE_INFO (type
))
5165 error ("%qT is not a template type", type
);
5169 tmpl
= TYPE_TI_TEMPLATE (type
);
5170 if (!PRIMARY_TEMPLATE_P (tmpl
))
5171 /* The type is nested in some template class. Nothing to worry
5172 about here; there are no new template parameters for the nested
5178 error ("template specifiers not specified in declaration of %qD",
5183 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5184 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5186 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5188 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5189 "redeclared with %d template parameter",
5190 "redeclared with %d template parameters",
5191 TREE_VEC_LENGTH (parms
));
5192 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5193 "previous declaration %q+D used %d template parameter",
5194 "previous declaration %q+D used %d template parameters",
5195 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5199 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5206 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5207 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5210 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5211 if (tmpl_parm
== error_mark_node
)
5214 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5215 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5216 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5218 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5220 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5221 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5222 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5223 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5224 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5225 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5226 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5227 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5228 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5230 error ("template parameter %q+#D", tmpl_parm
);
5231 error ("redeclared here as %q#D", parm
);
5235 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5237 /* We have in [temp.param]:
5239 A template-parameter may not be given default arguments
5240 by two different declarations in the same scope. */
5241 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5242 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5243 "original definition appeared here");
5247 if (parm_default
!= NULL_TREE
)
5248 /* Update the previous template parameters (which are the ones
5249 that will really count) with the new default value. */
5250 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5251 else if (tmpl_default
!= NULL_TREE
)
5252 /* Update the new parameters, too; they'll be used as the
5253 parameters for any members. */
5254 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5260 /* Simplify EXPR if it is a non-dependent expression. Returns the
5261 (possibly simplified) expression. */
5264 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5266 if (expr
== NULL_TREE
)
5269 /* If we're in a template, but EXPR isn't value dependent, simplify
5270 it. We're supposed to treat:
5272 template <typename T> void f(T[1 + 1]);
5273 template <typename T> void f(T[2]);
5275 as two declarations of the same function, for example. */
5276 if (processing_template_decl
5277 && !type_dependent_expression_p (expr
)
5278 && potential_constant_expression (expr
)
5279 && !value_dependent_expression_p (expr
))
5281 HOST_WIDE_INT saved_processing_template_decl
;
5283 saved_processing_template_decl
= processing_template_decl
;
5284 processing_template_decl
= 0;
5285 expr
= tsubst_copy_and_build (expr
,
5288 /*in_decl=*/NULL_TREE
,
5289 /*function_p=*/false,
5290 /*integral_constant_expression_p=*/true);
5291 processing_template_decl
= saved_processing_template_decl
;
5297 fold_non_dependent_expr (tree expr
)
5299 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5302 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5303 template declaration, or a TYPE_DECL for an alias declaration. */
5306 alias_type_or_template_p (tree t
)
5310 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5313 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5314 || DECL_ALIAS_TEMPLATE_P (t
));
5317 /* Return TRUE iff is a specialization of an alias template. */
5320 alias_template_specialization_p (tree t
)
5324 return (primary_template_instantiation_p (t
)
5325 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5328 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5329 must be a function or a pointer-to-function type, as specified
5330 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5331 and check that the resulting function has external linkage. */
5334 convert_nontype_argument_function (tree type
, tree expr
)
5338 linkage_kind linkage
;
5340 fn
= instantiate_type (type
, fns
, tf_none
);
5341 if (fn
== error_mark_node
)
5342 return error_mark_node
;
5345 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5346 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5347 if (BASELINK_P (fn_no_ptr
))
5348 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5350 /* [temp.arg.nontype]/1
5352 A template-argument for a non-type, non-template template-parameter
5355 -- the address of an object or function with external [C++11: or
5356 internal] linkage. */
5357 linkage
= decl_linkage (fn_no_ptr
);
5358 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5360 if (cxx_dialect
>= cxx0x
)
5361 error ("%qE is not a valid template argument for type %qT "
5362 "because %qD has no linkage",
5363 expr
, type
, fn_no_ptr
);
5365 error ("%qE is not a valid template argument for type %qT "
5366 "because %qD does not have external linkage",
5367 expr
, type
, fn_no_ptr
);
5374 /* Subroutine of convert_nontype_argument.
5375 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5376 Emit an error otherwise. */
5379 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5380 tsubst_flags_t complain
)
5383 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5385 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5387 if (complain
& tf_error
)
5389 error ("%qE is not a valid template argument for type %qT",
5391 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5396 /* Returns TRUE iff the address of OP is value-dependent.
5398 14.6.2.4 [temp.dep.temp]:
5399 A non-integral non-type template-argument is dependent if its type is
5400 dependent or it has either of the following forms
5403 and contains a nested-name-specifier which specifies a class-name that
5404 names a dependent type.
5406 We generalize this to just say that the address of a member of a
5407 dependent class is value-dependent; the above doesn't cover the
5408 address of a static data member named with an unqualified-id. */
5411 has_value_dependent_address (tree op
)
5413 /* We could use get_inner_reference here, but there's no need;
5414 this is only relevant for template non-type arguments, which
5415 can only be expressed as &id-expression. */
5418 tree ctx
= CP_DECL_CONTEXT (op
);
5419 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5426 /* The next set of functions are used for providing helpful explanatory
5427 diagnostics for failed overload resolution. Their messages should be
5428 indented by two spaces for consistency with the messages in
5432 unify_success (bool explain_p ATTRIBUTE_UNUSED
)
5438 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5441 inform (input_location
,
5442 " couldn't deduce template parameter %qD", parm
);
5447 unify_invalid (bool explain_p ATTRIBUTE_UNUSED
)
5453 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5456 inform (input_location
,
5457 " types %qT and %qT have incompatible cv-qualifiers",
5463 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5466 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5471 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5474 inform (input_location
,
5475 " template parameter %qD is not a parameter pack, but "
5482 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5485 inform (input_location
,
5486 " template argument %qE does not match "
5487 "pointer-to-member constant %qE",
5493 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5496 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5501 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5504 inform (input_location
,
5505 " inconsistent parameter pack deduction with %qT and %qT",
5511 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5516 inform (input_location
,
5517 " deduced conflicting types for parameter %qT (%qT and %qT)",
5518 parm
, first
, second
);
5520 inform (input_location
,
5521 " deduced conflicting values for non-type parameter "
5522 "%qE (%qE and %qE)", parm
, first
, second
);
5528 unify_vla_arg (bool explain_p
, tree arg
)
5531 inform (input_location
,
5532 " variable-sized array type %qT is not "
5533 "a valid template argument",
5539 unify_method_type_error (bool explain_p
, tree arg
)
5542 inform (input_location
,
5543 " member function type %qT is not a valid template argument",
5549 unify_arity (bool explain_p
, int have
, int wanted
)
5552 inform_n (input_location
, wanted
,
5553 " candidate expects %d argument, %d provided",
5554 " candidate expects %d arguments, %d provided",
5560 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5562 return unify_arity (explain_p
, have
, wanted
);
5566 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5568 return unify_arity (explain_p
, have
, wanted
);
5572 unify_arg_conversion (bool explain_p
, tree to_type
,
5573 tree from_type
, tree arg
)
5576 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5577 arg
, from_type
, to_type
);
5582 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5583 tree parm
, tree arg
)
5588 case tbr_ambiguous_baseclass
:
5589 inform (input_location
, " %qT is an ambiguous base class of %qT",
5593 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5600 unify_inconsistent_template_template_parameters (bool explain_p
)
5603 inform (input_location
,
5604 " template parameters of a template template argument are "
5605 "inconsistent with other deduced template arguments");
5610 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5613 inform (input_location
,
5614 " can't deduce a template for %qT from non-template type %qT",
5620 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5623 inform (input_location
,
5624 " template argument %qE does not match %qD", arg
, parm
);
5629 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5632 inform (input_location
,
5633 " could not resolve address from overloaded function %qE",
5638 /* Attempt to convert the non-type template parameter EXPR to the
5639 indicated TYPE. If the conversion is successful, return the
5640 converted value. If the conversion is unsuccessful, return
5641 NULL_TREE if we issued an error message, or error_mark_node if we
5642 did not. We issue error messages for out-and-out bad template
5643 parameters, but not simply because the conversion failed, since we
5644 might be just trying to do argument deduction. Both TYPE and EXPR
5645 must be non-dependent.
5647 The conversion follows the special rules described in
5648 [temp.arg.nontype], and it is much more strict than an implicit
5651 This function is called twice for each template argument (see
5652 lookup_template_class for a more accurate description of this
5653 problem). This means that we need to handle expressions which
5654 are not valid in a C++ source, but can be created from the
5655 first call (for instance, casts to perform conversions). These
5656 hacks can go away after we fix the double coercion problem. */
5659 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5663 /* Detect immediately string literals as invalid non-type argument.
5664 This special-case is not needed for correctness (we would easily
5665 catch this later), but only to provide better diagnostic for this
5666 common user mistake. As suggested by DR 100, we do not mention
5667 linkage issues in the diagnostic as this is not the point. */
5668 /* FIXME we're making this OK. */
5669 if (TREE_CODE (expr
) == STRING_CST
)
5671 if (complain
& tf_error
)
5672 error ("%qE is not a valid template argument for type %qT "
5673 "because string literals can never be used in this context",
5678 /* Add the ADDR_EXPR now for the benefit of
5679 value_dependent_expression_p. */
5680 if (TYPE_PTROBV_P (type
)
5681 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5682 expr
= decay_conversion (expr
);
5684 /* If we are in a template, EXPR may be non-dependent, but still
5685 have a syntactic, rather than semantic, form. For example, EXPR
5686 might be a SCOPE_REF, rather than the VAR_DECL to which the
5687 SCOPE_REF refers. Preserving the qualifying scope is necessary
5688 so that access checking can be performed when the template is
5689 instantiated -- but here we need the resolved form so that we can
5690 convert the argument. */
5691 if (TYPE_REF_OBJ_P (type
)
5692 && has_value_dependent_address (expr
))
5693 /* If we want the address and it's value-dependent, don't fold. */;
5694 else if (!type_unknown_p (expr
))
5695 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5696 if (error_operand_p (expr
))
5697 return error_mark_node
;
5698 expr_type
= TREE_TYPE (expr
);
5699 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5700 expr
= mark_lvalue_use (expr
);
5702 expr
= mark_rvalue_use (expr
);
5704 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5705 to a non-type argument of "nullptr". */
5706 if (expr
== nullptr_node
5707 && (TYPE_PTR_P (type
) || TYPE_PTR_TO_MEMBER_P (type
)))
5708 expr
= convert (type
, expr
);
5710 /* In C++11, integral or enumeration non-type template arguments can be
5711 arbitrary constant expressions. Pointer and pointer to
5712 member arguments can be general constant expressions that evaluate
5713 to a null value, but otherwise still need to be of a specific form. */
5714 if (cxx_dialect
>= cxx0x
)
5716 if (TREE_CODE (expr
) == PTRMEM_CST
)
5717 /* A PTRMEM_CST is already constant, and a valid template
5718 argument for a parameter of pointer to member type, we just want
5719 to leave it in that form rather than lower it to a
5721 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5722 expr
= maybe_constant_value (expr
);
5723 else if (TYPE_PTR_P (type
)
5724 || TYPE_PTR_TO_MEMBER_P (type
))
5726 tree folded
= maybe_constant_value (expr
);
5727 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5728 : null_member_pointer_value_p (folded
))
5733 /* HACK: Due to double coercion, we can get a
5734 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5735 which is the tree that we built on the first call (see
5736 below when coercing to reference to object or to reference to
5737 function). We just strip everything and get to the arg.
5738 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5740 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5742 tree probe_type
, probe
= expr
;
5743 if (REFERENCE_REF_P (probe
))
5744 probe
= TREE_OPERAND (probe
, 0);
5745 probe_type
= TREE_TYPE (probe
);
5746 if (TREE_CODE (probe
) == NOP_EXPR
)
5748 /* ??? Maybe we could use convert_from_reference here, but we
5749 would need to relax its constraints because the NOP_EXPR
5750 could actually change the type to something more cv-qualified,
5751 and this is not folded by convert_from_reference. */
5752 tree addr
= TREE_OPERAND (probe
, 0);
5753 gcc_assert (TREE_CODE (probe_type
) == REFERENCE_TYPE
);
5754 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5755 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5756 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5757 (TREE_TYPE (probe_type
),
5758 TREE_TYPE (TREE_TYPE (addr
))));
5760 expr
= TREE_OPERAND (addr
, 0);
5761 expr_type
= TREE_TYPE (expr
);
5765 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5766 parameter is a pointer to object, through decay and
5767 qualification conversion. Let's strip everything. */
5768 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5771 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5772 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5773 /* Skip the ADDR_EXPR only if it is part of the decay for
5774 an array. Otherwise, it is part of the original argument
5775 in the source code. */
5776 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5777 expr
= TREE_OPERAND (expr
, 0);
5778 expr_type
= TREE_TYPE (expr
);
5781 /* [temp.arg.nontype]/5, bullet 1
5783 For a non-type template-parameter of integral or enumeration type,
5784 integral promotions (_conv.prom_) and integral conversions
5785 (_conv.integral_) are applied. */
5786 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5788 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5789 t
= maybe_constant_value (t
);
5790 if (t
!= error_mark_node
)
5793 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5794 return error_mark_node
;
5796 /* Notice that there are constant expressions like '4 % 0' which
5797 do not fold into integer constants. */
5798 if (TREE_CODE (expr
) != INTEGER_CST
)
5800 if (complain
& tf_error
)
5802 int errs
= errorcount
, warns
= warningcount
;
5803 if (processing_template_decl
5804 && !require_potential_constant_expression (expr
))
5806 expr
= cxx_constant_value (expr
);
5807 if (errorcount
> errs
|| warningcount
> warns
)
5808 inform (EXPR_LOC_OR_HERE (expr
),
5809 "in template argument for type %qT ", type
);
5810 if (expr
== error_mark_node
)
5812 /* else cxx_constant_value complained but gave us
5813 a real constant, so go ahead. */
5814 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5820 /* [temp.arg.nontype]/5, bullet 2
5822 For a non-type template-parameter of type pointer to object,
5823 qualification conversions (_conv.qual_) and the array-to-pointer
5824 conversion (_conv.array_) are applied. */
5825 else if (TYPE_PTROBV_P (type
))
5827 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5829 A template-argument for a non-type, non-template template-parameter
5830 shall be one of: [...]
5832 -- the name of a non-type template-parameter;
5833 -- the address of an object or function with external linkage, [...]
5834 expressed as "& id-expression" where the & is optional if the name
5835 refers to a function or array, or if the corresponding
5836 template-parameter is a reference.
5838 Here, we do not care about functions, as they are invalid anyway
5839 for a parameter of type pointer-to-object. */
5841 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5842 /* Non-type template parameters are OK. */
5844 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5845 /* Null pointer values are OK in C++11. */;
5846 else if (TREE_CODE (expr
) != ADDR_EXPR
5847 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5849 if (TREE_CODE (expr
) == VAR_DECL
)
5851 error ("%qD is not a valid template argument "
5852 "because %qD is a variable, not the address of "
5857 /* Other values, like integer constants, might be valid
5858 non-type arguments of some other type. */
5859 return error_mark_node
;
5865 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5866 ? TREE_OPERAND (expr
, 0) : expr
);
5867 if (TREE_CODE (decl
) != VAR_DECL
)
5869 error ("%qE is not a valid template argument of type %qT "
5870 "because %qE is not a variable",
5874 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5876 error ("%qE is not a valid template argument of type %qT "
5877 "because %qD does not have external linkage",
5881 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5883 error ("%qE is not a valid template argument of type %qT "
5884 "because %qD has no linkage",
5890 expr
= decay_conversion (expr
);
5891 if (expr
== error_mark_node
)
5892 return error_mark_node
;
5894 expr
= perform_qualification_conversions (type
, expr
);
5895 if (expr
== error_mark_node
)
5896 return error_mark_node
;
5898 /* [temp.arg.nontype]/5, bullet 3
5900 For a non-type template-parameter of type reference to object, no
5901 conversions apply. The type referred to by the reference may be more
5902 cv-qualified than the (otherwise identical) type of the
5903 template-argument. The template-parameter is bound directly to the
5904 template-argument, which must be an lvalue. */
5905 else if (TYPE_REF_OBJ_P (type
))
5907 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5909 return error_mark_node
;
5911 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5913 error ("%qE is not a valid template argument for type %qT "
5914 "because of conflicts in cv-qualification", expr
, type
);
5918 if (!real_lvalue_p (expr
))
5920 error ("%qE is not a valid template argument for type %qT "
5921 "because it is not an lvalue", expr
, type
);
5925 /* [temp.arg.nontype]/1
5927 A template-argument for a non-type, non-template template-parameter
5928 shall be one of: [...]
5930 -- the address of an object or function with external linkage. */
5931 if (TREE_CODE (expr
) == INDIRECT_REF
5932 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5934 expr
= TREE_OPERAND (expr
, 0);
5937 error ("%q#D is not a valid template argument for type %qT "
5938 "because a reference variable does not have a constant "
5939 "address", expr
, type
);
5946 error ("%qE is not a valid template argument for type %qT "
5947 "because it is not an object with external linkage",
5952 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5954 error ("%qE is not a valid template argument for type %qT "
5955 "because object %qD has not external linkage",
5960 expr
= build_nop (type
, build_address (expr
));
5962 /* [temp.arg.nontype]/5, bullet 4
5964 For a non-type template-parameter of type pointer to function, only
5965 the function-to-pointer conversion (_conv.func_) is applied. If the
5966 template-argument represents a set of overloaded functions (or a
5967 pointer to such), the matching function is selected from the set
5969 else if (TYPE_PTRFN_P (type
))
5971 /* If the argument is a template-id, we might not have enough
5972 context information to decay the pointer. */
5973 if (!type_unknown_p (expr_type
))
5975 expr
= decay_conversion (expr
);
5976 if (expr
== error_mark_node
)
5977 return error_mark_node
;
5980 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5981 /* Null pointer values are OK in C++11. */
5982 return perform_qualification_conversions (type
, expr
);
5984 expr
= convert_nontype_argument_function (type
, expr
);
5985 if (!expr
|| expr
== error_mark_node
)
5988 if (TREE_CODE (expr
) != ADDR_EXPR
)
5990 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5991 error ("it must be the address of a function with external linkage");
5995 /* [temp.arg.nontype]/5, bullet 5
5997 For a non-type template-parameter of type reference to function, no
5998 conversions apply. If the template-argument represents a set of
5999 overloaded functions, the matching function is selected from the set
6001 else if (TYPE_REFFN_P (type
))
6003 if (TREE_CODE (expr
) == ADDR_EXPR
)
6005 error ("%qE is not a valid template argument for type %qT "
6006 "because it is a pointer", expr
, type
);
6007 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
6011 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
6012 if (!expr
|| expr
== error_mark_node
)
6015 expr
= build_nop (type
, build_address (expr
));
6017 /* [temp.arg.nontype]/5, bullet 6
6019 For a non-type template-parameter of type pointer to member function,
6020 no conversions apply. If the template-argument represents a set of
6021 overloaded member functions, the matching member function is selected
6022 from the set (_over.over_). */
6023 else if (TYPE_PTRMEMFUNC_P (type
))
6025 expr
= instantiate_type (type
, expr
, tf_none
);
6026 if (expr
== error_mark_node
)
6027 return error_mark_node
;
6029 /* [temp.arg.nontype] bullet 1 says the pointer to member
6030 expression must be a pointer-to-member constant. */
6031 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6032 return error_mark_node
;
6034 /* There is no way to disable standard conversions in
6035 resolve_address_of_overloaded_function (called by
6036 instantiate_type). It is possible that the call succeeded by
6037 converting &B::I to &D::I (where B is a base of D), so we need
6038 to reject this conversion here.
6040 Actually, even if there was a way to disable standard conversions,
6041 it would still be better to reject them here so that we can
6042 provide a superior diagnostic. */
6043 if (!same_type_p (TREE_TYPE (expr
), type
))
6045 error ("%qE is not a valid template argument for type %qT "
6046 "because it is of type %qT", expr
, type
,
6048 /* If we are just one standard conversion off, explain. */
6049 if (can_convert (type
, TREE_TYPE (expr
)))
6050 inform (input_location
,
6051 "standard conversions are not allowed in this context");
6055 /* [temp.arg.nontype]/5, bullet 7
6057 For a non-type template-parameter of type pointer to data member,
6058 qualification conversions (_conv.qual_) are applied. */
6059 else if (TYPE_PTRMEM_P (type
))
6061 /* [temp.arg.nontype] bullet 1 says the pointer to member
6062 expression must be a pointer-to-member constant. */
6063 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6064 return error_mark_node
;
6066 expr
= perform_qualification_conversions (type
, expr
);
6067 if (expr
== error_mark_node
)
6070 else if (NULLPTR_TYPE_P (type
))
6072 if (expr
!= nullptr_node
)
6074 error ("%qE is not a valid template argument for type %qT "
6075 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6080 /* A template non-type parameter must be one of the above. */
6084 /* Sanity check: did we actually convert the argument to the
6086 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6087 (type
, TREE_TYPE (expr
)));
6091 /* Subroutine of coerce_template_template_parms, which returns 1 if
6092 PARM_PARM and ARG_PARM match using the rule for the template
6093 parameters of template template parameters. Both PARM and ARG are
6094 template parameters; the rest of the arguments are the same as for
6095 coerce_template_template_parms.
6098 coerce_template_template_parm (tree parm
,
6100 tsubst_flags_t complain
,
6104 if (arg
== NULL_TREE
|| arg
== error_mark_node
6105 || parm
== NULL_TREE
|| parm
== error_mark_node
)
6108 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6111 switch (TREE_CODE (parm
))
6114 /* We encounter instantiations of templates like
6115 template <template <template <class> class> class TT>
6118 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6119 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6121 if (!coerce_template_template_parms
6122 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6128 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6129 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6130 /* Argument is a parameter pack but parameter is not. */
6135 /* The tsubst call is used to handle cases such as
6137 template <int> class C {};
6138 template <class T, template <T> class TT> class D {};
6141 i.e. the parameter list of TT depends on earlier parameters. */
6142 if (!uses_template_parms (TREE_TYPE (arg
))
6144 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6148 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6149 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6150 /* Argument is a parameter pack but parameter is not. */
6163 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6164 template template parameters. Both PARM_PARMS and ARG_PARMS are
6165 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6168 Consider the example:
6169 template <class T> class A;
6170 template<template <class U> class TT> class B;
6172 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6173 the parameters to A, and OUTER_ARGS contains A. */
6176 coerce_template_template_parms (tree parm_parms
,
6178 tsubst_flags_t complain
,
6182 int nparms
, nargs
, i
;
6186 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6187 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6189 nparms
= TREE_VEC_LENGTH (parm_parms
);
6190 nargs
= TREE_VEC_LENGTH (arg_parms
);
6192 /* Determine whether we have a parameter pack at the end of the
6193 template template parameter's template parameter list. */
6194 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6196 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6198 if (parm
== error_mark_node
)
6201 switch (TREE_CODE (parm
))
6205 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6210 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6220 && !(variadic_p
&& nargs
>= nparms
- 1))
6223 /* Check all of the template parameters except the parameter pack at
6224 the end (if any). */
6225 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6227 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6228 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6231 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6232 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6234 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6242 /* Check each of the template parameters in the template
6243 argument against the template parameter pack at the end of
6244 the template template parameter. */
6245 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6248 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6250 for (; i
< nargs
; ++i
)
6252 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6255 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6257 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6266 /* Verifies that the deduced template arguments (in TARGS) for the
6267 template template parameters (in TPARMS) represent valid bindings,
6268 by comparing the template parameter list of each template argument
6269 to the template parameter list of its corresponding template
6270 template parameter, in accordance with DR150. This
6271 routine can only be called after all template arguments have been
6272 deduced. It will return TRUE if all of the template template
6273 parameter bindings are okay, FALSE otherwise. */
6275 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6277 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6280 /* We're dealing with template parms in this process. */
6281 ++processing_template_decl
;
6283 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6285 for (i
= 0; i
< ntparms
; ++i
)
6287 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6288 tree targ
= TREE_VEC_ELT (targs
, i
);
6290 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6292 tree packed_args
= NULL_TREE
;
6295 if (ARGUMENT_PACK_P (targ
))
6297 /* Look inside the argument pack. */
6298 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6299 len
= TREE_VEC_LENGTH (packed_args
);
6302 for (idx
= 0; idx
< len
; ++idx
)
6304 tree targ_parms
= NULL_TREE
;
6307 /* Extract the next argument from the argument
6309 targ
= TREE_VEC_ELT (packed_args
, idx
);
6311 if (PACK_EXPANSION_P (targ
))
6312 /* Look at the pattern of the pack expansion. */
6313 targ
= PACK_EXPANSION_PATTERN (targ
);
6315 /* Extract the template parameters from the template
6317 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6318 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6319 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6320 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6322 /* Verify that we can coerce the template template
6323 parameters from the template argument to the template
6324 parameter. This requires an exact match. */
6326 && !coerce_template_template_parms
6327 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6342 --processing_template_decl
;
6346 /* Since type attributes aren't mangled, we need to strip them from
6347 template type arguments. */
6350 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6353 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6355 mv
= TYPE_MAIN_VARIANT (arg
);
6356 arg
= strip_typedefs (arg
);
6357 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6358 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6360 if (complain
& tf_warning
)
6361 warning (0, "ignoring attributes on template argument %qT", arg
);
6362 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6363 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6368 /* Convert the indicated template ARG as necessary to match the
6369 indicated template PARM. Returns the converted ARG, or
6370 error_mark_node if the conversion was unsuccessful. Error and
6371 warning messages are issued under control of COMPLAIN. This
6372 conversion is for the Ith parameter in the parameter list. ARGS is
6373 the full set of template arguments deduced so far. */
6376 convert_template_argument (tree parm
,
6379 tsubst_flags_t complain
,
6385 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6387 if (TREE_CODE (arg
) == TREE_LIST
6388 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6390 /* The template argument was the name of some
6391 member function. That's usually
6392 invalid, but static members are OK. In any
6393 case, grab the underlying fields/functions
6394 and issue an error later if required. */
6395 orig_arg
= TREE_VALUE (arg
);
6396 TREE_TYPE (arg
) = unknown_type_node
;
6401 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6402 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6403 || requires_tmpl_type
);
6405 /* When determining whether an argument pack expansion is a template,
6406 look at the pattern. */
6407 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6408 arg
= PACK_EXPANSION_PATTERN (arg
);
6410 /* Deal with an injected-class-name used as a template template arg. */
6411 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6413 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6414 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6416 if (cxx_dialect
>= cxx0x
)
6417 /* OK under DR 1004. */;
6418 else if (complain
& tf_warning_or_error
)
6419 pedwarn (input_location
, OPT_pedantic
, "injected-class-name %qD"
6420 " used as template template argument", TYPE_NAME (arg
));
6421 else if (flag_pedantic_errors
)
6429 ((TREE_CODE (arg
) == TEMPLATE_DECL
6430 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6431 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6432 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6435 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6436 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6437 arg
= TYPE_STUB_DECL (arg
);
6439 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6441 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6442 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6444 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6446 if (complain
& tf_error
)
6447 error ("invalid use of destructor %qE as a type", orig_arg
);
6448 return error_mark_node
;
6451 permerror (input_location
,
6452 "to refer to a type member of a template parameter, "
6453 "use %<typename %E%>", orig_arg
);
6455 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6456 TREE_OPERAND (arg
, 1),
6458 complain
& tf_error
);
6462 if (is_type
!= requires_type
)
6466 if (complain
& tf_error
)
6468 error ("type/value mismatch at argument %d in template "
6469 "parameter list for %qD",
6472 error (" expected a constant of type %qT, got %qT",
6474 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6475 else if (requires_tmpl_type
)
6476 error (" expected a class template, got %qE", orig_arg
);
6478 error (" expected a type, got %qE", orig_arg
);
6481 return error_mark_node
;
6483 if (is_tmpl_type
^ requires_tmpl_type
)
6485 if (in_decl
&& (complain
& tf_error
))
6487 error ("type/value mismatch at argument %d in template "
6488 "parameter list for %qD",
6491 error (" expected a type, got %qT", DECL_NAME (arg
));
6493 error (" expected a class template, got %qT", orig_arg
);
6495 return error_mark_node
;
6500 if (requires_tmpl_type
)
6502 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6503 /* The number of argument required is not known yet.
6504 Just accept it for now. */
6505 val
= TREE_TYPE (arg
);
6508 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6511 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6513 if (coerce_template_template_parms (parmparm
, argparm
,
6519 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6521 if (val
!= error_mark_node
)
6523 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6524 val
= TREE_TYPE (val
);
6525 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6526 val
= make_pack_expansion (val
);
6531 if (in_decl
&& (complain
& tf_error
))
6533 error ("type/value mismatch at argument %d in "
6534 "template parameter list for %qD",
6536 error (" expected a template of type %qD, got %qT",
6540 val
= error_mark_node
;
6546 /* We only form one instance of each template specialization.
6547 Therefore, if we use a non-canonical variant (i.e., a
6548 typedef), any future messages referring to the type will use
6549 the typedef, which is confusing if those future uses do not
6550 themselves also use the typedef. */
6552 val
= canonicalize_type_argument (val
, complain
);
6556 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6558 if (invalid_nontype_parm_type_p (t
, complain
))
6559 return error_mark_node
;
6561 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6563 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6567 /* Not sure if this is reachable, but it doesn't hurt
6569 error ("type mismatch in nontype parameter pack");
6570 val
= error_mark_node
;
6573 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6574 /* We used to call digest_init here. However, digest_init
6575 will report errors, which we don't want when complain
6576 is zero. More importantly, digest_init will try too
6577 hard to convert things: for example, `0' should not be
6578 converted to pointer type at this point according to
6579 the standard. Accepting this is not merely an
6580 extension, since deciding whether or not these
6581 conversions can occur is part of determining which
6582 function template to call, or whether a given explicit
6583 argument specification is valid. */
6584 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6588 if (val
== NULL_TREE
)
6589 val
= error_mark_node
;
6590 else if (val
== error_mark_node
&& (complain
& tf_error
))
6591 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6593 if (TREE_CODE (val
) == SCOPE_REF
)
6595 /* Strip typedefs from the SCOPE_REF. */
6596 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6597 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6599 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6600 QUALIFIED_NAME_IS_TEMPLATE (val
));
6607 /* Coerces the remaining template arguments in INNER_ARGS (from
6608 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6609 Returns the coerced argument pack. PARM_IDX is the position of this
6610 parameter in the template parameter list. ARGS is the original
6611 template argument list. */
6613 coerce_template_parameter_pack (tree parms
,
6621 tsubst_flags_t complain
)
6623 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6624 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6627 tree packed_types
= NULL_TREE
;
6629 if (arg_idx
> nargs
)
6632 packed_args
= make_tree_vec (nargs
- arg_idx
);
6634 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6635 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6637 /* When the template parameter is a non-type template
6638 parameter pack whose type uses parameter packs, we need
6639 to look at each of the template arguments
6640 separately. Build a vector of the types for these
6641 non-type template parameters in PACKED_TYPES. */
6643 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6644 packed_types
= tsubst_pack_expansion (expansion
, args
,
6647 if (packed_types
== error_mark_node
)
6648 return error_mark_node
;
6650 /* Check that we have the right number of arguments. */
6652 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6653 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6656 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6657 error ("wrong number of template arguments (%d, should be %d)",
6658 nargs
, needed_parms
);
6659 return error_mark_node
;
6662 /* If we aren't able to check the actual arguments now
6663 (because they haven't been expanded yet), we can at least
6664 verify that all of the types used for the non-type
6665 template parameter pack are, in fact, valid for non-type
6666 template parameters. */
6668 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6670 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6671 for (j
= 0; j
< len
; ++j
)
6673 tree t
= TREE_VEC_ELT (packed_types
, j
);
6674 if (invalid_nontype_parm_type_p (t
, complain
))
6675 return error_mark_node
;
6680 /* Convert the remaining arguments, which will be a part of the
6681 parameter pack "parm". */
6682 for (; arg_idx
< nargs
; ++arg_idx
)
6684 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6685 tree actual_parm
= TREE_VALUE (parm
);
6687 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6689 /* When we have a vector of types (corresponding to the
6690 non-type template parameter pack that uses parameter
6691 packs in its type, as mention above), and the
6692 argument is not an expansion (which expands to a
6693 currently unknown number of arguments), clone the
6694 parm and give it the next type in PACKED_TYPES. */
6695 actual_parm
= copy_node (actual_parm
);
6696 TREE_TYPE (actual_parm
) =
6697 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6700 if (arg
!= error_mark_node
)
6701 arg
= convert_template_argument (actual_parm
,
6702 arg
, new_args
, complain
, parm_idx
,
6704 if (arg
== error_mark_node
)
6706 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6709 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6710 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6711 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6714 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6715 TREE_TYPE (argument_pack
)
6716 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6717 TREE_CONSTANT (argument_pack
) = 1;
6720 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6721 #ifdef ENABLE_CHECKING
6722 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6723 TREE_VEC_LENGTH (packed_args
));
6725 return argument_pack
;
6728 /* Convert all template arguments to their appropriate types, and
6729 return a vector containing the innermost resulting template
6730 arguments. If any error occurs, return error_mark_node. Error and
6731 warning messages are issued under control of COMPLAIN.
6733 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6734 for arguments not specified in ARGS. Otherwise, if
6735 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6736 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6737 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6741 coerce_template_parms (tree parms
,
6744 tsubst_flags_t complain
,
6745 bool require_all_args
,
6746 bool use_default_args
)
6748 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6751 tree new_inner_args
;
6752 int saved_unevaluated_operand
;
6753 int saved_inhibit_evaluation_warnings
;
6755 /* When used as a boolean value, indicates whether this is a
6756 variadic template parameter list. Since it's an int, we can also
6757 subtract it from nparms to get the number of non-variadic
6760 int post_variadic_parms
= 0;
6762 if (args
== error_mark_node
)
6763 return error_mark_node
;
6765 nparms
= TREE_VEC_LENGTH (parms
);
6767 /* Determine if there are any parameter packs. */
6768 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6770 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6772 ++post_variadic_parms
;
6773 if (template_parameter_pack_p (tparm
))
6777 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6778 /* If there are no parameters that follow a parameter pack, we need to
6779 expand any argument packs so that we can deduce a parameter pack from
6780 some non-packed args followed by an argument pack, as in variadic85.C.
6781 If there are such parameters, we need to leave argument packs intact
6782 so the arguments are assigned properly. This can happen when dealing
6783 with a nested class inside a partial specialization of a class
6784 template, as in variadic92.C, or when deducing a template parameter pack
6785 from a sub-declarator, as in variadic114.C. */
6786 if (!post_variadic_parms
)
6787 inner_args
= expand_template_argument_pack (inner_args
);
6789 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6790 if ((nargs
> nparms
&& !variadic_p
)
6791 || (nargs
< nparms
- variadic_p
6793 && (!use_default_args
6794 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6795 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6797 if (complain
& tf_error
)
6801 nparms
-= variadic_p
;
6802 error ("wrong number of template arguments "
6803 "(%d, should be %d or more)", nargs
, nparms
);
6806 error ("wrong number of template arguments "
6807 "(%d, should be %d)", nargs
, nparms
);
6810 error ("provided for %q+D", in_decl
);
6813 return error_mark_node
;
6816 /* We need to evaluate the template arguments, even though this
6817 template-id may be nested within a "sizeof". */
6818 saved_unevaluated_operand
= cp_unevaluated_operand
;
6819 cp_unevaluated_operand
= 0;
6820 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6821 c_inhibit_evaluation_warnings
= 0;
6822 new_inner_args
= make_tree_vec (nparms
);
6823 new_args
= add_outermost_template_args (args
, new_inner_args
);
6824 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6829 /* Get the Ith template parameter. */
6830 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6832 if (parm
== error_mark_node
)
6834 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6838 /* Calculate the next argument. */
6839 if (arg_idx
< nargs
)
6840 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6844 if (template_parameter_pack_p (TREE_VALUE (parm
))
6845 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6847 /* All remaining arguments will be placed in the
6848 template parameter pack PARM. */
6849 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6850 inner_args
, arg_idx
,
6854 /* Store this argument. */
6855 if (arg
== error_mark_node
)
6857 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6859 /* We are done with all of the arguments. */
6866 if (PACK_EXPANSION_P (arg
))
6868 /* We don't know how many args we have yet, just
6869 use the unconverted ones for now. */
6870 new_inner_args
= args
;
6874 else if (require_all_args
)
6876 /* There must be a default arg in this case. */
6877 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6879 /* The position of the first default template argument,
6880 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6882 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6883 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6888 if (arg
== error_mark_node
)
6890 if (complain
& tf_error
)
6891 error ("template argument %d is invalid", arg_idx
+ 1);
6894 /* This only occurs if there was an error in the template
6895 parameter list itself (which we would already have
6896 reported) that we are trying to recover from, e.g., a class
6897 template with a parameter list such as
6898 template<typename..., typename>. */
6901 arg
= convert_template_argument (TREE_VALUE (parm
),
6902 arg
, new_args
, complain
,
6905 if (arg
== error_mark_node
)
6907 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6909 cp_unevaluated_operand
= saved_unevaluated_operand
;
6910 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6913 return error_mark_node
;
6915 #ifdef ENABLE_CHECKING
6916 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6917 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6918 TREE_VEC_LENGTH (new_inner_args
));
6921 return new_inner_args
;
6924 /* Returns 1 if template args OT and NT are equivalent. */
6927 template_args_equal (tree ot
, tree nt
)
6931 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6934 if (TREE_CODE (nt
) == TREE_VEC
)
6935 /* For member templates */
6936 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6937 else if (PACK_EXPANSION_P (ot
))
6938 return (PACK_EXPANSION_P (nt
)
6939 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6940 PACK_EXPANSION_PATTERN (nt
))
6941 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6942 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6943 else if (ARGUMENT_PACK_P (ot
))
6948 if (!ARGUMENT_PACK_P (nt
))
6951 opack
= ARGUMENT_PACK_ARGS (ot
);
6952 npack
= ARGUMENT_PACK_ARGS (nt
);
6953 len
= TREE_VEC_LENGTH (opack
);
6954 if (TREE_VEC_LENGTH (npack
) != len
)
6956 for (i
= 0; i
< len
; ++i
)
6957 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6958 TREE_VEC_ELT (npack
, i
)))
6962 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6964 /* We get here probably because we are in the middle of substituting
6965 into the pattern of a pack expansion. In that case the
6966 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6967 interested in. So we want to use the initial pack argument for
6969 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6970 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6971 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6972 return template_args_equal (ot
, nt
);
6974 else if (TYPE_P (nt
))
6975 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6976 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6979 return cp_tree_equal (ot
, nt
);
6982 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6983 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6984 NEWARG_PTR with the offending arguments if they are non-NULL. */
6987 comp_template_args_with_info (tree oldargs
, tree newargs
,
6988 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6992 if (oldargs
== newargs
)
6995 if (!oldargs
|| !newargs
)
6998 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7001 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7003 tree nt
= TREE_VEC_ELT (newargs
, i
);
7004 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7006 if (! template_args_equal (ot
, nt
))
7008 if (oldarg_ptr
!= NULL
)
7010 if (newarg_ptr
!= NULL
)
7018 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7019 of template arguments. Returns 0 otherwise. */
7022 comp_template_args (tree oldargs
, tree newargs
)
7024 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7028 add_pending_template (tree d
)
7030 tree ti
= (TYPE_P (d
)
7031 ? CLASSTYPE_TEMPLATE_INFO (d
)
7032 : DECL_TEMPLATE_INFO (d
));
7033 struct pending_template
*pt
;
7036 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7039 /* We are called both from instantiate_decl, where we've already had a
7040 tinst_level pushed, and instantiate_template, where we haven't.
7042 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7045 push_tinst_level (d
);
7047 pt
= ggc_alloc_pending_template ();
7049 pt
->tinst
= current_tinst_level
;
7050 if (last_pending_template
)
7051 last_pending_template
->next
= pt
;
7053 pending_templates
= pt
;
7055 last_pending_template
= pt
;
7057 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7064 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7065 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7066 documentation for TEMPLATE_ID_EXPR. */
7069 lookup_template_function (tree fns
, tree arglist
)
7073 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7074 return error_mark_node
;
7076 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7078 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
7080 error ("%q#D is not a function template", fns
);
7081 return error_mark_node
;
7084 if (BASELINK_P (fns
))
7086 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7088 BASELINK_FUNCTIONS (fns
),
7093 type
= TREE_TYPE (fns
);
7094 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7095 type
= unknown_type_node
;
7097 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7100 /* Within the scope of a template class S<T>, the name S gets bound
7101 (in build_self_reference) to a TYPE_DECL for the class, not a
7102 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7103 or one of its enclosing classes, and that type is a template,
7104 return the associated TEMPLATE_DECL. Otherwise, the original
7107 Also handle the case when DECL is a TREE_LIST of ambiguous
7108 injected-class-names from different bases. */
7111 maybe_get_template_decl_from_type_decl (tree decl
)
7113 if (decl
== NULL_TREE
)
7116 /* DR 176: A lookup that finds an injected-class-name (10.2
7117 [class.member.lookup]) can result in an ambiguity in certain cases
7118 (for example, if it is found in more than one base class). If all of
7119 the injected-class-names that are found refer to specializations of
7120 the same class template, and if the name is followed by a
7121 template-argument-list, the reference refers to the class template
7122 itself and not a specialization thereof, and is not ambiguous. */
7123 if (TREE_CODE (decl
) == TREE_LIST
)
7125 tree t
, tmpl
= NULL_TREE
;
7126 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7128 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7131 else if (tmpl
!= elt
)
7134 if (tmpl
&& t
== NULL_TREE
)
7140 return (decl
!= NULL_TREE
7141 && DECL_SELF_REFERENCE_P (decl
)
7142 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7143 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7146 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7147 parameters, find the desired type.
7149 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7151 IN_DECL, if non-NULL, is the template declaration we are trying to
7154 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7155 the class we are looking up.
7157 Issue error and warning messages under control of COMPLAIN.
7159 If the template class is really a local class in a template
7160 function, then the FUNCTION_CONTEXT is the function in which it is
7163 ??? Note that this function is currently called *twice* for each
7164 template-id: the first time from the parser, while creating the
7165 incomplete type (finish_template_type), and the second type during the
7166 real instantiation (instantiate_template_class). This is surely something
7167 that we want to avoid. It also causes some problems with argument
7168 coercion (see convert_nontype_argument for more information on this). */
7171 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7172 int entering_scope
, tsubst_flags_t complain
)
7174 tree templ
= NULL_TREE
, parmlist
;
7181 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
7183 tree value
= innermost_non_namespace_value (d1
);
7184 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7189 push_decl_namespace (context
);
7190 templ
= lookup_name (d1
);
7191 templ
= maybe_get_template_decl_from_type_decl (templ
);
7193 pop_decl_namespace ();
7196 context
= DECL_CONTEXT (templ
);
7198 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7200 tree type
= TREE_TYPE (d1
);
7202 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7203 an implicit typename for the second A. Deal with it. */
7204 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7205 type
= TREE_TYPE (type
);
7207 if (CLASSTYPE_TEMPLATE_INFO (type
))
7209 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7210 d1
= DECL_NAME (templ
);
7213 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7214 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7216 templ
= TYPE_TI_TEMPLATE (d1
);
7217 d1
= DECL_NAME (templ
);
7219 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7220 && DECL_TEMPLATE_RESULT (d1
)
7221 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7224 d1
= DECL_NAME (templ
);
7225 context
= DECL_CONTEXT (templ
);
7228 /* Issue an error message if we didn't find a template. */
7231 if (complain
& tf_error
)
7232 error ("%qT is not a template", d1
);
7233 return error_mark_node
;
7236 if (TREE_CODE (templ
) != TEMPLATE_DECL
7237 /* Make sure it's a user visible template, if it was named by
7239 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7240 && !PRIMARY_TEMPLATE_P (templ
)))
7242 if (complain
& tf_error
)
7244 error ("non-template type %qT used as a template", d1
);
7246 error ("for template declaration %q+D", in_decl
);
7248 return error_mark_node
;
7251 complain
&= ~tf_user
;
7253 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7255 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7256 template arguments */
7262 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7264 /* Consider an example where a template template parameter declared as
7266 template <class T, class U = std::allocator<T> > class TT
7268 The template parameter level of T and U are one level larger than
7269 of TT. To proper process the default argument of U, say when an
7270 instantiation `TT<int>' is seen, we need to build the full
7271 arguments containing {int} as the innermost level. Outer levels,
7272 available when not appearing as default template argument, can be
7273 obtained from the arguments of the enclosing template.
7275 Suppose that TT is later substituted with std::vector. The above
7276 instantiation is `TT<int, std::allocator<T> >' with TT at
7277 level 1, and T at level 2, while the template arguments at level 1
7278 becomes {std::vector} and the inner level 2 is {int}. */
7280 outer
= DECL_CONTEXT (templ
);
7282 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7283 else if (current_template_parms
)
7284 /* This is an argument of the current template, so we haven't set
7285 DECL_CONTEXT yet. */
7286 outer
= current_template_args ();
7289 arglist
= add_to_template_args (outer
, arglist
);
7291 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7293 /*require_all_args=*/true,
7294 /*use_default_args=*/true);
7295 if (arglist2
== error_mark_node
7296 || (!uses_template_parms (arglist2
)
7297 && check_instantiated_args (templ
, arglist2
, complain
)))
7298 return error_mark_node
;
7300 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7305 tree template_type
= TREE_TYPE (templ
);
7308 tree found
= NULL_TREE
;
7311 int is_dependent_type
;
7312 int use_partial_inst_tmpl
= false;
7314 if (template_type
== error_mark_node
)
7315 /* An error occured while building the template TEMPL, and a
7316 diagnostic has most certainly been emitted for that
7317 already. Let's propagate that error. */
7318 return error_mark_node
;
7320 gen_tmpl
= most_general_template (templ
);
7321 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7322 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7323 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7325 if (arg_depth
== 1 && parm_depth
> 1)
7327 /* We've been given an incomplete set of template arguments.
7330 template <class T> struct S1 {
7331 template <class U> struct S2 {};
7332 template <class U> struct S2<U*> {};
7335 we will be called with an ARGLIST of `U*', but the
7336 TEMPLATE will be `template <class T> template
7337 <class U> struct S1<T>::S2'. We must fill in the missing
7340 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7342 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7345 /* Now we should have enough arguments. */
7346 gcc_assert (parm_depth
== arg_depth
);
7348 /* From here on, we're only interested in the most general
7351 /* Calculate the BOUND_ARGS. These will be the args that are
7352 actually tsubst'd into the definition to create the
7356 /* We have multiple levels of arguments to coerce, at once. */
7358 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7360 tree bound_args
= make_tree_vec (parm_depth
);
7362 for (i
= saved_depth
,
7363 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7364 i
> 0 && t
!= NULL_TREE
;
7365 --i
, t
= TREE_CHAIN (t
))
7368 if (i
== saved_depth
)
7369 a
= coerce_template_parms (TREE_VALUE (t
),
7372 /*require_all_args=*/true,
7373 /*use_default_args=*/true);
7375 /* Outer levels should have already been coerced. */
7376 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7378 /* Don't process further if one of the levels fails. */
7379 if (a
== error_mark_node
)
7381 /* Restore the ARGLIST to its full size. */
7382 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7383 return error_mark_node
;
7386 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7388 /* We temporarily reduce the length of the ARGLIST so
7389 that coerce_template_parms will see only the arguments
7390 corresponding to the template parameters it is
7392 TREE_VEC_LENGTH (arglist
)--;
7395 /* Restore the ARGLIST to its full size. */
7396 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7398 arglist
= bound_args
;
7402 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7403 INNERMOST_TEMPLATE_ARGS (arglist
),
7406 /*require_all_args=*/true,
7407 /*use_default_args=*/true);
7409 if (arglist
== error_mark_node
)
7410 /* We were unable to bind the arguments. */
7411 return error_mark_node
;
7413 /* In the scope of a template class, explicit references to the
7414 template class refer to the type of the template, not any
7415 instantiation of it. For example, in:
7417 template <class T> class C { void f(C<T>); }
7419 the `C<T>' is just the same as `C'. Outside of the
7420 class, however, such a reference is an instantiation. */
7422 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7423 || currently_open_class (template_type
))
7424 /* comp_template_args is expensive, check it last. */
7425 && comp_template_args (TYPE_TI_ARGS (template_type
),
7427 return template_type
;
7429 /* If we already have this specialization, return it. */
7430 elt
.tmpl
= gen_tmpl
;
7432 hash
= hash_specialization (&elt
);
7433 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7439 is_dependent_type
= uses_template_parms (arglist
);
7441 /* If the deduced arguments are invalid, then the binding
7443 if (!is_dependent_type
7444 && check_instantiated_args (gen_tmpl
,
7445 INNERMOST_TEMPLATE_ARGS (arglist
),
7447 return error_mark_node
;
7449 if (!is_dependent_type
7450 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7451 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7452 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7454 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7455 DECL_NAME (gen_tmpl
),
7456 /*tag_scope=*/ts_global
);
7460 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7462 if (context
== error_mark_node
)
7463 return error_mark_node
;
7466 context
= global_namespace
;
7468 /* Create the type. */
7469 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7471 if (!is_dependent_type
)
7473 set_current_access_from_decl (TYPE_NAME (template_type
));
7474 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7475 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7476 arglist
, complain
, in_decl
),
7477 SCOPED_ENUM_P (template_type
), NULL
);
7481 /* We don't want to call start_enum for this type, since
7482 the values for the enumeration constants may involve
7483 template parameters. And, no one should be interested
7484 in the enumeration constants for such a type. */
7485 t
= cxx_make_type (ENUMERAL_TYPE
);
7486 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7488 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7489 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7490 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7492 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7494 /* The user referred to a specialization of an alias
7495 template represented by GEN_TMPL.
7497 [temp.alias]/2 says:
7499 When a template-id refers to the specialization of an
7500 alias template, it is equivalent to the associated
7501 type obtained by substitution of its
7502 template-arguments for the template-parameters in the
7503 type-id of the alias template. */
7505 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7506 /* Note that the call above (by indirectly calling
7507 register_specialization in tsubst_decl) registers the
7508 TYPE_DECL representing the specialization of the alias
7509 template. So next time someone substitutes ARGLIST for
7510 the template parms into the alias template (GEN_TMPL),
7511 she'll get that TYPE_DECL back. */
7513 if (t
== error_mark_node
)
7516 else if (CLASS_TYPE_P (template_type
))
7518 t
= make_class_type (TREE_CODE (template_type
));
7519 CLASSTYPE_DECLARED_CLASS (t
)
7520 = CLASSTYPE_DECLARED_CLASS (template_type
);
7521 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7522 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7524 /* A local class. Make sure the decl gets registered properly. */
7525 if (context
== current_function_decl
)
7526 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7528 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7529 /* This instantiation is another name for the primary
7530 template type. Set the TYPE_CANONICAL field
7532 TYPE_CANONICAL (t
) = template_type
;
7533 else if (any_template_arguments_need_structural_equality_p (arglist
))
7534 /* Some of the template arguments require structural
7535 equality testing, so this template class requires
7536 structural equality testing. */
7537 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7542 /* If we called start_enum or pushtag above, this information
7543 will already be set up. */
7546 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7548 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7549 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7550 DECL_SOURCE_LOCATION (type_decl
)
7551 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7554 type_decl
= TYPE_NAME (t
);
7556 if (CLASS_TYPE_P (template_type
))
7558 TREE_PRIVATE (type_decl
)
7559 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
7560 TREE_PROTECTED (type_decl
)
7561 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
7562 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7564 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7565 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7569 /* Let's consider the explicit specialization of a member
7570 of a class template specialization that is implicitely instantiated,
7575 template<class U> struct M {}; //#0
7580 struct S<int>::M<char> //#1
7584 [temp.expl.spec]/4 says this is valid.
7586 In this case, when we write:
7589 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7592 When we encounter #1, we want to store the partial instantiation
7593 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7595 For all cases other than this "explicit specialization of member of a
7596 class template", we just want to store the most general template into
7597 the CLASSTYPE_TI_TEMPLATE of M.
7599 This case of "explicit specialization of member of a class template"
7601 1/ the enclosing class is an instantiation of, and therefore not
7602 the same as, the context of the most general template, and
7603 2/ we aren't looking at the partial instantiation itself, i.e.
7604 the innermost arguments are not the same as the innermost parms of
7605 the most general template.
7607 So it's only when 1/ and 2/ happens that we want to use the partial
7608 instantiation of the member template in lieu of its most general
7611 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7612 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7613 /* the enclosing class must be an instantiation... */
7614 && CLASS_TYPE_P (context
)
7615 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7617 tree partial_inst_args
;
7618 TREE_VEC_LENGTH (arglist
)--;
7619 ++processing_template_decl
;
7621 tsubst (INNERMOST_TEMPLATE_ARGS
7622 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7623 arglist
, complain
, NULL_TREE
);
7624 --processing_template_decl
;
7625 TREE_VEC_LENGTH (arglist
)++;
7626 use_partial_inst_tmpl
=
7627 /*...and we must not be looking at the partial instantiation
7629 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7633 if (!use_partial_inst_tmpl
)
7634 /* This case is easy; there are no member templates involved. */
7638 /* This is a full instantiation of a member template. Find
7639 the partial instantiation of which this is an instance. */
7641 /* Temporarily reduce by one the number of levels in the ARGLIST
7642 so as to avoid comparing the last set of arguments. */
7643 TREE_VEC_LENGTH (arglist
)--;
7644 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7645 TREE_VEC_LENGTH (arglist
)++;
7646 /* FOUND is either a proper class type, or an alias
7647 template specialization. In the later case, it's a
7648 TYPE_DECL, resulting from the substituting of arguments
7649 for parameters in the TYPE_DECL of the alias template
7650 done earlier. So be careful while getting the template
7652 found
= TREE_CODE (found
) == TYPE_DECL
7653 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7654 : CLASSTYPE_TI_TEMPLATE (found
);
7657 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7660 slot
= htab_find_slot_with_hash (type_specializations
,
7661 &elt
, hash
, INSERT
);
7662 entry
= ggc_alloc_spec_entry ();
7666 /* Note this use of the partial instantiation so we can check it
7667 later in maybe_process_partial_specialization. */
7668 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7669 = tree_cons (arglist
, t
,
7670 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7672 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7673 /* Now that the type has been registered on the instantiations
7674 list, we set up the enumerators. Because the enumeration
7675 constants may involve the enumeration type itself, we make
7676 sure to register the type first, and then create the
7677 constants. That way, doing tsubst_expr for the enumeration
7678 constants won't result in recursive calls here; we'll find
7679 the instantiation and exit above. */
7680 tsubst_enum (template_type
, t
, arglist
);
7682 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7683 /* If the type makes use of template parameters, the
7684 code that generates debugging information will crash. */
7685 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7687 /* Possibly limit visibility based on template args. */
7688 TREE_PUBLIC (type_decl
) = 1;
7689 determine_visibility (type_decl
);
7695 /* Wrapper for lookup_template_class_1. */
7698 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7699 int entering_scope
, tsubst_flags_t complain
)
7702 timevar_push (TV_TEMPLATE_INST
);
7703 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7704 entering_scope
, complain
);
7705 timevar_pop (TV_TEMPLATE_INST
);
7713 /* True when we should also visit template parameters that occur in
7714 non-deduced contexts. */
7715 bool include_nondeduced_p
;
7716 struct pointer_set_t
*visited
;
7719 /* Called from for_each_template_parm via walk_tree. */
7722 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7725 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7726 tree_fn_t fn
= pfd
->fn
;
7727 void *data
= pfd
->data
;
7730 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7731 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7732 pfd
->include_nondeduced_p
))
7733 return error_mark_node
;
7735 switch (TREE_CODE (t
))
7738 if (TYPE_PTRMEMFUNC_P (t
))
7744 if (!TYPE_TEMPLATE_INFO (t
))
7746 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7747 fn
, data
, pfd
->visited
,
7748 pfd
->include_nondeduced_p
))
7749 return error_mark_node
;
7753 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7754 fn
, data
, pfd
->visited
,
7755 pfd
->include_nondeduced_p
)
7756 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7757 fn
, data
, pfd
->visited
,
7758 pfd
->include_nondeduced_p
))
7759 return error_mark_node
;
7763 /* Since we're not going to walk subtrees, we have to do this
7765 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7766 pfd
->visited
, pfd
->include_nondeduced_p
))
7767 return error_mark_node
;
7771 /* Check the return type. */
7772 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7773 pfd
->include_nondeduced_p
))
7774 return error_mark_node
;
7776 /* Check the parameter types. Since default arguments are not
7777 instantiated until they are needed, the TYPE_ARG_TYPES may
7778 contain expressions that involve template parameters. But,
7779 no-one should be looking at them yet. And, once they're
7780 instantiated, they don't contain template parameters, so
7781 there's no point in looking at them then, either. */
7785 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7786 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7787 pfd
->visited
, pfd
->include_nondeduced_p
))
7788 return error_mark_node
;
7790 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7791 want walk_tree walking into them itself. */
7797 case UNDERLYING_TYPE
:
7798 if (pfd
->include_nondeduced_p
7799 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7801 pfd
->include_nondeduced_p
))
7802 return error_mark_node
;
7807 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7808 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7809 pfd
->visited
, pfd
->include_nondeduced_p
))
7810 return error_mark_node
;
7815 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7816 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7817 pfd
->visited
, pfd
->include_nondeduced_p
))
7818 return error_mark_node
;
7819 if (DECL_CONTEXT (t
)
7820 && pfd
->include_nondeduced_p
7821 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7822 pfd
->visited
, pfd
->include_nondeduced_p
))
7823 return error_mark_node
;
7826 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7827 /* Record template parameters such as `T' inside `TT<T>'. */
7828 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7829 pfd
->include_nondeduced_p
))
7830 return error_mark_node
;
7833 case TEMPLATE_TEMPLATE_PARM
:
7834 case TEMPLATE_TYPE_PARM
:
7835 case TEMPLATE_PARM_INDEX
:
7836 if (fn
&& (*fn
)(t
, data
))
7837 return error_mark_node
;
7839 return error_mark_node
;
7843 /* A template template parameter is encountered. */
7844 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7845 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7846 pfd
->include_nondeduced_p
))
7847 return error_mark_node
;
7849 /* Already substituted template template parameter */
7855 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7857 pfd
->include_nondeduced_p
))
7858 return error_mark_node
;
7862 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7863 && pfd
->include_nondeduced_p
7864 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7865 (TREE_TYPE (t
)), fn
, data
,
7866 pfd
->visited
, pfd
->include_nondeduced_p
))
7867 return error_mark_node
;
7872 /* If there's no type, then this thing must be some expression
7873 involving template parameters. */
7874 if (!fn
&& !TREE_TYPE (t
))
7875 return error_mark_node
;
7880 case IMPLICIT_CONV_EXPR
:
7881 case REINTERPRET_CAST_EXPR
:
7882 case CONST_CAST_EXPR
:
7883 case STATIC_CAST_EXPR
:
7884 case DYNAMIC_CAST_EXPR
:
7888 case PSEUDO_DTOR_EXPR
:
7890 return error_mark_node
;
7897 /* We didn't find any template parameters we liked. */
7901 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7902 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7903 call FN with the parameter and the DATA.
7904 If FN returns nonzero, the iteration is terminated, and
7905 for_each_template_parm returns 1. Otherwise, the iteration
7906 continues. If FN never returns a nonzero value, the value
7907 returned by for_each_template_parm is 0. If FN is NULL, it is
7908 considered to be the function which always returns 1.
7910 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7911 parameters that occur in non-deduced contexts. When false, only
7912 visits those template parameters that can be deduced. */
7915 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7916 struct pointer_set_t
*visited
,
7917 bool include_nondeduced_p
)
7919 struct pair_fn_data pfd
;
7925 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7927 /* Walk the tree. (Conceptually, we would like to walk without
7928 duplicates, but for_each_template_parm_r recursively calls
7929 for_each_template_parm, so we would need to reorganize a fair
7930 bit to use walk_tree_without_duplicates, so we keep our own
7933 pfd
.visited
= visited
;
7935 pfd
.visited
= pointer_set_create ();
7936 result
= cp_walk_tree (&t
,
7937 for_each_template_parm_r
,
7939 pfd
.visited
) != NULL_TREE
;
7944 pointer_set_destroy (pfd
.visited
);
7951 /* Returns true if T depends on any template parameter. */
7954 uses_template_parms (tree t
)
7957 int saved_processing_template_decl
;
7959 saved_processing_template_decl
= processing_template_decl
;
7960 if (!saved_processing_template_decl
)
7961 processing_template_decl
= 1;
7963 dependent_p
= dependent_type_p (t
);
7964 else if (TREE_CODE (t
) == TREE_VEC
)
7965 dependent_p
= any_dependent_template_arguments_p (t
);
7966 else if (TREE_CODE (t
) == TREE_LIST
)
7967 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7968 || uses_template_parms (TREE_CHAIN (t
)));
7969 else if (TREE_CODE (t
) == TYPE_DECL
)
7970 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7973 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7974 || TREE_CODE (t
) == OVERLOAD
7976 || TREE_CODE (t
) == IDENTIFIER_NODE
7977 || TREE_CODE (t
) == TRAIT_EXPR
7978 || TREE_CODE (t
) == CONSTRUCTOR
7979 || CONSTANT_CLASS_P (t
))
7980 dependent_p
= (type_dependent_expression_p (t
)
7981 || value_dependent_expression_p (t
));
7984 gcc_assert (t
== error_mark_node
);
7985 dependent_p
= false;
7988 processing_template_decl
= saved_processing_template_decl
;
7993 /* Returns true if T depends on any template parameter with level LEVEL. */
7996 uses_template_parms_level (tree t
, int level
)
7998 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7999 /*include_nondeduced_p=*/true);
8002 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8003 ill-formed translation unit, i.e. a variable or function that isn't
8004 usable in a constant expression. */
8007 neglectable_inst_p (tree d
)
8010 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8011 : decl_maybe_constant_var_p (d
)));
8014 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8015 neglectable and instantiated from within an erroneous instantiation. */
8018 limit_bad_template_recursion (tree decl
)
8020 struct tinst_level
*lev
= current_tinst_level
;
8021 int errs
= errorcount
+ sorrycount
;
8022 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8025 for (; lev
; lev
= lev
->next
)
8026 if (neglectable_inst_p (lev
->decl
))
8029 return (lev
&& errs
> lev
->errors
);
8032 static int tinst_depth
;
8033 extern int max_tinst_depth
;
8034 #ifdef GATHER_STATISTICS
8037 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8039 /* We're starting to instantiate D; record the template instantiation context
8040 for diagnostics and to restore it later. */
8043 push_tinst_level (tree d
)
8045 struct tinst_level
*new_level
;
8047 if (tinst_depth
>= max_tinst_depth
)
8049 last_error_tinst_level
= current_tinst_level
;
8050 if (TREE_CODE (d
) == TREE_LIST
)
8051 error ("template instantiation depth exceeds maximum of %d (use "
8052 "-ftemplate-depth= to increase the maximum) substituting %qS",
8053 max_tinst_depth
, d
);
8055 error ("template instantiation depth exceeds maximum of %d (use "
8056 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8057 max_tinst_depth
, d
);
8059 print_instantiation_context ();
8064 /* If the current instantiation caused problems, don't let it instantiate
8065 anything else. Do allow deduction substitution and decls usable in
8066 constant expressions. */
8067 if (limit_bad_template_recursion (d
))
8070 new_level
= ggc_alloc_tinst_level ();
8071 new_level
->decl
= d
;
8072 new_level
->locus
= input_location
;
8073 new_level
->errors
= errorcount
+sorrycount
;
8074 new_level
->in_system_header_p
= in_system_header
;
8075 new_level
->next
= current_tinst_level
;
8076 current_tinst_level
= new_level
;
8079 #ifdef GATHER_STATISTICS
8080 if (tinst_depth
> depth_reached
)
8081 depth_reached
= tinst_depth
;
8087 /* We're done instantiating this template; return to the instantiation
8091 pop_tinst_level (void)
8093 /* Restore the filename and line number stashed away when we started
8094 this instantiation. */
8095 input_location
= current_tinst_level
->locus
;
8096 current_tinst_level
= current_tinst_level
->next
;
8100 /* We're instantiating a deferred template; restore the template
8101 instantiation context in which the instantiation was requested, which
8102 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8105 reopen_tinst_level (struct tinst_level
*level
)
8107 struct tinst_level
*t
;
8110 for (t
= level
; t
; t
= t
->next
)
8113 current_tinst_level
= level
;
8115 if (current_tinst_level
)
8116 current_tinst_level
->errors
= errorcount
+sorrycount
;
8120 /* Returns the TINST_LEVEL which gives the original instantiation
8123 struct tinst_level
*
8124 outermost_tinst_level (void)
8126 struct tinst_level
*level
= current_tinst_level
;
8129 level
= level
->next
;
8133 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8134 vector of template arguments, as for tsubst.
8136 Returns an appropriate tsubst'd friend declaration. */
8139 tsubst_friend_function (tree decl
, tree args
)
8143 if (TREE_CODE (decl
) == FUNCTION_DECL
8144 && DECL_TEMPLATE_INSTANTIATION (decl
)
8145 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8146 /* This was a friend declared with an explicit template
8147 argument list, e.g.:
8151 to indicate that f was a template instantiation, not a new
8152 function declaration. Now, we have to figure out what
8153 instantiation of what template. */
8155 tree template_id
, arglist
, fns
;
8158 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8160 /* Friend functions are looked up in the containing namespace scope.
8161 We must enter that scope, to avoid finding member functions of the
8162 current class with same name. */
8163 push_nested_namespace (ns
);
8164 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8165 tf_warning_or_error
, NULL_TREE
,
8166 /*integral_constant_expression_p=*/false);
8167 pop_nested_namespace (ns
);
8168 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8169 tf_warning_or_error
, NULL_TREE
);
8170 template_id
= lookup_template_function (fns
, arglist
);
8172 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8173 tmpl
= determine_specialization (template_id
, new_friend
,
8175 /*need_member_template=*/0,
8176 TREE_VEC_LENGTH (args
),
8178 return instantiate_template (tmpl
, new_args
, tf_error
);
8181 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8183 /* The NEW_FRIEND will look like an instantiation, to the
8184 compiler, but is not an instantiation from the point of view of
8185 the language. For example, we might have had:
8187 template <class T> struct S {
8188 template <class U> friend void f(T, U);
8191 Then, in S<int>, template <class U> void f(int, U) is not an
8192 instantiation of anything. */
8193 if (new_friend
== error_mark_node
)
8194 return error_mark_node
;
8196 DECL_USE_TEMPLATE (new_friend
) = 0;
8197 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8199 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8200 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8201 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8204 /* The mangled name for the NEW_FRIEND is incorrect. The function
8205 is not a template instantiation and should not be mangled like
8206 one. Therefore, we forget the mangling here; we'll recompute it
8207 later if we need it. */
8208 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8210 SET_DECL_RTL (new_friend
, NULL
);
8211 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8214 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8217 tree new_friend_template_info
;
8218 tree new_friend_result_template_info
;
8220 int new_friend_is_defn
;
8222 /* We must save some information from NEW_FRIEND before calling
8223 duplicate decls since that function will free NEW_FRIEND if
8225 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8226 new_friend_is_defn
=
8227 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8228 (template_for_substitution (new_friend
)))
8230 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8232 /* This declaration is a `primary' template. */
8233 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8235 new_friend_result_template_info
8236 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8239 new_friend_result_template_info
= NULL_TREE
;
8241 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8242 if (new_friend_is_defn
)
8243 DECL_INITIAL (new_friend
) = error_mark_node
;
8245 /* Inside pushdecl_namespace_level, we will push into the
8246 current namespace. However, the friend function should go
8247 into the namespace of the template. */
8248 ns
= decl_namespace_context (new_friend
);
8249 push_nested_namespace (ns
);
8250 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8251 pop_nested_namespace (ns
);
8253 if (old_decl
== error_mark_node
)
8254 return error_mark_node
;
8256 if (old_decl
!= new_friend
)
8258 /* This new friend declaration matched an existing
8259 declaration. For example, given:
8261 template <class T> void f(T);
8262 template <class U> class C {
8263 template <class T> friend void f(T) {}
8266 the friend declaration actually provides the definition
8267 of `f', once C has been instantiated for some type. So,
8268 old_decl will be the out-of-class template declaration,
8269 while new_friend is the in-class definition.
8271 But, if `f' was called before this point, the
8272 instantiation of `f' will have DECL_TI_ARGS corresponding
8273 to `T' but not to `U', references to which might appear
8274 in the definition of `f'. Previously, the most general
8275 template for an instantiation of `f' was the out-of-class
8276 version; now it is the in-class version. Therefore, we
8277 run through all specialization of `f', adding to their
8278 DECL_TI_ARGS appropriately. In particular, they need a
8279 new set of outer arguments, corresponding to the
8280 arguments for this class instantiation.
8282 The same situation can arise with something like this:
8285 template <class T> class C {
8289 when `C<int>' is instantiated. Now, `f(int)' is defined
8292 if (!new_friend_is_defn
)
8293 /* On the other hand, if the in-class declaration does
8294 *not* provide a definition, then we don't want to alter
8295 existing definitions. We can just leave everything
8300 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8301 tree new_args
= TI_ARGS (new_friend_template_info
);
8303 /* Overwrite whatever template info was there before, if
8304 any, with the new template information pertaining to
8306 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8308 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8310 /* We should have called reregister_specialization in
8312 gcc_assert (retrieve_specialization (new_template
,
8316 /* Instantiate it if the global has already been used. */
8317 if (DECL_ODR_USED (old_decl
))
8318 instantiate_decl (old_decl
, /*defer_ok=*/true,
8319 /*expl_inst_class_mem_p=*/false);
8325 /* Indicate that the old function template is a partial
8327 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8328 = new_friend_result_template_info
;
8330 gcc_assert (new_template
8331 == most_general_template (new_template
));
8332 gcc_assert (new_template
!= old_decl
);
8334 /* Reassign any specializations already in the hash table
8335 to the new more general template, and add the
8336 additional template args. */
8337 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8341 tree spec
= TREE_VALUE (t
);
8344 elt
.tmpl
= old_decl
;
8345 elt
.args
= DECL_TI_ARGS (spec
);
8346 elt
.spec
= NULL_TREE
;
8348 htab_remove_elt (decl_specializations
, &elt
);
8351 = add_outermost_template_args (new_args
,
8352 DECL_TI_ARGS (spec
));
8354 register_specialization
8355 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8358 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8362 /* The information from NEW_FRIEND has been merged into OLD_DECL
8363 by duplicate_decls. */
8364 new_friend
= old_decl
;
8369 tree context
= DECL_CONTEXT (new_friend
);
8373 template <class T> class C {
8374 template <class U> friend void C1<U>::f (); // case 1
8375 friend void C2<T>::f (); // case 2
8377 we only need to make sure CONTEXT is a complete type for
8378 case 2. To distinguish between the two cases, we note that
8379 CONTEXT of case 1 remains dependent type after tsubst while
8380 this isn't true for case 2. */
8381 ++processing_template_decl
;
8382 dependent_p
= dependent_type_p (context
);
8383 --processing_template_decl
;
8386 && !complete_type_or_else (context
, NULL_TREE
))
8387 return error_mark_node
;
8389 if (COMPLETE_TYPE_P (context
))
8391 /* Check to see that the declaration is really present, and,
8392 possibly obtain an improved declaration. */
8393 tree fn
= check_classfn (context
,
8394 new_friend
, NULL_TREE
);
8404 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8405 template arguments, as for tsubst.
8407 Returns an appropriate tsubst'd friend type or error_mark_node on
8411 tsubst_friend_class (tree friend_tmpl
, tree args
)
8417 context
= CP_DECL_CONTEXT (friend_tmpl
);
8419 if (context
!= global_namespace
)
8421 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8422 push_nested_namespace (context
);
8424 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8427 /* Look for a class template declaration. We look for hidden names
8428 because two friend declarations of the same template are the
8429 same. For example, in:
8432 template <typename> friend class F;
8434 template <typename> struct B {
8435 template <typename> friend class F;
8438 both F templates are the same. */
8439 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8440 /*block_p=*/true, 0,
8441 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
8443 /* But, if we don't find one, it might be because we're in a
8444 situation like this:
8452 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8453 for `S<int>', not the TEMPLATE_DECL. */
8454 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8456 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8457 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8460 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8462 /* The friend template has already been declared. Just
8463 check to see that the declarations match, and install any new
8464 default parameters. We must tsubst the default parameters,
8465 of course. We only need the innermost template parameters
8466 because that is all that redeclare_class_template will look
8468 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8469 > TMPL_ARGS_DEPTH (args
))
8472 location_t saved_input_location
;
8473 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8474 args
, tf_warning_or_error
);
8476 saved_input_location
= input_location
;
8477 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8478 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8479 input_location
= saved_input_location
;
8483 friend_type
= TREE_TYPE (tmpl
);
8487 /* The friend template has not already been declared. In this
8488 case, the instantiation of the template class will cause the
8489 injection of this template into the global scope. */
8490 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8491 if (tmpl
== error_mark_node
)
8492 return error_mark_node
;
8494 /* The new TMPL is not an instantiation of anything, so we
8495 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8496 the new type because that is supposed to be the corresponding
8497 template decl, i.e., TMPL. */
8498 DECL_USE_TEMPLATE (tmpl
) = 0;
8499 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8500 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8501 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8502 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8504 /* Inject this template into the global scope. */
8505 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8508 if (context
!= global_namespace
)
8510 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8511 pop_nested_namespace (context
);
8513 pop_nested_class ();
8519 /* Returns zero if TYPE cannot be completed later due to circularity.
8520 Otherwise returns one. */
8523 can_complete_type_without_circularity (tree type
)
8525 if (type
== NULL_TREE
|| type
== error_mark_node
)
8527 else if (COMPLETE_TYPE_P (type
))
8529 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8530 return can_complete_type_without_circularity (TREE_TYPE (type
));
8531 else if (CLASS_TYPE_P (type
)
8532 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8538 /* Apply any attributes which had to be deferred until instantiation
8539 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8540 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8543 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8544 tree args
, tsubst_flags_t complain
, tree in_decl
)
8546 tree last_dep
= NULL_TREE
;
8550 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8551 if (ATTR_IS_DEPENDENT (t
))
8554 attributes
= copy_list (attributes
);
8558 if (DECL_P (*decl_p
))
8560 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8562 p
= &DECL_ATTRIBUTES (*decl_p
);
8565 p
= &TYPE_ATTRIBUTES (*decl_p
);
8569 tree late_attrs
= NULL_TREE
;
8570 tree
*q
= &late_attrs
;
8572 for (*p
= attributes
; *p
; )
8575 if (ATTR_IS_DEPENDENT (t
))
8577 *p
= TREE_CHAIN (t
);
8578 TREE_CHAIN (t
) = NULL_TREE
;
8579 /* If the first attribute argument is an identifier, don't
8580 pass it through tsubst. Attributes like mode, format,
8581 cleanup and several target specific attributes expect it
8584 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8585 && TREE_VALUE (TREE_VALUE (t
))
8586 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8587 == IDENTIFIER_NODE
))
8590 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8592 /*integral_constant_expression_p=*/false);
8593 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8595 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8600 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8601 /*integral_constant_expression_p=*/false);
8603 q
= &TREE_CHAIN (t
);
8606 p
= &TREE_CHAIN (t
);
8609 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8613 /* Perform (or defer) access check for typedefs that were referenced
8614 from within the template TMPL code.
8615 This is a subroutine of instantiate_template and instantiate_class_template.
8616 TMPL is the template to consider and TARGS is the list of arguments of
8620 perform_typedefs_access_check (tree tmpl
, tree targs
)
8622 location_t saved_location
;
8624 qualified_typedef_usage_t
*iter
;
8627 || (!CLASS_TYPE_P (tmpl
)
8628 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8631 saved_location
= input_location
;
8632 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
8633 get_types_needing_access_check (tmpl
),
8636 tree type_decl
= iter
->typedef_decl
;
8637 tree type_scope
= iter
->context
;
8639 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8642 if (uses_template_parms (type_decl
))
8643 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8644 if (uses_template_parms (type_scope
))
8645 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8647 /* Make access check error messages point to the location
8648 of the use of the typedef. */
8649 input_location
= iter
->locus
;
8650 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8651 type_decl
, type_decl
);
8653 input_location
= saved_location
;
8657 instantiate_class_template_1 (tree type
)
8659 tree templ
, args
, pattern
, t
, member
;
8663 unsigned int saved_maximum_field_alignment
;
8665 if (type
== error_mark_node
)
8666 return error_mark_node
;
8668 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8669 || uses_template_parms (type
))
8672 /* Figure out which template is being instantiated. */
8673 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8674 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8676 /* Determine what specialization of the original template to
8678 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8679 if (t
== error_mark_node
)
8681 TYPE_BEING_DEFINED (type
) = 1;
8682 return error_mark_node
;
8686 /* This TYPE is actually an instantiation of a partial
8687 specialization. We replace the innermost set of ARGS with
8688 the arguments appropriate for substitution. For example,
8691 template <class T> struct S {};
8692 template <class T> struct S<T*> {};
8694 and supposing that we are instantiating S<int*>, ARGS will
8695 presently be {int*} -- but we need {int}. */
8696 pattern
= TREE_TYPE (t
);
8697 args
= TREE_PURPOSE (t
);
8701 pattern
= TREE_TYPE (templ
);
8702 args
= CLASSTYPE_TI_ARGS (type
);
8705 /* If the template we're instantiating is incomplete, then clearly
8706 there's nothing we can do. */
8707 if (!COMPLETE_TYPE_P (pattern
))
8710 /* If we've recursively instantiated too many templates, stop. */
8711 if (! push_tinst_level (type
))
8714 /* Now we're really doing the instantiation. Mark the type as in
8715 the process of being defined. */
8716 TYPE_BEING_DEFINED (type
) = 1;
8718 /* We may be in the middle of deferred access check. Disable
8720 push_deferring_access_checks (dk_no_deferred
);
8722 push_to_top_level ();
8723 /* Use #pragma pack from the template context. */
8724 saved_maximum_field_alignment
= maximum_field_alignment
;
8725 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8727 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8729 /* Set the input location to the most specialized template definition.
8730 This is needed if tsubsting causes an error. */
8731 typedecl
= TYPE_MAIN_DECL (pattern
);
8732 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8733 DECL_SOURCE_LOCATION (typedecl
);
8735 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8736 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8737 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8738 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8739 if (ANON_AGGR_TYPE_P (pattern
))
8740 SET_ANON_AGGR_TYPE_P (type
);
8741 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8743 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8744 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8745 /* Adjust visibility for template arguments. */
8746 determine_visibility (TYPE_MAIN_DECL (type
));
8748 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8750 pbinfo
= TYPE_BINFO (pattern
);
8752 /* We should never instantiate a nested class before its enclosing
8753 class; we need to look up the nested class by name before we can
8754 instantiate it, and that lookup should instantiate the enclosing
8756 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8757 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8759 base_list
= NULL_TREE
;
8760 if (BINFO_N_BASE_BINFOS (pbinfo
))
8766 /* We must enter the scope containing the type, as that is where
8767 the accessibility of types named in dependent bases are
8769 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8771 /* Substitute into each of the bases to determine the actual
8773 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8776 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8777 tree expanded_bases
= NULL_TREE
;
8780 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8783 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8784 args
, tf_error
, NULL_TREE
);
8785 if (expanded_bases
== error_mark_node
)
8788 len
= TREE_VEC_LENGTH (expanded_bases
);
8791 for (idx
= 0; idx
< len
; idx
++)
8794 /* Extract the already-expanded base class. */
8795 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8797 /* Substitute to figure out the base class. */
8798 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8801 if (base
== error_mark_node
)
8804 base_list
= tree_cons (access
, base
, base_list
);
8805 if (BINFO_VIRTUAL_P (pbase_binfo
))
8806 TREE_TYPE (base_list
) = integer_type_node
;
8810 /* The list is now in reverse order; correct that. */
8811 base_list
= nreverse (base_list
);
8814 pop_scope (pushed_scope
);
8816 /* Now call xref_basetypes to set up all the base-class
8818 xref_basetypes (type
, base_list
);
8820 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8821 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8822 args
, tf_error
, NULL_TREE
);
8823 fixup_attribute_variants (type
);
8825 /* Now that our base classes are set up, enter the scope of the
8826 class, so that name lookups into base classes, etc. will work
8827 correctly. This is precisely analogous to what we do in
8828 begin_class_definition when defining an ordinary non-template
8829 class, except we also need to push the enclosing classes. */
8830 push_nested_class (type
);
8832 /* Now members are processed in the order of declaration. */
8833 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8834 member
; member
= TREE_CHAIN (member
))
8836 tree t
= TREE_VALUE (member
);
8838 if (TREE_PURPOSE (member
))
8842 /* Build new CLASSTYPE_NESTED_UTDS. */
8845 bool class_template_p
;
8847 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8848 && TYPE_LANG_SPECIFIC (t
)
8849 && CLASSTYPE_IS_TEMPLATE (t
));
8850 /* If the member is a class template, then -- even after
8851 substitution -- there may be dependent types in the
8852 template argument list for the class. We increment
8853 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8854 that function will assume that no types are dependent
8855 when outside of a template. */
8856 if (class_template_p
)
8857 ++processing_template_decl
;
8858 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8859 if (class_template_p
)
8860 --processing_template_decl
;
8861 if (newtag
== error_mark_node
)
8864 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8866 tree name
= TYPE_IDENTIFIER (t
);
8868 if (class_template_p
)
8869 /* Unfortunately, lookup_template_class sets
8870 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8871 instantiation (i.e., for the type of a member
8872 template class nested within a template class.)
8873 This behavior is required for
8874 maybe_process_partial_specialization to work
8875 correctly, but is not accurate in this case;
8876 the TAG is not an instantiation of anything.
8877 (The corresponding TEMPLATE_DECL is an
8878 instantiation, but the TYPE is not.) */
8879 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8881 /* Now, we call pushtag to put this NEWTAG into the scope of
8882 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8883 pushtag calling push_template_decl. We don't have to do
8884 this for enums because it will already have been done in
8887 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8888 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8891 else if (TREE_CODE (t
) == FUNCTION_DECL
8892 || DECL_FUNCTION_TEMPLATE_P (t
))
8894 /* Build new TYPE_METHODS. */
8897 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8898 ++processing_template_decl
;
8899 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8900 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8901 --processing_template_decl
;
8902 set_current_access_from_decl (r
);
8903 finish_member_declaration (r
);
8904 /* Instantiate members marked with attribute used. */
8905 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8910 /* Build new TYPE_FIELDS. */
8911 if (TREE_CODE (t
) == STATIC_ASSERT
)
8914 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8915 tf_warning_or_error
, NULL_TREE
,
8916 /*integral_constant_expression_p=*/true);
8917 finish_static_assert (condition
,
8918 STATIC_ASSERT_MESSAGE (t
),
8919 STATIC_ASSERT_SOURCE_LOCATION (t
),
8922 else if (TREE_CODE (t
) != CONST_DECL
)
8926 /* The file and line for this declaration, to
8927 assist in error message reporting. Since we
8928 called push_tinst_level above, we don't need to
8930 input_location
= DECL_SOURCE_LOCATION (t
);
8932 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8933 ++processing_template_decl
;
8934 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8935 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8936 --processing_template_decl
;
8937 if (TREE_CODE (r
) == VAR_DECL
)
8941 [t]he initialization (and any associated
8942 side-effects) of a static data member does
8943 not occur unless the static data member is
8944 itself used in a way that requires the
8945 definition of the static data member to
8948 Therefore, we do not substitute into the
8949 initialized for the static data member here. */
8950 finish_static_data_member_decl
8953 /*init_const_expr_p=*/false,
8954 /*asmspec_tree=*/NULL_TREE
,
8956 /* Instantiate members marked with attribute used. */
8957 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8960 else if (TREE_CODE (r
) == FIELD_DECL
)
8962 /* Determine whether R has a valid type and can be
8963 completed later. If R is invalid, then it is
8964 replaced by error_mark_node so that it will not be
8965 added to TYPE_FIELDS. */
8966 tree rtype
= TREE_TYPE (r
);
8967 if (can_complete_type_without_circularity (rtype
))
8968 complete_type (rtype
);
8970 if (!COMPLETE_TYPE_P (rtype
))
8972 cxx_incomplete_type_error (r
, rtype
);
8973 r
= error_mark_node
;
8977 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8978 such a thing will already have been added to the field
8979 list by tsubst_enum in finish_member_declaration in the
8980 CLASSTYPE_NESTED_UTDS case above. */
8981 if (!(TREE_CODE (r
) == TYPE_DECL
8982 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8983 && DECL_ARTIFICIAL (r
)))
8985 set_current_access_from_decl (r
);
8986 finish_member_declaration (r
);
8993 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
8995 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8997 tree friend_type
= t
;
8998 bool adjust_processing_template_decl
= false;
9000 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9002 /* template <class T> friend class C; */
9003 friend_type
= tsubst_friend_class (friend_type
, args
);
9004 adjust_processing_template_decl
= true;
9006 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9008 /* template <class T> friend class C::D; */
9009 friend_type
= tsubst (friend_type
, args
,
9010 tf_warning_or_error
, NULL_TREE
);
9011 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9012 friend_type
= TREE_TYPE (friend_type
);
9013 adjust_processing_template_decl
= true;
9015 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9016 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9018 /* This could be either
9022 when dependent_type_p is false or
9024 template <class U> friend class T::C;
9027 friend_type
= tsubst (friend_type
, args
,
9028 tf_warning_or_error
, NULL_TREE
);
9029 /* Bump processing_template_decl for correct
9030 dependent_type_p calculation. */
9031 ++processing_template_decl
;
9032 if (dependent_type_p (friend_type
))
9033 adjust_processing_template_decl
= true;
9034 --processing_template_decl
;
9036 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9037 && hidden_name_p (TYPE_NAME (friend_type
)))
9041 where C hasn't been declared yet. Let's lookup name
9042 from namespace scope directly, bypassing any name that
9043 come from dependent base class. */
9044 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9046 /* The call to xref_tag_from_type does injection for friend
9048 push_nested_namespace (ns
);
9050 xref_tag_from_type (friend_type
, NULL_TREE
,
9051 /*tag_scope=*/ts_current
);
9052 pop_nested_namespace (ns
);
9054 else if (uses_template_parms (friend_type
))
9055 /* friend class C<T>; */
9056 friend_type
= tsubst (friend_type
, args
,
9057 tf_warning_or_error
, NULL_TREE
);
9062 where C is already declared or
9064 friend class C<int>;
9066 We don't have to do anything in these cases. */
9068 if (adjust_processing_template_decl
)
9069 /* Trick make_friend_class into realizing that the friend
9070 we're adding is a template, not an ordinary class. It's
9071 important that we use make_friend_class since it will
9072 perform some error-checking and output cross-reference
9074 ++processing_template_decl
;
9076 if (friend_type
!= error_mark_node
)
9077 make_friend_class (type
, friend_type
, /*complain=*/false);
9079 if (adjust_processing_template_decl
)
9080 --processing_template_decl
;
9084 /* Build new DECL_FRIENDLIST. */
9087 /* The file and line for this declaration, to
9088 assist in error message reporting. Since we
9089 called push_tinst_level above, we don't need to
9091 input_location
= DECL_SOURCE_LOCATION (t
);
9093 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9095 ++processing_template_decl
;
9096 push_deferring_access_checks (dk_no_check
);
9099 r
= tsubst_friend_function (t
, args
);
9100 add_friend (type
, r
, /*complain=*/false);
9101 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9103 pop_deferring_access_checks ();
9104 --processing_template_decl
;
9110 if (CLASSTYPE_LAMBDA_EXPR (type
))
9112 tree decl
= lambda_function (type
);
9115 instantiate_decl (decl
, false, false);
9116 maybe_add_lambda_conv_op (type
);
9119 gcc_assert (errorcount
);
9122 /* Set the file and line number information to whatever is given for
9123 the class itself. This puts error messages involving generated
9124 implicit functions at a predictable point, and the same point
9125 that would be used for non-template classes. */
9126 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9128 unreverse_member_declarations (type
);
9129 finish_struct_1 (type
);
9130 TYPE_BEING_DEFINED (type
) = 0;
9132 /* We don't instantiate default arguments for member functions. 14.7.1:
9134 The implicit instantiation of a class template specialization causes
9135 the implicit instantiation of the declarations, but not of the
9136 definitions or default arguments, of the class member functions,
9137 member classes, static data members and member templates.... */
9139 /* Some typedefs referenced from within the template code need to be access
9140 checked at template instantiation time, i.e now. These types were
9141 added to the template at parsing time. Let's get those and perform
9142 the access checks then. */
9143 perform_typedefs_access_check (pattern
, args
);
9144 perform_deferred_access_checks ();
9145 pop_nested_class ();
9146 maximum_field_alignment
= saved_maximum_field_alignment
;
9147 pop_from_top_level ();
9148 pop_deferring_access_checks ();
9151 /* The vtable for a template class can be emitted in any translation
9152 unit in which the class is instantiated. When there is no key
9153 method, however, finish_struct_1 will already have added TYPE to
9154 the keyed_classes list. */
9155 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9156 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9161 /* Wrapper for instantiate_class_template_1. */
9164 instantiate_class_template (tree type
)
9167 timevar_push (TV_TEMPLATE_INST
);
9168 ret
= instantiate_class_template_1 (type
);
9169 timevar_pop (TV_TEMPLATE_INST
);
9174 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9180 else if (TYPE_P (t
))
9181 r
= tsubst (t
, args
, complain
, in_decl
);
9184 if (!(complain
& tf_warning
))
9185 ++c_inhibit_evaluation_warnings
;
9186 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9187 /*integral_constant_expression_p=*/true);
9188 if (!(complain
& tf_warning
))
9189 --c_inhibit_evaluation_warnings
;
9190 /* Preserve the raw-reference nature of T. */
9191 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9192 && REFERENCE_REF_P (r
))
9193 r
= TREE_OPERAND (r
, 0);
9198 /* Given a function parameter pack TMPL_PARM and some function parameters
9199 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9200 and set *SPEC_P to point at the next point in the list. */
9203 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9205 /* Collect all of the extra "packed" parameters into an
9209 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9210 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9211 tree spec_parm
= *spec_p
;
9214 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9216 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9219 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9220 parmvec
= make_tree_vec (len
);
9221 parmtypevec
= make_tree_vec (len
);
9222 spec_parm
= *spec_p
;
9223 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9225 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9226 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9229 /* Build the argument packs. */
9230 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9231 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9232 TREE_TYPE (argpack
) = argtypepack
;
9233 *spec_p
= spec_parm
;
9238 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9239 NONTYPE_ARGUMENT_PACK. */
9242 make_fnparm_pack (tree spec_parm
)
9244 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9247 /* Substitute ARGS into T, which is an pack expansion
9248 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9249 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9250 (if only a partial substitution could be performed) or
9251 ERROR_MARK_NODE if there was an error. */
9253 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9257 tree pack
, packs
= NULL_TREE
;
9258 bool unsubstituted_packs
= false;
9259 bool real_packs
= false;
9260 int missing_level
= 0;
9263 struct pointer_map_t
*saved_local_specializations
= NULL
;
9264 bool need_local_specializations
= false;
9267 gcc_assert (PACK_EXPANSION_P (t
));
9268 pattern
= PACK_EXPANSION_PATTERN (t
);
9270 /* Add in any args remembered from an earlier partial instantiation. */
9271 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9273 levels
= TMPL_ARGS_DEPTH (args
);
9275 /* Determine the argument packs that will instantiate the parameter
9276 packs used in the expansion expression. While we're at it,
9277 compute the number of arguments to be expanded and make sure it
9279 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9280 pack
= TREE_CHAIN (pack
))
9282 tree parm_pack
= TREE_VALUE (pack
);
9283 tree arg_pack
= NULL_TREE
;
9284 tree orig_arg
= NULL_TREE
;
9287 if (TREE_CODE (parm_pack
) == BASES
)
9289 if (BASES_DIRECT (parm_pack
))
9290 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9291 args
, complain
, in_decl
, false));
9293 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9294 args
, complain
, in_decl
, false));
9296 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9298 if (PACK_EXPANSION_LOCAL_P (t
))
9299 arg_pack
= retrieve_local_specialization (parm_pack
);
9302 /* We can't rely on local_specializations for a parameter
9303 name used later in a function declaration (such as in a
9304 late-specified return type). Even if it exists, it might
9305 have the wrong value for a recursive call. Just make a
9306 dummy decl, since it's only used for its type. */
9307 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9308 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9309 /* Partial instantiation of the parm_pack, we can't build
9310 up an argument pack yet. */
9311 arg_pack
= NULL_TREE
;
9313 arg_pack
= make_fnparm_pack (arg_pack
);
9314 need_local_specializations
= true;
9320 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9322 if (level
<= levels
)
9323 arg_pack
= TMPL_ARG (args
, level
, idx
);
9326 orig_arg
= arg_pack
;
9327 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9328 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9330 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9331 /* This can only happen if we forget to expand an argument
9332 pack somewhere else. Just return an error, silently. */
9334 result
= make_tree_vec (1);
9335 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9339 if (arg_from_parm_pack_p (arg_pack
, parm_pack
))
9340 /* The argument pack that the parameter maps to is just an
9341 expansion of the parameter itself, such as one would find
9342 in the implicit typedef of a class inside the class itself.
9343 Consider this parameter "unsubstituted", so that we will
9344 maintain the outer pack expansion. */
9345 arg_pack
= NULL_TREE
;
9350 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9352 /* Don't bother trying to do a partial substitution with
9353 incomplete packs; we'll try again after deduction. */
9354 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9359 else if (len
!= my_len
)
9361 if (!(complain
& tf_error
))
9362 /* Fail quietly. */;
9363 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9364 error ("mismatched argument pack lengths while expanding "
9368 error ("mismatched argument pack lengths while expanding "
9371 return error_mark_node
;
9374 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
9375 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
),
9377 /* This isn't a real argument pack yet. */;
9381 /* Keep track of the parameter packs and their corresponding
9383 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9384 TREE_TYPE (packs
) = orig_arg
;
9388 /* We can't substitute for this parameter pack. We use a flag as
9389 well as the missing_level counter because function parameter
9390 packs don't have a level. */
9391 unsubstituted_packs
= true;
9392 if (!missing_level
|| missing_level
> level
)
9393 missing_level
= level
;
9397 /* We cannot expand this expansion expression, because we don't have
9398 all of the argument packs we need. */
9399 if (unsubstituted_packs
)
9403 /* We got some full packs, but we can't substitute them in until we
9404 have values for all the packs. So remember these until then. */
9407 t
= make_pack_expansion (pattern
);
9409 /* The call to add_to_template_args above assumes no overlap
9410 between saved args and new args, so prune away any fake
9411 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9412 if (missing_level
&& levels
>= missing_level
)
9414 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
)
9415 && missing_level
> 1);
9416 TREE_VEC_LENGTH (args
) = missing_level
- 1;
9417 save_args
= copy_node (args
);
9418 TREE_VEC_LENGTH (args
) = levels
;
9423 PACK_EXPANSION_EXTRA_ARGS (t
) = save_args
;
9427 /* There were no real arguments, we're just replacing a parameter
9428 pack with another version of itself. Substitute into the
9429 pattern and return a PACK_EXPANSION_*. The caller will need to
9431 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9432 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9433 /*integral_constant_expression_p=*/false);
9435 t
= tsubst (pattern
, args
, complain
, in_decl
);
9436 t
= make_pack_expansion (t
);
9441 /* We could not find any argument packs that work. */
9443 return error_mark_node
;
9445 if (need_local_specializations
)
9447 /* We're in a late-specified return type, so create our own local
9448 specializations map; the current map is either NULL or (in the
9449 case of recursive unification) might have bindings that we don't
9450 want to use or alter. */
9451 saved_local_specializations
= local_specializations
;
9452 local_specializations
= pointer_map_create ();
9455 /* For each argument in each argument pack, substitute into the
9457 result
= make_tree_vec (len
);
9458 for (i
= 0; i
< len
; ++i
)
9460 /* For parameter pack, change the substitution of the parameter
9461 pack to the ith argument in its argument pack, then expand
9463 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9465 tree parm
= TREE_PURPOSE (pack
);
9468 /* Select the Ith argument from the pack. */
9469 if (TREE_CODE (parm
) == PARM_DECL
)
9473 arg
= make_node (ARGUMENT_PACK_SELECT
);
9474 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9476 register_local_specialization (arg
, parm
);
9479 arg
= retrieve_local_specialization (parm
);
9484 template_parm_level_and_index (parm
, &level
, &idx
);
9488 arg
= make_node (ARGUMENT_PACK_SELECT
);
9489 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9490 /* Update the corresponding argument. */
9491 TMPL_ARG (args
, level
, idx
) = arg
;
9494 /* Re-use the ARGUMENT_PACK_SELECT. */
9495 arg
= TMPL_ARG (args
, level
, idx
);
9497 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
9500 /* Substitute into the PATTERN with the altered arguments. */
9501 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9502 TREE_VEC_ELT (result
, i
) =
9503 tsubst_expr (pattern
, args
, complain
, in_decl
,
9504 /*integral_constant_expression_p=*/false);
9506 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
9508 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
9510 result
= error_mark_node
;
9515 /* Update ARGS to restore the substitution from parameter packs to
9516 their argument packs. */
9517 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9519 tree parm
= TREE_PURPOSE (pack
);
9521 if (TREE_CODE (parm
) == PARM_DECL
)
9522 register_local_specialization (TREE_TYPE (pack
), parm
);
9526 template_parm_level_and_index (parm
, &level
, &idx
);
9528 /* Update the corresponding argument. */
9529 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9530 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9533 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9537 if (need_local_specializations
)
9539 pointer_map_destroy (local_specializations
);
9540 local_specializations
= saved_local_specializations
;
9546 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9547 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9548 parameter packs; all parms generated from a function parameter pack will
9549 have the same DECL_PARM_INDEX. */
9552 get_pattern_parm (tree parm
, tree tmpl
)
9554 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9557 if (DECL_ARTIFICIAL (parm
))
9559 for (patparm
= DECL_ARGUMENTS (pattern
);
9560 patparm
; patparm
= DECL_CHAIN (patparm
))
9561 if (DECL_ARTIFICIAL (patparm
)
9562 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9567 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9568 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9569 gcc_assert (DECL_PARM_INDEX (patparm
)
9570 == DECL_PARM_INDEX (parm
));
9576 /* Substitute ARGS into the vector or list of template arguments T. */
9579 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9582 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9585 if (t
== error_mark_node
)
9586 return error_mark_node
;
9588 len
= TREE_VEC_LENGTH (t
);
9589 elts
= XALLOCAVEC (tree
, len
);
9591 for (i
= 0; i
< len
; i
++)
9593 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9596 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9597 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9598 else if (PACK_EXPANSION_P (orig_arg
))
9600 /* Substitute into an expansion expression. */
9601 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9603 if (TREE_CODE (new_arg
) == TREE_VEC
)
9604 /* Add to the expanded length adjustment the number of
9605 expanded arguments. We subtract one from this
9606 measurement, because the argument pack expression
9607 itself is already counted as 1 in
9608 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9609 the argument pack is empty. */
9610 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9612 else if (ARGUMENT_PACK_P (orig_arg
))
9614 /* Substitute into each of the arguments. */
9615 new_arg
= TYPE_P (orig_arg
)
9616 ? cxx_make_type (TREE_CODE (orig_arg
))
9617 : make_node (TREE_CODE (orig_arg
));
9619 SET_ARGUMENT_PACK_ARGS (
9621 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9622 args
, complain
, in_decl
));
9624 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9625 new_arg
= error_mark_node
;
9627 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9628 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9630 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9632 if (TREE_TYPE (new_arg
) == error_mark_node
)
9633 new_arg
= error_mark_node
;
9637 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9639 if (new_arg
== error_mark_node
)
9640 return error_mark_node
;
9643 if (new_arg
!= orig_arg
)
9650 /* Make space for the expanded arguments coming from template
9652 t
= make_tree_vec (len
+ expanded_len_adjust
);
9653 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9654 arguments for a member template.
9655 In that case each TREE_VEC in ORIG_T represents a level of template
9656 arguments, and ORIG_T won't carry any non defaulted argument count.
9657 It will rather be the nested TREE_VECs that will carry one.
9658 In other words, ORIG_T carries a non defaulted argument count only
9659 if it doesn't contain any nested TREE_VEC. */
9660 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9662 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9663 count
+= expanded_len_adjust
;
9664 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9666 for (i
= 0, out
= 0; i
< len
; i
++)
9668 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9669 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9670 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9674 /* Now expand the template argument pack "in place". */
9675 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9676 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9680 TREE_VEC_ELT (t
, out
) = elts
[i
];
9688 /* Return the result of substituting ARGS into the template parameters
9689 given by PARMS. If there are m levels of ARGS and m + n levels of
9690 PARMS, then the result will contain n levels of PARMS. For
9691 example, if PARMS is `template <class T> template <class U>
9692 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9693 result will be `template <int*, double, class V>'. */
9696 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9701 /* When substituting into a template, we must set
9702 PROCESSING_TEMPLATE_DECL as the template parameters may be
9703 dependent if they are based on one-another, and the dependency
9704 predicates are short-circuit outside of templates. */
9705 ++processing_template_decl
;
9707 for (new_parms
= &r
;
9708 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9709 new_parms
= &(TREE_CHAIN (*new_parms
)),
9710 parms
= TREE_CHAIN (parms
))
9713 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9716 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9720 if (parms
== error_mark_node
)
9723 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9725 if (tuple
== error_mark_node
)
9728 TREE_VEC_ELT (new_vec
, i
) =
9729 tsubst_template_parm (tuple
, args
, complain
);
9733 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9734 - TMPL_ARGS_DEPTH (args
)),
9735 new_vec
, NULL_TREE
);
9738 --processing_template_decl
;
9743 /* Return the result of substituting ARGS into one template parameter
9744 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9745 parameter and which TREE_PURPOSE is the default argument of the
9746 template parameter. */
9749 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9751 tree default_value
, parm_decl
;
9753 if (args
== NULL_TREE
9755 || t
== error_mark_node
)
9758 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9760 default_value
= TREE_PURPOSE (t
);
9761 parm_decl
= TREE_VALUE (t
);
9763 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9764 if (TREE_CODE (parm_decl
) == PARM_DECL
9765 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9766 parm_decl
= error_mark_node
;
9767 default_value
= tsubst_template_arg (default_value
, args
,
9768 complain
, NULL_TREE
);
9770 return build_tree_list (default_value
, parm_decl
);
9773 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9774 type T. If T is not an aggregate or enumeration type, it is
9775 handled as if by tsubst. IN_DECL is as for tsubst. If
9776 ENTERING_SCOPE is nonzero, T is the context for a template which
9777 we are presently tsubst'ing. Return the substituted value. */
9780 tsubst_aggr_type (tree t
,
9782 tsubst_flags_t complain
,
9789 switch (TREE_CODE (t
))
9792 if (TYPE_PTRMEMFUNC_P (t
))
9793 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9795 /* Else fall through. */
9798 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9803 int saved_unevaluated_operand
;
9804 int saved_inhibit_evaluation_warnings
;
9806 /* In "sizeof(X<I>)" we need to evaluate "I". */
9807 saved_unevaluated_operand
= cp_unevaluated_operand
;
9808 cp_unevaluated_operand
= 0;
9809 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9810 c_inhibit_evaluation_warnings
= 0;
9812 /* First, determine the context for the type we are looking
9814 context
= TYPE_CONTEXT (t
);
9815 if (context
&& TYPE_P (context
))
9817 context
= tsubst_aggr_type (context
, args
, complain
,
9818 in_decl
, /*entering_scope=*/1);
9819 /* If context is a nested class inside a class template,
9820 it may still need to be instantiated (c++/33959). */
9821 context
= complete_type (context
);
9824 /* Then, figure out what arguments are appropriate for the
9825 type we are trying to find. For example, given:
9827 template <class T> struct S;
9828 template <class T, class U> void f(T, U) { S<U> su; }
9830 and supposing that we are instantiating f<int, double>,
9831 then our ARGS will be {int, double}, but, when looking up
9832 S we only want {double}. */
9833 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9835 if (argvec
== error_mark_node
)
9836 r
= error_mark_node
;
9839 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9840 entering_scope
, complain
);
9841 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9844 cp_unevaluated_operand
= saved_unevaluated_operand
;
9845 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9850 /* This is not a template type, so there's nothing to do. */
9854 return tsubst (t
, args
, complain
, in_decl
);
9858 /* Substitute into the default argument ARG (a default argument for
9859 FN), which has the indicated TYPE. */
9862 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9864 tree saved_class_ptr
= NULL_TREE
;
9865 tree saved_class_ref
= NULL_TREE
;
9867 /* This can happen in invalid code. */
9868 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9871 /* This default argument came from a template. Instantiate the
9872 default argument here, not in tsubst. In the case of
9881 we must be careful to do name lookup in the scope of S<T>,
9882 rather than in the current class. */
9883 push_access_scope (fn
);
9884 /* The "this" pointer is not valid in a default argument. */
9887 saved_class_ptr
= current_class_ptr
;
9888 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9889 saved_class_ref
= current_class_ref
;
9890 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9893 push_deferring_access_checks(dk_no_deferred
);
9894 /* The default argument expression may cause implicitly defined
9895 member functions to be synthesized, which will result in garbage
9896 collection. We must treat this situation as if we were within
9897 the body of function so as to avoid collecting live data on the
9900 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9901 tf_warning_or_error
, NULL_TREE
,
9902 /*integral_constant_expression_p=*/false);
9904 pop_deferring_access_checks();
9906 /* Restore the "this" pointer. */
9909 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9910 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9913 /* Make sure the default argument is reasonable. */
9914 arg
= check_default_argument (type
, arg
);
9916 pop_access_scope (fn
);
9921 /* Substitute into all the default arguments for FN. */
9924 tsubst_default_arguments (tree fn
)
9929 tmpl_args
= DECL_TI_ARGS (fn
);
9931 /* If this function is not yet instantiated, we certainly don't need
9932 its default arguments. */
9933 if (uses_template_parms (tmpl_args
))
9935 /* Don't do this again for clones. */
9936 if (DECL_CLONED_FUNCTION_P (fn
))
9939 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9941 arg
= TREE_CHAIN (arg
))
9942 if (TREE_PURPOSE (arg
))
9943 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9945 TREE_PURPOSE (arg
));
9948 /* Substitute the ARGS into the T, which is a _DECL. Return the
9949 result of the substitution. Issue error and warning messages under
9950 control of COMPLAIN. */
9953 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9955 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9956 location_t saved_loc
;
9961 /* Set the filename and linenumber to improve error-reporting. */
9962 saved_loc
= input_location
;
9963 input_location
= DECL_SOURCE_LOCATION (t
);
9965 switch (TREE_CODE (t
))
9969 /* We can get here when processing a member function template,
9970 member class template, or template template parameter. */
9971 tree decl
= DECL_TEMPLATE_RESULT (t
);
9976 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9978 /* Template template parameter is treated here. */
9979 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9980 if (new_type
== error_mark_node
)
9981 RETURN (error_mark_node
);
9984 DECL_CHAIN (r
) = NULL_TREE
;
9985 TREE_TYPE (r
) = new_type
;
9986 DECL_TEMPLATE_RESULT (r
)
9987 = build_decl (DECL_SOURCE_LOCATION (decl
),
9988 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9989 DECL_TEMPLATE_PARMS (r
)
9990 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9992 TYPE_NAME (new_type
) = r
;
9996 /* We might already have an instance of this template.
9997 The ARGS are for the surrounding class type, so the
9998 full args contain the tsubst'd args for the context,
9999 plus the innermost args from the template decl. */
10000 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10001 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10002 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10003 /* Because this is a template, the arguments will still be
10004 dependent, even after substitution. If
10005 PROCESSING_TEMPLATE_DECL is not set, the dependency
10006 predicates will short-circuit. */
10007 ++processing_template_decl
;
10008 full_args
= tsubst_template_args (tmpl_args
, args
,
10009 complain
, in_decl
);
10010 --processing_template_decl
;
10011 if (full_args
== error_mark_node
)
10012 RETURN (error_mark_node
);
10014 /* If this is a default template template argument,
10015 tsubst might not have changed anything. */
10016 if (full_args
== tmpl_args
)
10019 hash
= hash_tmpl_and_args (t
, full_args
);
10020 spec
= retrieve_specialization (t
, full_args
, hash
);
10021 if (spec
!= NULL_TREE
)
10027 /* Make a new template decl. It will be similar to the
10028 original, but will record the current template arguments.
10029 We also create a new function declaration, which is just
10030 like the old one, but points to this new template, rather
10031 than the old one. */
10033 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10034 DECL_CHAIN (r
) = NULL_TREE
;
10036 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10038 if (TREE_CODE (decl
) == TYPE_DECL
10039 && !TYPE_DECL_ALIAS_P (decl
))
10042 ++processing_template_decl
;
10043 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10044 --processing_template_decl
;
10045 if (new_type
== error_mark_node
)
10046 RETURN (error_mark_node
);
10048 TREE_TYPE (r
) = new_type
;
10049 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10050 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10051 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10052 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10057 ++processing_template_decl
;
10058 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10059 --processing_template_decl
;
10060 if (new_decl
== error_mark_node
)
10061 RETURN (error_mark_node
);
10063 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10064 DECL_TI_TEMPLATE (new_decl
) = r
;
10065 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10066 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10067 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10070 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10071 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10072 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10074 /* The template parameters for this new template are all the
10075 template parameters for the old template, except the
10076 outermost level of parameters. */
10077 DECL_TEMPLATE_PARMS (r
)
10078 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10081 if (PRIMARY_TEMPLATE_P (t
))
10082 DECL_PRIMARY_TEMPLATE (r
) = r
;
10084 if (TREE_CODE (decl
) != TYPE_DECL
)
10085 /* Record this non-type partial instantiation. */
10086 register_specialization (r
, t
,
10087 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10092 case FUNCTION_DECL
:
10095 tree argvec
= NULL_TREE
;
10103 /* Nobody should be tsubst'ing into non-template functions. */
10104 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10106 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10111 /* If T is not dependent, just return it. We have to
10112 increment PROCESSING_TEMPLATE_DECL because
10113 value_dependent_expression_p assumes that nothing is
10114 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10115 ++processing_template_decl
;
10116 dependent_p
= value_dependent_expression_p (t
);
10117 --processing_template_decl
;
10121 /* Calculate the most general template of which R is a
10122 specialization, and the complete set of arguments used to
10124 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10125 argvec
= tsubst_template_args (DECL_TI_ARGS
10126 (DECL_TEMPLATE_RESULT
10127 (DECL_TI_TEMPLATE (t
))),
10128 args
, complain
, in_decl
);
10129 if (argvec
== error_mark_node
)
10130 RETURN (error_mark_node
);
10132 /* Check to see if we already have this specialization. */
10133 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10134 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10142 /* We can see more levels of arguments than parameters if
10143 there was a specialization of a member template, like
10146 template <class T> struct S { template <class U> void f(); }
10147 template <> template <class U> void S<int>::f(U);
10149 Here, we'll be substituting into the specialization,
10150 because that's where we can find the code we actually
10151 want to generate, but we'll have enough arguments for
10152 the most general template.
10154 We also deal with the peculiar case:
10156 template <class T> struct S {
10157 template <class U> friend void f();
10159 template <class U> void f() {}
10161 template void f<double>();
10163 Here, the ARGS for the instantiation of will be {int,
10164 double}. But, we only need as many ARGS as there are
10165 levels of template parameters in CODE_PATTERN. We are
10166 careful not to get fooled into reducing the ARGS in
10169 template <class T> struct S { template <class U> void f(U); }
10170 template <class T> template <> void S<T>::f(int) {}
10172 which we can spot because the pattern will be a
10173 specialization in this case. */
10174 args_depth
= TMPL_ARGS_DEPTH (args
);
10176 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10177 if (args_depth
> parms_depth
10178 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10179 args
= get_innermost_template_args (args
, parms_depth
);
10183 /* This special case arises when we have something like this:
10185 template <class T> struct S {
10186 friend void f<int>(int, double);
10189 Here, the DECL_TI_TEMPLATE for the friend declaration
10190 will be an IDENTIFIER_NODE. We are being called from
10191 tsubst_friend_function, and we want only to create a
10192 new decl (R) with appropriate types so that we can call
10193 determine_specialization. */
10194 gen_tmpl
= NULL_TREE
;
10197 if (DECL_CLASS_SCOPE_P (t
))
10199 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10203 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10204 complain
, t
, /*entering_scope=*/1);
10209 ctx
= DECL_CONTEXT (t
);
10211 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10212 if (type
== error_mark_node
)
10213 RETURN (error_mark_node
);
10215 /* We do NOT check for matching decls pushed separately at this
10216 point, as they may not represent instantiations of this
10217 template, and in any case are considered separate under the
10220 DECL_USE_TEMPLATE (r
) = 0;
10221 TREE_TYPE (r
) = type
;
10222 /* Clear out the mangled name and RTL for the instantiation. */
10223 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10224 SET_DECL_RTL (r
, NULL
);
10225 /* Leave DECL_INITIAL set on deleted instantiations. */
10226 if (!DECL_DELETED_FN (r
))
10227 DECL_INITIAL (r
) = NULL_TREE
;
10228 DECL_CONTEXT (r
) = ctx
;
10230 if (member
&& DECL_CONV_FN_P (r
))
10231 /* Type-conversion operator. Reconstruct the name, in
10232 case it's the name of one of the template's parameters. */
10233 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10235 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10237 DECL_RESULT (r
) = NULL_TREE
;
10239 TREE_STATIC (r
) = 0;
10240 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10241 DECL_EXTERNAL (r
) = 1;
10242 /* If this is an instantiation of a function with internal
10243 linkage, we already know what object file linkage will be
10244 assigned to the instantiation. */
10245 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10246 DECL_DEFER_OUTPUT (r
) = 0;
10247 DECL_CHAIN (r
) = NULL_TREE
;
10248 DECL_PENDING_INLINE_INFO (r
) = 0;
10249 DECL_PENDING_INLINE_P (r
) = 0;
10250 DECL_SAVED_TREE (r
) = NULL_TREE
;
10251 DECL_STRUCT_FUNCTION (r
) = NULL
;
10253 /* We'll re-clone as appropriate in instantiate_template. */
10254 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10256 /* If we aren't complaining now, return on error before we register
10257 the specialization so that we'll complain eventually. */
10258 if ((complain
& tf_error
) == 0
10259 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10260 && !grok_op_properties (r
, /*complain=*/false))
10261 RETURN (error_mark_node
);
10263 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10264 this in the special friend case mentioned above where
10265 GEN_TMPL is NULL. */
10268 DECL_TEMPLATE_INFO (r
)
10269 = build_template_info (gen_tmpl
, argvec
);
10270 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10271 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10273 /* We're not supposed to instantiate default arguments
10274 until they are called, for a template. But, for a
10277 template <class T> void f ()
10278 { extern void g(int i = T()); }
10280 we should do the substitution when the template is
10281 instantiated. We handle the member function case in
10282 instantiate_class_template since the default arguments
10283 might refer to other members of the class. */
10285 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10286 && !uses_template_parms (argvec
))
10287 tsubst_default_arguments (r
);
10290 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10292 /* Copy the list of befriending classes. */
10293 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10295 friends
= &TREE_CHAIN (*friends
))
10297 *friends
= copy_node (*friends
);
10298 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10303 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10305 maybe_retrofit_in_chrg (r
);
10306 if (DECL_CONSTRUCTOR_P (r
))
10307 grok_ctor_properties (ctx
, r
);
10308 /* If this is an instantiation of a member template, clone it.
10309 If it isn't, that'll be handled by
10310 clone_constructors_and_destructors. */
10311 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10312 clone_function_decl (r
, /*update_method_vec_p=*/0);
10314 else if ((complain
& tf_error
) != 0
10315 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10316 && !grok_op_properties (r
, /*complain=*/true))
10317 RETURN (error_mark_node
);
10319 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10320 SET_DECL_FRIEND_CONTEXT (r
,
10321 tsubst (DECL_FRIEND_CONTEXT (t
),
10322 args
, complain
, in_decl
));
10324 /* Possibly limit visibility based on template args. */
10325 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10326 if (DECL_VISIBILITY_SPECIFIED (t
))
10328 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10329 DECL_ATTRIBUTES (r
)
10330 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10332 determine_visibility (r
);
10333 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10334 && !processing_template_decl
)
10335 defaulted_late_check (r
);
10337 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10338 args
, complain
, in_decl
);
10344 tree type
= NULL_TREE
;
10346 tree expanded_types
= NULL_TREE
;
10347 tree prev_r
= NULL_TREE
;
10348 tree first_r
= NULL_TREE
;
10350 if (FUNCTION_PARAMETER_PACK_P (t
))
10352 /* If there is a local specialization that isn't a
10353 parameter pack, it means that we're doing a "simple"
10354 substitution from inside tsubst_pack_expansion. Just
10355 return the local specialization (which will be a single
10357 tree spec
= retrieve_local_specialization (t
);
10359 && TREE_CODE (spec
) == PARM_DECL
10360 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10363 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10364 the parameters in this function parameter pack. */
10365 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10366 complain
, in_decl
);
10367 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10369 len
= TREE_VEC_LENGTH (expanded_types
);
10371 /* Zero-length parameter packs are boring. Just substitute
10374 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10379 /* All we did was update the type. Make a note of that. */
10380 type
= expanded_types
;
10381 expanded_types
= NULL_TREE
;
10385 /* Loop through all of the parameter's we'll build. When T is
10386 a function parameter pack, LEN is the number of expanded
10387 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10389 for (i
= 0; i
< len
; ++i
)
10393 if (DECL_TEMPLATE_PARM_P (t
))
10394 SET_DECL_TEMPLATE_PARM_P (r
);
10396 if (expanded_types
)
10397 /* We're on the Ith parameter of the function parameter
10400 /* An argument of a function parameter pack is not a parameter
10402 FUNCTION_PARAMETER_PACK_P (r
) = false;
10404 /* Get the Ith type. */
10405 type
= TREE_VEC_ELT (expanded_types
, i
);
10408 /* Rename the parameter to include the index. */
10410 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10413 /* We're dealing with a normal parameter. */
10414 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10416 type
= type_decays_to (type
);
10417 TREE_TYPE (r
) = type
;
10418 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10420 if (DECL_INITIAL (r
))
10422 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10423 DECL_INITIAL (r
) = TREE_TYPE (r
);
10425 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10426 complain
, in_decl
);
10429 DECL_CONTEXT (r
) = NULL_TREE
;
10431 if (!DECL_TEMPLATE_PARM_P (r
))
10432 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10434 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10435 args
, complain
, in_decl
);
10437 /* Keep track of the first new parameter we
10438 generate. That's what will be returned to the
10443 /* Build a proper chain of parameters when substituting
10444 into a function parameter pack. */
10446 DECL_CHAIN (prev_r
) = r
;
10449 if (DECL_CHAIN (t
))
10450 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10451 complain
, DECL_CHAIN (t
));
10453 /* FIRST_R contains the start of the chain we've built. */
10463 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10464 if (type
== error_mark_node
)
10465 RETURN (error_mark_node
);
10466 TREE_TYPE (r
) = type
;
10467 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10469 if (DECL_C_BIT_FIELD (r
))
10470 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10471 non-bit-fields DECL_INITIAL is a non-static data member
10472 initializer, which gets deferred instantiation. */
10474 = tsubst_expr (DECL_INITIAL (t
), args
,
10476 /*integral_constant_expression_p=*/true);
10477 else if (DECL_INITIAL (t
))
10479 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10480 NSDMI in perform_member_init. Still set DECL_INITIAL
10481 so that we know there is one. */
10482 DECL_INITIAL (r
) = void_zero_node
;
10483 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10484 retrofit_lang_decl (r
);
10485 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10487 /* We don't have to set DECL_CONTEXT here; it is set by
10488 finish_member_declaration. */
10489 DECL_CHAIN (r
) = NULL_TREE
;
10490 if (VOID_TYPE_P (type
))
10491 error ("instantiation of %q+D as type %qT", r
, type
);
10493 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10494 args
, complain
, in_decl
);
10499 /* We reach here only for member using decls. */
10500 if (DECL_DEPENDENT_P (t
))
10502 r
= do_class_using_decl
10503 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
10504 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
10506 r
= error_mark_node
;
10509 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10510 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10516 DECL_CHAIN (r
) = NULL_TREE
;
10523 tree argvec
= NULL_TREE
;
10524 tree gen_tmpl
= NULL_TREE
;
10526 tree tmpl
= NULL_TREE
;
10528 tree type
= NULL_TREE
;
10531 if (TREE_CODE (t
) == TYPE_DECL
10532 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10534 /* If this is the canonical decl, we don't have to
10535 mess with instantiations, and often we can't (for
10536 typename, template type parms and such). Note that
10537 TYPE_NAME is not correct for the above test if
10538 we've copied the type for a typedef. */
10539 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10540 if (type
== error_mark_node
)
10541 RETURN (error_mark_node
);
10542 r
= TYPE_NAME (type
);
10546 /* Check to see if we already have the specialization we
10549 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10551 /* T is a static data member or namespace-scope entity.
10552 We have to substitute into namespace-scope variables
10553 (even though such entities are never templates) because
10556 template <class T> void f() { extern T t; }
10558 where the entity referenced is not known until
10559 instantiation time. */
10561 ctx
= DECL_CONTEXT (t
);
10562 if (DECL_CLASS_SCOPE_P (t
))
10564 ctx
= tsubst_aggr_type (ctx
, args
,
10566 in_decl
, /*entering_scope=*/1);
10567 /* If CTX is unchanged, then T is in fact the
10568 specialization we want. That situation occurs when
10569 referencing a static data member within in its own
10570 class. We can use pointer equality, rather than
10571 same_type_p, because DECL_CONTEXT is always
10573 if (ctx
== DECL_CONTEXT (t
)
10574 && (TREE_CODE (t
) != TYPE_DECL
10575 /* ... unless T is a member template; in which
10576 case our caller can be willing to create a
10577 specialization of that template represented
10579 || !(DECL_TI_TEMPLATE (t
)
10580 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10586 tmpl
= DECL_TI_TEMPLATE (t
);
10587 gen_tmpl
= most_general_template (tmpl
);
10588 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10589 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10590 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10595 /* A local variable. */
10597 /* Subsequent calls to pushdecl will fill this in. */
10599 spec
= retrieve_local_specialization (t
);
10601 /* If we already have the specialization we need, there is
10602 nothing more to do. */
10609 /* Create a new node for the specialization we need. */
10611 if (type
== NULL_TREE
)
10613 if (is_typedef_decl (t
))
10614 type
= DECL_ORIGINAL_TYPE (t
);
10616 type
= TREE_TYPE (t
);
10617 if (TREE_CODE (t
) == VAR_DECL
10618 && VAR_HAD_UNKNOWN_BOUND (t
)
10619 && type
!= error_mark_node
)
10620 type
= strip_array_domain (type
);
10621 type
= tsubst (type
, args
, complain
, in_decl
);
10623 if (TREE_CODE (r
) == VAR_DECL
)
10625 /* Even if the original location is out of scope, the
10626 newly substituted one is not. */
10627 DECL_DEAD_FOR_LOCAL (r
) = 0;
10628 DECL_INITIALIZED_P (r
) = 0;
10629 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10630 if (type
== error_mark_node
)
10631 RETURN (error_mark_node
);
10632 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10634 /* It may seem that this case cannot occur, since:
10639 declares a function, not a variable. However:
10642 template <typename T> void g() { T t; }
10643 template void g<f>();
10645 is an attempt to declare a variable with function
10647 error ("variable %qD has function type",
10648 /* R is not yet sufficiently initialized, so we
10649 just use its name. */
10651 RETURN (error_mark_node
);
10653 type
= complete_type (type
);
10654 /* Wait until cp_finish_decl to set this again, to handle
10655 circular dependency (template/instantiate6.C). */
10656 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10657 type
= check_var_type (DECL_NAME (r
), type
);
10659 if (DECL_HAS_VALUE_EXPR_P (t
))
10661 tree ve
= DECL_VALUE_EXPR (t
);
10662 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10663 /*constant_expression_p=*/false);
10664 if (REFERENCE_REF_P (ve
))
10666 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10667 ve
= TREE_OPERAND (ve
, 0);
10669 SET_DECL_VALUE_EXPR (r
, ve
);
10672 else if (DECL_SELF_REFERENCE_P (t
))
10673 SET_DECL_SELF_REFERENCE_P (r
);
10674 TREE_TYPE (r
) = type
;
10675 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10676 DECL_CONTEXT (r
) = ctx
;
10677 /* Clear out the mangled name and RTL for the instantiation. */
10678 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10679 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10680 SET_DECL_RTL (r
, NULL
);
10681 /* The initializer must not be expanded until it is required;
10682 see [temp.inst]. */
10683 DECL_INITIAL (r
) = NULL_TREE
;
10684 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10685 SET_DECL_RTL (r
, NULL
);
10686 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10687 if (TREE_CODE (r
) == VAR_DECL
)
10689 /* Possibly limit visibility based on template args. */
10690 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10691 if (DECL_VISIBILITY_SPECIFIED (t
))
10693 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10694 DECL_ATTRIBUTES (r
)
10695 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10697 determine_visibility (r
);
10702 /* A static data member declaration is always marked
10703 external when it is declared in-class, even if an
10704 initializer is present. We mimic the non-template
10705 processing here. */
10706 DECL_EXTERNAL (r
) = 1;
10708 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10709 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10710 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10712 else if (cp_unevaluated_operand
)
10714 /* We're substituting this var in a decltype outside of its
10715 scope, such as for a lambda return type. Don't add it to
10716 local_specializations, do perform auto deduction. */
10717 tree auto_node
= type_uses_auto (type
);
10721 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10722 /*constant_expression_p=*/false);
10723 init
= resolve_nondeduced_context (init
);
10724 TREE_TYPE (r
) = type
10725 = do_auto_deduction (type
, init
, auto_node
);
10729 register_local_specialization (r
, t
);
10731 DECL_CHAIN (r
) = NULL_TREE
;
10733 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10735 args
, complain
, in_decl
);
10737 /* Preserve a typedef that names a type. */
10738 if (is_typedef_decl (r
))
10740 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10741 set_underlying_type (r
);
10744 layout_decl (r
, 0);
10749 gcc_unreachable ();
10754 /* Restore the file and line information. */
10755 input_location
= saved_loc
;
10760 /* Substitute into the ARG_TYPES of a function type. */
10763 tsubst_arg_types (tree arg_types
,
10765 tsubst_flags_t complain
,
10768 tree remaining_arg_types
;
10769 tree type
= NULL_TREE
;
10771 tree expanded_args
= NULL_TREE
;
10774 if (!arg_types
|| arg_types
== void_list_node
)
10777 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10778 args
, complain
, in_decl
);
10779 if (remaining_arg_types
== error_mark_node
)
10780 return error_mark_node
;
10782 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10784 /* For a pack expansion, perform substitution on the
10785 entire expression. Later on, we'll handle the arguments
10787 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10788 args
, complain
, in_decl
);
10790 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10791 /* So that we'll spin through the parameters, one by one. */
10792 i
= TREE_VEC_LENGTH (expanded_args
);
10795 /* We only partially substituted into the parameter
10796 pack. Our type is TYPE_PACK_EXPANSION. */
10797 type
= expanded_args
;
10798 expanded_args
= NULL_TREE
;
10806 type
= TREE_VEC_ELT (expanded_args
, i
);
10808 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10810 if (type
== error_mark_node
)
10811 return error_mark_node
;
10812 if (VOID_TYPE_P (type
))
10814 if (complain
& tf_error
)
10816 error ("invalid parameter type %qT", type
);
10818 error ("in declaration %q+D", in_decl
);
10820 return error_mark_node
;
10823 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10824 top-level qualifiers as required. */
10825 type
= cv_unqualified (type_decays_to (type
));
10827 /* We do not substitute into default arguments here. The standard
10828 mandates that they be instantiated only when needed, which is
10829 done in build_over_call. */
10830 default_arg
= TREE_PURPOSE (arg_types
);
10832 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10834 /* We've instantiated a template before its default arguments
10835 have been parsed. This can happen for a nested template
10836 class, and is not an error unless we require the default
10837 argument in a call of this function. */
10838 remaining_arg_types
=
10839 tree_cons (default_arg
, type
, remaining_arg_types
);
10840 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
10841 remaining_arg_types
);
10844 remaining_arg_types
=
10845 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10848 return remaining_arg_types
;
10851 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10852 *not* handle the exception-specification for FNTYPE, because the
10853 initial substitution of explicitly provided template parameters
10854 during argument deduction forbids substitution into the
10855 exception-specification:
10859 All references in the function type of the function template to the
10860 corresponding template parameters are replaced by the specified tem-
10861 plate argument values. If a substitution in a template parameter or
10862 in the function type of the function template results in an invalid
10863 type, type deduction fails. [Note: The equivalent substitution in
10864 exception specifications is done only when the function is instanti-
10865 ated, at which point a program is ill-formed if the substitution
10866 results in an invalid type.] */
10869 tsubst_function_type (tree t
,
10871 tsubst_flags_t complain
,
10878 /* The TYPE_CONTEXT is not used for function/method types. */
10879 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10881 /* Substitute the return type. */
10882 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10883 if (return_type
== error_mark_node
)
10884 return error_mark_node
;
10885 /* The standard does not presently indicate that creation of a
10886 function type with an invalid return type is a deduction failure.
10887 However, that is clearly analogous to creating an array of "void"
10888 or a reference to a reference. This is core issue #486. */
10889 if (TREE_CODE (return_type
) == ARRAY_TYPE
10890 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10892 if (complain
& tf_error
)
10894 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10895 error ("function returning an array");
10897 error ("function returning a function");
10899 return error_mark_node
;
10902 /* Substitute the argument types. */
10903 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
10904 complain
, in_decl
);
10905 if (arg_types
== error_mark_node
)
10906 return error_mark_node
;
10908 /* Construct a new type node and return it. */
10909 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10911 fntype
= build_function_type (return_type
, arg_types
);
10912 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10916 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10917 if (! MAYBE_CLASS_TYPE_P (r
))
10921 Type deduction may fail for any of the following
10924 -- Attempting to create "pointer to member of T" when T
10925 is not a class type. */
10926 if (complain
& tf_error
)
10927 error ("creating pointer to member function of non-class type %qT",
10929 return error_mark_node
;
10932 fntype
= build_method_type_directly (r
, return_type
,
10933 TREE_CHAIN (arg_types
));
10935 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10940 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10941 ARGS into that specification, and return the substituted
10942 specification. If there is no specification, return NULL_TREE. */
10945 tsubst_exception_specification (tree fntype
,
10947 tsubst_flags_t complain
,
10954 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10955 new_specs
= NULL_TREE
;
10956 if (specs
&& TREE_PURPOSE (specs
))
10958 /* A noexcept-specifier. */
10959 tree expr
= TREE_PURPOSE (specs
);
10960 if (expr
== boolean_true_node
|| expr
== boolean_false_node
)
10964 /* Defer instantiation of noexcept-specifiers to avoid
10965 excessive instantiations (c++/49107). */
10966 new_specs
= make_node (DEFERRED_NOEXCEPT
);
10967 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
10969 /* We already partially instantiated this member template,
10970 so combine the new args with the old. */
10971 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
10972 = DEFERRED_NOEXCEPT_PATTERN (expr
);
10973 DEFERRED_NOEXCEPT_ARGS (new_specs
)
10974 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
10978 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
10979 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
10983 new_specs
= tsubst_copy_and_build
10984 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
10985 /*integral_constant_expression_p=*/true);
10986 new_specs
= build_noexcept_spec (new_specs
, complain
);
10990 if (! TREE_VALUE (specs
))
10997 tree expanded_specs
= NULL_TREE
;
10999 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11001 /* Expand the pack expansion type. */
11002 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11006 if (expanded_specs
== error_mark_node
)
11007 return error_mark_node
;
11008 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11009 len
= TREE_VEC_LENGTH (expanded_specs
);
11012 /* We're substituting into a member template, so
11013 we got a TYPE_PACK_EXPANSION back. Add that
11014 expansion and move on. */
11015 gcc_assert (TREE_CODE (expanded_specs
)
11016 == TYPE_PACK_EXPANSION
);
11017 new_specs
= add_exception_specifier (new_specs
,
11020 specs
= TREE_CHAIN (specs
);
11025 for (i
= 0; i
< len
; ++i
)
11027 if (expanded_specs
)
11028 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11030 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11031 if (spec
== error_mark_node
)
11033 new_specs
= add_exception_specifier (new_specs
, spec
,
11037 specs
= TREE_CHAIN (specs
);
11043 /* Take the tree structure T and replace template parameters used
11044 therein with the argument vector ARGS. IN_DECL is an associated
11045 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11046 Issue error and warning messages under control of COMPLAIN. Note
11047 that we must be relatively non-tolerant of extensions here, in
11048 order to preserve conformance; if we allow substitutions that
11049 should not be allowed, we may allow argument deductions that should
11050 not succeed, and therefore report ambiguous overload situations
11051 where there are none. In theory, we could allow the substitution,
11052 but indicate that it should have failed, and allow our caller to
11053 make sure that the right thing happens, but we don't try to do this
11056 This function is used for dealing with types, decls and the like;
11057 for expressions, use tsubst_expr or tsubst_copy. */
11060 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11062 enum tree_code code
;
11063 tree type
, r
= NULL_TREE
;
11065 if (t
== NULL_TREE
|| t
== error_mark_node
11066 || t
== integer_type_node
11067 || t
== void_type_node
11068 || t
== char_type_node
11069 || t
== unknown_type_node
11070 || TREE_CODE (t
) == NAMESPACE_DECL
11071 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11075 return tsubst_decl (t
, args
, complain
);
11077 if (args
== NULL_TREE
)
11080 code
= TREE_CODE (t
);
11082 if (code
== IDENTIFIER_NODE
)
11083 type
= IDENTIFIER_TYPE_VALUE (t
);
11085 type
= TREE_TYPE (t
);
11087 gcc_assert (type
!= unknown_type_node
);
11089 /* Reuse typedefs. We need to do this to handle dependent attributes,
11090 such as attribute aligned. */
11092 && typedef_variant_p (t
))
11094 tree decl
= TYPE_NAME (t
);
11096 if (TYPE_DECL_ALIAS_P (decl
)
11097 && DECL_LANG_SPECIFIC (decl
)
11098 && DECL_TEMPLATE_INFO (decl
)
11099 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
11101 /* DECL represents an alias template and we want to
11102 instantiate it. Let's substitute our arguments for the
11103 template parameters into the declaration and get the
11105 r
= tsubst (decl
, args
, complain
, decl
);
11107 else if (DECL_CLASS_SCOPE_P (decl
)
11108 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11109 && uses_template_parms (DECL_CONTEXT (decl
)))
11111 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11112 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11113 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11115 else if (DECL_FUNCTION_SCOPE_P (decl
)
11116 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11117 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11118 r
= retrieve_local_specialization (decl
);
11120 /* The typedef is from a non-template context. */
11126 r
= cp_build_qualified_type_real
11127 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11128 complain
| tf_ignore_bad_quals
);
11132 /* We don't have an instantiation yet, so drop the typedef. */
11133 t
= DECL_ORIGINAL_TYPE (decl
);
11137 && code
!= TYPENAME_TYPE
11138 && code
!= TEMPLATE_TYPE_PARM
11139 && code
!= IDENTIFIER_NODE
11140 && code
!= FUNCTION_TYPE
11141 && code
!= METHOD_TYPE
)
11142 type
= tsubst (type
, args
, complain
, in_decl
);
11143 if (type
== error_mark_node
)
11144 return error_mark_node
;
11150 case ENUMERAL_TYPE
:
11151 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11152 /*entering_scope=*/0);
11155 case IDENTIFIER_NODE
:
11166 if (t
== integer_type_node
)
11169 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11170 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11174 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11176 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11177 /*integral_constant_expression_p=*/false);
11179 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11181 if (TREE_CODE (max
) == NOP_EXPR
11182 && TREE_SIDE_EFFECTS (omax
)
11183 && !TREE_TYPE (max
))
11184 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11186 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11187 with TREE_SIDE_EFFECTS that indicates this is not an integral
11188 constant expression. */
11189 if (processing_template_decl
11190 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11192 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11193 TREE_SIDE_EFFECTS (max
) = 1;
11196 return compute_array_index_type (NULL_TREE
, max
, complain
);
11199 case TEMPLATE_TYPE_PARM
:
11200 case TEMPLATE_TEMPLATE_PARM
:
11201 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11202 case TEMPLATE_PARM_INDEX
:
11207 tree arg
= NULL_TREE
;
11211 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11212 template_parm_level_and_index (t
, &level
, &idx
);
11214 levels
= TMPL_ARGS_DEPTH (args
);
11215 if (level
<= levels
)
11217 arg
= TMPL_ARG (args
, level
, idx
);
11219 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11220 /* See through ARGUMENT_PACK_SELECT arguments. */
11221 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11224 if (arg
== error_mark_node
)
11225 return error_mark_node
;
11226 else if (arg
!= NULL_TREE
)
11228 if (ARGUMENT_PACK_P (arg
))
11229 /* If ARG is an argument pack, we don't actually want to
11230 perform a substitution here, because substitutions
11231 for argument packs are only done
11232 element-by-element. We can get to this point when
11233 substituting the type of a non-type template
11234 parameter pack, when that type actually contains
11235 template parameter packs from an outer template, e.g.,
11237 template<typename... Types> struct A {
11238 template<Types... Values> struct B { };
11242 if (code
== TEMPLATE_TYPE_PARM
)
11245 gcc_assert (TYPE_P (arg
));
11247 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11249 return cp_build_qualified_type_real
11250 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11252 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11254 /* We are processing a type constructed from a
11255 template template parameter. */
11256 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11257 args
, complain
, in_decl
);
11258 if (argvec
== error_mark_node
)
11259 return error_mark_node
;
11261 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11262 || TREE_CODE (arg
) == TEMPLATE_DECL
11263 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11265 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11266 /* Consider this code:
11268 template <template <class> class Template>
11270 template <class Arg> using Bind = Template<Arg>;
11273 template <template <class> class Template, class Arg>
11274 using Instantiate = Template<Arg>; //#0
11276 template <template <class> class Template,
11279 Instantiate<Internal<Template>::template Bind,
11282 When #1 is parsed, the
11283 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11284 parameter `Template' in #0 matches the
11285 UNBOUND_CLASS_TEMPLATE representing the argument
11286 `Internal<Template>::template Bind'; We then want
11287 to assemble the type `Bind<Argument>' that can't
11288 be fully created right now, because
11289 `Internal<Template>' not being complete, the Bind
11290 template cannot be looked up in that context. So
11291 we need to "store" `Bind<Argument>' for later
11292 when the context of Bind becomes complete. Let's
11293 store that in a TYPENAME_TYPE. */
11294 return make_typename_type (TYPE_CONTEXT (arg
),
11295 build_nt (TEMPLATE_ID_EXPR
,
11296 TYPE_IDENTIFIER (arg
),
11301 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11302 are resolving nested-types in the signature of a
11303 member function templates. Otherwise ARG is a
11304 TEMPLATE_DECL and is the real template to be
11306 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11307 arg
= TYPE_NAME (arg
);
11309 r
= lookup_template_class (arg
,
11311 DECL_CONTEXT (arg
),
11312 /*entering_scope=*/0,
11314 return cp_build_qualified_type_real
11315 (r
, cp_type_quals (t
), complain
);
11318 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11319 return convert_from_reference (unshare_expr (arg
));
11323 /* This can happen during the attempted tsubst'ing in
11324 unify. This means that we don't yet have any information
11325 about the template parameter in question. */
11328 /* Early in template argument deduction substitution, we don't
11329 want to reduce the level of 'auto', or it will be confused
11330 with a normal template parm in subsequent deduction. */
11331 if (is_auto (t
) && (complain
& tf_partial
))
11334 /* If we get here, we must have been looking at a parm for a
11335 more deeply nested template. Make a new version of this
11336 template parameter, but with a lower level. */
11339 case TEMPLATE_TYPE_PARM
:
11340 case TEMPLATE_TEMPLATE_PARM
:
11341 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11342 if (cp_type_quals (t
))
11344 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11345 r
= cp_build_qualified_type_real
11346 (r
, cp_type_quals (t
),
11347 complain
| (code
== TEMPLATE_TYPE_PARM
11348 ? tf_ignore_bad_quals
: 0));
11353 TEMPLATE_TYPE_PARM_INDEX (r
)
11354 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11355 r
, levels
, args
, complain
);
11356 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11357 TYPE_MAIN_VARIANT (r
) = r
;
11358 TYPE_POINTER_TO (r
) = NULL_TREE
;
11359 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11361 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11362 /* We have reduced the level of the template
11363 template parameter, but not the levels of its
11364 template parameters, so canonical_type_parameter
11365 will not be able to find the canonical template
11366 template parameter for this level. Thus, we
11367 require structural equality checking to compare
11368 TEMPLATE_TEMPLATE_PARMs. */
11369 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11370 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11371 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11373 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11375 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11377 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11378 complain
, in_decl
);
11379 if (argvec
== error_mark_node
)
11380 return error_mark_node
;
11382 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11383 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11388 case TEMPLATE_PARM_INDEX
:
11389 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11393 gcc_unreachable ();
11401 tree purpose
, value
, chain
;
11403 if (t
== void_list_node
)
11406 purpose
= TREE_PURPOSE (t
);
11409 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11410 if (purpose
== error_mark_node
)
11411 return error_mark_node
;
11413 value
= TREE_VALUE (t
);
11416 value
= tsubst (value
, args
, complain
, in_decl
);
11417 if (value
== error_mark_node
)
11418 return error_mark_node
;
11420 chain
= TREE_CHAIN (t
);
11421 if (chain
&& chain
!= void_type_node
)
11423 chain
= tsubst (chain
, args
, complain
, in_decl
);
11424 if (chain
== error_mark_node
)
11425 return error_mark_node
;
11427 if (purpose
== TREE_PURPOSE (t
)
11428 && value
== TREE_VALUE (t
)
11429 && chain
== TREE_CHAIN (t
))
11431 return hash_tree_cons (purpose
, value
, chain
);
11435 /* We should never be tsubsting a binfo. */
11436 gcc_unreachable ();
11439 /* A vector of template arguments. */
11440 gcc_assert (!type
);
11441 return tsubst_template_args (t
, args
, complain
, in_decl
);
11444 case REFERENCE_TYPE
:
11446 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11451 Type deduction may fail for any of the following
11454 -- Attempting to create a pointer to reference type.
11455 -- Attempting to create a reference to a reference type or
11456 a reference to void.
11458 Core issue 106 says that creating a reference to a reference
11459 during instantiation is no longer a cause for failure. We
11460 only enforce this check in strict C++98 mode. */
11461 if ((TREE_CODE (type
) == REFERENCE_TYPE
11462 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11463 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11465 static location_t last_loc
;
11467 /* We keep track of the last time we issued this error
11468 message to avoid spewing a ton of messages during a
11469 single bad template instantiation. */
11470 if (complain
& tf_error
11471 && last_loc
!= input_location
)
11473 if (TREE_CODE (type
) == VOID_TYPE
)
11474 error ("forming reference to void");
11475 else if (code
== POINTER_TYPE
)
11476 error ("forming pointer to reference type %qT", type
);
11478 error ("forming reference to reference type %qT", type
);
11479 last_loc
= input_location
;
11482 return error_mark_node
;
11484 else if (code
== POINTER_TYPE
)
11486 r
= build_pointer_type (type
);
11487 if (TREE_CODE (type
) == METHOD_TYPE
)
11488 r
= build_ptrmemfunc_type (r
);
11490 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11491 /* In C++0x, during template argument substitution, when there is an
11492 attempt to create a reference to a reference type, reference
11493 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11495 "If a template-argument for a template-parameter T names a type
11496 that is a reference to a type A, an attempt to create the type
11497 'lvalue reference to cv T' creates the type 'lvalue reference to
11498 A,' while an attempt to create the type type rvalue reference to
11499 cv T' creates the type T"
11501 r
= cp_build_reference_type
11503 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11505 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11506 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11508 if (r
!= error_mark_node
)
11509 /* Will this ever be needed for TYPE_..._TO values? */
11516 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11517 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11521 Type deduction may fail for any of the following
11524 -- Attempting to create "pointer to member of T" when T
11525 is not a class type. */
11526 if (complain
& tf_error
)
11527 error ("creating pointer to member of non-class type %qT", r
);
11528 return error_mark_node
;
11530 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11532 if (complain
& tf_error
)
11533 error ("creating pointer to member reference type %qT", type
);
11534 return error_mark_node
;
11536 if (TREE_CODE (type
) == VOID_TYPE
)
11538 if (complain
& tf_error
)
11539 error ("creating pointer to member of type void");
11540 return error_mark_node
;
11542 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11543 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11545 /* The type of the implicit object parameter gets its
11546 cv-qualifiers from the FUNCTION_TYPE. */
11548 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11549 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11550 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11554 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11558 case FUNCTION_TYPE
:
11563 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11564 if (fntype
== error_mark_node
)
11565 return error_mark_node
;
11567 /* Substitute the exception specification. */
11568 specs
= tsubst_exception_specification (t
, args
, complain
,
11569 in_decl
, /*defer_ok*/true);
11570 if (specs
== error_mark_node
)
11571 return error_mark_node
;
11573 fntype
= build_exception_variant (fntype
, specs
);
11578 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11579 if (domain
== error_mark_node
)
11580 return error_mark_node
;
11582 /* As an optimization, we avoid regenerating the array type if
11583 it will obviously be the same as T. */
11584 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11587 /* These checks should match the ones in grokdeclarator.
11591 The deduction may fail for any of the following reasons:
11593 -- Attempting to create an array with an element type that
11594 is void, a function type, or a reference type, or [DR337]
11595 an abstract class type. */
11596 if (TREE_CODE (type
) == VOID_TYPE
11597 || TREE_CODE (type
) == FUNCTION_TYPE
11598 || TREE_CODE (type
) == REFERENCE_TYPE
)
11600 if (complain
& tf_error
)
11601 error ("creating array of %qT", type
);
11602 return error_mark_node
;
11604 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
11606 if (complain
& tf_error
)
11607 error ("creating array of %qT, which is an abstract class type",
11609 return error_mark_node
;
11612 r
= build_cplus_array_type (type
, domain
);
11614 if (TYPE_USER_ALIGN (t
))
11616 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11617 TYPE_USER_ALIGN (r
) = 1;
11623 case TYPENAME_TYPE
:
11625 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11626 in_decl
, /*entering_scope=*/1);
11627 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11628 complain
, in_decl
);
11630 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11631 return error_mark_node
;
11633 if (!MAYBE_CLASS_TYPE_P (ctx
))
11635 if (complain
& tf_error
)
11636 error ("%qT is not a class, struct, or union type", ctx
);
11637 return error_mark_node
;
11639 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11641 /* Normally, make_typename_type does not require that the CTX
11642 have complete type in order to allow things like:
11644 template <class T> struct S { typename S<T>::X Y; };
11646 But, such constructs have already been resolved by this
11647 point, so here CTX really should have complete type, unless
11648 it's a partial instantiation. */
11649 ctx
= complete_type (ctx
);
11650 if (!COMPLETE_TYPE_P (ctx
))
11652 if (complain
& tf_error
)
11653 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11654 return error_mark_node
;
11658 f
= make_typename_type (ctx
, f
, typename_type
,
11659 (complain
& tf_error
) | tf_keep_type_decl
);
11660 if (f
== error_mark_node
)
11662 if (TREE_CODE (f
) == TYPE_DECL
)
11664 complain
|= tf_ignore_bad_quals
;
11668 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11670 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11672 if (complain
& tf_error
)
11673 error ("%qT resolves to %qT, which is not an enumeration type",
11676 return error_mark_node
;
11678 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11680 if (complain
& tf_error
)
11681 error ("%qT resolves to %qT, which is is not a class type",
11684 return error_mark_node
;
11688 return cp_build_qualified_type_real
11689 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11692 case UNBOUND_CLASS_TEMPLATE
:
11694 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11695 in_decl
, /*entering_scope=*/1);
11696 tree name
= TYPE_IDENTIFIER (t
);
11697 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11699 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11700 return error_mark_node
;
11703 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11704 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11711 ++cp_unevaluated_operand
;
11712 ++c_inhibit_evaluation_warnings
;
11714 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11716 /*integral_constant_expression_p=*/false);
11718 --cp_unevaluated_operand
;
11719 --c_inhibit_evaluation_warnings
;
11721 type
= finish_typeof (type
);
11722 return cp_build_qualified_type_real (type
,
11724 | cp_type_quals (type
),
11728 case DECLTYPE_TYPE
:
11732 ++cp_unevaluated_operand
;
11733 ++c_inhibit_evaluation_warnings
;
11735 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11737 /*integral_constant_expression_p=*/false);
11739 --cp_unevaluated_operand
;
11740 --c_inhibit_evaluation_warnings
;
11742 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11743 type
= lambda_capture_field_type (type
);
11744 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11745 type
= lambda_proxy_type (type
);
11747 type
= finish_decltype_type
11748 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11749 return cp_build_qualified_type_real (type
,
11751 | cp_type_quals (type
),
11755 case UNDERLYING_TYPE
:
11757 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11758 complain
, in_decl
);
11759 return finish_underlying_type (type
);
11762 case TYPE_ARGUMENT_PACK
:
11763 case NONTYPE_ARGUMENT_PACK
:
11765 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11767 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11771 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11773 /* For template nontype argument packs, also substitute into
11775 if (code
== NONTYPE_ARGUMENT_PACK
)
11776 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11794 /* We should use one of the expression tsubsts for these codes. */
11795 gcc_unreachable ();
11798 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11799 return error_mark_node
;
11803 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11804 type of the expression on the left-hand side of the "." or "->"
11808 tsubst_baselink (tree baselink
, tree object_type
,
11809 tree args
, tsubst_flags_t complain
, tree in_decl
)
11812 tree qualifying_scope
;
11815 tree template_args
= 0;
11816 bool template_id_p
= false;
11817 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
11819 /* A baselink indicates a function from a base class. Both the
11820 BASELINK_ACCESS_BINFO and the base class referenced may
11821 indicate bases of the template class, rather than the
11822 instantiated class. In addition, lookups that were not
11823 ambiguous before may be ambiguous now. Therefore, we perform
11824 the lookup again. */
11825 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11826 qualifying_scope
= tsubst (qualifying_scope
, args
,
11827 complain
, in_decl
);
11828 fns
= BASELINK_FUNCTIONS (baselink
);
11829 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11830 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11832 template_id_p
= true;
11833 template_args
= TREE_OPERAND (fns
, 1);
11834 fns
= TREE_OPERAND (fns
, 0);
11836 template_args
= tsubst_template_args (template_args
, args
,
11837 complain
, in_decl
);
11839 name
= DECL_NAME (get_first_fn (fns
));
11840 if (IDENTIFIER_TYPENAME_P (name
))
11841 name
= mangle_conv_op_name_for_type (optype
);
11842 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11844 return error_mark_node
;
11846 /* If lookup found a single function, mark it as used at this
11847 point. (If it lookup found multiple functions the one selected
11848 later by overload resolution will be marked as used at that
11850 if (BASELINK_P (baselink
))
11851 fns
= BASELINK_FUNCTIONS (baselink
);
11852 if (!template_id_p
&& !really_overloaded_fn (fns
))
11853 mark_used (OVL_CURRENT (fns
));
11855 /* Add back the template arguments, if present. */
11856 if (BASELINK_P (baselink
) && template_id_p
)
11857 BASELINK_FUNCTIONS (baselink
)
11858 = build_nt (TEMPLATE_ID_EXPR
,
11859 BASELINK_FUNCTIONS (baselink
),
11861 /* Update the conversion operator type. */
11862 BASELINK_OPTYPE (baselink
) = optype
;
11865 object_type
= current_class_type
;
11868 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
11874 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11875 true if the qualified-id will be a postfix-expression in-and-of
11876 itself; false if more of the postfix-expression follows the
11877 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11881 tsubst_qualified_id (tree qualified_id
, tree args
,
11882 tsubst_flags_t complain
, tree in_decl
,
11883 bool done
, bool address_p
)
11889 tree template_args
;
11891 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11893 /* Figure out what name to look up. */
11894 name
= TREE_OPERAND (qualified_id
, 1);
11895 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11897 is_template
= true;
11898 template_args
= TREE_OPERAND (name
, 1);
11900 template_args
= tsubst_template_args (template_args
, args
,
11901 complain
, in_decl
);
11902 name
= TREE_OPERAND (name
, 0);
11906 is_template
= false;
11907 template_args
= NULL_TREE
;
11910 /* Substitute into the qualifying scope. When there are no ARGS, we
11911 are just trying to simplify a non-dependent expression. In that
11912 case the qualifying scope may be dependent, and, in any case,
11913 substituting will not help. */
11914 scope
= TREE_OPERAND (qualified_id
, 0);
11917 scope
= tsubst (scope
, args
, complain
, in_decl
);
11918 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11923 if (dependent_scope_p (scope
))
11926 expr
= build_min_nt (TEMPLATE_ID_EXPR
, expr
, template_args
);
11927 return build_qualified_name (NULL_TREE
, scope
, expr
,
11928 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11931 if (!BASELINK_P (name
) && !DECL_P (expr
))
11933 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11935 /* A BIT_NOT_EXPR is used to represent a destructor. */
11936 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11938 error ("qualifying type %qT does not match destructor name ~%qT",
11939 scope
, TREE_OPERAND (expr
, 0));
11940 expr
= error_mark_node
;
11943 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11944 /*is_type_p=*/0, false);
11947 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11948 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11949 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11951 if (complain
& tf_error
)
11953 error ("dependent-name %qE is parsed as a non-type, but "
11954 "instantiation yields a type", qualified_id
);
11955 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11957 return error_mark_node
;
11963 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11965 /* Remember that there was a reference to this entity. */
11969 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
11971 if (complain
& tf_error
)
11972 qualified_name_lookup_error (scope
,
11973 TREE_OPERAND (qualified_id
, 1),
11974 expr
, input_location
);
11975 return error_mark_node
;
11979 expr
= lookup_template_function (expr
, template_args
);
11981 if (expr
== error_mark_node
&& complain
& tf_error
)
11982 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
11983 expr
, input_location
);
11984 else if (TYPE_P (scope
))
11986 expr
= (adjust_result_of_qualified_name_lookup
11987 (expr
, scope
, current_class_type
));
11988 expr
= (finish_qualified_id_expr
11989 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
11990 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
11991 /*template_arg_p=*/false));
11994 /* Expressions do not generally have reference type. */
11995 if (TREE_CODE (expr
) != SCOPE_REF
11996 /* However, if we're about to form a pointer-to-member, we just
11997 want the referenced member referenced. */
11998 && TREE_CODE (expr
) != OFFSET_REF
)
11999 expr
= convert_from_reference (expr
);
12004 /* Like tsubst, but deals with expressions. This function just replaces
12005 template parms; to finish processing the resultant expression, use
12006 tsubst_copy_and_build or tsubst_expr. */
12009 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12011 enum tree_code code
;
12014 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12017 code
= TREE_CODE (t
);
12022 r
= retrieve_local_specialization (t
);
12028 /* We get here for a use of 'this' in an NSDMI. */
12029 if (DECL_NAME (t
) == this_identifier
12030 && at_function_scope_p ()
12031 && DECL_CONSTRUCTOR_P (current_function_decl
))
12032 return current_class_ptr
;
12034 /* This can happen for a parameter name used later in a function
12035 declaration (such as in a late-specified return type). Just
12036 make a dummy decl, since it's only used for its type. */
12037 gcc_assert (cp_unevaluated_operand
!= 0);
12038 /* We copy T because want to tsubst the PARM_DECL only,
12039 not the following PARM_DECLs that are chained to T. */
12041 r
= tsubst_decl (c
, args
, complain
);
12042 /* Give it the template pattern as its context; its true context
12043 hasn't been instantiated yet and this is good enough for
12045 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12048 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12049 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12058 if (DECL_TEMPLATE_PARM_P (t
))
12059 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12060 /* There is no need to substitute into namespace-scope
12062 if (DECL_NAMESPACE_SCOPE_P (t
))
12064 /* If ARGS is NULL, then T is known to be non-dependent. */
12065 if (args
== NULL_TREE
)
12066 return integral_constant_value (t
);
12068 /* Unfortunately, we cannot just call lookup_name here.
12071 template <int I> int f() {
12073 struct S { void g() { E e = a; } };
12076 When we instantiate f<7>::S::g(), say, lookup_name is not
12077 clever enough to find f<7>::a. */
12079 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
12080 /*entering_scope=*/0);
12082 for (v
= TYPE_VALUES (enum_type
);
12084 v
= TREE_CHAIN (v
))
12085 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12086 return TREE_VALUE (v
);
12088 /* We didn't find the name. That should never happen; if
12089 name-lookup found it during preliminary parsing, we
12090 should find it again here during instantiation. */
12091 gcc_unreachable ();
12096 if (DECL_CONTEXT (t
))
12100 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12101 /*entering_scope=*/1);
12102 if (ctx
!= DECL_CONTEXT (t
))
12104 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12107 if (complain
& tf_error
)
12108 error ("using invalid field %qD", t
);
12109 return error_mark_node
;
12118 case FUNCTION_DECL
:
12119 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12120 || local_variable_p (t
))
12121 t
= tsubst (t
, args
, complain
, in_decl
);
12125 case NAMESPACE_DECL
:
12129 /* An OVERLOAD will always be a non-dependent overload set; an
12130 overload set from function scope will just be represented with an
12131 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12132 gcc_assert (!uses_template_parms (t
));
12136 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
12138 case TEMPLATE_DECL
:
12139 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12140 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12141 args
, complain
, in_decl
);
12142 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12143 return tsubst (t
, args
, complain
, in_decl
);
12144 else if (DECL_CLASS_SCOPE_P (t
)
12145 && uses_template_parms (DECL_CONTEXT (t
)))
12147 /* Template template argument like the following example need
12150 template <template <class> class TT> struct C {};
12151 template <class T> struct D {
12152 template <class U> struct E {};
12157 We are processing the template argument `E' in #1 for
12158 the template instantiation #2. Originally, `E' is a
12159 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12160 have to substitute this with one having context `D<int>'. */
12162 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12163 return lookup_field (context
, DECL_NAME(t
), 0, false);
12166 /* Ordinary template template argument. */
12170 case REINTERPRET_CAST_EXPR
:
12171 case CONST_CAST_EXPR
:
12172 case STATIC_CAST_EXPR
:
12173 case DYNAMIC_CAST_EXPR
:
12174 case IMPLICIT_CONV_EXPR
:
12178 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12179 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12182 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12188 ++cp_unevaluated_operand
;
12189 ++c_inhibit_evaluation_warnings
;
12190 /* We only want to compute the number of arguments. */
12191 expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
12192 complain
, in_decl
);
12193 --cp_unevaluated_operand
;
12194 --c_inhibit_evaluation_warnings
;
12196 if (TREE_CODE (expanded
) == TREE_VEC
)
12197 len
= TREE_VEC_LENGTH (expanded
);
12199 if (expanded
== error_mark_node
)
12200 return error_mark_node
;
12201 else if (PACK_EXPANSION_P (expanded
)
12202 || (TREE_CODE (expanded
) == TREE_VEC
12204 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12206 if (TREE_CODE (expanded
) == TREE_VEC
)
12207 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12209 if (TYPE_P (expanded
))
12210 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12211 complain
& tf_error
);
12213 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12214 complain
& tf_error
);
12217 return build_int_cst (size_type_node
, len
);
12223 case TRUTH_NOT_EXPR
:
12226 case UNARY_PLUS_EXPR
: /* Unary + */
12228 case AT_ENCODE_EXPR
:
12232 case REALPART_EXPR
:
12233 case IMAGPART_EXPR
:
12235 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12236 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12238 case COMPONENT_REF
:
12243 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12244 name
= TREE_OPERAND (t
, 1);
12245 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12247 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12248 complain
, in_decl
);
12249 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12251 else if (TREE_CODE (name
) == SCOPE_REF
12252 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12254 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12255 complain
, in_decl
);
12256 name
= TREE_OPERAND (name
, 1);
12257 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12258 complain
, in_decl
);
12259 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12260 name
= build_qualified_name (/*type=*/NULL_TREE
,
12262 /*template_p=*/false);
12264 else if (BASELINK_P (name
))
12265 name
= tsubst_baselink (name
,
12266 non_reference (TREE_TYPE (object
)),
12270 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12271 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12277 case TRUNC_DIV_EXPR
:
12278 case CEIL_DIV_EXPR
:
12279 case FLOOR_DIV_EXPR
:
12280 case ROUND_DIV_EXPR
:
12281 case EXACT_DIV_EXPR
:
12285 case TRUNC_MOD_EXPR
:
12286 case FLOOR_MOD_EXPR
:
12287 case TRUTH_ANDIF_EXPR
:
12288 case TRUTH_ORIF_EXPR
:
12289 case TRUTH_AND_EXPR
:
12290 case TRUTH_OR_EXPR
:
12303 case COMPOUND_EXPR
:
12306 case PREDECREMENT_EXPR
:
12307 case PREINCREMENT_EXPR
:
12308 case POSTDECREMENT_EXPR
:
12309 case POSTINCREMENT_EXPR
:
12311 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12312 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12315 return build_qualified_name (/*type=*/NULL_TREE
,
12316 tsubst_copy (TREE_OPERAND (t
, 0),
12317 args
, complain
, in_decl
),
12318 tsubst_copy (TREE_OPERAND (t
, 1),
12319 args
, complain
, in_decl
),
12320 QUALIFIED_NAME_IS_TEMPLATE (t
));
12325 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12326 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12327 NULL_TREE
, NULL_TREE
);
12331 int n
= VL_EXP_OPERAND_LENGTH (t
);
12332 tree result
= build_vl_exp (CALL_EXPR
, n
);
12334 for (i
= 0; i
< n
; i
++)
12335 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12336 complain
, in_decl
);
12342 case PSEUDO_DTOR_EXPR
:
12345 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12346 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12347 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12348 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12355 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12356 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12357 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12358 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12365 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12366 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12367 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12368 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12372 case TEMPLATE_ID_EXPR
:
12374 /* Substituted template arguments */
12375 tree fn
= TREE_OPERAND (t
, 0);
12376 tree targs
= TREE_OPERAND (t
, 1);
12378 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12380 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12382 return lookup_template_function (fn
, targs
);
12387 tree purpose
, value
, chain
;
12389 if (t
== void_list_node
)
12392 purpose
= TREE_PURPOSE (t
);
12394 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12395 value
= TREE_VALUE (t
);
12397 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12398 chain
= TREE_CHAIN (t
);
12399 if (chain
&& chain
!= void_type_node
)
12400 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12401 if (purpose
== TREE_PURPOSE (t
)
12402 && value
== TREE_VALUE (t
)
12403 && chain
== TREE_CHAIN (t
))
12405 return tree_cons (purpose
, value
, chain
);
12410 case ENUMERAL_TYPE
:
12412 case TEMPLATE_TYPE_PARM
:
12413 case TEMPLATE_TEMPLATE_PARM
:
12414 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12415 case TEMPLATE_PARM_INDEX
:
12417 case REFERENCE_TYPE
:
12419 case FUNCTION_TYPE
:
12422 case TYPENAME_TYPE
:
12423 case UNBOUND_CLASS_TEMPLATE
:
12425 case DECLTYPE_TYPE
:
12427 return tsubst (t
, args
, complain
, in_decl
);
12429 case IDENTIFIER_NODE
:
12430 if (IDENTIFIER_TYPENAME_P (t
))
12432 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12433 return mangle_conv_op_name_for_type (new_type
);
12439 /* This is handled by tsubst_copy_and_build. */
12440 gcc_unreachable ();
12443 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12445 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12447 case CLEANUP_POINT_EXPR
:
12448 /* We shouldn't have built any of these during initial template
12449 generation. Instead, they should be built during instantiation
12450 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12451 gcc_unreachable ();
12455 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12456 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12457 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12458 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12459 mark_used (TREE_OPERAND (r
, 1));
12462 case EXPR_PACK_EXPANSION
:
12463 error ("invalid use of pack expansion expression");
12464 return error_mark_node
;
12466 case NONTYPE_ARGUMENT_PACK
:
12467 error ("use %<...%> to expand argument pack");
12468 return error_mark_node
;
12475 /* Instantiate any typedefs in the type. */
12476 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12477 r
= fold_convert (type
, t
);
12478 gcc_assert (TREE_CODE (r
) == code
);
12483 /* These can sometimes show up in a partial instantiation, but never
12484 involve template parms. */
12485 gcc_assert (!uses_template_parms (t
));
12489 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12490 gcc_checking_assert (false);
12495 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12498 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12501 tree new_clauses
= NULL
, nc
, oc
;
12503 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12505 nc
= copy_node (oc
);
12506 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12509 switch (OMP_CLAUSE_CODE (nc
))
12511 case OMP_CLAUSE_LASTPRIVATE
:
12512 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12514 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12515 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12516 in_decl
, /*integral_constant_expression_p=*/false);
12517 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12518 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12521 case OMP_CLAUSE_PRIVATE
:
12522 case OMP_CLAUSE_SHARED
:
12523 case OMP_CLAUSE_FIRSTPRIVATE
:
12524 case OMP_CLAUSE_REDUCTION
:
12525 case OMP_CLAUSE_COPYIN
:
12526 case OMP_CLAUSE_COPYPRIVATE
:
12527 case OMP_CLAUSE_IF
:
12528 case OMP_CLAUSE_NUM_THREADS
:
12529 case OMP_CLAUSE_SCHEDULE
:
12530 case OMP_CLAUSE_COLLAPSE
:
12531 case OMP_CLAUSE_FINAL
:
12532 OMP_CLAUSE_OPERAND (nc
, 0)
12533 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12534 in_decl
, /*integral_constant_expression_p=*/false);
12536 case OMP_CLAUSE_NOWAIT
:
12537 case OMP_CLAUSE_ORDERED
:
12538 case OMP_CLAUSE_DEFAULT
:
12539 case OMP_CLAUSE_UNTIED
:
12540 case OMP_CLAUSE_MERGEABLE
:
12543 gcc_unreachable ();
12547 return finish_omp_clauses (nreverse (new_clauses
));
12550 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12553 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12556 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12558 tree purpose
, value
, chain
;
12563 if (TREE_CODE (t
) != TREE_LIST
)
12564 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12565 /*function_p=*/false,
12566 /*integral_constant_expression_p=*/false);
12568 if (t
== void_list_node
)
12571 purpose
= TREE_PURPOSE (t
);
12573 purpose
= RECUR (purpose
);
12574 value
= TREE_VALUE (t
);
12577 if (TREE_CODE (value
) != LABEL_DECL
)
12578 value
= RECUR (value
);
12581 value
= lookup_label (DECL_NAME (value
));
12582 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12583 TREE_USED (value
) = 1;
12586 chain
= TREE_CHAIN (t
);
12587 if (chain
&& chain
!= void_type_node
)
12588 chain
= RECUR (chain
);
12589 return tree_cons (purpose
, value
, chain
);
12593 /* Substitute one OMP_FOR iterator. */
12596 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12597 tree condv
, tree incrv
, tree
*clauses
,
12598 tree args
, tsubst_flags_t complain
, tree in_decl
,
12599 bool integral_constant_expression_p
)
12601 #define RECUR(NODE) \
12602 tsubst_expr ((NODE), args, complain, in_decl, \
12603 integral_constant_expression_p)
12604 tree decl
, init
, cond
, incr
, auto_node
;
12606 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12607 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12608 decl
= RECUR (TREE_OPERAND (init
, 0));
12609 init
= TREE_OPERAND (init
, 1);
12610 auto_node
= type_uses_auto (TREE_TYPE (decl
));
12611 if (auto_node
&& init
)
12613 tree init_expr
= init
;
12614 if (TREE_CODE (init_expr
) == DECL_EXPR
)
12615 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
12616 init_expr
= RECUR (init_expr
);
12618 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
12620 gcc_assert (!type_dependent_expression_p (decl
));
12622 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12624 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12625 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12626 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12627 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12628 RECUR (TREE_OPERAND (incr
, 1)),
12631 incr
= RECUR (incr
);
12632 TREE_VEC_ELT (declv
, i
) = decl
;
12633 TREE_VEC_ELT (initv
, i
) = init
;
12634 TREE_VEC_ELT (condv
, i
) = cond
;
12635 TREE_VEC_ELT (incrv
, i
) = incr
;
12639 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
12642 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12644 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12645 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12646 && OMP_CLAUSE_DECL (c
) == decl
)
12648 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12649 && OMP_CLAUSE_DECL (c
) == decl
)
12650 error ("iteration variable %qD should not be firstprivate", decl
);
12651 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12652 && OMP_CLAUSE_DECL (c
) == decl
)
12653 error ("iteration variable %qD should not be reduction", decl
);
12657 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12658 OMP_CLAUSE_DECL (c
) = decl
;
12659 c
= finish_omp_clauses (c
);
12662 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12667 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12668 if (COMPARISON_CLASS_P (cond
))
12669 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12670 RECUR (TREE_OPERAND (cond
, 0)),
12671 RECUR (TREE_OPERAND (cond
, 1)));
12673 cond
= RECUR (cond
);
12674 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12675 switch (TREE_CODE (incr
))
12677 case PREINCREMENT_EXPR
:
12678 case PREDECREMENT_EXPR
:
12679 case POSTINCREMENT_EXPR
:
12680 case POSTDECREMENT_EXPR
:
12681 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12682 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12685 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12686 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12688 tree rhs
= TREE_OPERAND (incr
, 1);
12689 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12690 RECUR (TREE_OPERAND (incr
, 0)),
12691 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12692 RECUR (TREE_OPERAND (rhs
, 0)),
12693 RECUR (TREE_OPERAND (rhs
, 1))));
12696 incr
= RECUR (incr
);
12699 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12700 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12702 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12703 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12704 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12705 TREE_TYPE (decl
), lhs
,
12706 RECUR (TREE_OPERAND (incr
, 2))));
12708 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12709 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12710 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12712 tree rhs
= TREE_OPERAND (incr
, 2);
12713 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12714 RECUR (TREE_OPERAND (incr
, 0)),
12715 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12716 RECUR (TREE_OPERAND (rhs
, 0)),
12717 RECUR (TREE_OPERAND (rhs
, 1))));
12720 incr
= RECUR (incr
);
12723 incr
= RECUR (incr
);
12727 TREE_VEC_ELT (declv
, i
) = decl
;
12728 TREE_VEC_ELT (initv
, i
) = init
;
12729 TREE_VEC_ELT (condv
, i
) = cond
;
12730 TREE_VEC_ELT (incrv
, i
) = incr
;
12734 /* Like tsubst_copy for expressions, etc. but also does semantic
12738 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12739 bool integral_constant_expression_p
)
12741 #define RECUR(NODE) \
12742 tsubst_expr ((NODE), args, complain, in_decl, \
12743 integral_constant_expression_p)
12747 if (t
== NULL_TREE
|| t
== error_mark_node
)
12750 if (EXPR_HAS_LOCATION (t
))
12751 input_location
= EXPR_LOCATION (t
);
12752 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12753 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12755 switch (TREE_CODE (t
))
12757 case STATEMENT_LIST
:
12759 tree_stmt_iterator i
;
12760 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12761 RECUR (tsi_stmt (i
));
12765 case CTOR_INITIALIZER
:
12766 finish_mem_initializers (tsubst_initializer_list
12767 (TREE_OPERAND (t
, 0), args
));
12771 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12775 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12776 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12777 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12779 finish_expr_stmt (tmp
);
12783 do_using_directive (USING_STMT_NAMESPACE (t
));
12788 tree decl
, pattern_decl
;
12791 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12792 if (TREE_CODE (decl
) == LABEL_DECL
)
12793 finish_label_decl (DECL_NAME (decl
));
12794 else if (TREE_CODE (decl
) == USING_DECL
)
12796 tree scope
= USING_DECL_SCOPE (decl
);
12797 tree name
= DECL_NAME (decl
);
12800 scope
= tsubst (scope
, args
, complain
, in_decl
);
12801 decl
= lookup_qualified_name (scope
, name
,
12802 /*is_type_p=*/false,
12803 /*complain=*/false);
12804 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12805 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12807 do_local_using_decl (decl
, scope
, name
);
12811 init
= DECL_INITIAL (decl
);
12812 decl
= tsubst (decl
, args
, complain
, in_decl
);
12813 if (decl
!= error_mark_node
)
12815 /* By marking the declaration as instantiated, we avoid
12816 trying to instantiate it. Since instantiate_decl can't
12817 handle local variables, and since we've already done
12818 all that needs to be done, that's the right thing to
12820 if (TREE_CODE (decl
) == VAR_DECL
)
12821 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12822 if (TREE_CODE (decl
) == VAR_DECL
12823 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12824 /* Anonymous aggregates are a special case. */
12825 finish_anon_union (decl
);
12826 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12828 DECL_CONTEXT (decl
) = current_function_decl
;
12829 insert_capture_proxy (decl
);
12833 int const_init
= false;
12834 maybe_push_decl (decl
);
12835 if (TREE_CODE (decl
) == VAR_DECL
12836 && DECL_PRETTY_FUNCTION_P (decl
))
12838 /* For __PRETTY_FUNCTION__ we have to adjust the
12840 const char *const name
12841 = cxx_printable_name (current_function_decl
, 2);
12842 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12846 tree t
= RECUR (init
);
12850 /* If we had an initializer but it
12851 instantiated to nothing,
12852 value-initialize the object. This will
12853 only occur when the initializer was a
12854 pack expansion where the parameter packs
12855 used in that expansion were of length
12857 init
= build_value_init (TREE_TYPE (decl
),
12859 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12860 init
= get_target_expr_sfinae (init
, complain
);
12866 if (TREE_CODE (decl
) == VAR_DECL
)
12867 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12869 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12874 /* A DECL_EXPR can also be used as an expression, in the condition
12875 clause of an if/for/while construct. */
12880 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12881 RECUR (FOR_INIT_STMT (t
));
12882 finish_for_init_stmt (stmt
);
12883 tmp
= RECUR (FOR_COND (t
));
12884 finish_for_cond (tmp
, stmt
);
12885 tmp
= RECUR (FOR_EXPR (t
));
12886 finish_for_expr (tmp
, stmt
);
12887 RECUR (FOR_BODY (t
));
12888 finish_for_stmt (stmt
);
12891 case RANGE_FOR_STMT
:
12894 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12895 decl
= RANGE_FOR_DECL (t
);
12896 decl
= tsubst (decl
, args
, complain
, in_decl
);
12897 maybe_push_decl (decl
);
12898 expr
= RECUR (RANGE_FOR_EXPR (t
));
12899 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12900 RECUR (RANGE_FOR_BODY (t
));
12901 finish_for_stmt (stmt
);
12906 stmt
= begin_while_stmt ();
12907 tmp
= RECUR (WHILE_COND (t
));
12908 finish_while_stmt_cond (tmp
, stmt
);
12909 RECUR (WHILE_BODY (t
));
12910 finish_while_stmt (stmt
);
12914 stmt
= begin_do_stmt ();
12915 RECUR (DO_BODY (t
));
12916 finish_do_body (stmt
);
12917 tmp
= RECUR (DO_COND (t
));
12918 finish_do_stmt (tmp
, stmt
);
12922 stmt
= begin_if_stmt ();
12923 tmp
= RECUR (IF_COND (t
));
12924 finish_if_stmt_cond (tmp
, stmt
);
12925 RECUR (THEN_CLAUSE (t
));
12926 finish_then_clause (stmt
);
12928 if (ELSE_CLAUSE (t
))
12930 begin_else_clause (stmt
);
12931 RECUR (ELSE_CLAUSE (t
));
12932 finish_else_clause (stmt
);
12935 finish_if_stmt (stmt
);
12939 if (BIND_EXPR_BODY_BLOCK (t
))
12940 stmt
= begin_function_body ();
12942 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12943 ? BCS_TRY_BLOCK
: 0);
12945 RECUR (BIND_EXPR_BODY (t
));
12947 if (BIND_EXPR_BODY_BLOCK (t
))
12948 finish_function_body (stmt
);
12950 finish_compound_stmt (stmt
);
12954 finish_break_stmt ();
12957 case CONTINUE_STMT
:
12958 finish_continue_stmt ();
12962 stmt
= begin_switch_stmt ();
12963 tmp
= RECUR (SWITCH_STMT_COND (t
));
12964 finish_switch_cond (tmp
, stmt
);
12965 RECUR (SWITCH_STMT_BODY (t
));
12966 finish_switch_stmt (stmt
);
12969 case CASE_LABEL_EXPR
:
12970 finish_case_label (EXPR_LOCATION (t
),
12971 RECUR (CASE_LOW (t
)),
12972 RECUR (CASE_HIGH (t
)));
12977 tree decl
= LABEL_EXPR_LABEL (t
);
12980 label
= finish_label_stmt (DECL_NAME (decl
));
12981 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
12982 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
12987 tmp
= GOTO_DESTINATION (t
);
12988 if (TREE_CODE (tmp
) != LABEL_DECL
)
12989 /* Computed goto's must be tsubst'd into. On the other hand,
12990 non-computed gotos must not be; the identifier in question
12991 will have no binding. */
12994 tmp
= DECL_NAME (tmp
);
12995 finish_goto_stmt (tmp
);
12999 tmp
= finish_asm_stmt
13000 (ASM_VOLATILE_P (t
),
13001 RECUR (ASM_STRING (t
)),
13002 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13003 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13004 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13005 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13007 tree asm_expr
= tmp
;
13008 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13009 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13010 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13017 stmt
= begin_try_block ();
13018 RECUR (TRY_STMTS (t
));
13019 finish_cleanup_try_block (stmt
);
13020 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13024 tree compound_stmt
= NULL_TREE
;
13026 if (FN_TRY_BLOCK_P (t
))
13027 stmt
= begin_function_try_block (&compound_stmt
);
13029 stmt
= begin_try_block ();
13031 RECUR (TRY_STMTS (t
));
13033 if (FN_TRY_BLOCK_P (t
))
13034 finish_function_try_block (stmt
);
13036 finish_try_block (stmt
);
13038 RECUR (TRY_HANDLERS (t
));
13039 if (FN_TRY_BLOCK_P (t
))
13040 finish_function_handler_sequence (stmt
, compound_stmt
);
13042 finish_handler_sequence (stmt
);
13048 tree decl
= HANDLER_PARMS (t
);
13052 decl
= tsubst (decl
, args
, complain
, in_decl
);
13053 /* Prevent instantiate_decl from trying to instantiate
13054 this variable. We've already done all that needs to be
13056 if (decl
!= error_mark_node
)
13057 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13059 stmt
= begin_handler ();
13060 finish_handler_parms (decl
, stmt
);
13061 RECUR (HANDLER_BODY (t
));
13062 finish_handler (stmt
);
13067 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13070 case STATIC_ASSERT
:
13073 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13076 /*integral_constant_expression_p=*/true);
13077 finish_static_assert (condition
,
13078 STATIC_ASSERT_MESSAGE (t
),
13079 STATIC_ASSERT_SOURCE_LOCATION (t
),
13080 /*member_p=*/false);
13085 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
13086 args
, complain
, in_decl
);
13087 stmt
= begin_omp_parallel ();
13088 RECUR (OMP_PARALLEL_BODY (t
));
13089 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13090 = OMP_PARALLEL_COMBINED (t
);
13094 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
13095 args
, complain
, in_decl
);
13096 stmt
= begin_omp_task ();
13097 RECUR (OMP_TASK_BODY (t
));
13098 finish_omp_task (tmp
, stmt
);
13103 tree clauses
, body
, pre_body
;
13104 tree declv
, initv
, condv
, incrv
;
13107 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
13108 args
, complain
, in_decl
);
13109 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13110 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13111 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13112 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13114 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13115 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13116 &clauses
, args
, complain
, in_decl
,
13117 integral_constant_expression_p
);
13119 stmt
= begin_omp_structured_block ();
13121 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
13122 if (TREE_VEC_ELT (initv
, i
) == NULL
13123 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
13124 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
13125 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
13127 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
13128 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
13129 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
13133 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
13134 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
13135 gcc_assert (init
!= NULL
);
13136 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
13137 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
13139 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
13142 pre_body
= push_stmt_list ();
13143 RECUR (OMP_FOR_PRE_BODY (t
));
13144 pre_body
= pop_stmt_list (pre_body
);
13146 body
= push_stmt_list ();
13147 RECUR (OMP_FOR_BODY (t
));
13148 body
= pop_stmt_list (body
);
13150 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13151 body
, pre_body
, clauses
);
13153 add_stmt (finish_omp_structured_block (stmt
));
13159 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13160 stmt
= push_stmt_list ();
13161 RECUR (OMP_BODY (t
));
13162 stmt
= pop_stmt_list (stmt
);
13165 OMP_BODY (t
) = stmt
;
13166 OMP_CLAUSES (t
) = tmp
;
13174 stmt
= push_stmt_list ();
13175 RECUR (OMP_BODY (t
));
13176 stmt
= pop_stmt_list (stmt
);
13179 OMP_BODY (t
) = stmt
;
13184 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13185 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13187 tree op1
= TREE_OPERAND (t
, 1);
13188 tree rhs1
= NULL_TREE
;
13190 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13192 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13193 op1
= TREE_OPERAND (op1
, 1);
13195 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13196 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13197 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13198 NULL_TREE
, NULL_TREE
, rhs1
);
13202 tree op1
= TREE_OPERAND (t
, 1);
13203 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13204 tree rhs1
= NULL_TREE
;
13205 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13206 enum tree_code opcode
= NOP_EXPR
;
13207 if (code
== OMP_ATOMIC_READ
)
13209 v
= RECUR (TREE_OPERAND (op1
, 0));
13210 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13212 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13213 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13215 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13216 v
= RECUR (TREE_OPERAND (op1
, 0));
13217 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13218 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13220 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13221 op11
= TREE_OPERAND (op11
, 1);
13223 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13224 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13225 opcode
= TREE_CODE (op11
);
13230 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13231 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13233 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13237 case TRANSACTION_EXPR
:
13240 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13241 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13243 if (TRANSACTION_EXPR_IS_STMT (t
))
13245 tree body
= TRANSACTION_EXPR_BODY (t
);
13246 tree noex
= NULL_TREE
;
13247 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13249 noex
= MUST_NOT_THROW_COND (body
);
13250 if (noex
== NULL_TREE
)
13251 noex
= boolean_true_node
;
13252 body
= TREE_OPERAND (body
, 0);
13254 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13256 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13260 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13261 RECUR (TRANSACTION_EXPR_BODY (t
)),
13268 case MUST_NOT_THROW_EXPR
:
13269 return build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13270 RECUR (MUST_NOT_THROW_COND (t
)));
13272 case EXPR_PACK_EXPANSION
:
13273 error ("invalid use of pack expansion expression");
13274 return error_mark_node
;
13276 case NONTYPE_ARGUMENT_PACK
:
13277 error ("use %<...%> to expand argument pack");
13278 return error_mark_node
;
13281 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13283 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13284 /*function_p=*/false,
13285 integral_constant_expression_p
);
13292 /* T is a postfix-expression that is not being used in a function
13293 call. Return the substituted version of T. */
13296 tsubst_non_call_postfix_expression (tree t
, tree args
,
13297 tsubst_flags_t complain
,
13300 if (TREE_CODE (t
) == SCOPE_REF
)
13301 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13302 /*done=*/false, /*address_p=*/false);
13304 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13305 /*function_p=*/false,
13306 /*integral_constant_expression_p=*/false);
13311 /* Like tsubst but deals with expressions and performs semantic
13312 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13315 tsubst_copy_and_build (tree t
,
13317 tsubst_flags_t complain
,
13320 bool integral_constant_expression_p
)
13322 #define RECUR(NODE) \
13323 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13324 /*function_p=*/false, \
13325 integral_constant_expression_p)
13329 if (t
== NULL_TREE
|| t
== error_mark_node
)
13332 switch (TREE_CODE (t
))
13336 /* Fall through. */
13337 case IDENTIFIER_NODE
:
13341 bool non_integral_constant_expression_p
;
13342 const char *error_msg
;
13344 if (IDENTIFIER_TYPENAME_P (t
))
13346 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13347 t
= mangle_conv_op_name_for_type (new_type
);
13350 /* Look up the name. */
13351 decl
= lookup_name (t
);
13353 /* By convention, expressions use ERROR_MARK_NODE to indicate
13354 failure, not NULL_TREE. */
13355 if (decl
== NULL_TREE
)
13356 decl
= error_mark_node
;
13358 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13360 integral_constant_expression_p
,
13361 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13362 &non_integral_constant_expression_p
,
13363 /*template_p=*/false,
13365 /*address_p=*/false,
13366 /*template_arg_p=*/false,
13371 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13373 if (complain
& tf_error
)
13374 unqualified_name_lookup_error (decl
);
13375 decl
= error_mark_node
;
13380 case TEMPLATE_ID_EXPR
:
13383 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13384 tree targs
= TREE_OPERAND (t
, 1);
13387 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13389 if (TREE_CODE (templ
) == COMPONENT_REF
)
13391 object
= TREE_OPERAND (templ
, 0);
13392 templ
= TREE_OPERAND (templ
, 1);
13395 object
= NULL_TREE
;
13396 templ
= lookup_template_function (templ
, targs
);
13399 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13400 object
, templ
, NULL_TREE
);
13402 return baselink_for_fns (templ
);
13407 tree r
= RECUR (TREE_OPERAND (t
, 0));
13409 if (REFERENCE_REF_P (t
))
13411 /* A type conversion to reference type will be enclosed in
13412 such an indirect ref, but the substitution of the cast
13413 will have also added such an indirect ref. */
13414 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13415 r
= convert_from_reference (r
);
13418 r
= build_x_indirect_ref (r
, RO_UNARY_STAR
, complain
);
13424 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13425 RECUR (TREE_OPERAND (t
, 0)));
13427 case IMPLICIT_CONV_EXPR
:
13429 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13430 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13431 int flags
= LOOKUP_IMPLICIT
;
13432 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13433 flags
= LOOKUP_NORMAL
;
13434 return perform_implicit_conversion_flags (type
, expr
, complain
,
13441 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13442 RECUR (TREE_OPERAND (t
, 0)));
13445 case REINTERPRET_CAST_EXPR
:
13446 case CONST_CAST_EXPR
:
13447 case DYNAMIC_CAST_EXPR
:
13448 case STATIC_CAST_EXPR
:
13451 tree op
, r
= NULL_TREE
;
13453 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13454 if (integral_constant_expression_p
13455 && !cast_valid_in_integral_constant_expression_p (type
))
13457 if (complain
& tf_error
)
13458 error ("a cast to a type other than an integral or "
13459 "enumeration type cannot appear in a constant-expression");
13460 return error_mark_node
;
13463 op
= RECUR (TREE_OPERAND (t
, 0));
13465 ++c_inhibit_evaluation_warnings
;
13466 switch (TREE_CODE (t
))
13469 r
= build_functional_cast (type
, op
, complain
);
13471 case REINTERPRET_CAST_EXPR
:
13472 r
= build_reinterpret_cast (type
, op
, complain
);
13474 case CONST_CAST_EXPR
:
13475 r
= build_const_cast (type
, op
, complain
);
13477 case DYNAMIC_CAST_EXPR
:
13478 r
= build_dynamic_cast (type
, op
, complain
);
13480 case STATIC_CAST_EXPR
:
13481 r
= build_static_cast (type
, op
, complain
);
13484 gcc_unreachable ();
13486 --c_inhibit_evaluation_warnings
;
13491 case POSTDECREMENT_EXPR
:
13492 case POSTINCREMENT_EXPR
:
13493 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13494 args
, complain
, in_decl
);
13495 return build_x_unary_op (TREE_CODE (t
), op1
, complain
);
13497 case PREDECREMENT_EXPR
:
13498 case PREINCREMENT_EXPR
:
13502 case TRUTH_NOT_EXPR
:
13503 case UNARY_PLUS_EXPR
: /* Unary + */
13504 case REALPART_EXPR
:
13505 case IMAGPART_EXPR
:
13506 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)),
13509 case FIX_TRUNC_EXPR
:
13510 return cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13514 op1
= TREE_OPERAND (t
, 0);
13515 if (TREE_CODE (op1
) == LABEL_DECL
)
13516 return finish_label_address_expr (DECL_NAME (op1
),
13517 EXPR_LOCATION (op1
));
13518 if (TREE_CODE (op1
) == SCOPE_REF
)
13519 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13520 /*done=*/true, /*address_p=*/true);
13522 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13524 return build_x_unary_op (ADDR_EXPR
, op1
, complain
);
13529 case TRUNC_DIV_EXPR
:
13530 case CEIL_DIV_EXPR
:
13531 case FLOOR_DIV_EXPR
:
13532 case ROUND_DIV_EXPR
:
13533 case EXACT_DIV_EXPR
:
13537 case TRUNC_MOD_EXPR
:
13538 case FLOOR_MOD_EXPR
:
13539 case TRUTH_ANDIF_EXPR
:
13540 case TRUTH_ORIF_EXPR
:
13541 case TRUTH_AND_EXPR
:
13542 case TRUTH_OR_EXPR
:
13558 tree r
= build_x_binary_op
13560 RECUR (TREE_OPERAND (t
, 0)),
13561 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13563 : TREE_CODE (TREE_OPERAND (t
, 0))),
13564 RECUR (TREE_OPERAND (t
, 1)),
13565 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13567 : TREE_CODE (TREE_OPERAND (t
, 1))),
13570 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13571 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13576 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13577 /*address_p=*/false);
13579 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13580 args
, complain
, in_decl
);
13581 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
13584 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13585 return tsubst_copy (t
, args
, complain
, in_decl
);
13589 op1
= TREE_OPERAND (t
, 0);
13592 /* When there are no ARGS, we are trying to evaluate a
13593 non-dependent expression from the parser. Trying to do
13594 the substitutions may not work. */
13596 op1
= TREE_TYPE (op1
);
13600 ++cp_unevaluated_operand
;
13601 ++c_inhibit_evaluation_warnings
;
13602 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13603 /*function_p=*/false,
13604 /*integral_constant_expression_p=*/false);
13605 --cp_unevaluated_operand
;
13606 --c_inhibit_evaluation_warnings
;
13609 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13610 complain
& tf_error
);
13612 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13613 complain
& tf_error
);
13615 case AT_ENCODE_EXPR
:
13617 op1
= TREE_OPERAND (t
, 0);
13618 ++cp_unevaluated_operand
;
13619 ++c_inhibit_evaluation_warnings
;
13620 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13621 /*function_p=*/false,
13622 /*integral_constant_expression_p=*/false);
13623 --cp_unevaluated_operand
;
13624 --c_inhibit_evaluation_warnings
;
13625 return objc_build_encode_expr (op1
);
13628 case NOEXCEPT_EXPR
:
13629 op1
= TREE_OPERAND (t
, 0);
13630 ++cp_unevaluated_operand
;
13631 ++c_inhibit_evaluation_warnings
;
13632 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13633 /*function_p=*/false,
13634 /*integral_constant_expression_p=*/false);
13635 --cp_unevaluated_operand
;
13636 --c_inhibit_evaluation_warnings
;
13637 return finish_noexcept_expr (op1
, complain
);
13641 tree r
= build_x_modify_expr
13642 (RECUR (TREE_OPERAND (t
, 0)),
13643 TREE_CODE (TREE_OPERAND (t
, 1)),
13644 RECUR (TREE_OPERAND (t
, 2)),
13646 /* TREE_NO_WARNING must be set if either the expression was
13647 parenthesized or it uses an operator such as >>= rather
13648 than plain assignment. In the former case, it was already
13649 set and must be copied. In the latter case,
13650 build_x_modify_expr sets it and it must not be reset
13652 if (TREE_NO_WARNING (t
))
13653 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13658 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13659 args
, complain
, in_decl
);
13660 /* Remember that there was a reference to this entity. */
13663 return build_x_arrow (op1
, complain
);
13667 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13668 tree init
= RECUR (TREE_OPERAND (t
, 3));
13669 VEC(tree
,gc
) *placement_vec
;
13670 VEC(tree
,gc
) *init_vec
;
13673 if (placement
== NULL_TREE
)
13674 placement_vec
= NULL
;
13677 placement_vec
= make_tree_vector ();
13678 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13679 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
13682 /* If there was an initializer in the original tree, but it
13683 instantiated to an empty list, then we should pass a
13684 non-NULL empty vector to tell build_new that it was an
13685 empty initializer() rather than no initializer. This can
13686 only happen when the initializer is a pack expansion whose
13687 parameter packs are of length zero. */
13688 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13692 init_vec
= make_tree_vector ();
13693 if (init
== void_zero_node
)
13694 gcc_assert (init_vec
!= NULL
);
13697 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13698 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
13702 ret
= build_new (&placement_vec
,
13703 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13704 RECUR (TREE_OPERAND (t
, 2)),
13706 NEW_EXPR_USE_GLOBAL (t
),
13709 if (placement_vec
!= NULL
)
13710 release_tree_vector (placement_vec
);
13711 if (init_vec
!= NULL
)
13712 release_tree_vector (init_vec
);
13718 return delete_sanity
13719 (RECUR (TREE_OPERAND (t
, 0)),
13720 RECUR (TREE_OPERAND (t
, 1)),
13721 DELETE_EXPR_USE_VEC (t
),
13722 DELETE_EXPR_USE_GLOBAL (t
),
13725 case COMPOUND_EXPR
:
13726 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
13727 RECUR (TREE_OPERAND (t
, 1)),
13733 VEC(tree
,gc
) *call_args
;
13734 unsigned int nargs
, i
;
13739 function
= CALL_EXPR_FN (t
);
13740 /* When we parsed the expression, we determined whether or
13741 not Koenig lookup should be performed. */
13742 koenig_p
= KOENIG_LOOKUP_P (t
);
13743 if (TREE_CODE (function
) == SCOPE_REF
)
13745 qualified_p
= true;
13746 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13748 /*address_p=*/false);
13750 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13752 /* Do nothing; calling tsubst_copy_and_build on an identifier
13753 would incorrectly perform unqualified lookup again.
13755 Note that we can also have an IDENTIFIER_NODE if the earlier
13756 unqualified lookup found a member function; in that case
13757 koenig_p will be false and we do want to do the lookup
13758 again to find the instantiated member function.
13760 FIXME but doing that causes c++/15272, so we need to stop
13761 using IDENTIFIER_NODE in that situation. */
13762 qualified_p
= false;
13766 if (TREE_CODE (function
) == COMPONENT_REF
)
13768 tree op
= TREE_OPERAND (function
, 1);
13770 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13771 || (BASELINK_P (op
)
13772 && BASELINK_QUALIFIED_P (op
)));
13775 qualified_p
= false;
13777 function
= tsubst_copy_and_build (function
, args
, complain
,
13780 integral_constant_expression_p
);
13782 if (BASELINK_P (function
))
13783 qualified_p
= true;
13786 nargs
= call_expr_nargs (t
);
13787 call_args
= make_tree_vector ();
13788 for (i
= 0; i
< nargs
; ++i
)
13790 tree arg
= CALL_EXPR_ARG (t
, i
);
13792 if (!PACK_EXPANSION_P (arg
))
13793 VEC_safe_push (tree
, gc
, call_args
,
13794 RECUR (CALL_EXPR_ARG (t
, i
)));
13797 /* Expand the pack expansion and push each entry onto
13799 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13800 if (TREE_CODE (arg
) == TREE_VEC
)
13802 unsigned int len
, j
;
13804 len
= TREE_VEC_LENGTH (arg
);
13805 for (j
= 0; j
< len
; ++j
)
13807 tree value
= TREE_VEC_ELT (arg
, j
);
13808 if (value
!= NULL_TREE
)
13809 value
= convert_from_reference (value
);
13810 VEC_safe_push (tree
, gc
, call_args
, value
);
13815 /* A partial substitution. Add one entry. */
13816 VEC_safe_push (tree
, gc
, call_args
, arg
);
13821 /* We do not perform argument-dependent lookup if normal
13822 lookup finds a non-function, in accordance with the
13823 expected resolution of DR 218. */
13825 && ((is_overloaded_fn (function
)
13826 /* If lookup found a member function, the Koenig lookup is
13827 not appropriate, even if an unqualified-name was used
13828 to denote the function. */
13829 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13830 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13831 /* Only do this when substitution turns a dependent call
13832 into a non-dependent call. */
13833 && type_dependent_expression_p_push (t
)
13834 && !any_type_dependent_arguments_p (call_args
))
13835 function
= perform_koenig_lookup (function
, call_args
, false,
13838 if (TREE_CODE (function
) == IDENTIFIER_NODE
13839 && !any_type_dependent_arguments_p (call_args
))
13841 if (koenig_p
&& (complain
& tf_warning_or_error
))
13843 /* For backwards compatibility and good diagnostics, try
13844 the unqualified lookup again if we aren't in SFINAE
13846 tree unq
= (tsubst_copy_and_build
13847 (function
, args
, complain
, in_decl
, true,
13848 integral_constant_expression_p
));
13849 if (unq
== error_mark_node
)
13850 return error_mark_node
;
13852 if (unq
!= function
)
13855 if (TREE_CODE (fn
) == INDIRECT_REF
)
13856 fn
= TREE_OPERAND (fn
, 0);
13857 if (TREE_CODE (fn
) == COMPONENT_REF
)
13858 fn
= TREE_OPERAND (fn
, 1);
13859 if (is_overloaded_fn (fn
))
13860 fn
= get_first_fn (fn
);
13861 permerror (EXPR_LOC_OR_HERE (t
),
13862 "%qD was not declared in this scope, "
13863 "and no declarations were found by "
13864 "argument-dependent lookup at the point "
13865 "of instantiation", function
);
13867 /* Can't say anything more. */;
13868 else if (DECL_CLASS_SCOPE_P (fn
))
13870 inform (EXPR_LOC_OR_HERE (t
),
13871 "declarations in dependent base %qT are "
13872 "not found by unqualified lookup",
13873 DECL_CLASS_CONTEXT (fn
));
13874 if (current_class_ptr
)
13875 inform (EXPR_LOC_OR_HERE (t
),
13876 "use %<this->%D%> instead", function
);
13878 inform (EXPR_LOC_OR_HERE (t
),
13879 "use %<%T::%D%> instead",
13880 current_class_name
, function
);
13883 inform (0, "%q+D declared here, later in the "
13884 "translation unit", fn
);
13888 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13890 unqualified_name_lookup_error (function
);
13891 release_tree_vector (call_args
);
13892 return error_mark_node
;
13896 /* Remember that there was a reference to this entity. */
13897 if (DECL_P (function
))
13898 mark_used (function
);
13900 if (TREE_CODE (function
) == OFFSET_REF
)
13901 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
13902 else if (TREE_CODE (function
) == COMPONENT_REF
)
13904 tree instance
= TREE_OPERAND (function
, 0);
13905 tree fn
= TREE_OPERAND (function
, 1);
13907 if (processing_template_decl
13908 && (type_dependent_expression_p (instance
)
13909 || (!BASELINK_P (fn
)
13910 && TREE_CODE (fn
) != FIELD_DECL
)
13911 || type_dependent_expression_p (fn
)
13912 || any_type_dependent_arguments_p (call_args
)))
13913 ret
= build_nt_call_vec (function
, call_args
);
13914 else if (!BASELINK_P (fn
))
13915 ret
= finish_call_expr (function
, &call_args
,
13916 /*disallow_virtual=*/false,
13917 /*koenig_p=*/false,
13920 ret
= (build_new_method_call
13922 &call_args
, NULL_TREE
,
13923 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
13928 ret
= finish_call_expr (function
, &call_args
,
13929 /*disallow_virtual=*/qualified_p
,
13933 release_tree_vector (call_args
);
13940 tree cond
= RECUR (TREE_OPERAND (t
, 0));
13943 if (TREE_CODE (cond
) == INTEGER_CST
)
13945 if (integer_zerop (cond
))
13947 ++c_inhibit_evaluation_warnings
;
13948 exp1
= RECUR (TREE_OPERAND (t
, 1));
13949 --c_inhibit_evaluation_warnings
;
13950 exp2
= RECUR (TREE_OPERAND (t
, 2));
13954 exp1
= RECUR (TREE_OPERAND (t
, 1));
13955 ++c_inhibit_evaluation_warnings
;
13956 exp2
= RECUR (TREE_OPERAND (t
, 2));
13957 --c_inhibit_evaluation_warnings
;
13962 exp1
= RECUR (TREE_OPERAND (t
, 1));
13963 exp2
= RECUR (TREE_OPERAND (t
, 2));
13966 return build_x_conditional_expr (cond
, exp1
, exp2
, complain
);
13969 case PSEUDO_DTOR_EXPR
:
13970 return finish_pseudo_destructor_expr
13971 (RECUR (TREE_OPERAND (t
, 0)),
13972 RECUR (TREE_OPERAND (t
, 1)),
13973 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
13977 tree purpose
, value
, chain
;
13979 if (t
== void_list_node
)
13982 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
13983 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
13985 /* We have pack expansions, so expand those and
13986 create a new list out of it. */
13987 tree purposevec
= NULL_TREE
;
13988 tree valuevec
= NULL_TREE
;
13992 /* Expand the argument expressions. */
13993 if (TREE_PURPOSE (t
))
13994 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
13995 complain
, in_decl
);
13996 if (TREE_VALUE (t
))
13997 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
13998 complain
, in_decl
);
14000 /* Build the rest of the list. */
14001 chain
= TREE_CHAIN (t
);
14002 if (chain
&& chain
!= void_type_node
)
14003 chain
= RECUR (chain
);
14005 /* Determine the number of arguments. */
14006 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14008 len
= TREE_VEC_LENGTH (purposevec
);
14009 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14011 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14012 len
= TREE_VEC_LENGTH (valuevec
);
14015 /* Since we only performed a partial substitution into
14016 the argument pack, we only return a single list
14018 if (purposevec
== TREE_PURPOSE (t
)
14019 && valuevec
== TREE_VALUE (t
)
14020 && chain
== TREE_CHAIN (t
))
14023 return tree_cons (purposevec
, valuevec
, chain
);
14026 /* Convert the argument vectors into a TREE_LIST */
14030 /* Grab the Ith values. */
14032 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14035 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14038 /* Build the list (backwards). */
14039 chain
= tree_cons (purpose
, value
, chain
);
14045 purpose
= TREE_PURPOSE (t
);
14047 purpose
= RECUR (purpose
);
14048 value
= TREE_VALUE (t
);
14050 value
= RECUR (value
);
14051 chain
= TREE_CHAIN (t
);
14052 if (chain
&& chain
!= void_type_node
)
14053 chain
= RECUR (chain
);
14054 if (purpose
== TREE_PURPOSE (t
)
14055 && value
== TREE_VALUE (t
)
14056 && chain
== TREE_CHAIN (t
))
14058 return tree_cons (purpose
, value
, chain
);
14061 case COMPONENT_REF
:
14067 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14068 args
, complain
, in_decl
);
14069 /* Remember that there was a reference to this entity. */
14070 if (DECL_P (object
))
14071 mark_used (object
);
14072 object_type
= TREE_TYPE (object
);
14074 member
= TREE_OPERAND (t
, 1);
14075 if (BASELINK_P (member
))
14076 member
= tsubst_baselink (member
,
14077 non_reference (TREE_TYPE (object
)),
14078 args
, complain
, in_decl
);
14080 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14081 if (member
== error_mark_node
)
14082 return error_mark_node
;
14084 if (type_dependent_expression_p (object
))
14085 /* We can't do much here. */;
14086 else if (!CLASS_TYPE_P (object_type
))
14088 if (SCALAR_TYPE_P (object_type
))
14090 tree s
= NULL_TREE
;
14091 tree dtor
= member
;
14093 if (TREE_CODE (dtor
) == SCOPE_REF
)
14095 s
= TREE_OPERAND (dtor
, 0);
14096 dtor
= TREE_OPERAND (dtor
, 1);
14098 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14100 dtor
= TREE_OPERAND (dtor
, 0);
14102 return finish_pseudo_destructor_expr (object
, s
, dtor
);
14106 else if (TREE_CODE (member
) == SCOPE_REF
14107 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14109 /* Lookup the template functions now that we know what the
14111 tree scope
= TREE_OPERAND (member
, 0);
14112 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14113 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14114 member
= lookup_qualified_name (scope
, tmpl
,
14115 /*is_type_p=*/false,
14116 /*complain=*/false);
14117 if (BASELINK_P (member
))
14119 BASELINK_FUNCTIONS (member
)
14120 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14122 member
= (adjust_result_of_qualified_name_lookup
14123 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14128 qualified_name_lookup_error (scope
, tmpl
, member
,
14130 return error_mark_node
;
14133 else if (TREE_CODE (member
) == SCOPE_REF
14134 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14135 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14137 if (complain
& tf_error
)
14139 if (TYPE_P (TREE_OPERAND (member
, 0)))
14140 error ("%qT is not a class or namespace",
14141 TREE_OPERAND (member
, 0));
14143 error ("%qD is not a class or namespace",
14144 TREE_OPERAND (member
, 0));
14146 return error_mark_node
;
14148 else if (TREE_CODE (member
) == FIELD_DECL
)
14149 return finish_non_static_data_member (member
, object
, NULL_TREE
);
14151 return finish_class_member_access_expr (object
, member
,
14152 /*template_p=*/false,
14158 (RECUR (TREE_OPERAND (t
, 0)));
14162 VEC(constructor_elt
,gc
) *n
;
14163 constructor_elt
*ce
;
14164 unsigned HOST_WIDE_INT idx
;
14165 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14166 bool process_index_p
;
14168 bool need_copy_p
= false;
14171 if (type
== error_mark_node
)
14172 return error_mark_node
;
14174 /* digest_init will do the wrong thing if we let it. */
14175 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14178 /* We do not want to process the index of aggregate
14179 initializers as they are identifier nodes which will be
14180 looked up by digest_init. */
14181 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14183 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
14184 newlen
= VEC_length (constructor_elt
, n
);
14185 FOR_EACH_VEC_ELT (constructor_elt
, n
, idx
, ce
)
14187 if (ce
->index
&& process_index_p
)
14188 ce
->index
= RECUR (ce
->index
);
14190 if (PACK_EXPANSION_P (ce
->value
))
14192 /* Substitute into the pack expansion. */
14193 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14196 if (ce
->value
== error_mark_node
14197 || PACK_EXPANSION_P (ce
->value
))
14199 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14200 /* Just move the argument into place. */
14201 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14204 /* Update the length of the final CONSTRUCTOR
14205 arguments vector, and note that we will need to
14207 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14208 need_copy_p
= true;
14212 ce
->value
= RECUR (ce
->value
);
14217 VEC(constructor_elt
,gc
) *old_n
= n
;
14219 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
14220 FOR_EACH_VEC_ELT (constructor_elt
, old_n
, idx
, ce
)
14222 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14224 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14225 for (i
= 0; i
< len
; ++i
)
14226 CONSTRUCTOR_APPEND_ELT (n
, 0,
14227 TREE_VEC_ELT (ce
->value
, i
));
14230 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14234 r
= build_constructor (init_list_type_node
, n
);
14235 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14237 if (TREE_HAS_CONSTRUCTOR (t
))
14238 return finish_compound_literal (type
, r
, complain
);
14240 TREE_TYPE (r
) = type
;
14246 tree operand_0
= TREE_OPERAND (t
, 0);
14247 if (TYPE_P (operand_0
))
14249 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14250 return get_typeid (operand_0
);
14254 operand_0
= RECUR (operand_0
);
14255 return build_typeid (operand_0
);
14266 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14268 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14269 /* If the original type was a reference, we'll be wrapped in
14270 the appropriate INDIRECT_REF. */
14271 r
= convert_from_reference (r
);
14276 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
14277 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
14279 case OFFSETOF_EXPR
:
14280 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
14284 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14285 complain
, in_decl
);
14287 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14289 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14291 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
14296 tree old_stmt_expr
= cur_stmt_expr
;
14297 tree stmt_expr
= begin_stmt_expr ();
14299 cur_stmt_expr
= stmt_expr
;
14300 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14301 integral_constant_expression_p
);
14302 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14303 cur_stmt_expr
= old_stmt_expr
;
14305 /* If the resulting list of expression statement is empty,
14306 fold it further into void_zero_node. */
14307 if (empty_expr_stmt_p (stmt_expr
))
14308 stmt_expr
= void_zero_node
;
14314 t
= tsubst_copy (t
, args
, complain
, in_decl
);
14315 /* As in finish_id_expression, we resolve enumeration constants
14316 to their underlying values. */
14317 if (TREE_CODE (t
) == CONST_DECL
&& !processing_template_decl
)
14319 used_types_insert (TREE_TYPE (t
));
14320 return DECL_INITIAL (t
);
14326 tree r
= build_lambda_expr ();
14328 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14329 LAMBDA_EXPR_CLOSURE (r
) = type
;
14330 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14332 LAMBDA_EXPR_LOCATION (r
)
14333 = LAMBDA_EXPR_LOCATION (t
);
14334 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14335 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14336 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14337 LAMBDA_EXPR_DISCRIMINATOR (r
)
14338 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14339 LAMBDA_EXPR_EXTRA_SCOPE (r
)
14340 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t
));
14341 LAMBDA_EXPR_RETURN_TYPE (r
)
14342 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14344 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14345 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14347 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14348 determine_visibility (TYPE_NAME (type
));
14349 /* Now that we know visibility, instantiate the type so we have a
14350 declaration of the op() for later calls to lambda_function. */
14351 complete_type (type
);
14353 /* The capture list refers to closure members, so this needs to
14354 wait until after we finish instantiating the type. */
14355 LAMBDA_EXPR_CAPTURE_LIST (r
)
14356 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
14358 return build_lambda_object (r
);
14362 /* We can get here for a constant initializer of non-dependent type.
14363 FIXME stop folding in cp_parser_initializer_clause. */
14364 gcc_assert (TREE_CONSTANT (t
));
14366 tree r
= get_target_expr (RECUR (TARGET_EXPR_INITIAL (t
)));
14367 TREE_CONSTANT (r
) = true;
14371 case TRANSACTION_EXPR
:
14372 return tsubst_expr(t
, args
, complain
, in_decl
,
14373 integral_constant_expression_p
);
14376 /* Handle Objective-C++ constructs, if appropriate. */
14379 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14380 in_decl
, /*function_p=*/false);
14384 return tsubst_copy (t
, args
, complain
, in_decl
);
14390 /* Verify that the instantiated ARGS are valid. For type arguments,
14391 make sure that the type's linkage is ok. For non-type arguments,
14392 make sure they are constants if they are integral or enumerations.
14393 Emit an error under control of COMPLAIN, and return TRUE on error. */
14396 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14398 if (ARGUMENT_PACK_P (t
))
14400 tree vec
= ARGUMENT_PACK_ARGS (t
);
14401 int len
= TREE_VEC_LENGTH (vec
);
14402 bool result
= false;
14405 for (i
= 0; i
< len
; ++i
)
14406 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14410 else if (TYPE_P (t
))
14412 /* [basic.link]: A name with no linkage (notably, the name
14413 of a class or enumeration declared in a local scope)
14414 shall not be used to declare an entity with linkage.
14415 This implies that names with no linkage cannot be used as
14418 DR 757 relaxes this restriction for C++0x. */
14419 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14420 : no_linkage_check (t
, /*relaxed_p=*/false));
14424 /* DR 488 makes use of a type with no linkage cause
14425 type deduction to fail. */
14426 if (complain
& tf_error
)
14428 if (TYPE_ANONYMOUS_P (nt
))
14429 error ("%qT is/uses anonymous type", t
);
14431 error ("template argument for %qD uses local type %qT",
14436 /* In order to avoid all sorts of complications, we do not
14437 allow variably-modified types as template arguments. */
14438 else if (variably_modified_type_p (t
, NULL_TREE
))
14440 if (complain
& tf_error
)
14441 error ("%qT is a variably modified type", t
);
14445 /* A non-type argument of integral or enumerated type must be a
14447 else if (TREE_TYPE (t
)
14448 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14449 && !TREE_CONSTANT (t
))
14451 if (complain
& tf_error
)
14452 error ("integral expression %qE is not constant", t
);
14459 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14461 int ix
, len
= DECL_NTPARMS (tmpl
);
14462 bool result
= false;
14464 for (ix
= 0; ix
!= len
; ix
++)
14466 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14469 if (result
&& (complain
& tf_error
))
14470 error (" trying to instantiate %qD", tmpl
);
14474 /* In C++0x, it's possible to have a function template whose type depends
14475 on itself recursively. This is most obvious with decltype, but can also
14476 occur with enumeration scope (c++/48969). So we need to catch infinite
14477 recursion and reject the substitution at deduction time; this function
14478 will return error_mark_node for any repeated substitution.
14480 This also catches excessive recursion such as when f<N> depends on
14481 f<N-1> across all integers, and returns error_mark_node for all the
14482 substitutions back up to the initial one.
14484 This is, of course, not reentrant. */
14487 deduction_tsubst_fntype (tree fn
, tree targs
, tsubst_flags_t complain
)
14489 static bool excessive_deduction_depth
;
14490 static int deduction_depth
;
14491 struct pending_template
*old_last_pend
= last_pending_template
;
14492 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14494 tree fntype
= TREE_TYPE (fn
);
14498 if (excessive_deduction_depth
)
14499 return error_mark_node
;
14501 tinst
= build_tree_list (fn
, targs
);
14502 if (!push_tinst_level (tinst
))
14504 excessive_deduction_depth
= true;
14506 return error_mark_node
;
14509 input_location
= DECL_SOURCE_LOCATION (fn
);
14511 push_deduction_access_scope (fn
);
14512 r
= tsubst (fntype
, targs
, complain
, NULL_TREE
);
14513 pop_deduction_access_scope (fn
);
14516 if (excessive_deduction_depth
)
14518 r
= error_mark_node
;
14519 if (deduction_depth
== 0)
14520 /* Reset once we're all the way out. */
14521 excessive_deduction_depth
= false;
14524 pop_tinst_level ();
14525 /* We can't free this if a pending_template entry or last_error_tinst_level
14526 is pointing at it. */
14527 if (last_pending_template
== old_last_pend
14528 && last_error_tinst_level
== old_error_tinst
)
14533 /* Instantiate the indicated variable or function template TMPL with
14534 the template arguments in TARG_PTR. */
14537 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14539 tree targ_ptr
= orig_args
;
14544 if (tmpl
== error_mark_node
)
14545 return error_mark_node
;
14547 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14549 /* If this function is a clone, handle it specially. */
14550 if (DECL_CLONED_FUNCTION_P (tmpl
))
14555 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14556 DECL_CLONED_FUNCTION. */
14557 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14558 targ_ptr
, complain
);
14559 if (spec
== error_mark_node
)
14560 return error_mark_node
;
14562 /* Look for the clone. */
14563 FOR_EACH_CLONE (clone
, spec
)
14564 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14566 /* We should always have found the clone by now. */
14567 gcc_unreachable ();
14571 /* Check to see if we already have this specialization. */
14572 gen_tmpl
= most_general_template (tmpl
);
14573 if (tmpl
!= gen_tmpl
)
14574 /* The TMPL is a partial instantiation. To get a full set of
14575 arguments we must add the arguments used to perform the
14576 partial instantiation. */
14577 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14580 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14581 but it doesn't seem to be on the hot path. */
14582 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14584 gcc_assert (tmpl
== gen_tmpl
14585 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14587 || fndecl
== NULL_TREE
);
14589 if (spec
!= NULL_TREE
)
14592 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14594 return error_mark_node
;
14596 /* We are building a FUNCTION_DECL, during which the access of its
14597 parameters and return types have to be checked. However this
14598 FUNCTION_DECL which is the desired context for access checking
14599 is not built yet. We solve this chicken-and-egg problem by
14600 deferring all checks until we have the FUNCTION_DECL. */
14601 push_deferring_access_checks (dk_deferred
);
14603 /* Instantiation of the function happens in the context of the function
14604 template, not the context of the overload resolution we're doing. */
14605 push_to_top_level ();
14606 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14608 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14609 complain
, gen_tmpl
);
14610 push_nested_class (ctx
);
14612 /* Substitute template parameters to obtain the specialization. */
14613 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14614 targ_ptr
, complain
, gen_tmpl
);
14615 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14616 pop_nested_class ();
14617 pop_from_top_level ();
14619 if (fndecl
== error_mark_node
)
14620 return error_mark_node
;
14622 /* Now we know the specialization, compute access previously
14624 push_access_scope (fndecl
);
14626 /* Some typedefs referenced from within the template code need to be access
14627 checked at template instantiation time, i.e now. These types were
14628 added to the template at parsing time. Let's get those and perfom
14629 the acces checks then. */
14630 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl
), targ_ptr
);
14631 perform_deferred_access_checks ();
14632 pop_access_scope (fndecl
);
14633 pop_deferring_access_checks ();
14635 /* The DECL_TI_TEMPLATE should always be the immediate parent
14636 template, not the most general template. */
14637 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14639 /* If we've just instantiated the main entry point for a function,
14640 instantiate all the alternate entry points as well. We do this
14641 by cloning the instantiation of the main entry point, not by
14642 instantiating the template clones. */
14643 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14644 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14649 /* Wrapper for instantiate_template_1. */
14652 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14655 timevar_push (TV_TEMPLATE_INST
);
14656 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14657 timevar_pop (TV_TEMPLATE_INST
);
14661 /* We're going to do deduction substitution on the type of TMPL, a function
14662 template. In C++11 mode, push into that access scope. In C++03 mode,
14663 disable access checking. */
14666 push_deduction_access_scope (tree tmpl
)
14668 if (cxx_dialect
>= cxx0x
)
14670 int ptd
= processing_template_decl
;
14671 push_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
14672 /* Preserve processing_template_decl across push_to_top_level. */
14673 if (ptd
&& !processing_template_decl
)
14674 ++processing_template_decl
;
14677 push_deferring_access_checks (dk_no_check
);
14680 /* And pop back out. */
14683 pop_deduction_access_scope (tree tmpl
)
14685 if (cxx_dialect
>= cxx0x
)
14686 pop_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
14688 pop_deferring_access_checks ();
14691 /* PARM is a template parameter pack for FN. Returns true iff
14692 PARM is used in a deducible way in the argument list of FN. */
14695 pack_deducible_p (tree parm
, tree fn
)
14697 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14698 for (; t
; t
= TREE_CHAIN (t
))
14700 tree type
= TREE_VALUE (t
);
14702 if (!PACK_EXPANSION_P (type
))
14704 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14705 packs
; packs
= TREE_CHAIN (packs
))
14706 if (TREE_VALUE (packs
) == parm
)
14708 /* The template parameter pack is used in a function parameter
14709 pack. If this is the end of the parameter list, the
14710 template parameter pack is deducible. */
14711 if (TREE_CHAIN (t
) == void_list_node
)
14714 /* Otherwise, not. Well, it could be deduced from
14715 a non-pack parameter, but doing so would end up with
14716 a deduction mismatch, so don't bother. */
14720 /* The template parameter pack isn't used in any function parameter
14721 packs, but it might be used deeper, e.g. tuple<Args...>. */
14725 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14726 NARGS elements of the arguments that are being used when calling
14727 it. TARGS is a vector into which the deduced template arguments
14730 Return zero for success, 2 for an incomplete match that doesn't resolve
14731 all the types, and 1 for complete failure. An error message will be
14732 printed only for an incomplete match.
14734 If FN is a conversion operator, or we are trying to produce a specific
14735 specialization, RETURN_TYPE is the return type desired.
14737 The EXPLICIT_TARGS are explicit template arguments provided via a
14740 The parameter STRICT is one of:
14743 We are deducing arguments for a function call, as in
14744 [temp.deduct.call].
14747 We are deducing arguments for a conversion function, as in
14748 [temp.deduct.conv].
14751 We are deducing arguments when doing an explicit instantiation
14752 as in [temp.explicit], when determining an explicit specialization
14753 as in [temp.expl.spec], or when taking the address of a function
14754 template, as in [temp.deduct.funcaddr]. */
14757 fn_type_unification (tree fn
,
14758 tree explicit_targs
,
14761 unsigned int nargs
,
14763 unification_kind_t strict
,
14771 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14773 fntype
= TREE_TYPE (fn
);
14774 if (explicit_targs
)
14778 The specified template arguments must match the template
14779 parameters in kind (i.e., type, nontype, template), and there
14780 must not be more arguments than there are parameters;
14781 otherwise type deduction fails.
14783 Nontype arguments must match the types of the corresponding
14784 nontype template parameters, or must be convertible to the
14785 types of the corresponding nontype parameters as specified in
14786 _temp.arg.nontype_, otherwise type deduction fails.
14788 All references in the function type of the function template
14789 to the corresponding template parameters are replaced by the
14790 specified template argument values. If a substitution in a
14791 template parameter or in the function type of the function
14792 template results in an invalid type, type deduction fails. */
14793 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14794 int i
, len
= TREE_VEC_LENGTH (tparms
);
14795 tree converted_args
;
14796 bool incomplete
= false;
14798 if (explicit_targs
== error_mark_node
)
14799 return unify_invalid (explain_p
);
14802 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14804 ? tf_warning_or_error
14806 /*require_all_args=*/false,
14807 /*use_default_args=*/false));
14808 if (converted_args
== error_mark_node
)
14811 /* Substitute the explicit args into the function type. This is
14812 necessary so that, for instance, explicitly declared function
14813 arguments can match null pointed constants. If we were given
14814 an incomplete set of explicit args, we must not do semantic
14815 processing during substitution as we could create partial
14817 for (i
= 0; i
< len
; i
++)
14819 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14820 bool parameter_pack
= false;
14821 tree targ
= TREE_VEC_ELT (converted_args
, i
);
14823 /* Dig out the actual parm. */
14824 if (TREE_CODE (parm
) == TYPE_DECL
14825 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14827 parm
= TREE_TYPE (parm
);
14828 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14830 else if (TREE_CODE (parm
) == PARM_DECL
)
14832 parm
= DECL_INITIAL (parm
);
14833 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14836 if (!parameter_pack
&& targ
== NULL_TREE
)
14837 /* No explicit argument for this template parameter. */
14840 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
14842 /* Mark the argument pack as "incomplete". We could
14843 still deduce more arguments during unification.
14844 We remove this mark in type_unification_real. */
14847 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
14848 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
14849 = ARGUMENT_PACK_ARGS (targ
);
14852 /* We have some incomplete argument packs. */
14857 processing_template_decl
+= incomplete
;
14858 fntype
= deduction_tsubst_fntype (fn
, converted_args
,
14860 ? tf_warning_or_error
14861 : tf_none
) | tf_partial
);
14862 processing_template_decl
-= incomplete
;
14864 if (fntype
== error_mark_node
)
14867 /* Place the explicitly specified arguments in TARGS. */
14868 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
14869 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
14872 /* Never do unification on the 'this' parameter. */
14873 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
14879 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
14880 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
14881 new_args
[0] = return_type
;
14882 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
14887 /* We allow incomplete unification without an error message here
14888 because the standard doesn't seem to explicitly prohibit it. Our
14889 callers must be ready to deal with unification failures in any
14891 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
14892 targs
, parms
, args
, nargs
, /*subr=*/0,
14893 strict
, flags
, explain_p
);
14895 /* Now that we have bindings for all of the template arguments,
14896 ensure that the arguments deduced for the template template
14897 parameters have compatible template parameter lists. We cannot
14898 check this property before we have deduced all template
14899 arguments, because the template parameter types of a template
14900 template parameter might depend on prior template parameters
14901 deduced after the template template parameter. The following
14902 ill-formed example illustrates this issue:
14904 template<typename T, template<T> class C> void f(C<5>, T);
14906 template<int N> struct X {};
14909 f(X<5>(), 5l); // error: template argument deduction fails
14912 The template parameter list of 'C' depends on the template type
14913 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14914 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14915 time that we deduce 'C'. */
14917 && !template_template_parm_bindings_ok_p
14918 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
14919 return unify_inconsistent_template_template_parameters (explain_p
);
14922 /* All is well so far. Now, check:
14926 When all template arguments have been deduced, all uses of
14927 template parameters in nondeduced contexts are replaced with
14928 the corresponding deduced argument values. If the
14929 substitution results in an invalid type, as described above,
14930 type deduction fails. */
14932 tree substed
= deduction_tsubst_fntype (fn
, targs
,
14934 ? tf_warning_or_error
14936 if (substed
== error_mark_node
)
14939 /* If we're looking for an exact match, check that what we got
14940 is indeed an exact match. It might not be if some template
14941 parameters are used in non-deduced contexts. */
14942 if (strict
== DEDUCE_EXACT
)
14947 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
14949 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
14950 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
14951 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
14952 return unify_type_mismatch (explain_p
, args
[i
],
14953 TREE_VALUE (sarg
));
14960 /* Adjust types before performing type deduction, as described in
14961 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14962 sections are symmetric. PARM is the type of a function parameter
14963 or the return type of the conversion function. ARG is the type of
14964 the argument passed to the call, or the type of the value
14965 initialized with the result of the conversion function.
14966 ARG_EXPR is the original argument expression, which may be null. */
14969 maybe_adjust_types_for_deduction (unification_kind_t strict
,
14983 /* Swap PARM and ARG throughout the remainder of this
14984 function; the handling is precisely symmetric since PARM
14985 will initialize ARG rather than vice versa. */
14993 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14994 too, but here handle it by stripping the reference from PARM
14995 rather than by adding it to ARG. */
14996 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14997 && TYPE_REF_IS_RVALUE (*parm
)
14998 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14999 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15000 && TREE_CODE (*arg
) == REFERENCE_TYPE
15001 && !TYPE_REF_IS_RVALUE (*arg
))
15002 *parm
= TREE_TYPE (*parm
);
15003 /* Nothing else to do in this case. */
15007 gcc_unreachable ();
15010 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15012 /* [temp.deduct.call]
15014 If P is not a reference type:
15016 --If A is an array type, the pointer type produced by the
15017 array-to-pointer standard conversion (_conv.array_) is
15018 used in place of A for type deduction; otherwise,
15020 --If A is a function type, the pointer type produced by
15021 the function-to-pointer standard conversion
15022 (_conv.func_) is used in place of A for type deduction;
15025 --If A is a cv-qualified type, the top level
15026 cv-qualifiers of A's type are ignored for type
15028 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15029 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15030 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15031 *arg
= build_pointer_type (*arg
);
15033 *arg
= TYPE_MAIN_VARIANT (*arg
);
15036 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15037 of the form T&&, where T is a template parameter, and the argument
15038 is an lvalue, T is deduced as A& */
15039 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15040 && TYPE_REF_IS_RVALUE (*parm
)
15041 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15042 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15043 && (arg_expr
? real_lvalue_p (arg_expr
)
15044 /* try_one_overload doesn't provide an arg_expr, but
15045 functions are always lvalues. */
15046 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15047 *arg
= build_reference_type (*arg
);
15049 /* [temp.deduct.call]
15051 If P is a cv-qualified type, the top level cv-qualifiers
15052 of P's type are ignored for type deduction. If P is a
15053 reference type, the type referred to by P is used for
15055 *parm
= TYPE_MAIN_VARIANT (*parm
);
15056 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15058 *parm
= TREE_TYPE (*parm
);
15059 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15062 /* DR 322. For conversion deduction, remove a reference type on parm
15063 too (which has been swapped into ARG). */
15064 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15065 *arg
= TREE_TYPE (*arg
);
15070 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15071 template which does contain any deducible template parameters; check if
15072 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15073 unify_one_argument. */
15076 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15077 int flags
, bool explain_p
)
15082 type
= TREE_TYPE (arg
);
15086 if (same_type_p (parm
, type
))
15087 return unify_success (explain_p
);
15089 if (strict
== DEDUCE_CONV
)
15091 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
))
15092 return unify_success (explain_p
);
15094 else if (strict
!= DEDUCE_EXACT
)
15096 if (can_convert_arg (parm
, type
,
15097 TYPE_P (arg
) ? NULL_TREE
: arg
,
15099 return unify_success (explain_p
);
15102 if (strict
== DEDUCE_EXACT
)
15103 return unify_type_mismatch (explain_p
, parm
, arg
);
15105 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15108 /* Subroutine of type_unification_real and unify_pack_expansion to
15109 handle unification of a single P/A pair. Parameters are as
15110 for those functions. */
15113 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15114 int subr
, unification_kind_t strict
, int flags
,
15117 tree arg_expr
= NULL_TREE
;
15120 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15121 return unify_invalid (explain_p
);
15122 if (arg
== unknown_type_node
)
15123 /* We can't deduce anything from this, but we might get all the
15124 template args from other function args. */
15125 return unify_success (explain_p
);
15127 /* FIXME uses_deducible_template_parms */
15128 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15129 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15135 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15136 | UNIFY_ALLOW_MORE_CV_QUAL
15137 | UNIFY_ALLOW_DERIVED
);
15141 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15145 arg_strict
= UNIFY_ALLOW_NONE
;
15149 gcc_unreachable ();
15152 /* We only do these transformations if this is the top-level
15153 parameter_type_list in a call or declaration matching; in other
15154 situations (nested function declarators, template argument lists) we
15155 won't be comparing a type to an expression, and we don't do any type
15161 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15162 if (type_unknown_p (arg
))
15164 /* [temp.deduct.type] A template-argument can be
15165 deduced from a pointer to function or pointer
15166 to member function argument if the set of
15167 overloaded functions does not contain function
15168 templates and at most one of a set of
15169 overloaded functions provides a unique
15172 if (resolve_overloaded_unification
15173 (tparms
, targs
, parm
, arg
, strict
,
15174 arg_strict
, explain_p
))
15175 return unify_success (explain_p
);
15176 return unify_overload_resolution_failure (explain_p
, arg
);
15180 arg
= unlowered_expr_type (arg
);
15181 if (arg
== error_mark_node
)
15182 return unify_invalid (explain_p
);
15186 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15189 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15190 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15192 /* For deduction from an init-list we need the actual list. */
15193 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15195 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15198 /* Most parms like fn_type_unification.
15200 If SUBR is 1, we're being called recursively (to unify the
15201 arguments of a function or method parameter of a function
15205 type_unification_real (tree tparms
,
15209 unsigned int xnargs
,
15211 unification_kind_t strict
,
15217 int ntparms
= TREE_VEC_LENGTH (tparms
);
15218 int saw_undeduced
= 0;
15221 unsigned int nargs
;
15224 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15225 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15226 gcc_assert (ntparms
> 0);
15228 /* Reset the number of non-defaulted template arguments contained
15230 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15238 while (parms
&& parms
!= void_list_node
15241 parm
= TREE_VALUE (parms
);
15243 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15244 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15245 /* For a function parameter pack that occurs at the end of the
15246 parameter-declaration-list, the type A of each remaining
15247 argument of the call is compared with the type P of the
15248 declarator-id of the function parameter pack. */
15251 parms
= TREE_CHAIN (parms
);
15253 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15254 /* For a function parameter pack that does not occur at the
15255 end of the parameter-declaration-list, the type of the
15256 parameter pack is a non-deduced context. */
15262 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15268 && parms
!= void_list_node
15269 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15271 /* Unify the remaining arguments with the pack expansion type. */
15273 tree parmvec
= make_tree_vec (1);
15275 /* Allocate a TREE_VEC and copy in all of the arguments */
15276 argvec
= make_tree_vec (nargs
- ia
);
15277 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15278 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15280 /* Copy the parameter into parmvec. */
15281 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15282 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15283 /*subr=*/subr
, explain_p
))
15286 /* Advance to the end of the list of parameters. */
15287 parms
= TREE_CHAIN (parms
);
15290 /* Fail if we've reached the end of the parm list, and more args
15291 are present, and the parm list isn't variadic. */
15292 if (ia
< nargs
&& parms
== void_list_node
)
15293 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15294 /* Fail if parms are left and they don't have default values. */
15295 if (parms
&& parms
!= void_list_node
15296 && TREE_PURPOSE (parms
) == NULL_TREE
)
15298 unsigned int count
= nargs
;
15300 while (p
&& p
!= void_list_node
)
15303 p
= TREE_CHAIN (p
);
15305 return unify_too_few_arguments (explain_p
, ia
, count
);
15310 tsubst_flags_t complain
= (explain_p
15311 ? tf_warning_or_error
15314 /* Check to see if we need another pass before we start clearing
15315 ARGUMENT_PACK_INCOMPLETE_P. */
15316 for (i
= 0; i
< ntparms
; i
++)
15318 tree targ
= TREE_VEC_ELT (targs
, i
);
15319 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15321 if (targ
|| tparm
== error_mark_node
)
15323 tparm
= TREE_VALUE (tparm
);
15325 /* If this is an undeduced nontype parameter that depends on
15326 a type parameter, try another pass; its type may have been
15327 deduced from a later argument than the one from which
15328 this parameter can be deduced. */
15329 if (TREE_CODE (tparm
) == PARM_DECL
15330 && uses_template_parms (TREE_TYPE (tparm
))
15331 && !saw_undeduced
++)
15335 for (i
= 0; i
< ntparms
; i
++)
15337 tree targ
= TREE_VEC_ELT (targs
, i
);
15338 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15340 /* Clear the "incomplete" flags on all argument packs now so that
15341 substituting them into later default arguments works. */
15342 if (targ
&& ARGUMENT_PACK_P (targ
))
15344 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15345 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15348 if (targ
|| tparm
== error_mark_node
)
15350 tparm
= TREE_VALUE (tparm
);
15352 /* Core issue #226 (C++0x) [temp.deduct]:
15354 If a template argument has not been deduced, its
15355 default template argument, if any, is used.
15357 When we are in C++98 mode, TREE_PURPOSE will either
15358 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15359 to explicitly check cxx_dialect here. */
15360 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15362 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15363 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15364 location_t save_loc
= input_location
;
15366 input_location
= DECL_SOURCE_LOCATION (parm
);
15367 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15368 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15370 input_location
= save_loc
;
15371 if (arg
== error_mark_node
)
15375 TREE_VEC_ELT (targs
, i
) = arg
;
15376 /* The position of the first default template argument,
15377 is also the number of non-defaulted arguments in TARGS.
15379 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15380 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15385 /* If the type parameter is a parameter pack, then it will
15386 be deduced to an empty parameter pack. */
15387 if (template_parameter_pack_p (tparm
))
15391 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15393 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15394 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15395 TREE_CONSTANT (arg
) = 1;
15398 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15400 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15402 TREE_VEC_ELT (targs
, i
) = arg
;
15406 return unify_parameter_deduction_failure (explain_p
, tparm
);
15409 #ifdef ENABLE_CHECKING
15410 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15411 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15414 return unify_success (explain_p
);
15417 /* Subroutine of type_unification_real. Args are like the variables
15418 at the call site. ARG is an overloaded function (or template-id);
15419 we try deducing template args from each of the overloads, and if
15420 only one succeeds, we go with that. Modifies TARGS and returns
15421 true on success. */
15424 resolve_overloaded_unification (tree tparms
,
15428 unification_kind_t strict
,
15432 tree tempargs
= copy_node (targs
);
15434 tree goodfn
= NULL_TREE
;
15437 if (TREE_CODE (arg
) == ADDR_EXPR
)
15439 arg
= TREE_OPERAND (arg
, 0);
15445 if (TREE_CODE (arg
) == COMPONENT_REF
)
15446 /* Handle `&x' where `x' is some static or non-static member
15448 arg
= TREE_OPERAND (arg
, 1);
15450 if (TREE_CODE (arg
) == OFFSET_REF
)
15451 arg
= TREE_OPERAND (arg
, 1);
15453 /* Strip baselink information. */
15454 if (BASELINK_P (arg
))
15455 arg
= BASELINK_FUNCTIONS (arg
);
15457 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15459 /* If we got some explicit template args, we need to plug them into
15460 the affected templates before we try to unify, in case the
15461 explicit args will completely resolve the templates in question. */
15464 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15465 arg
= TREE_OPERAND (arg
, 0);
15467 for (; arg
; arg
= OVL_NEXT (arg
))
15469 tree fn
= OVL_CURRENT (arg
);
15470 tree subargs
, elem
;
15472 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15475 ++processing_template_decl
;
15476 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
15477 expl_subargs
, /*check_ret=*/false);
15478 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
15480 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15481 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15482 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15483 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15491 --processing_template_decl
;
15493 /* If no templates (or more than one) are fully resolved by the
15494 explicit arguments, this template-id is a non-deduced context; it
15495 could still be OK if we deduce all template arguments for the
15496 enclosing call through other arguments. */
15500 else if (TREE_CODE (arg
) != OVERLOAD
15501 && TREE_CODE (arg
) != FUNCTION_DECL
)
15502 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15503 -- but the deduction does not succeed because the expression is
15504 not just the function on its own. */
15507 for (; arg
; arg
= OVL_NEXT (arg
))
15508 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15509 TREE_TYPE (OVL_CURRENT (arg
)),
15510 strict
, sub_strict
, addr_p
, explain_p
)
15511 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15513 goodfn
= OVL_CURRENT (arg
);
15517 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15518 to function or pointer to member function argument if the set of
15519 overloaded functions does not contain function templates and at most
15520 one of a set of overloaded functions provides a unique match.
15522 So if we found multiple possibilities, we return success but don't
15523 deduce anything. */
15527 int i
= TREE_VEC_LENGTH (targs
);
15529 if (TREE_VEC_ELT (tempargs
, i
))
15530 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15538 /* Core DR 115: In contexts where deduction is done and fails, or in
15539 contexts where deduction is not done, if a template argument list is
15540 specified and it, along with any default template arguments, identifies
15541 a single function template specialization, then the template-id is an
15542 lvalue for the function template specialization. */
15545 resolve_nondeduced_context (tree orig_expr
)
15547 tree expr
, offset
, baselink
;
15550 if (!type_unknown_p (orig_expr
))
15555 offset
= NULL_TREE
;
15556 baselink
= NULL_TREE
;
15558 if (TREE_CODE (expr
) == ADDR_EXPR
)
15560 expr
= TREE_OPERAND (expr
, 0);
15563 if (TREE_CODE (expr
) == OFFSET_REF
)
15566 expr
= TREE_OPERAND (expr
, 1);
15568 if (BASELINK_P (expr
))
15571 expr
= BASELINK_FUNCTIONS (expr
);
15574 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15577 tree goodfn
= NULL_TREE
;
15579 /* If we got some explicit template args, we need to plug them into
15580 the affected templates before we try to unify, in case the
15581 explicit args will completely resolve the templates in question. */
15583 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15584 tree arg
= TREE_OPERAND (expr
, 0);
15585 tree badfn
= NULL_TREE
;
15586 tree badargs
= NULL_TREE
;
15588 for (; arg
; arg
= OVL_NEXT (arg
))
15590 tree fn
= OVL_CURRENT (arg
);
15591 tree subargs
, elem
;
15593 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15596 ++processing_template_decl
;
15597 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
15598 expl_subargs
, /*check_ret=*/false);
15599 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
15601 elem
= instantiate_template (fn
, subargs
, tf_none
);
15602 if (elem
== error_mark_node
)
15607 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15613 --processing_template_decl
;
15617 mark_used (goodfn
);
15620 expr
= build_baselink (BASELINK_BINFO (baselink
),
15621 BASELINK_ACCESS_BINFO (baselink
),
15622 expr
, BASELINK_OPTYPE (baselink
));
15626 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15627 expr
= build_offset_ref (base
, expr
, addr
);
15630 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15633 else if (good
== 0 && badargs
)
15634 /* There were no good options and at least one bad one, so let the
15635 user know what the problem is. */
15636 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15641 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15642 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15643 different overloads deduce different arguments for a given parm.
15644 ADDR_P is true if the expression for which deduction is being
15645 performed was of the form "& fn" rather than simply "fn".
15647 Returns 1 on success. */
15650 try_one_overload (tree tparms
,
15655 unification_kind_t strict
,
15664 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15665 to function or pointer to member function argument if the set of
15666 overloaded functions does not contain function templates and at most
15667 one of a set of overloaded functions provides a unique match.
15669 So if this is a template, just return success. */
15671 if (uses_template_parms (arg
))
15674 if (TREE_CODE (arg
) == METHOD_TYPE
)
15675 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15677 arg
= build_pointer_type (arg
);
15679 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15681 /* We don't copy orig_targs for this because if we have already deduced
15682 some template args from previous args, unify would complain when we
15683 try to deduce a template parameter for the same argument, even though
15684 there isn't really a conflict. */
15685 nargs
= TREE_VEC_LENGTH (targs
);
15686 tempargs
= make_tree_vec (nargs
);
15688 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15691 /* First make sure we didn't deduce anything that conflicts with
15692 explicitly specified args. */
15693 for (i
= nargs
; i
--; )
15695 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15696 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15700 else if (uses_template_parms (elt
))
15701 /* Since we're unifying against ourselves, we will fill in
15702 template args used in the function parm list with our own
15703 template parms. Discard them. */
15704 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15705 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15709 for (i
= nargs
; i
--; )
15711 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15714 TREE_VEC_ELT (targs
, i
) = elt
;
15720 /* PARM is a template class (perhaps with unbound template
15721 parameters). ARG is a fully instantiated type. If ARG can be
15722 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15723 TARGS are as for unify. */
15726 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15729 tree copy_of_targs
;
15731 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15732 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15733 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15736 /* We need to make a new template argument vector for the call to
15737 unify. If we used TARGS, we'd clutter it up with the result of
15738 the attempted unification, even if this class didn't work out.
15739 We also don't want to commit ourselves to all the unifications
15740 we've already done, since unification is supposed to be done on
15741 an argument-by-argument basis. In other words, consider the
15742 following pathological case:
15744 template <int I, int J, int K>
15747 template <int I, int J>
15748 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15750 template <int I, int J, int K>
15751 void f(S<I, J, K>, S<I, I, I>);
15760 Now, by the time we consider the unification involving `s2', we
15761 already know that we must have `f<0, 0, 0>'. But, even though
15762 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15763 because there are two ways to unify base classes of S<0, 1, 2>
15764 with S<I, I, I>. If we kept the already deduced knowledge, we
15765 would reject the possibility I=1. */
15766 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15768 /* If unification failed, we're done. */
15769 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15770 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15776 /* Given a template type PARM and a class type ARG, find the unique
15777 base type in ARG that is an instance of PARM. We do not examine
15778 ARG itself; only its base-classes. If there is not exactly one
15779 appropriate base class, return NULL_TREE. PARM may be the type of
15780 a partial specialization, as well as a plain template type. Used
15783 static enum template_base_result
15784 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15785 bool explain_p
, tree
*result
)
15787 tree rval
= NULL_TREE
;
15790 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15792 binfo
= TYPE_BINFO (complete_type (arg
));
15795 /* The type could not be completed. */
15796 *result
= NULL_TREE
;
15797 return tbr_incomplete_type
;
15800 /* Walk in inheritance graph order. The search order is not
15801 important, and this avoids multiple walks of virtual bases. */
15802 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15804 tree r
= try_class_unification (tparms
, targs
, parm
,
15805 BINFO_TYPE (binfo
), explain_p
);
15809 /* If there is more than one satisfactory baseclass, then:
15813 If they yield more than one possible deduced A, the type
15817 if (rval
&& !same_type_p (r
, rval
))
15819 *result
= NULL_TREE
;
15820 return tbr_ambiguous_baseclass
;
15828 return tbr_success
;
15831 /* Returns the level of DECL, which declares a template parameter. */
15834 template_decl_level (tree decl
)
15836 switch (TREE_CODE (decl
))
15839 case TEMPLATE_DECL
:
15840 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
15843 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
15846 gcc_unreachable ();
15851 /* Decide whether ARG can be unified with PARM, considering only the
15852 cv-qualifiers of each type, given STRICT as documented for unify.
15853 Returns nonzero iff the unification is OK on that basis. */
15856 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
15858 int arg_quals
= cp_type_quals (arg
);
15859 int parm_quals
= cp_type_quals (parm
);
15861 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15862 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15864 /* Although a CVR qualifier is ignored when being applied to a
15865 substituted template parameter ([8.3.2]/1 for example), that
15866 does not allow us to unify "const T" with "int&" because both
15867 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15868 It is ok when we're allowing additional CV qualifiers
15869 at the outer level [14.8.2.1]/3,1st bullet. */
15870 if ((TREE_CODE (arg
) == REFERENCE_TYPE
15871 || TREE_CODE (arg
) == FUNCTION_TYPE
15872 || TREE_CODE (arg
) == METHOD_TYPE
)
15873 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
15876 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
15877 && (parm_quals
& TYPE_QUAL_RESTRICT
))
15881 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15882 && (arg_quals
& parm_quals
) != parm_quals
)
15885 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
15886 && (parm_quals
& arg_quals
) != arg_quals
)
15892 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15894 template_parm_level_and_index (tree parm
, int* level
, int* index
)
15896 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15897 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15898 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15900 *index
= TEMPLATE_TYPE_IDX (parm
);
15901 *level
= TEMPLATE_TYPE_LEVEL (parm
);
15905 *index
= TEMPLATE_PARM_IDX (parm
);
15906 *level
= TEMPLATE_PARM_LEVEL (parm
);
15910 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15912 if (unify (TP, TA, P, A, S, EP)) \
15916 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15917 expansion at the end of PACKED_PARMS. Returns 0 if the type
15918 deduction succeeds, 1 otherwise. STRICT is the same as in
15919 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15920 call argument list. We'll need to adjust the arguments to make them
15921 types. SUBR tells us if this is from a recursive call to
15922 type_unification_real, or for comparing two template argument
15926 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
15927 tree packed_args
, unification_kind_t strict
,
15928 bool subr
, bool explain_p
)
15931 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
15932 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
15933 tree pack
, packs
= NULL_TREE
;
15934 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
15935 int len
= TREE_VEC_LENGTH (packed_args
);
15937 /* Determine the parameter packs we will be deducing from the
15938 pattern, and record their current deductions. */
15939 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
15940 pack
; pack
= TREE_CHAIN (pack
))
15942 tree parm_pack
= TREE_VALUE (pack
);
15945 /* Determine the index and level of this parameter pack. */
15946 template_parm_level_and_index (parm_pack
, &level
, &idx
);
15948 /* Keep track of the parameter packs and their corresponding
15950 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
15951 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
15954 /* Loop through all of the arguments that have not yet been
15955 unified and unify each with the pattern. */
15956 for (i
= start
; i
< len
; i
++)
15959 bool any_explicit
= false;
15960 tree arg
= TREE_VEC_ELT (packed_args
, i
);
15962 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
15963 or the element of its argument pack at the current index if
15964 this argument was explicitly specified. */
15965 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15969 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15972 if (TREE_VALUE (pack
)
15973 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
15974 && (i
< TREE_VEC_LENGTH (pargs
)))
15976 any_explicit
= true;
15977 arg
= TREE_VEC_ELT (pargs
, i
);
15979 TMPL_ARG (targs
, level
, idx
) = arg
;
15982 /* If we had explicit template arguments, substitute them into the
15983 pattern before deduction. */
15986 /* Some arguments might still be unspecified or dependent. */
15988 ++processing_template_decl
;
15989 dependent
= any_dependent_template_arguments_p (targs
);
15991 --processing_template_decl
;
15992 parm
= tsubst (pattern
, targs
,
15993 explain_p
? tf_warning_or_error
: tf_none
,
15996 --processing_template_decl
;
15997 if (parm
== error_mark_node
)
16003 /* Unify the pattern with the current argument. */
16004 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16005 LOOKUP_IMPLICIT
, explain_p
))
16008 /* For each parameter pack, collect the deduced value. */
16009 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16012 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16014 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16015 TMPL_ARG (targs
, level
, idx
);
16019 /* Verify that the results of unification with the parameter packs
16020 produce results consistent with what we've seen before, and make
16021 the deduced argument packs available. */
16022 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16024 tree old_pack
= TREE_VALUE (pack
);
16025 tree new_args
= TREE_TYPE (pack
);
16026 int i
, len
= TREE_VEC_LENGTH (new_args
);
16028 bool nondeduced_p
= false;
16030 /* By default keep the original deduced argument pack.
16031 If necessary, more specific code is going to update the
16032 resulting deduced argument later down in this function. */
16033 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16034 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16036 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16037 actually deduce anything. */
16038 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16039 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16040 nondeduced_p
= true;
16044 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16046 /* If we had fewer function args than explicit template args,
16047 just use the explicits. */
16048 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16049 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16050 if (len
< explicit_len
)
16051 new_args
= explicit_args
;
16057 /* Build the deduced *_ARGUMENT_PACK. */
16058 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16060 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16061 TREE_TYPE (result
) =
16062 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16063 TREE_CONSTANT (result
) = 1;
16066 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16068 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16070 /* Note the deduced argument packs for this parameter
16072 TMPL_ARG (targs
, level
, idx
) = result
;
16074 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16075 && (ARGUMENT_PACK_ARGS (old_pack
)
16076 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16078 /* We only had the explicitly-provided arguments before, but
16079 now we have a complete set of arguments. */
16080 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16082 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16083 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16084 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16088 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16089 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16091 if (!comp_template_args_with_info (old_args
, new_args
,
16092 &bad_old_arg
, &bad_new_arg
))
16093 /* Inconsistent unification of this parameter pack. */
16094 return unify_parameter_pack_inconsistent (explain_p
,
16100 return unify_success (explain_p
);
16103 /* Deduce the value of template parameters. TPARMS is the (innermost)
16104 set of template parameters to a template. TARGS is the bindings
16105 for those template parameters, as determined thus far; TARGS may
16106 include template arguments for outer levels of template parameters
16107 as well. PARM is a parameter to a template function, or a
16108 subcomponent of that parameter; ARG is the corresponding argument.
16109 This function attempts to match PARM with ARG in a manner
16110 consistent with the existing assignments in TARGS. If more values
16111 are deduced, then TARGS is updated.
16113 Returns 0 if the type deduction succeeds, 1 otherwise. The
16114 parameter STRICT is a bitwise or of the following flags:
16117 Require an exact match between PARM and ARG.
16118 UNIFY_ALLOW_MORE_CV_QUAL:
16119 Allow the deduced ARG to be more cv-qualified (by qualification
16120 conversion) than ARG.
16121 UNIFY_ALLOW_LESS_CV_QUAL:
16122 Allow the deduced ARG to be less cv-qualified than ARG.
16123 UNIFY_ALLOW_DERIVED:
16124 Allow the deduced ARG to be a template base class of ARG,
16125 or a pointer to a template base class of the type pointed to by
16127 UNIFY_ALLOW_INTEGER:
16128 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16129 case for more information.
16130 UNIFY_ALLOW_OUTER_LEVEL:
16131 This is the outermost level of a deduction. Used to determine validity
16132 of qualification conversions. A valid qualification conversion must
16133 have const qualified pointers leading up to the inner type which
16134 requires additional CV quals, except at the outer level, where const
16135 is not required [conv.qual]. It would be normal to set this flag in
16136 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16137 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16138 This is the outermost level of a deduction, and PARM can be more CV
16139 qualified at this point.
16140 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16141 This is the outermost level of a deduction, and PARM can be less CV
16142 qualified at this point. */
16145 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16151 int strict_in
= strict
;
16153 /* I don't think this will do the right thing with respect to types.
16154 But the only case I've seen it in so far has been array bounds, where
16155 signedness is the only information lost, and I think that will be
16157 while (TREE_CODE (parm
) == NOP_EXPR
)
16158 parm
= TREE_OPERAND (parm
, 0);
16160 if (arg
== error_mark_node
)
16161 return unify_invalid (explain_p
);
16162 if (arg
== unknown_type_node
16163 || arg
== init_list_type_node
)
16164 /* We can't deduce anything from this, but we might get all the
16165 template args from other function args. */
16166 return unify_success (explain_p
);
16168 /* If PARM uses template parameters, then we can't bail out here,
16169 even if ARG == PARM, since we won't record unifications for the
16170 template parameters. We might need them if we're trying to
16171 figure out which of two things is more specialized. */
16172 if (arg
== parm
&& !uses_template_parms (parm
))
16173 return unify_success (explain_p
);
16175 /* Handle init lists early, so the rest of the function can assume
16176 we're dealing with a type. */
16177 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16181 tree orig_parm
= parm
;
16183 /* Replace T with std::initializer_list<T> for deduction. */
16184 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16185 && flag_deduce_init_list
)
16186 parm
= listify (parm
);
16188 if (!is_std_init_list (parm
))
16189 /* We can only deduce from an initializer list argument if the
16190 parameter is std::initializer_list; otherwise this is a
16191 non-deduced context. */
16192 return unify_success (explain_p
);
16194 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16196 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16198 int elt_strict
= strict
;
16200 if (elt
== error_mark_node
)
16201 return unify_invalid (explain_p
);
16203 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16205 tree type
= TREE_TYPE (elt
);
16206 /* It should only be possible to get here for a call. */
16207 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16208 elt_strict
|= maybe_adjust_types_for_deduction
16209 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16213 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16217 /* If the std::initializer_list<T> deduction worked, replace the
16218 deduced A with std::initializer_list<A>. */
16219 if (orig_parm
!= parm
)
16221 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16222 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16223 targ
= listify (targ
);
16224 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16226 return unify_success (explain_p
);
16229 /* Immediately reject some pairs that won't unify because of
16230 cv-qualification mismatches. */
16231 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16233 /* It is the elements of the array which hold the cv quals of an array
16234 type, and the elements might be template type parms. We'll check
16235 when we recurse. */
16236 && TREE_CODE (arg
) != ARRAY_TYPE
16237 /* We check the cv-qualifiers when unifying with template type
16238 parameters below. We want to allow ARG `const T' to unify with
16239 PARM `T' for example, when computing which of two templates
16240 is more specialized, for example. */
16241 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16242 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16243 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16245 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16246 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16247 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16248 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16249 strict
&= ~UNIFY_ALLOW_DERIVED
;
16250 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16251 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16253 switch (TREE_CODE (parm
))
16255 case TYPENAME_TYPE
:
16257 case UNBOUND_CLASS_TEMPLATE
:
16258 /* In a type which contains a nested-name-specifier, template
16259 argument values cannot be deduced for template parameters used
16260 within the nested-name-specifier. */
16261 return unify_success (explain_p
);
16263 case TEMPLATE_TYPE_PARM
:
16264 case TEMPLATE_TEMPLATE_PARM
:
16265 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16266 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16267 if (tparm
== error_mark_node
)
16268 return unify_invalid (explain_p
);
16270 if (TEMPLATE_TYPE_LEVEL (parm
)
16271 != template_decl_level (tparm
))
16272 /* The PARM is not one we're trying to unify. Just check
16273 to see if it matches ARG. */
16275 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16276 && (is_auto (parm
) ? is_auto (arg
)
16277 : same_type_p (parm
, arg
)))
16278 return unify_success (explain_p
);
16280 return unify_type_mismatch (explain_p
, parm
, arg
);
16282 idx
= TEMPLATE_TYPE_IDX (parm
);
16283 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16284 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16285 if (tparm
== error_mark_node
)
16286 return unify_invalid (explain_p
);
16288 /* Check for mixed types and values. */
16289 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16290 && TREE_CODE (tparm
) != TYPE_DECL
)
16291 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16292 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16293 gcc_unreachable ();
16295 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16297 /* ARG must be constructed from a template class or a template
16298 template parameter. */
16299 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16300 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16301 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16304 tree parmvec
= TYPE_TI_ARGS (parm
);
16305 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16306 tree full_argvec
= add_to_template_args (targs
, argvec
);
16308 = DECL_INNERMOST_TEMPLATE_PARMS
16309 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16311 int parm_variadic_p
= 0;
16313 /* The resolution to DR150 makes clear that default
16314 arguments for an N-argument may not be used to bind T
16315 to a template template parameter with fewer than N
16316 parameters. It is not safe to permit the binding of
16317 default arguments as an extension, as that may change
16318 the meaning of a conforming program. Consider:
16320 struct Dense { static const unsigned int dim = 1; };
16322 template <template <typename> class View,
16324 void operator+(float, View<Block> const&);
16326 template <typename Block,
16327 unsigned int Dim = Block::dim>
16328 struct Lvalue_proxy { operator float() const; };
16332 Lvalue_proxy<Dense> p;
16337 Here, if Lvalue_proxy is permitted to bind to View, then
16338 the global operator+ will be used; if they are not, the
16339 Lvalue_proxy will be converted to float. */
16340 if (coerce_template_parms (parm_parms
,
16342 TYPE_TI_TEMPLATE (parm
),
16344 ? tf_warning_or_error
16346 /*require_all_args=*/true,
16347 /*use_default_args=*/false)
16348 == error_mark_node
)
16351 /* Deduce arguments T, i from TT<T> or TT<i>.
16352 We check each element of PARMVEC and ARGVEC individually
16353 rather than the whole TREE_VEC since they can have
16354 different number of elements. */
16356 parmvec
= expand_template_argument_pack (parmvec
);
16357 argvec
= expand_template_argument_pack (argvec
);
16359 len
= TREE_VEC_LENGTH (parmvec
);
16361 /* Check if the parameters end in a pack, making them
16364 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16365 parm_variadic_p
= 1;
16367 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16368 return unify_too_few_arguments (explain_p
,
16369 TREE_VEC_LENGTH (argvec
), len
);
16371 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16373 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16374 TREE_VEC_ELT (parmvec
, i
),
16375 TREE_VEC_ELT (argvec
, i
),
16376 UNIFY_ALLOW_NONE
, explain_p
);
16379 if (parm_variadic_p
16380 && unify_pack_expansion (tparms
, targs
,
16383 /*subr=*/true, explain_p
))
16386 arg
= TYPE_TI_TEMPLATE (arg
);
16388 /* Fall through to deduce template name. */
16391 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16392 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16394 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16396 /* Simple cases: Value already set, does match or doesn't. */
16397 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16398 return unify_success (explain_p
);
16400 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16404 /* If PARM is `const T' and ARG is only `int', we don't have
16405 a match unless we are allowing additional qualification.
16406 If ARG is `const int' and PARM is just `T' that's OK;
16407 that binds `const int' to `T'. */
16408 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16410 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16412 /* Consider the case where ARG is `const volatile int' and
16413 PARM is `const T'. Then, T should be `volatile int'. */
16414 arg
= cp_build_qualified_type_real
16415 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16416 if (arg
== error_mark_node
)
16417 return unify_invalid (explain_p
);
16419 /* Simple cases: Value already set, does match or doesn't. */
16420 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16421 return unify_success (explain_p
);
16423 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16425 /* Make sure that ARG is not a variable-sized array. (Note
16426 that were talking about variable-sized arrays (like
16427 `int[n]'), rather than arrays of unknown size (like
16428 `int[]').) We'll get very confused by such a type since
16429 the bound of the array is not constant, and therefore
16430 not mangleable. Besides, such types are not allowed in
16431 ISO C++, so we can do as we please here. We do allow
16432 them for 'auto' deduction, since that isn't ABI-exposed. */
16433 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16434 return unify_vla_arg (explain_p
, arg
);
16436 /* Strip typedefs as in convert_template_argument. */
16437 arg
= canonicalize_type_argument (arg
, tf_none
);
16440 /* If ARG is a parameter pack or an expansion, we cannot unify
16441 against it unless PARM is also a parameter pack. */
16442 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16443 && !template_parameter_pack_p (parm
))
16444 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16446 /* If the argument deduction results is a METHOD_TYPE,
16447 then there is a problem.
16448 METHOD_TYPE doesn't map to any real C++ type the result of
16449 the deduction can not be of that type. */
16450 if (TREE_CODE (arg
) == METHOD_TYPE
)
16451 return unify_method_type_error (explain_p
, arg
);
16453 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16454 return unify_success (explain_p
);
16456 case TEMPLATE_PARM_INDEX
:
16457 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16458 if (tparm
== error_mark_node
)
16459 return unify_invalid (explain_p
);
16461 if (TEMPLATE_PARM_LEVEL (parm
)
16462 != template_decl_level (tparm
))
16464 /* The PARM is not one we're trying to unify. Just check
16465 to see if it matches ARG. */
16466 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16467 && cp_tree_equal (parm
, arg
));
16469 unify_expression_unequal (explain_p
, parm
, arg
);
16473 idx
= TEMPLATE_PARM_IDX (parm
);
16474 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16478 int x
= !cp_tree_equal (targ
, arg
);
16480 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16484 /* [temp.deduct.type] If, in the declaration of a function template
16485 with a non-type template-parameter, the non-type
16486 template-parameter is used in an expression in the function
16487 parameter-list and, if the corresponding template-argument is
16488 deduced, the template-argument type shall match the type of the
16489 template-parameter exactly, except that a template-argument
16490 deduced from an array bound may be of any integral type.
16491 The non-type parameter might use already deduced type parameters. */
16492 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16493 if (!TREE_TYPE (arg
))
16494 /* Template-parameter dependent expression. Just accept it for now.
16495 It will later be processed in convert_template_argument. */
16497 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16499 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16500 && (TREE_CODE (tparm
) == INTEGER_TYPE
16501 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16502 /* Convert the ARG to the type of PARM; the deduced non-type
16503 template argument must exactly match the types of the
16504 corresponding parameter. */
16505 arg
= fold (build_nop (tparm
, arg
));
16506 else if (uses_template_parms (tparm
))
16507 /* We haven't deduced the type of this parameter yet. Try again
16509 return unify_success (explain_p
);
16511 return unify_type_mismatch (explain_p
, tparm
, arg
);
16513 /* If ARG is a parameter pack or an expansion, we cannot unify
16514 against it unless PARM is also a parameter pack. */
16515 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16516 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16517 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16519 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16520 return unify_success (explain_p
);
16524 /* A pointer-to-member constant can be unified only with
16525 another constant. */
16526 if (TREE_CODE (arg
) != PTRMEM_CST
)
16527 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16529 /* Just unify the class member. It would be useless (and possibly
16530 wrong, depending on the strict flags) to unify also
16531 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16532 arg refer to the same variable, even if through different
16533 classes. For instance:
16535 struct A { int x; };
16538 Unification of &A::x and &B::x must succeed. */
16539 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16540 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16545 if (TREE_CODE (arg
) != POINTER_TYPE
)
16546 return unify_type_mismatch (explain_p
, parm
, arg
);
16548 /* [temp.deduct.call]
16550 A can be another pointer or pointer to member type that can
16551 be converted to the deduced A via a qualification
16552 conversion (_conv.qual_).
16554 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16555 This will allow for additional cv-qualification of the
16556 pointed-to types if appropriate. */
16558 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16559 /* The derived-to-base conversion only persists through one
16560 level of pointers. */
16561 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16563 return unify (tparms
, targs
, TREE_TYPE (parm
),
16564 TREE_TYPE (arg
), strict
, explain_p
);
16567 case REFERENCE_TYPE
:
16568 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16569 return unify_type_mismatch (explain_p
, parm
, arg
);
16570 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16571 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16574 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16575 return unify_type_mismatch (explain_p
, parm
, arg
);
16576 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16577 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16578 return unify_type_mismatch (explain_p
, parm
, arg
);
16579 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16586 /* Our representation of array types uses "N - 1" as the
16587 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16588 not an integer constant. We cannot unify arbitrarily
16589 complex expressions, so we eliminate the MINUS_EXPRs
16591 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16592 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16595 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16596 parm_max
= TREE_OPERAND (parm_max
, 0);
16598 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16599 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16602 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16603 trying to unify the type of a variable with the type
16604 of a template parameter. For example:
16606 template <unsigned int N>
16607 void f (char (&) [N]);
16614 Here, the type of the ARG will be "int [g(i)]", and
16615 may be a SAVE_EXPR, etc. */
16616 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16617 return unify_vla_arg (explain_p
, arg
);
16618 arg_max
= TREE_OPERAND (arg_max
, 0);
16621 /* If only one of the bounds used a MINUS_EXPR, compensate
16622 by adding one to the other bound. */
16623 if (parm_cst
&& !arg_cst
)
16624 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16628 else if (arg_cst
&& !parm_cst
)
16629 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16634 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16635 UNIFY_ALLOW_INTEGER
, explain_p
);
16637 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16638 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16645 case ENUMERAL_TYPE
:
16648 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16649 return unify_type_mismatch (explain_p
, parm
, arg
);
16651 /* We have already checked cv-qualification at the top of the
16653 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16654 return unify_type_mismatch (explain_p
, parm
, arg
);
16656 /* As far as unification is concerned, this wins. Later checks
16657 will invalidate it if necessary. */
16658 return unify_success (explain_p
);
16660 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16661 /* Type INTEGER_CST can come from ordinary constant template args. */
16663 while (TREE_CODE (arg
) == NOP_EXPR
)
16664 arg
= TREE_OPERAND (arg
, 0);
16666 if (TREE_CODE (arg
) != INTEGER_CST
)
16667 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16668 return (tree_int_cst_equal (parm
, arg
)
16669 ? unify_success (explain_p
)
16670 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16674 int i
, len
, argslen
;
16675 int parm_variadic_p
= 0;
16677 if (TREE_CODE (arg
) != TREE_VEC
)
16678 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16680 len
= TREE_VEC_LENGTH (parm
);
16681 argslen
= TREE_VEC_LENGTH (arg
);
16683 /* Check for pack expansions in the parameters. */
16684 for (i
= 0; i
< len
; ++i
)
16686 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16689 /* We can unify against something with a trailing
16691 parm_variadic_p
= 1;
16693 /* [temp.deduct.type]/9: If the template argument list of
16694 P contains a pack expansion that is not the last
16695 template argument, the entire template argument list
16696 is a non-deduced context. */
16697 return unify_success (explain_p
);
16701 /* If we don't have enough arguments to satisfy the parameters
16702 (not counting the pack expression at the end), or we have
16703 too many arguments for a parameter list that doesn't end in
16704 a pack expression, we can't unify. */
16705 if (parm_variadic_p
16706 ? argslen
< len
- parm_variadic_p
16708 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16710 /* Unify all of the parameters that precede the (optional)
16711 pack expression. */
16712 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16714 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16715 TREE_VEC_ELT (parm
, i
),
16716 TREE_VEC_ELT (arg
, i
),
16717 UNIFY_ALLOW_NONE
, explain_p
);
16719 if (parm_variadic_p
)
16720 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16722 /*subr=*/true, explain_p
);
16723 return unify_success (explain_p
);
16728 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16729 return unify_type_mismatch (explain_p
, parm
, arg
);
16731 if (TYPE_PTRMEMFUNC_P (parm
))
16733 if (!TYPE_PTRMEMFUNC_P (arg
))
16734 return unify_type_mismatch (explain_p
, parm
, arg
);
16736 return unify (tparms
, targs
,
16737 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16738 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16739 strict
, explain_p
);
16742 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16744 tree t
= NULL_TREE
;
16746 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16748 /* First, we try to unify the PARM and ARG directly. */
16749 t
= try_class_unification (tparms
, targs
,
16750 parm
, arg
, explain_p
);
16754 /* Fallback to the special case allowed in
16755 [temp.deduct.call]:
16757 If P is a class, and P has the form
16758 template-id, then A can be a derived class of
16759 the deduced A. Likewise, if P is a pointer to
16760 a class of the form template-id, A can be a
16761 pointer to a derived class pointed to by the
16763 enum template_base_result r
;
16764 r
= get_template_base (tparms
, targs
, parm
, arg
,
16768 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16771 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16772 && (CLASSTYPE_TI_TEMPLATE (parm
)
16773 == CLASSTYPE_TI_TEMPLATE (arg
)))
16774 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16775 Then, we should unify `int' and `U'. */
16778 /* There's no chance of unification succeeding. */
16779 return unify_type_mismatch (explain_p
, parm
, arg
);
16781 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16782 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16784 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16785 return unify_type_mismatch (explain_p
, parm
, arg
);
16786 return unify_success (explain_p
);
16789 case FUNCTION_TYPE
:
16791 unsigned int nargs
;
16796 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16797 return unify_type_mismatch (explain_p
, parm
, arg
);
16799 /* CV qualifications for methods can never be deduced, they must
16800 match exactly. We need to check them explicitly here,
16801 because type_unification_real treats them as any other
16802 cv-qualified parameter. */
16803 if (TREE_CODE (parm
) == METHOD_TYPE
16804 && (!check_cv_quals_for_unify
16806 class_of_this_parm (arg
),
16807 class_of_this_parm (parm
))))
16808 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16810 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
16811 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
16813 nargs
= list_length (TYPE_ARG_TYPES (arg
));
16814 args
= XALLOCAVEC (tree
, nargs
);
16815 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
16816 a
!= NULL_TREE
&& a
!= void_list_node
;
16817 a
= TREE_CHAIN (a
), ++i
)
16818 args
[i
] = TREE_VALUE (a
);
16821 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
16822 args
, nargs
, 1, DEDUCE_EXACT
,
16823 LOOKUP_NORMAL
, explain_p
);
16827 /* Unify a pointer to member with a pointer to member function, which
16828 deduces the type of the member as a function type. */
16829 if (TYPE_PTRMEMFUNC_P (arg
))
16834 /* Check top-level cv qualifiers */
16835 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
16836 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16838 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16839 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
16840 UNIFY_ALLOW_NONE
, explain_p
);
16842 /* Determine the type of the function we are unifying against. */
16843 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
16845 build_function_type (TREE_TYPE (method_type
),
16846 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
16848 /* Extract the cv-qualifiers of the member function from the
16849 implicit object parameter and place them on the function
16850 type to be restored later. */
16851 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
16852 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
16855 if (TREE_CODE (arg
) != OFFSET_TYPE
)
16856 return unify_type_mismatch (explain_p
, parm
, arg
);
16857 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16858 TYPE_OFFSET_BASETYPE (arg
),
16859 UNIFY_ALLOW_NONE
, explain_p
);
16860 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16861 strict
, explain_p
);
16864 if (DECL_TEMPLATE_PARM_P (parm
))
16865 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
16866 if (arg
!= integral_constant_value (parm
))
16867 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16868 return unify_success (explain_p
);
16871 case TEMPLATE_DECL
:
16872 /* Matched cases are handled by the ARG == PARM test above. */
16873 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16876 /* A non-type template parameter that is a variable should be a
16877 an integral constant, in which case, it whould have been
16878 folded into its (constant) value. So we should not be getting
16879 a variable here. */
16880 gcc_unreachable ();
16882 case TYPE_ARGUMENT_PACK
:
16883 case NONTYPE_ARGUMENT_PACK
:
16884 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
16885 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
16888 case DECLTYPE_TYPE
:
16889 case UNDERLYING_TYPE
:
16890 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16891 or UNDERLYING_TYPE nodes. */
16892 return unify_success (explain_p
);
16895 /* Unification fails if we hit an error node. */
16896 return unify_invalid (explain_p
);
16899 /* An unresolved overload is a nondeduced context. */
16900 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
16901 return unify_success (explain_p
);
16902 gcc_assert (EXPR_P (parm
));
16904 /* We must be looking at an expression. This can happen with
16908 void foo(S<I>, S<I + 2>);
16910 This is a "nondeduced context":
16914 The nondeduced contexts are:
16916 --A type that is a template-id in which one or more of
16917 the template-arguments is an expression that references
16918 a template-parameter.
16920 In these cases, we assume deduction succeeded, but don't
16921 actually infer any unifications. */
16923 if (!uses_template_parms (parm
)
16924 && !template_args_equal (parm
, arg
))
16925 return unify_expression_unequal (explain_p
, parm
, arg
);
16927 return unify_success (explain_p
);
16930 #undef RECUR_AND_CHECK_FAILURE
16932 /* Note that DECL can be defined in this translation unit, if
16936 mark_definable (tree decl
)
16939 DECL_NOT_REALLY_EXTERN (decl
) = 1;
16940 FOR_EACH_CLONE (clone
, decl
)
16941 DECL_NOT_REALLY_EXTERN (clone
) = 1;
16944 /* Called if RESULT is explicitly instantiated, or is a member of an
16945 explicitly instantiated class. */
16948 mark_decl_instantiated (tree result
, int extern_p
)
16950 SET_DECL_EXPLICIT_INSTANTIATION (result
);
16952 /* If this entity has already been written out, it's too late to
16953 make any modifications. */
16954 if (TREE_ASM_WRITTEN (result
))
16957 if (TREE_CODE (result
) != FUNCTION_DECL
)
16958 /* The TREE_PUBLIC flag for function declarations will have been
16959 set correctly by tsubst. */
16960 TREE_PUBLIC (result
) = 1;
16962 /* This might have been set by an earlier implicit instantiation. */
16963 DECL_COMDAT (result
) = 0;
16966 DECL_NOT_REALLY_EXTERN (result
) = 0;
16969 mark_definable (result
);
16970 /* Always make artificials weak. */
16971 if (DECL_ARTIFICIAL (result
) && flag_weak
)
16972 comdat_linkage (result
);
16973 /* For WIN32 we also want to put explicit instantiations in
16974 linkonce sections. */
16975 else if (TREE_PUBLIC (result
))
16976 maybe_make_one_only (result
);
16979 /* If EXTERN_P, then this function will not be emitted -- unless
16980 followed by an explicit instantiation, at which point its linkage
16981 will be adjusted. If !EXTERN_P, then this function will be
16982 emitted here. In neither circumstance do we want
16983 import_export_decl to adjust the linkage. */
16984 DECL_INTERFACE_KNOWN (result
) = 1;
16987 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16988 important template arguments. If any are missing, we check whether
16989 they're important by using error_mark_node for substituting into any
16990 args that were used for partial ordering (the ones between ARGS and END)
16991 and seeing if it bubbles up. */
16994 check_undeduced_parms (tree targs
, tree args
, tree end
)
16996 bool found
= false;
16998 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
16999 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17002 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17006 for (; args
!= end
; args
= TREE_CHAIN (args
))
17008 tree substed
= tsubst (TREE_VALUE (args
), targs
, tf_none
, NULL_TREE
);
17009 if (substed
== error_mark_node
)
17016 /* Given two function templates PAT1 and PAT2, return:
17018 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17019 -1 if PAT2 is more specialized than PAT1.
17020 0 if neither is more specialized.
17022 LEN indicates the number of parameters we should consider
17023 (defaulted parameters should not be considered).
17025 The 1998 std underspecified function template partial ordering, and
17026 DR214 addresses the issue. We take pairs of arguments, one from
17027 each of the templates, and deduce them against each other. One of
17028 the templates will be more specialized if all the *other*
17029 template's arguments deduce against its arguments and at least one
17030 of its arguments *does* *not* deduce against the other template's
17031 corresponding argument. Deduction is done as for class templates.
17032 The arguments used in deduction have reference and top level cv
17033 qualifiers removed. Iff both arguments were originally reference
17034 types *and* deduction succeeds in both directions, the template
17035 with the more cv-qualified argument wins for that pairing (if
17036 neither is more cv-qualified, they both are equal). Unlike regular
17037 deduction, after all the arguments have been deduced in this way,
17038 we do *not* verify the deduced template argument values can be
17039 substituted into non-deduced contexts.
17041 The logic can be a bit confusing here, because we look at deduce1 and
17042 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17043 can find template arguments for pat1 to make arg1 look like arg2, that
17044 means that arg2 is at least as specialized as arg1. */
17047 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17049 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17050 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17051 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17052 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17053 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17054 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17055 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17056 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17057 tree origs1
, origs2
;
17058 bool lose1
= false;
17059 bool lose2
= false;
17061 /* Remove the this parameter from non-static member functions. If
17062 one is a non-static member function and the other is not a static
17063 member function, remove the first parameter from that function
17064 also. This situation occurs for operator functions where we
17065 locate both a member function (with this pointer) and non-member
17066 operator (with explicit first operand). */
17067 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17069 len
--; /* LEN is the number of significant arguments for DECL1 */
17070 args1
= TREE_CHAIN (args1
);
17071 if (!DECL_STATIC_FUNCTION_P (decl2
))
17072 args2
= TREE_CHAIN (args2
);
17074 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17076 args2
= TREE_CHAIN (args2
);
17077 if (!DECL_STATIC_FUNCTION_P (decl1
))
17080 args1
= TREE_CHAIN (args1
);
17084 /* If only one is a conversion operator, they are unordered. */
17085 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17088 /* Consider the return type for a conversion function */
17089 if (DECL_CONV_FN_P (decl1
))
17091 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17092 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17096 processing_template_decl
++;
17102 /* Stop when an ellipsis is seen. */
17103 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17105 tree arg1
= TREE_VALUE (args1
);
17106 tree arg2
= TREE_VALUE (args2
);
17107 int deduce1
, deduce2
;
17111 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17112 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17114 /* When both arguments are pack expansions, we need only
17115 unify the patterns themselves. */
17116 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17117 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17119 /* This is the last comparison we need to do. */
17123 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17125 arg1
= TREE_TYPE (arg1
);
17126 quals1
= cp_type_quals (arg1
);
17129 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17131 arg2
= TREE_TYPE (arg2
);
17132 quals2
= cp_type_quals (arg2
);
17135 if ((quals1
< 0) != (quals2
< 0))
17137 /* Only of the args is a reference, see if we should apply
17138 array/function pointer decay to it. This is not part of
17139 DR214, but is, IMHO, consistent with the deduction rules
17140 for the function call itself, and with our earlier
17141 implementation of the underspecified partial ordering
17142 rules. (nathan). */
17145 switch (TREE_CODE (arg1
))
17148 arg1
= TREE_TYPE (arg1
);
17150 case FUNCTION_TYPE
:
17151 arg1
= build_pointer_type (arg1
);
17160 switch (TREE_CODE (arg2
))
17163 arg2
= TREE_TYPE (arg2
);
17165 case FUNCTION_TYPE
:
17166 arg2
= build_pointer_type (arg2
);
17175 arg1
= TYPE_MAIN_VARIANT (arg1
);
17176 arg2
= TYPE_MAIN_VARIANT (arg2
);
17178 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17180 int i
, len2
= list_length (args2
);
17181 tree parmvec
= make_tree_vec (1);
17182 tree argvec
= make_tree_vec (len2
);
17185 /* Setup the parameter vector, which contains only ARG1. */
17186 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17188 /* Setup the argument vector, which contains the remaining
17190 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17191 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17193 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17194 argvec
, DEDUCE_EXACT
,
17195 /*subr=*/true, /*explain_p=*/false)
17198 /* We cannot deduce in the other direction, because ARG1 is
17199 a pack expansion but ARG2 is not. */
17202 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17204 int i
, len1
= list_length (args1
);
17205 tree parmvec
= make_tree_vec (1);
17206 tree argvec
= make_tree_vec (len1
);
17209 /* Setup the parameter vector, which contains only ARG1. */
17210 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17212 /* Setup the argument vector, which contains the remaining
17214 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17215 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17217 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17218 argvec
, DEDUCE_EXACT
,
17219 /*subr=*/true, /*explain_p=*/false)
17222 /* We cannot deduce in the other direction, because ARG2 is
17223 a pack expansion but ARG1 is not.*/
17229 /* The normal case, where neither argument is a pack
17231 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17232 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17234 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17235 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17239 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17240 arg2, then arg2 is not as specialized as arg1. */
17246 /* "If, for a given type, deduction succeeds in both directions
17247 (i.e., the types are identical after the transformations above)
17248 and if the type from the argument template is more cv-qualified
17249 than the type from the parameter template (as described above)
17250 that type is considered to be more specialized than the other. If
17251 neither type is more cv-qualified than the other then neither type
17252 is more specialized than the other." */
17254 if (deduce1
&& deduce2
17255 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17257 if ((quals1
& quals2
) == quals2
)
17259 if ((quals1
& quals2
) == quals1
)
17263 if (lose1
&& lose2
)
17264 /* We've failed to deduce something in either direction.
17265 These must be unordered. */
17268 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17269 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17270 /* We have already processed all of the arguments in our
17271 handing of the pack expansion type. */
17274 args1
= TREE_CHAIN (args1
);
17275 args2
= TREE_CHAIN (args2
);
17278 /* "In most cases, all template parameters must have values in order for
17279 deduction to succeed, but for partial ordering purposes a template
17280 parameter may remain without a value provided it is not used in the
17281 types being used for partial ordering."
17283 Thus, if we are missing any of the targs1 we need to substitute into
17284 origs1, then pat2 is not as specialized as pat1. This can happen when
17285 there is a nondeduced context. */
17286 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17288 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17291 processing_template_decl
--;
17293 /* All things being equal, if the next argument is a pack expansion
17294 for one function but not for the other, prefer the
17295 non-variadic function. FIXME this is bogus; see c++/41958. */
17297 && args1
&& TREE_VALUE (args1
)
17298 && args2
&& TREE_VALUE (args2
))
17300 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17301 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17304 if (lose1
== lose2
)
17312 /* Determine which of two partial specializations is more specialized.
17314 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17315 to the first partial specialization. The TREE_VALUE is the
17316 innermost set of template parameters for the partial
17317 specialization. PAT2 is similar, but for the second template.
17319 Return 1 if the first partial specialization is more specialized;
17320 -1 if the second is more specialized; 0 if neither is more
17323 See [temp.class.order] for information about determining which of
17324 two templates is more specialized. */
17327 more_specialized_class (tree pat1
, tree pat2
)
17332 bool any_deductions
= false;
17334 tmpl1
= TREE_TYPE (pat1
);
17335 tmpl2
= TREE_TYPE (pat2
);
17337 /* Just like what happens for functions, if we are ordering between
17338 different class template specializations, we may encounter dependent
17339 types in the arguments, and we need our dependency check functions
17340 to behave correctly. */
17341 ++processing_template_decl
;
17342 targs
= get_class_bindings (TREE_VALUE (pat1
),
17343 CLASSTYPE_TI_ARGS (tmpl1
),
17344 CLASSTYPE_TI_ARGS (tmpl2
));
17348 any_deductions
= true;
17351 targs
= get_class_bindings (TREE_VALUE (pat2
),
17352 CLASSTYPE_TI_ARGS (tmpl2
),
17353 CLASSTYPE_TI_ARGS (tmpl1
));
17357 any_deductions
= true;
17359 --processing_template_decl
;
17361 /* In the case of a tie where at least one of the class templates
17362 has a parameter pack at the end, the template with the most
17363 non-packed parameters wins. */
17366 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17367 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17369 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17370 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17371 int len1
= TREE_VEC_LENGTH (args1
);
17372 int len2
= TREE_VEC_LENGTH (args2
);
17374 /* We don't count the pack expansion at the end. */
17375 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17377 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17382 else if (len1
< len2
)
17389 /* Return the template arguments that will produce the function signature
17390 DECL from the function template FN, with the explicit template
17391 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17392 also match. Return NULL_TREE if no satisfactory arguments could be
17396 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17398 int ntparms
= DECL_NTPARMS (fn
);
17399 tree targs
= make_tree_vec (ntparms
);
17401 tree decl_arg_types
;
17403 unsigned int nargs
, ix
;
17406 /* Substitute the explicit template arguments into the type of DECL.
17407 The call to fn_type_unification will handle substitution into the
17409 decl_type
= TREE_TYPE (decl
);
17410 if (explicit_args
&& decl
== DECL_TEMPLATE_RESULT (fn
))
17413 tree converted_args
;
17415 if (DECL_TEMPLATE_INFO (decl
))
17416 tmpl
= DECL_TI_TEMPLATE (decl
);
17418 /* We can get here for some invalid specializations. */
17422 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17423 explicit_args
, NULL_TREE
,
17425 /*require_all_args=*/false,
17426 /*use_default_args=*/false);
17427 if (converted_args
== error_mark_node
)
17430 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
17431 if (decl_type
== error_mark_node
)
17435 /* Never do unification on the 'this' parameter. */
17436 decl_arg_types
= skip_artificial_parms_for (decl
,
17437 TYPE_ARG_TYPES (decl_type
));
17439 nargs
= list_length (decl_arg_types
);
17440 args
= XALLOCAVEC (tree
, nargs
);
17441 for (arg
= decl_arg_types
, ix
= 0;
17442 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17443 arg
= TREE_CHAIN (arg
), ++ix
)
17444 args
[ix
] = TREE_VALUE (arg
);
17446 if (fn_type_unification (fn
, explicit_args
, targs
,
17448 (check_rettype
|| DECL_CONV_FN_P (fn
)
17449 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17450 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false))
17456 /* Return the innermost template arguments that, when applied to a
17457 template specialization whose innermost template parameters are
17458 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17461 For example, suppose we have:
17463 template <class T, class U> struct S {};
17464 template <class T> struct S<T*, int> {};
17466 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17467 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17468 int}. The resulting vector will be {double}, indicating that `T'
17469 is bound to `double'. */
17472 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
17474 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17476 tree innermost_deduced_args
;
17478 innermost_deduced_args
= make_tree_vec (ntparms
);
17479 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17481 deduced_args
= copy_node (args
);
17482 SET_TMPL_ARGS_LEVEL (deduced_args
,
17483 TMPL_ARGS_DEPTH (deduced_args
),
17484 innermost_deduced_args
);
17487 deduced_args
= innermost_deduced_args
;
17489 if (unify (tparms
, deduced_args
,
17490 INNERMOST_TEMPLATE_ARGS (spec_args
),
17491 INNERMOST_TEMPLATE_ARGS (args
),
17492 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17495 for (i
= 0; i
< ntparms
; ++i
)
17496 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17499 /* Verify that nondeduced template arguments agree with the type
17500 obtained from argument deduction.
17504 struct A { typedef int X; };
17505 template <class T, class U> struct C {};
17506 template <class T> struct C<T, typename T::X> {};
17508 Then with the instantiation `C<A, int>', we can deduce that
17509 `T' is `A' but unify () does not check whether `typename T::X'
17511 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17512 if (spec_args
== error_mark_node
17513 /* We only need to check the innermost arguments; the other
17514 arguments will always agree. */
17515 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17516 INNERMOST_TEMPLATE_ARGS (args
)))
17519 /* Now that we have bindings for all of the template arguments,
17520 ensure that the arguments deduced for the template template
17521 parameters have compatible template parameter lists. See the use
17522 of template_template_parm_bindings_ok_p in fn_type_unification
17523 for more information. */
17524 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17527 return deduced_args
;
17530 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17531 Return the TREE_LIST node with the most specialized template, if
17532 any. If there is no most specialized template, the error_mark_node
17535 Note that this function does not look at, or modify, the
17536 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17537 returned is one of the elements of INSTANTIATIONS, callers may
17538 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17539 and retrieve it from the value returned. */
17542 most_specialized_instantiation (tree templates
)
17546 ++processing_template_decl
;
17549 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17553 if (get_bindings (TREE_VALUE (champ
),
17554 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17555 NULL_TREE
, /*check_ret=*/true))
17558 if (get_bindings (TREE_VALUE (fn
),
17559 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17560 NULL_TREE
, /*check_ret=*/true))
17567 /* Equally specialized, move to next function. If there
17568 is no next function, nothing's most specialized. */
17569 fn
= TREE_CHAIN (fn
);
17577 /* Now verify that champ is better than everything earlier in the
17578 instantiation list. */
17579 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17580 if (get_bindings (TREE_VALUE (champ
),
17581 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17582 NULL_TREE
, /*check_ret=*/true)
17583 || !get_bindings (TREE_VALUE (fn
),
17584 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17585 NULL_TREE
, /*check_ret=*/true))
17591 processing_template_decl
--;
17594 return error_mark_node
;
17599 /* If DECL is a specialization of some template, return the most
17600 general such template. Otherwise, returns NULL_TREE.
17602 For example, given:
17604 template <class T> struct S { template <class U> void f(U); };
17606 if TMPL is `template <class U> void S<int>::f(U)' this will return
17607 the full template. This function will not trace past partial
17608 specializations, however. For example, given in addition:
17610 template <class T> struct S<T*> { template <class U> void f(U); };
17612 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17613 `template <class T> template <class U> S<T*>::f(U)'. */
17616 most_general_template (tree decl
)
17618 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17619 an immediate specialization. */
17620 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17622 if (DECL_TEMPLATE_INFO (decl
)) {
17623 decl
= DECL_TI_TEMPLATE (decl
);
17625 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17626 template friend. */
17627 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17633 /* Look for more and more general templates. */
17634 while (DECL_TEMPLATE_INFO (decl
))
17636 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17637 (See cp-tree.h for details.) */
17638 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17641 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17642 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17645 /* Stop if we run into an explicitly specialized class template. */
17646 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17647 && DECL_CONTEXT (decl
)
17648 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17651 decl
= DECL_TI_TEMPLATE (decl
);
17657 /* Return the most specialized of the class template partial
17658 specializations of TMPL which can produce TYPE, a specialization of
17659 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17660 a _TYPE node corresponding to the partial specialization, while the
17661 TREE_PURPOSE is the set of template arguments that must be
17662 substituted into the TREE_TYPE in order to generate TYPE.
17664 If the choice of partial specialization is ambiguous, a diagnostic
17665 is issued, and the error_mark_node is returned. If there are no
17666 partial specializations of TMPL matching TYPE, then NULL_TREE is
17670 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17672 tree list
= NULL_TREE
;
17678 tree outer_args
= NULL_TREE
;
17680 tmpl
= most_general_template (tmpl
);
17681 args
= CLASSTYPE_TI_ARGS (type
);
17683 /* For determining which partial specialization to use, only the
17684 innermost args are interesting. */
17685 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17687 outer_args
= strip_innermost_template_args (args
, 1);
17688 args
= INNERMOST_TEMPLATE_ARGS (args
);
17691 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17693 tree partial_spec_args
;
17695 tree parms
= TREE_VALUE (t
);
17697 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17699 ++processing_template_decl
;
17705 /* Discard the outer levels of args, and then substitute in the
17706 template args from the enclosing class. */
17707 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17708 partial_spec_args
= tsubst_template_args
17709 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17711 /* PARMS already refers to just the innermost parms, but the
17712 template parms in partial_spec_args had their levels lowered
17713 by tsubst, so we need to do the same for the parm list. We
17714 can't just tsubst the TREE_VEC itself, as tsubst wants to
17715 treat a TREE_VEC as an argument vector. */
17716 parms
= copy_node (parms
);
17717 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17718 TREE_VEC_ELT (parms
, i
) =
17719 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17723 partial_spec_args
=
17724 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17725 add_to_template_args (outer_args
,
17726 partial_spec_args
),
17728 /*require_all_args=*/true,
17729 /*use_default_args=*/true);
17731 --processing_template_decl
;
17733 if (partial_spec_args
== error_mark_node
)
17734 return error_mark_node
;
17736 spec_args
= get_class_bindings (parms
,
17742 spec_args
= add_to_template_args (outer_args
, spec_args
);
17743 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17744 TREE_TYPE (list
) = TREE_TYPE (t
);
17751 ambiguous_p
= false;
17754 t
= TREE_CHAIN (t
);
17755 for (; t
; t
= TREE_CHAIN (t
))
17757 fate
= more_specialized_class (champ
, t
);
17764 t
= TREE_CHAIN (t
);
17767 ambiguous_p
= true;
17776 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17778 fate
= more_specialized_class (champ
, t
);
17781 ambiguous_p
= true;
17789 char *spaces
= NULL
;
17790 if (!(complain
& tf_error
))
17791 return error_mark_node
;
17792 error ("ambiguous class template instantiation for %q#T", type
);
17793 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17794 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17796 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17797 spaces
= spaces
? spaces
: get_spaces (str
);
17800 return error_mark_node
;
17806 /* Explicitly instantiate DECL. */
17809 do_decl_instantiation (tree decl
, tree storage
)
17811 tree result
= NULL_TREE
;
17814 if (!decl
|| decl
== error_mark_node
)
17815 /* An error occurred, for which grokdeclarator has already issued
17816 an appropriate message. */
17818 else if (! DECL_LANG_SPECIFIC (decl
))
17820 error ("explicit instantiation of non-template %q#D", decl
);
17823 else if (TREE_CODE (decl
) == VAR_DECL
)
17825 /* There is an asymmetry here in the way VAR_DECLs and
17826 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17827 the latter, the DECL we get back will be marked as a
17828 template instantiation, and the appropriate
17829 DECL_TEMPLATE_INFO will be set up. This does not happen for
17830 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17831 should handle VAR_DECLs as it currently handles
17833 if (!DECL_CLASS_SCOPE_P (decl
))
17835 error ("%qD is not a static data member of a class template", decl
);
17838 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17839 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17841 error ("no matching template for %qD found", decl
);
17844 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17846 error ("type %qT for explicit instantiation %qD does not match "
17847 "declared type %qT", TREE_TYPE (result
), decl
,
17852 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17854 error ("explicit instantiation of %q#D", decl
);
17860 /* Check for various error cases. Note that if the explicit
17861 instantiation is valid the RESULT will currently be marked as an
17862 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17863 until we get here. */
17865 if (DECL_TEMPLATE_SPECIALIZATION (result
))
17867 /* DR 259 [temp.spec].
17869 Both an explicit instantiation and a declaration of an explicit
17870 specialization shall not appear in a program unless the explicit
17871 instantiation follows a declaration of the explicit specialization.
17873 For a given set of template parameters, if an explicit
17874 instantiation of a template appears after a declaration of an
17875 explicit specialization for that template, the explicit
17876 instantiation has no effect. */
17879 else if (DECL_EXPLICIT_INSTANTIATION (result
))
17883 No program shall explicitly instantiate any template more
17886 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17887 the first instantiation was `extern' and the second is not,
17888 and EXTERN_P for the opposite case. */
17889 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
17890 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
17891 /* If an "extern" explicit instantiation follows an ordinary
17892 explicit instantiation, the template is instantiated. */
17896 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
17898 error ("no matching template for %qD found", result
);
17901 else if (!DECL_TEMPLATE_INFO (result
))
17903 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
17907 if (storage
== NULL_TREE
)
17909 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17911 if (!in_system_header
&& (cxx_dialect
== cxx98
))
17912 pedwarn (input_location
, OPT_pedantic
,
17913 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17918 error ("storage class %qD applied to template instantiation", storage
);
17920 check_explicit_instantiation_namespace (result
);
17921 mark_decl_instantiated (result
, extern_p
);
17923 instantiate_decl (result
, /*defer_ok=*/1,
17924 /*expl_inst_class_mem_p=*/false);
17928 mark_class_instantiated (tree t
, int extern_p
)
17930 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
17931 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
17932 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
17933 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
17936 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
17937 rest_of_type_compilation (t
, 1);
17941 /* Called from do_type_instantiation through binding_table_foreach to
17942 do recursive instantiation for the type bound in ENTRY. */
17944 bt_instantiate_type_proc (binding_entry entry
, void *data
)
17946 tree storage
= *(tree
*) data
;
17948 if (MAYBE_CLASS_TYPE_P (entry
->type
)
17949 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
17950 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
17953 /* Called from do_type_instantiation to instantiate a member
17954 (a member function or a static member variable) of an
17955 explicitly instantiated class template. */
17957 instantiate_class_member (tree decl
, int extern_p
)
17959 mark_decl_instantiated (decl
, extern_p
);
17961 instantiate_decl (decl
, /*defer_ok=*/1,
17962 /*expl_inst_class_mem_p=*/true);
17965 /* Perform an explicit instantiation of template class T. STORAGE, if
17966 non-null, is the RID for extern, inline or static. COMPLAIN is
17967 nonzero if this is called from the parser, zero if called recursively,
17968 since the standard is unclear (as detailed below). */
17971 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
17976 int previous_instantiation_extern_p
= 0;
17978 if (TREE_CODE (t
) == TYPE_DECL
)
17981 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
17984 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
17986 error ("explicit instantiation of non-class template %qD", tmpl
);
17988 error ("explicit instantiation of non-template type %qT", t
);
17994 if (!COMPLETE_TYPE_P (t
))
17996 if (complain
& tf_error
)
17997 error ("explicit instantiation of %q#T before definition of template",
18002 if (storage
!= NULL_TREE
)
18004 if (!in_system_header
)
18006 if (storage
== ridpointers
[(int) RID_EXTERN
])
18008 if (cxx_dialect
== cxx98
)
18009 pedwarn (input_location
, OPT_pedantic
,
18010 "ISO C++ 1998 forbids the use of %<extern%> on "
18011 "explicit instantiations");
18014 pedwarn (input_location
, OPT_pedantic
,
18015 "ISO C++ forbids the use of %qE"
18016 " on explicit instantiations", storage
);
18019 if (storage
== ridpointers
[(int) RID_INLINE
])
18021 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18023 else if (storage
== ridpointers
[(int) RID_STATIC
])
18027 error ("storage class %qD applied to template instantiation",
18033 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18035 /* DR 259 [temp.spec].
18037 Both an explicit instantiation and a declaration of an explicit
18038 specialization shall not appear in a program unless the explicit
18039 instantiation follows a declaration of the explicit specialization.
18041 For a given set of template parameters, if an explicit
18042 instantiation of a template appears after a declaration of an
18043 explicit specialization for that template, the explicit
18044 instantiation has no effect. */
18047 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18051 No program shall explicitly instantiate any template more
18054 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18055 instantiation was `extern'. If EXTERN_P then the second is.
18056 These cases are OK. */
18057 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18059 if (!previous_instantiation_extern_p
&& !extern_p
18060 && (complain
& tf_error
))
18061 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18063 /* If we've already instantiated the template, just return now. */
18064 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18068 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18069 mark_class_instantiated (t
, extern_p
);
18077 /* In contrast to implicit instantiation, where only the
18078 declarations, and not the definitions, of members are
18079 instantiated, we have here:
18083 The explicit instantiation of a class template specialization
18084 implies the instantiation of all of its members not
18085 previously explicitly specialized in the translation unit
18086 containing the explicit instantiation.
18088 Of course, we can't instantiate member template classes, since
18089 we don't have any arguments for them. Note that the standard
18090 is unclear on whether the instantiation of the members are
18091 *explicit* instantiations or not. However, the most natural
18092 interpretation is that it should be an explicit instantiation. */
18095 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18096 if (TREE_CODE (tmp
) == FUNCTION_DECL
18097 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18098 instantiate_class_member (tmp
, extern_p
);
18100 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18101 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
18102 instantiate_class_member (tmp
, extern_p
);
18104 if (CLASSTYPE_NESTED_UTDS (t
))
18105 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18106 bt_instantiate_type_proc
, &storage
);
18110 /* Given a function DECL, which is a specialization of TMPL, modify
18111 DECL to be a re-instantiation of TMPL with the same template
18112 arguments. TMPL should be the template into which tsubst'ing
18113 should occur for DECL, not the most general template.
18115 One reason for doing this is a scenario like this:
18118 void f(const T&, int i);
18120 void g() { f(3, 7); }
18123 void f(const T& t, const int i) { }
18125 Note that when the template is first instantiated, with
18126 instantiate_template, the resulting DECL will have no name for the
18127 first parameter, and the wrong type for the second. So, when we go
18128 to instantiate the DECL, we regenerate it. */
18131 regenerate_decl_from_template (tree decl
, tree tmpl
)
18133 /* The arguments used to instantiate DECL, from the most general
18138 args
= DECL_TI_ARGS (decl
);
18139 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18141 /* Make sure that we can see identifiers, and compute access
18143 push_access_scope (decl
);
18145 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18153 args_depth
= TMPL_ARGS_DEPTH (args
);
18154 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18155 if (args_depth
> parms_depth
)
18156 args
= get_innermost_template_args (args
, parms_depth
);
18158 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18159 args
, tf_error
, NULL_TREE
,
18160 /*defer_ok*/false);
18161 if (specs
&& specs
!= error_mark_node
)
18162 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18165 /* Merge parameter declarations. */
18166 decl_parm
= skip_artificial_parms_for (decl
,
18167 DECL_ARGUMENTS (decl
));
18169 = skip_artificial_parms_for (code_pattern
,
18170 DECL_ARGUMENTS (code_pattern
));
18171 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18176 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18177 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18178 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18180 parm_type
= type_decays_to (parm_type
);
18181 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18182 TREE_TYPE (decl_parm
) = parm_type
;
18183 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18184 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18186 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18187 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18189 decl_parm
= DECL_CHAIN (decl_parm
);
18190 pattern_parm
= DECL_CHAIN (pattern_parm
);
18192 /* Merge any parameters that match with the function parameter
18194 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18197 tree expanded_types
;
18198 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18199 the parameters in this function parameter pack. */
18200 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18201 args
, tf_error
, NULL_TREE
);
18202 len
= TREE_VEC_LENGTH (expanded_types
);
18203 for (i
= 0; i
< len
; i
++)
18208 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18209 /* Rename the parameter to include the index. */
18210 DECL_NAME (decl_parm
) =
18211 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18212 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18213 parm_type
= type_decays_to (parm_type
);
18214 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18215 TREE_TYPE (decl_parm
) = parm_type
;
18216 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18217 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18219 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18220 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18222 decl_parm
= DECL_CHAIN (decl_parm
);
18225 /* Merge additional specifiers from the CODE_PATTERN. */
18226 if (DECL_DECLARED_INLINE_P (code_pattern
)
18227 && !DECL_DECLARED_INLINE_P (decl
))
18228 DECL_DECLARED_INLINE_P (decl
) = 1;
18230 else if (TREE_CODE (decl
) == VAR_DECL
)
18232 DECL_INITIAL (decl
) =
18233 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18234 tf_error
, DECL_TI_TEMPLATE (decl
),
18235 /*integral_constant_expression_p=*/false);
18236 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18237 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18238 tf_error
, DECL_TI_TEMPLATE (decl
));
18241 gcc_unreachable ();
18243 pop_access_scope (decl
);
18246 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18247 substituted to get DECL. */
18250 template_for_substitution (tree decl
)
18252 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18254 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18255 for the instantiation. This is not always the most general
18256 template. Consider, for example:
18259 struct S { template <class U> void f();
18260 template <> void f<int>(); };
18262 and an instantiation of S<double>::f<int>. We want TD to be the
18263 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18264 while (/* An instantiation cannot have a definition, so we need a
18265 more general template. */
18266 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18267 /* We must also deal with friend templates. Given:
18269 template <class T> struct S {
18270 template <class U> friend void f() {};
18273 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18274 so far as the language is concerned, but that's still
18275 where we get the pattern for the instantiation from. On
18276 other hand, if the definition comes outside the class, say:
18278 template <class T> struct S {
18279 template <class U> friend void f();
18281 template <class U> friend void f() {}
18283 we don't need to look any further. That's what the check for
18284 DECL_INITIAL is for. */
18285 || (TREE_CODE (decl
) == FUNCTION_DECL
18286 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18287 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18289 /* The present template, TD, should not be a definition. If it
18290 were a definition, we should be using it! Note that we
18291 cannot restructure the loop to just keep going until we find
18292 a template with a definition, since that might go too far if
18293 a specialization was declared, but not defined. */
18294 gcc_assert (TREE_CODE (decl
) != VAR_DECL
18295 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18297 /* Fetch the more general template. */
18298 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18304 /* Returns true if we need to instantiate this template instance even if we
18305 know we aren't going to emit it.. */
18308 always_instantiate_p (tree decl
)
18310 /* We always instantiate inline functions so that we can inline them. An
18311 explicit instantiation declaration prohibits implicit instantiation of
18312 non-inline functions. With high levels of optimization, we would
18313 normally inline non-inline functions -- but we're not allowed to do
18314 that for "extern template" functions. Therefore, we check
18315 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18316 return ((TREE_CODE (decl
) == FUNCTION_DECL
18317 && (DECL_DECLARED_INLINE_P (decl
)
18318 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
18319 /* And we need to instantiate static data members so that
18320 their initializers are available in integral constant
18322 || (TREE_CODE (decl
) == VAR_DECL
18323 && decl_maybe_constant_var_p (decl
)));
18326 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18327 instantiate it now, modifying TREE_TYPE (fn). */
18330 maybe_instantiate_noexcept (tree fn
)
18332 tree fntype
, spec
, noex
, clone
;
18334 if (DECL_CLONED_FUNCTION_P (fn
))
18335 fn
= DECL_CLONED_FUNCTION (fn
);
18336 fntype
= TREE_TYPE (fn
);
18337 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18339 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18342 noex
= TREE_PURPOSE (spec
);
18344 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18346 if (push_tinst_level (fn
))
18348 push_access_scope (fn
);
18349 input_location
= DECL_SOURCE_LOCATION (fn
);
18350 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18351 DEFERRED_NOEXCEPT_ARGS (noex
),
18352 tf_warning_or_error
, fn
,
18353 /*function_p=*/false,
18354 /*integral_constant_expression_p=*/true);
18355 pop_access_scope (fn
);
18356 pop_tinst_level ();
18357 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18358 if (spec
== error_mark_node
)
18359 spec
= noexcept_false_spec
;
18362 spec
= noexcept_false_spec
;
18366 /* This is an implicitly declared function, so NOEX is a list of
18367 other functions to evaluate and merge. */
18369 spec
= noexcept_true_spec
;
18370 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18372 tree fn
= OVL_CURRENT (elt
);
18374 maybe_instantiate_noexcept (fn
);
18375 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18376 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18380 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18382 FOR_EACH_CLONE (clone
, fn
)
18384 if (TREE_TYPE (clone
) == fntype
)
18385 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18387 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18391 /* Produce the definition of D, a _DECL generated from a template. If
18392 DEFER_OK is nonzero, then we don't have to actually do the
18393 instantiation now; we just have to do it sometime. Normally it is
18394 an error if this is an explicit instantiation but D is undefined.
18395 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18396 explicitly instantiated class template. */
18399 instantiate_decl (tree d
, int defer_ok
,
18400 bool expl_inst_class_mem_p
)
18402 tree tmpl
= DECL_TI_TEMPLATE (d
);
18409 bool pattern_defined
;
18411 location_t saved_loc
= input_location
;
18414 /* This function should only be used to instantiate templates for
18415 functions and static member variables. */
18416 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18417 || TREE_CODE (d
) == VAR_DECL
);
18419 /* Variables are never deferred; if instantiation is required, they
18420 are instantiated right away. That allows for better code in the
18421 case that an expression refers to the value of the variable --
18422 if the variable has a constant value the referring expression can
18423 take advantage of that fact. */
18424 if (TREE_CODE (d
) == VAR_DECL
18425 || DECL_DECLARED_CONSTEXPR_P (d
))
18428 /* Don't instantiate cloned functions. Instead, instantiate the
18429 functions they cloned. */
18430 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18431 d
= DECL_CLONED_FUNCTION (d
);
18433 if (DECL_TEMPLATE_INSTANTIATED (d
)
18434 || (TREE_CODE (d
) == FUNCTION_DECL
18435 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18436 || DECL_TEMPLATE_SPECIALIZATION (d
))
18437 /* D has already been instantiated or explicitly specialized, so
18438 there's nothing for us to do here.
18440 It might seem reasonable to check whether or not D is an explicit
18441 instantiation, and, if so, stop here. But when an explicit
18442 instantiation is deferred until the end of the compilation,
18443 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18444 the instantiation. */
18447 /* Check to see whether we know that this template will be
18448 instantiated in some other file, as with "extern template"
18450 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18452 /* In general, we do not instantiate such templates. */
18453 if (external_p
&& !always_instantiate_p (d
))
18456 gen_tmpl
= most_general_template (tmpl
);
18457 gen_args
= DECL_TI_ARGS (d
);
18459 if (tmpl
!= gen_tmpl
)
18460 /* We should already have the extra args. */
18461 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18462 == TMPL_ARGS_DEPTH (gen_args
));
18463 /* And what's in the hash table should match D. */
18464 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18465 || spec
== NULL_TREE
);
18467 /* This needs to happen before any tsubsting. */
18468 if (! push_tinst_level (d
))
18471 timevar_push (TV_TEMPLATE_INST
);
18473 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18474 for the instantiation. */
18475 td
= template_for_substitution (d
);
18476 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18478 /* We should never be trying to instantiate a member of a class
18479 template or partial specialization. */
18480 gcc_assert (d
!= code_pattern
);
18482 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18483 || DECL_TEMPLATE_SPECIALIZATION (td
))
18484 /* In the case of a friend template whose definition is provided
18485 outside the class, we may have too many arguments. Drop the
18486 ones we don't need. The same is true for specializations. */
18487 args
= get_innermost_template_args
18488 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18492 if (TREE_CODE (d
) == FUNCTION_DECL
)
18493 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18494 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18496 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18498 /* We may be in the middle of deferred access check. Disable it now. */
18499 push_deferring_access_checks (dk_no_deferred
);
18501 /* Unless an explicit instantiation directive has already determined
18502 the linkage of D, remember that a definition is available for
18504 if (pattern_defined
18505 && !DECL_INTERFACE_KNOWN (d
)
18506 && !DECL_NOT_REALLY_EXTERN (d
))
18507 mark_definable (d
);
18509 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18510 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18511 input_location
= DECL_SOURCE_LOCATION (d
);
18513 /* If D is a member of an explicitly instantiated class template,
18514 and no definition is available, treat it like an implicit
18516 if (!pattern_defined
&& expl_inst_class_mem_p
18517 && DECL_EXPLICIT_INSTANTIATION (d
))
18519 /* Leave linkage flags alone on instantiations with anonymous
18521 if (TREE_PUBLIC (d
))
18523 DECL_NOT_REALLY_EXTERN (d
) = 0;
18524 DECL_INTERFACE_KNOWN (d
) = 0;
18526 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18529 if (TREE_CODE (d
) == FUNCTION_DECL
)
18530 maybe_instantiate_noexcept (d
);
18532 /* Recheck the substitutions to obtain any warning messages
18533 about ignoring cv qualifiers. Don't do this for artificial decls,
18534 as it breaks the context-sensitive substitution for lambda op(). */
18535 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
18537 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18538 tree type
= TREE_TYPE (gen
);
18540 /* Make sure that we can see identifiers, and compute access
18541 correctly. D is already the target FUNCTION_DECL with the
18543 push_access_scope (d
);
18545 if (TREE_CODE (gen
) == FUNCTION_DECL
)
18547 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
18548 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
18549 d
, /*defer_ok*/true);
18550 /* Don't simply tsubst the function type, as that will give
18551 duplicate warnings about poor parameter qualifications.
18552 The function arguments are the same as the decl_arguments
18553 without the top level cv qualifiers. */
18554 type
= TREE_TYPE (type
);
18556 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
18558 pop_access_scope (d
);
18561 /* Defer all other templates, unless we have been explicitly
18562 forbidden from doing so. */
18563 if (/* If there is no definition, we cannot instantiate the
18566 /* If it's OK to postpone instantiation, do so. */
18568 /* If this is a static data member that will be defined
18569 elsewhere, we don't want to instantiate the entire data
18570 member, but we do want to instantiate the initializer so that
18571 we can substitute that elsewhere. */
18572 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18574 /* The definition of the static data member is now required so
18575 we must substitute the initializer. */
18576 if (TREE_CODE (d
) == VAR_DECL
18577 && !DECL_INITIAL (d
)
18578 && DECL_INITIAL (code_pattern
))
18582 bool const_init
= false;
18584 ns
= decl_namespace_context (d
);
18585 push_nested_namespace (ns
);
18586 push_nested_class (DECL_CONTEXT (d
));
18587 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18589 tf_warning_or_error
, NULL_TREE
,
18590 /*integral_constant_expression_p=*/false);
18591 /* Make sure the initializer is still constant, in case of
18592 circular dependency (template/instantiate6.C). */
18594 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18595 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18596 /*asmspec_tree=*/NULL_TREE
,
18597 LOOKUP_ONLYCONVERTING
);
18598 pop_nested_class ();
18599 pop_nested_namespace (ns
);
18602 /* We restore the source position here because it's used by
18603 add_pending_template. */
18604 input_location
= saved_loc
;
18606 if (at_eof
&& !pattern_defined
18607 && DECL_EXPLICIT_INSTANTIATION (d
)
18608 && DECL_NOT_REALLY_EXTERN (d
))
18611 The definition of a non-exported function template, a
18612 non-exported member function template, or a non-exported
18613 member function or static data member of a class template
18614 shall be present in every translation unit in which it is
18615 explicitly instantiated. */
18616 permerror (input_location
, "explicit instantiation of %qD "
18617 "but no definition available", d
);
18619 /* If we're in unevaluated context, we just wanted to get the
18620 constant value; this isn't an odr use, so don't queue
18621 a full instantiation. */
18622 if (cp_unevaluated_operand
!= 0)
18624 /* ??? Historically, we have instantiated inline functions, even
18625 when marked as "extern template". */
18626 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18627 add_pending_template (d
);
18630 /* Tell the repository that D is available in this translation unit
18631 -- and see if it is supposed to be instantiated here. */
18632 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18634 /* In a PCH file, despite the fact that the repository hasn't
18635 requested instantiation in the PCH it is still possible that
18636 an instantiation will be required in a file that includes the
18639 add_pending_template (d
);
18640 /* Instantiate inline functions so that the inliner can do its
18641 job, even though we'll not be emitting a copy of this
18643 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18647 need_push
= !cfun
|| !global_bindings_p ();
18649 push_to_top_level ();
18651 /* Mark D as instantiated so that recursive calls to
18652 instantiate_decl do not try to instantiate it again. */
18653 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18655 /* Regenerate the declaration in case the template has been modified
18656 by a subsequent redeclaration. */
18657 regenerate_decl_from_template (d
, td
);
18659 /* We already set the file and line above. Reset them now in case
18660 they changed as a result of calling regenerate_decl_from_template. */
18661 input_location
= DECL_SOURCE_LOCATION (d
);
18663 if (TREE_CODE (d
) == VAR_DECL
)
18666 bool const_init
= false;
18668 /* Clear out DECL_RTL; whatever was there before may not be right
18669 since we've reset the type of the declaration. */
18670 SET_DECL_RTL (d
, NULL
);
18671 DECL_IN_AGGR_P (d
) = 0;
18673 /* The initializer is placed in DECL_INITIAL by
18674 regenerate_decl_from_template so we don't need to
18675 push/pop_access_scope again here. Pull it out so that
18676 cp_finish_decl can process it. */
18677 init
= DECL_INITIAL (d
);
18678 DECL_INITIAL (d
) = NULL_TREE
;
18679 DECL_INITIALIZED_P (d
) = 0;
18681 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18682 initializer. That function will defer actual emission until
18683 we have a chance to determine linkage. */
18684 DECL_EXTERNAL (d
) = 0;
18686 /* Enter the scope of D so that access-checking works correctly. */
18687 push_nested_class (DECL_CONTEXT (d
));
18688 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18689 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18690 pop_nested_class ();
18692 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18693 synthesize_method (d
);
18694 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18696 struct pointer_map_t
*saved_local_specializations
;
18701 /* Save away the current list, in case we are instantiating one
18702 template from within the body of another. */
18703 saved_local_specializations
= local_specializations
;
18705 /* Set up the list of local specializations. */
18706 local_specializations
= pointer_map_create ();
18708 /* Set up context. */
18709 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18711 /* Create substitution entries for the parameters. */
18712 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18713 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18714 spec_parm
= DECL_ARGUMENTS (d
);
18715 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18717 register_local_specialization (spec_parm
, tmpl_parm
);
18718 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18719 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18721 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18723 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18725 register_local_specialization (spec_parm
, tmpl_parm
);
18726 spec_parm
= DECL_CHAIN (spec_parm
);
18730 /* Register the (value) argument pack as a specialization of
18731 TMPL_PARM, then move on. */
18732 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18733 register_local_specialization (argpack
, tmpl_parm
);
18736 gcc_assert (!spec_parm
);
18738 /* Substitute into the body of the function. */
18739 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18740 tf_warning_or_error
, tmpl
,
18741 /*integral_constant_expression_p=*/false);
18743 /* Set the current input_location to the end of the function
18744 so that finish_function knows where we are. */
18745 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18747 /* We don't need the local specializations any more. */
18748 pointer_map_destroy (local_specializations
);
18749 local_specializations
= saved_local_specializations
;
18751 /* Finish the function. */
18752 d
= finish_function (0);
18753 expand_or_defer_fn (d
);
18756 /* We're not deferring instantiation any more. */
18757 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18760 pop_from_top_level ();
18763 input_location
= saved_loc
;
18764 pop_deferring_access_checks ();
18765 pop_tinst_level ();
18767 timevar_pop (TV_TEMPLATE_INST
);
18772 /* Run through the list of templates that we wish we could
18773 instantiate, and instantiate any we can. RETRIES is the
18774 number of times we retry pending template instantiation. */
18777 instantiate_pending_templates (int retries
)
18780 location_t saved_loc
= input_location
;
18782 /* Instantiating templates may trigger vtable generation. This in turn
18783 may require further template instantiations. We place a limit here
18784 to avoid infinite loop. */
18785 if (pending_templates
&& retries
>= max_tinst_depth
)
18787 tree decl
= pending_templates
->tinst
->decl
;
18789 error ("template instantiation depth exceeds maximum of %d"
18790 " instantiating %q+D, possibly from virtual table generation"
18791 " (use -ftemplate-depth= to increase the maximum)",
18792 max_tinst_depth
, decl
);
18793 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18794 /* Pretend that we defined it. */
18795 DECL_INITIAL (decl
) = error_mark_node
;
18801 struct pending_template
**t
= &pending_templates
;
18802 struct pending_template
*last
= NULL
;
18806 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18807 bool complete
= false;
18809 if (TYPE_P (instantiation
))
18813 if (!COMPLETE_TYPE_P (instantiation
))
18815 instantiate_class_template (instantiation
);
18816 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18817 for (fn
= TYPE_METHODS (instantiation
);
18819 fn
= TREE_CHAIN (fn
))
18820 if (! DECL_ARTIFICIAL (fn
))
18821 instantiate_decl (fn
,
18823 /*expl_inst_class_mem_p=*/false);
18824 if (COMPLETE_TYPE_P (instantiation
))
18828 complete
= COMPLETE_TYPE_P (instantiation
);
18832 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18833 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18836 = instantiate_decl (instantiation
,
18838 /*expl_inst_class_mem_p=*/false);
18839 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18843 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18844 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18848 /* If INSTANTIATION has been instantiated, then we don't
18849 need to consider it again in the future. */
18857 current_tinst_level
= NULL
;
18859 last_pending_template
= last
;
18861 while (reconsider
);
18863 input_location
= saved_loc
;
18866 /* Substitute ARGVEC into T, which is a list of initializers for
18867 either base class or a non-static data member. The TREE_PURPOSEs
18868 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18869 instantiate_decl. */
18872 tsubst_initializer_list (tree t
, tree argvec
)
18874 tree inits
= NULL_TREE
;
18876 for (; t
; t
= TREE_CHAIN (t
))
18880 tree expanded_bases
= NULL_TREE
;
18881 tree expanded_arguments
= NULL_TREE
;
18884 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
18889 /* Expand the base class expansion type into separate base
18891 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
18892 tf_warning_or_error
,
18894 if (expanded_bases
== error_mark_node
)
18897 /* We'll be building separate TREE_LISTs of arguments for
18899 len
= TREE_VEC_LENGTH (expanded_bases
);
18900 expanded_arguments
= make_tree_vec (len
);
18901 for (i
= 0; i
< len
; i
++)
18902 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
18904 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18905 expand each argument in the TREE_VALUE of t. */
18906 expr
= make_node (EXPR_PACK_EXPANSION
);
18907 PACK_EXPANSION_LOCAL_P (expr
) = true;
18908 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
18909 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
18911 if (TREE_VALUE (t
) == void_type_node
)
18912 /* VOID_TYPE_NODE is used to indicate
18913 value-initialization. */
18915 for (i
= 0; i
< len
; i
++)
18916 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
18920 /* Substitute parameter packs into each argument in the
18922 in_base_initializer
= 1;
18923 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
18925 tree expanded_exprs
;
18927 /* Expand the argument. */
18928 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
18930 = tsubst_pack_expansion (expr
, argvec
,
18931 tf_warning_or_error
,
18933 if (expanded_exprs
== error_mark_node
)
18936 /* Prepend each of the expanded expressions to the
18937 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18938 for (i
= 0; i
< len
; i
++)
18940 TREE_VEC_ELT (expanded_arguments
, i
) =
18941 tree_cons (NULL_TREE
,
18942 TREE_VEC_ELT (expanded_exprs
, i
),
18943 TREE_VEC_ELT (expanded_arguments
, i
));
18946 in_base_initializer
= 0;
18948 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18949 since we built them backwards. */
18950 for (i
= 0; i
< len
; i
++)
18952 TREE_VEC_ELT (expanded_arguments
, i
) =
18953 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
18958 for (i
= 0; i
< len
; ++i
)
18960 if (expanded_bases
)
18962 decl
= TREE_VEC_ELT (expanded_bases
, i
);
18963 decl
= expand_member_init (decl
);
18964 init
= TREE_VEC_ELT (expanded_arguments
, i
);
18968 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
18969 tf_warning_or_error
, NULL_TREE
);
18971 decl
= expand_member_init (decl
);
18972 if (decl
&& !DECL_P (decl
))
18973 in_base_initializer
= 1;
18975 init
= TREE_VALUE (t
);
18976 if (init
!= void_type_node
)
18977 init
= tsubst_expr (init
, argvec
,
18978 tf_warning_or_error
, NULL_TREE
,
18979 /*integral_constant_expression_p=*/false);
18980 in_base_initializer
= 0;
18985 init
= build_tree_list (decl
, init
);
18986 TREE_CHAIN (init
) = inits
;
18994 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18997 set_current_access_from_decl (tree decl
)
18999 if (TREE_PRIVATE (decl
))
19000 current_access_specifier
= access_private_node
;
19001 else if (TREE_PROTECTED (decl
))
19002 current_access_specifier
= access_protected_node
;
19004 current_access_specifier
= access_public_node
;
19007 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19008 is the instantiation (which should have been created with
19009 start_enum) and ARGS are the template arguments to use. */
19012 tsubst_enum (tree tag
, tree newtag
, tree args
)
19016 if (SCOPED_ENUM_P (newtag
))
19017 begin_scope (sk_scoped_enum
, newtag
);
19019 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19024 decl
= TREE_VALUE (e
);
19025 /* Note that in a template enum, the TREE_VALUE is the
19026 CONST_DECL, not the corresponding INTEGER_CST. */
19027 value
= tsubst_expr (DECL_INITIAL (decl
),
19028 args
, tf_warning_or_error
, NULL_TREE
,
19029 /*integral_constant_expression_p=*/true);
19031 /* Give this enumeration constant the correct access. */
19032 set_current_access_from_decl (decl
);
19034 /* Actually build the enumerator itself. */
19036 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19039 if (SCOPED_ENUM_P (newtag
))
19042 finish_enum_value_list (newtag
);
19043 finish_enum (newtag
);
19045 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19046 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19049 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19050 its type -- but without substituting the innermost set of template
19051 arguments. So, innermost set of template parameters will appear in
19055 get_mostly_instantiated_function_type (tree decl
)
19063 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19064 targs
= DECL_TI_ARGS (decl
);
19065 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19066 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19068 /* There should be as many levels of arguments as there are levels
19070 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19072 fn_type
= TREE_TYPE (tmpl
);
19074 if (parm_depth
== 1)
19075 /* No substitution is necessary. */
19082 /* Replace the innermost level of the TARGS with NULL_TREEs to
19083 let tsubst know not to substitute for those parameters. */
19084 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19085 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19086 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19087 TMPL_ARGS_LEVEL (targs
, i
));
19088 SET_TMPL_ARGS_LEVEL (partial_args
,
19089 TMPL_ARGS_DEPTH (targs
),
19090 make_tree_vec (DECL_NTPARMS (tmpl
)));
19092 /* Make sure that we can see identifiers, and compute access
19094 push_access_scope (decl
);
19096 ++processing_template_decl
;
19097 /* Now, do the (partial) substitution to figure out the
19098 appropriate function type. */
19099 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
19100 --processing_template_decl
;
19102 /* Substitute into the template parameters to obtain the real
19103 innermost set of parameters. This step is important if the
19104 innermost set of template parameters contains value
19105 parameters whose types depend on outer template parameters. */
19106 TREE_VEC_LENGTH (partial_args
)--;
19107 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
19109 pop_access_scope (decl
);
19115 /* Return truthvalue if we're processing a template different from
19116 the last one involved in diagnostics. */
19118 problematic_instantiation_changed (void)
19120 return current_tinst_level
!= last_error_tinst_level
;
19123 /* Remember current template involved in diagnostics. */
19125 record_last_problematic_instantiation (void)
19127 last_error_tinst_level
= current_tinst_level
;
19130 struct tinst_level
*
19131 current_instantiation (void)
19133 return current_tinst_level
;
19136 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19137 type. Return zero for ok, nonzero for disallowed. Issue error and
19138 warning messages under control of COMPLAIN. */
19141 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19143 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19145 else if (POINTER_TYPE_P (type
))
19147 else if (TYPE_PTR_TO_MEMBER_P (type
))
19149 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19151 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19153 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19155 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19158 if (complain
& tf_error
)
19160 if (type
== error_mark_node
)
19161 inform (input_location
, "invalid template non-type parameter");
19163 error ("%q#T is not a valid type for a template non-type parameter",
19169 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19170 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19173 dependent_type_p_r (tree type
)
19179 A type is dependent if it is:
19181 -- a template parameter. Template template parameters are types
19182 for us (since TYPE_P holds true for them) so we handle
19184 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19185 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19187 /* -- a qualified-id with a nested-name-specifier which contains a
19188 class-name that names a dependent type or whose unqualified-id
19189 names a dependent type. */
19190 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19192 /* -- a cv-qualified type where the cv-unqualified type is
19194 type
= TYPE_MAIN_VARIANT (type
);
19195 /* -- a compound type constructed from any dependent type. */
19196 if (TYPE_PTR_TO_MEMBER_P (type
))
19197 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19198 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19200 else if (TREE_CODE (type
) == POINTER_TYPE
19201 || TREE_CODE (type
) == REFERENCE_TYPE
)
19202 return dependent_type_p (TREE_TYPE (type
));
19203 else if (TREE_CODE (type
) == FUNCTION_TYPE
19204 || TREE_CODE (type
) == METHOD_TYPE
)
19208 if (dependent_type_p (TREE_TYPE (type
)))
19210 for (arg_type
= TYPE_ARG_TYPES (type
);
19212 arg_type
= TREE_CHAIN (arg_type
))
19213 if (dependent_type_p (TREE_VALUE (arg_type
)))
19217 /* -- an array type constructed from any dependent type or whose
19218 size is specified by a constant expression that is
19221 We checked for type- and value-dependence of the bounds in
19222 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19223 if (TREE_CODE (type
) == ARRAY_TYPE
)
19225 if (TYPE_DOMAIN (type
)
19226 && dependent_type_p (TYPE_DOMAIN (type
)))
19228 return dependent_type_p (TREE_TYPE (type
));
19231 /* -- a template-id in which either the template name is a template
19233 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19235 /* ... or any of the template arguments is a dependent type or
19236 an expression that is type-dependent or value-dependent. */
19237 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19238 && (any_dependent_template_arguments_p
19239 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19242 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19243 dependent; if the argument of the `typeof' expression is not
19244 type-dependent, then it should already been have resolved. */
19245 if (TREE_CODE (type
) == TYPEOF_TYPE
19246 || TREE_CODE (type
) == DECLTYPE_TYPE
19247 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19250 /* A template argument pack is dependent if any of its packed
19252 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19254 tree args
= ARGUMENT_PACK_ARGS (type
);
19255 int i
, len
= TREE_VEC_LENGTH (args
);
19256 for (i
= 0; i
< len
; ++i
)
19257 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19261 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19262 be template parameters. */
19263 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19266 /* The standard does not specifically mention types that are local
19267 to template functions or local classes, but they should be
19268 considered dependent too. For example:
19270 template <int I> void f() {
19275 The size of `E' cannot be known until the value of `I' has been
19276 determined. Therefore, `E' must be considered dependent. */
19277 scope
= TYPE_CONTEXT (type
);
19278 if (scope
&& TYPE_P (scope
))
19279 return dependent_type_p (scope
);
19280 /* Don't use type_dependent_expression_p here, as it can lead
19281 to infinite recursion trying to determine whether a lambda
19282 nested in a lambda is dependent (c++/47687). */
19283 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19284 && DECL_LANG_SPECIFIC (scope
)
19285 && DECL_TEMPLATE_INFO (scope
)
19286 && (any_dependent_template_arguments_p
19287 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19290 /* Other types are non-dependent. */
19294 /* Returns TRUE if TYPE is dependent, in the sense of
19295 [temp.dep.type]. Note that a NULL type is considered dependent. */
19298 dependent_type_p (tree type
)
19300 /* If there are no template parameters in scope, then there can't be
19301 any dependent types. */
19302 if (!processing_template_decl
)
19304 /* If we are not processing a template, then nobody should be
19305 providing us with a dependent type. */
19307 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19311 /* If the type is NULL, we have not computed a type for the entity
19312 in question; in that case, the type is dependent. */
19316 /* Erroneous types can be considered non-dependent. */
19317 if (type
== error_mark_node
)
19320 /* If we have not already computed the appropriate value for TYPE,
19322 if (!TYPE_DEPENDENT_P_VALID (type
))
19324 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19325 TYPE_DEPENDENT_P_VALID (type
) = 1;
19328 return TYPE_DEPENDENT_P (type
);
19331 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19332 lookup. In other words, a dependent type that is not the current
19336 dependent_scope_p (tree scope
)
19338 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19339 && !currently_open_class (scope
));
19342 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19343 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19346 /* Note that this predicate is not appropriate for general expressions;
19347 only constant expressions (that satisfy potential_constant_expression)
19348 can be tested for value dependence.
19350 We should really also have a predicate for "instantiation-dependent".
19352 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
19353 (what about instantiation-dependent constant-expressions?)
19354 is_late_template_attribute: defer if instantiation-dependent.
19355 compute_array_index_type: proceed if constant and not t- or v-dependent
19356 if instantiation-dependent, need to remember full expression
19357 uses_template_parms: FIXME - need to audit callers
19358 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
19359 dependent_type_p [array_type]: dependent if index type is dependent
19361 static_assert - instantiation-dependent */
19364 value_dependent_expression_p (tree expression
)
19366 if (!processing_template_decl
)
19369 /* A name declared with a dependent type. */
19370 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19373 switch (TREE_CODE (expression
))
19375 case IDENTIFIER_NODE
:
19376 /* A name that has not been looked up -- must be dependent. */
19379 case TEMPLATE_PARM_INDEX
:
19380 /* A non-type template parm. */
19384 /* A non-type template parm. */
19385 if (DECL_TEMPLATE_PARM_P (expression
))
19387 return value_dependent_expression_p (DECL_INITIAL (expression
));
19390 /* A constant with literal type and is initialized
19391 with an expression that is value-dependent. */
19392 if (DECL_INITIAL (expression
)
19393 && decl_constant_var_p (expression
)
19394 && value_dependent_expression_p (DECL_INITIAL (expression
)))
19398 case DYNAMIC_CAST_EXPR
:
19399 case STATIC_CAST_EXPR
:
19400 case CONST_CAST_EXPR
:
19401 case REINTERPRET_CAST_EXPR
:
19403 /* These expressions are value-dependent if the type to which
19404 the cast occurs is dependent or the expression being casted
19405 is value-dependent. */
19407 tree type
= TREE_TYPE (expression
);
19409 if (dependent_type_p (type
))
19412 /* A functional cast has a list of operands. */
19413 expression
= TREE_OPERAND (expression
, 0);
19416 /* If there are no operands, it must be an expression such
19417 as "int()". This should not happen for aggregate types
19418 because it would form non-constant expressions. */
19419 gcc_assert (cxx_dialect
>= cxx0x
19420 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19425 if (TREE_CODE (expression
) == TREE_LIST
)
19426 return any_value_dependent_elements_p (expression
);
19428 return value_dependent_expression_p (expression
);
19434 /* A `sizeof' expression is value-dependent if the operand is
19435 type-dependent or is a pack expansion. */
19436 expression
= TREE_OPERAND (expression
, 0);
19437 if (PACK_EXPANSION_P (expression
))
19439 else if (TYPE_P (expression
))
19440 return dependent_type_p (expression
);
19441 return type_dependent_expression_p (expression
);
19443 case AT_ENCODE_EXPR
:
19444 /* An 'encode' expression is value-dependent if the operand is
19446 expression
= TREE_OPERAND (expression
, 0);
19447 return dependent_type_p (expression
);
19449 case NOEXCEPT_EXPR
:
19450 expression
= TREE_OPERAND (expression
, 0);
19451 return type_dependent_expression_p (expression
);
19455 tree name
= TREE_OPERAND (expression
, 1);
19456 return value_dependent_expression_p (name
);
19459 case COMPONENT_REF
:
19460 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19461 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19463 case NONTYPE_ARGUMENT_PACK
:
19464 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19465 is value-dependent. */
19467 tree values
= ARGUMENT_PACK_ARGS (expression
);
19468 int i
, len
= TREE_VEC_LENGTH (values
);
19470 for (i
= 0; i
< len
; ++i
)
19471 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19479 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19480 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19481 || (type2
? dependent_type_p (type2
) : false));
19485 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19486 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19489 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19490 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19494 tree op
= TREE_OPERAND (expression
, 0);
19495 return (value_dependent_expression_p (op
)
19496 || has_value_dependent_address (op
));
19501 tree fn
= get_callee_fndecl (expression
);
19503 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19505 nargs
= call_expr_nargs (expression
);
19506 for (i
= 0; i
< nargs
; ++i
)
19508 tree op
= CALL_EXPR_ARG (expression
, i
);
19509 /* In a call to a constexpr member function, look through the
19510 implicit ADDR_EXPR on the object argument so that it doesn't
19511 cause the call to be considered value-dependent. We also
19512 look through it in potential_constant_expression. */
19513 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19514 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19515 && TREE_CODE (op
) == ADDR_EXPR
)
19516 op
= TREE_OPERAND (op
, 0);
19517 if (value_dependent_expression_p (op
))
19523 case TEMPLATE_ID_EXPR
:
19524 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19526 return type_dependent_expression_p (expression
);
19532 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19533 if (value_dependent_expression_p (val
))
19539 /* Treat a GNU statement expression as dependent to avoid crashing
19540 under fold_non_dependent_expr; it can't be constant. */
19544 /* A constant expression is value-dependent if any subexpression is
19545 value-dependent. */
19546 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19548 case tcc_reference
:
19550 case tcc_comparison
:
19552 case tcc_expression
:
19555 int i
, len
= cp_tree_operand_length (expression
);
19557 for (i
= 0; i
< len
; i
++)
19559 tree t
= TREE_OPERAND (expression
, i
);
19561 /* In some cases, some of the operands may be missing.l
19562 (For example, in the case of PREDECREMENT_EXPR, the
19563 amount to increment by may be missing.) That doesn't
19564 make the expression dependent. */
19565 if (t
&& value_dependent_expression_p (t
))
19576 /* The expression is not value-dependent. */
19580 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19581 [temp.dep.expr]. Note that an expression with no type is
19582 considered dependent. Other parts of the compiler arrange for an
19583 expression with type-dependent subexpressions to have no type, so
19584 this function doesn't have to be fully recursive. */
19587 type_dependent_expression_p (tree expression
)
19589 if (!processing_template_decl
)
19592 if (expression
== error_mark_node
)
19595 /* An unresolved name is always dependent. */
19596 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19597 || TREE_CODE (expression
) == USING_DECL
)
19600 /* Some expression forms are never type-dependent. */
19601 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19602 || TREE_CODE (expression
) == SIZEOF_EXPR
19603 || TREE_CODE (expression
) == ALIGNOF_EXPR
19604 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19605 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19606 || TREE_CODE (expression
) == TRAIT_EXPR
19607 || TREE_CODE (expression
) == TYPEID_EXPR
19608 || TREE_CODE (expression
) == DELETE_EXPR
19609 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19610 || TREE_CODE (expression
) == THROW_EXPR
)
19613 /* The types of these expressions depends only on the type to which
19614 the cast occurs. */
19615 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19616 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19617 || TREE_CODE (expression
) == CONST_CAST_EXPR
19618 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19619 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19620 || TREE_CODE (expression
) == CAST_EXPR
)
19621 return dependent_type_p (TREE_TYPE (expression
));
19623 /* The types of these expressions depends only on the type created
19624 by the expression. */
19625 if (TREE_CODE (expression
) == NEW_EXPR
19626 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19628 /* For NEW_EXPR tree nodes created inside a template, either
19629 the object type itself or a TREE_LIST may appear as the
19631 tree type
= TREE_OPERAND (expression
, 1);
19632 if (TREE_CODE (type
) == TREE_LIST
)
19633 /* This is an array type. We need to check array dimensions
19635 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19636 || value_dependent_expression_p
19637 (TREE_OPERAND (TREE_VALUE (type
), 1));
19639 return dependent_type_p (type
);
19642 if (TREE_CODE (expression
) == SCOPE_REF
)
19644 tree scope
= TREE_OPERAND (expression
, 0);
19645 tree name
= TREE_OPERAND (expression
, 1);
19647 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19648 contains an identifier associated by name lookup with one or more
19649 declarations declared with a dependent type, or...a
19650 nested-name-specifier or qualified-id that names a member of an
19651 unknown specialization. */
19652 return (type_dependent_expression_p (name
)
19653 || dependent_scope_p (scope
));
19656 if (TREE_CODE (expression
) == FUNCTION_DECL
19657 && DECL_LANG_SPECIFIC (expression
)
19658 && DECL_TEMPLATE_INFO (expression
)
19659 && (any_dependent_template_arguments_p
19660 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19663 if (TREE_CODE (expression
) == TEMPLATE_DECL
19664 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19667 if (TREE_CODE (expression
) == STMT_EXPR
)
19668 expression
= stmt_expr_value_expr (expression
);
19670 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19675 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19677 if (type_dependent_expression_p (elt
))
19683 /* A static data member of the current instantiation with incomplete
19684 array type is type-dependent, as the definition and specializations
19685 can have different bounds. */
19686 if (TREE_CODE (expression
) == VAR_DECL
19687 && DECL_CLASS_SCOPE_P (expression
)
19688 && dependent_type_p (DECL_CONTEXT (expression
))
19689 && VAR_HAD_UNKNOWN_BOUND (expression
))
19692 if (TREE_TYPE (expression
) == unknown_type_node
)
19694 if (TREE_CODE (expression
) == ADDR_EXPR
)
19695 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19696 if (TREE_CODE (expression
) == COMPONENT_REF
19697 || TREE_CODE (expression
) == OFFSET_REF
)
19699 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19701 expression
= TREE_OPERAND (expression
, 1);
19702 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19705 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19706 if (TREE_CODE (expression
) == SCOPE_REF
)
19709 if (BASELINK_P (expression
))
19710 expression
= BASELINK_FUNCTIONS (expression
);
19712 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19714 if (any_dependent_template_arguments_p
19715 (TREE_OPERAND (expression
, 1)))
19717 expression
= TREE_OPERAND (expression
, 0);
19719 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19720 || TREE_CODE (expression
) == FUNCTION_DECL
);
19724 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19726 expression
= OVL_NEXT (expression
);
19731 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19733 return (dependent_type_p (TREE_TYPE (expression
)));
19736 /* Like type_dependent_expression_p, but it also works while not processing
19737 a template definition, i.e. during substitution or mangling. */
19740 type_dependent_expression_p_push (tree expr
)
19743 ++processing_template_decl
;
19744 b
= type_dependent_expression_p (expr
);
19745 --processing_template_decl
;
19749 /* Returns TRUE if ARGS contains a type-dependent expression. */
19752 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
19757 FOR_EACH_VEC_ELT (tree
, args
, i
, arg
)
19759 if (type_dependent_expression_p (arg
))
19765 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19766 expressions) contains any type-dependent expressions. */
19769 any_type_dependent_elements_p (const_tree list
)
19771 for (; list
; list
= TREE_CHAIN (list
))
19772 if (value_dependent_expression_p (TREE_VALUE (list
)))
19778 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19779 expressions) contains any value-dependent expressions. */
19782 any_value_dependent_elements_p (const_tree list
)
19784 for (; list
; list
= TREE_CHAIN (list
))
19785 if (value_dependent_expression_p (TREE_VALUE (list
)))
19791 /* Returns TRUE if the ARG (a template argument) is dependent. */
19794 dependent_template_arg_p (tree arg
)
19796 if (!processing_template_decl
)
19799 /* Assume a template argument that was wrongly written by the user
19800 is dependent. This is consistent with what
19801 any_dependent_template_arguments_p [that calls this function]
19803 if (!arg
|| arg
== error_mark_node
)
19806 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
19807 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
19809 if (TREE_CODE (arg
) == TEMPLATE_DECL
19810 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19811 return dependent_template_p (arg
);
19812 else if (ARGUMENT_PACK_P (arg
))
19814 tree args
= ARGUMENT_PACK_ARGS (arg
);
19815 int i
, len
= TREE_VEC_LENGTH (args
);
19816 for (i
= 0; i
< len
; ++i
)
19818 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19824 else if (TYPE_P (arg
))
19825 return dependent_type_p (arg
);
19827 return (type_dependent_expression_p (arg
)
19828 || value_dependent_expression_p (arg
));
19831 /* Returns true if ARGS (a collection of template arguments) contains
19832 any types that require structural equality testing. */
19835 any_template_arguments_need_structural_equality_p (tree args
)
19842 if (args
== error_mark_node
)
19845 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19847 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19848 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19850 tree arg
= TREE_VEC_ELT (level
, j
);
19851 tree packed_args
= NULL_TREE
;
19854 if (ARGUMENT_PACK_P (arg
))
19856 /* Look inside the argument pack. */
19857 packed_args
= ARGUMENT_PACK_ARGS (arg
);
19858 len
= TREE_VEC_LENGTH (packed_args
);
19861 for (k
= 0; k
< len
; ++k
)
19864 arg
= TREE_VEC_ELT (packed_args
, k
);
19866 if (error_operand_p (arg
))
19868 else if (TREE_CODE (arg
) == TEMPLATE_DECL
19869 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19871 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
19873 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
19874 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
19883 /* Returns true if ARGS (a collection of template arguments) contains
19884 any dependent arguments. */
19887 any_dependent_template_arguments_p (const_tree args
)
19894 if (args
== error_mark_node
)
19897 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19899 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19900 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19901 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
19908 /* Returns TRUE if the template TMPL is dependent. */
19911 dependent_template_p (tree tmpl
)
19913 if (TREE_CODE (tmpl
) == OVERLOAD
)
19917 if (dependent_template_p (OVL_CURRENT (tmpl
)))
19919 tmpl
= OVL_NEXT (tmpl
);
19924 /* Template template parameters are dependent. */
19925 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
19926 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
19928 /* So are names that have not been looked up. */
19929 if (TREE_CODE (tmpl
) == SCOPE_REF
19930 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
19932 /* So are member templates of dependent classes. */
19933 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
19934 return dependent_type_p (DECL_CONTEXT (tmpl
));
19938 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19941 dependent_template_id_p (tree tmpl
, tree args
)
19943 return (dependent_template_p (tmpl
)
19944 || any_dependent_template_arguments_p (args
));
19947 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19951 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
19955 if (!processing_template_decl
)
19958 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
19960 tree decl
= TREE_VEC_ELT (declv
, i
);
19961 tree init
= TREE_VEC_ELT (initv
, i
);
19962 tree cond
= TREE_VEC_ELT (condv
, i
);
19963 tree incr
= TREE_VEC_ELT (incrv
, i
);
19965 if (type_dependent_expression_p (decl
))
19968 if (init
&& type_dependent_expression_p (init
))
19971 if (type_dependent_expression_p (cond
))
19974 if (COMPARISON_CLASS_P (cond
)
19975 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
19976 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
19979 if (TREE_CODE (incr
) == MODOP_EXPR
)
19981 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
19982 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
19985 else if (type_dependent_expression_p (incr
))
19987 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
19989 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
19991 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
19993 tree t
= TREE_OPERAND (incr
, 1);
19994 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
19995 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
20004 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20005 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20006 no such TYPE can be found. Note that this function peers inside
20007 uninstantiated templates and therefore should be used only in
20008 extremely limited situations. ONLY_CURRENT_P restricts this
20009 peering to the currently open classes hierarchy (which is required
20010 when comparing types). */
20013 resolve_typename_type (tree type
, bool only_current_p
)
20022 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
20024 scope
= TYPE_CONTEXT (type
);
20025 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20026 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20027 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20028 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20029 identifier of the TYPENAME_TYPE anymore.
20030 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20031 TYPENAME_TYPE instead, we avoid messing up with a possible
20032 typedef variant case. */
20033 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
20035 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20036 it first before we can figure out what NAME refers to. */
20037 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20038 scope
= resolve_typename_type (scope
, only_current_p
);
20039 /* If we don't know what SCOPE refers to, then we cannot resolve the
20041 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20043 /* If the SCOPE is a template type parameter, we have no way of
20044 resolving the name. */
20045 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20047 /* If the SCOPE is not the current instantiation, there's no reason
20048 to look inside it. */
20049 if (only_current_p
&& !currently_open_class (scope
))
20051 /* If this is a typedef, we don't want to look inside (c++/11987). */
20052 if (typedef_variant_p (type
))
20054 /* If SCOPE isn't the template itself, it will not have a valid
20055 TYPE_FIELDS list. */
20056 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20057 /* scope is either the template itself or a compatible instantiation
20058 like X<T>, so look up the name in the original template. */
20059 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20061 /* scope is a partial instantiation, so we can't do the lookup or we
20062 will lose the template arguments. */
20064 /* Enter the SCOPE so that name lookup will be resolved as if we
20065 were in the class definition. In particular, SCOPE will no
20066 longer be considered a dependent type. */
20067 pushed_scope
= push_scope (scope
);
20068 /* Look up the declaration. */
20069 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20070 tf_warning_or_error
);
20072 result
= NULL_TREE
;
20074 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20075 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20078 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
20079 && TREE_CODE (decl
) == TYPE_DECL
)
20081 result
= TREE_TYPE (decl
);
20082 if (result
== error_mark_node
)
20083 result
= NULL_TREE
;
20085 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20086 && DECL_CLASS_TEMPLATE_P (decl
))
20090 /* Obtain the template and the arguments. */
20091 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20092 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20093 /* Instantiate the template. */
20094 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20095 /*entering_scope=*/0,
20096 tf_error
| tf_user
);
20097 if (result
== error_mark_node
)
20098 result
= NULL_TREE
;
20101 /* Leave the SCOPE. */
20103 pop_scope (pushed_scope
);
20105 /* If we failed to resolve it, return the original typename. */
20109 /* If lookup found a typename type, resolve that too. */
20110 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20112 /* Ill-formed programs can cause infinite recursion here, so we
20113 must catch that. */
20114 TYPENAME_IS_RESOLVING_P (type
) = 1;
20115 result
= resolve_typename_type (result
, only_current_p
);
20116 TYPENAME_IS_RESOLVING_P (type
) = 0;
20119 /* Qualify the resulting type. */
20120 quals
= cp_type_quals (type
);
20122 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20127 /* EXPR is an expression which is not type-dependent. Return a proxy
20128 for EXPR that can be used to compute the types of larger
20129 expressions containing EXPR. */
20132 build_non_dependent_expr (tree expr
)
20136 #ifdef ENABLE_CHECKING
20137 /* Try to get a constant value for all non-type-dependent expressions in
20138 order to expose bugs in *_dependent_expression_p and constexpr. */
20139 if (cxx_dialect
>= cxx0x
)
20140 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20143 /* Preserve OVERLOADs; the functions must be available to resolve
20146 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20147 inner_expr
= stmt_expr_value_expr (inner_expr
);
20148 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20149 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20150 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20151 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20152 if (is_overloaded_fn (inner_expr
)
20153 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20155 /* There is no need to return a proxy for a variable. */
20156 if (TREE_CODE (expr
) == VAR_DECL
)
20158 /* Preserve string constants; conversions from string constants to
20159 "char *" are allowed, even though normally a "const char *"
20160 cannot be used to initialize a "char *". */
20161 if (TREE_CODE (expr
) == STRING_CST
)
20163 /* Preserve arithmetic constants, as an optimization -- there is no
20164 reason to create a new node. */
20165 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20167 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20168 There is at least one place where we want to know that a
20169 particular expression is a throw-expression: when checking a ?:
20170 expression, there are special rules if the second or third
20171 argument is a throw-expression. */
20172 if (TREE_CODE (expr
) == THROW_EXPR
)
20175 /* Don't wrap an initializer list, we need to be able to look inside. */
20176 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20179 if (TREE_CODE (expr
) == COND_EXPR
)
20180 return build3 (COND_EXPR
,
20182 TREE_OPERAND (expr
, 0),
20183 (TREE_OPERAND (expr
, 1)
20184 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
20185 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
20186 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
20187 if (TREE_CODE (expr
) == COMPOUND_EXPR
20188 && !COMPOUND_EXPR_OVERLOADED (expr
))
20189 return build2 (COMPOUND_EXPR
,
20191 TREE_OPERAND (expr
, 0),
20192 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
20194 /* If the type is unknown, it can't really be non-dependent */
20195 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
20197 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20198 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
20201 /* ARGS is a vector of expressions as arguments to a function call.
20202 Replace the arguments with equivalent non-dependent expressions.
20203 This modifies ARGS in place. */
20206 make_args_non_dependent (VEC(tree
,gc
) *args
)
20211 FOR_EACH_VEC_ELT (tree
, args
, ix
, arg
)
20213 tree newarg
= build_non_dependent_expr (arg
);
20215 VEC_replace (tree
, args
, ix
, newarg
);
20219 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20220 with a level one deeper than the actual template parms. */
20225 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
20226 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
20227 TYPE_DECL
, get_identifier ("auto"), au
);
20228 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
20229 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
20230 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
20231 0, TYPE_NAME (au
), NULL_TREE
);
20232 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
20233 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
20234 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
20239 /* Given type ARG, return std::initializer_list<ARG>. */
20244 tree std_init_list
= namespace_binding
20245 (get_identifier ("initializer_list"), std_node
);
20247 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
20249 error ("deducing from brace-enclosed initializer list requires "
20250 "#include <initializer_list>");
20251 return error_mark_node
;
20253 argvec
= make_tree_vec (1);
20254 TREE_VEC_ELT (argvec
, 0) = arg
;
20255 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
20256 NULL_TREE
, 0, tf_warning_or_error
);
20259 /* Replace auto in TYPE with std::initializer_list<auto>. */
20262 listify_autos (tree type
, tree auto_node
)
20264 tree init_auto
= listify (auto_node
);
20265 tree argvec
= make_tree_vec (1);
20266 TREE_VEC_ELT (argvec
, 0) = init_auto
;
20267 if (processing_template_decl
)
20268 argvec
= add_to_template_args (current_template_args (), argvec
);
20269 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20272 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20273 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20276 do_auto_deduction (tree type
, tree init
, tree auto_node
)
20278 tree parms
, tparms
, targs
;
20282 if (init
== error_mark_node
)
20283 return error_mark_node
;
20285 if (processing_template_decl
20286 && (TREE_TYPE (init
) == NULL_TREE
20287 || BRACE_ENCLOSED_INITIALIZER_P (init
)))
20288 /* Not enough information to try this yet. */
20291 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20292 with either a new invented type template parameter U or, if the
20293 initializer is a braced-init-list (8.5.4), with
20294 std::initializer_list<U>. */
20295 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
20296 type
= listify_autos (type
, auto_node
);
20298 init
= resolve_nondeduced_context (init
);
20300 parms
= build_tree_list (NULL_TREE
, type
);
20302 tparms
= make_tree_vec (1);
20303 targs
= make_tree_vec (1);
20304 TREE_VEC_ELT (tparms
, 0)
20305 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20306 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20307 DEDUCE_CALL
, LOOKUP_NORMAL
,
20308 /*explain_p=*/false);
20311 if (processing_template_decl
)
20312 /* Try again at instantiation time. */
20314 if (type
&& type
!= error_mark_node
)
20315 /* If type is error_mark_node a diagnostic must have been
20316 emitted by now. Also, having a mention to '<type error>'
20317 in the diagnostic is not really useful to the user. */
20319 if (cfun
&& auto_node
== current_function_auto_return_pattern
20320 && LAMBDA_FUNCTION_P (current_function_decl
))
20321 error ("unable to deduce lambda return type from %qE", init
);
20323 error ("unable to deduce %qT from %qE", type
, init
);
20325 return error_mark_node
;
20328 /* If the list of declarators contains more than one declarator, the type
20329 of each declared variable is determined as described above. If the
20330 type deduced for the template parameter U is not the same in each
20331 deduction, the program is ill-formed. */
20332 if (TREE_TYPE (auto_node
)
20333 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20335 if (cfun
&& auto_node
== current_function_auto_return_pattern
20336 && LAMBDA_FUNCTION_P (current_function_decl
))
20337 error ("inconsistent types %qT and %qT deduced for "
20338 "lambda return type", TREE_TYPE (auto_node
),
20339 TREE_VEC_ELT (targs
, 0));
20341 error ("inconsistent deduction for %qT: %qT and then %qT",
20342 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20343 return error_mark_node
;
20345 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20347 if (processing_template_decl
)
20348 targs
= add_to_template_args (current_template_args (), targs
);
20349 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20352 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20356 splice_late_return_type (tree type
, tree late_return_type
)
20360 if (late_return_type
== NULL_TREE
)
20362 argvec
= make_tree_vec (1);
20363 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20364 if (processing_template_parmlist
)
20365 /* For a late-specified return type in a template type-parameter, we
20366 need to add a dummy argument level for its parmlist. */
20367 argvec
= add_to_template_args
20368 (make_tree_vec (processing_template_parmlist
), argvec
);
20369 if (current_template_parms
)
20370 argvec
= add_to_template_args (current_template_args (), argvec
);
20371 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20374 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20377 is_auto (const_tree type
)
20379 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20380 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20386 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20387 appear as a type-specifier for the declaration in question, we don't
20388 have to look through the whole type. */
20391 type_uses_auto (tree type
)
20393 enum tree_code code
;
20394 if (is_auto (type
))
20397 code
= TREE_CODE (type
);
20399 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20400 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20401 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20402 return type_uses_auto (TREE_TYPE (type
));
20404 if (TYPE_PTRMEMFUNC_P (type
))
20405 return type_uses_auto (TREE_TYPE (TREE_TYPE
20406 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20411 /* For a given template T, return the vector of typedefs referenced
20412 in T for which access check is needed at T instantiation time.
20413 T is either a FUNCTION_DECL or a RECORD_TYPE.
20414 Those typedefs were added to T by the function
20415 append_type_to_template_for_access_check. */
20417 VEC(qualified_typedef_usage_t
,gc
)*
20418 get_types_needing_access_check (tree t
)
20421 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
20423 if (!t
|| t
== error_mark_node
)
20426 if (!(ti
= get_template_info (t
)))
20429 if (CLASS_TYPE_P (t
)
20430 || TREE_CODE (t
) == FUNCTION_DECL
)
20432 if (!TI_TEMPLATE (ti
))
20435 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20441 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20442 tied to T. That list of typedefs will be access checked at
20443 T instantiation time.
20444 T is either a FUNCTION_DECL or a RECORD_TYPE.
20445 TYPE_DECL is a TYPE_DECL node representing a typedef.
20446 SCOPE is the scope through which TYPE_DECL is accessed.
20447 LOCATION is the location of the usage point of TYPE_DECL.
20449 This function is a subroutine of
20450 append_type_to_template_for_access_check. */
20453 append_type_to_template_for_access_check_1 (tree t
,
20456 location_t location
)
20458 qualified_typedef_usage_t typedef_usage
;
20461 if (!t
|| t
== error_mark_node
)
20464 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20465 || CLASS_TYPE_P (t
))
20467 && TREE_CODE (type_decl
) == TYPE_DECL
20470 if (!(ti
= get_template_info (t
)))
20473 gcc_assert (TI_TEMPLATE (ti
));
20475 typedef_usage
.typedef_decl
= type_decl
;
20476 typedef_usage
.context
= scope
;
20477 typedef_usage
.locus
= location
;
20479 VEC_safe_push (qualified_typedef_usage_t
, gc
,
20480 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
20484 /* Append TYPE_DECL to the template TEMPL.
20485 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20486 At TEMPL instanciation time, TYPE_DECL will be checked to see
20487 if it can be accessed through SCOPE.
20488 LOCATION is the location of the usage point of TYPE_DECL.
20490 e.g. consider the following code snippet:
20497 template<class U> struct S
20499 C::myint mi; // <-- usage point of the typedef C::myint
20504 At S<char> instantiation time, we need to check the access of C::myint
20505 In other words, we need to check the access of the myint typedef through
20506 the C scope. For that purpose, this function will add the myint typedef
20507 and the scope C through which its being accessed to a list of typedefs
20508 tied to the template S. That list will be walked at template instantiation
20509 time and access check performed on each typedefs it contains.
20510 Note that this particular code snippet should yield an error because
20511 myint is private to C. */
20514 append_type_to_template_for_access_check (tree templ
,
20517 location_t location
)
20519 qualified_typedef_usage_t
*iter
;
20522 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20524 /* Make sure we don't append the type to the template twice. */
20525 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
20526 get_types_needing_access_check (templ
),
20528 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20531 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20535 /* Set up the hash tables for template instantiations. */
20538 init_template_processing (void)
20540 decl_specializations
= htab_create_ggc (37,
20541 hash_specialization
,
20542 eq_specializations
,
20544 type_specializations
= htab_create_ggc (37,
20545 hash_specialization
,
20546 eq_specializations
,
20550 /* Print stats about the template hash tables for -fstats. */
20553 print_template_statistics (void)
20555 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20556 "%f collisions\n", (long) htab_size (decl_specializations
),
20557 (long) htab_elements (decl_specializations
),
20558 htab_collisions (decl_specializations
));
20559 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20560 "%f collisions\n", (long) htab_size (type_specializations
),
20561 (long) htab_elements (type_specializations
),
20562 htab_collisions (type_specializations
));
20565 #include "gt-cp-pt.h"