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"
45 #include "tree-iterator.h"
48 /* The type of functions taking a tree, and some additional data, and
50 typedef int (*tree_fn_t
) (tree
, void*);
52 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
53 instantiations have been deferred, either because their definitions
54 were not yet available, or because we were putting off doing the work. */
55 struct GTY ((chain_next ("%h.next"))) pending_template
{
56 struct pending_template
*next
;
57 struct tinst_level
*tinst
;
60 static GTY(()) struct pending_template
*pending_templates
;
61 static GTY(()) struct pending_template
*last_pending_template
;
63 int processing_template_parmlist
;
64 static int template_header_count
;
66 static GTY(()) tree saved_trees
;
67 static VEC(int,heap
) *inline_parm_levels
;
69 static GTY(()) struct tinst_level
*current_tinst_level
;
71 static GTY(()) tree saved_access_scope
;
73 /* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76 static tree cur_stmt_expr
;
78 /* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
81 static struct pointer_map_t
*local_specializations
;
83 /* True if we've recursed into fn_type_unification too many times. */
84 static bool excessive_deduction_depth
;
86 typedef struct GTY(()) spec_entry
93 static GTY ((param_is (spec_entry
)))
94 htab_t decl_specializations
;
96 static GTY ((param_is (spec_entry
)))
97 htab_t type_specializations
;
99 /* Contains canonical template parameter types. The vector is indexed by
100 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
101 TREE_LIST, whose TREE_VALUEs contain the canonical template
102 parameters of various types and levels. */
103 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
105 #define UNIFY_ALLOW_NONE 0
106 #define UNIFY_ALLOW_MORE_CV_QUAL 1
107 #define UNIFY_ALLOW_LESS_CV_QUAL 2
108 #define UNIFY_ALLOW_DERIVED 4
109 #define UNIFY_ALLOW_INTEGER 8
110 #define UNIFY_ALLOW_OUTER_LEVEL 16
111 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
112 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
114 enum template_base_result
{
116 tbr_ambiguous_baseclass
,
120 static void push_access_scope (tree
);
121 static void pop_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
, 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, 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
, 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
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
208 static tree
instantiate_alias_template (tree
, tree
, tsubst_flags_t
);
210 /* Make the current scope suitable for access checking when we are
211 processing T. T can be FUNCTION_DECL for instantiated function
212 template, VAR_DECL for static member variable, or TYPE_DECL for
213 alias template (needed by instantiate_decl). */
216 push_access_scope (tree t
)
218 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
219 || TREE_CODE (t
) == TYPE_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 of 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_Wpedantic
, "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 (tmpl
);
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
);
1726 tree cand
= OVL_CURRENT (fn
);
1729 /* Pick the prefix string. */
1730 if (!more
&& !OVL_NEXT (fns
))
1732 inform (DECL_SOURCE_LOCATION (cand
),
1733 "candidate is: %#D", cand
);
1737 *str
= _("candidates are:");
1738 spaces
= get_spaces (*str
);
1740 inform (DECL_SOURCE_LOCATION (cand
), "%s %#D", *str
, cand
);
1741 *str
= spaces
? spaces
: *str
;
1751 /* Print the list of candidate FNS in an error message. FNS can also
1752 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1755 print_candidates (tree fns
)
1757 const char *str
= NULL
;
1758 print_candidates_1 (fns
, false, &str
);
1759 gcc_assert (str
== NULL
);
1762 /* Returns the template (one of the functions given by TEMPLATE_ID)
1763 which can be specialized to match the indicated DECL with the
1764 explicit template args given in TEMPLATE_ID. The DECL may be
1765 NULL_TREE if none is available. In that case, the functions in
1766 TEMPLATE_ID are non-members.
1768 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1769 specialization of a member template.
1771 The TEMPLATE_COUNT is the number of references to qualifying
1772 template classes that appeared in the name of the function. See
1773 check_explicit_specialization for a more accurate description.
1775 TSK indicates what kind of template declaration (if any) is being
1776 declared. TSK_TEMPLATE indicates that the declaration given by
1777 DECL, though a FUNCTION_DECL, has template parameters, and is
1778 therefore a template function.
1780 The template args (those explicitly specified and those deduced)
1781 are output in a newly created vector *TARGS_OUT.
1783 If it is impossible to determine the result, an error message is
1784 issued. The error_mark_node is returned to indicate failure. */
1787 determine_specialization (tree template_id
,
1790 int need_member_template
,
1796 tree explicit_targs
;
1797 tree candidates
= NULL_TREE
;
1798 /* A TREE_LIST of templates of which DECL may be a specialization.
1799 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1800 corresponding TREE_PURPOSE is the set of template arguments that,
1801 when used to instantiate the template, would produce a function
1802 with the signature of DECL. */
1803 tree templates
= NULL_TREE
;
1805 cp_binding_level
*b
;
1807 *targs_out
= NULL_TREE
;
1809 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1810 return error_mark_node
;
1812 /* We shouldn't be specializing a member template of an
1813 unspecialized class template; we already gave an error in
1814 check_specialization_scope, now avoid crashing. */
1815 if (template_count
&& DECL_CLASS_SCOPE_P (decl
)
1816 && template_class_depth (DECL_CONTEXT (decl
)) > 0)
1818 gcc_assert (errorcount
);
1819 return error_mark_node
;
1822 fns
= TREE_OPERAND (template_id
, 0);
1823 explicit_targs
= TREE_OPERAND (template_id
, 1);
1825 if (fns
== error_mark_node
)
1826 return error_mark_node
;
1828 /* Check for baselinks. */
1829 if (BASELINK_P (fns
))
1830 fns
= BASELINK_FUNCTIONS (fns
);
1832 if (!is_overloaded_fn (fns
))
1834 error ("%qD is not a function template", fns
);
1835 return error_mark_node
;
1838 /* Count the number of template headers specified for this
1841 for (b
= current_binding_level
;
1842 b
->kind
== sk_template_parms
;
1846 for (; fns
; fns
= OVL_NEXT (fns
))
1848 tree fn
= OVL_CURRENT (fns
);
1850 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1852 tree decl_arg_types
;
1856 /* In case of explicit specialization, we need to check if
1857 the number of template headers appearing in the specialization
1858 is correct. This is usually done in check_explicit_specialization,
1859 but the check done there cannot be exhaustive when specializing
1860 member functions. Consider the following code:
1862 template <> void A<int>::f(int);
1863 template <> template <> void A<int>::f(int);
1865 Assuming that A<int> is not itself an explicit specialization
1866 already, the first line specializes "f" which is a non-template
1867 member function, whilst the second line specializes "f" which
1868 is a template member function. So both lines are syntactically
1869 correct, and check_explicit_specialization does not reject
1872 Here, we can do better, as we are matching the specialization
1873 against the declarations. We count the number of template
1874 headers, and we check if they match TEMPLATE_COUNT + 1
1875 (TEMPLATE_COUNT is the number of qualifying template classes,
1876 plus there must be another header for the member template
1879 Notice that if header_count is zero, this is not a
1880 specialization but rather a template instantiation, so there
1881 is no check we can perform here. */
1882 if (header_count
&& header_count
!= template_count
+ 1)
1885 /* Check that the number of template arguments at the
1886 innermost level for DECL is the same as for FN. */
1887 if (current_binding_level
->kind
== sk_template_parms
1888 && !current_binding_level
->explicit_spec_p
1889 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1890 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1891 (current_template_parms
))))
1894 /* DECL might be a specialization of FN. */
1895 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1896 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1898 /* For a non-static member function, we need to make sure
1899 that the const qualification is the same. Since
1900 get_bindings does not try to merge the "this" parameter,
1901 we must do the comparison explicitly. */
1902 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1903 && !same_type_p (TREE_VALUE (fn_arg_types
),
1904 TREE_VALUE (decl_arg_types
)))
1907 /* Skip the "this" parameter and, for constructors of
1908 classes with virtual bases, the VTT parameter. A
1909 full specialization of a constructor will have a VTT
1910 parameter, but a template never will. */
1912 = skip_artificial_parms_for (decl
, decl_arg_types
);
1914 = skip_artificial_parms_for (fn
, fn_arg_types
);
1916 /* Function templates cannot be specializations; there are
1917 no partial specializations of functions. Therefore, if
1918 the type of DECL does not match FN, there is no
1920 if (tsk
== tsk_template
)
1922 if (compparms (fn_arg_types
, decl_arg_types
))
1923 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1927 /* See whether this function might be a specialization of this
1928 template. Suppress access control because we might be trying
1929 to make this specialization a friend, and we have already done
1930 access control for the declaration of the specialization. */
1931 push_deferring_access_checks (dk_no_check
);
1932 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1933 pop_deferring_access_checks ();
1936 /* We cannot deduce template arguments that when used to
1937 specialize TMPL will produce DECL. */
1940 /* Make sure that the deduced arguments actually work. */
1941 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1942 if (insttype
== error_mark_node
)
1945 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1946 if (!compparms (fn_arg_types
, decl_arg_types
))
1949 /* Save this template, and the arguments deduced. */
1950 templates
= tree_cons (targs
, fn
, templates
);
1952 else if (need_member_template
)
1953 /* FN is an ordinary member function, and we need a
1954 specialization of a member template. */
1956 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1957 /* We can get IDENTIFIER_NODEs here in certain erroneous
1960 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1961 /* This is just an ordinary non-member function. Nothing can
1962 be a specialization of that. */
1964 else if (DECL_ARTIFICIAL (fn
))
1965 /* Cannot specialize functions that are created implicitly. */
1969 tree decl_arg_types
;
1971 /* This is an ordinary member function. However, since
1972 we're here, we can assume it's enclosing class is a
1973 template class. For example,
1975 template <typename T> struct S { void f(); };
1976 template <> void S<int>::f() {}
1978 Here, S<int>::f is a non-template, but S<int> is a
1979 template class. If FN has the same type as DECL, we
1980 might be in business. */
1982 if (!DECL_TEMPLATE_INFO (fn
))
1983 /* Its enclosing class is an explicit specialization
1984 of a template class. This is not a candidate. */
1987 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1988 TREE_TYPE (TREE_TYPE (fn
))))
1989 /* The return types differ. */
1992 /* Adjust the type of DECL in case FN is a static member. */
1993 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1994 if (DECL_STATIC_FUNCTION_P (fn
)
1995 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1996 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1998 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2001 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
2005 if (templates
&& TREE_CHAIN (templates
))
2011 It is possible for a specialization with a given function
2012 signature to be instantiated from more than one function
2013 template. In such cases, explicit specification of the
2014 template arguments must be used to uniquely identify the
2015 function template specialization being specialized.
2017 Note that here, there's no suggestion that we're supposed to
2018 determine which of the candidate templates is most
2019 specialized. However, we, also have:
2023 Partial ordering of overloaded function template
2024 declarations is used in the following contexts to select
2025 the function template to which a function template
2026 specialization refers:
2028 -- when an explicit specialization refers to a function
2031 So, we do use the partial ordering rules, at least for now.
2032 This extension can only serve to make invalid programs valid,
2033 so it's safe. And, there is strong anecdotal evidence that
2034 the committee intended the partial ordering rules to apply;
2035 the EDG front end has that behavior, and John Spicer claims
2036 that the committee simply forgot to delete the wording in
2037 [temp.expl.spec]. */
2038 tree tmpl
= most_specialized_instantiation (templates
);
2039 if (tmpl
!= error_mark_node
)
2042 TREE_CHAIN (templates
) = NULL_TREE
;
2046 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2048 error ("template-id %qD for %q+D does not match any template "
2049 "declaration", template_id
, decl
);
2050 if (header_count
&& header_count
!= template_count
+ 1)
2051 inform (input_location
, "saw %d %<template<>%>, need %d for "
2052 "specializing a member function template",
2053 header_count
, template_count
+ 1);
2054 return error_mark_node
;
2056 else if ((templates
&& TREE_CHAIN (templates
))
2057 || (candidates
&& TREE_CHAIN (candidates
))
2058 || (templates
&& candidates
))
2060 error ("ambiguous template specialization %qD for %q+D",
2062 candidates
= chainon (candidates
, templates
);
2063 print_candidates (candidates
);
2064 return error_mark_node
;
2067 /* We have one, and exactly one, match. */
2070 tree fn
= TREE_VALUE (candidates
);
2071 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2072 /* DECL is a re-declaration or partial instantiation of a template
2074 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2076 /* It was a specialization of an ordinary member function in a
2078 return DECL_TI_TEMPLATE (fn
);
2081 /* It was a specialization of a template. */
2082 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2083 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2085 *targs_out
= copy_node (targs
);
2086 SET_TMPL_ARGS_LEVEL (*targs_out
,
2087 TMPL_ARGS_DEPTH (*targs_out
),
2088 TREE_PURPOSE (templates
));
2091 *targs_out
= TREE_PURPOSE (templates
);
2092 return TREE_VALUE (templates
);
2095 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2096 but with the default argument values filled in from those in the
2100 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2103 tree new_spec_types
;
2108 if (spec_types
== void_list_node
)
2109 return void_list_node
;
2111 /* Substitute into the rest of the list. */
2113 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2114 TREE_CHAIN (tmpl_types
));
2116 /* Add the default argument for this parameter. */
2117 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2118 TREE_VALUE (spec_types
),
2122 /* DECL is an explicit specialization. Replicate default arguments
2123 from the template it specializes. (That way, code like:
2125 template <class T> void f(T = 3);
2126 template <> void f(double);
2129 works, as required.) An alternative approach would be to look up
2130 the correct default arguments at the call-site, but this approach
2131 is consistent with how implicit instantiations are handled. */
2134 copy_default_args_to_explicit_spec (tree decl
)
2139 tree new_spec_types
;
2143 tree object_type
= NULL_TREE
;
2144 tree in_charge
= NULL_TREE
;
2145 tree vtt
= NULL_TREE
;
2147 /* See if there's anything we need to do. */
2148 tmpl
= DECL_TI_TEMPLATE (decl
);
2149 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2150 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2151 if (TREE_PURPOSE (t
))
2156 old_type
= TREE_TYPE (decl
);
2157 spec_types
= TYPE_ARG_TYPES (old_type
);
2159 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2161 /* Remove the this pointer, but remember the object's type for
2163 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2164 spec_types
= TREE_CHAIN (spec_types
);
2165 tmpl_types
= TREE_CHAIN (tmpl_types
);
2167 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2169 /* DECL may contain more parameters than TMPL due to the extra
2170 in-charge parameter in constructors and destructors. */
2171 in_charge
= spec_types
;
2172 spec_types
= TREE_CHAIN (spec_types
);
2174 if (DECL_HAS_VTT_PARM_P (decl
))
2177 spec_types
= TREE_CHAIN (spec_types
);
2181 /* Compute the merged default arguments. */
2183 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2185 /* Compute the new FUNCTION_TYPE. */
2189 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2194 /* Put the in-charge parameter back. */
2195 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2196 TREE_VALUE (in_charge
),
2199 new_type
= build_method_type_directly (object_type
,
2200 TREE_TYPE (old_type
),
2204 new_type
= build_function_type (TREE_TYPE (old_type
),
2206 new_type
= cp_build_type_attribute_variant (new_type
,
2207 TYPE_ATTRIBUTES (old_type
));
2208 new_type
= build_exception_variant (new_type
,
2209 TYPE_RAISES_EXCEPTIONS (old_type
));
2210 TREE_TYPE (decl
) = new_type
;
2213 /* Return the number of template headers we expect to see for a definition
2214 or specialization of CTYPE or one of its non-template members. */
2217 num_template_headers_for_class (tree ctype
)
2219 int num_templates
= 0;
2221 while (ctype
&& CLASS_TYPE_P (ctype
))
2223 /* You're supposed to have one `template <...>' for every
2224 template class, but you don't need one for a full
2225 specialization. For example:
2227 template <class T> struct S{};
2228 template <> struct S<int> { void f(); };
2229 void S<int>::f () {}
2231 is correct; there shouldn't be a `template <>' for the
2232 definition of `S<int>::f'. */
2233 if (!CLASSTYPE_TEMPLATE_INFO (ctype
))
2234 /* If CTYPE does not have template information of any
2235 kind, then it is not a template, nor is it nested
2236 within a template. */
2238 if (explicit_class_specialization_p (ctype
))
2240 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype
)))
2243 ctype
= TYPE_CONTEXT (ctype
);
2246 return num_templates
;
2249 /* Do a simple sanity check on the template headers that precede the
2250 variable declaration DECL. */
2253 check_template_variable (tree decl
)
2255 tree ctx
= CP_DECL_CONTEXT (decl
);
2256 int wanted
= num_template_headers_for_class (ctx
);
2257 if (!TYPE_P (ctx
) || !CLASSTYPE_TEMPLATE_INFO (ctx
))
2258 permerror (DECL_SOURCE_LOCATION (decl
),
2259 "%qD is not a static data member of a class template", decl
);
2260 else if (template_header_count
> wanted
)
2262 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2263 "too many template headers for %D (should be %d)",
2265 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
2266 inform (DECL_SOURCE_LOCATION (decl
),
2267 "members of an explicitly specialized class are defined "
2268 "without a template header");
2272 /* Check to see if the function just declared, as indicated in
2273 DECLARATOR, and in DECL, is a specialization of a function
2274 template. We may also discover that the declaration is an explicit
2275 instantiation at this point.
2277 Returns DECL, or an equivalent declaration that should be used
2278 instead if all goes well. Issues an error message if something is
2279 amiss. Returns error_mark_node if the error is not easily
2282 FLAGS is a bitmask consisting of the following flags:
2284 2: The function has a definition.
2285 4: The function is a friend.
2287 The TEMPLATE_COUNT is the number of references to qualifying
2288 template classes that appeared in the name of the function. For
2291 template <class T> struct S { void f(); };
2294 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2295 classes are not counted in the TEMPLATE_COUNT, so that in
2297 template <class T> struct S {};
2298 template <> struct S<int> { void f(); }
2299 template <> void S<int>::f();
2301 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2302 invalid; there should be no template <>.)
2304 If the function is a specialization, it is marked as such via
2305 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2306 is set up correctly, and it is added to the list of specializations
2307 for that template. */
2310 check_explicit_specialization (tree declarator
,
2315 int have_def
= flags
& 2;
2316 int is_friend
= flags
& 4;
2317 int specialization
= 0;
2318 int explicit_instantiation
= 0;
2319 int member_specialization
= 0;
2320 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2321 tree dname
= DECL_NAME (decl
);
2326 if (!processing_specialization
)
2329 tsk
= tsk_excessive_parms
;
2332 tsk
= current_tmpl_spec_kind (template_count
);
2337 if (processing_specialization
)
2340 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2342 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2345 /* This could be something like:
2347 template <class T> void f(T);
2348 class S { friend void f<>(int); } */
2352 /* This case handles bogus declarations like template <>
2353 template <class T> void f<int>(); */
2355 error ("template-id %qD in declaration of primary template",
2362 case tsk_invalid_member_spec
:
2363 /* The error has already been reported in
2364 check_specialization_scope. */
2365 return error_mark_node
;
2367 case tsk_invalid_expl_inst
:
2368 error ("template parameter list used in explicit instantiation");
2374 error ("definition provided for explicit instantiation");
2376 explicit_instantiation
= 1;
2379 case tsk_excessive_parms
:
2380 case tsk_insufficient_parms
:
2381 if (tsk
== tsk_excessive_parms
)
2382 error ("too many template parameter lists in declaration of %qD",
2384 else if (template_header_count
)
2385 error("too few template parameter lists in declaration of %qD", decl
);
2387 error("explicit specialization of %qD must be introduced by "
2388 "%<template <>%>", decl
);
2392 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2394 member_specialization
= 1;
2400 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2402 /* This case handles bogus declarations like template <>
2403 template <class T> void f<int>(); */
2405 if (uses_template_parms (declarator
))
2406 error ("function template partial specialization %qD "
2407 "is not allowed", declarator
);
2409 error ("template-id %qD in declaration of primary template",
2414 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2415 /* This is a specialization of a member template, without
2416 specialization the containing class. Something like:
2418 template <class T> struct S {
2419 template <class U> void f (U);
2421 template <> template <class U> void S<int>::f(U) {}
2423 That's a specialization -- but of the entire template. */
2431 if (specialization
|| member_specialization
)
2433 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2434 for (; t
; t
= TREE_CHAIN (t
))
2435 if (TREE_PURPOSE (t
))
2437 permerror (input_location
,
2438 "default argument specified in explicit specialization");
2443 if (specialization
|| member_specialization
|| explicit_instantiation
)
2445 tree tmpl
= NULL_TREE
;
2446 tree targs
= NULL_TREE
;
2448 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2449 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2453 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2458 /* If there is no class context, the explicit instantiation
2459 must be at namespace scope. */
2460 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2462 /* Find the namespace binding, using the declaration
2464 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2466 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2468 error ("%qD is not a template function", dname
);
2469 fns
= error_mark_node
;
2473 tree fn
= OVL_CURRENT (fns
);
2474 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2475 CP_DECL_CONTEXT (fn
)))
2476 error ("%qD is not declared in %qD",
2477 decl
, current_namespace
);
2481 declarator
= lookup_template_function (fns
, NULL_TREE
);
2484 if (declarator
== error_mark_node
)
2485 return error_mark_node
;
2487 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2489 if (!explicit_instantiation
)
2490 /* A specialization in class scope. This is invalid,
2491 but the error will already have been flagged by
2492 check_specialization_scope. */
2493 return error_mark_node
;
2496 /* It's not valid to write an explicit instantiation in
2499 class C { template void f(); }
2501 This case is caught by the parser. However, on
2504 template class C { void f(); };
2506 (which is invalid) we can get here. The error will be
2513 else if (ctype
!= NULL_TREE
2514 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2517 /* Find the list of functions in ctype that have the same
2518 name as the declared function. */
2519 tree name
= TREE_OPERAND (declarator
, 0);
2520 tree fns
= NULL_TREE
;
2523 if (constructor_name_p (name
, ctype
))
2525 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2527 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2528 : !CLASSTYPE_DESTRUCTORS (ctype
))
2530 /* From [temp.expl.spec]:
2532 If such an explicit specialization for the member
2533 of a class template names an implicitly-declared
2534 special member function (clause _special_), the
2535 program is ill-formed.
2537 Similar language is found in [temp.explicit]. */
2538 error ("specialization of implicitly-declared special member function");
2539 return error_mark_node
;
2542 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2545 if (!DECL_CONV_FN_P (decl
))
2547 idx
= lookup_fnfields_1 (ctype
, name
);
2549 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2553 VEC(tree
,gc
) *methods
;
2556 /* For a type-conversion operator, we cannot do a
2557 name-based lookup. We might be looking for `operator
2558 int' which will be a specialization of `operator T'.
2559 So, we find *all* the conversion operators, and then
2560 select from them. */
2563 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2565 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2566 VEC_iterate (tree
, methods
, idx
, ovl
);
2569 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2570 /* There are no more conversion functions. */
2573 /* Glue all these conversion functions together
2574 with those we already have. */
2575 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2576 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2580 if (fns
== NULL_TREE
)
2582 error ("no member function %qD declared in %qT", name
, ctype
);
2583 return error_mark_node
;
2586 TREE_OPERAND (declarator
, 0) = fns
;
2589 /* Figure out what exactly is being specialized at this point.
2590 Note that for an explicit instantiation, even one for a
2591 member function, we cannot tell apriori whether the
2592 instantiation is for a member template, or just a member
2593 function of a template class. Even if a member template is
2594 being instantiated, the member template arguments may be
2595 elided if they can be deduced from the rest of the
2597 tmpl
= determine_specialization (declarator
, decl
,
2599 member_specialization
,
2603 if (!tmpl
|| tmpl
== error_mark_node
)
2604 /* We couldn't figure out what this declaration was
2606 return error_mark_node
;
2609 tree gen_tmpl
= most_general_template (tmpl
);
2611 if (explicit_instantiation
)
2613 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2614 is done by do_decl_instantiation later. */
2616 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2617 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2619 if (arg_depth
> parm_depth
)
2621 /* If TMPL is not the most general template (for
2622 example, if TMPL is a friend template that is
2623 injected into namespace scope), then there will
2624 be too many levels of TARGS. Remove some of them
2629 new_targs
= make_tree_vec (parm_depth
);
2630 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2631 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2632 = TREE_VEC_ELT (targs
, i
);
2636 return instantiate_template (tmpl
, targs
, tf_error
);
2639 /* If we thought that the DECL was a member function, but it
2640 turns out to be specializing a static member function,
2641 make DECL a static member function as well. */
2642 if (DECL_STATIC_FUNCTION_P (tmpl
)
2643 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2644 revert_static_member_fn (decl
);
2646 /* If this is a specialization of a member template of a
2647 template class, we want to return the TEMPLATE_DECL, not
2648 the specialization of it. */
2649 if (tsk
== tsk_template
)
2651 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2652 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2653 DECL_INITIAL (result
) = NULL_TREE
;
2657 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2658 DECL_SOURCE_LOCATION (result
)
2659 = DECL_SOURCE_LOCATION (decl
);
2660 /* We want to use the argument list specified in the
2661 definition, not in the original declaration. */
2662 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2663 for (parm
= DECL_ARGUMENTS (result
); parm
;
2664 parm
= DECL_CHAIN (parm
))
2665 DECL_CONTEXT (parm
) = result
;
2667 return register_specialization (tmpl
, gen_tmpl
, targs
,
2671 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2672 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2674 /* Inherit default function arguments from the template
2675 DECL is specializing. */
2676 copy_default_args_to_explicit_spec (decl
);
2678 /* This specialization has the same protection as the
2679 template it specializes. */
2680 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2681 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2683 /* 7.1.1-1 [dcl.stc]
2685 A storage-class-specifier shall not be specified in an
2686 explicit specialization...
2688 The parser rejects these, so unless action is taken here,
2689 explicit function specializations will always appear with
2692 The action recommended by the C++ CWG in response to C++
2693 defect report 605 is to make the storage class and linkage
2694 of the explicit specialization match the templated function:
2696 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2698 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2700 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2701 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2703 /* This specialization has the same linkage and visibility as
2704 the function template it specializes. */
2705 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2706 if (! TREE_PUBLIC (decl
))
2708 DECL_INTERFACE_KNOWN (decl
) = 1;
2709 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2711 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2712 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2714 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2715 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2719 /* If DECL is a friend declaration, declared using an
2720 unqualified name, the namespace associated with DECL may
2721 have been set incorrectly. For example, in:
2723 template <typename T> void f(T);
2725 struct S { friend void f<int>(int); }
2728 we will have set the DECL_CONTEXT for the friend
2729 declaration to N, rather than to the global namespace. */
2730 if (DECL_NAMESPACE_SCOPE_P (decl
))
2731 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2733 if (is_friend
&& !have_def
)
2734 /* This is not really a declaration of a specialization.
2735 It's just the name of an instantiation. But, it's not
2736 a request for an instantiation, either. */
2737 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2738 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2739 /* This is indeed a specialization. In case of constructors
2740 and destructors, we need in-charge and not-in-charge
2741 versions in V3 ABI. */
2742 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2744 /* Register this specialization so that we can find it
2746 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2753 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2754 parameters. These are represented in the same format used for
2755 DECL_TEMPLATE_PARMS. */
2758 comp_template_parms (const_tree parms1
, const_tree parms2
)
2763 if (parms1
== parms2
)
2766 for (p1
= parms1
, p2
= parms2
;
2767 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2768 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2770 tree t1
= TREE_VALUE (p1
);
2771 tree t2
= TREE_VALUE (p2
);
2774 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2775 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2777 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2780 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2782 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2783 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2785 /* If either of the template parameters are invalid, assume
2786 they match for the sake of error recovery. */
2787 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2790 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2793 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2794 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2795 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2797 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2802 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2803 /* One set of parameters has more parameters lists than the
2810 /* Determine whether PARM is a parameter pack. */
2813 template_parameter_pack_p (const_tree parm
)
2815 /* Determine if we have a non-type template parameter pack. */
2816 if (TREE_CODE (parm
) == PARM_DECL
)
2817 return (DECL_TEMPLATE_PARM_P (parm
)
2818 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2819 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2820 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2822 /* If this is a list of template parameters, we could get a
2823 TYPE_DECL or a TEMPLATE_DECL. */
2824 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2825 parm
= TREE_TYPE (parm
);
2827 /* Otherwise it must be a type template parameter. */
2828 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2829 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2830 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2833 /* Determine if T is a function parameter pack. */
2836 function_parameter_pack_p (const_tree t
)
2838 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2839 return FUNCTION_PARAMETER_PACK_P (t
);
2843 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2844 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2847 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2849 if (! primary_func_tmpl_inst
2850 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2851 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2854 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2857 /* Return true iff the function parameter PARAM_DECL was expanded
2858 from the function parameter pack PACK. */
2861 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2863 if (DECL_ARTIFICIAL (param_decl
)
2864 || !function_parameter_pack_p (pack
))
2867 /* The parameter pack and its pack arguments have the same
2869 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2872 /* Determine whether ARGS describes a variadic template args list,
2873 i.e., one that is terminated by a template argument pack. */
2876 template_args_variadic_p (tree args
)
2881 if (args
== NULL_TREE
)
2884 args
= INNERMOST_TEMPLATE_ARGS (args
);
2885 nargs
= TREE_VEC_LENGTH (args
);
2890 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2892 return ARGUMENT_PACK_P (last_parm
);
2895 /* Generate a new name for the parameter pack name NAME (an
2896 IDENTIFIER_NODE) that incorporates its */
2899 make_ith_pack_parameter_name (tree name
, int i
)
2901 /* Munge the name to include the parameter index. */
2902 #define NUMBUF_LEN 128
2903 char numbuf
[NUMBUF_LEN
];
2907 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2908 newname_len
= IDENTIFIER_LENGTH (name
)
2909 + strlen (numbuf
) + 2;
2910 newname
= (char*)alloca (newname_len
);
2911 snprintf (newname
, newname_len
,
2912 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2913 return get_identifier (newname
);
2916 /* Return true if T is a primary function, class or alias template
2920 primary_template_instantiation_p (const_tree t
)
2925 if (TREE_CODE (t
) == FUNCTION_DECL
)
2926 return DECL_LANG_SPECIFIC (t
)
2927 && DECL_TEMPLATE_INSTANTIATION (t
)
2928 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2929 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2930 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2931 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2933 && TYPE_TEMPLATE_INFO (t
)
2934 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
2935 && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t
)))
2940 /* Return true if PARM is a template template parameter. */
2943 template_template_parameter_p (const_tree parm
)
2945 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2948 /* Return true iff PARM is a DECL representing a type template
2952 template_type_parameter_p (const_tree parm
)
2955 && (TREE_CODE (parm
) == TYPE_DECL
2956 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2957 && DECL_TEMPLATE_PARM_P (parm
));
2960 /* Return the template parameters of T if T is a
2961 primary template instantiation, NULL otherwise. */
2964 get_primary_template_innermost_parameters (const_tree t
)
2966 tree parms
= NULL
, template_info
= NULL
;
2968 if ((template_info
= get_template_info (t
))
2969 && primary_template_instantiation_p (t
))
2970 parms
= INNERMOST_TEMPLATE_PARMS
2971 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2976 /* Return the template parameters of the LEVELth level from the full list
2977 of template parameters PARMS. */
2980 get_template_parms_at_level (tree parms
, int level
)
2984 || TREE_CODE (parms
) != TREE_LIST
2985 || level
> TMPL_PARMS_DEPTH (parms
))
2988 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2989 if (TMPL_PARMS_DEPTH (p
) == level
)
2995 /* Returns the template arguments of T if T is a template instantiation,
2999 get_template_innermost_arguments (const_tree t
)
3001 tree args
= NULL
, template_info
= NULL
;
3003 if ((template_info
= get_template_info (t
))
3004 && TI_ARGS (template_info
))
3005 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
3010 /* Return the argument pack elements of T if T is a template argument pack,
3014 get_template_argument_pack_elems (const_tree t
)
3016 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
3017 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
3020 return ARGUMENT_PACK_ARGS (t
);
3023 /* Structure used to track the progress of find_parameter_packs_r. */
3024 struct find_parameter_pack_data
3026 /* TREE_LIST that will contain all of the parameter packs found by
3028 tree
* parameter_packs
;
3030 /* Set of AST nodes that have been visited by the traversal. */
3031 struct pointer_set_t
*visited
;
3034 /* Identifies all of the argument packs that occur in a template
3035 argument and appends them to the TREE_LIST inside DATA, which is a
3036 find_parameter_pack_data structure. This is a subroutine of
3037 make_pack_expansion and uses_parameter_packs. */
3039 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
3042 struct find_parameter_pack_data
* ppd
=
3043 (struct find_parameter_pack_data
*)data
;
3044 bool parameter_pack_p
= false;
3046 /* Handle type aliases/typedefs. */
3049 && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
3050 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
3052 if (TYPE_TEMPLATE_INFO (t
))
3053 cp_walk_tree (&TYPE_TI_ARGS (t
),
3054 &find_parameter_packs_r
,
3060 /* Identify whether this is a parameter pack or not. */
3061 switch (TREE_CODE (t
))
3063 case TEMPLATE_PARM_INDEX
:
3064 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3065 parameter_pack_p
= true;
3068 case TEMPLATE_TYPE_PARM
:
3069 t
= TYPE_MAIN_VARIANT (t
);
3070 case TEMPLATE_TEMPLATE_PARM
:
3071 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3072 parameter_pack_p
= true;
3076 if (FUNCTION_PARAMETER_PACK_P (t
))
3078 /* We don't want to walk into the type of a PARM_DECL,
3079 because we don't want to see the type parameter pack. */
3081 parameter_pack_p
= true;
3086 parameter_pack_p
= true;
3089 /* Not a parameter pack. */
3093 if (parameter_pack_p
)
3095 /* Add this parameter pack to the list. */
3096 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3100 cp_walk_tree (&TYPE_CONTEXT (t
),
3101 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3103 /* This switch statement will return immediately if we don't find a
3105 switch (TREE_CODE (t
))
3107 case TEMPLATE_PARM_INDEX
:
3110 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3111 /* Check the template itself. */
3112 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3113 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3114 /* Check the template arguments. */
3115 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3120 case TEMPLATE_TYPE_PARM
:
3121 case TEMPLATE_TEMPLATE_PARM
:
3128 if (TYPE_PTRMEMFUNC_P (t
))
3134 if (TYPE_TEMPLATE_INFO (t
))
3135 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3136 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3143 cp_walk_tree (&TREE_TYPE (t
),
3144 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3148 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3153 case TYPE_PACK_EXPANSION
:
3154 case EXPR_PACK_EXPANSION
:
3159 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3164 case IDENTIFIER_NODE
:
3165 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3177 /* Determines if the expression or type T uses any parameter packs. */
3179 uses_parameter_packs (tree t
)
3181 tree parameter_packs
= NULL_TREE
;
3182 struct find_parameter_pack_data ppd
;
3183 ppd
.parameter_packs
= ¶meter_packs
;
3184 ppd
.visited
= pointer_set_create ();
3185 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3186 pointer_set_destroy (ppd
.visited
);
3187 return parameter_packs
!= NULL_TREE
;
3190 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3191 representation a base-class initializer into a parameter pack
3192 expansion. If all goes well, the resulting node will be an
3193 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3196 make_pack_expansion (tree arg
)
3199 tree parameter_packs
= NULL_TREE
;
3200 bool for_types
= false;
3201 struct find_parameter_pack_data ppd
;
3203 if (!arg
|| arg
== error_mark_node
)
3206 if (TREE_CODE (arg
) == TREE_LIST
)
3208 /* The only time we will see a TREE_LIST here is for a base
3209 class initializer. In this case, the TREE_PURPOSE will be a
3210 _TYPE node (representing the base class expansion we're
3211 initializing) and the TREE_VALUE will be a TREE_LIST
3212 containing the initialization arguments.
3214 The resulting expansion looks somewhat different from most
3215 expansions. Rather than returning just one _EXPANSION, we
3216 return a TREE_LIST whose TREE_PURPOSE is a
3217 TYPE_PACK_EXPANSION containing the bases that will be
3218 initialized. The TREE_VALUE will be identical to the
3219 original TREE_VALUE, which is a list of arguments that will
3220 be passed to each base. We do not introduce any new pack
3221 expansion nodes into the TREE_VALUE (although it is possible
3222 that some already exist), because the TREE_PURPOSE and
3223 TREE_VALUE all need to be expanded together with the same
3224 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3225 resulting TREE_PURPOSE will mention the parameter packs in
3226 both the bases and the arguments to the bases. */
3229 tree parameter_packs
= NULL_TREE
;
3231 /* Determine which parameter packs will be used by the base
3233 ppd
.visited
= pointer_set_create ();
3234 ppd
.parameter_packs
= ¶meter_packs
;
3235 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3238 if (parameter_packs
== NULL_TREE
)
3240 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3241 pointer_set_destroy (ppd
.visited
);
3242 return error_mark_node
;
3245 if (TREE_VALUE (arg
) != void_type_node
)
3247 /* Collect the sets of parameter packs used in each of the
3248 initialization arguments. */
3249 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3251 /* Determine which parameter packs will be expanded in this
3253 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3258 pointer_set_destroy (ppd
.visited
);
3260 /* Create the pack expansion type for the base type. */
3261 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3262 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3263 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3265 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3266 they will rarely be compared to anything. */
3267 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3269 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3272 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3275 /* Build the PACK_EXPANSION_* node. */
3277 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3278 : make_node (EXPR_PACK_EXPANSION
);
3279 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3280 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3282 /* Propagate type and const-expression information. */
3283 TREE_TYPE (result
) = TREE_TYPE (arg
);
3284 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3287 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3288 they will rarely be compared to anything. */
3289 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3291 /* Determine which parameter packs will be expanded. */
3292 ppd
.parameter_packs
= ¶meter_packs
;
3293 ppd
.visited
= pointer_set_create ();
3294 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3295 pointer_set_destroy (ppd
.visited
);
3297 /* Make sure we found some parameter packs. */
3298 if (parameter_packs
== NULL_TREE
)
3301 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3303 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3304 return error_mark_node
;
3306 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3308 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3313 /* Checks T for any "bare" parameter packs, which have not yet been
3314 expanded, and issues an error if any are found. This operation can
3315 only be done on full expressions or types (e.g., an expression
3316 statement, "if" condition, etc.), because we could have expressions like:
3318 foo(f(g(h(args)))...)
3320 where "args" is a parameter pack. check_for_bare_parameter_packs
3321 should not be called for the subexpressions args, h(args),
3322 g(h(args)), or f(g(h(args))), because we would produce erroneous
3325 Returns TRUE and emits an error if there were bare parameter packs,
3326 returns FALSE otherwise. */
3328 check_for_bare_parameter_packs (tree t
)
3330 tree parameter_packs
= NULL_TREE
;
3331 struct find_parameter_pack_data ppd
;
3333 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3336 if (TREE_CODE (t
) == TYPE_DECL
)
3339 ppd
.parameter_packs
= ¶meter_packs
;
3340 ppd
.visited
= pointer_set_create ();
3341 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3342 pointer_set_destroy (ppd
.visited
);
3344 if (parameter_packs
)
3346 error ("parameter packs not expanded with %<...%>:");
3347 while (parameter_packs
)
3349 tree pack
= TREE_VALUE (parameter_packs
);
3350 tree name
= NULL_TREE
;
3352 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3353 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3354 name
= TYPE_NAME (pack
);
3355 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3356 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3358 name
= DECL_NAME (pack
);
3361 inform (input_location
, " %qD", name
);
3363 inform (input_location
, " <anonymous>");
3365 parameter_packs
= TREE_CHAIN (parameter_packs
);
3374 /* Expand any parameter packs that occur in the template arguments in
3377 expand_template_argument_pack (tree args
)
3379 tree result_args
= NULL_TREE
;
3380 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3381 int num_result_args
= -1;
3382 int non_default_args_count
= -1;
3384 /* First, determine if we need to expand anything, and the number of
3385 slots we'll need. */
3386 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3388 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3389 if (arg
== NULL_TREE
)
3391 if (ARGUMENT_PACK_P (arg
))
3393 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3394 if (num_result_args
< 0)
3395 num_result_args
= in_arg
+ num_packed
;
3397 num_result_args
+= num_packed
;
3401 if (num_result_args
>= 0)
3406 /* If no expansion is necessary, we're done. */
3407 if (num_result_args
< 0)
3410 /* Expand arguments. */
3411 result_args
= make_tree_vec (num_result_args
);
3412 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3413 non_default_args_count
=
3414 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3415 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3417 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3418 if (ARGUMENT_PACK_P (arg
))
3420 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3421 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3422 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3423 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3424 if (non_default_args_count
> 0)
3425 non_default_args_count
+= num_packed
;
3429 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3433 if (non_default_args_count
>= 0)
3434 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3438 /* Checks if DECL shadows a template parameter.
3440 [temp.local]: A template-parameter shall not be redeclared within its
3441 scope (including nested scopes).
3443 Emits an error and returns TRUE if the DECL shadows a parameter,
3444 returns FALSE otherwise. */
3447 check_template_shadow (tree decl
)
3451 /* If we're not in a template, we can't possibly shadow a template
3453 if (!current_template_parms
)
3456 /* Figure out what we're shadowing. */
3457 if (TREE_CODE (decl
) == OVERLOAD
)
3458 decl
= OVL_CURRENT (decl
);
3459 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3461 /* If there's no previous binding for this name, we're not shadowing
3462 anything, let alone a template parameter. */
3466 /* If we're not shadowing a template parameter, we're done. Note
3467 that OLDDECL might be an OVERLOAD (or perhaps even an
3468 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3470 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3473 /* We check for decl != olddecl to avoid bogus errors for using a
3474 name inside a class. We check TPFI to avoid duplicate errors for
3475 inline member templates. */
3477 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3480 error ("declaration of %q+#D", decl
);
3481 error (" shadows template parm %q+#D", olddecl
);
3485 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3486 ORIG_LEVEL, DECL, and TYPE. */
3489 build_template_parm_index (int index
,
3495 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3496 TEMPLATE_PARM_IDX (t
) = index
;
3497 TEMPLATE_PARM_LEVEL (t
) = level
;
3498 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3499 TEMPLATE_PARM_DECL (t
) = decl
;
3500 TREE_TYPE (t
) = type
;
3501 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3502 TREE_READONLY (t
) = TREE_READONLY (decl
);
3507 /* Find the canonical type parameter for the given template type
3508 parameter. Returns the canonical type parameter, which may be TYPE
3509 if no such parameter existed. */
3512 canonical_type_parameter (tree type
)
3515 int idx
= TEMPLATE_TYPE_IDX (type
);
3516 if (!canonical_template_parms
)
3517 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3519 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3520 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3522 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3523 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3524 list
= TREE_CHAIN (list
);
3527 return TREE_VALUE (list
);
3530 VEC_replace(tree
, canonical_template_parms
, idx
,
3531 tree_cons (NULL_TREE
, type
,
3532 VEC_index (tree
, canonical_template_parms
, idx
)));
3537 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3538 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3539 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3540 new one is created. */
3543 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3544 tsubst_flags_t complain
)
3546 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3547 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3548 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3549 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3551 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3554 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3555 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3556 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3557 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3558 DECL_ARTIFICIAL (decl
) = 1;
3559 SET_DECL_TEMPLATE_PARM_P (decl
);
3561 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3562 TEMPLATE_PARM_LEVEL (index
) - levels
,
3563 TEMPLATE_PARM_ORIG_LEVEL (index
),
3565 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3566 TEMPLATE_PARM_PARAMETER_PACK (t
)
3567 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3569 /* Template template parameters need this. */
3570 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3571 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3572 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3576 return TEMPLATE_PARM_DESCENDANTS (index
);
3579 /* Process information from new template parameter PARM and append it
3580 to the LIST being built. This new parameter is a non-type
3581 parameter iff IS_NON_TYPE is true. This new parameter is a
3582 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3583 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3584 parameter list PARM belongs to. This is used used to create a
3585 proper canonical type for the type of PARM that is to be created,
3586 iff PARM is a type. If the size is not known, this parameter shall
3590 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3591 bool is_non_type
, bool is_parameter_pack
)
3598 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3599 defval
= TREE_PURPOSE (parm
);
3603 tree p
= tree_last (list
);
3605 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3608 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3609 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3611 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3621 parm
= TREE_VALUE (parm
);
3623 SET_DECL_TEMPLATE_PARM_P (parm
);
3625 if (TREE_TYPE (parm
) == error_mark_node
)
3627 err_parm_list
= build_tree_list (defval
, parm
);
3628 TREE_VALUE (err_parm_list
) = error_mark_node
;
3629 return chainon (list
, err_parm_list
);
3635 The top-level cv-qualifiers on the template-parameter are
3636 ignored when determining its type. */
3637 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3638 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3640 err_parm_list
= build_tree_list (defval
, parm
);
3641 TREE_VALUE (err_parm_list
) = error_mark_node
;
3642 return chainon (list
, err_parm_list
);
3645 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3647 /* This template parameter is not a parameter pack, but it
3648 should be. Complain about "bare" parameter packs. */
3649 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3651 /* Recover by calling this a parameter pack. */
3652 is_parameter_pack
= true;
3656 /* A template parameter is not modifiable. */
3657 TREE_CONSTANT (parm
) = 1;
3658 TREE_READONLY (parm
) = 1;
3659 decl
= build_decl (parm_loc
,
3660 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3661 TREE_CONSTANT (decl
) = 1;
3662 TREE_READONLY (decl
) = 1;
3663 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3664 = build_template_parm_index (idx
, processing_template_decl
,
3665 processing_template_decl
,
3666 decl
, TREE_TYPE (parm
));
3668 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3669 = is_parameter_pack
;
3674 parm
= TREE_VALUE (TREE_VALUE (parm
));
3676 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3678 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3679 /* This is for distinguishing between real templates and template
3680 template parameters */
3681 TREE_TYPE (parm
) = t
;
3682 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3687 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3688 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3689 decl
= build_decl (parm_loc
,
3690 TYPE_DECL
, parm
, t
);
3693 TYPE_NAME (t
) = decl
;
3694 TYPE_STUB_DECL (t
) = decl
;
3696 TEMPLATE_TYPE_PARM_INDEX (t
)
3697 = build_template_parm_index (idx
, processing_template_decl
,
3698 processing_template_decl
,
3699 decl
, TREE_TYPE (parm
));
3700 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3701 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3703 DECL_ARTIFICIAL (decl
) = 1;
3704 SET_DECL_TEMPLATE_PARM_P (decl
);
3706 parm
= build_tree_list (defval
, parm
);
3707 return chainon (list
, parm
);
3710 /* The end of a template parameter list has been reached. Process the
3711 tree list into a parameter vector, converting each parameter into a more
3712 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3716 end_template_parm_list (tree parms
)
3720 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3722 current_template_parms
3723 = tree_cons (size_int (processing_template_decl
),
3724 saved_parmlist
, current_template_parms
);
3726 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3728 next
= TREE_CHAIN (parm
);
3729 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3730 TREE_CHAIN (parm
) = NULL_TREE
;
3733 --processing_template_parmlist
;
3735 return saved_parmlist
;
3738 /* end_template_decl is called after a template declaration is seen. */
3741 end_template_decl (void)
3743 reset_specialization ();
3745 if (! processing_template_decl
)
3748 /* This matches the pushlevel in begin_template_parm_list. */
3751 --processing_template_decl
;
3752 current_template_parms
= TREE_CHAIN (current_template_parms
);
3755 /* Takes a TREE_LIST representing a template parameter and convert it
3756 into an argument suitable to be passed to the type substitution
3757 functions. Note that If the TREE_LIST contains an error_mark
3758 node, the returned argument is error_mark_node. */
3761 template_parm_to_arg (tree t
)
3765 || TREE_CODE (t
) != TREE_LIST
)
3768 if (error_operand_p (TREE_VALUE (t
)))
3769 return error_mark_node
;
3773 if (TREE_CODE (t
) == TYPE_DECL
3774 || TREE_CODE (t
) == TEMPLATE_DECL
)
3778 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3780 /* Turn this argument into a TYPE_ARGUMENT_PACK
3781 with a single element, which expands T. */
3782 tree vec
= make_tree_vec (1);
3783 #ifdef ENABLE_CHECKING
3784 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3785 (vec
, TREE_VEC_LENGTH (vec
));
3787 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3789 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
3790 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3795 t
= DECL_INITIAL (t
);
3797 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
3799 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3800 with a single element, which expands T. */
3801 tree vec
= make_tree_vec (1);
3802 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
3803 #ifdef ENABLE_CHECKING
3804 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
3805 (vec
, TREE_VEC_LENGTH (vec
));
3807 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
3809 t
= make_node (NONTYPE_ARGUMENT_PACK
);
3810 SET_ARGUMENT_PACK_ARGS (t
, vec
);
3811 TREE_TYPE (t
) = type
;
3817 /* This function returns TRUE if PARM_PACK is a template parameter
3818 pack and if ARG_PACK is what template_parm_to_arg returned when
3819 passed PARM_PACK. */
3822 arg_from_parm_pack_p (tree arg_pack
, tree parm_pack
)
3824 /* For clarity in the comments below let's use the representation
3825 argument_pack<elements>' to denote an argument pack and its
3828 In the 'if' block below, we want to detect cases where
3829 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
3830 check if ARG_PACK is an argument pack which sole element is
3831 the expansion of PARM_PACK. That argument pack is typically
3832 created by template_parm_to_arg when passed a parameter
3836 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
3837 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
3839 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
3840 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
3841 if ((TYPE_P (pattern
) && same_type_p (pattern
, parm_pack
))
3842 || (!TYPE_P (pattern
) && cp_tree_equal (parm_pack
, pattern
)))
3843 /* The argument pack that the parameter maps to is just an
3844 expansion of the parameter itself, such as one would
3845 find in the implicit typedef of a class inside the
3846 class itself. Consider this parameter "unsubstituted",
3847 so that we will maintain the outer pack expansion. */
3853 /* Within the declaration of a template, return all levels of template
3854 parameters that apply. The template parameters are represented as
3855 a TREE_VEC, in the form documented in cp-tree.h for template
3859 current_template_args (void)
3862 tree args
= NULL_TREE
;
3863 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
3866 /* If there is only one level of template parameters, we do not
3867 create a TREE_VEC of TREE_VECs. Instead, we return a single
3868 TREE_VEC containing the arguments. */
3870 args
= make_tree_vec (length
);
3872 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
3874 tree a
= copy_node (TREE_VALUE (header
));
3877 TREE_TYPE (a
) = NULL_TREE
;
3878 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
3879 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
3881 #ifdef ENABLE_CHECKING
3882 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
3886 TREE_VEC_ELT (args
, --l
) = a
;
3891 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
3892 /* This can happen for template parms of a template template
3895 template<template<class T, class U> class TT> struct S;
3897 Consider the level of the parms of TT; T and U both have
3898 level 2; TT has no template parm of level 1. So in this case
3899 the first element of full_template_args is NULL_TREE. If we
3900 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
3901 of 2. This will make tsubst wrongly consider that T and U
3902 have level 1. Instead, let's create a dummy vector as the
3903 first element of full_template_args so that TMPL_ARG_DEPTH
3904 returns the correct depth for args. */
3905 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
3909 /* Update the declared TYPE by doing any lookups which were thought to be
3910 dependent, but are not now that we know the SCOPE of the declarator. */
3913 maybe_update_decl_type (tree orig_type
, tree scope
)
3915 tree type
= orig_type
;
3917 if (type
== NULL_TREE
)
3920 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3921 type
= TREE_TYPE (type
);
3923 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
3924 && dependent_type_p (type
)
3925 /* Don't bother building up the args in this case. */
3926 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
3928 /* tsubst in the args corresponding to the template parameters,
3929 including auto if present. Most things will be unchanged, but
3930 make_typename_type and tsubst_qualified_id will resolve
3931 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
3932 tree args
= current_template_args ();
3933 tree auto_node
= type_uses_auto (type
);
3937 tree auto_vec
= make_tree_vec (1);
3938 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
3939 args
= add_to_template_args (args
, auto_vec
);
3941 pushed
= push_scope (scope
);
3942 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
3947 if (type
== error_mark_node
)
3950 if (TREE_CODE (orig_type
) == TYPE_DECL
)
3952 if (same_type_p (type
, TREE_TYPE (orig_type
)))
3955 type
= TYPE_NAME (type
);
3960 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
3961 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
3962 a member template. Used by push_template_decl below. */
3965 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
3967 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
3968 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
3969 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
3970 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
3971 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
3976 struct template_parm_data
3978 /* The level of the template parameters we are currently
3982 /* The index of the specialization argument we are currently
3986 /* An array whose size is the number of template parameters. The
3987 elements are nonzero if the parameter has been used in any one
3988 of the arguments processed so far. */
3991 /* An array whose size is the number of template arguments. The
3992 elements are nonzero if the argument makes use of template
3993 parameters of this level. */
3994 int* arg_uses_template_parms
;
3997 /* Subroutine of push_template_decl used to see if each template
3998 parameter in a partial specialization is used in the explicit
3999 argument list. If T is of the LEVEL given in DATA (which is
4000 treated as a template_parm_data*), then DATA->PARMS is marked
4004 mark_template_parm (tree t
, void* data
)
4008 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4010 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4012 level
= TEMPLATE_PARM_LEVEL (t
);
4013 idx
= TEMPLATE_PARM_IDX (t
);
4017 level
= TEMPLATE_TYPE_LEVEL (t
);
4018 idx
= TEMPLATE_TYPE_IDX (t
);
4021 if (level
== tpd
->level
)
4023 tpd
->parms
[idx
] = 1;
4024 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4027 /* Return zero so that for_each_template_parm will continue the
4028 traversal of the tree; we want to mark *every* template parm. */
4032 /* Process the partial specialization DECL. */
4035 process_partial_specialization (tree decl
)
4037 tree type
= TREE_TYPE (decl
);
4038 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4039 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4040 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4041 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4044 int nargs
= TREE_VEC_LENGTH (inner_args
);
4047 bool did_error_intro
= false;
4048 struct template_parm_data tpd
;
4049 struct template_parm_data tpd2
;
4051 gcc_assert (current_template_parms
);
4053 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4054 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4056 /* We check that each of the template parameters given in the
4057 partial specialization is used in the argument list to the
4058 specialization. For example:
4060 template <class T> struct S;
4061 template <class T> struct S<T*>;
4063 The second declaration is OK because `T*' uses the template
4064 parameter T, whereas
4066 template <class T> struct S<int>;
4068 is no good. Even trickier is:
4079 The S2<T> declaration is actually invalid; it is a
4080 full-specialization. Of course,
4083 struct S2<T (*)(U)>;
4085 or some such would have been OK. */
4086 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4087 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4088 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4090 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4091 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4092 for (i
= 0; i
< nargs
; ++i
)
4094 tpd
.current_arg
= i
;
4095 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4096 &mark_template_parm
,
4099 /*include_nondeduced_p=*/false);
4101 for (i
= 0; i
< ntparms
; ++i
)
4102 if (tpd
.parms
[i
] == 0)
4104 /* One of the template parms was not used in the
4106 if (!did_error_intro
)
4108 error ("template parameters not used in partial specialization:");
4109 did_error_intro
= true;
4112 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4115 if (did_error_intro
)
4116 return error_mark_node
;
4118 /* [temp.class.spec]
4120 The argument list of the specialization shall not be identical to
4121 the implicit argument list of the primary template. */
4122 if (comp_template_args
4124 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4126 error ("partial specialization %qT does not specialize any template arguments", type
);
4128 /* A partial specialization that replaces multiple parameters of the
4129 primary template with a pack expansion is less specialized for those
4131 if (nargs
< DECL_NTPARMS (maintmpl
))
4133 error ("partial specialization is not more specialized than the "
4134 "primary template because it replaces multiple parameters "
4135 "with a pack expansion");
4136 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4140 /* [temp.class.spec]
4142 A partially specialized non-type argument expression shall not
4143 involve template parameters of the partial specialization except
4144 when the argument expression is a simple identifier.
4146 The type of a template parameter corresponding to a specialized
4147 non-type argument shall not be dependent on a parameter of the
4150 Also, we verify that pack expansions only occur at the
4151 end of the argument list. */
4152 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4154 for (i
= 0; i
< nargs
; ++i
)
4156 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4157 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4158 tree packed_args
= NULL_TREE
;
4161 if (ARGUMENT_PACK_P (arg
))
4163 /* Extract the arguments from the argument pack. We'll be
4164 iterating over these in the following loop. */
4165 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4166 len
= TREE_VEC_LENGTH (packed_args
);
4169 for (j
= 0; j
< len
; j
++)
4172 /* Get the Jth argument in the parameter pack. */
4173 arg
= TREE_VEC_ELT (packed_args
, j
);
4175 if (PACK_EXPANSION_P (arg
))
4177 /* Pack expansions must come at the end of the
4179 if ((packed_args
&& j
< len
- 1)
4180 || (!packed_args
&& i
< nargs
- 1))
4182 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4183 error ("parameter pack argument %qE must be at the "
4184 "end of the template argument list", arg
);
4186 error ("parameter pack argument %qT must be at the "
4187 "end of the template argument list", arg
);
4191 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4192 /* We only care about the pattern. */
4193 arg
= PACK_EXPANSION_PATTERN (arg
);
4195 if (/* These first two lines are the `non-type' bit. */
4197 && TREE_CODE (arg
) != TEMPLATE_DECL
4198 /* This next line is the `argument expression is not just a
4199 simple identifier' condition and also the `specialized
4200 non-type argument' bit. */
4201 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4203 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4204 || (packed_args
&& uses_template_parms (arg
)))
4205 error ("template argument %qE involves template parameter(s)",
4209 /* Look at the corresponding template parameter,
4210 marking which template parameters its type depends
4212 tree type
= TREE_TYPE (parm
);
4216 /* We haven't yet initialized TPD2. Do so now. */
4217 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4218 /* The number of parameters here is the number in the
4219 main template, which, as checked in the assertion
4221 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4223 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4226 /* Mark the template parameters. But this time, we're
4227 looking for the template parameters of the main
4228 template, not in the specialization. */
4229 tpd2
.current_arg
= i
;
4230 tpd2
.arg_uses_template_parms
[i
] = 0;
4231 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4232 for_each_template_parm (type
,
4233 &mark_template_parm
,
4236 /*include_nondeduced_p=*/false);
4238 if (tpd2
.arg_uses_template_parms
[i
])
4240 /* The type depended on some template parameters.
4241 If they are fully specialized in the
4242 specialization, that's OK. */
4245 for (j
= 0; j
< nargs
; ++j
)
4246 if (tpd2
.parms
[j
] != 0
4247 && tpd
.arg_uses_template_parms
[j
])
4250 error_n (input_location
, count
,
4251 "type %qT of template argument %qE depends "
4252 "on a template parameter",
4253 "type %qT of template argument %qE depends "
4254 "on template parameters",
4263 /* We should only get here once. */
4264 gcc_assert (!COMPLETE_TYPE_P (type
));
4266 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4267 = tree_cons (specargs
, inner_parms
,
4268 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4269 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4271 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4272 inst
= TREE_CHAIN (inst
))
4274 tree inst_type
= TREE_VALUE (inst
);
4275 if (COMPLETE_TYPE_P (inst_type
)
4276 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4278 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4279 if (spec
&& TREE_TYPE (spec
) == type
)
4280 permerror (input_location
,
4281 "partial specialization of %qT after instantiation "
4282 "of %qT", type
, inst_type
);
4289 /* Check that a template declaration's use of default arguments and
4290 parameter packs is not invalid. Here, PARMS are the template
4291 parameters. IS_PRIMARY is true if DECL is the thing declared by
4292 a primary template. IS_PARTIAL is true if DECL is a partial
4295 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4296 declaration (but not a definition); 1 indicates a declaration, 2
4297 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4298 emitted for extraneous default arguments.
4300 Returns TRUE if there were no errors found, FALSE otherwise. */
4303 check_default_tmpl_args (tree decl
, tree parms
, bool is_primary
,
4304 bool is_partial
, int is_friend_decl
)
4307 int last_level_to_check
;
4309 bool no_errors
= true;
4313 A default template-argument shall not be specified in a
4314 function template declaration or a function template definition, nor
4315 in the template-parameter-list of the definition of a member of a
4318 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4319 /* You can't have a function template declaration in a local
4320 scope, nor you can you define a member of a class template in a
4324 if (current_class_type
4325 && !TYPE_BEING_DEFINED (current_class_type
)
4326 && DECL_LANG_SPECIFIC (decl
)
4327 && DECL_DECLARES_FUNCTION_P (decl
)
4328 /* If this is either a friend defined in the scope of the class
4329 or a member function. */
4330 && (DECL_FUNCTION_MEMBER_P (decl
)
4331 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4332 : DECL_FRIEND_CONTEXT (decl
)
4333 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4335 /* And, if it was a member function, it really was defined in
4336 the scope of the class. */
4337 && (!DECL_FUNCTION_MEMBER_P (decl
)
4338 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4339 /* We already checked these parameters when the template was
4340 declared, so there's no need to do it again now. This function
4341 was defined in class scope, but we're processing it's body now
4342 that the class is complete. */
4345 /* Core issue 226 (C++0x only): the following only applies to class
4348 && ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
))
4352 If a template-parameter has a default template-argument, all
4353 subsequent template-parameters shall have a default
4354 template-argument supplied. */
4355 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4357 tree inner_parms
= TREE_VALUE (parm_level
);
4358 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4359 int seen_def_arg_p
= 0;
4362 for (i
= 0; i
< ntparms
; ++i
)
4364 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4366 if (parm
== error_mark_node
)
4369 if (TREE_PURPOSE (parm
))
4371 else if (seen_def_arg_p
4372 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4374 error ("no default argument for %qD", TREE_VALUE (parm
));
4375 /* For better subsequent error-recovery, we indicate that
4376 there should have been a default argument. */
4377 TREE_PURPOSE (parm
) = error_mark_node
;
4380 else if (!is_partial
4382 /* Don't complain about an enclosing partial
4384 && parm_level
== parms
4385 && TREE_CODE (decl
) == TYPE_DECL
4387 && template_parameter_pack_p (TREE_VALUE (parm
)))
4389 /* A primary class template can only have one
4390 parameter pack, at the end of the template
4393 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4394 error ("parameter pack %qE must be at the end of the"
4395 " template parameter list", TREE_VALUE (parm
));
4397 error ("parameter pack %qT must be at the end of the"
4398 " template parameter list",
4399 TREE_TYPE (TREE_VALUE (parm
)));
4401 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4409 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4413 /* For an ordinary class template, default template arguments are
4414 allowed at the innermost level, e.g.:
4415 template <class T = int>
4417 but, in a partial specialization, they're not allowed even
4418 there, as we have in [temp.class.spec]:
4420 The template parameter list of a specialization shall not
4421 contain default template argument values.
4423 So, for a partial specialization, or for a function template
4424 (in C++98/C++03), we look at all of them. */
4427 /* But, for a primary class template that is not a partial
4428 specialization we look at all template parameters except the
4430 parms
= TREE_CHAIN (parms
);
4432 /* Figure out what error message to issue. */
4433 if (is_friend_decl
== 2)
4434 msg
= G_("default template arguments may not be used in function template "
4435 "friend re-declaration");
4436 else if (is_friend_decl
)
4437 msg
= G_("default template arguments may not be used in function template "
4438 "friend declarations");
4439 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4440 msg
= G_("default template arguments may not be used in function templates "
4441 "without -std=c++11 or -std=gnu++11");
4442 else if (is_partial
)
4443 msg
= G_("default template arguments may not be used in "
4444 "partial specializations");
4446 msg
= G_("default argument for template parameter for class enclosing %qD");
4448 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4449 /* If we're inside a class definition, there's no need to
4450 examine the parameters to the class itself. On the one
4451 hand, they will be checked when the class is defined, and,
4452 on the other, default arguments are valid in things like:
4453 template <class T = double>
4454 struct S { template <class U> void f(U); };
4455 Here the default argument for `S' has no bearing on the
4456 declaration of `f'. */
4457 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4459 /* Check everything. */
4460 last_level_to_check
= 0;
4462 for (parm_level
= parms
;
4463 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4464 parm_level
= TREE_CHAIN (parm_level
))
4466 tree inner_parms
= TREE_VALUE (parm_level
);
4470 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4471 for (i
= 0; i
< ntparms
; ++i
)
4473 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4476 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4481 if (is_friend_decl
== 2)
4488 /* Clear out the default argument so that we are not
4490 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4494 /* At this point, if we're still interested in issuing messages,
4495 they must apply to classes surrounding the object declared. */
4497 msg
= G_("default argument for template parameter for class "
4504 /* Worker for push_template_decl_real, called via
4505 for_each_template_parm. DATA is really an int, indicating the
4506 level of the parameters we are interested in. If T is a template
4507 parameter of that level, return nonzero. */
4510 template_parm_this_level_p (tree t
, void* data
)
4512 int this_level
= *(int *)data
;
4515 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4516 level
= TEMPLATE_PARM_LEVEL (t
);
4518 level
= TEMPLATE_TYPE_LEVEL (t
);
4519 return level
== this_level
;
4522 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4523 parameters given by current_template_args, or reuses a
4524 previously existing one, if appropriate. Returns the DECL, or an
4525 equivalent one, if it is replaced via a call to duplicate_decls.
4527 If IS_FRIEND is true, DECL is a friend declaration. */
4530 push_template_decl_real (tree decl
, bool is_friend
)
4538 int new_template_p
= 0;
4539 /* True if the template is a member template, in the sense of
4541 bool member_template_p
= false;
4543 if (decl
== error_mark_node
|| !current_template_parms
)
4544 return error_mark_node
;
4546 /* See if this is a partial specialization. */
4547 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4548 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4549 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4551 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4555 /* For a friend, we want the context of the friend function, not
4556 the type of which it is a friend. */
4557 ctx
= CP_DECL_CONTEXT (decl
);
4558 else if (CP_DECL_CONTEXT (decl
)
4559 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4560 /* In the case of a virtual function, we want the class in which
4562 ctx
= CP_DECL_CONTEXT (decl
);
4564 /* Otherwise, if we're currently defining some class, the DECL
4565 is assumed to be a member of the class. */
4566 ctx
= current_scope ();
4568 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4571 if (!DECL_CONTEXT (decl
))
4572 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4574 /* See if this is a primary template. */
4575 if (is_friend
&& ctx
)
4576 /* A friend template that specifies a class context, i.e.
4577 template <typename T> friend void A<T>::f();
4581 is_primary
= template_parm_scope_p ();
4585 if (DECL_CLASS_SCOPE_P (decl
))
4586 member_template_p
= true;
4587 if (TREE_CODE (decl
) == TYPE_DECL
4588 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4590 error ("template class without a name");
4591 return error_mark_node
;
4593 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4595 if (DECL_DESTRUCTOR_P (decl
))
4599 A destructor shall not be a member template. */
4600 error ("destructor %qD declared as member template", decl
);
4601 return error_mark_node
;
4603 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4604 && (!prototype_p (TREE_TYPE (decl
))
4605 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4606 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4607 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4608 == void_list_node
)))
4610 /* [basic.stc.dynamic.allocation]
4612 An allocation function can be a function
4613 template. ... Template allocation functions shall
4614 have two or more parameters. */
4615 error ("invalid template declaration of %qD", decl
);
4616 return error_mark_node
;
4619 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4620 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4622 else if (TREE_CODE (decl
) == TYPE_DECL
4623 && TYPE_DECL_ALIAS_P (decl
))
4624 /* alias-declaration */
4625 gcc_assert (!DECL_ARTIFICIAL (decl
));
4628 error ("template declaration of %q#D", decl
);
4629 return error_mark_node
;
4633 /* Check to see that the rules regarding the use of default
4634 arguments are not being violated. */
4635 check_default_tmpl_args (decl
, current_template_parms
,
4636 is_primary
, is_partial
, /*is_friend_decl=*/0);
4638 /* Ensure that there are no parameter packs in the type of this
4639 declaration that have not been expanded. */
4640 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4642 /* Check each of the arguments individually to see if there are
4643 any bare parameter packs. */
4644 tree type
= TREE_TYPE (decl
);
4645 tree arg
= DECL_ARGUMENTS (decl
);
4646 tree argtype
= TYPE_ARG_TYPES (type
);
4648 while (arg
&& argtype
)
4650 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4651 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4653 /* This is a PARM_DECL that contains unexpanded parameter
4654 packs. We have already complained about this in the
4655 check_for_bare_parameter_packs call, so just replace
4656 these types with ERROR_MARK_NODE. */
4657 TREE_TYPE (arg
) = error_mark_node
;
4658 TREE_VALUE (argtype
) = error_mark_node
;
4661 arg
= DECL_CHAIN (arg
);
4662 argtype
= TREE_CHAIN (argtype
);
4665 /* Check for bare parameter packs in the return type and the
4666 exception specifiers. */
4667 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4668 /* Errors were already issued, set return type to int
4669 as the frontend doesn't expect error_mark_node as
4671 TREE_TYPE (type
) = integer_type_node
;
4672 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4673 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4675 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4676 && TYPE_DECL_ALIAS_P (decl
))
4677 ? DECL_ORIGINAL_TYPE (decl
)
4678 : TREE_TYPE (decl
)))
4680 TREE_TYPE (decl
) = error_mark_node
;
4681 return error_mark_node
;
4685 return process_partial_specialization (decl
);
4687 args
= current_template_args ();
4690 || TREE_CODE (ctx
) == FUNCTION_DECL
4691 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4692 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4694 if (DECL_LANG_SPECIFIC (decl
)
4695 && DECL_TEMPLATE_INFO (decl
)
4696 && DECL_TI_TEMPLATE (decl
))
4697 tmpl
= DECL_TI_TEMPLATE (decl
);
4698 /* If DECL is a TYPE_DECL for a class-template, then there won't
4699 be DECL_LANG_SPECIFIC. The information equivalent to
4700 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4701 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4702 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4703 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4705 /* Since a template declaration already existed for this
4706 class-type, we must be redeclaring it here. Make sure
4707 that the redeclaration is valid. */
4708 redeclare_class_template (TREE_TYPE (decl
),
4709 current_template_parms
);
4710 /* We don't need to create a new TEMPLATE_DECL; just use the
4711 one we already had. */
4712 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4716 tmpl
= build_template_decl (decl
, current_template_parms
,
4720 if (DECL_LANG_SPECIFIC (decl
)
4721 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4723 /* A specialization of a member template of a template
4725 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4726 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4727 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4733 tree a
, t
, current
, parms
;
4735 tree tinfo
= get_template_info (decl
);
4739 error ("template definition of non-template %q#D", decl
);
4740 return error_mark_node
;
4743 tmpl
= TI_TEMPLATE (tinfo
);
4745 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4746 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4747 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4748 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4752 /* The declaration is a specialization of a member
4753 template, declared outside the class. Therefore, the
4754 innermost template arguments will be NULL, so we
4755 replace them with the arguments determined by the
4756 earlier call to check_explicit_specialization. */
4757 args
= DECL_TI_ARGS (decl
);
4760 = build_template_decl (decl
, current_template_parms
,
4762 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4763 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4764 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4765 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4766 DECL_TEMPLATE_INFO (new_tmpl
)
4767 = build_template_info (tmpl
, args
);
4769 register_specialization (new_tmpl
,
4770 most_general_template (tmpl
),
4776 /* Make sure the template headers we got make sense. */
4778 parms
= DECL_TEMPLATE_PARMS (tmpl
);
4779 i
= TMPL_PARMS_DEPTH (parms
);
4780 if (TMPL_ARGS_DEPTH (args
) != i
)
4782 error ("expected %d levels of template parms for %q#D, got %d",
4783 i
, decl
, TMPL_ARGS_DEPTH (args
));
4786 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
4788 a
= TMPL_ARGS_LEVEL (args
, i
);
4789 t
= INNERMOST_TEMPLATE_PARMS (parms
);
4791 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
4793 if (current
== decl
)
4794 error ("got %d template parameters for %q#D",
4795 TREE_VEC_LENGTH (a
), decl
);
4797 error ("got %d template parameters for %q#T",
4798 TREE_VEC_LENGTH (a
), current
);
4799 error (" but %d required", TREE_VEC_LENGTH (t
));
4800 return error_mark_node
;
4803 if (current
== decl
)
4805 else if (current
== NULL_TREE
)
4806 /* Can happen in erroneous input. */
4809 current
= (TYPE_P (current
)
4810 ? TYPE_CONTEXT (current
)
4811 : DECL_CONTEXT (current
));
4814 /* Check that the parms are used in the appropriate qualifying scopes
4815 in the declarator. */
4816 if (!comp_template_args
4818 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
4821 template arguments to %qD do not match original template %qD",
4822 decl
, DECL_TEMPLATE_RESULT (tmpl
));
4823 if (!uses_template_parms (TI_ARGS (tinfo
)))
4824 inform (input_location
, "use template<> for an explicit specialization");
4825 /* Avoid crash in import_export_decl. */
4826 DECL_INTERFACE_KNOWN (decl
) = 1;
4827 return error_mark_node
;
4831 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
4832 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
4834 /* Push template declarations for global functions and types. Note
4835 that we do not try to push a global template friend declared in a
4836 template class; such a thing may well depend on the template
4837 parameters of the class. */
4838 if (new_template_p
&& !ctx
4839 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
4841 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
4842 if (tmpl
== error_mark_node
)
4843 return error_mark_node
;
4845 /* Hide template friend classes that haven't been declared yet. */
4846 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
4848 DECL_ANTICIPATED (tmpl
) = 1;
4849 DECL_FRIEND_P (tmpl
) = 1;
4855 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
4858 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
4859 if (DECL_CONV_FN_P (tmpl
))
4861 int depth
= TMPL_PARMS_DEPTH (parms
);
4863 /* It is a conversion operator. See if the type converted to
4864 depends on innermost template operands. */
4866 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
4868 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
4871 /* Give template template parms a DECL_CONTEXT of the template
4872 for which they are a parameter. */
4873 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4874 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
4876 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4877 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
4878 DECL_CONTEXT (parm
) = tmpl
;
4882 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
4883 back to its most general template. If TMPL is a specialization,
4884 ARGS may only have the innermost set of arguments. Add the missing
4885 argument levels if necessary. */
4886 if (DECL_TEMPLATE_INFO (tmpl
))
4887 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
4889 info
= build_template_info (tmpl
, args
);
4891 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
4892 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
4895 if (is_primary
&& !DECL_LANG_SPECIFIC (decl
))
4896 retrofit_lang_decl (decl
);
4897 if (DECL_LANG_SPECIFIC (decl
))
4898 DECL_TEMPLATE_INFO (decl
) = info
;
4901 return DECL_TEMPLATE_RESULT (tmpl
);
4905 push_template_decl (tree decl
)
4907 return push_template_decl_real (decl
, false);
4910 /* Called when a class template TYPE is redeclared with the indicated
4911 template PARMS, e.g.:
4913 template <class T> struct S;
4914 template <class T> struct S {}; */
4917 redeclare_class_template (tree type
, tree parms
)
4923 if (!TYPE_TEMPLATE_INFO (type
))
4925 error ("%qT is not a template type", type
);
4929 tmpl
= TYPE_TI_TEMPLATE (type
);
4930 if (!PRIMARY_TEMPLATE_P (tmpl
))
4931 /* The type is nested in some template class. Nothing to worry
4932 about here; there are no new template parameters for the nested
4938 error ("template specifiers not specified in declaration of %qD",
4943 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
4944 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
4946 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
4948 error_n (input_location
, TREE_VEC_LENGTH (parms
),
4949 "redeclared with %d template parameter",
4950 "redeclared with %d template parameters",
4951 TREE_VEC_LENGTH (parms
));
4952 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
4953 "previous declaration %q+D used %d template parameter",
4954 "previous declaration %q+D used %d template parameters",
4955 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
4959 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
4966 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
4967 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
4970 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
4971 if (tmpl_parm
== error_mark_node
)
4974 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
4975 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
4976 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
4978 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4980 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
4981 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
4982 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
4983 || (TREE_CODE (tmpl_parm
) != PARM_DECL
4984 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
4985 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
4986 || (TREE_CODE (tmpl_parm
) == PARM_DECL
4987 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
4988 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
4990 error ("template parameter %q+#D", tmpl_parm
);
4991 error ("redeclared here as %q#D", parm
);
4995 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
4997 /* We have in [temp.param]:
4999 A template-parameter may not be given default arguments
5000 by two different declarations in the same scope. */
5001 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5002 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5003 "original definition appeared here");
5007 if (parm_default
!= NULL_TREE
)
5008 /* Update the previous template parameters (which are the ones
5009 that will really count) with the new default value. */
5010 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5011 else if (tmpl_default
!= NULL_TREE
)
5012 /* Update the new parameters, too; they'll be used as the
5013 parameters for any members. */
5014 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5020 /* Simplify EXPR if it is a non-dependent expression. Returns the
5021 (possibly simplified) expression. */
5024 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5026 if (expr
== NULL_TREE
)
5029 /* If we're in a template, but EXPR isn't value dependent, simplify
5030 it. We're supposed to treat:
5032 template <typename T> void f(T[1 + 1]);
5033 template <typename T> void f(T[2]);
5035 as two declarations of the same function, for example. */
5036 if (processing_template_decl
5037 && !type_dependent_expression_p (expr
)
5038 && potential_constant_expression (expr
)
5039 && !value_dependent_expression_p (expr
))
5041 HOST_WIDE_INT saved_processing_template_decl
;
5043 saved_processing_template_decl
= processing_template_decl
;
5044 processing_template_decl
= 0;
5045 expr
= tsubst_copy_and_build (expr
,
5048 /*in_decl=*/NULL_TREE
,
5049 /*function_p=*/false,
5050 /*integral_constant_expression_p=*/true);
5051 processing_template_decl
= saved_processing_template_decl
;
5057 fold_non_dependent_expr (tree expr
)
5059 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5062 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5063 template declaration, or a TYPE_DECL for an alias declaration. */
5066 alias_type_or_template_p (tree t
)
5070 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5073 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5074 || DECL_ALIAS_TEMPLATE_P (t
));
5077 /* Return TRUE iff is a specialization of an alias template. */
5080 alias_template_specialization_p (tree t
)
5084 return (primary_template_instantiation_p (t
)
5085 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5088 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5089 must be a function or a pointer-to-function type, as specified
5090 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5091 and check that the resulting function has external linkage. */
5094 convert_nontype_argument_function (tree type
, tree expr
)
5098 linkage_kind linkage
;
5100 fn
= instantiate_type (type
, fns
, tf_none
);
5101 if (fn
== error_mark_node
)
5102 return error_mark_node
;
5105 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5106 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5107 if (BASELINK_P (fn_no_ptr
))
5108 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5110 /* [temp.arg.nontype]/1
5112 A template-argument for a non-type, non-template template-parameter
5115 -- the address of an object or function with external [C++11: or
5116 internal] linkage. */
5117 linkage
= decl_linkage (fn_no_ptr
);
5118 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5120 if (cxx_dialect
>= cxx0x
)
5121 error ("%qE is not a valid template argument for type %qT "
5122 "because %qD has no linkage",
5123 expr
, type
, fn_no_ptr
);
5125 error ("%qE is not a valid template argument for type %qT "
5126 "because %qD does not have external linkage",
5127 expr
, type
, fn_no_ptr
);
5134 /* Subroutine of convert_nontype_argument.
5135 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5136 Emit an error otherwise. */
5139 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5140 tsubst_flags_t complain
)
5143 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5145 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5147 if (complain
& tf_error
)
5149 error ("%qE is not a valid template argument for type %qT",
5151 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5156 /* Returns TRUE iff the address of OP is value-dependent.
5158 14.6.2.4 [temp.dep.temp]:
5159 A non-integral non-type template-argument is dependent if its type is
5160 dependent or it has either of the following forms
5163 and contains a nested-name-specifier which specifies a class-name that
5164 names a dependent type.
5166 We generalize this to just say that the address of a member of a
5167 dependent class is value-dependent; the above doesn't cover the
5168 address of a static data member named with an unqualified-id. */
5171 has_value_dependent_address (tree op
)
5173 /* We could use get_inner_reference here, but there's no need;
5174 this is only relevant for template non-type arguments, which
5175 can only be expressed as &id-expression. */
5178 tree ctx
= CP_DECL_CONTEXT (op
);
5179 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5186 /* The next set of functions are used for providing helpful explanatory
5187 diagnostics for failed overload resolution. Their messages should be
5188 indented by two spaces for consistency with the messages in
5192 unify_success (bool /*explain_p*/)
5198 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5201 inform (input_location
,
5202 " couldn't deduce template parameter %qD", parm
);
5207 unify_invalid (bool /*explain_p*/)
5213 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5216 inform (input_location
,
5217 " types %qT and %qT have incompatible cv-qualifiers",
5223 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5226 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5231 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5234 inform (input_location
,
5235 " template parameter %qD is not a parameter pack, but "
5242 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5245 inform (input_location
,
5246 " template argument %qE does not match "
5247 "pointer-to-member constant %qE",
5253 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5256 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5261 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5264 inform (input_location
,
5265 " inconsistent parameter pack deduction with %qT and %qT",
5271 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5276 inform (input_location
,
5277 " deduced conflicting types for parameter %qT (%qT and %qT)",
5278 parm
, first
, second
);
5280 inform (input_location
,
5281 " deduced conflicting values for non-type parameter "
5282 "%qE (%qE and %qE)", parm
, first
, second
);
5288 unify_vla_arg (bool explain_p
, tree arg
)
5291 inform (input_location
,
5292 " variable-sized array type %qT is not "
5293 "a valid template argument",
5299 unify_method_type_error (bool explain_p
, tree arg
)
5302 inform (input_location
,
5303 " member function type %qT is not a valid template argument",
5309 unify_arity (bool explain_p
, int have
, int wanted
)
5312 inform_n (input_location
, wanted
,
5313 " candidate expects %d argument, %d provided",
5314 " candidate expects %d arguments, %d provided",
5320 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5322 return unify_arity (explain_p
, have
, wanted
);
5326 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5328 return unify_arity (explain_p
, have
, wanted
);
5332 unify_arg_conversion (bool explain_p
, tree to_type
,
5333 tree from_type
, tree arg
)
5336 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5337 arg
, from_type
, to_type
);
5342 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5343 tree parm
, tree arg
)
5348 case tbr_ambiguous_baseclass
:
5349 inform (input_location
, " %qT is an ambiguous base class of %qT",
5353 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5360 unify_inconsistent_template_template_parameters (bool explain_p
)
5363 inform (input_location
,
5364 " template parameters of a template template argument are "
5365 "inconsistent with other deduced template arguments");
5370 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5373 inform (input_location
,
5374 " can't deduce a template for %qT from non-template type %qT",
5380 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5383 inform (input_location
,
5384 " template argument %qE does not match %qD", arg
, parm
);
5389 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5392 inform (input_location
,
5393 " could not resolve address from overloaded function %qE",
5398 /* Attempt to convert the non-type template parameter EXPR to the
5399 indicated TYPE. If the conversion is successful, return the
5400 converted value. If the conversion is unsuccessful, return
5401 NULL_TREE if we issued an error message, or error_mark_node if we
5402 did not. We issue error messages for out-and-out bad template
5403 parameters, but not simply because the conversion failed, since we
5404 might be just trying to do argument deduction. Both TYPE and EXPR
5405 must be non-dependent.
5407 The conversion follows the special rules described in
5408 [temp.arg.nontype], and it is much more strict than an implicit
5411 This function is called twice for each template argument (see
5412 lookup_template_class for a more accurate description of this
5413 problem). This means that we need to handle expressions which
5414 are not valid in a C++ source, but can be created from the
5415 first call (for instance, casts to perform conversions). These
5416 hacks can go away after we fix the double coercion problem. */
5419 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5423 /* Detect immediately string literals as invalid non-type argument.
5424 This special-case is not needed for correctness (we would easily
5425 catch this later), but only to provide better diagnostic for this
5426 common user mistake. As suggested by DR 100, we do not mention
5427 linkage issues in the diagnostic as this is not the point. */
5428 /* FIXME we're making this OK. */
5429 if (TREE_CODE (expr
) == STRING_CST
)
5431 if (complain
& tf_error
)
5432 error ("%qE is not a valid template argument for type %qT "
5433 "because string literals can never be used in this context",
5438 /* Add the ADDR_EXPR now for the benefit of
5439 value_dependent_expression_p. */
5440 if (TYPE_PTROBV_P (type
)
5441 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5443 expr
= decay_conversion (expr
, complain
);
5444 if (expr
== error_mark_node
)
5445 return error_mark_node
;
5448 /* If we are in a template, EXPR may be non-dependent, but still
5449 have a syntactic, rather than semantic, form. For example, EXPR
5450 might be a SCOPE_REF, rather than the VAR_DECL to which the
5451 SCOPE_REF refers. Preserving the qualifying scope is necessary
5452 so that access checking can be performed when the template is
5453 instantiated -- but here we need the resolved form so that we can
5454 convert the argument. */
5455 if (TYPE_REF_OBJ_P (type
)
5456 && has_value_dependent_address (expr
))
5457 /* If we want the address and it's value-dependent, don't fold. */;
5458 else if (!type_unknown_p (expr
))
5459 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5460 if (error_operand_p (expr
))
5461 return error_mark_node
;
5462 expr_type
= TREE_TYPE (expr
);
5463 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5464 expr
= mark_lvalue_use (expr
);
5466 expr
= mark_rvalue_use (expr
);
5468 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5469 to a non-type argument of "nullptr". */
5470 if (expr
== nullptr_node
&& TYPE_PTR_OR_PTRMEM_P (type
))
5471 expr
= convert (type
, expr
);
5473 /* In C++11, integral or enumeration non-type template arguments can be
5474 arbitrary constant expressions. Pointer and pointer to
5475 member arguments can be general constant expressions that evaluate
5476 to a null value, but otherwise still need to be of a specific form. */
5477 if (cxx_dialect
>= cxx0x
)
5479 if (TREE_CODE (expr
) == PTRMEM_CST
)
5480 /* A PTRMEM_CST is already constant, and a valid template
5481 argument for a parameter of pointer to member type, we just want
5482 to leave it in that form rather than lower it to a
5484 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5485 expr
= maybe_constant_value (expr
);
5486 else if (TYPE_PTR_OR_PTRMEM_P (type
))
5488 tree folded
= maybe_constant_value (expr
);
5489 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5490 : null_member_pointer_value_p (folded
))
5495 /* HACK: Due to double coercion, we can get a
5496 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5497 which is the tree that we built on the first call (see
5498 below when coercing to reference to object or to reference to
5499 function). We just strip everything and get to the arg.
5500 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5502 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5504 tree probe_type
, probe
= expr
;
5505 if (REFERENCE_REF_P (probe
))
5506 probe
= TREE_OPERAND (probe
, 0);
5507 probe_type
= TREE_TYPE (probe
);
5508 if (TREE_CODE (probe
) == NOP_EXPR
)
5510 /* ??? Maybe we could use convert_from_reference here, but we
5511 would need to relax its constraints because the NOP_EXPR
5512 could actually change the type to something more cv-qualified,
5513 and this is not folded by convert_from_reference. */
5514 tree addr
= TREE_OPERAND (probe
, 0);
5515 gcc_assert (TREE_CODE (probe_type
) == REFERENCE_TYPE
);
5516 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5517 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5518 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5519 (TREE_TYPE (probe_type
),
5520 TREE_TYPE (TREE_TYPE (addr
))));
5522 expr
= TREE_OPERAND (addr
, 0);
5523 expr_type
= TREE_TYPE (expr
);
5527 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5528 parameter is a pointer to object, through decay and
5529 qualification conversion. Let's strip everything. */
5530 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5533 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5534 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5535 /* Skip the ADDR_EXPR only if it is part of the decay for
5536 an array. Otherwise, it is part of the original argument
5537 in the source code. */
5538 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5539 expr
= TREE_OPERAND (expr
, 0);
5540 expr_type
= TREE_TYPE (expr
);
5543 /* [temp.arg.nontype]/5, bullet 1
5545 For a non-type template-parameter of integral or enumeration type,
5546 integral promotions (_conv.prom_) and integral conversions
5547 (_conv.integral_) are applied. */
5548 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5550 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5551 t
= maybe_constant_value (t
);
5552 if (t
!= error_mark_node
)
5555 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5556 return error_mark_node
;
5558 /* Notice that there are constant expressions like '4 % 0' which
5559 do not fold into integer constants. */
5560 if (TREE_CODE (expr
) != INTEGER_CST
)
5562 if (complain
& tf_error
)
5564 int errs
= errorcount
, warns
= warningcount
;
5565 if (processing_template_decl
5566 && !require_potential_constant_expression (expr
))
5568 expr
= cxx_constant_value (expr
);
5569 if (errorcount
> errs
|| warningcount
> warns
)
5570 inform (EXPR_LOC_OR_HERE (expr
),
5571 "in template argument for type %qT ", type
);
5572 if (expr
== error_mark_node
)
5574 /* else cxx_constant_value complained but gave us
5575 a real constant, so go ahead. */
5576 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5582 /* [temp.arg.nontype]/5, bullet 2
5584 For a non-type template-parameter of type pointer to object,
5585 qualification conversions (_conv.qual_) and the array-to-pointer
5586 conversion (_conv.array_) are applied. */
5587 else if (TYPE_PTROBV_P (type
))
5589 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5591 A template-argument for a non-type, non-template template-parameter
5592 shall be one of: [...]
5594 -- the name of a non-type template-parameter;
5595 -- the address of an object or function with external linkage, [...]
5596 expressed as "& id-expression" where the & is optional if the name
5597 refers to a function or array, or if the corresponding
5598 template-parameter is a reference.
5600 Here, we do not care about functions, as they are invalid anyway
5601 for a parameter of type pointer-to-object. */
5603 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5604 /* Non-type template parameters are OK. */
5606 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5607 /* Null pointer values are OK in C++11. */;
5608 else if (TREE_CODE (expr
) != ADDR_EXPR
5609 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5611 if (TREE_CODE (expr
) == VAR_DECL
)
5613 error ("%qD is not a valid template argument "
5614 "because %qD is a variable, not the address of "
5619 /* Other values, like integer constants, might be valid
5620 non-type arguments of some other type. */
5621 return error_mark_node
;
5627 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5628 ? TREE_OPERAND (expr
, 0) : expr
);
5629 if (TREE_CODE (decl
) != VAR_DECL
)
5631 error ("%qE is not a valid template argument of type %qT "
5632 "because %qE is not a variable",
5636 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5638 error ("%qE is not a valid template argument of type %qT "
5639 "because %qD does not have external linkage",
5643 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5645 error ("%qE is not a valid template argument of type %qT "
5646 "because %qD has no linkage",
5652 expr
= decay_conversion (expr
, complain
);
5653 if (expr
== error_mark_node
)
5654 return error_mark_node
;
5656 expr
= perform_qualification_conversions (type
, expr
);
5657 if (expr
== error_mark_node
)
5658 return error_mark_node
;
5660 /* [temp.arg.nontype]/5, bullet 3
5662 For a non-type template-parameter of type reference to object, no
5663 conversions apply. The type referred to by the reference may be more
5664 cv-qualified than the (otherwise identical) type of the
5665 template-argument. The template-parameter is bound directly to the
5666 template-argument, which must be an lvalue. */
5667 else if (TYPE_REF_OBJ_P (type
))
5669 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5671 return error_mark_node
;
5673 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5675 error ("%qE is not a valid template argument for type %qT "
5676 "because of conflicts in cv-qualification", expr
, type
);
5680 if (!real_lvalue_p (expr
))
5682 error ("%qE is not a valid template argument for type %qT "
5683 "because it is not an lvalue", expr
, type
);
5687 /* [temp.arg.nontype]/1
5689 A template-argument for a non-type, non-template template-parameter
5690 shall be one of: [...]
5692 -- the address of an object or function with external linkage. */
5693 if (TREE_CODE (expr
) == INDIRECT_REF
5694 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5696 expr
= TREE_OPERAND (expr
, 0);
5699 error ("%q#D is not a valid template argument for type %qT "
5700 "because a reference variable does not have a constant "
5701 "address", expr
, type
);
5708 error ("%qE is not a valid template argument for type %qT "
5709 "because it is not an object with external linkage",
5714 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5716 error ("%qE is not a valid template argument for type %qT "
5717 "because object %qD has not external linkage",
5722 expr
= build_nop (type
, build_address (expr
));
5724 /* [temp.arg.nontype]/5, bullet 4
5726 For a non-type template-parameter of type pointer to function, only
5727 the function-to-pointer conversion (_conv.func_) is applied. If the
5728 template-argument represents a set of overloaded functions (or a
5729 pointer to such), the matching function is selected from the set
5731 else if (TYPE_PTRFN_P (type
))
5733 /* If the argument is a template-id, we might not have enough
5734 context information to decay the pointer. */
5735 if (!type_unknown_p (expr_type
))
5737 expr
= decay_conversion (expr
, complain
);
5738 if (expr
== error_mark_node
)
5739 return error_mark_node
;
5742 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5743 /* Null pointer values are OK in C++11. */
5744 return perform_qualification_conversions (type
, expr
);
5746 expr
= convert_nontype_argument_function (type
, expr
);
5747 if (!expr
|| expr
== error_mark_node
)
5750 if (TREE_CODE (expr
) != ADDR_EXPR
)
5752 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5753 error ("it must be the address of a function with external linkage");
5757 /* [temp.arg.nontype]/5, bullet 5
5759 For a non-type template-parameter of type reference to function, no
5760 conversions apply. If the template-argument represents a set of
5761 overloaded functions, the matching function is selected from the set
5763 else if (TYPE_REFFN_P (type
))
5765 if (TREE_CODE (expr
) == ADDR_EXPR
)
5767 error ("%qE is not a valid template argument for type %qT "
5768 "because it is a pointer", expr
, type
);
5769 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5773 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
5774 if (!expr
|| expr
== error_mark_node
)
5777 expr
= build_nop (type
, build_address (expr
));
5779 /* [temp.arg.nontype]/5, bullet 6
5781 For a non-type template-parameter of type pointer to member function,
5782 no conversions apply. If the template-argument represents a set of
5783 overloaded member functions, the matching member function is selected
5784 from the set (_over.over_). */
5785 else if (TYPE_PTRMEMFUNC_P (type
))
5787 expr
= instantiate_type (type
, expr
, tf_none
);
5788 if (expr
== error_mark_node
)
5789 return error_mark_node
;
5791 /* [temp.arg.nontype] bullet 1 says the pointer to member
5792 expression must be a pointer-to-member constant. */
5793 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5794 return error_mark_node
;
5796 /* There is no way to disable standard conversions in
5797 resolve_address_of_overloaded_function (called by
5798 instantiate_type). It is possible that the call succeeded by
5799 converting &B::I to &D::I (where B is a base of D), so we need
5800 to reject this conversion here.
5802 Actually, even if there was a way to disable standard conversions,
5803 it would still be better to reject them here so that we can
5804 provide a superior diagnostic. */
5805 if (!same_type_p (TREE_TYPE (expr
), type
))
5807 error ("%qE is not a valid template argument for type %qT "
5808 "because it is of type %qT", expr
, type
,
5810 /* If we are just one standard conversion off, explain. */
5811 if (can_convert (type
, TREE_TYPE (expr
), complain
))
5812 inform (input_location
,
5813 "standard conversions are not allowed in this context");
5817 /* [temp.arg.nontype]/5, bullet 7
5819 For a non-type template-parameter of type pointer to data member,
5820 qualification conversions (_conv.qual_) are applied. */
5821 else if (TYPE_PTRDATAMEM_P (type
))
5823 /* [temp.arg.nontype] bullet 1 says the pointer to member
5824 expression must be a pointer-to-member constant. */
5825 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
5826 return error_mark_node
;
5828 expr
= perform_qualification_conversions (type
, expr
);
5829 if (expr
== error_mark_node
)
5832 else if (NULLPTR_TYPE_P (type
))
5834 if (expr
!= nullptr_node
)
5836 error ("%qE is not a valid template argument for type %qT "
5837 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
5842 /* A template non-type parameter must be one of the above. */
5846 /* Sanity check: did we actually convert the argument to the
5848 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5849 (type
, TREE_TYPE (expr
)));
5853 /* Subroutine of coerce_template_template_parms, which returns 1 if
5854 PARM_PARM and ARG_PARM match using the rule for the template
5855 parameters of template template parameters. Both PARM and ARG are
5856 template parameters; the rest of the arguments are the same as for
5857 coerce_template_template_parms.
5860 coerce_template_template_parm (tree parm
,
5862 tsubst_flags_t complain
,
5866 if (arg
== NULL_TREE
|| arg
== error_mark_node
5867 || parm
== NULL_TREE
|| parm
== error_mark_node
)
5870 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5873 switch (TREE_CODE (parm
))
5876 /* We encounter instantiations of templates like
5877 template <template <template <class> class> class TT>
5880 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
5881 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
5883 if (!coerce_template_template_parms
5884 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
5890 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
5891 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5892 /* Argument is a parameter pack but parameter is not. */
5897 /* The tsubst call is used to handle cases such as
5899 template <int> class C {};
5900 template <class T, template <T> class TT> class D {};
5903 i.e. the parameter list of TT depends on earlier parameters. */
5904 if (!uses_template_parms (TREE_TYPE (arg
))
5906 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
5910 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
5911 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5912 /* Argument is a parameter pack but parameter is not. */
5925 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5926 template template parameters. Both PARM_PARMS and ARG_PARMS are
5927 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
5930 Consider the example:
5931 template <class T> class A;
5932 template<template <class U> class TT> class B;
5934 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5935 the parameters to A, and OUTER_ARGS contains A. */
5938 coerce_template_template_parms (tree parm_parms
,
5940 tsubst_flags_t complain
,
5944 int nparms
, nargs
, i
;
5948 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
5949 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
5951 nparms
= TREE_VEC_LENGTH (parm_parms
);
5952 nargs
= TREE_VEC_LENGTH (arg_parms
);
5954 /* Determine whether we have a parameter pack at the end of the
5955 template template parameter's template parameter list. */
5956 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
5958 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
5960 if (parm
== error_mark_node
)
5963 switch (TREE_CODE (parm
))
5967 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
5972 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
5982 && !(variadic_p
&& nargs
>= nparms
- 1))
5985 /* Check all of the template parameters except the parameter pack at
5986 the end (if any). */
5987 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
5989 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
5990 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
5993 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
5994 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
5996 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6004 /* Check each of the template parameters in the template
6005 argument against the template parameter pack at the end of
6006 the template template parameter. */
6007 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6010 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6012 for (; i
< nargs
; ++i
)
6014 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6017 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6019 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6028 /* Verifies that the deduced template arguments (in TARGS) for the
6029 template template parameters (in TPARMS) represent valid bindings,
6030 by comparing the template parameter list of each template argument
6031 to the template parameter list of its corresponding template
6032 template parameter, in accordance with DR150. This
6033 routine can only be called after all template arguments have been
6034 deduced. It will return TRUE if all of the template template
6035 parameter bindings are okay, FALSE otherwise. */
6037 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6039 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6042 /* We're dealing with template parms in this process. */
6043 ++processing_template_decl
;
6045 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6047 for (i
= 0; i
< ntparms
; ++i
)
6049 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6050 tree targ
= TREE_VEC_ELT (targs
, i
);
6052 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6054 tree packed_args
= NULL_TREE
;
6057 if (ARGUMENT_PACK_P (targ
))
6059 /* Look inside the argument pack. */
6060 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6061 len
= TREE_VEC_LENGTH (packed_args
);
6064 for (idx
= 0; idx
< len
; ++idx
)
6066 tree targ_parms
= NULL_TREE
;
6069 /* Extract the next argument from the argument
6071 targ
= TREE_VEC_ELT (packed_args
, idx
);
6073 if (PACK_EXPANSION_P (targ
))
6074 /* Look at the pattern of the pack expansion. */
6075 targ
= PACK_EXPANSION_PATTERN (targ
);
6077 /* Extract the template parameters from the template
6079 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6080 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6081 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6082 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6084 /* Verify that we can coerce the template template
6085 parameters from the template argument to the template
6086 parameter. This requires an exact match. */
6088 && !coerce_template_template_parms
6089 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6104 --processing_template_decl
;
6108 /* Since type attributes aren't mangled, we need to strip them from
6109 template type arguments. */
6112 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6115 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6117 mv
= TYPE_MAIN_VARIANT (arg
);
6118 arg
= strip_typedefs (arg
);
6119 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6120 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6122 if (complain
& tf_warning
)
6123 warning (0, "ignoring attributes on template argument %qT", arg
);
6124 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6125 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6130 /* Convert the indicated template ARG as necessary to match the
6131 indicated template PARM. Returns the converted ARG, or
6132 error_mark_node if the conversion was unsuccessful. Error and
6133 warning messages are issued under control of COMPLAIN. This
6134 conversion is for the Ith parameter in the parameter list. ARGS is
6135 the full set of template arguments deduced so far. */
6138 convert_template_argument (tree parm
,
6141 tsubst_flags_t complain
,
6147 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6149 if (TREE_CODE (arg
) == TREE_LIST
6150 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6152 /* The template argument was the name of some
6153 member function. That's usually
6154 invalid, but static members are OK. In any
6155 case, grab the underlying fields/functions
6156 and issue an error later if required. */
6157 orig_arg
= TREE_VALUE (arg
);
6158 TREE_TYPE (arg
) = unknown_type_node
;
6163 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6164 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6165 || requires_tmpl_type
);
6167 /* When determining whether an argument pack expansion is a template,
6168 look at the pattern. */
6169 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6170 arg
= PACK_EXPANSION_PATTERN (arg
);
6172 /* Deal with an injected-class-name used as a template template arg. */
6173 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6175 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6176 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6178 if (cxx_dialect
>= cxx0x
)
6179 /* OK under DR 1004. */;
6180 else if (complain
& tf_warning_or_error
)
6181 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6182 " used as template template argument", TYPE_NAME (arg
));
6183 else if (flag_pedantic_errors
)
6191 ((TREE_CODE (arg
) == TEMPLATE_DECL
6192 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6193 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6194 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6195 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6198 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6199 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6200 arg
= TYPE_STUB_DECL (arg
);
6202 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6204 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6205 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6207 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6209 if (complain
& tf_error
)
6210 error ("invalid use of destructor %qE as a type", orig_arg
);
6211 return error_mark_node
;
6214 permerror (input_location
,
6215 "to refer to a type member of a template parameter, "
6216 "use %<typename %E%>", orig_arg
);
6218 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6219 TREE_OPERAND (arg
, 1),
6225 if (is_type
!= requires_type
)
6229 if (complain
& tf_error
)
6231 error ("type/value mismatch at argument %d in template "
6232 "parameter list for %qD",
6235 error (" expected a constant of type %qT, got %qT",
6237 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6238 else if (requires_tmpl_type
)
6239 error (" expected a class template, got %qE", orig_arg
);
6241 error (" expected a type, got %qE", orig_arg
);
6244 return error_mark_node
;
6246 if (is_tmpl_type
^ requires_tmpl_type
)
6248 if (in_decl
&& (complain
& tf_error
))
6250 error ("type/value mismatch at argument %d in template "
6251 "parameter list for %qD",
6254 error (" expected a type, got %qT", DECL_NAME (arg
));
6256 error (" expected a class template, got %qT", orig_arg
);
6258 return error_mark_node
;
6263 if (requires_tmpl_type
)
6265 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6267 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6268 /* The number of argument required is not known yet.
6269 Just accept it for now. */
6270 val
= TREE_TYPE (arg
);
6273 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6276 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6278 if (coerce_template_template_parms (parmparm
, argparm
,
6284 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6286 if (val
!= error_mark_node
)
6288 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6289 val
= TREE_TYPE (val
);
6290 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6291 val
= make_pack_expansion (val
);
6296 if (in_decl
&& (complain
& tf_error
))
6298 error ("type/value mismatch at argument %d in "
6299 "template parameter list for %qD",
6301 error (" expected a template of type %qD, got %qT",
6305 val
= error_mark_node
;
6311 /* We only form one instance of each template specialization.
6312 Therefore, if we use a non-canonical variant (i.e., a
6313 typedef), any future messages referring to the type will use
6314 the typedef, which is confusing if those future uses do not
6315 themselves also use the typedef. */
6317 val
= canonicalize_type_argument (val
, complain
);
6321 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6323 if (invalid_nontype_parm_type_p (t
, complain
))
6324 return error_mark_node
;
6326 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6328 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6332 /* Not sure if this is reachable, but it doesn't hurt
6334 error ("type mismatch in nontype parameter pack");
6335 val
= error_mark_node
;
6338 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6339 /* We used to call digest_init here. However, digest_init
6340 will report errors, which we don't want when complain
6341 is zero. More importantly, digest_init will try too
6342 hard to convert things: for example, `0' should not be
6343 converted to pointer type at this point according to
6344 the standard. Accepting this is not merely an
6345 extension, since deciding whether or not these
6346 conversions can occur is part of determining which
6347 function template to call, or whether a given explicit
6348 argument specification is valid. */
6349 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6351 val
= strip_typedefs_expr (orig_arg
);
6353 if (val
== NULL_TREE
)
6354 val
= error_mark_node
;
6355 else if (val
== error_mark_node
&& (complain
& tf_error
))
6356 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6358 if (TREE_CODE (val
) == SCOPE_REF
)
6360 /* Strip typedefs from the SCOPE_REF. */
6361 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6362 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6364 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6365 QUALIFIED_NAME_IS_TEMPLATE (val
));
6372 /* Coerces the remaining template arguments in INNER_ARGS (from
6373 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6374 Returns the coerced argument pack. PARM_IDX is the position of this
6375 parameter in the template parameter list. ARGS is the original
6376 template argument list. */
6378 coerce_template_parameter_pack (tree parms
,
6386 tsubst_flags_t complain
)
6388 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6389 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6392 tree packed_types
= NULL_TREE
;
6394 if (arg_idx
> nargs
)
6397 packed_args
= make_tree_vec (nargs
- arg_idx
);
6399 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6400 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6402 /* When the template parameter is a non-type template
6403 parameter pack whose type uses parameter packs, we need
6404 to look at each of the template arguments
6405 separately. Build a vector of the types for these
6406 non-type template parameters in PACKED_TYPES. */
6408 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6409 packed_types
= tsubst_pack_expansion (expansion
, args
,
6412 if (packed_types
== error_mark_node
)
6413 return error_mark_node
;
6415 /* Check that we have the right number of arguments. */
6417 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6418 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6421 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6422 error ("wrong number of template arguments (%d, should be %d)",
6423 nargs
, needed_parms
);
6424 return error_mark_node
;
6427 /* If we aren't able to check the actual arguments now
6428 (because they haven't been expanded yet), we can at least
6429 verify that all of the types used for the non-type
6430 template parameter pack are, in fact, valid for non-type
6431 template parameters. */
6433 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6435 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6436 for (j
= 0; j
< len
; ++j
)
6438 tree t
= TREE_VEC_ELT (packed_types
, j
);
6439 if (invalid_nontype_parm_type_p (t
, complain
))
6440 return error_mark_node
;
6445 /* Convert the remaining arguments, which will be a part of the
6446 parameter pack "parm". */
6447 for (; arg_idx
< nargs
; ++arg_idx
)
6449 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6450 tree actual_parm
= TREE_VALUE (parm
);
6452 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6454 /* When we have a vector of types (corresponding to the
6455 non-type template parameter pack that uses parameter
6456 packs in its type, as mention above), and the
6457 argument is not an expansion (which expands to a
6458 currently unknown number of arguments), clone the
6459 parm and give it the next type in PACKED_TYPES. */
6460 actual_parm
= copy_node (actual_parm
);
6461 TREE_TYPE (actual_parm
) =
6462 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6465 if (arg
== error_mark_node
)
6467 if (complain
& tf_error
)
6468 error ("template argument %d is invalid", arg_idx
+ 1);
6471 arg
= convert_template_argument (actual_parm
,
6472 arg
, new_args
, complain
, parm_idx
,
6474 if (arg
== error_mark_node
)
6476 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6479 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6480 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6481 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6484 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6485 TREE_TYPE (argument_pack
)
6486 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6487 TREE_CONSTANT (argument_pack
) = 1;
6490 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6491 #ifdef ENABLE_CHECKING
6492 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6493 TREE_VEC_LENGTH (packed_args
));
6495 return argument_pack
;
6498 /* Returns true if the template argument vector ARGS contains
6499 any pack expansions, false otherwise. */
6502 any_pack_expanson_args_p (tree args
)
6506 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6507 if (PACK_EXPANSION_P (TREE_VEC_ELT (args
, i
)))
6512 /* Convert all template arguments to their appropriate types, and
6513 return a vector containing the innermost resulting template
6514 arguments. If any error occurs, return error_mark_node. Error and
6515 warning messages are issued under control of COMPLAIN.
6517 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6518 for arguments not specified in ARGS. Otherwise, if
6519 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6520 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6521 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6525 coerce_template_parms (tree parms
,
6528 tsubst_flags_t complain
,
6529 bool require_all_args
,
6530 bool use_default_args
)
6532 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6535 tree new_inner_args
;
6536 int saved_unevaluated_operand
;
6537 int saved_inhibit_evaluation_warnings
;
6539 /* When used as a boolean value, indicates whether this is a
6540 variadic template parameter list. Since it's an int, we can also
6541 subtract it from nparms to get the number of non-variadic
6544 int post_variadic_parms
= 0;
6546 if (args
== error_mark_node
)
6547 return error_mark_node
;
6549 nparms
= TREE_VEC_LENGTH (parms
);
6551 /* Determine if there are any parameter packs. */
6552 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6554 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6556 ++post_variadic_parms
;
6557 if (template_parameter_pack_p (tparm
))
6561 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6562 /* If there are no parameters that follow a parameter pack, we need to
6563 expand any argument packs so that we can deduce a parameter pack from
6564 some non-packed args followed by an argument pack, as in variadic85.C.
6565 If there are such parameters, we need to leave argument packs intact
6566 so the arguments are assigned properly. This can happen when dealing
6567 with a nested class inside a partial specialization of a class
6568 template, as in variadic92.C, or when deducing a template parameter pack
6569 from a sub-declarator, as in variadic114.C. */
6570 if (!post_variadic_parms
)
6571 inner_args
= expand_template_argument_pack (inner_args
);
6573 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6574 if ((nargs
> nparms
&& !variadic_p
)
6575 || (nargs
< nparms
- variadic_p
6577 && !any_pack_expanson_args_p (inner_args
)
6578 && (!use_default_args
6579 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6580 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6582 if (complain
& tf_error
)
6586 nparms
-= variadic_p
;
6587 error ("wrong number of template arguments "
6588 "(%d, should be %d or more)", nargs
, nparms
);
6591 error ("wrong number of template arguments "
6592 "(%d, should be %d)", nargs
, nparms
);
6595 error ("provided for %q+D", in_decl
);
6598 return error_mark_node
;
6601 /* We need to evaluate the template arguments, even though this
6602 template-id may be nested within a "sizeof". */
6603 saved_unevaluated_operand
= cp_unevaluated_operand
;
6604 cp_unevaluated_operand
= 0;
6605 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6606 c_inhibit_evaluation_warnings
= 0;
6607 new_inner_args
= make_tree_vec (nparms
);
6608 new_args
= add_outermost_template_args (args
, new_inner_args
);
6609 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6614 /* Get the Ith template parameter. */
6615 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6617 if (parm
== error_mark_node
)
6619 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6623 /* Calculate the next argument. */
6624 if (arg_idx
< nargs
)
6625 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6629 if (template_parameter_pack_p (TREE_VALUE (parm
))
6630 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6632 /* All remaining arguments will be placed in the
6633 template parameter pack PARM. */
6634 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6635 inner_args
, arg_idx
,
6639 /* Store this argument. */
6640 if (arg
== error_mark_node
)
6642 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6644 /* We are done with all of the arguments. */
6651 if (PACK_EXPANSION_P (arg
))
6653 /* We don't know how many args we have yet, just
6654 use the unconverted ones for now. */
6655 new_inner_args
= inner_args
;
6659 else if (require_all_args
)
6661 /* There must be a default arg in this case. */
6662 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6664 /* The position of the first default template argument,
6665 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6667 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6668 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6673 if (arg
== error_mark_node
)
6675 if (complain
& tf_error
)
6676 error ("template argument %d is invalid", arg_idx
+ 1);
6679 /* This only occurs if there was an error in the template
6680 parameter list itself (which we would already have
6681 reported) that we are trying to recover from, e.g., a class
6682 template with a parameter list such as
6683 template<typename..., typename>. */
6686 arg
= convert_template_argument (TREE_VALUE (parm
),
6687 arg
, new_args
, complain
,
6690 if (arg
== error_mark_node
)
6692 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6694 cp_unevaluated_operand
= saved_unevaluated_operand
;
6695 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6698 return error_mark_node
;
6700 #ifdef ENABLE_CHECKING
6701 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6702 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6703 TREE_VEC_LENGTH (new_inner_args
));
6706 return new_inner_args
;
6709 /* Returns 1 if template args OT and NT are equivalent. */
6712 template_args_equal (tree ot
, tree nt
)
6716 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6719 if (TREE_CODE (nt
) == TREE_VEC
)
6720 /* For member templates */
6721 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6722 else if (PACK_EXPANSION_P (ot
))
6723 return (PACK_EXPANSION_P (nt
)
6724 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6725 PACK_EXPANSION_PATTERN (nt
))
6726 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6727 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6728 else if (ARGUMENT_PACK_P (ot
))
6733 if (!ARGUMENT_PACK_P (nt
))
6736 opack
= ARGUMENT_PACK_ARGS (ot
);
6737 npack
= ARGUMENT_PACK_ARGS (nt
);
6738 len
= TREE_VEC_LENGTH (opack
);
6739 if (TREE_VEC_LENGTH (npack
) != len
)
6741 for (i
= 0; i
< len
; ++i
)
6742 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6743 TREE_VEC_ELT (npack
, i
)))
6747 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6749 /* We get here probably because we are in the middle of substituting
6750 into the pattern of a pack expansion. In that case the
6751 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6752 interested in. So we want to use the initial pack argument for
6754 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6755 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6756 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6757 return template_args_equal (ot
, nt
);
6759 else if (TYPE_P (nt
))
6760 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6761 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6764 return cp_tree_equal (ot
, nt
);
6767 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6768 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6769 NEWARG_PTR with the offending arguments if they are non-NULL. */
6772 comp_template_args_with_info (tree oldargs
, tree newargs
,
6773 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6777 if (oldargs
== newargs
)
6780 if (!oldargs
|| !newargs
)
6783 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6786 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6788 tree nt
= TREE_VEC_ELT (newargs
, i
);
6789 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6791 if (! template_args_equal (ot
, nt
))
6793 if (oldarg_ptr
!= NULL
)
6795 if (newarg_ptr
!= NULL
)
6803 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
6804 of template arguments. Returns 0 otherwise. */
6807 comp_template_args (tree oldargs
, tree newargs
)
6809 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
6813 add_pending_template (tree d
)
6815 tree ti
= (TYPE_P (d
)
6816 ? CLASSTYPE_TEMPLATE_INFO (d
)
6817 : DECL_TEMPLATE_INFO (d
));
6818 struct pending_template
*pt
;
6821 if (TI_PENDING_TEMPLATE_FLAG (ti
))
6824 /* We are called both from instantiate_decl, where we've already had a
6825 tinst_level pushed, and instantiate_template, where we haven't.
6827 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
6830 push_tinst_level (d
);
6832 pt
= ggc_alloc_pending_template ();
6834 pt
->tinst
= current_tinst_level
;
6835 if (last_pending_template
)
6836 last_pending_template
->next
= pt
;
6838 pending_templates
= pt
;
6840 last_pending_template
= pt
;
6842 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
6849 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
6850 ARGLIST. Valid choices for FNS are given in the cp-tree.def
6851 documentation for TEMPLATE_ID_EXPR. */
6854 lookup_template_function (tree fns
, tree arglist
)
6858 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
6859 return error_mark_node
;
6861 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
6863 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
6865 error ("%q#D is not a function template", fns
);
6866 return error_mark_node
;
6869 if (BASELINK_P (fns
))
6871 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
6873 BASELINK_FUNCTIONS (fns
),
6878 type
= TREE_TYPE (fns
);
6879 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
6880 type
= unknown_type_node
;
6882 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
6885 /* Within the scope of a template class S<T>, the name S gets bound
6886 (in build_self_reference) to a TYPE_DECL for the class, not a
6887 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
6888 or one of its enclosing classes, and that type is a template,
6889 return the associated TEMPLATE_DECL. Otherwise, the original
6892 Also handle the case when DECL is a TREE_LIST of ambiguous
6893 injected-class-names from different bases. */
6896 maybe_get_template_decl_from_type_decl (tree decl
)
6898 if (decl
== NULL_TREE
)
6901 /* DR 176: A lookup that finds an injected-class-name (10.2
6902 [class.member.lookup]) can result in an ambiguity in certain cases
6903 (for example, if it is found in more than one base class). If all of
6904 the injected-class-names that are found refer to specializations of
6905 the same class template, and if the name is followed by a
6906 template-argument-list, the reference refers to the class template
6907 itself and not a specialization thereof, and is not ambiguous. */
6908 if (TREE_CODE (decl
) == TREE_LIST
)
6910 tree t
, tmpl
= NULL_TREE
;
6911 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
6913 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
6916 else if (tmpl
!= elt
)
6919 if (tmpl
&& t
== NULL_TREE
)
6925 return (decl
!= NULL_TREE
6926 && DECL_SELF_REFERENCE_P (decl
)
6927 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
6928 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
6931 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
6932 parameters, find the desired type.
6934 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
6936 IN_DECL, if non-NULL, is the template declaration we are trying to
6939 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
6940 the class we are looking up.
6942 Issue error and warning messages under control of COMPLAIN.
6944 If the template class is really a local class in a template
6945 function, then the FUNCTION_CONTEXT is the function in which it is
6948 ??? Note that this function is currently called *twice* for each
6949 template-id: the first time from the parser, while creating the
6950 incomplete type (finish_template_type), and the second type during the
6951 real instantiation (instantiate_template_class). This is surely something
6952 that we want to avoid. It also causes some problems with argument
6953 coercion (see convert_nontype_argument for more information on this). */
6956 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
6957 int entering_scope
, tsubst_flags_t complain
)
6959 tree templ
= NULL_TREE
, parmlist
;
6966 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
6968 tree value
= innermost_non_namespace_value (d1
);
6969 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
6974 push_decl_namespace (context
);
6975 templ
= lookup_name (d1
);
6976 templ
= maybe_get_template_decl_from_type_decl (templ
);
6978 pop_decl_namespace ();
6981 context
= DECL_CONTEXT (templ
);
6983 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
6985 tree type
= TREE_TYPE (d1
);
6987 /* If we are declaring a constructor, say A<T>::A<T>, we will get
6988 an implicit typename for the second A. Deal with it. */
6989 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
6990 type
= TREE_TYPE (type
);
6992 if (CLASSTYPE_TEMPLATE_INFO (type
))
6994 templ
= CLASSTYPE_TI_TEMPLATE (type
);
6995 d1
= DECL_NAME (templ
);
6998 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
6999 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7001 templ
= TYPE_TI_TEMPLATE (d1
);
7002 d1
= DECL_NAME (templ
);
7004 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7005 && DECL_TEMPLATE_RESULT (d1
)
7006 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7009 d1
= DECL_NAME (templ
);
7010 context
= DECL_CONTEXT (templ
);
7013 /* Issue an error message if we didn't find a template. */
7016 if (complain
& tf_error
)
7017 error ("%qT is not a template", d1
);
7018 return error_mark_node
;
7021 if (TREE_CODE (templ
) != TEMPLATE_DECL
7022 /* Make sure it's a user visible template, if it was named by
7024 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7025 && !PRIMARY_TEMPLATE_P (templ
)))
7027 if (complain
& tf_error
)
7029 error ("non-template type %qT used as a template", d1
);
7031 error ("for template declaration %q+D", in_decl
);
7033 return error_mark_node
;
7036 complain
&= ~tf_user
;
7038 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7040 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7041 template arguments */
7047 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7049 /* Consider an example where a template template parameter declared as
7051 template <class T, class U = std::allocator<T> > class TT
7053 The template parameter level of T and U are one level larger than
7054 of TT. To proper process the default argument of U, say when an
7055 instantiation `TT<int>' is seen, we need to build the full
7056 arguments containing {int} as the innermost level. Outer levels,
7057 available when not appearing as default template argument, can be
7058 obtained from the arguments of the enclosing template.
7060 Suppose that TT is later substituted with std::vector. The above
7061 instantiation is `TT<int, std::allocator<T> >' with TT at
7062 level 1, and T at level 2, while the template arguments at level 1
7063 becomes {std::vector} and the inner level 2 is {int}. */
7065 outer
= DECL_CONTEXT (templ
);
7067 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7068 else if (current_template_parms
)
7069 /* This is an argument of the current template, so we haven't set
7070 DECL_CONTEXT yet. */
7071 outer
= current_template_args ();
7074 arglist
= add_to_template_args (outer
, arglist
);
7076 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7078 /*require_all_args=*/true,
7079 /*use_default_args=*/true);
7080 if (arglist2
== error_mark_node
7081 || (!uses_template_parms (arglist2
)
7082 && check_instantiated_args (templ
, arglist2
, complain
)))
7083 return error_mark_node
;
7085 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7090 tree template_type
= TREE_TYPE (templ
);
7093 tree found
= NULL_TREE
;
7096 int is_dependent_type
;
7097 int use_partial_inst_tmpl
= false;
7099 if (template_type
== error_mark_node
)
7100 /* An error occured while building the template TEMPL, and a
7101 diagnostic has most certainly been emitted for that
7102 already. Let's propagate that error. */
7103 return error_mark_node
;
7105 gen_tmpl
= most_general_template (templ
);
7106 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7107 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7108 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7110 if (arg_depth
== 1 && parm_depth
> 1)
7112 /* We've been given an incomplete set of template arguments.
7115 template <class T> struct S1 {
7116 template <class U> struct S2 {};
7117 template <class U> struct S2<U*> {};
7120 we will be called with an ARGLIST of `U*', but the
7121 TEMPLATE will be `template <class T> template
7122 <class U> struct S1<T>::S2'. We must fill in the missing
7125 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7127 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7130 /* Now we should have enough arguments. */
7131 gcc_assert (parm_depth
== arg_depth
);
7133 /* From here on, we're only interested in the most general
7136 /* Calculate the BOUND_ARGS. These will be the args that are
7137 actually tsubst'd into the definition to create the
7141 /* We have multiple levels of arguments to coerce, at once. */
7143 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7145 tree bound_args
= make_tree_vec (parm_depth
);
7147 for (i
= saved_depth
,
7148 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7149 i
> 0 && t
!= NULL_TREE
;
7150 --i
, t
= TREE_CHAIN (t
))
7153 if (i
== saved_depth
)
7154 a
= coerce_template_parms (TREE_VALUE (t
),
7157 /*require_all_args=*/true,
7158 /*use_default_args=*/true);
7160 /* Outer levels should have already been coerced. */
7161 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7163 /* Don't process further if one of the levels fails. */
7164 if (a
== error_mark_node
)
7166 /* Restore the ARGLIST to its full size. */
7167 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7168 return error_mark_node
;
7171 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7173 /* We temporarily reduce the length of the ARGLIST so
7174 that coerce_template_parms will see only the arguments
7175 corresponding to the template parameters it is
7177 TREE_VEC_LENGTH (arglist
)--;
7180 /* Restore the ARGLIST to its full size. */
7181 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7183 arglist
= bound_args
;
7187 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7188 INNERMOST_TEMPLATE_ARGS (arglist
),
7191 /*require_all_args=*/true,
7192 /*use_default_args=*/true);
7194 if (arglist
== error_mark_node
)
7195 /* We were unable to bind the arguments. */
7196 return error_mark_node
;
7198 /* In the scope of a template class, explicit references to the
7199 template class refer to the type of the template, not any
7200 instantiation of it. For example, in:
7202 template <class T> class C { void f(C<T>); }
7204 the `C<T>' is just the same as `C'. Outside of the
7205 class, however, such a reference is an instantiation. */
7207 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7208 || currently_open_class (template_type
))
7209 /* comp_template_args is expensive, check it last. */
7210 && comp_template_args (TYPE_TI_ARGS (template_type
),
7212 return template_type
;
7214 /* If we already have this specialization, return it. */
7215 elt
.tmpl
= gen_tmpl
;
7217 hash
= hash_specialization (&elt
);
7218 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7224 is_dependent_type
= uses_template_parms (arglist
);
7226 /* If the deduced arguments are invalid, then the binding
7228 if (!is_dependent_type
7229 && check_instantiated_args (gen_tmpl
,
7230 INNERMOST_TEMPLATE_ARGS (arglist
),
7232 return error_mark_node
;
7234 if (!is_dependent_type
7235 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7236 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7237 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7239 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7240 DECL_NAME (gen_tmpl
),
7241 /*tag_scope=*/ts_global
);
7245 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7247 if (context
== error_mark_node
)
7248 return error_mark_node
;
7251 context
= global_namespace
;
7253 /* Create the type. */
7254 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7256 if (!is_dependent_type
)
7258 set_current_access_from_decl (TYPE_NAME (template_type
));
7259 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7260 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7261 arglist
, complain
, in_decl
),
7262 SCOPED_ENUM_P (template_type
), NULL
);
7266 /* We don't want to call start_enum for this type, since
7267 the values for the enumeration constants may involve
7268 template parameters. And, no one should be interested
7269 in the enumeration constants for such a type. */
7270 t
= cxx_make_type (ENUMERAL_TYPE
);
7271 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7273 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7274 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7275 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7277 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7279 /* The user referred to a specialization of an alias
7280 template represented by GEN_TMPL.
7282 [temp.alias]/2 says:
7284 When a template-id refers to the specialization of an
7285 alias template, it is equivalent to the associated
7286 type obtained by substitution of its
7287 template-arguments for the template-parameters in the
7288 type-id of the alias template. */
7290 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7291 /* Note that the call above (by indirectly calling
7292 register_specialization in tsubst_decl) registers the
7293 TYPE_DECL representing the specialization of the alias
7294 template. So next time someone substitutes ARGLIST for
7295 the template parms into the alias template (GEN_TMPL),
7296 she'll get that TYPE_DECL back. */
7298 if (t
== error_mark_node
)
7301 else if (CLASS_TYPE_P (template_type
))
7303 t
= make_class_type (TREE_CODE (template_type
));
7304 CLASSTYPE_DECLARED_CLASS (t
)
7305 = CLASSTYPE_DECLARED_CLASS (template_type
);
7306 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7307 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7309 /* A local class. Make sure the decl gets registered properly. */
7310 if (context
== current_function_decl
)
7311 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7313 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7314 /* This instantiation is another name for the primary
7315 template type. Set the TYPE_CANONICAL field
7317 TYPE_CANONICAL (t
) = template_type
;
7318 else if (any_template_arguments_need_structural_equality_p (arglist
))
7319 /* Some of the template arguments require structural
7320 equality testing, so this template class requires
7321 structural equality testing. */
7322 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7327 /* If we called start_enum or pushtag above, this information
7328 will already be set up. */
7331 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7333 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7334 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7335 DECL_SOURCE_LOCATION (type_decl
)
7336 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7339 type_decl
= TYPE_NAME (t
);
7341 if (CLASS_TYPE_P (template_type
))
7343 TREE_PRIVATE (type_decl
)
7344 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
7345 TREE_PROTECTED (type_decl
)
7346 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
7347 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7349 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7350 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7354 /* Let's consider the explicit specialization of a member
7355 of a class template specialization that is implicitely instantiated,
7360 template<class U> struct M {}; //#0
7365 struct S<int>::M<char> //#1
7369 [temp.expl.spec]/4 says this is valid.
7371 In this case, when we write:
7374 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7377 When we encounter #1, we want to store the partial instantiation
7378 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7380 For all cases other than this "explicit specialization of member of a
7381 class template", we just want to store the most general template into
7382 the CLASSTYPE_TI_TEMPLATE of M.
7384 This case of "explicit specialization of member of a class template"
7386 1/ the enclosing class is an instantiation of, and therefore not
7387 the same as, the context of the most general template, and
7388 2/ we aren't looking at the partial instantiation itself, i.e.
7389 the innermost arguments are not the same as the innermost parms of
7390 the most general template.
7392 So it's only when 1/ and 2/ happens that we want to use the partial
7393 instantiation of the member template in lieu of its most general
7396 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7397 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7398 /* the enclosing class must be an instantiation... */
7399 && CLASS_TYPE_P (context
)
7400 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7402 tree partial_inst_args
;
7403 TREE_VEC_LENGTH (arglist
)--;
7404 ++processing_template_decl
;
7406 tsubst (INNERMOST_TEMPLATE_ARGS
7407 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7408 arglist
, complain
, NULL_TREE
);
7409 --processing_template_decl
;
7410 TREE_VEC_LENGTH (arglist
)++;
7411 use_partial_inst_tmpl
=
7412 /*...and we must not be looking at the partial instantiation
7414 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7418 if (!use_partial_inst_tmpl
)
7419 /* This case is easy; there are no member templates involved. */
7423 /* This is a full instantiation of a member template. Find
7424 the partial instantiation of which this is an instance. */
7426 /* Temporarily reduce by one the number of levels in the ARGLIST
7427 so as to avoid comparing the last set of arguments. */
7428 TREE_VEC_LENGTH (arglist
)--;
7429 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7430 TREE_VEC_LENGTH (arglist
)++;
7431 /* FOUND is either a proper class type, or an alias
7432 template specialization. In the later case, it's a
7433 TYPE_DECL, resulting from the substituting of arguments
7434 for parameters in the TYPE_DECL of the alias template
7435 done earlier. So be careful while getting the template
7437 found
= TREE_CODE (found
) == TYPE_DECL
7438 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7439 : CLASSTYPE_TI_TEMPLATE (found
);
7442 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7445 slot
= htab_find_slot_with_hash (type_specializations
,
7446 &elt
, hash
, INSERT
);
7447 entry
= ggc_alloc_spec_entry ();
7451 /* Note this use of the partial instantiation so we can check it
7452 later in maybe_process_partial_specialization. */
7453 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7454 = tree_cons (arglist
, t
,
7455 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7457 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7458 /* Now that the type has been registered on the instantiations
7459 list, we set up the enumerators. Because the enumeration
7460 constants may involve the enumeration type itself, we make
7461 sure to register the type first, and then create the
7462 constants. That way, doing tsubst_expr for the enumeration
7463 constants won't result in recursive calls here; we'll find
7464 the instantiation and exit above. */
7465 tsubst_enum (template_type
, t
, arglist
);
7467 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7468 /* If the type makes use of template parameters, the
7469 code that generates debugging information will crash. */
7470 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7472 /* Possibly limit visibility based on template args. */
7473 TREE_PUBLIC (type_decl
) = 1;
7474 determine_visibility (type_decl
);
7480 /* Wrapper for lookup_template_class_1. */
7483 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7484 int entering_scope
, tsubst_flags_t complain
)
7487 timevar_push (TV_TEMPLATE_INST
);
7488 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7489 entering_scope
, complain
);
7490 timevar_pop (TV_TEMPLATE_INST
);
7498 /* True when we should also visit template parameters that occur in
7499 non-deduced contexts. */
7500 bool include_nondeduced_p
;
7501 struct pointer_set_t
*visited
;
7504 /* Called from for_each_template_parm via walk_tree. */
7507 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7510 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7511 tree_fn_t fn
= pfd
->fn
;
7512 void *data
= pfd
->data
;
7515 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7516 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7517 pfd
->include_nondeduced_p
))
7518 return error_mark_node
;
7520 switch (TREE_CODE (t
))
7523 if (TYPE_PTRMEMFUNC_P (t
))
7529 if (!TYPE_TEMPLATE_INFO (t
))
7531 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7532 fn
, data
, pfd
->visited
,
7533 pfd
->include_nondeduced_p
))
7534 return error_mark_node
;
7538 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7539 fn
, data
, pfd
->visited
,
7540 pfd
->include_nondeduced_p
)
7541 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7542 fn
, data
, pfd
->visited
,
7543 pfd
->include_nondeduced_p
))
7544 return error_mark_node
;
7548 /* Since we're not going to walk subtrees, we have to do this
7550 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7551 pfd
->visited
, pfd
->include_nondeduced_p
))
7552 return error_mark_node
;
7556 /* Check the return type. */
7557 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7558 pfd
->include_nondeduced_p
))
7559 return error_mark_node
;
7561 /* Check the parameter types. Since default arguments are not
7562 instantiated until they are needed, the TYPE_ARG_TYPES may
7563 contain expressions that involve template parameters. But,
7564 no-one should be looking at them yet. And, once they're
7565 instantiated, they don't contain template parameters, so
7566 there's no point in looking at them then, either. */
7570 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7571 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7572 pfd
->visited
, pfd
->include_nondeduced_p
))
7573 return error_mark_node
;
7575 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7576 want walk_tree walking into them itself. */
7582 case UNDERLYING_TYPE
:
7583 if (pfd
->include_nondeduced_p
7584 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7586 pfd
->include_nondeduced_p
))
7587 return error_mark_node
;
7592 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7593 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7594 pfd
->visited
, pfd
->include_nondeduced_p
))
7595 return error_mark_node
;
7600 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7601 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7602 pfd
->visited
, pfd
->include_nondeduced_p
))
7603 return error_mark_node
;
7604 if (DECL_CONTEXT (t
)
7605 && pfd
->include_nondeduced_p
7606 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7607 pfd
->visited
, pfd
->include_nondeduced_p
))
7608 return error_mark_node
;
7611 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7612 /* Record template parameters such as `T' inside `TT<T>'. */
7613 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7614 pfd
->include_nondeduced_p
))
7615 return error_mark_node
;
7618 case TEMPLATE_TEMPLATE_PARM
:
7619 case TEMPLATE_TYPE_PARM
:
7620 case TEMPLATE_PARM_INDEX
:
7621 if (fn
&& (*fn
)(t
, data
))
7622 return error_mark_node
;
7624 return error_mark_node
;
7628 /* A template template parameter is encountered. */
7629 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7630 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7631 pfd
->include_nondeduced_p
))
7632 return error_mark_node
;
7634 /* Already substituted template template parameter */
7640 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7642 pfd
->include_nondeduced_p
))
7643 return error_mark_node
;
7647 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7648 && pfd
->include_nondeduced_p
7649 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7650 (TREE_TYPE (t
)), fn
, data
,
7651 pfd
->visited
, pfd
->include_nondeduced_p
))
7652 return error_mark_node
;
7657 /* If there's no type, then this thing must be some expression
7658 involving template parameters. */
7659 if (!fn
&& !TREE_TYPE (t
))
7660 return error_mark_node
;
7665 case IMPLICIT_CONV_EXPR
:
7666 case REINTERPRET_CAST_EXPR
:
7667 case CONST_CAST_EXPR
:
7668 case STATIC_CAST_EXPR
:
7669 case DYNAMIC_CAST_EXPR
:
7673 case PSEUDO_DTOR_EXPR
:
7675 return error_mark_node
;
7682 /* We didn't find any template parameters we liked. */
7686 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7687 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7688 call FN with the parameter and the DATA.
7689 If FN returns nonzero, the iteration is terminated, and
7690 for_each_template_parm returns 1. Otherwise, the iteration
7691 continues. If FN never returns a nonzero value, the value
7692 returned by for_each_template_parm is 0. If FN is NULL, it is
7693 considered to be the function which always returns 1.
7695 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7696 parameters that occur in non-deduced contexts. When false, only
7697 visits those template parameters that can be deduced. */
7700 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7701 struct pointer_set_t
*visited
,
7702 bool include_nondeduced_p
)
7704 struct pair_fn_data pfd
;
7710 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7712 /* Walk the tree. (Conceptually, we would like to walk without
7713 duplicates, but for_each_template_parm_r recursively calls
7714 for_each_template_parm, so we would need to reorganize a fair
7715 bit to use walk_tree_without_duplicates, so we keep our own
7718 pfd
.visited
= visited
;
7720 pfd
.visited
= pointer_set_create ();
7721 result
= cp_walk_tree (&t
,
7722 for_each_template_parm_r
,
7724 pfd
.visited
) != NULL_TREE
;
7729 pointer_set_destroy (pfd
.visited
);
7736 /* Returns true if T depends on any template parameter. */
7739 uses_template_parms (tree t
)
7742 int saved_processing_template_decl
;
7744 saved_processing_template_decl
= processing_template_decl
;
7745 if (!saved_processing_template_decl
)
7746 processing_template_decl
= 1;
7748 dependent_p
= dependent_type_p (t
);
7749 else if (TREE_CODE (t
) == TREE_VEC
)
7750 dependent_p
= any_dependent_template_arguments_p (t
);
7751 else if (TREE_CODE (t
) == TREE_LIST
)
7752 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7753 || uses_template_parms (TREE_CHAIN (t
)));
7754 else if (TREE_CODE (t
) == TYPE_DECL
)
7755 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7758 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7759 || TREE_CODE (t
) == OVERLOAD
7761 || TREE_CODE (t
) == IDENTIFIER_NODE
7762 || TREE_CODE (t
) == TRAIT_EXPR
7763 || TREE_CODE (t
) == CONSTRUCTOR
7764 || CONSTANT_CLASS_P (t
))
7765 dependent_p
= (type_dependent_expression_p (t
)
7766 || value_dependent_expression_p (t
));
7769 gcc_assert (t
== error_mark_node
);
7770 dependent_p
= false;
7773 processing_template_decl
= saved_processing_template_decl
;
7778 /* Returns true iff current_function_decl is an incompletely instantiated
7779 template. Useful instead of processing_template_decl because the latter
7780 is set to 0 during fold_non_dependent_expr. */
7783 in_template_function (void)
7785 tree fn
= current_function_decl
;
7787 ++processing_template_decl
;
7788 ret
= (fn
&& DECL_LANG_SPECIFIC (fn
)
7789 && DECL_TEMPLATE_INFO (fn
)
7790 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn
)));
7791 --processing_template_decl
;
7795 /* Returns true if T depends on any template parameter with level LEVEL. */
7798 uses_template_parms_level (tree t
, int level
)
7800 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7801 /*include_nondeduced_p=*/true);
7804 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7805 ill-formed translation unit, i.e. a variable or function that isn't
7806 usable in a constant expression. */
7809 neglectable_inst_p (tree d
)
7812 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
7813 : decl_maybe_constant_var_p (d
)));
7816 /* Returns TRUE iff we should refuse to instantiate DECL because it's
7817 neglectable and instantiated from within an erroneous instantiation. */
7820 limit_bad_template_recursion (tree decl
)
7822 struct tinst_level
*lev
= current_tinst_level
;
7823 int errs
= errorcount
+ sorrycount
;
7824 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
7827 for (; lev
; lev
= lev
->next
)
7828 if (neglectable_inst_p (lev
->decl
))
7831 return (lev
&& errs
> lev
->errors
);
7834 static int tinst_depth
;
7835 extern int max_tinst_depth
;
7838 static GTY(()) struct tinst_level
*last_error_tinst_level
;
7840 /* We're starting to instantiate D; record the template instantiation context
7841 for diagnostics and to restore it later. */
7844 push_tinst_level (tree d
)
7846 struct tinst_level
*new_level
;
7848 if (tinst_depth
>= max_tinst_depth
)
7850 last_error_tinst_level
= current_tinst_level
;
7851 if (TREE_CODE (d
) == TREE_LIST
)
7852 error ("template instantiation depth exceeds maximum of %d (use "
7853 "-ftemplate-depth= to increase the maximum) substituting %qS",
7854 max_tinst_depth
, d
);
7856 error ("template instantiation depth exceeds maximum of %d (use "
7857 "-ftemplate-depth= to increase the maximum) instantiating %qD",
7858 max_tinst_depth
, d
);
7860 print_instantiation_context ();
7865 /* If the current instantiation caused problems, don't let it instantiate
7866 anything else. Do allow deduction substitution and decls usable in
7867 constant expressions. */
7868 if (limit_bad_template_recursion (d
))
7871 new_level
= ggc_alloc_tinst_level ();
7872 new_level
->decl
= d
;
7873 new_level
->locus
= input_location
;
7874 new_level
->errors
= errorcount
+sorrycount
;
7875 new_level
->in_system_header_p
= in_system_header
;
7876 new_level
->next
= current_tinst_level
;
7877 current_tinst_level
= new_level
;
7880 if (GATHER_STATISTICS
&& (tinst_depth
> depth_reached
))
7881 depth_reached
= tinst_depth
;
7886 /* We're done instantiating this template; return to the instantiation
7890 pop_tinst_level (void)
7892 /* Restore the filename and line number stashed away when we started
7893 this instantiation. */
7894 input_location
= current_tinst_level
->locus
;
7895 current_tinst_level
= current_tinst_level
->next
;
7899 /* We're instantiating a deferred template; restore the template
7900 instantiation context in which the instantiation was requested, which
7901 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
7904 reopen_tinst_level (struct tinst_level
*level
)
7906 struct tinst_level
*t
;
7909 for (t
= level
; t
; t
= t
->next
)
7912 current_tinst_level
= level
;
7914 if (current_tinst_level
)
7915 current_tinst_level
->errors
= errorcount
+sorrycount
;
7919 /* Returns the TINST_LEVEL which gives the original instantiation
7922 struct tinst_level
*
7923 outermost_tinst_level (void)
7925 struct tinst_level
*level
= current_tinst_level
;
7928 level
= level
->next
;
7932 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
7933 vector of template arguments, as for tsubst.
7935 Returns an appropriate tsubst'd friend declaration. */
7938 tsubst_friend_function (tree decl
, tree args
)
7942 if (TREE_CODE (decl
) == FUNCTION_DECL
7943 && DECL_TEMPLATE_INSTANTIATION (decl
)
7944 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
7945 /* This was a friend declared with an explicit template
7946 argument list, e.g.:
7950 to indicate that f was a template instantiation, not a new
7951 function declaration. Now, we have to figure out what
7952 instantiation of what template. */
7954 tree template_id
, arglist
, fns
;
7957 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
7959 /* Friend functions are looked up in the containing namespace scope.
7960 We must enter that scope, to avoid finding member functions of the
7961 current class with same name. */
7962 push_nested_namespace (ns
);
7963 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
7964 tf_warning_or_error
, NULL_TREE
,
7965 /*integral_constant_expression_p=*/false);
7966 pop_nested_namespace (ns
);
7967 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
7968 tf_warning_or_error
, NULL_TREE
);
7969 template_id
= lookup_template_function (fns
, arglist
);
7971 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7972 tmpl
= determine_specialization (template_id
, new_friend
,
7974 /*need_member_template=*/0,
7975 TREE_VEC_LENGTH (args
),
7977 return instantiate_template (tmpl
, new_args
, tf_error
);
7980 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
7982 /* The NEW_FRIEND will look like an instantiation, to the
7983 compiler, but is not an instantiation from the point of view of
7984 the language. For example, we might have had:
7986 template <class T> struct S {
7987 template <class U> friend void f(T, U);
7990 Then, in S<int>, template <class U> void f(int, U) is not an
7991 instantiation of anything. */
7992 if (new_friend
== error_mark_node
)
7993 return error_mark_node
;
7995 DECL_USE_TEMPLATE (new_friend
) = 0;
7996 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
7998 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
7999 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8000 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8003 /* The mangled name for the NEW_FRIEND is incorrect. The function
8004 is not a template instantiation and should not be mangled like
8005 one. Therefore, we forget the mangling here; we'll recompute it
8006 later if we need it. */
8007 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8009 SET_DECL_RTL (new_friend
, NULL
);
8010 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8013 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8016 tree new_friend_template_info
;
8017 tree new_friend_result_template_info
;
8019 int new_friend_is_defn
;
8021 /* We must save some information from NEW_FRIEND before calling
8022 duplicate decls since that function will free NEW_FRIEND if
8024 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8025 new_friend_is_defn
=
8026 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8027 (template_for_substitution (new_friend
)))
8029 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8031 /* This declaration is a `primary' template. */
8032 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8034 new_friend_result_template_info
8035 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8038 new_friend_result_template_info
= NULL_TREE
;
8040 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8041 if (new_friend_is_defn
)
8042 DECL_INITIAL (new_friend
) = error_mark_node
;
8044 /* Inside pushdecl_namespace_level, we will push into the
8045 current namespace. However, the friend function should go
8046 into the namespace of the template. */
8047 ns
= decl_namespace_context (new_friend
);
8048 push_nested_namespace (ns
);
8049 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8050 pop_nested_namespace (ns
);
8052 if (old_decl
== error_mark_node
)
8053 return error_mark_node
;
8055 if (old_decl
!= new_friend
)
8057 /* This new friend declaration matched an existing
8058 declaration. For example, given:
8060 template <class T> void f(T);
8061 template <class U> class C {
8062 template <class T> friend void f(T) {}
8065 the friend declaration actually provides the definition
8066 of `f', once C has been instantiated for some type. So,
8067 old_decl will be the out-of-class template declaration,
8068 while new_friend is the in-class definition.
8070 But, if `f' was called before this point, the
8071 instantiation of `f' will have DECL_TI_ARGS corresponding
8072 to `T' but not to `U', references to which might appear
8073 in the definition of `f'. Previously, the most general
8074 template for an instantiation of `f' was the out-of-class
8075 version; now it is the in-class version. Therefore, we
8076 run through all specialization of `f', adding to their
8077 DECL_TI_ARGS appropriately. In particular, they need a
8078 new set of outer arguments, corresponding to the
8079 arguments for this class instantiation.
8081 The same situation can arise with something like this:
8084 template <class T> class C {
8088 when `C<int>' is instantiated. Now, `f(int)' is defined
8091 if (!new_friend_is_defn
)
8092 /* On the other hand, if the in-class declaration does
8093 *not* provide a definition, then we don't want to alter
8094 existing definitions. We can just leave everything
8099 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8100 tree new_args
= TI_ARGS (new_friend_template_info
);
8102 /* Overwrite whatever template info was there before, if
8103 any, with the new template information pertaining to
8105 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8107 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8109 /* We should have called reregister_specialization in
8111 gcc_assert (retrieve_specialization (new_template
,
8115 /* Instantiate it if the global has already been used. */
8116 if (DECL_ODR_USED (old_decl
))
8117 instantiate_decl (old_decl
, /*defer_ok=*/true,
8118 /*expl_inst_class_mem_p=*/false);
8124 /* Indicate that the old function template is a partial
8126 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8127 = new_friend_result_template_info
;
8129 gcc_assert (new_template
8130 == most_general_template (new_template
));
8131 gcc_assert (new_template
!= old_decl
);
8133 /* Reassign any specializations already in the hash table
8134 to the new more general template, and add the
8135 additional template args. */
8136 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8140 tree spec
= TREE_VALUE (t
);
8143 elt
.tmpl
= old_decl
;
8144 elt
.args
= DECL_TI_ARGS (spec
);
8145 elt
.spec
= NULL_TREE
;
8147 htab_remove_elt (decl_specializations
, &elt
);
8150 = add_outermost_template_args (new_args
,
8151 DECL_TI_ARGS (spec
));
8153 register_specialization
8154 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8157 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8161 /* The information from NEW_FRIEND has been merged into OLD_DECL
8162 by duplicate_decls. */
8163 new_friend
= old_decl
;
8168 tree context
= DECL_CONTEXT (new_friend
);
8172 template <class T> class C {
8173 template <class U> friend void C1<U>::f (); // case 1
8174 friend void C2<T>::f (); // case 2
8176 we only need to make sure CONTEXT is a complete type for
8177 case 2. To distinguish between the two cases, we note that
8178 CONTEXT of case 1 remains dependent type after tsubst while
8179 this isn't true for case 2. */
8180 ++processing_template_decl
;
8181 dependent_p
= dependent_type_p (context
);
8182 --processing_template_decl
;
8185 && !complete_type_or_else (context
, NULL_TREE
))
8186 return error_mark_node
;
8188 if (COMPLETE_TYPE_P (context
))
8190 /* Check to see that the declaration is really present, and,
8191 possibly obtain an improved declaration. */
8192 tree fn
= check_classfn (context
,
8193 new_friend
, NULL_TREE
);
8203 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8204 template arguments, as for tsubst.
8206 Returns an appropriate tsubst'd friend type or error_mark_node on
8210 tsubst_friend_class (tree friend_tmpl
, tree args
)
8216 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl
))
8218 tree t
= tsubst (TREE_TYPE (friend_tmpl
), args
, tf_none
, NULL_TREE
);
8219 return TREE_TYPE (t
);
8222 context
= CP_DECL_CONTEXT (friend_tmpl
);
8224 if (context
!= global_namespace
)
8226 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8227 push_nested_namespace (context
);
8229 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8232 /* Look for a class template declaration. We look for hidden names
8233 because two friend declarations of the same template are the
8234 same. For example, in:
8237 template <typename> friend class F;
8239 template <typename> struct B {
8240 template <typename> friend class F;
8243 both F templates are the same. */
8244 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8245 /*block_p=*/true, 0, LOOKUP_HIDDEN
);
8247 /* But, if we don't find one, it might be because we're in a
8248 situation like this:
8256 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8257 for `S<int>', not the TEMPLATE_DECL. */
8258 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8260 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8261 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8264 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8266 /* The friend template has already been declared. Just
8267 check to see that the declarations match, and install any new
8268 default parameters. We must tsubst the default parameters,
8269 of course. We only need the innermost template parameters
8270 because that is all that redeclare_class_template will look
8272 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8273 > TMPL_ARGS_DEPTH (args
))
8276 location_t saved_input_location
;
8277 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8278 args
, tf_warning_or_error
);
8280 saved_input_location
= input_location
;
8281 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8282 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8283 input_location
= saved_input_location
;
8287 friend_type
= TREE_TYPE (tmpl
);
8291 /* The friend template has not already been declared. In this
8292 case, the instantiation of the template class will cause the
8293 injection of this template into the global scope. */
8294 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8295 if (tmpl
== error_mark_node
)
8296 return error_mark_node
;
8298 /* The new TMPL is not an instantiation of anything, so we
8299 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8300 the new type because that is supposed to be the corresponding
8301 template decl, i.e., TMPL. */
8302 DECL_USE_TEMPLATE (tmpl
) = 0;
8303 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8304 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8305 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8306 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8308 /* Inject this template into the global scope. */
8309 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8312 if (context
!= global_namespace
)
8314 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8315 pop_nested_namespace (context
);
8317 pop_nested_class ();
8323 /* Returns zero if TYPE cannot be completed later due to circularity.
8324 Otherwise returns one. */
8327 can_complete_type_without_circularity (tree type
)
8329 if (type
== NULL_TREE
|| type
== error_mark_node
)
8331 else if (COMPLETE_TYPE_P (type
))
8333 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8334 return can_complete_type_without_circularity (TREE_TYPE (type
));
8335 else if (CLASS_TYPE_P (type
)
8336 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8342 /* Apply any attributes which had to be deferred until instantiation
8343 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8344 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8347 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8348 tree args
, tsubst_flags_t complain
, tree in_decl
)
8350 tree last_dep
= NULL_TREE
;
8354 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8355 if (ATTR_IS_DEPENDENT (t
))
8358 attributes
= copy_list (attributes
);
8362 if (DECL_P (*decl_p
))
8364 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8366 p
= &DECL_ATTRIBUTES (*decl_p
);
8369 p
= &TYPE_ATTRIBUTES (*decl_p
);
8373 tree late_attrs
= NULL_TREE
;
8374 tree
*q
= &late_attrs
;
8376 for (*p
= attributes
; *p
; )
8379 if (ATTR_IS_DEPENDENT (t
))
8381 *p
= TREE_CHAIN (t
);
8382 TREE_CHAIN (t
) = NULL_TREE
;
8383 /* If the first attribute argument is an identifier, don't
8384 pass it through tsubst. Attributes like mode, format,
8385 cleanup and several target specific attributes expect it
8388 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8389 && TREE_VALUE (TREE_VALUE (t
))
8390 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8391 == IDENTIFIER_NODE
))
8394 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8396 /*integral_constant_expression_p=*/false);
8397 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8399 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8404 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8405 /*integral_constant_expression_p=*/false);
8407 q
= &TREE_CHAIN (t
);
8410 p
= &TREE_CHAIN (t
);
8413 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8417 /* Perform (or defer) access check for typedefs that were referenced
8418 from within the template TMPL code.
8419 This is a subroutine of instantiate_decl and instantiate_class_template.
8420 TMPL is the template to consider and TARGS is the list of arguments of
8424 perform_typedefs_access_check (tree tmpl
, tree targs
)
8426 location_t saved_location
;
8428 qualified_typedef_usage_t
*iter
;
8431 || (!CLASS_TYPE_P (tmpl
)
8432 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8435 saved_location
= input_location
;
8436 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
8437 get_types_needing_access_check (tmpl
),
8440 tree type_decl
= iter
->typedef_decl
;
8441 tree type_scope
= iter
->context
;
8443 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8446 if (uses_template_parms (type_decl
))
8447 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8448 if (uses_template_parms (type_scope
))
8449 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8451 /* Make access check error messages point to the location
8452 of the use of the typedef. */
8453 input_location
= iter
->locus
;
8454 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8455 type_decl
, type_decl
,
8456 tf_warning_or_error
);
8458 input_location
= saved_location
;
8462 instantiate_class_template_1 (tree type
)
8464 tree templ
, args
, pattern
, t
, member
;
8468 unsigned int saved_maximum_field_alignment
;
8471 if (type
== error_mark_node
)
8472 return error_mark_node
;
8474 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8475 || uses_template_parms (type
))
8478 /* Figure out which template is being instantiated. */
8479 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8480 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8482 /* Determine what specialization of the original template to
8484 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8485 if (t
== error_mark_node
)
8487 TYPE_BEING_DEFINED (type
) = 1;
8488 return error_mark_node
;
8492 /* This TYPE is actually an instantiation of a partial
8493 specialization. We replace the innermost set of ARGS with
8494 the arguments appropriate for substitution. For example,
8497 template <class T> struct S {};
8498 template <class T> struct S<T*> {};
8500 and supposing that we are instantiating S<int*>, ARGS will
8501 presently be {int*} -- but we need {int}. */
8502 pattern
= TREE_TYPE (t
);
8503 args
= TREE_PURPOSE (t
);
8507 pattern
= TREE_TYPE (templ
);
8508 args
= CLASSTYPE_TI_ARGS (type
);
8511 /* If the template we're instantiating is incomplete, then clearly
8512 there's nothing we can do. */
8513 if (!COMPLETE_TYPE_P (pattern
))
8516 /* If we've recursively instantiated too many templates, stop. */
8517 if (! push_tinst_level (type
))
8520 /* Now we're really doing the instantiation. Mark the type as in
8521 the process of being defined. */
8522 TYPE_BEING_DEFINED (type
) = 1;
8524 /* We may be in the middle of deferred access check. Disable
8526 push_deferring_access_checks (dk_no_deferred
);
8528 fn_context
= decl_function_context (TYPE_MAIN_DECL (type
));
8530 push_to_top_level ();
8531 /* Use #pragma pack from the template context. */
8532 saved_maximum_field_alignment
= maximum_field_alignment
;
8533 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8535 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8537 /* Set the input location to the most specialized template definition.
8538 This is needed if tsubsting causes an error. */
8539 typedecl
= TYPE_MAIN_DECL (pattern
);
8540 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8541 DECL_SOURCE_LOCATION (typedecl
);
8543 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8544 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8545 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8546 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8547 if (ANON_AGGR_TYPE_P (pattern
))
8548 SET_ANON_AGGR_TYPE_P (type
);
8549 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8551 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8552 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8553 /* Adjust visibility for template arguments. */
8554 determine_visibility (TYPE_MAIN_DECL (type
));
8556 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8558 pbinfo
= TYPE_BINFO (pattern
);
8560 /* We should never instantiate a nested class before its enclosing
8561 class; we need to look up the nested class by name before we can
8562 instantiate it, and that lookup should instantiate the enclosing
8564 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8565 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8567 base_list
= NULL_TREE
;
8568 if (BINFO_N_BASE_BINFOS (pbinfo
))
8574 /* We must enter the scope containing the type, as that is where
8575 the accessibility of types named in dependent bases are
8577 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8579 /* Substitute into each of the bases to determine the actual
8581 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8584 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8585 tree expanded_bases
= NULL_TREE
;
8588 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8591 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8592 args
, tf_error
, NULL_TREE
);
8593 if (expanded_bases
== error_mark_node
)
8596 len
= TREE_VEC_LENGTH (expanded_bases
);
8599 for (idx
= 0; idx
< len
; idx
++)
8602 /* Extract the already-expanded base class. */
8603 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8605 /* Substitute to figure out the base class. */
8606 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8609 if (base
== error_mark_node
)
8612 base_list
= tree_cons (access
, base
, base_list
);
8613 if (BINFO_VIRTUAL_P (pbase_binfo
))
8614 TREE_TYPE (base_list
) = integer_type_node
;
8618 /* The list is now in reverse order; correct that. */
8619 base_list
= nreverse (base_list
);
8622 pop_scope (pushed_scope
);
8624 /* Now call xref_basetypes to set up all the base-class
8626 xref_basetypes (type
, base_list
);
8628 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8629 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8630 args
, tf_error
, NULL_TREE
);
8631 fixup_attribute_variants (type
);
8633 /* Now that our base classes are set up, enter the scope of the
8634 class, so that name lookups into base classes, etc. will work
8635 correctly. This is precisely analogous to what we do in
8636 begin_class_definition when defining an ordinary non-template
8637 class, except we also need to push the enclosing classes. */
8638 push_nested_class (type
);
8640 /* Now members are processed in the order of declaration. */
8641 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8642 member
; member
= TREE_CHAIN (member
))
8644 tree t
= TREE_VALUE (member
);
8646 if (TREE_PURPOSE (member
))
8650 /* Build new CLASSTYPE_NESTED_UTDS. */
8653 bool class_template_p
;
8655 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8656 && TYPE_LANG_SPECIFIC (t
)
8657 && CLASSTYPE_IS_TEMPLATE (t
));
8658 /* If the member is a class template, then -- even after
8659 substitution -- there may be dependent types in the
8660 template argument list for the class. We increment
8661 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8662 that function will assume that no types are dependent
8663 when outside of a template. */
8664 if (class_template_p
)
8665 ++processing_template_decl
;
8666 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8667 if (class_template_p
)
8668 --processing_template_decl
;
8669 if (newtag
== error_mark_node
)
8672 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8674 tree name
= TYPE_IDENTIFIER (t
);
8676 if (class_template_p
)
8677 /* Unfortunately, lookup_template_class sets
8678 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8679 instantiation (i.e., for the type of a member
8680 template class nested within a template class.)
8681 This behavior is required for
8682 maybe_process_partial_specialization to work
8683 correctly, but is not accurate in this case;
8684 the TAG is not an instantiation of anything.
8685 (The corresponding TEMPLATE_DECL is an
8686 instantiation, but the TYPE is not.) */
8687 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8689 /* Now, we call pushtag to put this NEWTAG into the scope of
8690 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8691 pushtag calling push_template_decl. We don't have to do
8692 this for enums because it will already have been done in
8695 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8696 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8699 else if (TREE_CODE (t
) == FUNCTION_DECL
8700 || DECL_FUNCTION_TEMPLATE_P (t
))
8702 /* Build new TYPE_METHODS. */
8705 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8706 ++processing_template_decl
;
8707 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8708 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8709 --processing_template_decl
;
8710 set_current_access_from_decl (r
);
8711 finish_member_declaration (r
);
8712 /* Instantiate members marked with attribute used. */
8713 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8718 /* Build new TYPE_FIELDS. */
8719 if (TREE_CODE (t
) == STATIC_ASSERT
)
8723 ++c_inhibit_evaluation_warnings
;
8725 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8726 tf_warning_or_error
, NULL_TREE
,
8727 /*integral_constant_expression_p=*/true);
8728 --c_inhibit_evaluation_warnings
;
8730 finish_static_assert (condition
,
8731 STATIC_ASSERT_MESSAGE (t
),
8732 STATIC_ASSERT_SOURCE_LOCATION (t
),
8735 else if (TREE_CODE (t
) != CONST_DECL
)
8739 /* The file and line for this declaration, to
8740 assist in error message reporting. Since we
8741 called push_tinst_level above, we don't need to
8743 input_location
= DECL_SOURCE_LOCATION (t
);
8745 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8746 ++processing_template_decl
;
8747 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8748 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8749 --processing_template_decl
;
8750 if (TREE_CODE (r
) == VAR_DECL
)
8754 [t]he initialization (and any associated
8755 side-effects) of a static data member does
8756 not occur unless the static data member is
8757 itself used in a way that requires the
8758 definition of the static data member to
8761 Therefore, we do not substitute into the
8762 initialized for the static data member here. */
8763 finish_static_data_member_decl
8766 /*init_const_expr_p=*/false,
8767 /*asmspec_tree=*/NULL_TREE
,
8769 /* Instantiate members marked with attribute used. */
8770 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8773 else if (TREE_CODE (r
) == FIELD_DECL
)
8775 /* Determine whether R has a valid type and can be
8776 completed later. If R is invalid, then it is
8777 replaced by error_mark_node so that it will not be
8778 added to TYPE_FIELDS. */
8779 tree rtype
= TREE_TYPE (r
);
8780 if (can_complete_type_without_circularity (rtype
))
8781 complete_type (rtype
);
8783 if (!COMPLETE_TYPE_P (rtype
))
8785 cxx_incomplete_type_error (r
, rtype
);
8786 r
= error_mark_node
;
8790 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8791 such a thing will already have been added to the field
8792 list by tsubst_enum in finish_member_declaration in the
8793 CLASSTYPE_NESTED_UTDS case above. */
8794 if (!(TREE_CODE (r
) == TYPE_DECL
8795 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
8796 && DECL_ARTIFICIAL (r
)))
8798 set_current_access_from_decl (r
);
8799 finish_member_declaration (r
);
8806 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
)
8807 || DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
8809 /* Build new CLASSTYPE_FRIEND_CLASSES. */
8811 tree friend_type
= t
;
8812 bool adjust_processing_template_decl
= false;
8814 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8816 /* template <class T> friend class C; */
8817 friend_type
= tsubst_friend_class (friend_type
, args
);
8818 adjust_processing_template_decl
= true;
8820 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
8822 /* template <class T> friend class C::D; */
8823 friend_type
= tsubst (friend_type
, args
,
8824 tf_warning_or_error
, NULL_TREE
);
8825 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
8826 friend_type
= TREE_TYPE (friend_type
);
8827 adjust_processing_template_decl
= true;
8829 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
8830 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
8832 /* This could be either
8836 when dependent_type_p is false or
8838 template <class U> friend class T::C;
8841 friend_type
= tsubst (friend_type
, args
,
8842 tf_warning_or_error
, NULL_TREE
);
8843 /* Bump processing_template_decl for correct
8844 dependent_type_p calculation. */
8845 ++processing_template_decl
;
8846 if (dependent_type_p (friend_type
))
8847 adjust_processing_template_decl
= true;
8848 --processing_template_decl
;
8850 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
8851 && hidden_name_p (TYPE_NAME (friend_type
)))
8855 where C hasn't been declared yet. Let's lookup name
8856 from namespace scope directly, bypassing any name that
8857 come from dependent base class. */
8858 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
8860 /* The call to xref_tag_from_type does injection for friend
8862 push_nested_namespace (ns
);
8864 xref_tag_from_type (friend_type
, NULL_TREE
,
8865 /*tag_scope=*/ts_current
);
8866 pop_nested_namespace (ns
);
8868 else if (uses_template_parms (friend_type
))
8869 /* friend class C<T>; */
8870 friend_type
= tsubst (friend_type
, args
,
8871 tf_warning_or_error
, NULL_TREE
);
8876 where C is already declared or
8878 friend class C<int>;
8880 We don't have to do anything in these cases. */
8882 if (adjust_processing_template_decl
)
8883 /* Trick make_friend_class into realizing that the friend
8884 we're adding is a template, not an ordinary class. It's
8885 important that we use make_friend_class since it will
8886 perform some error-checking and output cross-reference
8888 ++processing_template_decl
;
8890 if (friend_type
!= error_mark_node
)
8891 make_friend_class (type
, friend_type
, /*complain=*/false);
8893 if (adjust_processing_template_decl
)
8894 --processing_template_decl
;
8898 /* Build new DECL_FRIENDLIST. */
8901 /* The file and line for this declaration, to
8902 assist in error message reporting. Since we
8903 called push_tinst_level above, we don't need to
8905 input_location
= DECL_SOURCE_LOCATION (t
);
8907 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8909 ++processing_template_decl
;
8910 push_deferring_access_checks (dk_no_check
);
8913 r
= tsubst_friend_function (t
, args
);
8914 add_friend (type
, r
, /*complain=*/false);
8915 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8917 pop_deferring_access_checks ();
8918 --processing_template_decl
;
8924 if (CLASSTYPE_LAMBDA_EXPR (type
))
8926 tree decl
= lambda_function (type
);
8929 tree lam
= CLASSTYPE_LAMBDA_EXPR (type
);
8930 LAMBDA_EXPR_THIS_CAPTURE (lam
)
8931 = lookup_field_1 (type
, get_identifier ("__this"), false);
8933 instantiate_decl (decl
, false, false);
8934 maybe_add_lambda_conv_op (type
);
8936 LAMBDA_EXPR_THIS_CAPTURE (lam
) = NULL_TREE
;
8939 gcc_assert (errorcount
);
8942 /* Set the file and line number information to whatever is given for
8943 the class itself. This puts error messages involving generated
8944 implicit functions at a predictable point, and the same point
8945 that would be used for non-template classes. */
8946 input_location
= DECL_SOURCE_LOCATION (typedecl
);
8948 unreverse_member_declarations (type
);
8949 finish_struct_1 (type
);
8950 TYPE_BEING_DEFINED (type
) = 0;
8952 /* We don't instantiate default arguments for member functions. 14.7.1:
8954 The implicit instantiation of a class template specialization causes
8955 the implicit instantiation of the declarations, but not of the
8956 definitions or default arguments, of the class member functions,
8957 member classes, static data members and member templates.... */
8959 /* Some typedefs referenced from within the template code need to be access
8960 checked at template instantiation time, i.e now. These types were
8961 added to the template at parsing time. Let's get those and perform
8962 the access checks then. */
8963 perform_typedefs_access_check (pattern
, args
);
8964 perform_deferred_access_checks (tf_warning_or_error
);
8965 pop_nested_class ();
8966 maximum_field_alignment
= saved_maximum_field_alignment
;
8968 pop_from_top_level ();
8969 pop_deferring_access_checks ();
8972 /* The vtable for a template class can be emitted in any translation
8973 unit in which the class is instantiated. When there is no key
8974 method, however, finish_struct_1 will already have added TYPE to
8975 the keyed_classes list. */
8976 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
8977 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
8982 /* Wrapper for instantiate_class_template_1. */
8985 instantiate_class_template (tree type
)
8988 timevar_push (TV_TEMPLATE_INST
);
8989 ret
= instantiate_class_template_1 (type
);
8990 timevar_pop (TV_TEMPLATE_INST
);
8995 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9001 else if (TYPE_P (t
))
9002 r
= tsubst (t
, args
, complain
, in_decl
);
9005 if (!(complain
& tf_warning
))
9006 ++c_inhibit_evaluation_warnings
;
9007 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9008 /*integral_constant_expression_p=*/true);
9009 if (!(complain
& tf_warning
))
9010 --c_inhibit_evaluation_warnings
;
9011 /* Preserve the raw-reference nature of T. */
9012 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9013 && REFERENCE_REF_P (r
))
9014 r
= TREE_OPERAND (r
, 0);
9019 /* Given a function parameter pack TMPL_PARM and some function parameters
9020 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9021 and set *SPEC_P to point at the next point in the list. */
9024 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9026 /* Collect all of the extra "packed" parameters into an
9030 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9031 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9032 tree spec_parm
= *spec_p
;
9035 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9037 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9040 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9041 parmvec
= make_tree_vec (len
);
9042 parmtypevec
= make_tree_vec (len
);
9043 spec_parm
= *spec_p
;
9044 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9046 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9047 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9050 /* Build the argument packs. */
9051 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9052 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9053 TREE_TYPE (argpack
) = argtypepack
;
9054 *spec_p
= spec_parm
;
9059 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9060 NONTYPE_ARGUMENT_PACK. */
9063 make_fnparm_pack (tree spec_parm
)
9065 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9068 /* Substitute ARGS into T, which is an pack expansion
9069 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9070 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9071 (if only a partial substitution could be performed) or
9072 ERROR_MARK_NODE if there was an error. */
9074 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9078 tree pack
, packs
= NULL_TREE
;
9079 bool unsubstituted_packs
= false;
9080 bool real_packs
= false;
9081 int missing_level
= 0;
9084 struct pointer_map_t
*saved_local_specializations
= NULL
;
9085 bool need_local_specializations
= false;
9088 gcc_assert (PACK_EXPANSION_P (t
));
9089 pattern
= PACK_EXPANSION_PATTERN (t
);
9091 /* Add in any args remembered from an earlier partial instantiation. */
9092 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9094 levels
= TMPL_ARGS_DEPTH (args
);
9096 /* Determine the argument packs that will instantiate the parameter
9097 packs used in the expansion expression. While we're at it,
9098 compute the number of arguments to be expanded and make sure it
9100 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9101 pack
= TREE_CHAIN (pack
))
9103 tree parm_pack
= TREE_VALUE (pack
);
9104 tree arg_pack
= NULL_TREE
;
9105 tree orig_arg
= NULL_TREE
;
9108 if (TREE_CODE (parm_pack
) == BASES
)
9110 if (BASES_DIRECT (parm_pack
))
9111 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9112 args
, complain
, in_decl
, false));
9114 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9115 args
, complain
, in_decl
, false));
9117 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9119 if (PACK_EXPANSION_LOCAL_P (t
))
9120 arg_pack
= retrieve_local_specialization (parm_pack
);
9123 /* We can't rely on local_specializations for a parameter
9124 name used later in a function declaration (such as in a
9125 late-specified return type). Even if it exists, it might
9126 have the wrong value for a recursive call. Just make a
9127 dummy decl, since it's only used for its type. */
9128 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9129 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9130 /* Partial instantiation of the parm_pack, we can't build
9131 up an argument pack yet. */
9132 arg_pack
= NULL_TREE
;
9134 arg_pack
= make_fnparm_pack (arg_pack
);
9135 need_local_specializations
= true;
9141 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9143 if (level
<= levels
)
9144 arg_pack
= TMPL_ARG (args
, level
, idx
);
9147 orig_arg
= arg_pack
;
9148 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9149 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9151 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9152 /* This can only happen if we forget to expand an argument
9153 pack somewhere else. Just return an error, silently. */
9155 result
= make_tree_vec (1);
9156 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9160 if (arg_from_parm_pack_p (arg_pack
, parm_pack
))
9161 /* The argument pack that the parameter maps to is just an
9162 expansion of the parameter itself, such as one would find
9163 in the implicit typedef of a class inside the class itself.
9164 Consider this parameter "unsubstituted", so that we will
9165 maintain the outer pack expansion. */
9166 arg_pack
= NULL_TREE
;
9171 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9173 /* Don't bother trying to do a partial substitution with
9174 incomplete packs; we'll try again after deduction. */
9175 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9180 else if (len
!= my_len
)
9182 if (!(complain
& tf_error
))
9183 /* Fail quietly. */;
9184 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9185 error ("mismatched argument pack lengths while expanding "
9189 error ("mismatched argument pack lengths while expanding "
9192 return error_mark_node
;
9195 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
9196 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
),
9198 /* This isn't a real argument pack yet. */;
9202 /* Keep track of the parameter packs and their corresponding
9204 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9205 TREE_TYPE (packs
) = orig_arg
;
9209 /* We can't substitute for this parameter pack. We use a flag as
9210 well as the missing_level counter because function parameter
9211 packs don't have a level. */
9212 unsubstituted_packs
= true;
9213 if (!missing_level
|| missing_level
> level
)
9214 missing_level
= level
;
9218 /* We cannot expand this expansion expression, because we don't have
9219 all of the argument packs we need. */
9220 if (unsubstituted_packs
)
9224 /* We got some full packs, but we can't substitute them in until we
9225 have values for all the packs. So remember these until then. */
9228 t
= make_pack_expansion (pattern
);
9230 /* The call to add_to_template_args above assumes no overlap
9231 between saved args and new args, so prune away any fake
9232 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9233 if (missing_level
&& levels
>= missing_level
)
9235 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
)
9236 && missing_level
> 1);
9237 TREE_VEC_LENGTH (args
) = missing_level
- 1;
9238 save_args
= copy_node (args
);
9239 TREE_VEC_LENGTH (args
) = levels
;
9244 PACK_EXPANSION_EXTRA_ARGS (t
) = save_args
;
9248 /* There were no real arguments, we're just replacing a parameter
9249 pack with another version of itself. Substitute into the
9250 pattern and return a PACK_EXPANSION_*. The caller will need to
9252 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9253 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9254 /*integral_constant_expression_p=*/false);
9256 t
= tsubst (pattern
, args
, complain
, in_decl
);
9257 t
= make_pack_expansion (t
);
9262 /* We could not find any argument packs that work. */
9264 return error_mark_node
;
9266 if (need_local_specializations
)
9268 /* We're in a late-specified return type, so create our own local
9269 specializations map; the current map is either NULL or (in the
9270 case of recursive unification) might have bindings that we don't
9271 want to use or alter. */
9272 saved_local_specializations
= local_specializations
;
9273 local_specializations
= pointer_map_create ();
9276 /* For each argument in each argument pack, substitute into the
9278 result
= make_tree_vec (len
);
9279 for (i
= 0; i
< len
; ++i
)
9281 /* For parameter pack, change the substitution of the parameter
9282 pack to the ith argument in its argument pack, then expand
9284 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9286 tree parm
= TREE_PURPOSE (pack
);
9289 /* Select the Ith argument from the pack. */
9290 if (TREE_CODE (parm
) == PARM_DECL
)
9294 arg
= make_node (ARGUMENT_PACK_SELECT
);
9295 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9297 register_local_specialization (arg
, parm
);
9300 arg
= retrieve_local_specialization (parm
);
9305 template_parm_level_and_index (parm
, &level
, &idx
);
9309 arg
= make_node (ARGUMENT_PACK_SELECT
);
9310 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9311 /* Update the corresponding argument. */
9312 TMPL_ARG (args
, level
, idx
) = arg
;
9315 /* Re-use the ARGUMENT_PACK_SELECT. */
9316 arg
= TMPL_ARG (args
, level
, idx
);
9318 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
9321 /* Substitute into the PATTERN with the altered arguments. */
9322 if (!TYPE_P (pattern
))
9323 TREE_VEC_ELT (result
, i
) =
9324 tsubst_expr (pattern
, args
, complain
, in_decl
,
9325 /*integral_constant_expression_p=*/false);
9327 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
9329 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
9331 result
= error_mark_node
;
9336 /* Update ARGS to restore the substitution from parameter packs to
9337 their argument packs. */
9338 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9340 tree parm
= TREE_PURPOSE (pack
);
9342 if (TREE_CODE (parm
) == PARM_DECL
)
9343 register_local_specialization (TREE_TYPE (pack
), parm
);
9347 template_parm_level_and_index (parm
, &level
, &idx
);
9349 /* Update the corresponding argument. */
9350 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9351 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9354 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9358 if (need_local_specializations
)
9360 pointer_map_destroy (local_specializations
);
9361 local_specializations
= saved_local_specializations
;
9367 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9368 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9369 parameter packs; all parms generated from a function parameter pack will
9370 have the same DECL_PARM_INDEX. */
9373 get_pattern_parm (tree parm
, tree tmpl
)
9375 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9378 if (DECL_ARTIFICIAL (parm
))
9380 for (patparm
= DECL_ARGUMENTS (pattern
);
9381 patparm
; patparm
= DECL_CHAIN (patparm
))
9382 if (DECL_ARTIFICIAL (patparm
)
9383 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9388 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9389 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9390 gcc_assert (DECL_PARM_INDEX (patparm
)
9391 == DECL_PARM_INDEX (parm
));
9397 /* Substitute ARGS into the vector or list of template arguments T. */
9400 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9403 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9406 if (t
== error_mark_node
)
9407 return error_mark_node
;
9409 len
= TREE_VEC_LENGTH (t
);
9410 elts
= XALLOCAVEC (tree
, len
);
9412 for (i
= 0; i
< len
; i
++)
9414 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9417 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9418 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9419 else if (PACK_EXPANSION_P (orig_arg
))
9421 /* Substitute into an expansion expression. */
9422 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9424 if (TREE_CODE (new_arg
) == TREE_VEC
)
9425 /* Add to the expanded length adjustment the number of
9426 expanded arguments. We subtract one from this
9427 measurement, because the argument pack expression
9428 itself is already counted as 1 in
9429 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9430 the argument pack is empty. */
9431 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9433 else if (ARGUMENT_PACK_P (orig_arg
))
9435 /* Substitute into each of the arguments. */
9436 new_arg
= TYPE_P (orig_arg
)
9437 ? cxx_make_type (TREE_CODE (orig_arg
))
9438 : make_node (TREE_CODE (orig_arg
));
9440 SET_ARGUMENT_PACK_ARGS (
9442 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9443 args
, complain
, in_decl
));
9445 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9446 new_arg
= error_mark_node
;
9448 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9449 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9451 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9453 if (TREE_TYPE (new_arg
) == error_mark_node
)
9454 new_arg
= error_mark_node
;
9458 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9460 if (new_arg
== error_mark_node
)
9461 return error_mark_node
;
9464 if (new_arg
!= orig_arg
)
9471 /* Make space for the expanded arguments coming from template
9473 t
= make_tree_vec (len
+ expanded_len_adjust
);
9474 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9475 arguments for a member template.
9476 In that case each TREE_VEC in ORIG_T represents a level of template
9477 arguments, and ORIG_T won't carry any non defaulted argument count.
9478 It will rather be the nested TREE_VECs that will carry one.
9479 In other words, ORIG_T carries a non defaulted argument count only
9480 if it doesn't contain any nested TREE_VEC. */
9481 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9483 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9484 count
+= expanded_len_adjust
;
9485 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9487 for (i
= 0, out
= 0; i
< len
; i
++)
9489 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9490 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9491 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9495 /* Now expand the template argument pack "in place". */
9496 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9497 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9501 TREE_VEC_ELT (t
, out
) = elts
[i
];
9509 /* Return the result of substituting ARGS into the template parameters
9510 given by PARMS. If there are m levels of ARGS and m + n levels of
9511 PARMS, then the result will contain n levels of PARMS. For
9512 example, if PARMS is `template <class T> template <class U>
9513 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9514 result will be `template <int*, double, class V>'. */
9517 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9522 /* When substituting into a template, we must set
9523 PROCESSING_TEMPLATE_DECL as the template parameters may be
9524 dependent if they are based on one-another, and the dependency
9525 predicates are short-circuit outside of templates. */
9526 ++processing_template_decl
;
9528 for (new_parms
= &r
;
9529 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9530 new_parms
= &(TREE_CHAIN (*new_parms
)),
9531 parms
= TREE_CHAIN (parms
))
9534 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9537 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9541 if (parms
== error_mark_node
)
9544 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9546 if (tuple
== error_mark_node
)
9549 TREE_VEC_ELT (new_vec
, i
) =
9550 tsubst_template_parm (tuple
, args
, complain
);
9554 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9555 - TMPL_ARGS_DEPTH (args
)),
9556 new_vec
, NULL_TREE
);
9559 --processing_template_decl
;
9564 /* Return the result of substituting ARGS into one template parameter
9565 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9566 parameter and which TREE_PURPOSE is the default argument of the
9567 template parameter. */
9570 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9572 tree default_value
, parm_decl
;
9574 if (args
== NULL_TREE
9576 || t
== error_mark_node
)
9579 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9581 default_value
= TREE_PURPOSE (t
);
9582 parm_decl
= TREE_VALUE (t
);
9584 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9585 if (TREE_CODE (parm_decl
) == PARM_DECL
9586 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9587 parm_decl
= error_mark_node
;
9588 default_value
= tsubst_template_arg (default_value
, args
,
9589 complain
, NULL_TREE
);
9591 return build_tree_list (default_value
, parm_decl
);
9594 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9595 type T. If T is not an aggregate or enumeration type, it is
9596 handled as if by tsubst. IN_DECL is as for tsubst. If
9597 ENTERING_SCOPE is nonzero, T is the context for a template which
9598 we are presently tsubst'ing. Return the substituted value. */
9601 tsubst_aggr_type (tree t
,
9603 tsubst_flags_t complain
,
9610 switch (TREE_CODE (t
))
9613 if (TYPE_PTRMEMFUNC_P (t
))
9614 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9616 /* Else fall through. */
9619 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9624 int saved_unevaluated_operand
;
9625 int saved_inhibit_evaluation_warnings
;
9627 /* In "sizeof(X<I>)" we need to evaluate "I". */
9628 saved_unevaluated_operand
= cp_unevaluated_operand
;
9629 cp_unevaluated_operand
= 0;
9630 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9631 c_inhibit_evaluation_warnings
= 0;
9633 /* First, determine the context for the type we are looking
9635 context
= TYPE_CONTEXT (t
);
9636 if (context
&& TYPE_P (context
))
9638 context
= tsubst_aggr_type (context
, args
, complain
,
9639 in_decl
, /*entering_scope=*/1);
9640 /* If context is a nested class inside a class template,
9641 it may still need to be instantiated (c++/33959). */
9642 context
= complete_type (context
);
9645 /* Then, figure out what arguments are appropriate for the
9646 type we are trying to find. For example, given:
9648 template <class T> struct S;
9649 template <class T, class U> void f(T, U) { S<U> su; }
9651 and supposing that we are instantiating f<int, double>,
9652 then our ARGS will be {int, double}, but, when looking up
9653 S we only want {double}. */
9654 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9656 if (argvec
== error_mark_node
)
9657 r
= error_mark_node
;
9660 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9661 entering_scope
, complain
);
9662 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9665 cp_unevaluated_operand
= saved_unevaluated_operand
;
9666 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9671 /* This is not a template type, so there's nothing to do. */
9675 return tsubst (t
, args
, complain
, in_decl
);
9679 /* Substitute into the default argument ARG (a default argument for
9680 FN), which has the indicated TYPE. */
9683 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9685 tree saved_class_ptr
= NULL_TREE
;
9686 tree saved_class_ref
= NULL_TREE
;
9687 int errs
= errorcount
+ sorrycount
;
9689 /* This can happen in invalid code. */
9690 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9693 /* This default argument came from a template. Instantiate the
9694 default argument here, not in tsubst. In the case of
9703 we must be careful to do name lookup in the scope of S<T>,
9704 rather than in the current class. */
9705 push_access_scope (fn
);
9706 /* The "this" pointer is not valid in a default argument. */
9709 saved_class_ptr
= current_class_ptr
;
9710 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9711 saved_class_ref
= current_class_ref
;
9712 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9715 push_deferring_access_checks(dk_no_deferred
);
9716 /* The default argument expression may cause implicitly defined
9717 member functions to be synthesized, which will result in garbage
9718 collection. We must treat this situation as if we were within
9719 the body of function so as to avoid collecting live data on the
9722 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9723 tf_warning_or_error
, NULL_TREE
,
9724 /*integral_constant_expression_p=*/false);
9726 pop_deferring_access_checks();
9728 /* Restore the "this" pointer. */
9731 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9732 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9735 if (errorcount
+sorrycount
> errs
)
9736 inform (input_location
,
9737 " when instantiating default argument for call to %D", fn
);
9739 /* Make sure the default argument is reasonable. */
9740 arg
= check_default_argument (type
, arg
);
9742 pop_access_scope (fn
);
9747 /* Substitute into all the default arguments for FN. */
9750 tsubst_default_arguments (tree fn
)
9755 tmpl_args
= DECL_TI_ARGS (fn
);
9757 /* If this function is not yet instantiated, we certainly don't need
9758 its default arguments. */
9759 if (uses_template_parms (tmpl_args
))
9761 /* Don't do this again for clones. */
9762 if (DECL_CLONED_FUNCTION_P (fn
))
9765 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9767 arg
= TREE_CHAIN (arg
))
9768 if (TREE_PURPOSE (arg
))
9769 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9771 TREE_PURPOSE (arg
));
9774 /* Substitute the ARGS into the T, which is a _DECL. Return the
9775 result of the substitution. Issue error and warning messages under
9776 control of COMPLAIN. */
9779 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9781 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9782 location_t saved_loc
;
9787 /* Set the filename and linenumber to improve error-reporting. */
9788 saved_loc
= input_location
;
9789 input_location
= DECL_SOURCE_LOCATION (t
);
9791 switch (TREE_CODE (t
))
9795 /* We can get here when processing a member function template,
9796 member class template, or template template parameter. */
9797 tree decl
= DECL_TEMPLATE_RESULT (t
);
9802 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
9804 /* Template template parameter is treated here. */
9805 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9806 if (new_type
== error_mark_node
)
9807 RETURN (error_mark_node
);
9810 DECL_CHAIN (r
) = NULL_TREE
;
9811 TREE_TYPE (r
) = new_type
;
9812 DECL_TEMPLATE_RESULT (r
)
9813 = build_decl (DECL_SOURCE_LOCATION (decl
),
9814 TYPE_DECL
, DECL_NAME (decl
), new_type
);
9815 DECL_TEMPLATE_PARMS (r
)
9816 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9818 TYPE_NAME (new_type
) = r
;
9822 /* We might already have an instance of this template.
9823 The ARGS are for the surrounding class type, so the
9824 full args contain the tsubst'd args for the context,
9825 plus the innermost args from the template decl. */
9826 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
9827 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
9828 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
9829 /* Because this is a template, the arguments will still be
9830 dependent, even after substitution. If
9831 PROCESSING_TEMPLATE_DECL is not set, the dependency
9832 predicates will short-circuit. */
9833 ++processing_template_decl
;
9834 full_args
= tsubst_template_args (tmpl_args
, args
,
9836 --processing_template_decl
;
9837 if (full_args
== error_mark_node
)
9838 RETURN (error_mark_node
);
9840 /* If this is a default template template argument,
9841 tsubst might not have changed anything. */
9842 if (full_args
== tmpl_args
)
9845 hash
= hash_tmpl_and_args (t
, full_args
);
9846 spec
= retrieve_specialization (t
, full_args
, hash
);
9847 if (spec
!= NULL_TREE
)
9853 /* Make a new template decl. It will be similar to the
9854 original, but will record the current template arguments.
9855 We also create a new function declaration, which is just
9856 like the old one, but points to this new template, rather
9857 than the old one. */
9859 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
9860 DECL_CHAIN (r
) = NULL_TREE
;
9862 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
9864 if (TREE_CODE (decl
) == TYPE_DECL
9865 && !TYPE_DECL_ALIAS_P (decl
))
9868 ++processing_template_decl
;
9869 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
9870 --processing_template_decl
;
9871 if (new_type
== error_mark_node
)
9872 RETURN (error_mark_node
);
9874 TREE_TYPE (r
) = new_type
;
9875 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
9876 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
9877 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
9878 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
9883 ++processing_template_decl
;
9884 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
9885 --processing_template_decl
;
9886 if (new_decl
== error_mark_node
)
9887 RETURN (error_mark_node
);
9889 DECL_TEMPLATE_RESULT (r
) = new_decl
;
9890 DECL_TI_TEMPLATE (new_decl
) = r
;
9891 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
9892 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
9893 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
9896 SET_DECL_IMPLICIT_INSTANTIATION (r
);
9897 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
9898 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
9900 /* The template parameters for this new template are all the
9901 template parameters for the old template, except the
9902 outermost level of parameters. */
9903 DECL_TEMPLATE_PARMS (r
)
9904 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
9907 if (PRIMARY_TEMPLATE_P (t
))
9908 DECL_PRIMARY_TEMPLATE (r
) = r
;
9910 if (TREE_CODE (decl
) != TYPE_DECL
)
9911 /* Record this non-type partial instantiation. */
9912 register_specialization (r
, t
,
9913 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
9921 tree argvec
= NULL_TREE
;
9929 /* Nobody should be tsubst'ing into non-template functions. */
9930 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
9932 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
9937 /* If T is not dependent, just return it. We have to
9938 increment PROCESSING_TEMPLATE_DECL because
9939 value_dependent_expression_p assumes that nothing is
9940 dependent when PROCESSING_TEMPLATE_DECL is zero. */
9941 ++processing_template_decl
;
9942 dependent_p
= value_dependent_expression_p (t
);
9943 --processing_template_decl
;
9947 /* Calculate the most general template of which R is a
9948 specialization, and the complete set of arguments used to
9950 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
9951 argvec
= tsubst_template_args (DECL_TI_ARGS
9952 (DECL_TEMPLATE_RESULT
9953 (DECL_TI_TEMPLATE (t
))),
9954 args
, complain
, in_decl
);
9955 if (argvec
== error_mark_node
)
9956 RETURN (error_mark_node
);
9958 /* Check to see if we already have this specialization. */
9959 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
9960 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
9968 /* We can see more levels of arguments than parameters if
9969 there was a specialization of a member template, like
9972 template <class T> struct S { template <class U> void f(); }
9973 template <> template <class U> void S<int>::f(U);
9975 Here, we'll be substituting into the specialization,
9976 because that's where we can find the code we actually
9977 want to generate, but we'll have enough arguments for
9978 the most general template.
9980 We also deal with the peculiar case:
9982 template <class T> struct S {
9983 template <class U> friend void f();
9985 template <class U> void f() {}
9987 template void f<double>();
9989 Here, the ARGS for the instantiation of will be {int,
9990 double}. But, we only need as many ARGS as there are
9991 levels of template parameters in CODE_PATTERN. We are
9992 careful not to get fooled into reducing the ARGS in
9995 template <class T> struct S { template <class U> void f(U); }
9996 template <class T> template <> void S<T>::f(int) {}
9998 which we can spot because the pattern will be a
9999 specialization in this case. */
10000 args_depth
= TMPL_ARGS_DEPTH (args
);
10002 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10003 if (args_depth
> parms_depth
10004 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10005 args
= get_innermost_template_args (args
, parms_depth
);
10009 /* This special case arises when we have something like this:
10011 template <class T> struct S {
10012 friend void f<int>(int, double);
10015 Here, the DECL_TI_TEMPLATE for the friend declaration
10016 will be an IDENTIFIER_NODE. We are being called from
10017 tsubst_friend_function, and we want only to create a
10018 new decl (R) with appropriate types so that we can call
10019 determine_specialization. */
10020 gen_tmpl
= NULL_TREE
;
10023 if (DECL_CLASS_SCOPE_P (t
))
10025 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10029 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10030 complain
, t
, /*entering_scope=*/1);
10035 ctx
= DECL_CONTEXT (t
);
10037 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10038 if (type
== error_mark_node
)
10039 RETURN (error_mark_node
);
10041 /* If we hit excessive deduction depth, the type is bogus even if
10042 it isn't error_mark_node, so don't build a decl. */
10043 if (excessive_deduction_depth
)
10044 RETURN (error_mark_node
);
10046 /* We do NOT check for matching decls pushed separately at this
10047 point, as they may not represent instantiations of this
10048 template, and in any case are considered separate under the
10051 DECL_USE_TEMPLATE (r
) = 0;
10052 TREE_TYPE (r
) = type
;
10053 /* Clear out the mangled name and RTL for the instantiation. */
10054 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10055 SET_DECL_RTL (r
, NULL
);
10056 /* Leave DECL_INITIAL set on deleted instantiations. */
10057 if (!DECL_DELETED_FN (r
))
10058 DECL_INITIAL (r
) = NULL_TREE
;
10059 DECL_CONTEXT (r
) = ctx
;
10061 if (member
&& DECL_CONV_FN_P (r
))
10062 /* Type-conversion operator. Reconstruct the name, in
10063 case it's the name of one of the template's parameters. */
10064 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10066 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10068 DECL_RESULT (r
) = NULL_TREE
;
10070 TREE_STATIC (r
) = 0;
10071 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10072 DECL_EXTERNAL (r
) = 1;
10073 /* If this is an instantiation of a function with internal
10074 linkage, we already know what object file linkage will be
10075 assigned to the instantiation. */
10076 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10077 DECL_DEFER_OUTPUT (r
) = 0;
10078 DECL_CHAIN (r
) = NULL_TREE
;
10079 DECL_PENDING_INLINE_INFO (r
) = 0;
10080 DECL_PENDING_INLINE_P (r
) = 0;
10081 DECL_SAVED_TREE (r
) = NULL_TREE
;
10082 DECL_STRUCT_FUNCTION (r
) = NULL
;
10084 /* We'll re-clone as appropriate in instantiate_template. */
10085 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10087 /* If we aren't complaining now, return on error before we register
10088 the specialization so that we'll complain eventually. */
10089 if ((complain
& tf_error
) == 0
10090 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10091 && !grok_op_properties (r
, /*complain=*/false))
10092 RETURN (error_mark_node
);
10094 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10095 this in the special friend case mentioned above where
10096 GEN_TMPL is NULL. */
10099 DECL_TEMPLATE_INFO (r
)
10100 = build_template_info (gen_tmpl
, argvec
);
10101 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10102 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10104 /* We're not supposed to instantiate default arguments
10105 until they are called, for a template. But, for a
10108 template <class T> void f ()
10109 { extern void g(int i = T()); }
10111 we should do the substitution when the template is
10112 instantiated. We handle the member function case in
10113 instantiate_class_template since the default arguments
10114 might refer to other members of the class. */
10116 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10117 && !uses_template_parms (argvec
))
10118 tsubst_default_arguments (r
);
10121 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10123 /* Copy the list of befriending classes. */
10124 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10126 friends
= &TREE_CHAIN (*friends
))
10128 *friends
= copy_node (*friends
);
10129 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10134 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10136 maybe_retrofit_in_chrg (r
);
10137 if (DECL_CONSTRUCTOR_P (r
))
10138 grok_ctor_properties (ctx
, r
);
10139 /* If this is an instantiation of a member template, clone it.
10140 If it isn't, that'll be handled by
10141 clone_constructors_and_destructors. */
10142 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10143 clone_function_decl (r
, /*update_method_vec_p=*/0);
10145 else if ((complain
& tf_error
) != 0
10146 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10147 && !grok_op_properties (r
, /*complain=*/true))
10148 RETURN (error_mark_node
);
10150 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10151 SET_DECL_FRIEND_CONTEXT (r
,
10152 tsubst (DECL_FRIEND_CONTEXT (t
),
10153 args
, complain
, in_decl
));
10155 /* Possibly limit visibility based on template args. */
10156 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10157 if (DECL_VISIBILITY_SPECIFIED (t
))
10159 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10160 DECL_ATTRIBUTES (r
)
10161 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10163 determine_visibility (r
);
10164 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10165 && !processing_template_decl
)
10166 defaulted_late_check (r
);
10168 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10169 args
, complain
, in_decl
);
10175 tree type
= NULL_TREE
;
10177 tree expanded_types
= NULL_TREE
;
10178 tree prev_r
= NULL_TREE
;
10179 tree first_r
= NULL_TREE
;
10181 if (FUNCTION_PARAMETER_PACK_P (t
))
10183 /* If there is a local specialization that isn't a
10184 parameter pack, it means that we're doing a "simple"
10185 substitution from inside tsubst_pack_expansion. Just
10186 return the local specialization (which will be a single
10188 tree spec
= retrieve_local_specialization (t
);
10190 && TREE_CODE (spec
) == PARM_DECL
10191 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10194 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10195 the parameters in this function parameter pack. */
10196 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10197 complain
, in_decl
);
10198 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10200 len
= TREE_VEC_LENGTH (expanded_types
);
10202 /* Zero-length parameter packs are boring. Just substitute
10205 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10210 /* All we did was update the type. Make a note of that. */
10211 type
= expanded_types
;
10212 expanded_types
= NULL_TREE
;
10216 /* Loop through all of the parameter's we'll build. When T is
10217 a function parameter pack, LEN is the number of expanded
10218 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10220 for (i
= 0; i
< len
; ++i
)
10224 if (DECL_TEMPLATE_PARM_P (t
))
10225 SET_DECL_TEMPLATE_PARM_P (r
);
10227 if (expanded_types
)
10228 /* We're on the Ith parameter of the function parameter
10231 /* An argument of a function parameter pack is not a parameter
10233 FUNCTION_PARAMETER_PACK_P (r
) = false;
10235 /* Get the Ith type. */
10236 type
= TREE_VEC_ELT (expanded_types
, i
);
10239 /* Rename the parameter to include the index. */
10241 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10244 /* We're dealing with a normal parameter. */
10245 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10247 type
= type_decays_to (type
);
10248 TREE_TYPE (r
) = type
;
10249 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10251 if (DECL_INITIAL (r
))
10253 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10254 DECL_INITIAL (r
) = TREE_TYPE (r
);
10256 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10257 complain
, in_decl
);
10260 DECL_CONTEXT (r
) = NULL_TREE
;
10262 if (!DECL_TEMPLATE_PARM_P (r
))
10263 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10265 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10266 args
, complain
, in_decl
);
10268 /* Keep track of the first new parameter we
10269 generate. That's what will be returned to the
10274 /* Build a proper chain of parameters when substituting
10275 into a function parameter pack. */
10277 DECL_CHAIN (prev_r
) = r
;
10280 /* If cp_unevaluated_operand is set, we're just looking for a
10281 single dummy parameter, so don't keep going. */
10282 if (DECL_CHAIN (t
) && !cp_unevaluated_operand
)
10283 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10284 complain
, DECL_CHAIN (t
));
10286 /* FIRST_R contains the start of the chain we've built. */
10296 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10297 if (type
== error_mark_node
)
10298 RETURN (error_mark_node
);
10299 TREE_TYPE (r
) = type
;
10300 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10302 if (DECL_C_BIT_FIELD (r
))
10303 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10304 non-bit-fields DECL_INITIAL is a non-static data member
10305 initializer, which gets deferred instantiation. */
10307 = tsubst_expr (DECL_INITIAL (t
), args
,
10309 /*integral_constant_expression_p=*/true);
10310 else if (DECL_INITIAL (t
))
10312 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10313 NSDMI in perform_member_init. Still set DECL_INITIAL
10314 so that we know there is one. */
10315 DECL_INITIAL (r
) = void_zero_node
;
10316 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10317 retrofit_lang_decl (r
);
10318 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10320 /* We don't have to set DECL_CONTEXT here; it is set by
10321 finish_member_declaration. */
10322 DECL_CHAIN (r
) = NULL_TREE
;
10323 if (VOID_TYPE_P (type
))
10324 error ("instantiation of %q+D as type %qT", r
, type
);
10326 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10327 args
, complain
, in_decl
);
10332 /* We reach here only for member using decls. We also need to check
10333 uses_template_parms because DECL_DEPENDENT_P is not set for a
10334 using-declaration that designates a member of the current
10335 instantiation (c++/53549). */
10336 if (DECL_DEPENDENT_P (t
)
10337 || uses_template_parms (USING_DECL_SCOPE (t
)))
10339 r
= do_class_using_decl
10340 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
10341 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
10343 r
= error_mark_node
;
10346 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10347 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10353 DECL_CHAIN (r
) = NULL_TREE
;
10360 tree argvec
= NULL_TREE
;
10361 tree gen_tmpl
= NULL_TREE
;
10363 tree tmpl
= NULL_TREE
;
10365 tree type
= NULL_TREE
;
10368 if (TREE_CODE (t
) == TYPE_DECL
10369 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10371 /* If this is the canonical decl, we don't have to
10372 mess with instantiations, and often we can't (for
10373 typename, template type parms and such). Note that
10374 TYPE_NAME is not correct for the above test if
10375 we've copied the type for a typedef. */
10376 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10377 if (type
== error_mark_node
)
10378 RETURN (error_mark_node
);
10379 r
= TYPE_NAME (type
);
10383 /* Check to see if we already have the specialization we
10386 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10388 /* T is a static data member or namespace-scope entity.
10389 We have to substitute into namespace-scope variables
10390 (even though such entities are never templates) because
10393 template <class T> void f() { extern T t; }
10395 where the entity referenced is not known until
10396 instantiation time. */
10398 ctx
= DECL_CONTEXT (t
);
10399 if (DECL_CLASS_SCOPE_P (t
))
10401 ctx
= tsubst_aggr_type (ctx
, args
,
10403 in_decl
, /*entering_scope=*/1);
10404 /* If CTX is unchanged, then T is in fact the
10405 specialization we want. That situation occurs when
10406 referencing a static data member within in its own
10407 class. We can use pointer equality, rather than
10408 same_type_p, because DECL_CONTEXT is always
10410 if (ctx
== DECL_CONTEXT (t
)
10411 && (TREE_CODE (t
) != TYPE_DECL
10412 /* ... unless T is a member template; in which
10413 case our caller can be willing to create a
10414 specialization of that template represented
10416 || !(DECL_TI_TEMPLATE (t
)
10417 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10423 tmpl
= DECL_TI_TEMPLATE (t
);
10424 gen_tmpl
= most_general_template (tmpl
);
10425 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10426 if (argvec
== error_mark_node
)
10427 RETURN (error_mark_node
);
10428 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10429 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10434 /* A local variable. */
10436 /* Subsequent calls to pushdecl will fill this in. */
10438 spec
= retrieve_local_specialization (t
);
10440 /* If we already have the specialization we need, there is
10441 nothing more to do. */
10448 if (TREE_CODE (t
) == VAR_DECL
&& DECL_ANON_UNION_VAR_P (t
))
10450 /* Just use name lookup to find a member alias for an anonymous
10451 union, but then add it to the hash table. */
10452 r
= lookup_name (DECL_NAME (t
));
10453 gcc_assert (DECL_ANON_UNION_VAR_P (r
));
10454 register_local_specialization (r
, t
);
10458 /* Create a new node for the specialization we need. */
10460 if (type
== NULL_TREE
)
10462 if (is_typedef_decl (t
))
10463 type
= DECL_ORIGINAL_TYPE (t
);
10465 type
= TREE_TYPE (t
);
10466 if (TREE_CODE (t
) == VAR_DECL
10467 && VAR_HAD_UNKNOWN_BOUND (t
)
10468 && type
!= error_mark_node
)
10469 type
= strip_array_domain (type
);
10470 type
= tsubst (type
, args
, complain
, in_decl
);
10472 if (TREE_CODE (r
) == VAR_DECL
)
10474 /* Even if the original location is out of scope, the
10475 newly substituted one is not. */
10476 DECL_DEAD_FOR_LOCAL (r
) = 0;
10477 DECL_INITIALIZED_P (r
) = 0;
10478 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10479 if (type
== error_mark_node
)
10480 RETURN (error_mark_node
);
10481 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10483 /* It may seem that this case cannot occur, since:
10488 declares a function, not a variable. However:
10491 template <typename T> void g() { T t; }
10492 template void g<f>();
10494 is an attempt to declare a variable with function
10496 error ("variable %qD has function type",
10497 /* R is not yet sufficiently initialized, so we
10498 just use its name. */
10500 RETURN (error_mark_node
);
10502 type
= complete_type (type
);
10503 /* Wait until cp_finish_decl to set this again, to handle
10504 circular dependency (template/instantiate6.C). */
10505 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10506 type
= check_var_type (DECL_NAME (r
), type
);
10508 if (DECL_HAS_VALUE_EXPR_P (t
))
10510 tree ve
= DECL_VALUE_EXPR (t
);
10511 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10512 /*constant_expression_p=*/false);
10513 if (REFERENCE_REF_P (ve
))
10515 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10516 ve
= TREE_OPERAND (ve
, 0);
10518 SET_DECL_VALUE_EXPR (r
, ve
);
10521 else if (DECL_SELF_REFERENCE_P (t
))
10522 SET_DECL_SELF_REFERENCE_P (r
);
10523 TREE_TYPE (r
) = type
;
10524 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10525 DECL_CONTEXT (r
) = ctx
;
10526 /* Clear out the mangled name and RTL for the instantiation. */
10527 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10528 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10529 SET_DECL_RTL (r
, NULL
);
10530 /* The initializer must not be expanded until it is required;
10531 see [temp.inst]. */
10532 DECL_INITIAL (r
) = NULL_TREE
;
10533 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10534 SET_DECL_RTL (r
, NULL
);
10535 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10536 if (TREE_CODE (r
) == VAR_DECL
)
10538 /* Possibly limit visibility based on template args. */
10539 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10540 if (DECL_VISIBILITY_SPECIFIED (t
))
10542 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10543 DECL_ATTRIBUTES (r
)
10544 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10546 determine_visibility (r
);
10551 /* A static data member declaration is always marked
10552 external when it is declared in-class, even if an
10553 initializer is present. We mimic the non-template
10554 processing here. */
10555 DECL_EXTERNAL (r
) = 1;
10557 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10558 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10559 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10561 else if (cp_unevaluated_operand
)
10563 /* We're substituting this var in a decltype outside of its
10564 scope, such as for a lambda return type. Don't add it to
10565 local_specializations, do perform auto deduction. */
10566 tree auto_node
= type_uses_auto (type
);
10570 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10571 /*constant_expression_p=*/false);
10572 init
= resolve_nondeduced_context (init
);
10573 TREE_TYPE (r
) = type
10574 = do_auto_deduction (type
, init
, auto_node
);
10578 register_local_specialization (r
, t
);
10580 DECL_CHAIN (r
) = NULL_TREE
;
10582 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10584 args
, complain
, in_decl
);
10586 /* Preserve a typedef that names a type. */
10587 if (is_typedef_decl (r
))
10589 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10590 set_underlying_type (r
);
10593 layout_decl (r
, 0);
10598 gcc_unreachable ();
10603 /* Restore the file and line information. */
10604 input_location
= saved_loc
;
10609 /* Substitute into the ARG_TYPES of a function type.
10610 If END is a TREE_CHAIN, leave it and any following types
10614 tsubst_arg_types (tree arg_types
,
10617 tsubst_flags_t complain
,
10620 tree remaining_arg_types
;
10621 tree type
= NULL_TREE
;
10623 tree expanded_args
= NULL_TREE
;
10626 if (!arg_types
|| arg_types
== void_list_node
|| arg_types
== end
)
10629 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10630 args
, end
, complain
, in_decl
);
10631 if (remaining_arg_types
== error_mark_node
)
10632 return error_mark_node
;
10634 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10636 /* For a pack expansion, perform substitution on the
10637 entire expression. Later on, we'll handle the arguments
10639 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10640 args
, complain
, in_decl
);
10642 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10643 /* So that we'll spin through the parameters, one by one. */
10644 i
= TREE_VEC_LENGTH (expanded_args
);
10647 /* We only partially substituted into the parameter
10648 pack. Our type is TYPE_PACK_EXPANSION. */
10649 type
= expanded_args
;
10650 expanded_args
= NULL_TREE
;
10658 type
= TREE_VEC_ELT (expanded_args
, i
);
10660 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10662 if (type
== error_mark_node
)
10663 return error_mark_node
;
10664 if (VOID_TYPE_P (type
))
10666 if (complain
& tf_error
)
10668 error ("invalid parameter type %qT", type
);
10670 error ("in declaration %q+D", in_decl
);
10672 return error_mark_node
;
10675 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10676 top-level qualifiers as required. */
10677 type
= cv_unqualified (type_decays_to (type
));
10679 /* We do not substitute into default arguments here. The standard
10680 mandates that they be instantiated only when needed, which is
10681 done in build_over_call. */
10682 default_arg
= TREE_PURPOSE (arg_types
);
10684 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10686 /* We've instantiated a template before its default arguments
10687 have been parsed. This can happen for a nested template
10688 class, and is not an error unless we require the default
10689 argument in a call of this function. */
10690 remaining_arg_types
=
10691 tree_cons (default_arg
, type
, remaining_arg_types
);
10692 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
10693 remaining_arg_types
);
10696 remaining_arg_types
=
10697 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10700 return remaining_arg_types
;
10703 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10704 *not* handle the exception-specification for FNTYPE, because the
10705 initial substitution of explicitly provided template parameters
10706 during argument deduction forbids substitution into the
10707 exception-specification:
10711 All references in the function type of the function template to the
10712 corresponding template parameters are replaced by the specified tem-
10713 plate argument values. If a substitution in a template parameter or
10714 in the function type of the function template results in an invalid
10715 type, type deduction fails. [Note: The equivalent substitution in
10716 exception specifications is done only when the function is instanti-
10717 ated, at which point a program is ill-formed if the substitution
10718 results in an invalid type.] */
10721 tsubst_function_type (tree t
,
10723 tsubst_flags_t complain
,
10730 /* The TYPE_CONTEXT is not used for function/method types. */
10731 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10733 /* Substitute the return type. */
10734 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10735 if (return_type
== error_mark_node
)
10736 return error_mark_node
;
10737 /* The standard does not presently indicate that creation of a
10738 function type with an invalid return type is a deduction failure.
10739 However, that is clearly analogous to creating an array of "void"
10740 or a reference to a reference. This is core issue #486. */
10741 if (TREE_CODE (return_type
) == ARRAY_TYPE
10742 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10744 if (complain
& tf_error
)
10746 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10747 error ("function returning an array");
10749 error ("function returning a function");
10751 return error_mark_node
;
10754 /* Substitute the argument types. */
10755 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
, NULL_TREE
,
10756 complain
, in_decl
);
10757 if (arg_types
== error_mark_node
)
10758 return error_mark_node
;
10760 /* Construct a new type node and return it. */
10761 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10763 fntype
= build_function_type (return_type
, arg_types
);
10764 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10768 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10769 if (! MAYBE_CLASS_TYPE_P (r
))
10773 Type deduction may fail for any of the following
10776 -- Attempting to create "pointer to member of T" when T
10777 is not a class type. */
10778 if (complain
& tf_error
)
10779 error ("creating pointer to member function of non-class type %qT",
10781 return error_mark_node
;
10784 fntype
= build_method_type_directly (r
, return_type
,
10785 TREE_CHAIN (arg_types
));
10787 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10792 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10793 ARGS into that specification, and return the substituted
10794 specification. If there is no specification, return NULL_TREE. */
10797 tsubst_exception_specification (tree fntype
,
10799 tsubst_flags_t complain
,
10806 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10807 new_specs
= NULL_TREE
;
10808 if (specs
&& TREE_PURPOSE (specs
))
10810 /* A noexcept-specifier. */
10811 tree expr
= TREE_PURPOSE (specs
);
10812 if (expr
== boolean_true_node
|| expr
== boolean_false_node
)
10816 /* Defer instantiation of noexcept-specifiers to avoid
10817 excessive instantiations (c++/49107). */
10818 new_specs
= make_node (DEFERRED_NOEXCEPT
);
10819 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
10821 /* We already partially instantiated this member template,
10822 so combine the new args with the old. */
10823 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
10824 = DEFERRED_NOEXCEPT_PATTERN (expr
);
10825 DEFERRED_NOEXCEPT_ARGS (new_specs
)
10826 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
10830 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
10831 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
10835 new_specs
= tsubst_copy_and_build
10836 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
10837 /*integral_constant_expression_p=*/true);
10838 new_specs
= build_noexcept_spec (new_specs
, complain
);
10842 if (! TREE_VALUE (specs
))
10849 tree expanded_specs
= NULL_TREE
;
10851 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
10853 /* Expand the pack expansion type. */
10854 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
10858 if (expanded_specs
== error_mark_node
)
10859 return error_mark_node
;
10860 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
10861 len
= TREE_VEC_LENGTH (expanded_specs
);
10864 /* We're substituting into a member template, so
10865 we got a TYPE_PACK_EXPANSION back. Add that
10866 expansion and move on. */
10867 gcc_assert (TREE_CODE (expanded_specs
)
10868 == TYPE_PACK_EXPANSION
);
10869 new_specs
= add_exception_specifier (new_specs
,
10872 specs
= TREE_CHAIN (specs
);
10877 for (i
= 0; i
< len
; ++i
)
10879 if (expanded_specs
)
10880 spec
= TREE_VEC_ELT (expanded_specs
, i
);
10882 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
10883 if (spec
== error_mark_node
)
10885 new_specs
= add_exception_specifier (new_specs
, spec
,
10889 specs
= TREE_CHAIN (specs
);
10895 /* Take the tree structure T and replace template parameters used
10896 therein with the argument vector ARGS. IN_DECL is an associated
10897 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
10898 Issue error and warning messages under control of COMPLAIN. Note
10899 that we must be relatively non-tolerant of extensions here, in
10900 order to preserve conformance; if we allow substitutions that
10901 should not be allowed, we may allow argument deductions that should
10902 not succeed, and therefore report ambiguous overload situations
10903 where there are none. In theory, we could allow the substitution,
10904 but indicate that it should have failed, and allow our caller to
10905 make sure that the right thing happens, but we don't try to do this
10908 This function is used for dealing with types, decls and the like;
10909 for expressions, use tsubst_expr or tsubst_copy. */
10912 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
10914 enum tree_code code
;
10915 tree type
, r
= NULL_TREE
;
10917 if (t
== NULL_TREE
|| t
== error_mark_node
10918 || t
== integer_type_node
10919 || t
== void_type_node
10920 || t
== char_type_node
10921 || t
== unknown_type_node
10922 || TREE_CODE (t
) == NAMESPACE_DECL
10923 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
10927 return tsubst_decl (t
, args
, complain
);
10929 if (args
== NULL_TREE
)
10932 code
= TREE_CODE (t
);
10934 if (code
== IDENTIFIER_NODE
)
10935 type
= IDENTIFIER_TYPE_VALUE (t
);
10937 type
= TREE_TYPE (t
);
10939 gcc_assert (type
!= unknown_type_node
);
10941 /* Reuse typedefs. We need to do this to handle dependent attributes,
10942 such as attribute aligned. */
10944 && typedef_variant_p (t
))
10946 tree decl
= TYPE_NAME (t
);
10948 if (TYPE_DECL_ALIAS_P (decl
)
10949 && DECL_LANG_SPECIFIC (decl
)
10950 && DECL_TEMPLATE_INFO (decl
)
10951 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
10953 /* DECL represents an alias template and we want to
10955 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10956 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
10957 r
= instantiate_alias_template (tmpl
, gen_args
, complain
);
10959 else if (DECL_CLASS_SCOPE_P (decl
)
10960 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10961 && uses_template_parms (DECL_CONTEXT (decl
)))
10963 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
10964 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
10965 r
= retrieve_specialization (tmpl
, gen_args
, 0);
10967 else if (DECL_FUNCTION_SCOPE_P (decl
)
10968 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
10969 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
10970 r
= retrieve_local_specialization (decl
);
10972 /* The typedef is from a non-template context. */
10978 r
= cp_build_qualified_type_real
10979 (r
, cp_type_quals (t
) | cp_type_quals (r
),
10980 complain
| tf_ignore_bad_quals
);
10984 /* We don't have an instantiation yet, so drop the typedef. */
10985 t
= DECL_ORIGINAL_TYPE (decl
);
10989 && code
!= TYPENAME_TYPE
10990 && code
!= TEMPLATE_TYPE_PARM
10991 && code
!= IDENTIFIER_NODE
10992 && code
!= FUNCTION_TYPE
10993 && code
!= METHOD_TYPE
)
10994 type
= tsubst (type
, args
, complain
, in_decl
);
10995 if (type
== error_mark_node
)
10996 return error_mark_node
;
11002 case ENUMERAL_TYPE
:
11003 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11004 /*entering_scope=*/0);
11007 case IDENTIFIER_NODE
:
11018 if (t
== integer_type_node
)
11021 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11022 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11026 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11028 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11029 /*integral_constant_expression_p=*/false);
11031 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11033 if (TREE_CODE (max
) == NOP_EXPR
11034 && TREE_SIDE_EFFECTS (omax
)
11035 && !TREE_TYPE (max
))
11036 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11038 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11039 with TREE_SIDE_EFFECTS that indicates this is not an integral
11040 constant expression. */
11041 if (processing_template_decl
11042 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11044 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11045 TREE_SIDE_EFFECTS (max
) = 1;
11048 return compute_array_index_type (NULL_TREE
, max
, complain
);
11051 case TEMPLATE_TYPE_PARM
:
11052 case TEMPLATE_TEMPLATE_PARM
:
11053 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11054 case TEMPLATE_PARM_INDEX
:
11059 tree arg
= NULL_TREE
;
11063 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11064 template_parm_level_and_index (t
, &level
, &idx
);
11066 levels
= TMPL_ARGS_DEPTH (args
);
11067 if (level
<= levels
)
11069 arg
= TMPL_ARG (args
, level
, idx
);
11071 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11072 /* See through ARGUMENT_PACK_SELECT arguments. */
11073 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11076 if (arg
== error_mark_node
)
11077 return error_mark_node
;
11078 else if (arg
!= NULL_TREE
)
11080 if (ARGUMENT_PACK_P (arg
))
11081 /* If ARG is an argument pack, we don't actually want to
11082 perform a substitution here, because substitutions
11083 for argument packs are only done
11084 element-by-element. We can get to this point when
11085 substituting the type of a non-type template
11086 parameter pack, when that type actually contains
11087 template parameter packs from an outer template, e.g.,
11089 template<typename... Types> struct A {
11090 template<Types... Values> struct B { };
11094 if (code
== TEMPLATE_TYPE_PARM
)
11097 gcc_assert (TYPE_P (arg
));
11099 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11101 return cp_build_qualified_type_real
11102 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11104 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11106 /* We are processing a type constructed from a
11107 template template parameter. */
11108 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11109 args
, complain
, in_decl
);
11110 if (argvec
== error_mark_node
)
11111 return error_mark_node
;
11113 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11114 || TREE_CODE (arg
) == TEMPLATE_DECL
11115 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11117 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11118 /* Consider this code:
11120 template <template <class> class Template>
11122 template <class Arg> using Bind = Template<Arg>;
11125 template <template <class> class Template, class Arg>
11126 using Instantiate = Template<Arg>; //#0
11128 template <template <class> class Template,
11131 Instantiate<Internal<Template>::template Bind,
11134 When #1 is parsed, the
11135 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11136 parameter `Template' in #0 matches the
11137 UNBOUND_CLASS_TEMPLATE representing the argument
11138 `Internal<Template>::template Bind'; We then want
11139 to assemble the type `Bind<Argument>' that can't
11140 be fully created right now, because
11141 `Internal<Template>' not being complete, the Bind
11142 template cannot be looked up in that context. So
11143 we need to "store" `Bind<Argument>' for later
11144 when the context of Bind becomes complete. Let's
11145 store that in a TYPENAME_TYPE. */
11146 return make_typename_type (TYPE_CONTEXT (arg
),
11147 build_nt (TEMPLATE_ID_EXPR
,
11148 TYPE_IDENTIFIER (arg
),
11153 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11154 are resolving nested-types in the signature of a
11155 member function templates. Otherwise ARG is a
11156 TEMPLATE_DECL and is the real template to be
11158 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11159 arg
= TYPE_NAME (arg
);
11161 r
= lookup_template_class (arg
,
11163 DECL_CONTEXT (arg
),
11164 /*entering_scope=*/0,
11166 return cp_build_qualified_type_real
11167 (r
, cp_type_quals (t
), complain
);
11170 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11171 return convert_from_reference (unshare_expr (arg
));
11175 /* This can happen during the attempted tsubst'ing in
11176 unify. This means that we don't yet have any information
11177 about the template parameter in question. */
11180 /* Early in template argument deduction substitution, we don't
11181 want to reduce the level of 'auto', or it will be confused
11182 with a normal template parm in subsequent deduction. */
11183 if (is_auto (t
) && (complain
& tf_partial
))
11186 /* If we get here, we must have been looking at a parm for a
11187 more deeply nested template. Make a new version of this
11188 template parameter, but with a lower level. */
11191 case TEMPLATE_TYPE_PARM
:
11192 case TEMPLATE_TEMPLATE_PARM
:
11193 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11194 if (cp_type_quals (t
))
11196 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11197 r
= cp_build_qualified_type_real
11198 (r
, cp_type_quals (t
),
11199 complain
| (code
== TEMPLATE_TYPE_PARM
11200 ? tf_ignore_bad_quals
: 0));
11205 TEMPLATE_TYPE_PARM_INDEX (r
)
11206 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11207 r
, levels
, args
, complain
);
11208 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11209 TYPE_MAIN_VARIANT (r
) = r
;
11210 TYPE_POINTER_TO (r
) = NULL_TREE
;
11211 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11213 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11214 /* We have reduced the level of the template
11215 template parameter, but not the levels of its
11216 template parameters, so canonical_type_parameter
11217 will not be able to find the canonical template
11218 template parameter for this level. Thus, we
11219 require structural equality checking to compare
11220 TEMPLATE_TEMPLATE_PARMs. */
11221 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11222 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11223 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11225 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11227 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11229 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11230 complain
, in_decl
);
11231 if (argvec
== error_mark_node
)
11232 return error_mark_node
;
11234 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11235 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11240 case TEMPLATE_PARM_INDEX
:
11241 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11245 gcc_unreachable ();
11253 tree purpose
, value
, chain
;
11255 if (t
== void_list_node
)
11258 purpose
= TREE_PURPOSE (t
);
11261 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11262 if (purpose
== error_mark_node
)
11263 return error_mark_node
;
11265 value
= TREE_VALUE (t
);
11268 value
= tsubst (value
, args
, complain
, in_decl
);
11269 if (value
== error_mark_node
)
11270 return error_mark_node
;
11272 chain
= TREE_CHAIN (t
);
11273 if (chain
&& chain
!= void_type_node
)
11275 chain
= tsubst (chain
, args
, complain
, in_decl
);
11276 if (chain
== error_mark_node
)
11277 return error_mark_node
;
11279 if (purpose
== TREE_PURPOSE (t
)
11280 && value
== TREE_VALUE (t
)
11281 && chain
== TREE_CHAIN (t
))
11283 return hash_tree_cons (purpose
, value
, chain
);
11287 /* We should never be tsubsting a binfo. */
11288 gcc_unreachable ();
11291 /* A vector of template arguments. */
11292 gcc_assert (!type
);
11293 return tsubst_template_args (t
, args
, complain
, in_decl
);
11296 case REFERENCE_TYPE
:
11298 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11303 Type deduction may fail for any of the following
11306 -- Attempting to create a pointer to reference type.
11307 -- Attempting to create a reference to a reference type or
11308 a reference to void.
11310 Core issue 106 says that creating a reference to a reference
11311 during instantiation is no longer a cause for failure. We
11312 only enforce this check in strict C++98 mode. */
11313 if ((TREE_CODE (type
) == REFERENCE_TYPE
11314 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11315 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11317 static location_t last_loc
;
11319 /* We keep track of the last time we issued this error
11320 message to avoid spewing a ton of messages during a
11321 single bad template instantiation. */
11322 if (complain
& tf_error
11323 && last_loc
!= input_location
)
11325 if (TREE_CODE (type
) == VOID_TYPE
)
11326 error ("forming reference to void");
11327 else if (code
== POINTER_TYPE
)
11328 error ("forming pointer to reference type %qT", type
);
11330 error ("forming reference to reference type %qT", type
);
11331 last_loc
= input_location
;
11334 return error_mark_node
;
11336 else if (code
== POINTER_TYPE
)
11338 r
= build_pointer_type (type
);
11339 if (TREE_CODE (type
) == METHOD_TYPE
)
11340 r
= build_ptrmemfunc_type (r
);
11342 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11343 /* In C++0x, during template argument substitution, when there is an
11344 attempt to create a reference to a reference type, reference
11345 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11347 "If a template-argument for a template-parameter T names a type
11348 that is a reference to a type A, an attempt to create the type
11349 'lvalue reference to cv T' creates the type 'lvalue reference to
11350 A,' while an attempt to create the type type rvalue reference to
11351 cv T' creates the type T"
11353 r
= cp_build_reference_type
11355 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11357 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11358 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11360 if (r
!= error_mark_node
)
11361 /* Will this ever be needed for TYPE_..._TO values? */
11368 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11369 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11373 Type deduction may fail for any of the following
11376 -- Attempting to create "pointer to member of T" when T
11377 is not a class type. */
11378 if (complain
& tf_error
)
11379 error ("creating pointer to member of non-class type %qT", r
);
11380 return error_mark_node
;
11382 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11384 if (complain
& tf_error
)
11385 error ("creating pointer to member reference type %qT", type
);
11386 return error_mark_node
;
11388 if (TREE_CODE (type
) == VOID_TYPE
)
11390 if (complain
& tf_error
)
11391 error ("creating pointer to member of type void");
11392 return error_mark_node
;
11394 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11395 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11397 /* The type of the implicit object parameter gets its
11398 cv-qualifiers from the FUNCTION_TYPE. */
11400 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11401 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11402 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11406 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11410 case FUNCTION_TYPE
:
11415 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11416 if (fntype
== error_mark_node
)
11417 return error_mark_node
;
11419 /* Substitute the exception specification. */
11420 specs
= tsubst_exception_specification (t
, args
, complain
,
11421 in_decl
, /*defer_ok*/true);
11422 if (specs
== error_mark_node
)
11423 return error_mark_node
;
11425 fntype
= build_exception_variant (fntype
, specs
);
11430 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11431 if (domain
== error_mark_node
)
11432 return error_mark_node
;
11434 /* As an optimization, we avoid regenerating the array type if
11435 it will obviously be the same as T. */
11436 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11439 /* These checks should match the ones in grokdeclarator.
11443 The deduction may fail for any of the following reasons:
11445 -- Attempting to create an array with an element type that
11446 is void, a function type, or a reference type, or [DR337]
11447 an abstract class type. */
11448 if (TREE_CODE (type
) == VOID_TYPE
11449 || TREE_CODE (type
) == FUNCTION_TYPE
11450 || TREE_CODE (type
) == REFERENCE_TYPE
)
11452 if (complain
& tf_error
)
11453 error ("creating array of %qT", type
);
11454 return error_mark_node
;
11456 if (ABSTRACT_CLASS_TYPE_P (type
))
11458 if (complain
& tf_error
)
11459 error ("creating array of %qT, which is an abstract class type",
11461 return error_mark_node
;
11464 r
= build_cplus_array_type (type
, domain
);
11466 if (TYPE_USER_ALIGN (t
))
11468 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11469 TYPE_USER_ALIGN (r
) = 1;
11475 case TYPENAME_TYPE
:
11477 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11478 in_decl
, /*entering_scope=*/1);
11479 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11480 complain
, in_decl
);
11482 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11483 return error_mark_node
;
11485 if (!MAYBE_CLASS_TYPE_P (ctx
))
11487 if (complain
& tf_error
)
11488 error ("%qT is not a class, struct, or union type", ctx
);
11489 return error_mark_node
;
11491 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11493 /* Normally, make_typename_type does not require that the CTX
11494 have complete type in order to allow things like:
11496 template <class T> struct S { typename S<T>::X Y; };
11498 But, such constructs have already been resolved by this
11499 point, so here CTX really should have complete type, unless
11500 it's a partial instantiation. */
11501 ctx
= complete_type (ctx
);
11502 if (!COMPLETE_TYPE_P (ctx
))
11504 if (complain
& tf_error
)
11505 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11506 return error_mark_node
;
11510 f
= make_typename_type (ctx
, f
, typename_type
,
11511 complain
| tf_keep_type_decl
);
11512 if (f
== error_mark_node
)
11514 if (TREE_CODE (f
) == TYPE_DECL
)
11516 complain
|= tf_ignore_bad_quals
;
11520 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11522 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11524 if (complain
& tf_error
)
11525 error ("%qT resolves to %qT, which is not an enumeration type",
11528 return error_mark_node
;
11530 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11532 if (complain
& tf_error
)
11533 error ("%qT resolves to %qT, which is is not a class type",
11536 return error_mark_node
;
11540 return cp_build_qualified_type_real
11541 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11544 case UNBOUND_CLASS_TEMPLATE
:
11546 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11547 in_decl
, /*entering_scope=*/1);
11548 tree name
= TYPE_IDENTIFIER (t
);
11549 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11551 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11552 return error_mark_node
;
11555 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11556 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11563 ++cp_unevaluated_operand
;
11564 ++c_inhibit_evaluation_warnings
;
11566 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11568 /*integral_constant_expression_p=*/false);
11570 --cp_unevaluated_operand
;
11571 --c_inhibit_evaluation_warnings
;
11573 type
= finish_typeof (type
);
11574 return cp_build_qualified_type_real (type
,
11576 | cp_type_quals (type
),
11580 case DECLTYPE_TYPE
:
11584 ++cp_unevaluated_operand
;
11585 ++c_inhibit_evaluation_warnings
;
11587 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11589 /*integral_constant_expression_p=*/false);
11591 --cp_unevaluated_operand
;
11592 --c_inhibit_evaluation_warnings
;
11594 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11595 type
= lambda_capture_field_type (type
);
11596 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11597 type
= lambda_proxy_type (type
);
11599 type
= finish_decltype_type
11600 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11601 return cp_build_qualified_type_real (type
,
11603 | cp_type_quals (type
),
11607 case UNDERLYING_TYPE
:
11609 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11610 complain
, in_decl
);
11611 return finish_underlying_type (type
);
11614 case TYPE_ARGUMENT_PACK
:
11615 case NONTYPE_ARGUMENT_PACK
:
11617 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11619 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11623 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11625 /* For template nontype argument packs, also substitute into
11627 if (code
== NONTYPE_ARGUMENT_PACK
)
11628 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11646 /* We should use one of the expression tsubsts for these codes. */
11647 gcc_unreachable ();
11650 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11651 return error_mark_node
;
11655 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11656 type of the expression on the left-hand side of the "." or "->"
11660 tsubst_baselink (tree baselink
, tree object_type
,
11661 tree args
, tsubst_flags_t complain
, tree in_decl
)
11664 tree qualifying_scope
;
11667 tree template_args
= 0;
11668 bool template_id_p
= false;
11669 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
11671 /* A baselink indicates a function from a base class. Both the
11672 BASELINK_ACCESS_BINFO and the base class referenced may
11673 indicate bases of the template class, rather than the
11674 instantiated class. In addition, lookups that were not
11675 ambiguous before may be ambiguous now. Therefore, we perform
11676 the lookup again. */
11677 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11678 qualifying_scope
= tsubst (qualifying_scope
, args
,
11679 complain
, in_decl
);
11680 fns
= BASELINK_FUNCTIONS (baselink
);
11681 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11682 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11684 template_id_p
= true;
11685 template_args
= TREE_OPERAND (fns
, 1);
11686 fns
= TREE_OPERAND (fns
, 0);
11688 template_args
= tsubst_template_args (template_args
, args
,
11689 complain
, in_decl
);
11691 name
= DECL_NAME (get_first_fn (fns
));
11692 if (IDENTIFIER_TYPENAME_P (name
))
11693 name
= mangle_conv_op_name_for_type (optype
);
11694 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11696 return error_mark_node
;
11698 /* If lookup found a single function, mark it as used at this
11699 point. (If it lookup found multiple functions the one selected
11700 later by overload resolution will be marked as used at that
11702 if (BASELINK_P (baselink
))
11703 fns
= BASELINK_FUNCTIONS (baselink
);
11704 if (!template_id_p
&& !really_overloaded_fn (fns
))
11705 mark_used (OVL_CURRENT (fns
));
11707 /* Add back the template arguments, if present. */
11708 if (BASELINK_P (baselink
) && template_id_p
)
11709 BASELINK_FUNCTIONS (baselink
)
11710 = build_nt (TEMPLATE_ID_EXPR
,
11711 BASELINK_FUNCTIONS (baselink
),
11713 /* Update the conversion operator type. */
11714 BASELINK_OPTYPE (baselink
) = optype
;
11717 object_type
= current_class_type
;
11720 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
11726 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11727 true if the qualified-id will be a postfix-expression in-and-of
11728 itself; false if more of the postfix-expression follows the
11729 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11733 tsubst_qualified_id (tree qualified_id
, tree args
,
11734 tsubst_flags_t complain
, tree in_decl
,
11735 bool done
, bool address_p
)
11741 tree template_args
;
11742 location_t loc
= UNKNOWN_LOCATION
;
11744 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11746 /* Figure out what name to look up. */
11747 name
= TREE_OPERAND (qualified_id
, 1);
11748 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11750 is_template
= true;
11751 loc
= EXPR_LOCATION (name
);
11752 template_args
= TREE_OPERAND (name
, 1);
11754 template_args
= tsubst_template_args (template_args
, args
,
11755 complain
, in_decl
);
11756 name
= TREE_OPERAND (name
, 0);
11760 is_template
= false;
11761 template_args
= NULL_TREE
;
11764 /* Substitute into the qualifying scope. When there are no ARGS, we
11765 are just trying to simplify a non-dependent expression. In that
11766 case the qualifying scope may be dependent, and, in any case,
11767 substituting will not help. */
11768 scope
= TREE_OPERAND (qualified_id
, 0);
11771 scope
= tsubst (scope
, args
, complain
, in_decl
);
11772 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11777 if (dependent_scope_p (scope
))
11780 expr
= build_min_nt_loc (loc
, TEMPLATE_ID_EXPR
, expr
, template_args
);
11781 return build_qualified_name (NULL_TREE
, scope
, expr
,
11782 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11785 if (!BASELINK_P (name
) && !DECL_P (expr
))
11787 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11789 /* A BIT_NOT_EXPR is used to represent a destructor. */
11790 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11792 error ("qualifying type %qT does not match destructor name ~%qT",
11793 scope
, TREE_OPERAND (expr
, 0));
11794 expr
= error_mark_node
;
11797 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11798 /*is_type_p=*/0, false);
11801 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11802 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11803 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11805 if (complain
& tf_error
)
11807 error ("dependent-name %qE is parsed as a non-type, but "
11808 "instantiation yields a type", qualified_id
);
11809 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11811 return error_mark_node
;
11817 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11819 /* Remember that there was a reference to this entity. */
11823 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
11825 if (complain
& tf_error
)
11826 qualified_name_lookup_error (scope
,
11827 TREE_OPERAND (qualified_id
, 1),
11828 expr
, input_location
);
11829 return error_mark_node
;
11833 expr
= lookup_template_function (expr
, template_args
);
11835 if (expr
== error_mark_node
&& complain
& tf_error
)
11836 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
11837 expr
, input_location
);
11838 else if (TYPE_P (scope
))
11840 expr
= (adjust_result_of_qualified_name_lookup
11841 (expr
, scope
, current_class_type
));
11842 expr
= (finish_qualified_id_expr
11843 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
11844 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
11845 /*template_arg_p=*/false));
11848 /* Expressions do not generally have reference type. */
11849 if (TREE_CODE (expr
) != SCOPE_REF
11850 /* However, if we're about to form a pointer-to-member, we just
11851 want the referenced member referenced. */
11852 && TREE_CODE (expr
) != OFFSET_REF
)
11853 expr
= convert_from_reference (expr
);
11858 /* Like tsubst, but deals with expressions. This function just replaces
11859 template parms; to finish processing the resultant expression, use
11860 tsubst_copy_and_build or tsubst_expr. */
11863 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11865 enum tree_code code
;
11868 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
11871 code
= TREE_CODE (t
);
11876 r
= retrieve_local_specialization (t
);
11878 if (r
== NULL_TREE
)
11880 /* We get here for a use of 'this' in an NSDMI. */
11881 if (DECL_NAME (t
) == this_identifier
11882 && at_function_scope_p ()
11883 && DECL_CONSTRUCTOR_P (current_function_decl
))
11884 return current_class_ptr
;
11886 /* This can happen for a parameter name used later in a function
11887 declaration (such as in a late-specified return type). Just
11888 make a dummy decl, since it's only used for its type. */
11889 gcc_assert (cp_unevaluated_operand
!= 0);
11890 r
= tsubst_decl (t
, args
, complain
);
11891 /* Give it the template pattern as its context; its true context
11892 hasn't been instantiated yet and this is good enough for
11894 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
11897 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
11898 r
= ARGUMENT_PACK_SELECT_ARG (r
);
11907 if (DECL_TEMPLATE_PARM_P (t
))
11908 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
11909 /* There is no need to substitute into namespace-scope
11911 if (DECL_NAMESPACE_SCOPE_P (t
))
11913 /* If ARGS is NULL, then T is known to be non-dependent. */
11914 if (args
== NULL_TREE
)
11915 return integral_constant_value (t
);
11917 /* Unfortunately, we cannot just call lookup_name here.
11920 template <int I> int f() {
11922 struct S { void g() { E e = a; } };
11925 When we instantiate f<7>::S::g(), say, lookup_name is not
11926 clever enough to find f<7>::a. */
11928 = tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11929 /*entering_scope=*/0);
11931 for (v
= TYPE_VALUES (enum_type
);
11933 v
= TREE_CHAIN (v
))
11934 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
11935 return TREE_VALUE (v
);
11937 /* We didn't find the name. That should never happen; if
11938 name-lookup found it during preliminary parsing, we
11939 should find it again here during instantiation. */
11940 gcc_unreachable ();
11945 if (DECL_CONTEXT (t
))
11949 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
11950 /*entering_scope=*/1);
11951 if (ctx
!= DECL_CONTEXT (t
))
11953 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
11956 if (complain
& tf_error
)
11957 error ("using invalid field %qD", t
);
11958 return error_mark_node
;
11967 case FUNCTION_DECL
:
11968 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
11969 || local_variable_p (t
))
11970 t
= tsubst (t
, args
, complain
, in_decl
);
11974 case NAMESPACE_DECL
:
11978 /* An OVERLOAD will always be a non-dependent overload set; an
11979 overload set from function scope will just be represented with an
11980 IDENTIFIER_NODE, and from class scope with a BASELINK. */
11981 gcc_assert (!uses_template_parms (t
));
11985 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
11987 case TEMPLATE_DECL
:
11988 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
11989 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
11990 args
, complain
, in_decl
);
11991 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
11992 return tsubst (t
, args
, complain
, in_decl
);
11993 else if (DECL_CLASS_SCOPE_P (t
)
11994 && uses_template_parms (DECL_CONTEXT (t
)))
11996 /* Template template argument like the following example need
11999 template <template <class> class TT> struct C {};
12000 template <class T> struct D {
12001 template <class U> struct E {};
12006 We are processing the template argument `E' in #1 for
12007 the template instantiation #2. Originally, `E' is a
12008 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12009 have to substitute this with one having context `D<int>'. */
12011 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12012 return lookup_field (context
, DECL_NAME(t
), 0, false);
12015 /* Ordinary template template argument. */
12019 case REINTERPRET_CAST_EXPR
:
12020 case CONST_CAST_EXPR
:
12021 case STATIC_CAST_EXPR
:
12022 case DYNAMIC_CAST_EXPR
:
12023 case IMPLICIT_CONV_EXPR
:
12027 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12028 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12031 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12037 ++cp_unevaluated_operand
;
12038 ++c_inhibit_evaluation_warnings
;
12039 /* We only want to compute the number of arguments. */
12040 expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
12041 complain
, in_decl
);
12042 --cp_unevaluated_operand
;
12043 --c_inhibit_evaluation_warnings
;
12045 if (TREE_CODE (expanded
) == TREE_VEC
)
12046 len
= TREE_VEC_LENGTH (expanded
);
12048 if (expanded
== error_mark_node
)
12049 return error_mark_node
;
12050 else if (PACK_EXPANSION_P (expanded
)
12051 || (TREE_CODE (expanded
) == TREE_VEC
12053 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12055 if (TREE_CODE (expanded
) == TREE_VEC
)
12056 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12058 if (TYPE_P (expanded
))
12059 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12060 complain
& tf_error
);
12062 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12063 complain
& tf_error
);
12066 return build_int_cst (size_type_node
, len
);
12072 case TRUTH_NOT_EXPR
:
12075 case UNARY_PLUS_EXPR
: /* Unary + */
12077 case AT_ENCODE_EXPR
:
12081 case REALPART_EXPR
:
12082 case IMAGPART_EXPR
:
12084 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12085 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12087 case COMPONENT_REF
:
12092 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12093 name
= TREE_OPERAND (t
, 1);
12094 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12096 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12097 complain
, in_decl
);
12098 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12100 else if (TREE_CODE (name
) == SCOPE_REF
12101 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12103 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12104 complain
, in_decl
);
12105 name
= TREE_OPERAND (name
, 1);
12106 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12107 complain
, in_decl
);
12108 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12109 name
= build_qualified_name (/*type=*/NULL_TREE
,
12111 /*template_p=*/false);
12113 else if (BASELINK_P (name
))
12114 name
= tsubst_baselink (name
,
12115 non_reference (TREE_TYPE (object
)),
12119 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12120 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12126 case TRUNC_DIV_EXPR
:
12127 case CEIL_DIV_EXPR
:
12128 case FLOOR_DIV_EXPR
:
12129 case ROUND_DIV_EXPR
:
12130 case EXACT_DIV_EXPR
:
12134 case TRUNC_MOD_EXPR
:
12135 case FLOOR_MOD_EXPR
:
12136 case TRUTH_ANDIF_EXPR
:
12137 case TRUTH_ORIF_EXPR
:
12138 case TRUTH_AND_EXPR
:
12139 case TRUTH_OR_EXPR
:
12152 case COMPOUND_EXPR
:
12155 case PREDECREMENT_EXPR
:
12156 case PREINCREMENT_EXPR
:
12157 case POSTDECREMENT_EXPR
:
12158 case POSTINCREMENT_EXPR
:
12160 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12161 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12164 return build_qualified_name (/*type=*/NULL_TREE
,
12165 tsubst_copy (TREE_OPERAND (t
, 0),
12166 args
, complain
, in_decl
),
12167 tsubst_copy (TREE_OPERAND (t
, 1),
12168 args
, complain
, in_decl
),
12169 QUALIFIED_NAME_IS_TEMPLATE (t
));
12174 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12175 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12176 NULL_TREE
, NULL_TREE
);
12180 int n
= VL_EXP_OPERAND_LENGTH (t
);
12181 tree result
= build_vl_exp (CALL_EXPR
, n
);
12183 for (i
= 0; i
< n
; i
++)
12184 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12185 complain
, in_decl
);
12191 case PSEUDO_DTOR_EXPR
:
12194 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12195 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12196 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12197 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12204 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12205 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12206 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12207 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12214 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12215 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12216 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12217 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12221 case TEMPLATE_ID_EXPR
:
12223 /* Substituted template arguments */
12224 tree fn
= TREE_OPERAND (t
, 0);
12225 tree targs
= TREE_OPERAND (t
, 1);
12227 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12229 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12231 return lookup_template_function (fn
, targs
);
12236 tree purpose
, value
, chain
;
12238 if (t
== void_list_node
)
12241 purpose
= TREE_PURPOSE (t
);
12243 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12244 value
= TREE_VALUE (t
);
12246 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12247 chain
= TREE_CHAIN (t
);
12248 if (chain
&& chain
!= void_type_node
)
12249 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12250 if (purpose
== TREE_PURPOSE (t
)
12251 && value
== TREE_VALUE (t
)
12252 && chain
== TREE_CHAIN (t
))
12254 return tree_cons (purpose
, value
, chain
);
12259 case ENUMERAL_TYPE
:
12261 case TEMPLATE_TYPE_PARM
:
12262 case TEMPLATE_TEMPLATE_PARM
:
12263 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12264 case TEMPLATE_PARM_INDEX
:
12266 case REFERENCE_TYPE
:
12268 case FUNCTION_TYPE
:
12271 case TYPENAME_TYPE
:
12272 case UNBOUND_CLASS_TEMPLATE
:
12274 case DECLTYPE_TYPE
:
12276 return tsubst (t
, args
, complain
, in_decl
);
12278 case IDENTIFIER_NODE
:
12279 if (IDENTIFIER_TYPENAME_P (t
))
12281 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12282 return mangle_conv_op_name_for_type (new_type
);
12288 /* This is handled by tsubst_copy_and_build. */
12289 gcc_unreachable ();
12292 return build_x_va_arg (EXPR_LOCATION (t
),
12293 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12295 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12297 case CLEANUP_POINT_EXPR
:
12298 /* We shouldn't have built any of these during initial template
12299 generation. Instead, they should be built during instantiation
12300 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12301 gcc_unreachable ();
12305 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12306 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12307 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12308 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12309 mark_used (TREE_OPERAND (r
, 1));
12312 case EXPR_PACK_EXPANSION
:
12313 error ("invalid use of pack expansion expression");
12314 return error_mark_node
;
12316 case NONTYPE_ARGUMENT_PACK
:
12317 error ("use %<...%> to expand argument pack");
12318 return error_mark_node
;
12325 /* Instantiate any typedefs in the type. */
12326 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12327 r
= fold_convert (type
, t
);
12328 gcc_assert (TREE_CODE (r
) == code
);
12333 /* These can sometimes show up in a partial instantiation, but never
12334 involve template parms. */
12335 gcc_assert (!uses_template_parms (t
));
12339 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12340 gcc_checking_assert (false);
12345 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12348 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12351 tree new_clauses
= NULL
, nc
, oc
;
12353 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12355 nc
= copy_node (oc
);
12356 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12359 switch (OMP_CLAUSE_CODE (nc
))
12361 case OMP_CLAUSE_LASTPRIVATE
:
12362 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12364 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12365 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12366 in_decl
, /*integral_constant_expression_p=*/false);
12367 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12368 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12371 case OMP_CLAUSE_PRIVATE
:
12372 case OMP_CLAUSE_SHARED
:
12373 case OMP_CLAUSE_FIRSTPRIVATE
:
12374 case OMP_CLAUSE_REDUCTION
:
12375 case OMP_CLAUSE_COPYIN
:
12376 case OMP_CLAUSE_COPYPRIVATE
:
12377 case OMP_CLAUSE_IF
:
12378 case OMP_CLAUSE_NUM_THREADS
:
12379 case OMP_CLAUSE_SCHEDULE
:
12380 case OMP_CLAUSE_COLLAPSE
:
12381 case OMP_CLAUSE_FINAL
:
12382 OMP_CLAUSE_OPERAND (nc
, 0)
12383 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12384 in_decl
, /*integral_constant_expression_p=*/false);
12386 case OMP_CLAUSE_NOWAIT
:
12387 case OMP_CLAUSE_ORDERED
:
12388 case OMP_CLAUSE_DEFAULT
:
12389 case OMP_CLAUSE_UNTIED
:
12390 case OMP_CLAUSE_MERGEABLE
:
12393 gcc_unreachable ();
12397 return finish_omp_clauses (nreverse (new_clauses
));
12400 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12403 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12406 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12408 tree purpose
, value
, chain
;
12413 if (TREE_CODE (t
) != TREE_LIST
)
12414 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12415 /*function_p=*/false,
12416 /*integral_constant_expression_p=*/false);
12418 if (t
== void_list_node
)
12421 purpose
= TREE_PURPOSE (t
);
12423 purpose
= RECUR (purpose
);
12424 value
= TREE_VALUE (t
);
12427 if (TREE_CODE (value
) != LABEL_DECL
)
12428 value
= RECUR (value
);
12431 value
= lookup_label (DECL_NAME (value
));
12432 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12433 TREE_USED (value
) = 1;
12436 chain
= TREE_CHAIN (t
);
12437 if (chain
&& chain
!= void_type_node
)
12438 chain
= RECUR (chain
);
12439 return tree_cons (purpose
, value
, chain
);
12443 /* Substitute one OMP_FOR iterator. */
12446 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12447 tree condv
, tree incrv
, tree
*clauses
,
12448 tree args
, tsubst_flags_t complain
, tree in_decl
,
12449 bool integral_constant_expression_p
)
12451 #define RECUR(NODE) \
12452 tsubst_expr ((NODE), args, complain, in_decl, \
12453 integral_constant_expression_p)
12454 tree decl
, init
, cond
, incr
;
12457 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12458 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12459 decl
= TREE_OPERAND (init
, 0);
12460 init
= TREE_OPERAND (init
, 1);
12461 /* Do this before substituting into decl to handle 'auto'. */
12462 init_decl
= (init
&& TREE_CODE (init
) == DECL_EXPR
);
12463 init
= RECUR (init
);
12464 decl
= RECUR (decl
);
12467 gcc_assert (!processing_template_decl
);
12468 init
= DECL_INITIAL (decl
);
12469 DECL_INITIAL (decl
) = NULL_TREE
;
12472 gcc_assert (!type_dependent_expression_p (decl
));
12474 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12476 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12477 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12478 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12479 incr
= build_x_modify_expr (EXPR_LOCATION (incr
),
12480 RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12481 RECUR (TREE_OPERAND (incr
, 1)),
12484 incr
= RECUR (incr
);
12485 TREE_VEC_ELT (declv
, i
) = decl
;
12486 TREE_VEC_ELT (initv
, i
) = init
;
12487 TREE_VEC_ELT (condv
, i
) = cond
;
12488 TREE_VEC_ELT (incrv
, i
) = incr
;
12492 if (init
&& !init_decl
)
12495 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12497 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12498 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12499 && OMP_CLAUSE_DECL (c
) == decl
)
12501 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12502 && OMP_CLAUSE_DECL (c
) == decl
)
12503 error ("iteration variable %qD should not be firstprivate", decl
);
12504 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12505 && OMP_CLAUSE_DECL (c
) == decl
)
12506 error ("iteration variable %qD should not be reduction", decl
);
12510 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12511 OMP_CLAUSE_DECL (c
) = decl
;
12512 c
= finish_omp_clauses (c
);
12515 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12520 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12521 if (COMPARISON_CLASS_P (cond
))
12522 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12523 RECUR (TREE_OPERAND (cond
, 0)),
12524 RECUR (TREE_OPERAND (cond
, 1)));
12526 cond
= RECUR (cond
);
12527 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12528 switch (TREE_CODE (incr
))
12530 case PREINCREMENT_EXPR
:
12531 case PREDECREMENT_EXPR
:
12532 case POSTINCREMENT_EXPR
:
12533 case POSTDECREMENT_EXPR
:
12534 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12535 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12538 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12539 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12541 tree rhs
= TREE_OPERAND (incr
, 1);
12542 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12543 RECUR (TREE_OPERAND (incr
, 0)),
12544 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12545 RECUR (TREE_OPERAND (rhs
, 0)),
12546 RECUR (TREE_OPERAND (rhs
, 1))));
12549 incr
= RECUR (incr
);
12552 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12553 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12555 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12556 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12557 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12558 TREE_TYPE (decl
), lhs
,
12559 RECUR (TREE_OPERAND (incr
, 2))));
12561 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12562 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12563 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12565 tree rhs
= TREE_OPERAND (incr
, 2);
12566 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12567 RECUR (TREE_OPERAND (incr
, 0)),
12568 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12569 RECUR (TREE_OPERAND (rhs
, 0)),
12570 RECUR (TREE_OPERAND (rhs
, 1))));
12573 incr
= RECUR (incr
);
12576 incr
= RECUR (incr
);
12580 TREE_VEC_ELT (declv
, i
) = decl
;
12581 TREE_VEC_ELT (initv
, i
) = init
;
12582 TREE_VEC_ELT (condv
, i
) = cond
;
12583 TREE_VEC_ELT (incrv
, i
) = incr
;
12587 /* Like tsubst_copy for expressions, etc. but also does semantic
12591 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12592 bool integral_constant_expression_p
)
12594 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12595 #define RECUR(NODE) \
12596 tsubst_expr ((NODE), args, complain, in_decl, \
12597 integral_constant_expression_p)
12603 if (t
== NULL_TREE
|| t
== error_mark_node
)
12606 loc
= input_location
;
12607 if (EXPR_HAS_LOCATION (t
))
12608 input_location
= EXPR_LOCATION (t
);
12609 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12610 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12612 switch (TREE_CODE (t
))
12614 case STATEMENT_LIST
:
12616 tree_stmt_iterator i
;
12617 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12618 RECUR (tsi_stmt (i
));
12622 case CTOR_INITIALIZER
:
12623 finish_mem_initializers (tsubst_initializer_list
12624 (TREE_OPERAND (t
, 0), args
));
12628 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12632 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12633 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12634 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12636 finish_expr_stmt (tmp
);
12640 do_using_directive (USING_STMT_NAMESPACE (t
));
12645 tree decl
, pattern_decl
;
12648 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12649 if (TREE_CODE (decl
) == LABEL_DECL
)
12650 finish_label_decl (DECL_NAME (decl
));
12651 else if (TREE_CODE (decl
) == USING_DECL
)
12653 tree scope
= USING_DECL_SCOPE (decl
);
12654 tree name
= DECL_NAME (decl
);
12657 scope
= tsubst (scope
, args
, complain
, in_decl
);
12658 decl
= lookup_qualified_name (scope
, name
,
12659 /*is_type_p=*/false,
12660 /*complain=*/false);
12661 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12662 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12664 do_local_using_decl (decl
, scope
, name
);
12668 init
= DECL_INITIAL (decl
);
12669 decl
= tsubst (decl
, args
, complain
, in_decl
);
12670 if (decl
!= error_mark_node
)
12672 /* By marking the declaration as instantiated, we avoid
12673 trying to instantiate it. Since instantiate_decl can't
12674 handle local variables, and since we've already done
12675 all that needs to be done, that's the right thing to
12677 if (TREE_CODE (decl
) == VAR_DECL
)
12678 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12679 if (TREE_CODE (decl
) == VAR_DECL
12680 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12681 /* Anonymous aggregates are a special case. */
12682 finish_anon_union (decl
);
12683 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12685 DECL_CONTEXT (decl
) = current_function_decl
;
12686 insert_capture_proxy (decl
);
12688 else if (DECL_IMPLICIT_TYPEDEF_P (t
))
12689 /* We already did a pushtag. */;
12692 int const_init
= false;
12693 maybe_push_decl (decl
);
12694 if (TREE_CODE (decl
) == VAR_DECL
12695 && DECL_PRETTY_FUNCTION_P (decl
))
12697 /* For __PRETTY_FUNCTION__ we have to adjust the
12699 const char *const name
12700 = cxx_printable_name (current_function_decl
, 2);
12701 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12705 tree t
= RECUR (init
);
12709 /* If we had an initializer but it
12710 instantiated to nothing,
12711 value-initialize the object. This will
12712 only occur when the initializer was a
12713 pack expansion where the parameter packs
12714 used in that expansion were of length
12716 init
= build_value_init (TREE_TYPE (decl
),
12718 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12719 init
= get_target_expr_sfinae (init
, complain
);
12725 if (TREE_CODE (decl
) == VAR_DECL
)
12726 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12728 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12737 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12738 RECUR (FOR_INIT_STMT (t
));
12739 finish_for_init_stmt (stmt
);
12740 tmp
= RECUR (FOR_COND (t
));
12741 finish_for_cond (tmp
, stmt
);
12742 tmp
= RECUR (FOR_EXPR (t
));
12743 finish_for_expr (tmp
, stmt
);
12744 RECUR (FOR_BODY (t
));
12745 finish_for_stmt (stmt
);
12748 case RANGE_FOR_STMT
:
12751 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12752 decl
= RANGE_FOR_DECL (t
);
12753 decl
= tsubst (decl
, args
, complain
, in_decl
);
12754 maybe_push_decl (decl
);
12755 expr
= RECUR (RANGE_FOR_EXPR (t
));
12756 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12757 RECUR (RANGE_FOR_BODY (t
));
12758 finish_for_stmt (stmt
);
12763 stmt
= begin_while_stmt ();
12764 tmp
= RECUR (WHILE_COND (t
));
12765 finish_while_stmt_cond (tmp
, stmt
);
12766 RECUR (WHILE_BODY (t
));
12767 finish_while_stmt (stmt
);
12771 stmt
= begin_do_stmt ();
12772 RECUR (DO_BODY (t
));
12773 finish_do_body (stmt
);
12774 tmp
= RECUR (DO_COND (t
));
12775 finish_do_stmt (tmp
, stmt
);
12779 stmt
= begin_if_stmt ();
12780 tmp
= RECUR (IF_COND (t
));
12781 finish_if_stmt_cond (tmp
, stmt
);
12782 RECUR (THEN_CLAUSE (t
));
12783 finish_then_clause (stmt
);
12785 if (ELSE_CLAUSE (t
))
12787 begin_else_clause (stmt
);
12788 RECUR (ELSE_CLAUSE (t
));
12789 finish_else_clause (stmt
);
12792 finish_if_stmt (stmt
);
12796 if (BIND_EXPR_BODY_BLOCK (t
))
12797 stmt
= begin_function_body ();
12799 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12800 ? BCS_TRY_BLOCK
: 0);
12802 RECUR (BIND_EXPR_BODY (t
));
12804 if (BIND_EXPR_BODY_BLOCK (t
))
12805 finish_function_body (stmt
);
12807 finish_compound_stmt (stmt
);
12811 finish_break_stmt ();
12814 case CONTINUE_STMT
:
12815 finish_continue_stmt ();
12819 stmt
= begin_switch_stmt ();
12820 tmp
= RECUR (SWITCH_STMT_COND (t
));
12821 finish_switch_cond (tmp
, stmt
);
12822 RECUR (SWITCH_STMT_BODY (t
));
12823 finish_switch_stmt (stmt
);
12826 case CASE_LABEL_EXPR
:
12827 finish_case_label (EXPR_LOCATION (t
),
12828 RECUR (CASE_LOW (t
)),
12829 RECUR (CASE_HIGH (t
)));
12834 tree decl
= LABEL_EXPR_LABEL (t
);
12837 label
= finish_label_stmt (DECL_NAME (decl
));
12838 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
12839 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
12844 tmp
= GOTO_DESTINATION (t
);
12845 if (TREE_CODE (tmp
) != LABEL_DECL
)
12846 /* Computed goto's must be tsubst'd into. On the other hand,
12847 non-computed gotos must not be; the identifier in question
12848 will have no binding. */
12851 tmp
= DECL_NAME (tmp
);
12852 finish_goto_stmt (tmp
);
12856 tmp
= finish_asm_stmt
12857 (ASM_VOLATILE_P (t
),
12858 RECUR (ASM_STRING (t
)),
12859 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
12860 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
12861 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
12862 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
12864 tree asm_expr
= tmp
;
12865 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
12866 asm_expr
= TREE_OPERAND (asm_expr
, 0);
12867 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
12874 stmt
= begin_try_block ();
12875 RECUR (TRY_STMTS (t
));
12876 finish_cleanup_try_block (stmt
);
12877 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
12881 tree compound_stmt
= NULL_TREE
;
12883 if (FN_TRY_BLOCK_P (t
))
12884 stmt
= begin_function_try_block (&compound_stmt
);
12886 stmt
= begin_try_block ();
12888 RECUR (TRY_STMTS (t
));
12890 if (FN_TRY_BLOCK_P (t
))
12891 finish_function_try_block (stmt
);
12893 finish_try_block (stmt
);
12895 RECUR (TRY_HANDLERS (t
));
12896 if (FN_TRY_BLOCK_P (t
))
12897 finish_function_handler_sequence (stmt
, compound_stmt
);
12899 finish_handler_sequence (stmt
);
12905 tree decl
= HANDLER_PARMS (t
);
12909 decl
= tsubst (decl
, args
, complain
, in_decl
);
12910 /* Prevent instantiate_decl from trying to instantiate
12911 this variable. We've already done all that needs to be
12913 if (decl
!= error_mark_node
)
12914 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12916 stmt
= begin_handler ();
12917 finish_handler_parms (decl
, stmt
);
12918 RECUR (HANDLER_BODY (t
));
12919 finish_handler (stmt
);
12924 tmp
= tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
12925 if (CLASS_TYPE_P (tmp
))
12927 /* Local classes are not independent templates; they are
12928 instantiated along with their containing function. And this
12929 way we don't have to deal with pushing out of one local class
12930 to instantiate a member of another local class. */
12932 /* Closures are handled by the LAMBDA_EXPR. */
12933 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t
)));
12934 complete_type (tmp
);
12935 for (fn
= TYPE_METHODS (tmp
); fn
; fn
= DECL_CHAIN (fn
))
12936 if (!DECL_ARTIFICIAL (fn
))
12937 instantiate_decl (fn
, /*defer_ok*/0, /*expl_inst_class*/false);
12941 case STATIC_ASSERT
:
12945 ++c_inhibit_evaluation_warnings
;
12947 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
12950 /*integral_constant_expression_p=*/true);
12951 --c_inhibit_evaluation_warnings
;
12953 finish_static_assert (condition
,
12954 STATIC_ASSERT_MESSAGE (t
),
12955 STATIC_ASSERT_SOURCE_LOCATION (t
),
12956 /*member_p=*/false);
12961 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
12962 args
, complain
, in_decl
);
12963 stmt
= begin_omp_parallel ();
12964 RECUR (OMP_PARALLEL_BODY (t
));
12965 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
12966 = OMP_PARALLEL_COMBINED (t
);
12970 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
12971 args
, complain
, in_decl
);
12972 stmt
= begin_omp_task ();
12973 RECUR (OMP_TASK_BODY (t
));
12974 finish_omp_task (tmp
, stmt
);
12979 tree clauses
, body
, pre_body
;
12980 tree declv
, initv
, condv
, incrv
;
12983 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
12984 args
, complain
, in_decl
);
12985 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12986 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12987 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12988 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
12990 stmt
= begin_omp_structured_block ();
12992 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
12993 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
12994 &clauses
, args
, complain
, in_decl
,
12995 integral_constant_expression_p
);
12997 pre_body
= push_stmt_list ();
12998 RECUR (OMP_FOR_PRE_BODY (t
));
12999 pre_body
= pop_stmt_list (pre_body
);
13001 body
= push_stmt_list ();
13002 RECUR (OMP_FOR_BODY (t
));
13003 body
= pop_stmt_list (body
);
13005 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13006 body
, pre_body
, clauses
);
13008 add_stmt (finish_omp_structured_block (stmt
));
13014 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13015 stmt
= push_stmt_list ();
13016 RECUR (OMP_BODY (t
));
13017 stmt
= pop_stmt_list (stmt
);
13020 OMP_BODY (t
) = stmt
;
13021 OMP_CLAUSES (t
) = tmp
;
13029 stmt
= push_stmt_list ();
13030 RECUR (OMP_BODY (t
));
13031 stmt
= pop_stmt_list (stmt
);
13034 OMP_BODY (t
) = stmt
;
13039 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13040 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13042 tree op1
= TREE_OPERAND (t
, 1);
13043 tree rhs1
= NULL_TREE
;
13045 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13047 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13048 op1
= TREE_OPERAND (op1
, 1);
13050 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13051 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13052 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13053 NULL_TREE
, NULL_TREE
, rhs1
);
13057 tree op1
= TREE_OPERAND (t
, 1);
13058 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13059 tree rhs1
= NULL_TREE
;
13060 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13061 enum tree_code opcode
= NOP_EXPR
;
13062 if (code
== OMP_ATOMIC_READ
)
13064 v
= RECUR (TREE_OPERAND (op1
, 0));
13065 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13067 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13068 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13070 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13071 v
= RECUR (TREE_OPERAND (op1
, 0));
13072 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13073 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13075 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13076 op11
= TREE_OPERAND (op11
, 1);
13078 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13079 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13080 opcode
= TREE_CODE (op11
);
13085 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13086 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13088 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13092 case TRANSACTION_EXPR
:
13095 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13096 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13098 if (TRANSACTION_EXPR_IS_STMT (t
))
13100 tree body
= TRANSACTION_EXPR_BODY (t
);
13101 tree noex
= NULL_TREE
;
13102 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13104 noex
= MUST_NOT_THROW_COND (body
);
13105 if (noex
== NULL_TREE
)
13106 noex
= boolean_true_node
;
13107 body
= TREE_OPERAND (body
, 0);
13109 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13111 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13115 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13116 RECUR (TRANSACTION_EXPR_BODY (t
)),
13123 case MUST_NOT_THROW_EXPR
:
13124 RETURN (build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13125 RECUR (MUST_NOT_THROW_COND (t
))));
13127 case EXPR_PACK_EXPANSION
:
13128 error ("invalid use of pack expansion expression");
13129 RETURN (error_mark_node
);
13131 case NONTYPE_ARGUMENT_PACK
:
13132 error ("use %<...%> to expand argument pack");
13133 RETURN (error_mark_node
);
13135 case COMPOUND_EXPR
:
13136 tmp
= RECUR (TREE_OPERAND (t
, 0));
13137 if (tmp
== NULL_TREE
)
13138 /* If the first operand was a statement, we're done with it. */
13139 RETURN (RECUR (TREE_OPERAND (t
, 1)));
13140 RETURN (build_x_compound_expr (EXPR_LOCATION (t
), tmp
,
13141 RECUR (TREE_OPERAND (t
, 1)),
13145 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13147 RETURN (tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13148 /*function_p=*/false,
13149 integral_constant_expression_p
));
13152 RETURN (NULL_TREE
);
13154 input_location
= loc
;
13160 /* T is a postfix-expression that is not being used in a function
13161 call. Return the substituted version of T. */
13164 tsubst_non_call_postfix_expression (tree t
, tree args
,
13165 tsubst_flags_t complain
,
13168 if (TREE_CODE (t
) == SCOPE_REF
)
13169 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13170 /*done=*/false, /*address_p=*/false);
13172 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13173 /*function_p=*/false,
13174 /*integral_constant_expression_p=*/false);
13179 /* Like tsubst but deals with expressions and performs semantic
13180 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13183 tsubst_copy_and_build (tree t
,
13185 tsubst_flags_t complain
,
13188 bool integral_constant_expression_p
)
13190 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
13191 #define RECUR(NODE) \
13192 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13193 /*function_p=*/false, \
13194 integral_constant_expression_p)
13199 if (t
== NULL_TREE
|| t
== error_mark_node
)
13202 loc
= input_location
;
13203 if (EXPR_HAS_LOCATION (t
))
13204 input_location
= EXPR_LOCATION (t
);
13206 switch (TREE_CODE (t
))
13210 /* Fall through. */
13211 case IDENTIFIER_NODE
:
13215 bool non_integral_constant_expression_p
;
13216 const char *error_msg
;
13218 if (IDENTIFIER_TYPENAME_P (t
))
13220 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13221 t
= mangle_conv_op_name_for_type (new_type
);
13224 /* Look up the name. */
13225 decl
= lookup_name (t
);
13227 /* By convention, expressions use ERROR_MARK_NODE to indicate
13228 failure, not NULL_TREE. */
13229 if (decl
== NULL_TREE
)
13230 decl
= error_mark_node
;
13232 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13234 integral_constant_expression_p
,
13235 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13236 &non_integral_constant_expression_p
,
13237 /*template_p=*/false,
13239 /*address_p=*/false,
13240 /*template_arg_p=*/false,
13245 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13247 if (complain
& tf_error
)
13248 unqualified_name_lookup_error (decl
);
13249 decl
= error_mark_node
;
13254 case TEMPLATE_ID_EXPR
:
13257 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13258 tree targs
= TREE_OPERAND (t
, 1);
13261 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13263 if (TREE_CODE (templ
) == COMPONENT_REF
)
13265 object
= TREE_OPERAND (templ
, 0);
13266 templ
= TREE_OPERAND (templ
, 1);
13269 object
= NULL_TREE
;
13270 templ
= lookup_template_function (templ
, targs
);
13273 RETURN (build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13274 object
, templ
, NULL_TREE
));
13276 RETURN (baselink_for_fns (templ
));
13281 tree r
= RECUR (TREE_OPERAND (t
, 0));
13283 if (REFERENCE_REF_P (t
))
13285 /* A type conversion to reference type will be enclosed in
13286 such an indirect ref, but the substitution of the cast
13287 will have also added such an indirect ref. */
13288 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13289 r
= convert_from_reference (r
);
13292 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
, complain
);
13298 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13299 RECUR (TREE_OPERAND (t
, 0))));
13301 case IMPLICIT_CONV_EXPR
:
13303 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13304 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13305 int flags
= LOOKUP_IMPLICIT
;
13306 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13307 flags
= LOOKUP_NORMAL
;
13308 RETURN (perform_implicit_conversion_flags (type
, expr
, complain
,
13315 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13316 RECUR (TREE_OPERAND (t
, 0))));
13319 case REINTERPRET_CAST_EXPR
:
13320 case CONST_CAST_EXPR
:
13321 case DYNAMIC_CAST_EXPR
:
13322 case STATIC_CAST_EXPR
:
13325 tree op
, r
= NULL_TREE
;
13327 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13328 if (integral_constant_expression_p
13329 && !cast_valid_in_integral_constant_expression_p (type
))
13331 if (complain
& tf_error
)
13332 error ("a cast to a type other than an integral or "
13333 "enumeration type cannot appear in a constant-expression");
13334 RETURN (error_mark_node
);
13337 op
= RECUR (TREE_OPERAND (t
, 0));
13339 ++c_inhibit_evaluation_warnings
;
13340 switch (TREE_CODE (t
))
13343 r
= build_functional_cast (type
, op
, complain
);
13345 case REINTERPRET_CAST_EXPR
:
13346 r
= build_reinterpret_cast (type
, op
, complain
);
13348 case CONST_CAST_EXPR
:
13349 r
= build_const_cast (type
, op
, complain
);
13351 case DYNAMIC_CAST_EXPR
:
13352 r
= build_dynamic_cast (type
, op
, complain
);
13354 case STATIC_CAST_EXPR
:
13355 r
= build_static_cast (type
, op
, complain
);
13358 gcc_unreachable ();
13360 --c_inhibit_evaluation_warnings
;
13365 case POSTDECREMENT_EXPR
:
13366 case POSTINCREMENT_EXPR
:
13367 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13368 args
, complain
, in_decl
);
13369 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
), op1
, complain
));
13371 case PREDECREMENT_EXPR
:
13372 case PREINCREMENT_EXPR
:
13376 case TRUTH_NOT_EXPR
:
13377 case UNARY_PLUS_EXPR
: /* Unary + */
13378 case REALPART_EXPR
:
13379 case IMAGPART_EXPR
:
13380 RETURN (build_x_unary_op (input_location
, TREE_CODE (t
),
13381 RECUR (TREE_OPERAND (t
, 0)), complain
));
13383 case FIX_TRUNC_EXPR
:
13384 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13388 op1
= TREE_OPERAND (t
, 0);
13389 if (TREE_CODE (op1
) == LABEL_DECL
)
13390 RETURN (finish_label_address_expr (DECL_NAME (op1
),
13391 EXPR_LOCATION (op1
)));
13392 if (TREE_CODE (op1
) == SCOPE_REF
)
13393 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13394 /*done=*/true, /*address_p=*/true);
13396 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13398 RETURN (build_x_unary_op (input_location
, ADDR_EXPR
, op1
, complain
));
13403 case TRUNC_DIV_EXPR
:
13404 case CEIL_DIV_EXPR
:
13405 case FLOOR_DIV_EXPR
:
13406 case ROUND_DIV_EXPR
:
13407 case EXACT_DIV_EXPR
:
13411 case TRUNC_MOD_EXPR
:
13412 case FLOOR_MOD_EXPR
:
13413 case TRUTH_ANDIF_EXPR
:
13414 case TRUTH_ORIF_EXPR
:
13415 case TRUTH_AND_EXPR
:
13416 case TRUTH_OR_EXPR
:
13434 ++c_inhibit_evaluation_warnings
;
13436 r
= build_x_binary_op
13437 (input_location
, TREE_CODE (t
),
13438 RECUR (TREE_OPERAND (t
, 0)),
13439 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13441 : TREE_CODE (TREE_OPERAND (t
, 0))),
13442 RECUR (TREE_OPERAND (t
, 1)),
13443 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13445 : TREE_CODE (TREE_OPERAND (t
, 1))),
13448 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13449 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13451 --c_inhibit_evaluation_warnings
;
13457 RETURN (tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13458 /*address_p=*/false));
13460 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13461 args
, complain
, in_decl
);
13462 RETURN (build_x_array_ref (EXPR_LOCATION (t
), op1
,
13463 RECUR (TREE_OPERAND (t
, 1)), complain
));
13466 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13467 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
13471 op1
= TREE_OPERAND (t
, 0);
13474 /* When there are no ARGS, we are trying to evaluate a
13475 non-dependent expression from the parser. Trying to do
13476 the substitutions may not work. */
13478 op1
= TREE_TYPE (op1
);
13482 ++cp_unevaluated_operand
;
13483 ++c_inhibit_evaluation_warnings
;
13484 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13485 /*function_p=*/false,
13486 /*integral_constant_expression_p=*/false);
13487 --cp_unevaluated_operand
;
13488 --c_inhibit_evaluation_warnings
;
13491 RETURN (cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13492 complain
& tf_error
));
13494 RETURN (cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13495 complain
& tf_error
));
13497 case AT_ENCODE_EXPR
:
13499 op1
= TREE_OPERAND (t
, 0);
13500 ++cp_unevaluated_operand
;
13501 ++c_inhibit_evaluation_warnings
;
13502 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13503 /*function_p=*/false,
13504 /*integral_constant_expression_p=*/false);
13505 --cp_unevaluated_operand
;
13506 --c_inhibit_evaluation_warnings
;
13507 RETURN (objc_build_encode_expr (op1
));
13510 case NOEXCEPT_EXPR
:
13511 op1
= TREE_OPERAND (t
, 0);
13512 ++cp_unevaluated_operand
;
13513 ++c_inhibit_evaluation_warnings
;
13514 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13515 /*function_p=*/false,
13516 /*integral_constant_expression_p=*/false);
13517 --cp_unevaluated_operand
;
13518 --c_inhibit_evaluation_warnings
;
13519 RETURN (finish_noexcept_expr (op1
, complain
));
13523 tree r
= build_x_modify_expr
13524 (EXPR_LOCATION (t
),
13525 RECUR (TREE_OPERAND (t
, 0)),
13526 TREE_CODE (TREE_OPERAND (t
, 1)),
13527 RECUR (TREE_OPERAND (t
, 2)),
13529 /* TREE_NO_WARNING must be set if either the expression was
13530 parenthesized or it uses an operator such as >>= rather
13531 than plain assignment. In the former case, it was already
13532 set and must be copied. In the latter case,
13533 build_x_modify_expr sets it and it must not be reset
13535 if (TREE_NO_WARNING (t
))
13536 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13541 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13542 args
, complain
, in_decl
);
13543 /* Remember that there was a reference to this entity. */
13546 RETURN (build_x_arrow (input_location
, op1
, complain
));
13550 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13551 tree init
= RECUR (TREE_OPERAND (t
, 3));
13552 VEC(tree
,gc
) *placement_vec
;
13553 VEC(tree
,gc
) *init_vec
;
13556 if (placement
== NULL_TREE
)
13557 placement_vec
= NULL
;
13560 placement_vec
= make_tree_vector ();
13561 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13562 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
13565 /* If there was an initializer in the original tree, but it
13566 instantiated to an empty list, then we should pass a
13567 non-NULL empty vector to tell build_new that it was an
13568 empty initializer() rather than no initializer. This can
13569 only happen when the initializer is a pack expansion whose
13570 parameter packs are of length zero. */
13571 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13575 init_vec
= make_tree_vector ();
13576 if (init
== void_zero_node
)
13577 gcc_assert (init_vec
!= NULL
);
13580 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13581 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
13585 ret
= build_new (&placement_vec
,
13586 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13587 RECUR (TREE_OPERAND (t
, 2)),
13589 NEW_EXPR_USE_GLOBAL (t
),
13592 if (placement_vec
!= NULL
)
13593 release_tree_vector (placement_vec
);
13594 if (init_vec
!= NULL
)
13595 release_tree_vector (init_vec
);
13601 RETURN (delete_sanity
13602 (RECUR (TREE_OPERAND (t
, 0)),
13603 RECUR (TREE_OPERAND (t
, 1)),
13604 DELETE_EXPR_USE_VEC (t
),
13605 DELETE_EXPR_USE_GLOBAL (t
),
13608 case COMPOUND_EXPR
:
13609 RETURN (build_x_compound_expr (EXPR_LOCATION (t
),
13610 RECUR (TREE_OPERAND (t
, 0)),
13611 RECUR (TREE_OPERAND (t
, 1)),
13617 VEC(tree
,gc
) *call_args
;
13618 unsigned int nargs
, i
;
13623 function
= CALL_EXPR_FN (t
);
13624 /* When we parsed the expression, we determined whether or
13625 not Koenig lookup should be performed. */
13626 koenig_p
= KOENIG_LOOKUP_P (t
);
13627 if (TREE_CODE (function
) == SCOPE_REF
)
13629 qualified_p
= true;
13630 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13632 /*address_p=*/false);
13634 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13636 /* Do nothing; calling tsubst_copy_and_build on an identifier
13637 would incorrectly perform unqualified lookup again.
13639 Note that we can also have an IDENTIFIER_NODE if the earlier
13640 unqualified lookup found a member function; in that case
13641 koenig_p will be false and we do want to do the lookup
13642 again to find the instantiated member function.
13644 FIXME but doing that causes c++/15272, so we need to stop
13645 using IDENTIFIER_NODE in that situation. */
13646 qualified_p
= false;
13650 if (TREE_CODE (function
) == COMPONENT_REF
)
13652 tree op
= TREE_OPERAND (function
, 1);
13654 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13655 || (BASELINK_P (op
)
13656 && BASELINK_QUALIFIED_P (op
)));
13659 qualified_p
= false;
13661 function
= tsubst_copy_and_build (function
, args
, complain
,
13664 integral_constant_expression_p
);
13666 if (BASELINK_P (function
))
13667 qualified_p
= true;
13670 nargs
= call_expr_nargs (t
);
13671 call_args
= make_tree_vector ();
13672 for (i
= 0; i
< nargs
; ++i
)
13674 tree arg
= CALL_EXPR_ARG (t
, i
);
13676 if (!PACK_EXPANSION_P (arg
))
13677 VEC_safe_push (tree
, gc
, call_args
,
13678 RECUR (CALL_EXPR_ARG (t
, i
)));
13681 /* Expand the pack expansion and push each entry onto
13683 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13684 if (TREE_CODE (arg
) == TREE_VEC
)
13686 unsigned int len
, j
;
13688 len
= TREE_VEC_LENGTH (arg
);
13689 for (j
= 0; j
< len
; ++j
)
13691 tree value
= TREE_VEC_ELT (arg
, j
);
13692 if (value
!= NULL_TREE
)
13693 value
= convert_from_reference (value
);
13694 VEC_safe_push (tree
, gc
, call_args
, value
);
13699 /* A partial substitution. Add one entry. */
13700 VEC_safe_push (tree
, gc
, call_args
, arg
);
13705 /* We do not perform argument-dependent lookup if normal
13706 lookup finds a non-function, in accordance with the
13707 expected resolution of DR 218. */
13709 && ((is_overloaded_fn (function
)
13710 /* If lookup found a member function, the Koenig lookup is
13711 not appropriate, even if an unqualified-name was used
13712 to denote the function. */
13713 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13714 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13715 /* Only do this when substitution turns a dependent call
13716 into a non-dependent call. */
13717 && type_dependent_expression_p_push (t
)
13718 && !any_type_dependent_arguments_p (call_args
))
13719 function
= perform_koenig_lookup (function
, call_args
, false,
13722 if (TREE_CODE (function
) == IDENTIFIER_NODE
13723 && !any_type_dependent_arguments_p (call_args
))
13725 if (koenig_p
&& (complain
& tf_warning_or_error
))
13727 /* For backwards compatibility and good diagnostics, try
13728 the unqualified lookup again if we aren't in SFINAE
13730 tree unq
= (tsubst_copy_and_build
13731 (function
, args
, complain
, in_decl
, true,
13732 integral_constant_expression_p
));
13733 if (unq
== error_mark_node
)
13734 RETURN (error_mark_node
);
13736 if (unq
!= function
)
13739 if (TREE_CODE (fn
) == INDIRECT_REF
)
13740 fn
= TREE_OPERAND (fn
, 0);
13741 if (TREE_CODE (fn
) == COMPONENT_REF
)
13742 fn
= TREE_OPERAND (fn
, 1);
13743 if (is_overloaded_fn (fn
))
13744 fn
= get_first_fn (fn
);
13745 permerror (EXPR_LOC_OR_HERE (t
),
13746 "%qD was not declared in this scope, "
13747 "and no declarations were found by "
13748 "argument-dependent lookup at the point "
13749 "of instantiation", function
);
13751 /* Can't say anything more. */;
13752 else if (DECL_CLASS_SCOPE_P (fn
))
13754 inform (EXPR_LOC_OR_HERE (t
),
13755 "declarations in dependent base %qT are "
13756 "not found by unqualified lookup",
13757 DECL_CLASS_CONTEXT (fn
));
13758 if (current_class_ptr
)
13759 inform (EXPR_LOC_OR_HERE (t
),
13760 "use %<this->%D%> instead", function
);
13762 inform (EXPR_LOC_OR_HERE (t
),
13763 "use %<%T::%D%> instead",
13764 current_class_name
, function
);
13767 inform (0, "%q+D declared here, later in the "
13768 "translation unit", fn
);
13772 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13774 unqualified_name_lookup_error (function
);
13775 release_tree_vector (call_args
);
13776 RETURN (error_mark_node
);
13780 /* Remember that there was a reference to this entity. */
13781 if (DECL_P (function
))
13782 mark_used (function
);
13784 if (TREE_CODE (function
) == OFFSET_REF
)
13785 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
13786 else if (TREE_CODE (function
) == COMPONENT_REF
)
13788 tree instance
= TREE_OPERAND (function
, 0);
13789 tree fn
= TREE_OPERAND (function
, 1);
13791 if (processing_template_decl
13792 && (type_dependent_expression_p (instance
)
13793 || (!BASELINK_P (fn
)
13794 && TREE_CODE (fn
) != FIELD_DECL
)
13795 || type_dependent_expression_p (fn
)
13796 || any_type_dependent_arguments_p (call_args
)))
13797 ret
= build_nt_call_vec (function
, call_args
);
13798 else if (!BASELINK_P (fn
))
13799 ret
= finish_call_expr (function
, &call_args
,
13800 /*disallow_virtual=*/false,
13801 /*koenig_p=*/false,
13804 ret
= (build_new_method_call
13806 &call_args
, NULL_TREE
,
13807 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
13812 ret
= finish_call_expr (function
, &call_args
,
13813 /*disallow_virtual=*/qualified_p
,
13817 release_tree_vector (call_args
);
13824 tree cond
= RECUR (TREE_OPERAND (t
, 0));
13827 if (TREE_CODE (cond
) == INTEGER_CST
)
13829 if (integer_zerop (cond
))
13831 ++c_inhibit_evaluation_warnings
;
13832 exp1
= RECUR (TREE_OPERAND (t
, 1));
13833 --c_inhibit_evaluation_warnings
;
13834 exp2
= RECUR (TREE_OPERAND (t
, 2));
13838 exp1
= RECUR (TREE_OPERAND (t
, 1));
13839 ++c_inhibit_evaluation_warnings
;
13840 exp2
= RECUR (TREE_OPERAND (t
, 2));
13841 --c_inhibit_evaluation_warnings
;
13846 exp1
= RECUR (TREE_OPERAND (t
, 1));
13847 exp2
= RECUR (TREE_OPERAND (t
, 2));
13850 RETURN (build_x_conditional_expr (EXPR_LOCATION (t
),
13851 cond
, exp1
, exp2
, complain
));
13854 case PSEUDO_DTOR_EXPR
:
13855 RETURN (finish_pseudo_destructor_expr
13856 (RECUR (TREE_OPERAND (t
, 0)),
13857 RECUR (TREE_OPERAND (t
, 1)),
13858 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
)));
13862 tree purpose
, value
, chain
;
13864 if (t
== void_list_node
)
13867 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
13868 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
13870 /* We have pack expansions, so expand those and
13871 create a new list out of it. */
13872 tree purposevec
= NULL_TREE
;
13873 tree valuevec
= NULL_TREE
;
13877 /* Expand the argument expressions. */
13878 if (TREE_PURPOSE (t
))
13879 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
13880 complain
, in_decl
);
13881 if (TREE_VALUE (t
))
13882 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
13883 complain
, in_decl
);
13885 /* Build the rest of the list. */
13886 chain
= TREE_CHAIN (t
);
13887 if (chain
&& chain
!= void_type_node
)
13888 chain
= RECUR (chain
);
13890 /* Determine the number of arguments. */
13891 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
13893 len
= TREE_VEC_LENGTH (purposevec
);
13894 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
13896 else if (TREE_CODE (valuevec
) == TREE_VEC
)
13897 len
= TREE_VEC_LENGTH (valuevec
);
13900 /* Since we only performed a partial substitution into
13901 the argument pack, we only RETURN (a single list
13903 if (purposevec
== TREE_PURPOSE (t
)
13904 && valuevec
== TREE_VALUE (t
)
13905 && chain
== TREE_CHAIN (t
))
13908 RETURN (tree_cons (purposevec
, valuevec
, chain
));
13911 /* Convert the argument vectors into a TREE_LIST */
13915 /* Grab the Ith values. */
13917 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
13920 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
13923 /* Build the list (backwards). */
13924 chain
= tree_cons (purpose
, value
, chain
);
13930 purpose
= TREE_PURPOSE (t
);
13932 purpose
= RECUR (purpose
);
13933 value
= TREE_VALUE (t
);
13935 value
= RECUR (value
);
13936 chain
= TREE_CHAIN (t
);
13937 if (chain
&& chain
!= void_type_node
)
13938 chain
= RECUR (chain
);
13939 if (purpose
== TREE_PURPOSE (t
)
13940 && value
== TREE_VALUE (t
)
13941 && chain
== TREE_CHAIN (t
))
13943 RETURN (tree_cons (purpose
, value
, chain
));
13946 case COMPONENT_REF
:
13952 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13953 args
, complain
, in_decl
);
13954 /* Remember that there was a reference to this entity. */
13955 if (DECL_P (object
))
13956 mark_used (object
);
13957 object_type
= TREE_TYPE (object
);
13959 member
= TREE_OPERAND (t
, 1);
13960 if (BASELINK_P (member
))
13961 member
= tsubst_baselink (member
,
13962 non_reference (TREE_TYPE (object
)),
13963 args
, complain
, in_decl
);
13965 member
= tsubst_copy (member
, args
, complain
, in_decl
);
13966 if (member
== error_mark_node
)
13967 RETURN (error_mark_node
);
13969 if (type_dependent_expression_p (object
))
13970 /* We can't do much here. */;
13971 else if (!CLASS_TYPE_P (object_type
))
13973 if (scalarish_type_p (object_type
))
13975 tree s
= NULL_TREE
;
13976 tree dtor
= member
;
13978 if (TREE_CODE (dtor
) == SCOPE_REF
)
13980 s
= TREE_OPERAND (dtor
, 0);
13981 dtor
= TREE_OPERAND (dtor
, 1);
13983 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
13985 dtor
= TREE_OPERAND (dtor
, 0);
13987 RETURN (finish_pseudo_destructor_expr (object
, s
, dtor
));
13991 else if (TREE_CODE (member
) == SCOPE_REF
13992 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
13994 /* Lookup the template functions now that we know what the
13996 tree scope
= TREE_OPERAND (member
, 0);
13997 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
13998 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
13999 member
= lookup_qualified_name (scope
, tmpl
,
14000 /*is_type_p=*/false,
14001 /*complain=*/false);
14002 if (BASELINK_P (member
))
14004 BASELINK_FUNCTIONS (member
)
14005 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14007 member
= (adjust_result_of_qualified_name_lookup
14008 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14013 qualified_name_lookup_error (scope
, tmpl
, member
,
14015 RETURN (error_mark_node
);
14018 else if (TREE_CODE (member
) == SCOPE_REF
14019 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14020 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14022 if (complain
& tf_error
)
14024 if (TYPE_P (TREE_OPERAND (member
, 0)))
14025 error ("%qT is not a class or namespace",
14026 TREE_OPERAND (member
, 0));
14028 error ("%qD is not a class or namespace",
14029 TREE_OPERAND (member
, 0));
14031 RETURN (error_mark_node
);
14033 else if (TREE_CODE (member
) == FIELD_DECL
)
14034 RETURN (finish_non_static_data_member (member
, object
, NULL_TREE
));
14036 RETURN (finish_class_member_access_expr (object
, member
,
14037 /*template_p=*/false,
14042 RETURN (build_throw
14043 (RECUR (TREE_OPERAND (t
, 0))));
14047 VEC(constructor_elt
,gc
) *n
;
14048 constructor_elt
*ce
;
14049 unsigned HOST_WIDE_INT idx
;
14050 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14051 bool process_index_p
;
14053 bool need_copy_p
= false;
14056 if (type
== error_mark_node
)
14057 RETURN (error_mark_node
);
14059 /* digest_init will do the wrong thing if we let it. */
14060 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14063 /* We do not want to process the index of aggregate
14064 initializers as they are identifier nodes which will be
14065 looked up by digest_init. */
14066 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14068 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
14069 newlen
= VEC_length (constructor_elt
, n
);
14070 FOR_EACH_VEC_ELT (constructor_elt
, n
, idx
, ce
)
14072 if (ce
->index
&& process_index_p
)
14073 ce
->index
= RECUR (ce
->index
);
14075 if (PACK_EXPANSION_P (ce
->value
))
14077 /* Substitute into the pack expansion. */
14078 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14081 if (ce
->value
== error_mark_node
14082 || PACK_EXPANSION_P (ce
->value
))
14084 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14085 /* Just move the argument into place. */
14086 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14089 /* Update the length of the final CONSTRUCTOR
14090 arguments vector, and note that we will need to
14092 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14093 need_copy_p
= true;
14097 ce
->value
= RECUR (ce
->value
);
14102 VEC(constructor_elt
,gc
) *old_n
= n
;
14104 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
14105 FOR_EACH_VEC_ELT (constructor_elt
, old_n
, idx
, ce
)
14107 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14109 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14110 for (i
= 0; i
< len
; ++i
)
14111 CONSTRUCTOR_APPEND_ELT (n
, 0,
14112 TREE_VEC_ELT (ce
->value
, i
));
14115 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14119 r
= build_constructor (init_list_type_node
, n
);
14120 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14122 if (TREE_HAS_CONSTRUCTOR (t
))
14123 RETURN (finish_compound_literal (type
, r
, complain
));
14125 TREE_TYPE (r
) = type
;
14131 tree operand_0
= TREE_OPERAND (t
, 0);
14132 if (TYPE_P (operand_0
))
14134 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14135 RETURN (get_typeid (operand_0
));
14139 operand_0
= RECUR (operand_0
);
14140 RETURN (build_typeid (operand_0
));
14151 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14153 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14154 /* If the original type was a reference, we'll be wrapped in
14155 the appropriate INDIRECT_REF. */
14156 r
= convert_from_reference (r
);
14161 RETURN (build_x_va_arg (EXPR_LOCATION (t
),
14162 RECUR (TREE_OPERAND (t
, 0)),
14163 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
)));
14165 case OFFSETOF_EXPR
:
14166 RETURN (finish_offsetof (RECUR (TREE_OPERAND (t
, 0))));
14170 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14171 complain
, in_decl
);
14173 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14175 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14177 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
));
14182 tree old_stmt_expr
= cur_stmt_expr
;
14183 tree stmt_expr
= begin_stmt_expr ();
14185 cur_stmt_expr
= stmt_expr
;
14186 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14187 integral_constant_expression_p
);
14188 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14189 cur_stmt_expr
= old_stmt_expr
;
14191 /* If the resulting list of expression statement is empty,
14192 fold it further into void_zero_node. */
14193 if (empty_expr_stmt_p (stmt_expr
))
14194 stmt_expr
= void_zero_node
;
14196 RETURN (stmt_expr
);
14201 tree r
= build_lambda_expr ();
14203 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14204 LAMBDA_EXPR_CLOSURE (r
) = type
;
14205 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14207 LAMBDA_EXPR_LOCATION (r
)
14208 = LAMBDA_EXPR_LOCATION (t
);
14209 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14210 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14211 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14212 LAMBDA_EXPR_DISCRIMINATOR (r
)
14213 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14214 /* For a function scope, we want to use tsubst so that we don't
14215 complain about referring to an auto function before its return
14216 type has been deduced. Otherwise, we want to use tsubst_copy so
14217 that we look up the existing field/parameter/variable rather
14218 than build a new one. */
14219 tree scope
= LAMBDA_EXPR_EXTRA_SCOPE (t
);
14220 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
14221 scope
= tsubst (LAMBDA_EXPR_EXTRA_SCOPE (t
), args
,
14222 complain
, in_decl
);
14224 scope
= RECUR (scope
);
14225 LAMBDA_EXPR_EXTRA_SCOPE (r
) = scope
;
14226 LAMBDA_EXPR_RETURN_TYPE (r
)
14227 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14229 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14230 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14232 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14233 determine_visibility (TYPE_NAME (type
));
14234 /* Now that we know visibility, instantiate the type so we have a
14235 declaration of the op() for later calls to lambda_function. */
14236 complete_type (type
);
14238 /* The capture list refers to closure members, so this needs to
14239 wait until after we finish instantiating the type. */
14240 LAMBDA_EXPR_CAPTURE_LIST (r
)
14241 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
14243 RETURN (build_lambda_object (r
));
14247 /* We can get here for a constant initializer of non-dependent type.
14248 FIXME stop folding in cp_parser_initializer_clause. */
14249 gcc_assert (TREE_CONSTANT (t
));
14251 tree r
= get_target_expr (RECUR (TARGET_EXPR_INITIAL (t
)));
14252 TREE_CONSTANT (r
) = true;
14256 case TRANSACTION_EXPR
:
14257 RETURN (tsubst_expr(t
, args
, complain
, in_decl
,
14258 integral_constant_expression_p
));
14261 /* Handle Objective-C++ constructs, if appropriate. */
14264 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14265 in_decl
, /*function_p=*/false);
14269 RETURN (tsubst_copy (t
, args
, complain
, in_decl
));
14275 input_location
= loc
;
14279 /* Verify that the instantiated ARGS are valid. For type arguments,
14280 make sure that the type's linkage is ok. For non-type arguments,
14281 make sure they are constants if they are integral or enumerations.
14282 Emit an error under control of COMPLAIN, and return TRUE on error. */
14285 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14287 if (ARGUMENT_PACK_P (t
))
14289 tree vec
= ARGUMENT_PACK_ARGS (t
);
14290 int len
= TREE_VEC_LENGTH (vec
);
14291 bool result
= false;
14294 for (i
= 0; i
< len
; ++i
)
14295 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14299 else if (TYPE_P (t
))
14301 /* [basic.link]: A name with no linkage (notably, the name
14302 of a class or enumeration declared in a local scope)
14303 shall not be used to declare an entity with linkage.
14304 This implies that names with no linkage cannot be used as
14307 DR 757 relaxes this restriction for C++0x. */
14308 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14309 : no_linkage_check (t
, /*relaxed_p=*/false));
14313 /* DR 488 makes use of a type with no linkage cause
14314 type deduction to fail. */
14315 if (complain
& tf_error
)
14317 if (TYPE_ANONYMOUS_P (nt
))
14318 error ("%qT is/uses anonymous type", t
);
14320 error ("template argument for %qD uses local type %qT",
14325 /* In order to avoid all sorts of complications, we do not
14326 allow variably-modified types as template arguments. */
14327 else if (variably_modified_type_p (t
, NULL_TREE
))
14329 if (complain
& tf_error
)
14330 error ("%qT is a variably modified type", t
);
14334 /* A non-type argument of integral or enumerated type must be a
14336 else if (TREE_TYPE (t
)
14337 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14338 && !TREE_CONSTANT (t
))
14340 if (complain
& tf_error
)
14341 error ("integral expression %qE is not constant", t
);
14348 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14350 int ix
, len
= DECL_NTPARMS (tmpl
);
14351 bool result
= false;
14353 for (ix
= 0; ix
!= len
; ix
++)
14355 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14358 if (result
&& (complain
& tf_error
))
14359 error (" trying to instantiate %qD", tmpl
);
14363 /* We're out of SFINAE context now, so generate diagnostics for the access
14364 errors we saw earlier when instantiating D from TMPL and ARGS. */
14367 recheck_decl_substitution (tree d
, tree tmpl
, tree args
)
14369 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
14370 tree type
= TREE_TYPE (pattern
);
14371 location_t loc
= input_location
;
14373 push_access_scope (d
);
14374 push_deferring_access_checks (dk_no_deferred
);
14375 input_location
= DECL_SOURCE_LOCATION (pattern
);
14376 tsubst (type
, args
, tf_warning_or_error
, d
);
14377 input_location
= loc
;
14378 pop_deferring_access_checks ();
14379 pop_access_scope (d
);
14382 /* Instantiate the indicated variable, function, or alias template TMPL with
14383 the template arguments in TARG_PTR. */
14386 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14388 tree targ_ptr
= orig_args
;
14392 bool access_ok
= true;
14394 if (tmpl
== error_mark_node
)
14395 return error_mark_node
;
14397 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14399 /* If this function is a clone, handle it specially. */
14400 if (DECL_CLONED_FUNCTION_P (tmpl
))
14405 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14406 DECL_CLONED_FUNCTION. */
14407 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14408 targ_ptr
, complain
);
14409 if (spec
== error_mark_node
)
14410 return error_mark_node
;
14412 /* Look for the clone. */
14413 FOR_EACH_CLONE (clone
, spec
)
14414 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14416 /* We should always have found the clone by now. */
14417 gcc_unreachable ();
14421 /* Check to see if we already have this specialization. */
14422 gen_tmpl
= most_general_template (tmpl
);
14423 if (tmpl
!= gen_tmpl
)
14424 /* The TMPL is a partial instantiation. To get a full set of
14425 arguments we must add the arguments used to perform the
14426 partial instantiation. */
14427 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14430 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14431 but it doesn't seem to be on the hot path. */
14432 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14434 gcc_assert (tmpl
== gen_tmpl
14435 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14437 || fndecl
== NULL_TREE
);
14439 if (spec
!= NULL_TREE
)
14441 if (FNDECL_HAS_ACCESS_ERRORS (spec
))
14443 if (complain
& tf_error
)
14444 recheck_decl_substitution (spec
, gen_tmpl
, targ_ptr
);
14445 return error_mark_node
;
14450 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14452 return error_mark_node
;
14454 /* We are building a FUNCTION_DECL, during which the access of its
14455 parameters and return types have to be checked. However this
14456 FUNCTION_DECL which is the desired context for access checking
14457 is not built yet. We solve this chicken-and-egg problem by
14458 deferring all checks until we have the FUNCTION_DECL. */
14459 push_deferring_access_checks (dk_deferred
);
14461 /* Instantiation of the function happens in the context of the function
14462 template, not the context of the overload resolution we're doing. */
14463 push_to_top_level ();
14464 /* If there are dependent arguments, e.g. because we're doing partial
14465 ordering, make sure processing_template_decl stays set. */
14466 if (uses_template_parms (targ_ptr
))
14467 ++processing_template_decl
;
14468 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14470 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14471 complain
, gen_tmpl
);
14472 push_nested_class (ctx
);
14474 /* Substitute template parameters to obtain the specialization. */
14475 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14476 targ_ptr
, complain
, gen_tmpl
);
14477 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14478 pop_nested_class ();
14479 pop_from_top_level ();
14481 if (fndecl
== error_mark_node
)
14483 pop_deferring_access_checks ();
14484 return error_mark_node
;
14487 /* The DECL_TI_TEMPLATE should always be the immediate parent
14488 template, not the most general template. */
14489 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14491 /* Now we know the specialization, compute access previously
14493 push_access_scope (fndecl
);
14494 if (!perform_deferred_access_checks (complain
))
14496 pop_access_scope (fndecl
);
14497 pop_deferring_access_checks ();
14499 /* If we've just instantiated the main entry point for a function,
14500 instantiate all the alternate entry points as well. We do this
14501 by cloning the instantiation of the main entry point, not by
14502 instantiating the template clones. */
14503 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14504 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14508 if (!(complain
& tf_error
))
14510 /* Remember to reinstantiate when we're out of SFINAE so the user
14511 can see the errors. */
14512 FNDECL_HAS_ACCESS_ERRORS (fndecl
) = true;
14514 return error_mark_node
;
14519 /* Wrapper for instantiate_template_1. */
14522 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14525 timevar_push (TV_TEMPLATE_INST
);
14526 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14527 timevar_pop (TV_TEMPLATE_INST
);
14531 /* Instantiate the alias template TMPL with ARGS. Also push a template
14532 instantiation level, which instantiate_template doesn't do because
14533 functions and variables have sufficient context established by the
14537 instantiate_alias_template (tree tmpl
, tree args
, tsubst_flags_t complain
)
14539 struct pending_template
*old_last_pend
= last_pending_template
;
14540 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14541 if (tmpl
== error_mark_node
|| args
== error_mark_node
)
14542 return error_mark_node
;
14543 tree tinst
= build_tree_list (tmpl
, args
);
14544 if (!push_tinst_level (tinst
))
14547 return error_mark_node
;
14549 tree r
= instantiate_template (tmpl
, args
, complain
);
14550 pop_tinst_level ();
14551 /* We can't free this if a pending_template entry or last_error_tinst_level
14552 is pointing at it. */
14553 if (last_pending_template
== old_last_pend
14554 && last_error_tinst_level
== old_error_tinst
)
14560 /* PARM is a template parameter pack for FN. Returns true iff
14561 PARM is used in a deducible way in the argument list of FN. */
14564 pack_deducible_p (tree parm
, tree fn
)
14566 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14567 for (; t
; t
= TREE_CHAIN (t
))
14569 tree type
= TREE_VALUE (t
);
14571 if (!PACK_EXPANSION_P (type
))
14573 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14574 packs
; packs
= TREE_CHAIN (packs
))
14575 if (TREE_VALUE (packs
) == parm
)
14577 /* The template parameter pack is used in a function parameter
14578 pack. If this is the end of the parameter list, the
14579 template parameter pack is deducible. */
14580 if (TREE_CHAIN (t
) == void_list_node
)
14583 /* Otherwise, not. Well, it could be deduced from
14584 a non-pack parameter, but doing so would end up with
14585 a deduction mismatch, so don't bother. */
14589 /* The template parameter pack isn't used in any function parameter
14590 packs, but it might be used deeper, e.g. tuple<Args...>. */
14594 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14595 NARGS elements of the arguments that are being used when calling
14596 it. TARGS is a vector into which the deduced template arguments
14599 Return zero for success, 2 for an incomplete match that doesn't resolve
14600 all the types, and 1 for complete failure. An error message will be
14601 printed only for an incomplete match.
14603 If FN is a conversion operator, or we are trying to produce a specific
14604 specialization, RETURN_TYPE is the return type desired.
14606 The EXPLICIT_TARGS are explicit template arguments provided via a
14609 The parameter STRICT is one of:
14612 We are deducing arguments for a function call, as in
14613 [temp.deduct.call].
14616 We are deducing arguments for a conversion function, as in
14617 [temp.deduct.conv].
14620 We are deducing arguments when doing an explicit instantiation
14621 as in [temp.explicit], when determining an explicit specialization
14622 as in [temp.expl.spec], or when taking the address of a function
14623 template, as in [temp.deduct.funcaddr]. */
14626 fn_type_unification (tree fn
,
14627 tree explicit_targs
,
14630 unsigned int nargs
,
14632 unification_kind_t strict
,
14638 tree decl
= NULL_TREE
;
14639 tsubst_flags_t complain
= (explain_p
? tf_warning_or_error
: tf_none
);
14641 static int deduction_depth
;
14642 struct pending_template
*old_last_pend
= last_pending_template
;
14643 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14644 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14646 tree r
= error_mark_node
;
14648 /* Adjust any explicit template arguments before entering the
14649 substitution context. */
14650 if (explicit_targs
)
14653 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14655 /*require_all_args=*/false,
14656 /*use_default_args=*/false));
14657 if (explicit_targs
== error_mark_node
)
14658 return error_mark_node
;
14661 /* In C++0x, it's possible to have a function template whose type depends
14662 on itself recursively. This is most obvious with decltype, but can also
14663 occur with enumeration scope (c++/48969). So we need to catch infinite
14664 recursion and reject the substitution at deduction time; this function
14665 will return error_mark_node for any repeated substitution.
14667 This also catches excessive recursion such as when f<N> depends on
14668 f<N-1> across all integers, and returns error_mark_node for all the
14669 substitutions back up to the initial one.
14671 This is, of course, not reentrant. */
14672 if (excessive_deduction_depth
)
14673 return error_mark_node
;
14674 tinst
= build_tree_list (fn
, targs
);
14675 if (!push_tinst_level (tinst
))
14677 excessive_deduction_depth
= true;
14679 return error_mark_node
;
14682 push_deferring_access_checks (dk_deferred
);
14684 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14686 fntype
= TREE_TYPE (fn
);
14687 if (explicit_targs
)
14691 The specified template arguments must match the template
14692 parameters in kind (i.e., type, nontype, template), and there
14693 must not be more arguments than there are parameters;
14694 otherwise type deduction fails.
14696 Nontype arguments must match the types of the corresponding
14697 nontype template parameters, or must be convertible to the
14698 types of the corresponding nontype parameters as specified in
14699 _temp.arg.nontype_, otherwise type deduction fails.
14701 All references in the function type of the function template
14702 to the corresponding template parameters are replaced by the
14703 specified template argument values. If a substitution in a
14704 template parameter or in the function type of the function
14705 template results in an invalid type, type deduction fails. */
14706 int i
, len
= TREE_VEC_LENGTH (tparms
);
14707 location_t loc
= input_location
;
14708 bool incomplete
= false;
14710 /* Substitute the explicit args into the function type. This is
14711 necessary so that, for instance, explicitly declared function
14712 arguments can match null pointed constants. If we were given
14713 an incomplete set of explicit args, we must not do semantic
14714 processing during substitution as we could create partial
14716 for (i
= 0; i
< len
; i
++)
14718 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14719 bool parameter_pack
= false;
14720 tree targ
= TREE_VEC_ELT (explicit_targs
, i
);
14722 /* Dig out the actual parm. */
14723 if (TREE_CODE (parm
) == TYPE_DECL
14724 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14726 parm
= TREE_TYPE (parm
);
14727 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14729 else if (TREE_CODE (parm
) == PARM_DECL
)
14731 parm
= DECL_INITIAL (parm
);
14732 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14735 if (!parameter_pack
&& targ
== NULL_TREE
)
14736 /* No explicit argument for this template parameter. */
14739 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
14741 /* Mark the argument pack as "incomplete". We could
14742 still deduce more arguments during unification.
14743 We remove this mark in type_unification_real. */
14746 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
14747 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
14748 = ARGUMENT_PACK_ARGS (targ
);
14751 /* We have some incomplete argument packs. */
14756 processing_template_decl
+= incomplete
;
14757 input_location
= DECL_SOURCE_LOCATION (fn
);
14758 fntype
= tsubst (TREE_TYPE (fn
), explicit_targs
,
14759 complain
| tf_partial
, NULL_TREE
);
14760 input_location
= loc
;
14761 processing_template_decl
-= incomplete
;
14763 if (fntype
== error_mark_node
)
14766 /* Place the explicitly specified arguments in TARGS. */
14767 for (i
= NUM_TMPL_ARGS (explicit_targs
); i
--;)
14768 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (explicit_targs
, i
);
14771 /* Never do unification on the 'this' parameter. */
14772 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
14778 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
14779 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
14780 new_args
[0] = return_type
;
14781 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
14786 /* We allow incomplete unification without an error message here
14787 because the standard doesn't seem to explicitly prohibit it. Our
14788 callers must be ready to deal with unification failures in any
14791 pop_tinst_level ();
14792 ok
= !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
14793 targs
, parms
, args
, nargs
, /*subr=*/0,
14794 strict
, flags
, explain_p
);
14795 push_tinst_level (tinst
);
14799 /* Now that we have bindings for all of the template arguments,
14800 ensure that the arguments deduced for the template template
14801 parameters have compatible template parameter lists. We cannot
14802 check this property before we have deduced all template
14803 arguments, because the template parameter types of a template
14804 template parameter might depend on prior template parameters
14805 deduced after the template template parameter. The following
14806 ill-formed example illustrates this issue:
14808 template<typename T, template<T> class C> void f(C<5>, T);
14810 template<int N> struct X {};
14813 f(X<5>(), 5l); // error: template argument deduction fails
14816 The template parameter list of 'C' depends on the template type
14817 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14818 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14819 time that we deduce 'C'. */
14820 if (!template_template_parm_bindings_ok_p
14821 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
14823 unify_inconsistent_template_template_parameters (explain_p
);
14827 /* All is well so far. Now, check:
14831 When all template arguments have been deduced, all uses of
14832 template parameters in nondeduced contexts are replaced with
14833 the corresponding deduced argument values. If the
14834 substitution results in an invalid type, as described above,
14835 type deduction fails. */
14836 decl
= instantiate_template (fn
, targs
, complain
);
14837 if (decl
== error_mark_node
)
14840 /* Now perform any access checks encountered during deduction, such as
14841 for default template arguments. */
14842 push_access_scope (decl
);
14843 ok
= perform_deferred_access_checks (complain
);
14844 pop_access_scope (decl
);
14848 /* If we're looking for an exact match, check that what we got
14849 is indeed an exact match. It might not be if some template
14850 parameters are used in non-deduced contexts. */
14851 if (strict
== DEDUCE_EXACT
)
14853 tree substed
= TREE_TYPE (decl
);
14857 = skip_artificial_parms_for (decl
, TYPE_ARG_TYPES (substed
));
14859 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
14860 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
14861 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
14863 unify_type_mismatch (explain_p
, args
[i
],
14864 TREE_VALUE (sarg
));
14872 pop_deferring_access_checks ();
14874 if (excessive_deduction_depth
)
14876 if (deduction_depth
== 0)
14877 /* Reset once we're all the way out. */
14878 excessive_deduction_depth
= false;
14881 pop_tinst_level ();
14882 /* We can't free this if a pending_template entry or last_error_tinst_level
14883 is pointing at it. */
14884 if (last_pending_template
== old_last_pend
14885 && last_error_tinst_level
== old_error_tinst
)
14891 /* Adjust types before performing type deduction, as described in
14892 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14893 sections are symmetric. PARM is the type of a function parameter
14894 or the return type of the conversion function. ARG is the type of
14895 the argument passed to the call, or the type of the value
14896 initialized with the result of the conversion function.
14897 ARG_EXPR is the original argument expression, which may be null. */
14900 maybe_adjust_types_for_deduction (unification_kind_t strict
,
14914 /* Swap PARM and ARG throughout the remainder of this
14915 function; the handling is precisely symmetric since PARM
14916 will initialize ARG rather than vice versa. */
14924 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14925 too, but here handle it by stripping the reference from PARM
14926 rather than by adding it to ARG. */
14927 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14928 && TYPE_REF_IS_RVALUE (*parm
)
14929 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14930 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
14931 && TREE_CODE (*arg
) == REFERENCE_TYPE
14932 && !TYPE_REF_IS_RVALUE (*arg
))
14933 *parm
= TREE_TYPE (*parm
);
14934 /* Nothing else to do in this case. */
14938 gcc_unreachable ();
14941 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
14943 /* [temp.deduct.call]
14945 If P is not a reference type:
14947 --If A is an array type, the pointer type produced by the
14948 array-to-pointer standard conversion (_conv.array_) is
14949 used in place of A for type deduction; otherwise,
14951 --If A is a function type, the pointer type produced by
14952 the function-to-pointer standard conversion
14953 (_conv.func_) is used in place of A for type deduction;
14956 --If A is a cv-qualified type, the top level
14957 cv-qualifiers of A's type are ignored for type
14959 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
14960 *arg
= build_pointer_type (TREE_TYPE (*arg
));
14961 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
14962 *arg
= build_pointer_type (*arg
);
14964 *arg
= TYPE_MAIN_VARIANT (*arg
);
14967 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
14968 of the form T&&, where T is a template parameter, and the argument
14969 is an lvalue, T is deduced as A& */
14970 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14971 && TYPE_REF_IS_RVALUE (*parm
)
14972 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14973 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
14974 && (arg_expr
? real_lvalue_p (arg_expr
)
14975 /* try_one_overload doesn't provide an arg_expr, but
14976 functions are always lvalues. */
14977 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
14978 *arg
= build_reference_type (*arg
);
14980 /* [temp.deduct.call]
14982 If P is a cv-qualified type, the top level cv-qualifiers
14983 of P's type are ignored for type deduction. If P is a
14984 reference type, the type referred to by P is used for
14986 *parm
= TYPE_MAIN_VARIANT (*parm
);
14987 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
14989 *parm
= TREE_TYPE (*parm
);
14990 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
14993 /* DR 322. For conversion deduction, remove a reference type on parm
14994 too (which has been swapped into ARG). */
14995 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
14996 *arg
= TREE_TYPE (*arg
);
15001 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15002 template which does contain any deducible template parameters; check if
15003 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15004 unify_one_argument. */
15007 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15008 int flags
, bool explain_p
)
15013 type
= TREE_TYPE (arg
);
15017 if (same_type_p (parm
, type
))
15018 return unify_success (explain_p
);
15020 if (strict
== DEDUCE_CONV
)
15022 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15023 explain_p
? tf_warning_or_error
: tf_none
))
15024 return unify_success (explain_p
);
15026 else if (strict
!= DEDUCE_EXACT
)
15028 if (can_convert_arg (parm
, type
,
15029 TYPE_P (arg
) ? NULL_TREE
: arg
,
15030 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15031 return unify_success (explain_p
);
15034 if (strict
== DEDUCE_EXACT
)
15035 return unify_type_mismatch (explain_p
, parm
, arg
);
15037 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15040 /* Subroutine of type_unification_real and unify_pack_expansion to
15041 handle unification of a single P/A pair. Parameters are as
15042 for those functions. */
15045 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15046 int subr
, unification_kind_t strict
, int flags
,
15049 tree arg_expr
= NULL_TREE
;
15052 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15053 return unify_invalid (explain_p
);
15054 if (arg
== unknown_type_node
)
15055 /* We can't deduce anything from this, but we might get all the
15056 template args from other function args. */
15057 return unify_success (explain_p
);
15059 /* FIXME uses_deducible_template_parms */
15060 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15061 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15067 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15068 | UNIFY_ALLOW_MORE_CV_QUAL
15069 | UNIFY_ALLOW_DERIVED
);
15073 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15077 arg_strict
= UNIFY_ALLOW_NONE
;
15081 gcc_unreachable ();
15084 /* We only do these transformations if this is the top-level
15085 parameter_type_list in a call or declaration matching; in other
15086 situations (nested function declarators, template argument lists) we
15087 won't be comparing a type to an expression, and we don't do any type
15093 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15094 if (type_unknown_p (arg
))
15096 /* [temp.deduct.type] A template-argument can be
15097 deduced from a pointer to function or pointer
15098 to member function argument if the set of
15099 overloaded functions does not contain function
15100 templates and at most one of a set of
15101 overloaded functions provides a unique
15104 if (resolve_overloaded_unification
15105 (tparms
, targs
, parm
, arg
, strict
,
15106 arg_strict
, explain_p
))
15107 return unify_success (explain_p
);
15108 return unify_overload_resolution_failure (explain_p
, arg
);
15112 arg
= unlowered_expr_type (arg
);
15113 if (arg
== error_mark_node
)
15114 return unify_invalid (explain_p
);
15118 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15121 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15122 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15124 /* For deduction from an init-list we need the actual list. */
15125 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15127 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15130 /* Most parms like fn_type_unification.
15132 If SUBR is 1, we're being called recursively (to unify the
15133 arguments of a function or method parameter of a function
15137 type_unification_real (tree tparms
,
15141 unsigned int xnargs
,
15143 unification_kind_t strict
,
15149 int ntparms
= TREE_VEC_LENGTH (tparms
);
15150 int saw_undeduced
= 0;
15153 unsigned int nargs
;
15156 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15157 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15158 gcc_assert (ntparms
> 0);
15160 /* Reset the number of non-defaulted template arguments contained
15162 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15170 while (parms
&& parms
!= void_list_node
15173 parm
= TREE_VALUE (parms
);
15175 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15176 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15177 /* For a function parameter pack that occurs at the end of the
15178 parameter-declaration-list, the type A of each remaining
15179 argument of the call is compared with the type P of the
15180 declarator-id of the function parameter pack. */
15183 parms
= TREE_CHAIN (parms
);
15185 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15186 /* For a function parameter pack that does not occur at the
15187 end of the parameter-declaration-list, the type of the
15188 parameter pack is a non-deduced context. */
15194 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15200 && parms
!= void_list_node
15201 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15203 /* Unify the remaining arguments with the pack expansion type. */
15205 tree parmvec
= make_tree_vec (1);
15207 /* Allocate a TREE_VEC and copy in all of the arguments */
15208 argvec
= make_tree_vec (nargs
- ia
);
15209 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15210 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15212 /* Copy the parameter into parmvec. */
15213 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15214 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15215 /*subr=*/subr
, explain_p
))
15218 /* Advance to the end of the list of parameters. */
15219 parms
= TREE_CHAIN (parms
);
15222 /* Fail if we've reached the end of the parm list, and more args
15223 are present, and the parm list isn't variadic. */
15224 if (ia
< nargs
&& parms
== void_list_node
)
15225 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15226 /* Fail if parms are left and they don't have default values. */
15227 if (parms
&& parms
!= void_list_node
15228 && TREE_PURPOSE (parms
) == NULL_TREE
)
15230 unsigned int count
= nargs
;
15232 while (p
&& p
!= void_list_node
)
15235 p
= TREE_CHAIN (p
);
15237 return unify_too_few_arguments (explain_p
, ia
, count
);
15242 tsubst_flags_t complain
= (explain_p
15243 ? tf_warning_or_error
15246 /* Check to see if we need another pass before we start clearing
15247 ARGUMENT_PACK_INCOMPLETE_P. */
15248 for (i
= 0; i
< ntparms
; i
++)
15250 tree targ
= TREE_VEC_ELT (targs
, i
);
15251 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15253 if (targ
|| tparm
== error_mark_node
)
15255 tparm
= TREE_VALUE (tparm
);
15257 /* If this is an undeduced nontype parameter that depends on
15258 a type parameter, try another pass; its type may have been
15259 deduced from a later argument than the one from which
15260 this parameter can be deduced. */
15261 if (TREE_CODE (tparm
) == PARM_DECL
15262 && uses_template_parms (TREE_TYPE (tparm
))
15263 && !saw_undeduced
++)
15267 for (i
= 0; i
< ntparms
; i
++)
15269 tree targ
= TREE_VEC_ELT (targs
, i
);
15270 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15272 /* Clear the "incomplete" flags on all argument packs now so that
15273 substituting them into later default arguments works. */
15274 if (targ
&& ARGUMENT_PACK_P (targ
))
15276 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15277 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15280 if (targ
|| tparm
== error_mark_node
)
15282 tparm
= TREE_VALUE (tparm
);
15284 /* Core issue #226 (C++0x) [temp.deduct]:
15286 If a template argument has not been deduced, its
15287 default template argument, if any, is used.
15289 When we are in C++98 mode, TREE_PURPOSE will either
15290 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15291 to explicitly check cxx_dialect here. */
15292 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15294 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15295 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15296 location_t save_loc
= input_location
;
15298 input_location
= DECL_SOURCE_LOCATION (parm
);
15299 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15300 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15302 input_location
= save_loc
;
15303 if (arg
== error_mark_node
)
15307 TREE_VEC_ELT (targs
, i
) = arg
;
15308 /* The position of the first default template argument,
15309 is also the number of non-defaulted arguments in TARGS.
15311 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15312 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15317 /* If the type parameter is a parameter pack, then it will
15318 be deduced to an empty parameter pack. */
15319 if (template_parameter_pack_p (tparm
))
15323 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15325 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15326 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15327 TREE_CONSTANT (arg
) = 1;
15330 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15332 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15334 TREE_VEC_ELT (targs
, i
) = arg
;
15338 return unify_parameter_deduction_failure (explain_p
, tparm
);
15341 #ifdef ENABLE_CHECKING
15342 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15343 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15346 return unify_success (explain_p
);
15349 /* Subroutine of type_unification_real. Args are like the variables
15350 at the call site. ARG is an overloaded function (or template-id);
15351 we try deducing template args from each of the overloads, and if
15352 only one succeeds, we go with that. Modifies TARGS and returns
15353 true on success. */
15356 resolve_overloaded_unification (tree tparms
,
15360 unification_kind_t strict
,
15364 tree tempargs
= copy_node (targs
);
15366 tree goodfn
= NULL_TREE
;
15369 if (TREE_CODE (arg
) == ADDR_EXPR
)
15371 arg
= TREE_OPERAND (arg
, 0);
15377 if (TREE_CODE (arg
) == COMPONENT_REF
)
15378 /* Handle `&x' where `x' is some static or non-static member
15380 arg
= TREE_OPERAND (arg
, 1);
15382 if (TREE_CODE (arg
) == OFFSET_REF
)
15383 arg
= TREE_OPERAND (arg
, 1);
15385 /* Strip baselink information. */
15386 if (BASELINK_P (arg
))
15387 arg
= BASELINK_FUNCTIONS (arg
);
15389 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15391 /* If we got some explicit template args, we need to plug them into
15392 the affected templates before we try to unify, in case the
15393 explicit args will completely resolve the templates in question. */
15396 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15397 arg
= TREE_OPERAND (arg
, 0);
15399 for (; arg
; arg
= OVL_NEXT (arg
))
15401 tree fn
= OVL_CURRENT (arg
);
15402 tree subargs
, elem
;
15404 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15407 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15408 expl_subargs
, NULL_TREE
, tf_none
,
15409 /*require_all_args=*/true,
15410 /*use_default_args=*/true);
15411 if (subargs
!= error_mark_node
15412 && !any_dependent_template_arguments_p (subargs
))
15414 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15415 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15416 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15417 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15426 /* If no templates (or more than one) are fully resolved by the
15427 explicit arguments, this template-id is a non-deduced context; it
15428 could still be OK if we deduce all template arguments for the
15429 enclosing call through other arguments. */
15433 else if (TREE_CODE (arg
) != OVERLOAD
15434 && TREE_CODE (arg
) != FUNCTION_DECL
)
15435 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15436 -- but the deduction does not succeed because the expression is
15437 not just the function on its own. */
15440 for (; arg
; arg
= OVL_NEXT (arg
))
15441 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15442 TREE_TYPE (OVL_CURRENT (arg
)),
15443 strict
, sub_strict
, addr_p
, explain_p
)
15444 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15446 goodfn
= OVL_CURRENT (arg
);
15450 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15451 to function or pointer to member function argument if the set of
15452 overloaded functions does not contain function templates and at most
15453 one of a set of overloaded functions provides a unique match.
15455 So if we found multiple possibilities, we return success but don't
15456 deduce anything. */
15460 int i
= TREE_VEC_LENGTH (targs
);
15462 if (TREE_VEC_ELT (tempargs
, i
))
15463 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15471 /* Core DR 115: In contexts where deduction is done and fails, or in
15472 contexts where deduction is not done, if a template argument list is
15473 specified and it, along with any default template arguments, identifies
15474 a single function template specialization, then the template-id is an
15475 lvalue for the function template specialization. */
15478 resolve_nondeduced_context (tree orig_expr
)
15480 tree expr
, offset
, baselink
;
15483 if (!type_unknown_p (orig_expr
))
15488 offset
= NULL_TREE
;
15489 baselink
= NULL_TREE
;
15491 if (TREE_CODE (expr
) == ADDR_EXPR
)
15493 expr
= TREE_OPERAND (expr
, 0);
15496 if (TREE_CODE (expr
) == OFFSET_REF
)
15499 expr
= TREE_OPERAND (expr
, 1);
15501 if (BASELINK_P (expr
))
15504 expr
= BASELINK_FUNCTIONS (expr
);
15507 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15510 tree goodfn
= NULL_TREE
;
15512 /* If we got some explicit template args, we need to plug them into
15513 the affected templates before we try to unify, in case the
15514 explicit args will completely resolve the templates in question. */
15516 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15517 tree arg
= TREE_OPERAND (expr
, 0);
15518 tree badfn
= NULL_TREE
;
15519 tree badargs
= NULL_TREE
;
15521 for (; arg
; arg
= OVL_NEXT (arg
))
15523 tree fn
= OVL_CURRENT (arg
);
15524 tree subargs
, elem
;
15526 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15529 subargs
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
15530 expl_subargs
, NULL_TREE
, tf_none
,
15531 /*require_all_args=*/true,
15532 /*use_default_args=*/true);
15533 if (subargs
!= error_mark_node
15534 && !any_dependent_template_arguments_p (subargs
))
15536 elem
= instantiate_template (fn
, subargs
, tf_none
);
15537 if (elem
== error_mark_node
)
15542 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15551 mark_used (goodfn
);
15554 expr
= build_baselink (BASELINK_BINFO (baselink
),
15555 BASELINK_ACCESS_BINFO (baselink
),
15556 expr
, BASELINK_OPTYPE (baselink
));
15560 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15561 expr
= build_offset_ref (base
, expr
, addr
);
15564 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15567 else if (good
== 0 && badargs
)
15568 /* There were no good options and at least one bad one, so let the
15569 user know what the problem is. */
15570 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15575 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15576 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15577 different overloads deduce different arguments for a given parm.
15578 ADDR_P is true if the expression for which deduction is being
15579 performed was of the form "& fn" rather than simply "fn".
15581 Returns 1 on success. */
15584 try_one_overload (tree tparms
,
15589 unification_kind_t strict
,
15598 if (arg
== error_mark_node
)
15601 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15602 to function or pointer to member function argument if the set of
15603 overloaded functions does not contain function templates and at most
15604 one of a set of overloaded functions provides a unique match.
15606 So if this is a template, just return success. */
15608 if (uses_template_parms (arg
))
15611 if (TREE_CODE (arg
) == METHOD_TYPE
)
15612 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15614 arg
= build_pointer_type (arg
);
15616 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15618 /* We don't copy orig_targs for this because if we have already deduced
15619 some template args from previous args, unify would complain when we
15620 try to deduce a template parameter for the same argument, even though
15621 there isn't really a conflict. */
15622 nargs
= TREE_VEC_LENGTH (targs
);
15623 tempargs
= make_tree_vec (nargs
);
15625 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15628 /* First make sure we didn't deduce anything that conflicts with
15629 explicitly specified args. */
15630 for (i
= nargs
; i
--; )
15632 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15633 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15637 else if (uses_template_parms (elt
))
15638 /* Since we're unifying against ourselves, we will fill in
15639 template args used in the function parm list with our own
15640 template parms. Discard them. */
15641 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15642 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15646 for (i
= nargs
; i
--; )
15648 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15651 TREE_VEC_ELT (targs
, i
) = elt
;
15657 /* PARM is a template class (perhaps with unbound template
15658 parameters). ARG is a fully instantiated type. If ARG can be
15659 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15660 TARGS are as for unify. */
15663 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15666 tree copy_of_targs
;
15668 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15669 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15670 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15673 /* We need to make a new template argument vector for the call to
15674 unify. If we used TARGS, we'd clutter it up with the result of
15675 the attempted unification, even if this class didn't work out.
15676 We also don't want to commit ourselves to all the unifications
15677 we've already done, since unification is supposed to be done on
15678 an argument-by-argument basis. In other words, consider the
15679 following pathological case:
15681 template <int I, int J, int K>
15684 template <int I, int J>
15685 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15687 template <int I, int J, int K>
15688 void f(S<I, J, K>, S<I, I, I>);
15697 Now, by the time we consider the unification involving `s2', we
15698 already know that we must have `f<0, 0, 0>'. But, even though
15699 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15700 because there are two ways to unify base classes of S<0, 1, 2>
15701 with S<I, I, I>. If we kept the already deduced knowledge, we
15702 would reject the possibility I=1. */
15703 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15705 /* If unification failed, we're done. */
15706 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15707 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15713 /* Given a template type PARM and a class type ARG, find the unique
15714 base type in ARG that is an instance of PARM. We do not examine
15715 ARG itself; only its base-classes. If there is not exactly one
15716 appropriate base class, return NULL_TREE. PARM may be the type of
15717 a partial specialization, as well as a plain template type. Used
15720 static enum template_base_result
15721 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15722 bool explain_p
, tree
*result
)
15724 tree rval
= NULL_TREE
;
15727 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15729 binfo
= TYPE_BINFO (complete_type (arg
));
15732 /* The type could not be completed. */
15733 *result
= NULL_TREE
;
15734 return tbr_incomplete_type
;
15737 /* Walk in inheritance graph order. The search order is not
15738 important, and this avoids multiple walks of virtual bases. */
15739 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15741 tree r
= try_class_unification (tparms
, targs
, parm
,
15742 BINFO_TYPE (binfo
), explain_p
);
15746 /* If there is more than one satisfactory baseclass, then:
15750 If they yield more than one possible deduced A, the type
15754 if (rval
&& !same_type_p (r
, rval
))
15756 *result
= NULL_TREE
;
15757 return tbr_ambiguous_baseclass
;
15765 return tbr_success
;
15768 /* Returns the level of DECL, which declares a template parameter. */
15771 template_decl_level (tree decl
)
15773 switch (TREE_CODE (decl
))
15776 case TEMPLATE_DECL
:
15777 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
15780 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
15783 gcc_unreachable ();
15788 /* Decide whether ARG can be unified with PARM, considering only the
15789 cv-qualifiers of each type, given STRICT as documented for unify.
15790 Returns nonzero iff the unification is OK on that basis. */
15793 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
15795 int arg_quals
= cp_type_quals (arg
);
15796 int parm_quals
= cp_type_quals (parm
);
15798 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15799 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15801 /* Although a CVR qualifier is ignored when being applied to a
15802 substituted template parameter ([8.3.2]/1 for example), that
15803 does not allow us to unify "const T" with "int&" because both
15804 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15805 It is ok when we're allowing additional CV qualifiers
15806 at the outer level [14.8.2.1]/3,1st bullet. */
15807 if ((TREE_CODE (arg
) == REFERENCE_TYPE
15808 || TREE_CODE (arg
) == FUNCTION_TYPE
15809 || TREE_CODE (arg
) == METHOD_TYPE
)
15810 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
15813 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
15814 && (parm_quals
& TYPE_QUAL_RESTRICT
))
15818 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15819 && (arg_quals
& parm_quals
) != parm_quals
)
15822 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
15823 && (parm_quals
& arg_quals
) != arg_quals
)
15829 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15831 template_parm_level_and_index (tree parm
, int* level
, int* index
)
15833 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15834 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15835 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15837 *index
= TEMPLATE_TYPE_IDX (parm
);
15838 *level
= TEMPLATE_TYPE_LEVEL (parm
);
15842 *index
= TEMPLATE_PARM_IDX (parm
);
15843 *level
= TEMPLATE_PARM_LEVEL (parm
);
15847 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15849 if (unify (TP, TA, P, A, S, EP)) \
15853 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15854 expansion at the end of PACKED_PARMS. Returns 0 if the type
15855 deduction succeeds, 1 otherwise. STRICT is the same as in
15856 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15857 call argument list. We'll need to adjust the arguments to make them
15858 types. SUBR tells us if this is from a recursive call to
15859 type_unification_real, or for comparing two template argument
15863 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
15864 tree packed_args
, unification_kind_t strict
,
15865 bool subr
, bool explain_p
)
15868 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
15869 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
15870 tree pack
, packs
= NULL_TREE
;
15871 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
15872 int len
= TREE_VEC_LENGTH (packed_args
);
15874 /* Determine the parameter packs we will be deducing from the
15875 pattern, and record their current deductions. */
15876 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
15877 pack
; pack
= TREE_CHAIN (pack
))
15879 tree parm_pack
= TREE_VALUE (pack
);
15882 /* Determine the index and level of this parameter pack. */
15883 template_parm_level_and_index (parm_pack
, &level
, &idx
);
15885 /* Keep track of the parameter packs and their corresponding
15887 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
15888 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
15891 /* Loop through all of the arguments that have not yet been
15892 unified and unify each with the pattern. */
15893 for (i
= start
; i
< len
; i
++)
15896 bool any_explicit
= false;
15897 tree arg
= TREE_VEC_ELT (packed_args
, i
);
15899 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
15900 or the element of its argument pack at the current index if
15901 this argument was explicitly specified. */
15902 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15906 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15909 if (TREE_VALUE (pack
)
15910 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
15911 && (i
< TREE_VEC_LENGTH (pargs
)))
15913 any_explicit
= true;
15914 arg
= TREE_VEC_ELT (pargs
, i
);
15916 TMPL_ARG (targs
, level
, idx
) = arg
;
15919 /* If we had explicit template arguments, substitute them into the
15920 pattern before deduction. */
15923 /* Some arguments might still be unspecified or dependent. */
15925 ++processing_template_decl
;
15926 dependent
= any_dependent_template_arguments_p (targs
);
15928 --processing_template_decl
;
15929 parm
= tsubst (pattern
, targs
,
15930 explain_p
? tf_warning_or_error
: tf_none
,
15933 --processing_template_decl
;
15934 if (parm
== error_mark_node
)
15940 /* Unify the pattern with the current argument. */
15941 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15942 LOOKUP_IMPLICIT
, explain_p
))
15945 /* For each parameter pack, collect the deduced value. */
15946 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15949 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15951 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
15952 TMPL_ARG (targs
, level
, idx
);
15956 /* Verify that the results of unification with the parameter packs
15957 produce results consistent with what we've seen before, and make
15958 the deduced argument packs available. */
15959 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15961 tree old_pack
= TREE_VALUE (pack
);
15962 tree new_args
= TREE_TYPE (pack
);
15963 int i
, len
= TREE_VEC_LENGTH (new_args
);
15965 bool nondeduced_p
= false;
15967 /* By default keep the original deduced argument pack.
15968 If necessary, more specific code is going to update the
15969 resulting deduced argument later down in this function. */
15970 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15971 TMPL_ARG (targs
, level
, idx
) = old_pack
;
15973 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
15974 actually deduce anything. */
15975 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
15976 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
15977 nondeduced_p
= true;
15981 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
15983 /* If we had fewer function args than explicit template args,
15984 just use the explicits. */
15985 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
15986 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
15987 if (len
< explicit_len
)
15988 new_args
= explicit_args
;
15994 /* Build the deduced *_ARGUMENT_PACK. */
15995 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
15997 result
= make_node (NONTYPE_ARGUMENT_PACK
);
15998 TREE_TYPE (result
) =
15999 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16000 TREE_CONSTANT (result
) = 1;
16003 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16005 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16007 /* Note the deduced argument packs for this parameter
16009 TMPL_ARG (targs
, level
, idx
) = result
;
16011 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16012 && (ARGUMENT_PACK_ARGS (old_pack
)
16013 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16015 /* We only had the explicitly-provided arguments before, but
16016 now we have a complete set of arguments. */
16017 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16019 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16020 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16021 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16025 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16026 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16028 if (!comp_template_args_with_info (old_args
, new_args
,
16029 &bad_old_arg
, &bad_new_arg
))
16030 /* Inconsistent unification of this parameter pack. */
16031 return unify_parameter_pack_inconsistent (explain_p
,
16037 return unify_success (explain_p
);
16040 /* Deduce the value of template parameters. TPARMS is the (innermost)
16041 set of template parameters to a template. TARGS is the bindings
16042 for those template parameters, as determined thus far; TARGS may
16043 include template arguments for outer levels of template parameters
16044 as well. PARM is a parameter to a template function, or a
16045 subcomponent of that parameter; ARG is the corresponding argument.
16046 This function attempts to match PARM with ARG in a manner
16047 consistent with the existing assignments in TARGS. If more values
16048 are deduced, then TARGS is updated.
16050 Returns 0 if the type deduction succeeds, 1 otherwise. The
16051 parameter STRICT is a bitwise or of the following flags:
16054 Require an exact match between PARM and ARG.
16055 UNIFY_ALLOW_MORE_CV_QUAL:
16056 Allow the deduced ARG to be more cv-qualified (by qualification
16057 conversion) than ARG.
16058 UNIFY_ALLOW_LESS_CV_QUAL:
16059 Allow the deduced ARG to be less cv-qualified than ARG.
16060 UNIFY_ALLOW_DERIVED:
16061 Allow the deduced ARG to be a template base class of ARG,
16062 or a pointer to a template base class of the type pointed to by
16064 UNIFY_ALLOW_INTEGER:
16065 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16066 case for more information.
16067 UNIFY_ALLOW_OUTER_LEVEL:
16068 This is the outermost level of a deduction. Used to determine validity
16069 of qualification conversions. A valid qualification conversion must
16070 have const qualified pointers leading up to the inner type which
16071 requires additional CV quals, except at the outer level, where const
16072 is not required [conv.qual]. It would be normal to set this flag in
16073 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16074 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16075 This is the outermost level of a deduction, and PARM can be more CV
16076 qualified at this point.
16077 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16078 This is the outermost level of a deduction, and PARM can be less CV
16079 qualified at this point. */
16082 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16088 int strict_in
= strict
;
16090 /* I don't think this will do the right thing with respect to types.
16091 But the only case I've seen it in so far has been array bounds, where
16092 signedness is the only information lost, and I think that will be
16094 while (TREE_CODE (parm
) == NOP_EXPR
)
16095 parm
= TREE_OPERAND (parm
, 0);
16097 if (arg
== error_mark_node
)
16098 return unify_invalid (explain_p
);
16099 if (arg
== unknown_type_node
16100 || arg
== init_list_type_node
)
16101 /* We can't deduce anything from this, but we might get all the
16102 template args from other function args. */
16103 return unify_success (explain_p
);
16105 /* If PARM uses template parameters, then we can't bail out here,
16106 even if ARG == PARM, since we won't record unifications for the
16107 template parameters. We might need them if we're trying to
16108 figure out which of two things is more specialized. */
16109 if (arg
== parm
&& !uses_template_parms (parm
))
16110 return unify_success (explain_p
);
16112 /* Handle init lists early, so the rest of the function can assume
16113 we're dealing with a type. */
16114 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16118 tree orig_parm
= parm
;
16120 /* Replace T with std::initializer_list<T> for deduction. */
16121 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16122 && flag_deduce_init_list
)
16123 parm
= listify (parm
);
16125 if (!is_std_init_list (parm
))
16126 /* We can only deduce from an initializer list argument if the
16127 parameter is std::initializer_list; otherwise this is a
16128 non-deduced context. */
16129 return unify_success (explain_p
);
16131 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16133 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16135 int elt_strict
= strict
;
16137 if (elt
== error_mark_node
)
16138 return unify_invalid (explain_p
);
16140 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16142 tree type
= TREE_TYPE (elt
);
16143 /* It should only be possible to get here for a call. */
16144 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16145 elt_strict
|= maybe_adjust_types_for_deduction
16146 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16150 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16154 /* If the std::initializer_list<T> deduction worked, replace the
16155 deduced A with std::initializer_list<A>. */
16156 if (orig_parm
!= parm
)
16158 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16159 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16160 targ
= listify (targ
);
16161 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16163 return unify_success (explain_p
);
16166 /* Immediately reject some pairs that won't unify because of
16167 cv-qualification mismatches. */
16168 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16170 /* It is the elements of the array which hold the cv quals of an array
16171 type, and the elements might be template type parms. We'll check
16172 when we recurse. */
16173 && TREE_CODE (arg
) != ARRAY_TYPE
16174 /* We check the cv-qualifiers when unifying with template type
16175 parameters below. We want to allow ARG `const T' to unify with
16176 PARM `T' for example, when computing which of two templates
16177 is more specialized, for example. */
16178 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16179 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16180 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16182 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16183 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16184 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16185 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16186 strict
&= ~UNIFY_ALLOW_DERIVED
;
16187 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16188 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16190 switch (TREE_CODE (parm
))
16192 case TYPENAME_TYPE
:
16194 case UNBOUND_CLASS_TEMPLATE
:
16195 /* In a type which contains a nested-name-specifier, template
16196 argument values cannot be deduced for template parameters used
16197 within the nested-name-specifier. */
16198 return unify_success (explain_p
);
16200 case TEMPLATE_TYPE_PARM
:
16201 case TEMPLATE_TEMPLATE_PARM
:
16202 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16203 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16204 if (tparm
== error_mark_node
)
16205 return unify_invalid (explain_p
);
16207 if (TEMPLATE_TYPE_LEVEL (parm
)
16208 != template_decl_level (tparm
))
16209 /* The PARM is not one we're trying to unify. Just check
16210 to see if it matches ARG. */
16212 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16213 && (is_auto (parm
) ? is_auto (arg
)
16214 : same_type_p (parm
, arg
)))
16215 return unify_success (explain_p
);
16217 return unify_type_mismatch (explain_p
, parm
, arg
);
16219 idx
= TEMPLATE_TYPE_IDX (parm
);
16220 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16221 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16222 if (tparm
== error_mark_node
)
16223 return unify_invalid (explain_p
);
16225 /* Check for mixed types and values. */
16226 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16227 && TREE_CODE (tparm
) != TYPE_DECL
)
16228 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16229 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16230 gcc_unreachable ();
16232 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16234 /* ARG must be constructed from a template class or a template
16235 template parameter. */
16236 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16237 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16238 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16241 tree parmvec
= TYPE_TI_ARGS (parm
);
16242 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16243 tree full_argvec
= add_to_template_args (targs
, argvec
);
16245 = DECL_INNERMOST_TEMPLATE_PARMS
16246 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16248 int parm_variadic_p
= 0;
16250 /* The resolution to DR150 makes clear that default
16251 arguments for an N-argument may not be used to bind T
16252 to a template template parameter with fewer than N
16253 parameters. It is not safe to permit the binding of
16254 default arguments as an extension, as that may change
16255 the meaning of a conforming program. Consider:
16257 struct Dense { static const unsigned int dim = 1; };
16259 template <template <typename> class View,
16261 void operator+(float, View<Block> const&);
16263 template <typename Block,
16264 unsigned int Dim = Block::dim>
16265 struct Lvalue_proxy { operator float() const; };
16269 Lvalue_proxy<Dense> p;
16274 Here, if Lvalue_proxy is permitted to bind to View, then
16275 the global operator+ will be used; if they are not, the
16276 Lvalue_proxy will be converted to float. */
16277 if (coerce_template_parms (parm_parms
,
16279 TYPE_TI_TEMPLATE (parm
),
16281 ? tf_warning_or_error
16283 /*require_all_args=*/true,
16284 /*use_default_args=*/false)
16285 == error_mark_node
)
16288 /* Deduce arguments T, i from TT<T> or TT<i>.
16289 We check each element of PARMVEC and ARGVEC individually
16290 rather than the whole TREE_VEC since they can have
16291 different number of elements. */
16293 parmvec
= expand_template_argument_pack (parmvec
);
16294 argvec
= expand_template_argument_pack (argvec
);
16296 len
= TREE_VEC_LENGTH (parmvec
);
16298 /* Check if the parameters end in a pack, making them
16301 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16302 parm_variadic_p
= 1;
16304 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16305 return unify_too_few_arguments (explain_p
,
16306 TREE_VEC_LENGTH (argvec
), len
);
16308 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16310 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16311 TREE_VEC_ELT (parmvec
, i
),
16312 TREE_VEC_ELT (argvec
, i
),
16313 UNIFY_ALLOW_NONE
, explain_p
);
16316 if (parm_variadic_p
16317 && unify_pack_expansion (tparms
, targs
,
16320 /*subr=*/true, explain_p
))
16323 arg
= TYPE_TI_TEMPLATE (arg
);
16325 /* Fall through to deduce template name. */
16328 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16329 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16331 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16333 /* Simple cases: Value already set, does match or doesn't. */
16334 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16335 return unify_success (explain_p
);
16337 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16341 /* If PARM is `const T' and ARG is only `int', we don't have
16342 a match unless we are allowing additional qualification.
16343 If ARG is `const int' and PARM is just `T' that's OK;
16344 that binds `const int' to `T'. */
16345 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16347 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16349 /* Consider the case where ARG is `const volatile int' and
16350 PARM is `const T'. Then, T should be `volatile int'. */
16351 arg
= cp_build_qualified_type_real
16352 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16353 if (arg
== error_mark_node
)
16354 return unify_invalid (explain_p
);
16356 /* Simple cases: Value already set, does match or doesn't. */
16357 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16358 return unify_success (explain_p
);
16360 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16362 /* Make sure that ARG is not a variable-sized array. (Note
16363 that were talking about variable-sized arrays (like
16364 `int[n]'), rather than arrays of unknown size (like
16365 `int[]').) We'll get very confused by such a type since
16366 the bound of the array is not constant, and therefore
16367 not mangleable. Besides, such types are not allowed in
16368 ISO C++, so we can do as we please here. We do allow
16369 them for 'auto' deduction, since that isn't ABI-exposed. */
16370 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16371 return unify_vla_arg (explain_p
, arg
);
16373 /* Strip typedefs as in convert_template_argument. */
16374 arg
= canonicalize_type_argument (arg
, tf_none
);
16377 /* If ARG is a parameter pack or an expansion, we cannot unify
16378 against it unless PARM is also a parameter pack. */
16379 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16380 && !template_parameter_pack_p (parm
))
16381 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16383 /* If the argument deduction results is a METHOD_TYPE,
16384 then there is a problem.
16385 METHOD_TYPE doesn't map to any real C++ type the result of
16386 the deduction can not be of that type. */
16387 if (TREE_CODE (arg
) == METHOD_TYPE
)
16388 return unify_method_type_error (explain_p
, arg
);
16390 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16391 return unify_success (explain_p
);
16393 case TEMPLATE_PARM_INDEX
:
16394 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16395 if (tparm
== error_mark_node
)
16396 return unify_invalid (explain_p
);
16398 if (TEMPLATE_PARM_LEVEL (parm
)
16399 != template_decl_level (tparm
))
16401 /* The PARM is not one we're trying to unify. Just check
16402 to see if it matches ARG. */
16403 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16404 && cp_tree_equal (parm
, arg
));
16406 unify_expression_unequal (explain_p
, parm
, arg
);
16410 idx
= TEMPLATE_PARM_IDX (parm
);
16411 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16415 int x
= !cp_tree_equal (targ
, arg
);
16417 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16421 /* [temp.deduct.type] If, in the declaration of a function template
16422 with a non-type template-parameter, the non-type
16423 template-parameter is used in an expression in the function
16424 parameter-list and, if the corresponding template-argument is
16425 deduced, the template-argument type shall match the type of the
16426 template-parameter exactly, except that a template-argument
16427 deduced from an array bound may be of any integral type.
16428 The non-type parameter might use already deduced type parameters. */
16429 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16430 if (!TREE_TYPE (arg
))
16431 /* Template-parameter dependent expression. Just accept it for now.
16432 It will later be processed in convert_template_argument. */
16434 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16436 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16437 && (TREE_CODE (tparm
) == INTEGER_TYPE
16438 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16439 /* Convert the ARG to the type of PARM; the deduced non-type
16440 template argument must exactly match the types of the
16441 corresponding parameter. */
16442 arg
= fold (build_nop (tparm
, arg
));
16443 else if (uses_template_parms (tparm
))
16444 /* We haven't deduced the type of this parameter yet. Try again
16446 return unify_success (explain_p
);
16448 return unify_type_mismatch (explain_p
, tparm
, arg
);
16450 /* If ARG is a parameter pack or an expansion, we cannot unify
16451 against it unless PARM is also a parameter pack. */
16452 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16453 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16454 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16456 arg
= strip_typedefs_expr (arg
);
16457 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16458 return unify_success (explain_p
);
16462 /* A pointer-to-member constant can be unified only with
16463 another constant. */
16464 if (TREE_CODE (arg
) != PTRMEM_CST
)
16465 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16467 /* Just unify the class member. It would be useless (and possibly
16468 wrong, depending on the strict flags) to unify also
16469 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16470 arg refer to the same variable, even if through different
16471 classes. For instance:
16473 struct A { int x; };
16476 Unification of &A::x and &B::x must succeed. */
16477 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16478 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16483 if (TREE_CODE (arg
) != POINTER_TYPE
)
16484 return unify_type_mismatch (explain_p
, parm
, arg
);
16486 /* [temp.deduct.call]
16488 A can be another pointer or pointer to member type that can
16489 be converted to the deduced A via a qualification
16490 conversion (_conv.qual_).
16492 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16493 This will allow for additional cv-qualification of the
16494 pointed-to types if appropriate. */
16496 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16497 /* The derived-to-base conversion only persists through one
16498 level of pointers. */
16499 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16501 return unify (tparms
, targs
, TREE_TYPE (parm
),
16502 TREE_TYPE (arg
), strict
, explain_p
);
16505 case REFERENCE_TYPE
:
16506 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16507 return unify_type_mismatch (explain_p
, parm
, arg
);
16508 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16509 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16512 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16513 return unify_type_mismatch (explain_p
, parm
, arg
);
16514 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16515 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16516 return unify_type_mismatch (explain_p
, parm
, arg
);
16517 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16524 /* Our representation of array types uses "N - 1" as the
16525 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16526 not an integer constant. We cannot unify arbitrarily
16527 complex expressions, so we eliminate the MINUS_EXPRs
16529 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16530 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16533 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16534 parm_max
= TREE_OPERAND (parm_max
, 0);
16536 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16537 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16540 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16541 trying to unify the type of a variable with the type
16542 of a template parameter. For example:
16544 template <unsigned int N>
16545 void f (char (&) [N]);
16552 Here, the type of the ARG will be "int [g(i)]", and
16553 may be a SAVE_EXPR, etc. */
16554 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16555 return unify_vla_arg (explain_p
, arg
);
16556 arg_max
= TREE_OPERAND (arg_max
, 0);
16559 /* If only one of the bounds used a MINUS_EXPR, compensate
16560 by adding one to the other bound. */
16561 if (parm_cst
&& !arg_cst
)
16562 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16566 else if (arg_cst
&& !parm_cst
)
16567 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16572 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16573 UNIFY_ALLOW_INTEGER
, explain_p
);
16575 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16576 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16583 case ENUMERAL_TYPE
:
16586 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16587 return unify_type_mismatch (explain_p
, parm
, arg
);
16589 /* We have already checked cv-qualification at the top of the
16591 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16592 return unify_type_mismatch (explain_p
, parm
, arg
);
16594 /* As far as unification is concerned, this wins. Later checks
16595 will invalidate it if necessary. */
16596 return unify_success (explain_p
);
16598 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16599 /* Type INTEGER_CST can come from ordinary constant template args. */
16601 while (TREE_CODE (arg
) == NOP_EXPR
)
16602 arg
= TREE_OPERAND (arg
, 0);
16604 if (TREE_CODE (arg
) != INTEGER_CST
)
16605 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16606 return (tree_int_cst_equal (parm
, arg
)
16607 ? unify_success (explain_p
)
16608 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16612 int i
, len
, argslen
;
16613 int parm_variadic_p
= 0;
16615 if (TREE_CODE (arg
) != TREE_VEC
)
16616 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16618 len
= TREE_VEC_LENGTH (parm
);
16619 argslen
= TREE_VEC_LENGTH (arg
);
16621 /* Check for pack expansions in the parameters. */
16622 for (i
= 0; i
< len
; ++i
)
16624 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16627 /* We can unify against something with a trailing
16629 parm_variadic_p
= 1;
16631 /* [temp.deduct.type]/9: If the template argument list of
16632 P contains a pack expansion that is not the last
16633 template argument, the entire template argument list
16634 is a non-deduced context. */
16635 return unify_success (explain_p
);
16639 /* If we don't have enough arguments to satisfy the parameters
16640 (not counting the pack expression at the end), or we have
16641 too many arguments for a parameter list that doesn't end in
16642 a pack expression, we can't unify. */
16643 if (parm_variadic_p
16644 ? argslen
< len
- parm_variadic_p
16646 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16648 /* Unify all of the parameters that precede the (optional)
16649 pack expression. */
16650 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16652 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16653 TREE_VEC_ELT (parm
, i
),
16654 TREE_VEC_ELT (arg
, i
),
16655 UNIFY_ALLOW_NONE
, explain_p
);
16657 if (parm_variadic_p
)
16658 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16660 /*subr=*/true, explain_p
);
16661 return unify_success (explain_p
);
16666 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16667 return unify_type_mismatch (explain_p
, parm
, arg
);
16669 if (TYPE_PTRMEMFUNC_P (parm
))
16671 if (!TYPE_PTRMEMFUNC_P (arg
))
16672 return unify_type_mismatch (explain_p
, parm
, arg
);
16674 return unify (tparms
, targs
,
16675 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16676 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16677 strict
, explain_p
);
16680 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16682 tree t
= NULL_TREE
;
16684 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16686 /* First, we try to unify the PARM and ARG directly. */
16687 t
= try_class_unification (tparms
, targs
,
16688 parm
, arg
, explain_p
);
16692 /* Fallback to the special case allowed in
16693 [temp.deduct.call]:
16695 If P is a class, and P has the form
16696 template-id, then A can be a derived class of
16697 the deduced A. Likewise, if P is a pointer to
16698 a class of the form template-id, A can be a
16699 pointer to a derived class pointed to by the
16701 enum template_base_result r
;
16702 r
= get_template_base (tparms
, targs
, parm
, arg
,
16706 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16709 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16710 && (CLASSTYPE_TI_TEMPLATE (parm
)
16711 == CLASSTYPE_TI_TEMPLATE (arg
)))
16712 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16713 Then, we should unify `int' and `U'. */
16716 /* There's no chance of unification succeeding. */
16717 return unify_type_mismatch (explain_p
, parm
, arg
);
16719 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16720 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16722 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16723 return unify_type_mismatch (explain_p
, parm
, arg
);
16724 return unify_success (explain_p
);
16727 case FUNCTION_TYPE
:
16729 unsigned int nargs
;
16734 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16735 return unify_type_mismatch (explain_p
, parm
, arg
);
16737 /* CV qualifications for methods can never be deduced, they must
16738 match exactly. We need to check them explicitly here,
16739 because type_unification_real treats them as any other
16740 cv-qualified parameter. */
16741 if (TREE_CODE (parm
) == METHOD_TYPE
16742 && (!check_cv_quals_for_unify
16744 class_of_this_parm (arg
),
16745 class_of_this_parm (parm
))))
16746 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16748 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
16749 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
16751 nargs
= list_length (TYPE_ARG_TYPES (arg
));
16752 args
= XALLOCAVEC (tree
, nargs
);
16753 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
16754 a
!= NULL_TREE
&& a
!= void_list_node
;
16755 a
= TREE_CHAIN (a
), ++i
)
16756 args
[i
] = TREE_VALUE (a
);
16759 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
16760 args
, nargs
, 1, DEDUCE_EXACT
,
16761 LOOKUP_NORMAL
, explain_p
);
16765 /* Unify a pointer to member with a pointer to member function, which
16766 deduces the type of the member as a function type. */
16767 if (TYPE_PTRMEMFUNC_P (arg
))
16772 /* Check top-level cv qualifiers */
16773 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
16774 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16776 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16777 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
16778 UNIFY_ALLOW_NONE
, explain_p
);
16780 /* Determine the type of the function we are unifying against. */
16781 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
16783 build_function_type (TREE_TYPE (method_type
),
16784 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
16786 /* Extract the cv-qualifiers of the member function from the
16787 implicit object parameter and place them on the function
16788 type to be restored later. */
16789 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
16790 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
16793 if (TREE_CODE (arg
) != OFFSET_TYPE
)
16794 return unify_type_mismatch (explain_p
, parm
, arg
);
16795 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16796 TYPE_OFFSET_BASETYPE (arg
),
16797 UNIFY_ALLOW_NONE
, explain_p
);
16798 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16799 strict
, explain_p
);
16802 if (DECL_TEMPLATE_PARM_P (parm
))
16803 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
16804 if (arg
!= integral_constant_value (parm
))
16805 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16806 return unify_success (explain_p
);
16809 case TEMPLATE_DECL
:
16810 /* Matched cases are handled by the ARG == PARM test above. */
16811 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16814 /* A non-type template parameter that is a variable should be a
16815 an integral constant, in which case, it whould have been
16816 folded into its (constant) value. So we should not be getting
16817 a variable here. */
16818 gcc_unreachable ();
16820 case TYPE_ARGUMENT_PACK
:
16821 case NONTYPE_ARGUMENT_PACK
:
16822 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
16823 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
16826 case DECLTYPE_TYPE
:
16827 case UNDERLYING_TYPE
:
16828 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16829 or UNDERLYING_TYPE nodes. */
16830 return unify_success (explain_p
);
16833 /* Unification fails if we hit an error node. */
16834 return unify_invalid (explain_p
);
16837 /* An unresolved overload is a nondeduced context. */
16838 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
16839 return unify_success (explain_p
);
16840 gcc_assert (EXPR_P (parm
));
16842 /* We must be looking at an expression. This can happen with
16846 void foo(S<I>, S<I + 2>);
16848 This is a "nondeduced context":
16852 The nondeduced contexts are:
16854 --A type that is a template-id in which one or more of
16855 the template-arguments is an expression that references
16856 a template-parameter.
16858 In these cases, we assume deduction succeeded, but don't
16859 actually infer any unifications. */
16861 if (!uses_template_parms (parm
)
16862 && !template_args_equal (parm
, arg
))
16863 return unify_expression_unequal (explain_p
, parm
, arg
);
16865 return unify_success (explain_p
);
16868 #undef RECUR_AND_CHECK_FAILURE
16870 /* Note that DECL can be defined in this translation unit, if
16874 mark_definable (tree decl
)
16877 DECL_NOT_REALLY_EXTERN (decl
) = 1;
16878 FOR_EACH_CLONE (clone
, decl
)
16879 DECL_NOT_REALLY_EXTERN (clone
) = 1;
16882 /* Called if RESULT is explicitly instantiated, or is a member of an
16883 explicitly instantiated class. */
16886 mark_decl_instantiated (tree result
, int extern_p
)
16888 SET_DECL_EXPLICIT_INSTANTIATION (result
);
16890 /* If this entity has already been written out, it's too late to
16891 make any modifications. */
16892 if (TREE_ASM_WRITTEN (result
))
16895 if (TREE_CODE (result
) != FUNCTION_DECL
)
16896 /* The TREE_PUBLIC flag for function declarations will have been
16897 set correctly by tsubst. */
16898 TREE_PUBLIC (result
) = 1;
16900 /* This might have been set by an earlier implicit instantiation. */
16901 DECL_COMDAT (result
) = 0;
16904 DECL_NOT_REALLY_EXTERN (result
) = 0;
16907 mark_definable (result
);
16908 /* Always make artificials weak. */
16909 if (DECL_ARTIFICIAL (result
) && flag_weak
)
16910 comdat_linkage (result
);
16911 /* For WIN32 we also want to put explicit instantiations in
16912 linkonce sections. */
16913 else if (TREE_PUBLIC (result
))
16914 maybe_make_one_only (result
);
16917 /* If EXTERN_P, then this function will not be emitted -- unless
16918 followed by an explicit instantiation, at which point its linkage
16919 will be adjusted. If !EXTERN_P, then this function will be
16920 emitted here. In neither circumstance do we want
16921 import_export_decl to adjust the linkage. */
16922 DECL_INTERFACE_KNOWN (result
) = 1;
16925 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16926 important template arguments. If any are missing, we check whether
16927 they're important by using error_mark_node for substituting into any
16928 args that were used for partial ordering (the ones between ARGS and END)
16929 and seeing if it bubbles up. */
16932 check_undeduced_parms (tree targs
, tree args
, tree end
)
16934 bool found
= false;
16936 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
16937 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
16940 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
16944 tree substed
= tsubst_arg_types (args
, targs
, end
, tf_none
, NULL_TREE
);
16945 if (substed
== error_mark_node
)
16951 /* Given two function templates PAT1 and PAT2, return:
16953 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
16954 -1 if PAT2 is more specialized than PAT1.
16955 0 if neither is more specialized.
16957 LEN indicates the number of parameters we should consider
16958 (defaulted parameters should not be considered).
16960 The 1998 std underspecified function template partial ordering, and
16961 DR214 addresses the issue. We take pairs of arguments, one from
16962 each of the templates, and deduce them against each other. One of
16963 the templates will be more specialized if all the *other*
16964 template's arguments deduce against its arguments and at least one
16965 of its arguments *does* *not* deduce against the other template's
16966 corresponding argument. Deduction is done as for class templates.
16967 The arguments used in deduction have reference and top level cv
16968 qualifiers removed. Iff both arguments were originally reference
16969 types *and* deduction succeeds in both directions, the template
16970 with the more cv-qualified argument wins for that pairing (if
16971 neither is more cv-qualified, they both are equal). Unlike regular
16972 deduction, after all the arguments have been deduced in this way,
16973 we do *not* verify the deduced template argument values can be
16974 substituted into non-deduced contexts.
16976 The logic can be a bit confusing here, because we look at deduce1 and
16977 targs1 to see if pat2 is at least as specialized, and vice versa; if we
16978 can find template arguments for pat1 to make arg1 look like arg2, that
16979 means that arg2 is at least as specialized as arg1. */
16982 more_specialized_fn (tree pat1
, tree pat2
, int len
)
16984 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
16985 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
16986 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
16987 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
16988 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
16989 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
16990 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
16991 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
16992 tree origs1
, origs2
;
16993 bool lose1
= false;
16994 bool lose2
= false;
16996 /* Remove the this parameter from non-static member functions. If
16997 one is a non-static member function and the other is not a static
16998 member function, remove the first parameter from that function
16999 also. This situation occurs for operator functions where we
17000 locate both a member function (with this pointer) and non-member
17001 operator (with explicit first operand). */
17002 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17004 len
--; /* LEN is the number of significant arguments for DECL1 */
17005 args1
= TREE_CHAIN (args1
);
17006 if (!DECL_STATIC_FUNCTION_P (decl2
))
17007 args2
= TREE_CHAIN (args2
);
17009 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17011 args2
= TREE_CHAIN (args2
);
17012 if (!DECL_STATIC_FUNCTION_P (decl1
))
17015 args1
= TREE_CHAIN (args1
);
17019 /* If only one is a conversion operator, they are unordered. */
17020 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17023 /* Consider the return type for a conversion function */
17024 if (DECL_CONV_FN_P (decl1
))
17026 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17027 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17031 processing_template_decl
++;
17037 /* Stop when an ellipsis is seen. */
17038 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17040 tree arg1
= TREE_VALUE (args1
);
17041 tree arg2
= TREE_VALUE (args2
);
17042 int deduce1
, deduce2
;
17046 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17047 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17049 /* When both arguments are pack expansions, we need only
17050 unify the patterns themselves. */
17051 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17052 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17054 /* This is the last comparison we need to do. */
17058 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17060 arg1
= TREE_TYPE (arg1
);
17061 quals1
= cp_type_quals (arg1
);
17064 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17066 arg2
= TREE_TYPE (arg2
);
17067 quals2
= cp_type_quals (arg2
);
17070 arg1
= TYPE_MAIN_VARIANT (arg1
);
17071 arg2
= TYPE_MAIN_VARIANT (arg2
);
17073 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17075 int i
, len2
= list_length (args2
);
17076 tree parmvec
= make_tree_vec (1);
17077 tree argvec
= make_tree_vec (len2
);
17080 /* Setup the parameter vector, which contains only ARG1. */
17081 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17083 /* Setup the argument vector, which contains the remaining
17085 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17086 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17088 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17089 argvec
, DEDUCE_EXACT
,
17090 /*subr=*/true, /*explain_p=*/false)
17093 /* We cannot deduce in the other direction, because ARG1 is
17094 a pack expansion but ARG2 is not. */
17097 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17099 int i
, len1
= list_length (args1
);
17100 tree parmvec
= make_tree_vec (1);
17101 tree argvec
= make_tree_vec (len1
);
17104 /* Setup the parameter vector, which contains only ARG1. */
17105 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17107 /* Setup the argument vector, which contains the remaining
17109 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17110 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17112 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17113 argvec
, DEDUCE_EXACT
,
17114 /*subr=*/true, /*explain_p=*/false)
17117 /* We cannot deduce in the other direction, because ARG2 is
17118 a pack expansion but ARG1 is not.*/
17124 /* The normal case, where neither argument is a pack
17126 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17127 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17129 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17130 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17134 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17135 arg2, then arg2 is not as specialized as arg1. */
17141 /* "If, for a given type, deduction succeeds in both directions
17142 (i.e., the types are identical after the transformations above)
17143 and if the type from the argument template is more cv-qualified
17144 than the type from the parameter template (as described above)
17145 that type is considered to be more specialized than the other. If
17146 neither type is more cv-qualified than the other then neither type
17147 is more specialized than the other." */
17149 if (deduce1
&& deduce2
17150 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17152 if ((quals1
& quals2
) == quals2
)
17154 if ((quals1
& quals2
) == quals1
)
17158 if (lose1
&& lose2
)
17159 /* We've failed to deduce something in either direction.
17160 These must be unordered. */
17163 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17164 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17165 /* We have already processed all of the arguments in our
17166 handing of the pack expansion type. */
17169 args1
= TREE_CHAIN (args1
);
17170 args2
= TREE_CHAIN (args2
);
17173 /* "In most cases, all template parameters must have values in order for
17174 deduction to succeed, but for partial ordering purposes a template
17175 parameter may remain without a value provided it is not used in the
17176 types being used for partial ordering."
17178 Thus, if we are missing any of the targs1 we need to substitute into
17179 origs1, then pat2 is not as specialized as pat1. This can happen when
17180 there is a nondeduced context. */
17181 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17183 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17186 processing_template_decl
--;
17188 /* All things being equal, if the next argument is a pack expansion
17189 for one function but not for the other, prefer the
17190 non-variadic function. FIXME this is bogus; see c++/41958. */
17192 && args1
&& TREE_VALUE (args1
)
17193 && args2
&& TREE_VALUE (args2
))
17195 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17196 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17199 if (lose1
== lose2
)
17207 /* Determine which of two partial specializations of MAIN_TMPL is more
17210 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17211 to the first partial specialization. The TREE_VALUE is the
17212 innermost set of template parameters for the partial
17213 specialization. PAT2 is similar, but for the second template.
17215 Return 1 if the first partial specialization is more specialized;
17216 -1 if the second is more specialized; 0 if neither is more
17219 See [temp.class.order] for information about determining which of
17220 two templates is more specialized. */
17223 more_specialized_class (tree main_tmpl
, tree pat1
, tree pat2
)
17228 bool any_deductions
= false;
17230 tmpl1
= TREE_TYPE (pat1
);
17231 tmpl2
= TREE_TYPE (pat2
);
17233 /* Just like what happens for functions, if we are ordering between
17234 different class template specializations, we may encounter dependent
17235 types in the arguments, and we need our dependency check functions
17236 to behave correctly. */
17237 ++processing_template_decl
;
17238 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat1
),
17239 CLASSTYPE_TI_ARGS (tmpl1
),
17240 CLASSTYPE_TI_ARGS (tmpl2
));
17244 any_deductions
= true;
17247 targs
= get_class_bindings (main_tmpl
, TREE_VALUE (pat2
),
17248 CLASSTYPE_TI_ARGS (tmpl2
),
17249 CLASSTYPE_TI_ARGS (tmpl1
));
17253 any_deductions
= true;
17255 --processing_template_decl
;
17257 /* In the case of a tie where at least one of the class templates
17258 has a parameter pack at the end, the template with the most
17259 non-packed parameters wins. */
17262 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17263 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17265 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17266 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17267 int len1
= TREE_VEC_LENGTH (args1
);
17268 int len2
= TREE_VEC_LENGTH (args2
);
17270 /* We don't count the pack expansion at the end. */
17271 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17273 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17278 else if (len1
< len2
)
17285 /* Return the template arguments that will produce the function signature
17286 DECL from the function template FN, with the explicit template
17287 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17288 also match. Return NULL_TREE if no satisfactory arguments could be
17292 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17294 int ntparms
= DECL_NTPARMS (fn
);
17295 tree targs
= make_tree_vec (ntparms
);
17296 tree decl_type
= TREE_TYPE (decl
);
17297 tree decl_arg_types
;
17299 unsigned int nargs
, ix
;
17302 gcc_assert (decl
!= DECL_TEMPLATE_RESULT (fn
));
17304 /* Never do unification on the 'this' parameter. */
17305 decl_arg_types
= skip_artificial_parms_for (decl
,
17306 TYPE_ARG_TYPES (decl_type
));
17308 nargs
= list_length (decl_arg_types
);
17309 args
= XALLOCAVEC (tree
, nargs
);
17310 for (arg
= decl_arg_types
, ix
= 0;
17311 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17312 arg
= TREE_CHAIN (arg
), ++ix
)
17313 args
[ix
] = TREE_VALUE (arg
);
17315 if (fn_type_unification (fn
, explicit_args
, targs
,
17317 (check_rettype
|| DECL_CONV_FN_P (fn
)
17318 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17319 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false)
17320 == error_mark_node
)
17326 /* Return the innermost template arguments that, when applied to a partial
17327 specialization of MAIN_TMPL whose innermost template parameters are
17328 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17331 For example, suppose we have:
17333 template <class T, class U> struct S {};
17334 template <class T> struct S<T*, int> {};
17336 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17337 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17338 int}. The resulting vector will be {double}, indicating that `T'
17339 is bound to `double'. */
17342 get_class_bindings (tree main_tmpl
, tree tparms
, tree spec_args
, tree args
)
17344 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17346 tree innermost_deduced_args
;
17348 innermost_deduced_args
= make_tree_vec (ntparms
);
17349 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17351 deduced_args
= copy_node (args
);
17352 SET_TMPL_ARGS_LEVEL (deduced_args
,
17353 TMPL_ARGS_DEPTH (deduced_args
),
17354 innermost_deduced_args
);
17357 deduced_args
= innermost_deduced_args
;
17359 if (unify (tparms
, deduced_args
,
17360 INNERMOST_TEMPLATE_ARGS (spec_args
),
17361 INNERMOST_TEMPLATE_ARGS (args
),
17362 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17365 for (i
= 0; i
< ntparms
; ++i
)
17366 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17369 /* Verify that nondeduced template arguments agree with the type
17370 obtained from argument deduction.
17374 struct A { typedef int X; };
17375 template <class T, class U> struct C {};
17376 template <class T> struct C<T, typename T::X> {};
17378 Then with the instantiation `C<A, int>', we can deduce that
17379 `T' is `A' but unify () does not check whether `typename T::X'
17381 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17382 spec_args
= coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (main_tmpl
),
17383 spec_args
, main_tmpl
,
17384 tf_none
, false, false);
17385 if (spec_args
== error_mark_node
17386 /* We only need to check the innermost arguments; the other
17387 arguments will always agree. */
17388 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17389 INNERMOST_TEMPLATE_ARGS (args
)))
17392 /* Now that we have bindings for all of the template arguments,
17393 ensure that the arguments deduced for the template template
17394 parameters have compatible template parameter lists. See the use
17395 of template_template_parm_bindings_ok_p in fn_type_unification
17396 for more information. */
17397 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17400 return deduced_args
;
17403 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17404 Return the TREE_LIST node with the most specialized template, if
17405 any. If there is no most specialized template, the error_mark_node
17408 Note that this function does not look at, or modify, the
17409 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17410 returned is one of the elements of INSTANTIATIONS, callers may
17411 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17412 and retrieve it from the value returned. */
17415 most_specialized_instantiation (tree templates
)
17419 ++processing_template_decl
;
17422 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17426 if (get_bindings (TREE_VALUE (champ
),
17427 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17428 NULL_TREE
, /*check_ret=*/true))
17431 if (get_bindings (TREE_VALUE (fn
),
17432 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17433 NULL_TREE
, /*check_ret=*/true))
17440 /* Equally specialized, move to next function. If there
17441 is no next function, nothing's most specialized. */
17442 fn
= TREE_CHAIN (fn
);
17450 /* Now verify that champ is better than everything earlier in the
17451 instantiation list. */
17452 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17453 if (get_bindings (TREE_VALUE (champ
),
17454 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17455 NULL_TREE
, /*check_ret=*/true)
17456 || !get_bindings (TREE_VALUE (fn
),
17457 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17458 NULL_TREE
, /*check_ret=*/true))
17464 processing_template_decl
--;
17467 return error_mark_node
;
17472 /* If DECL is a specialization of some template, return the most
17473 general such template. Otherwise, returns NULL_TREE.
17475 For example, given:
17477 template <class T> struct S { template <class U> void f(U); };
17479 if TMPL is `template <class U> void S<int>::f(U)' this will return
17480 the full template. This function will not trace past partial
17481 specializations, however. For example, given in addition:
17483 template <class T> struct S<T*> { template <class U> void f(U); };
17485 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17486 `template <class T> template <class U> S<T*>::f(U)'. */
17489 most_general_template (tree decl
)
17491 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17492 an immediate specialization. */
17493 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17495 if (DECL_TEMPLATE_INFO (decl
)) {
17496 decl
= DECL_TI_TEMPLATE (decl
);
17498 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17499 template friend. */
17500 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17506 /* Look for more and more general templates. */
17507 while (DECL_TEMPLATE_INFO (decl
))
17509 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17510 (See cp-tree.h for details.) */
17511 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17514 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17515 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17518 /* Stop if we run into an explicitly specialized class template. */
17519 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17520 && DECL_CONTEXT (decl
)
17521 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17524 decl
= DECL_TI_TEMPLATE (decl
);
17530 /* Return the most specialized of the class template partial
17531 specializations of TMPL which can produce TYPE, a specialization of
17532 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17533 a _TYPE node corresponding to the partial specialization, while the
17534 TREE_PURPOSE is the set of template arguments that must be
17535 substituted into the TREE_TYPE in order to generate TYPE.
17537 If the choice of partial specialization is ambiguous, a diagnostic
17538 is issued, and the error_mark_node is returned. If there are no
17539 partial specializations of TMPL matching TYPE, then NULL_TREE is
17543 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17545 tree list
= NULL_TREE
;
17551 tree outer_args
= NULL_TREE
;
17553 tmpl
= most_general_template (tmpl
);
17554 args
= CLASSTYPE_TI_ARGS (type
);
17556 /* For determining which partial specialization to use, only the
17557 innermost args are interesting. */
17558 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17560 outer_args
= strip_innermost_template_args (args
, 1);
17561 args
= INNERMOST_TEMPLATE_ARGS (args
);
17564 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17566 tree partial_spec_args
;
17568 tree parms
= TREE_VALUE (t
);
17570 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17572 ++processing_template_decl
;
17578 /* Discard the outer levels of args, and then substitute in the
17579 template args from the enclosing class. */
17580 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17581 partial_spec_args
= tsubst_template_args
17582 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17584 /* PARMS already refers to just the innermost parms, but the
17585 template parms in partial_spec_args had their levels lowered
17586 by tsubst, so we need to do the same for the parm list. We
17587 can't just tsubst the TREE_VEC itself, as tsubst wants to
17588 treat a TREE_VEC as an argument vector. */
17589 parms
= copy_node (parms
);
17590 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17591 TREE_VEC_ELT (parms
, i
) =
17592 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17596 partial_spec_args
=
17597 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17598 add_to_template_args (outer_args
,
17599 partial_spec_args
),
17601 /*require_all_args=*/true,
17602 /*use_default_args=*/true);
17604 --processing_template_decl
;
17606 if (partial_spec_args
== error_mark_node
)
17607 return error_mark_node
;
17609 spec_args
= get_class_bindings (tmpl
, parms
,
17615 spec_args
= add_to_template_args (outer_args
, spec_args
);
17616 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17617 TREE_TYPE (list
) = TREE_TYPE (t
);
17624 ambiguous_p
= false;
17627 t
= TREE_CHAIN (t
);
17628 for (; t
; t
= TREE_CHAIN (t
))
17630 fate
= more_specialized_class (tmpl
, champ
, t
);
17637 t
= TREE_CHAIN (t
);
17640 ambiguous_p
= true;
17649 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17651 fate
= more_specialized_class (tmpl
, champ
, t
);
17654 ambiguous_p
= true;
17662 char *spaces
= NULL
;
17663 if (!(complain
& tf_error
))
17664 return error_mark_node
;
17665 error ("ambiguous class template instantiation for %q#T", type
);
17666 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17667 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17669 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17670 spaces
= spaces
? spaces
: get_spaces (str
);
17673 return error_mark_node
;
17679 /* Explicitly instantiate DECL. */
17682 do_decl_instantiation (tree decl
, tree storage
)
17684 tree result
= NULL_TREE
;
17687 if (!decl
|| decl
== error_mark_node
)
17688 /* An error occurred, for which grokdeclarator has already issued
17689 an appropriate message. */
17691 else if (! DECL_LANG_SPECIFIC (decl
))
17693 error ("explicit instantiation of non-template %q#D", decl
);
17696 else if (TREE_CODE (decl
) == VAR_DECL
)
17698 /* There is an asymmetry here in the way VAR_DECLs and
17699 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17700 the latter, the DECL we get back will be marked as a
17701 template instantiation, and the appropriate
17702 DECL_TEMPLATE_INFO will be set up. This does not happen for
17703 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17704 should handle VAR_DECLs as it currently handles
17706 if (!DECL_CLASS_SCOPE_P (decl
))
17708 error ("%qD is not a static data member of a class template", decl
);
17711 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17712 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17714 error ("no matching template for %qD found", decl
);
17717 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17719 error ("type %qT for explicit instantiation %qD does not match "
17720 "declared type %qT", TREE_TYPE (result
), decl
,
17725 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17727 error ("explicit instantiation of %q#D", decl
);
17733 /* Check for various error cases. Note that if the explicit
17734 instantiation is valid the RESULT will currently be marked as an
17735 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17736 until we get here. */
17738 if (DECL_TEMPLATE_SPECIALIZATION (result
))
17740 /* DR 259 [temp.spec].
17742 Both an explicit instantiation and a declaration of an explicit
17743 specialization shall not appear in a program unless the explicit
17744 instantiation follows a declaration of the explicit specialization.
17746 For a given set of template parameters, if an explicit
17747 instantiation of a template appears after a declaration of an
17748 explicit specialization for that template, the explicit
17749 instantiation has no effect. */
17752 else if (DECL_EXPLICIT_INSTANTIATION (result
))
17756 No program shall explicitly instantiate any template more
17759 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17760 the first instantiation was `extern' and the second is not,
17761 and EXTERN_P for the opposite case. */
17762 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
17763 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
17764 /* If an "extern" explicit instantiation follows an ordinary
17765 explicit instantiation, the template is instantiated. */
17769 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
17771 error ("no matching template for %qD found", result
);
17774 else if (!DECL_TEMPLATE_INFO (result
))
17776 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
17780 if (storage
== NULL_TREE
)
17782 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17784 if (!in_system_header
&& (cxx_dialect
== cxx98
))
17785 pedwarn (input_location
, OPT_Wpedantic
,
17786 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17791 error ("storage class %qD applied to template instantiation", storage
);
17793 check_explicit_instantiation_namespace (result
);
17794 mark_decl_instantiated (result
, extern_p
);
17796 instantiate_decl (result
, /*defer_ok=*/1,
17797 /*expl_inst_class_mem_p=*/false);
17801 mark_class_instantiated (tree t
, int extern_p
)
17803 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
17804 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
17805 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
17806 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
17809 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
17810 rest_of_type_compilation (t
, 1);
17814 /* Called from do_type_instantiation through binding_table_foreach to
17815 do recursive instantiation for the type bound in ENTRY. */
17817 bt_instantiate_type_proc (binding_entry entry
, void *data
)
17819 tree storage
= *(tree
*) data
;
17821 if (MAYBE_CLASS_TYPE_P (entry
->type
)
17822 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
17823 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
17826 /* Called from do_type_instantiation to instantiate a member
17827 (a member function or a static member variable) of an
17828 explicitly instantiated class template. */
17830 instantiate_class_member (tree decl
, int extern_p
)
17832 mark_decl_instantiated (decl
, extern_p
);
17834 instantiate_decl (decl
, /*defer_ok=*/1,
17835 /*expl_inst_class_mem_p=*/true);
17838 /* Perform an explicit instantiation of template class T. STORAGE, if
17839 non-null, is the RID for extern, inline or static. COMPLAIN is
17840 nonzero if this is called from the parser, zero if called recursively,
17841 since the standard is unclear (as detailed below). */
17844 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
17849 int previous_instantiation_extern_p
= 0;
17851 if (TREE_CODE (t
) == TYPE_DECL
)
17854 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
17857 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
17859 error ("explicit instantiation of non-class template %qD", tmpl
);
17861 error ("explicit instantiation of non-template type %qT", t
);
17867 if (!COMPLETE_TYPE_P (t
))
17869 if (complain
& tf_error
)
17870 error ("explicit instantiation of %q#T before definition of template",
17875 if (storage
!= NULL_TREE
)
17877 if (!in_system_header
)
17879 if (storage
== ridpointers
[(int) RID_EXTERN
])
17881 if (cxx_dialect
== cxx98
)
17882 pedwarn (input_location
, OPT_Wpedantic
,
17883 "ISO C++ 1998 forbids the use of %<extern%> on "
17884 "explicit instantiations");
17887 pedwarn (input_location
, OPT_Wpedantic
,
17888 "ISO C++ forbids the use of %qE"
17889 " on explicit instantiations", storage
);
17892 if (storage
== ridpointers
[(int) RID_INLINE
])
17894 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17896 else if (storage
== ridpointers
[(int) RID_STATIC
])
17900 error ("storage class %qD applied to template instantiation",
17906 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
17908 /* DR 259 [temp.spec].
17910 Both an explicit instantiation and a declaration of an explicit
17911 specialization shall not appear in a program unless the explicit
17912 instantiation follows a declaration of the explicit specialization.
17914 For a given set of template parameters, if an explicit
17915 instantiation of a template appears after a declaration of an
17916 explicit specialization for that template, the explicit
17917 instantiation has no effect. */
17920 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
17924 No program shall explicitly instantiate any template more
17927 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
17928 instantiation was `extern'. If EXTERN_P then the second is.
17929 These cases are OK. */
17930 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
17932 if (!previous_instantiation_extern_p
&& !extern_p
17933 && (complain
& tf_error
))
17934 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
17936 /* If we've already instantiated the template, just return now. */
17937 if (!CLASSTYPE_INTERFACE_ONLY (t
))
17941 check_explicit_instantiation_namespace (TYPE_NAME (t
));
17942 mark_class_instantiated (t
, extern_p
);
17950 /* In contrast to implicit instantiation, where only the
17951 declarations, and not the definitions, of members are
17952 instantiated, we have here:
17956 The explicit instantiation of a class template specialization
17957 implies the instantiation of all of its members not
17958 previously explicitly specialized in the translation unit
17959 containing the explicit instantiation.
17961 Of course, we can't instantiate member template classes, since
17962 we don't have any arguments for them. Note that the standard
17963 is unclear on whether the instantiation of the members are
17964 *explicit* instantiations or not. However, the most natural
17965 interpretation is that it should be an explicit instantiation. */
17968 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
17969 if (TREE_CODE (tmp
) == FUNCTION_DECL
17970 && DECL_TEMPLATE_INSTANTIATION (tmp
))
17971 instantiate_class_member (tmp
, extern_p
);
17973 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
17974 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
17975 instantiate_class_member (tmp
, extern_p
);
17977 if (CLASSTYPE_NESTED_UTDS (t
))
17978 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
17979 bt_instantiate_type_proc
, &storage
);
17983 /* Given a function DECL, which is a specialization of TMPL, modify
17984 DECL to be a re-instantiation of TMPL with the same template
17985 arguments. TMPL should be the template into which tsubst'ing
17986 should occur for DECL, not the most general template.
17988 One reason for doing this is a scenario like this:
17991 void f(const T&, int i);
17993 void g() { f(3, 7); }
17996 void f(const T& t, const int i) { }
17998 Note that when the template is first instantiated, with
17999 instantiate_template, the resulting DECL will have no name for the
18000 first parameter, and the wrong type for the second. So, when we go
18001 to instantiate the DECL, we regenerate it. */
18004 regenerate_decl_from_template (tree decl
, tree tmpl
)
18006 /* The arguments used to instantiate DECL, from the most general
18011 args
= DECL_TI_ARGS (decl
);
18012 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18014 /* Make sure that we can see identifiers, and compute access
18016 push_access_scope (decl
);
18018 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18026 args_depth
= TMPL_ARGS_DEPTH (args
);
18027 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18028 if (args_depth
> parms_depth
)
18029 args
= get_innermost_template_args (args
, parms_depth
);
18031 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18032 args
, tf_error
, NULL_TREE
,
18033 /*defer_ok*/false);
18034 if (specs
&& specs
!= error_mark_node
)
18035 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18038 /* Merge parameter declarations. */
18039 decl_parm
= skip_artificial_parms_for (decl
,
18040 DECL_ARGUMENTS (decl
));
18042 = skip_artificial_parms_for (code_pattern
,
18043 DECL_ARGUMENTS (code_pattern
));
18044 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18049 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18050 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18051 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18053 parm_type
= type_decays_to (parm_type
);
18054 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18055 TREE_TYPE (decl_parm
) = parm_type
;
18056 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18057 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18059 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18060 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18062 decl_parm
= DECL_CHAIN (decl_parm
);
18063 pattern_parm
= DECL_CHAIN (pattern_parm
);
18065 /* Merge any parameters that match with the function parameter
18067 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18070 tree expanded_types
;
18071 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18072 the parameters in this function parameter pack. */
18073 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18074 args
, tf_error
, NULL_TREE
);
18075 len
= TREE_VEC_LENGTH (expanded_types
);
18076 for (i
= 0; i
< len
; i
++)
18081 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18082 /* Rename the parameter to include the index. */
18083 DECL_NAME (decl_parm
) =
18084 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18085 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18086 parm_type
= type_decays_to (parm_type
);
18087 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18088 TREE_TYPE (decl_parm
) = parm_type
;
18089 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18090 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18092 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18093 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18095 decl_parm
= DECL_CHAIN (decl_parm
);
18098 /* Merge additional specifiers from the CODE_PATTERN. */
18099 if (DECL_DECLARED_INLINE_P (code_pattern
)
18100 && !DECL_DECLARED_INLINE_P (decl
))
18101 DECL_DECLARED_INLINE_P (decl
) = 1;
18103 else if (TREE_CODE (decl
) == VAR_DECL
)
18105 DECL_INITIAL (decl
) =
18106 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18107 tf_error
, DECL_TI_TEMPLATE (decl
),
18108 /*integral_constant_expression_p=*/false);
18109 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18110 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18111 tf_error
, DECL_TI_TEMPLATE (decl
));
18114 gcc_unreachable ();
18116 pop_access_scope (decl
);
18119 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18120 substituted to get DECL. */
18123 template_for_substitution (tree decl
)
18125 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18127 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18128 for the instantiation. This is not always the most general
18129 template. Consider, for example:
18132 struct S { template <class U> void f();
18133 template <> void f<int>(); };
18135 and an instantiation of S<double>::f<int>. We want TD to be the
18136 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18137 while (/* An instantiation cannot have a definition, so we need a
18138 more general template. */
18139 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18140 /* We must also deal with friend templates. Given:
18142 template <class T> struct S {
18143 template <class U> friend void f() {};
18146 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18147 so far as the language is concerned, but that's still
18148 where we get the pattern for the instantiation from. On
18149 other hand, if the definition comes outside the class, say:
18151 template <class T> struct S {
18152 template <class U> friend void f();
18154 template <class U> friend void f() {}
18156 we don't need to look any further. That's what the check for
18157 DECL_INITIAL is for. */
18158 || (TREE_CODE (decl
) == FUNCTION_DECL
18159 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18160 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18162 /* The present template, TD, should not be a definition. If it
18163 were a definition, we should be using it! Note that we
18164 cannot restructure the loop to just keep going until we find
18165 a template with a definition, since that might go too far if
18166 a specialization was declared, but not defined. */
18167 gcc_assert (TREE_CODE (decl
) != VAR_DECL
18168 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18170 /* Fetch the more general template. */
18171 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18177 /* Returns true if we need to instantiate this template instance even if we
18178 know we aren't going to emit it.. */
18181 always_instantiate_p (tree decl
)
18183 /* We always instantiate inline functions so that we can inline them. An
18184 explicit instantiation declaration prohibits implicit instantiation of
18185 non-inline functions. With high levels of optimization, we would
18186 normally inline non-inline functions -- but we're not allowed to do
18187 that for "extern template" functions. Therefore, we check
18188 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18189 return ((TREE_CODE (decl
) == FUNCTION_DECL
18190 && (DECL_DECLARED_INLINE_P (decl
)
18191 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
18192 /* And we need to instantiate static data members so that
18193 their initializers are available in integral constant
18195 || (TREE_CODE (decl
) == VAR_DECL
18196 && decl_maybe_constant_var_p (decl
)));
18199 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18200 instantiate it now, modifying TREE_TYPE (fn). */
18203 maybe_instantiate_noexcept (tree fn
)
18205 tree fntype
, spec
, noex
, clone
;
18207 if (DECL_CLONED_FUNCTION_P (fn
))
18208 fn
= DECL_CLONED_FUNCTION (fn
);
18209 fntype
= TREE_TYPE (fn
);
18210 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18212 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18215 noex
= TREE_PURPOSE (spec
);
18217 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18219 if (push_tinst_level (fn
))
18221 push_access_scope (fn
);
18222 input_location
= DECL_SOURCE_LOCATION (fn
);
18223 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18224 DEFERRED_NOEXCEPT_ARGS (noex
),
18225 tf_warning_or_error
, fn
,
18226 /*function_p=*/false,
18227 /*integral_constant_expression_p=*/true);
18228 pop_access_scope (fn
);
18229 pop_tinst_level ();
18230 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18231 if (spec
== error_mark_node
)
18232 spec
= noexcept_false_spec
;
18235 spec
= noexcept_false_spec
;
18239 /* This is an implicitly declared function, so NOEX is a list of
18240 other functions to evaluate and merge. */
18242 spec
= noexcept_true_spec
;
18243 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18245 tree fn
= OVL_CURRENT (elt
);
18247 maybe_instantiate_noexcept (fn
);
18248 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18249 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18253 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18255 FOR_EACH_CLONE (clone
, fn
)
18257 if (TREE_TYPE (clone
) == fntype
)
18258 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18260 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18264 /* Produce the definition of D, a _DECL generated from a template. If
18265 DEFER_OK is nonzero, then we don't have to actually do the
18266 instantiation now; we just have to do it sometime. Normally it is
18267 an error if this is an explicit instantiation but D is undefined.
18268 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18269 explicitly instantiated class template. */
18272 instantiate_decl (tree d
, int defer_ok
,
18273 bool expl_inst_class_mem_p
)
18275 tree tmpl
= DECL_TI_TEMPLATE (d
);
18282 bool pattern_defined
;
18283 location_t saved_loc
= input_location
;
18288 /* This function should only be used to instantiate templates for
18289 functions and static member variables. */
18290 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18291 || TREE_CODE (d
) == VAR_DECL
);
18293 /* Variables are never deferred; if instantiation is required, they
18294 are instantiated right away. That allows for better code in the
18295 case that an expression refers to the value of the variable --
18296 if the variable has a constant value the referring expression can
18297 take advantage of that fact. */
18298 if (TREE_CODE (d
) == VAR_DECL
18299 || DECL_DECLARED_CONSTEXPR_P (d
))
18302 /* Don't instantiate cloned functions. Instead, instantiate the
18303 functions they cloned. */
18304 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18305 d
= DECL_CLONED_FUNCTION (d
);
18307 if (DECL_TEMPLATE_INSTANTIATED (d
)
18308 || (TREE_CODE (d
) == FUNCTION_DECL
18309 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18310 || DECL_TEMPLATE_SPECIALIZATION (d
))
18311 /* D has already been instantiated or explicitly specialized, so
18312 there's nothing for us to do here.
18314 It might seem reasonable to check whether or not D is an explicit
18315 instantiation, and, if so, stop here. But when an explicit
18316 instantiation is deferred until the end of the compilation,
18317 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18318 the instantiation. */
18321 /* Check to see whether we know that this template will be
18322 instantiated in some other file, as with "extern template"
18324 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18326 /* In general, we do not instantiate such templates. */
18327 if (external_p
&& !always_instantiate_p (d
))
18330 gen_tmpl
= most_general_template (tmpl
);
18331 gen_args
= DECL_TI_ARGS (d
);
18333 if (tmpl
!= gen_tmpl
)
18334 /* We should already have the extra args. */
18335 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18336 == TMPL_ARGS_DEPTH (gen_args
));
18337 /* And what's in the hash table should match D. */
18338 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18339 || spec
== NULL_TREE
);
18341 /* This needs to happen before any tsubsting. */
18342 if (! push_tinst_level (d
))
18345 timevar_push (TV_TEMPLATE_INST
);
18347 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18348 for the instantiation. */
18349 td
= template_for_substitution (d
);
18350 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18352 /* We should never be trying to instantiate a member of a class
18353 template or partial specialization. */
18354 gcc_assert (d
!= code_pattern
);
18356 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18357 || DECL_TEMPLATE_SPECIALIZATION (td
))
18358 /* In the case of a friend template whose definition is provided
18359 outside the class, we may have too many arguments. Drop the
18360 ones we don't need. The same is true for specializations. */
18361 args
= get_innermost_template_args
18362 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18366 if (TREE_CODE (d
) == FUNCTION_DECL
)
18367 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18368 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18370 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18372 /* We may be in the middle of deferred access check. Disable it now. */
18373 push_deferring_access_checks (dk_no_deferred
);
18375 /* Unless an explicit instantiation directive has already determined
18376 the linkage of D, remember that a definition is available for
18378 if (pattern_defined
18379 && !DECL_INTERFACE_KNOWN (d
)
18380 && !DECL_NOT_REALLY_EXTERN (d
))
18381 mark_definable (d
);
18383 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18384 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18385 input_location
= DECL_SOURCE_LOCATION (d
);
18387 /* If D is a member of an explicitly instantiated class template,
18388 and no definition is available, treat it like an implicit
18390 if (!pattern_defined
&& expl_inst_class_mem_p
18391 && DECL_EXPLICIT_INSTANTIATION (d
))
18393 /* Leave linkage flags alone on instantiations with anonymous
18395 if (TREE_PUBLIC (d
))
18397 DECL_NOT_REALLY_EXTERN (d
) = 0;
18398 DECL_INTERFACE_KNOWN (d
) = 0;
18400 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18403 if (TREE_CODE (d
) == FUNCTION_DECL
)
18404 maybe_instantiate_noexcept (d
);
18406 /* Defer all other templates, unless we have been explicitly
18407 forbidden from doing so. */
18408 if (/* If there is no definition, we cannot instantiate the
18411 /* If it's OK to postpone instantiation, do so. */
18413 /* If this is a static data member that will be defined
18414 elsewhere, we don't want to instantiate the entire data
18415 member, but we do want to instantiate the initializer so that
18416 we can substitute that elsewhere. */
18417 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18419 /* The definition of the static data member is now required so
18420 we must substitute the initializer. */
18421 if (TREE_CODE (d
) == VAR_DECL
18422 && !DECL_INITIAL (d
)
18423 && DECL_INITIAL (code_pattern
))
18427 bool const_init
= false;
18429 ns
= decl_namespace_context (d
);
18430 push_nested_namespace (ns
);
18431 push_nested_class (DECL_CONTEXT (d
));
18432 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18434 tf_warning_or_error
, NULL_TREE
,
18435 /*integral_constant_expression_p=*/false);
18436 /* Make sure the initializer is still constant, in case of
18437 circular dependency (template/instantiate6.C). */
18439 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18440 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18441 /*asmspec_tree=*/NULL_TREE
,
18442 LOOKUP_ONLYCONVERTING
);
18443 pop_nested_class ();
18444 pop_nested_namespace (ns
);
18447 /* We restore the source position here because it's used by
18448 add_pending_template. */
18449 input_location
= saved_loc
;
18451 if (at_eof
&& !pattern_defined
18452 && DECL_EXPLICIT_INSTANTIATION (d
)
18453 && DECL_NOT_REALLY_EXTERN (d
))
18456 The definition of a non-exported function template, a
18457 non-exported member function template, or a non-exported
18458 member function or static data member of a class template
18459 shall be present in every translation unit in which it is
18460 explicitly instantiated. */
18461 permerror (input_location
, "explicit instantiation of %qD "
18462 "but no definition available", d
);
18464 /* If we're in unevaluated context, we just wanted to get the
18465 constant value; this isn't an odr use, so don't queue
18466 a full instantiation. */
18467 if (cp_unevaluated_operand
!= 0)
18469 /* ??? Historically, we have instantiated inline functions, even
18470 when marked as "extern template". */
18471 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18472 add_pending_template (d
);
18475 /* Tell the repository that D is available in this translation unit
18476 -- and see if it is supposed to be instantiated here. */
18477 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18479 /* In a PCH file, despite the fact that the repository hasn't
18480 requested instantiation in the PCH it is still possible that
18481 an instantiation will be required in a file that includes the
18484 add_pending_template (d
);
18485 /* Instantiate inline functions so that the inliner can do its
18486 job, even though we'll not be emitting a copy of this
18488 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18492 fn_context
= decl_function_context (d
);
18493 nested
= (current_function_decl
!= NULL_TREE
);
18495 push_to_top_level ();
18497 push_function_context ();
18499 /* Mark D as instantiated so that recursive calls to
18500 instantiate_decl do not try to instantiate it again. */
18501 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18503 /* Regenerate the declaration in case the template has been modified
18504 by a subsequent redeclaration. */
18505 regenerate_decl_from_template (d
, td
);
18507 /* We already set the file and line above. Reset them now in case
18508 they changed as a result of calling regenerate_decl_from_template. */
18509 input_location
= DECL_SOURCE_LOCATION (d
);
18511 if (TREE_CODE (d
) == VAR_DECL
)
18514 bool const_init
= false;
18516 /* Clear out DECL_RTL; whatever was there before may not be right
18517 since we've reset the type of the declaration. */
18518 SET_DECL_RTL (d
, NULL
);
18519 DECL_IN_AGGR_P (d
) = 0;
18521 /* The initializer is placed in DECL_INITIAL by
18522 regenerate_decl_from_template so we don't need to
18523 push/pop_access_scope again here. Pull it out so that
18524 cp_finish_decl can process it. */
18525 init
= DECL_INITIAL (d
);
18526 DECL_INITIAL (d
) = NULL_TREE
;
18527 DECL_INITIALIZED_P (d
) = 0;
18529 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18530 initializer. That function will defer actual emission until
18531 we have a chance to determine linkage. */
18532 DECL_EXTERNAL (d
) = 0;
18534 /* Enter the scope of D so that access-checking works correctly. */
18535 push_nested_class (DECL_CONTEXT (d
));
18536 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18537 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18538 pop_nested_class ();
18540 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18541 synthesize_method (d
);
18542 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18544 struct pointer_map_t
*saved_local_specializations
;
18549 /* Save away the current list, in case we are instantiating one
18550 template from within the body of another. */
18551 saved_local_specializations
= local_specializations
;
18553 /* Set up the list of local specializations. */
18554 local_specializations
= pointer_map_create ();
18556 /* Set up context. */
18557 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18559 /* Some typedefs referenced from within the template code need to be
18560 access checked at template instantiation time, i.e now. These
18561 types were added to the template at parsing time. Let's get those
18562 and perform the access checks then. */
18563 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (gen_tmpl
),
18566 /* Create substitution entries for the parameters. */
18567 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18568 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18569 spec_parm
= DECL_ARGUMENTS (d
);
18570 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18572 register_local_specialization (spec_parm
, tmpl_parm
);
18573 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18574 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18576 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18578 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18580 register_local_specialization (spec_parm
, tmpl_parm
);
18581 spec_parm
= DECL_CHAIN (spec_parm
);
18585 /* Register the (value) argument pack as a specialization of
18586 TMPL_PARM, then move on. */
18587 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18588 register_local_specialization (argpack
, tmpl_parm
);
18591 gcc_assert (!spec_parm
);
18593 /* Substitute into the body of the function. */
18594 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18595 tf_warning_or_error
, tmpl
,
18596 /*integral_constant_expression_p=*/false);
18598 /* Set the current input_location to the end of the function
18599 so that finish_function knows where we are. */
18600 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18602 /* We don't need the local specializations any more. */
18603 pointer_map_destroy (local_specializations
);
18604 local_specializations
= saved_local_specializations
;
18606 /* Finish the function. */
18607 d
= finish_function (0);
18608 expand_or_defer_fn (d
);
18611 /* We're not deferring instantiation any more. */
18612 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18615 pop_from_top_level ();
18617 pop_function_context ();
18620 input_location
= saved_loc
;
18621 pop_deferring_access_checks ();
18622 pop_tinst_level ();
18624 timevar_pop (TV_TEMPLATE_INST
);
18629 /* Run through the list of templates that we wish we could
18630 instantiate, and instantiate any we can. RETRIES is the
18631 number of times we retry pending template instantiation. */
18634 instantiate_pending_templates (int retries
)
18637 location_t saved_loc
= input_location
;
18639 /* Instantiating templates may trigger vtable generation. This in turn
18640 may require further template instantiations. We place a limit here
18641 to avoid infinite loop. */
18642 if (pending_templates
&& retries
>= max_tinst_depth
)
18644 tree decl
= pending_templates
->tinst
->decl
;
18646 error ("template instantiation depth exceeds maximum of %d"
18647 " instantiating %q+D, possibly from virtual table generation"
18648 " (use -ftemplate-depth= to increase the maximum)",
18649 max_tinst_depth
, decl
);
18650 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18651 /* Pretend that we defined it. */
18652 DECL_INITIAL (decl
) = error_mark_node
;
18658 struct pending_template
**t
= &pending_templates
;
18659 struct pending_template
*last
= NULL
;
18663 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18664 bool complete
= false;
18666 if (TYPE_P (instantiation
))
18670 if (!COMPLETE_TYPE_P (instantiation
))
18672 instantiate_class_template (instantiation
);
18673 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18674 for (fn
= TYPE_METHODS (instantiation
);
18676 fn
= TREE_CHAIN (fn
))
18677 if (! DECL_ARTIFICIAL (fn
))
18678 instantiate_decl (fn
,
18680 /*expl_inst_class_mem_p=*/false);
18681 if (COMPLETE_TYPE_P (instantiation
))
18685 complete
= COMPLETE_TYPE_P (instantiation
);
18689 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18690 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18693 = instantiate_decl (instantiation
,
18695 /*expl_inst_class_mem_p=*/false);
18696 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18700 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18701 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18705 /* If INSTANTIATION has been instantiated, then we don't
18706 need to consider it again in the future. */
18714 current_tinst_level
= NULL
;
18716 last_pending_template
= last
;
18718 while (reconsider
);
18720 input_location
= saved_loc
;
18723 /* Substitute ARGVEC into T, which is a list of initializers for
18724 either base class or a non-static data member. The TREE_PURPOSEs
18725 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18726 instantiate_decl. */
18729 tsubst_initializer_list (tree t
, tree argvec
)
18731 tree inits
= NULL_TREE
;
18733 for (; t
; t
= TREE_CHAIN (t
))
18737 tree expanded_bases
= NULL_TREE
;
18738 tree expanded_arguments
= NULL_TREE
;
18741 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
18746 /* Expand the base class expansion type into separate base
18748 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
18749 tf_warning_or_error
,
18751 if (expanded_bases
== error_mark_node
)
18754 /* We'll be building separate TREE_LISTs of arguments for
18756 len
= TREE_VEC_LENGTH (expanded_bases
);
18757 expanded_arguments
= make_tree_vec (len
);
18758 for (i
= 0; i
< len
; i
++)
18759 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
18761 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18762 expand each argument in the TREE_VALUE of t. */
18763 expr
= make_node (EXPR_PACK_EXPANSION
);
18764 PACK_EXPANSION_LOCAL_P (expr
) = true;
18765 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
18766 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
18768 if (TREE_VALUE (t
) == void_type_node
)
18769 /* VOID_TYPE_NODE is used to indicate
18770 value-initialization. */
18772 for (i
= 0; i
< len
; i
++)
18773 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
18777 /* Substitute parameter packs into each argument in the
18779 in_base_initializer
= 1;
18780 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
18782 tree expanded_exprs
;
18784 /* Expand the argument. */
18785 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
18787 = tsubst_pack_expansion (expr
, argvec
,
18788 tf_warning_or_error
,
18790 if (expanded_exprs
== error_mark_node
)
18793 /* Prepend each of the expanded expressions to the
18794 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18795 for (i
= 0; i
< len
; i
++)
18797 TREE_VEC_ELT (expanded_arguments
, i
) =
18798 tree_cons (NULL_TREE
,
18799 TREE_VEC_ELT (expanded_exprs
, i
),
18800 TREE_VEC_ELT (expanded_arguments
, i
));
18803 in_base_initializer
= 0;
18805 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18806 since we built them backwards. */
18807 for (i
= 0; i
< len
; i
++)
18809 TREE_VEC_ELT (expanded_arguments
, i
) =
18810 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
18815 for (i
= 0; i
< len
; ++i
)
18817 if (expanded_bases
)
18819 decl
= TREE_VEC_ELT (expanded_bases
, i
);
18820 decl
= expand_member_init (decl
);
18821 init
= TREE_VEC_ELT (expanded_arguments
, i
);
18826 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
18827 tf_warning_or_error
, NULL_TREE
);
18829 decl
= expand_member_init (decl
);
18830 if (decl
&& !DECL_P (decl
))
18831 in_base_initializer
= 1;
18833 init
= TREE_VALUE (t
);
18835 if (init
!= void_type_node
)
18836 init
= tsubst_expr (init
, argvec
,
18837 tf_warning_or_error
, NULL_TREE
,
18838 /*integral_constant_expression_p=*/false);
18839 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
18840 /* If we had an initializer but it instantiated to nothing,
18841 value-initialize the object. This will only occur when
18842 the initializer was a pack expansion where the parameter
18843 packs used in that expansion were of length zero. */
18844 init
= void_type_node
;
18845 in_base_initializer
= 0;
18850 init
= build_tree_list (decl
, init
);
18851 TREE_CHAIN (init
) = inits
;
18859 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18862 set_current_access_from_decl (tree decl
)
18864 if (TREE_PRIVATE (decl
))
18865 current_access_specifier
= access_private_node
;
18866 else if (TREE_PROTECTED (decl
))
18867 current_access_specifier
= access_protected_node
;
18869 current_access_specifier
= access_public_node
;
18872 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18873 is the instantiation (which should have been created with
18874 start_enum) and ARGS are the template arguments to use. */
18877 tsubst_enum (tree tag
, tree newtag
, tree args
)
18881 if (SCOPED_ENUM_P (newtag
))
18882 begin_scope (sk_scoped_enum
, newtag
);
18884 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
18889 decl
= TREE_VALUE (e
);
18890 /* Note that in a template enum, the TREE_VALUE is the
18891 CONST_DECL, not the corresponding INTEGER_CST. */
18892 value
= tsubst_expr (DECL_INITIAL (decl
),
18893 args
, tf_warning_or_error
, NULL_TREE
,
18894 /*integral_constant_expression_p=*/true);
18896 /* Give this enumeration constant the correct access. */
18897 set_current_access_from_decl (decl
);
18899 /* Actually build the enumerator itself. */
18901 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
18904 if (SCOPED_ENUM_P (newtag
))
18907 finish_enum_value_list (newtag
);
18908 finish_enum (newtag
);
18910 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
18911 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
18914 /* DECL is a FUNCTION_DECL that is a template specialization. Return
18915 its type -- but without substituting the innermost set of template
18916 arguments. So, innermost set of template parameters will appear in
18920 get_mostly_instantiated_function_type (tree decl
)
18928 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
18929 targs
= DECL_TI_ARGS (decl
);
18930 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
18931 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
18933 /* There should be as many levels of arguments as there are levels
18935 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
18937 fn_type
= TREE_TYPE (tmpl
);
18939 if (parm_depth
== 1)
18940 /* No substitution is necessary. */
18947 /* Replace the innermost level of the TARGS with NULL_TREEs to
18948 let tsubst know not to substitute for those parameters. */
18949 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
18950 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
18951 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
18952 TMPL_ARGS_LEVEL (targs
, i
));
18953 SET_TMPL_ARGS_LEVEL (partial_args
,
18954 TMPL_ARGS_DEPTH (targs
),
18955 make_tree_vec (DECL_NTPARMS (tmpl
)));
18957 /* Make sure that we can see identifiers, and compute access
18959 push_access_scope (decl
);
18961 ++processing_template_decl
;
18962 /* Now, do the (partial) substitution to figure out the
18963 appropriate function type. */
18964 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
18965 --processing_template_decl
;
18967 /* Substitute into the template parameters to obtain the real
18968 innermost set of parameters. This step is important if the
18969 innermost set of template parameters contains value
18970 parameters whose types depend on outer template parameters. */
18971 TREE_VEC_LENGTH (partial_args
)--;
18972 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
18974 pop_access_scope (decl
);
18980 /* Return truthvalue if we're processing a template different from
18981 the last one involved in diagnostics. */
18983 problematic_instantiation_changed (void)
18985 return current_tinst_level
!= last_error_tinst_level
;
18988 /* Remember current template involved in diagnostics. */
18990 record_last_problematic_instantiation (void)
18992 last_error_tinst_level
= current_tinst_level
;
18995 struct tinst_level
*
18996 current_instantiation (void)
18998 return current_tinst_level
;
19001 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19002 type. Return zero for ok, nonzero for disallowed. Issue error and
19003 warning messages under control of COMPLAIN. */
19006 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19008 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19010 else if (POINTER_TYPE_P (type
))
19012 else if (TYPE_PTRMEM_P (type
))
19014 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19016 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19018 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19020 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19023 if (complain
& tf_error
)
19025 if (type
== error_mark_node
)
19026 inform (input_location
, "invalid template non-type parameter");
19028 error ("%q#T is not a valid type for a template non-type parameter",
19034 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19035 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19038 dependent_type_p_r (tree type
)
19044 A type is dependent if it is:
19046 -- a template parameter. Template template parameters are types
19047 for us (since TYPE_P holds true for them) so we handle
19049 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19050 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19052 /* -- a qualified-id with a nested-name-specifier which contains a
19053 class-name that names a dependent type or whose unqualified-id
19054 names a dependent type. */
19055 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19057 /* -- a cv-qualified type where the cv-unqualified type is
19059 type
= TYPE_MAIN_VARIANT (type
);
19060 /* -- a compound type constructed from any dependent type. */
19061 if (TYPE_PTRMEM_P (type
))
19062 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19063 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19065 else if (TREE_CODE (type
) == POINTER_TYPE
19066 || TREE_CODE (type
) == REFERENCE_TYPE
)
19067 return dependent_type_p (TREE_TYPE (type
));
19068 else if (TREE_CODE (type
) == FUNCTION_TYPE
19069 || TREE_CODE (type
) == METHOD_TYPE
)
19073 if (dependent_type_p (TREE_TYPE (type
)))
19075 for (arg_type
= TYPE_ARG_TYPES (type
);
19077 arg_type
= TREE_CHAIN (arg_type
))
19078 if (dependent_type_p (TREE_VALUE (arg_type
)))
19082 /* -- an array type constructed from any dependent type or whose
19083 size is specified by a constant expression that is
19086 We checked for type- and value-dependence of the bounds in
19087 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19088 if (TREE_CODE (type
) == ARRAY_TYPE
)
19090 if (TYPE_DOMAIN (type
)
19091 && dependent_type_p (TYPE_DOMAIN (type
)))
19093 return dependent_type_p (TREE_TYPE (type
));
19096 /* -- a template-id in which either the template name is a template
19098 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19100 /* ... or any of the template arguments is a dependent type or
19101 an expression that is type-dependent or value-dependent. */
19102 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19103 && (any_dependent_template_arguments_p
19104 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19107 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19108 dependent; if the argument of the `typeof' expression is not
19109 type-dependent, then it should already been have resolved. */
19110 if (TREE_CODE (type
) == TYPEOF_TYPE
19111 || TREE_CODE (type
) == DECLTYPE_TYPE
19112 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19115 /* A template argument pack is dependent if any of its packed
19117 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19119 tree args
= ARGUMENT_PACK_ARGS (type
);
19120 int i
, len
= TREE_VEC_LENGTH (args
);
19121 for (i
= 0; i
< len
; ++i
)
19122 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19126 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19127 be template parameters. */
19128 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19131 /* The standard does not specifically mention types that are local
19132 to template functions or local classes, but they should be
19133 considered dependent too. For example:
19135 template <int I> void f() {
19140 The size of `E' cannot be known until the value of `I' has been
19141 determined. Therefore, `E' must be considered dependent. */
19142 scope
= TYPE_CONTEXT (type
);
19143 if (scope
&& TYPE_P (scope
))
19144 return dependent_type_p (scope
);
19145 /* Don't use type_dependent_expression_p here, as it can lead
19146 to infinite recursion trying to determine whether a lambda
19147 nested in a lambda is dependent (c++/47687). */
19148 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19149 && DECL_LANG_SPECIFIC (scope
)
19150 && DECL_TEMPLATE_INFO (scope
)
19151 && (any_dependent_template_arguments_p
19152 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19155 /* Other types are non-dependent. */
19159 /* Returns TRUE if TYPE is dependent, in the sense of
19160 [temp.dep.type]. Note that a NULL type is considered dependent. */
19163 dependent_type_p (tree type
)
19165 /* If there are no template parameters in scope, then there can't be
19166 any dependent types. */
19167 if (!processing_template_decl
)
19169 /* If we are not processing a template, then nobody should be
19170 providing us with a dependent type. */
19172 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19176 /* If the type is NULL, we have not computed a type for the entity
19177 in question; in that case, the type is dependent. */
19181 /* Erroneous types can be considered non-dependent. */
19182 if (type
== error_mark_node
)
19185 /* If we have not already computed the appropriate value for TYPE,
19187 if (!TYPE_DEPENDENT_P_VALID (type
))
19189 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19190 TYPE_DEPENDENT_P_VALID (type
) = 1;
19193 return TYPE_DEPENDENT_P (type
);
19196 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19197 lookup. In other words, a dependent type that is not the current
19201 dependent_scope_p (tree scope
)
19203 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19204 && !currently_open_class (scope
));
19207 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19208 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19211 /* Note that this predicate is not appropriate for general expressions;
19212 only constant expressions (that satisfy potential_constant_expression)
19213 can be tested for value dependence. */
19216 value_dependent_expression_p (tree expression
)
19218 if (!processing_template_decl
)
19221 /* A name declared with a dependent type. */
19222 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19225 switch (TREE_CODE (expression
))
19227 case IDENTIFIER_NODE
:
19228 /* A name that has not been looked up -- must be dependent. */
19231 case TEMPLATE_PARM_INDEX
:
19232 /* A non-type template parm. */
19236 /* A non-type template parm. */
19237 if (DECL_TEMPLATE_PARM_P (expression
))
19239 return value_dependent_expression_p (DECL_INITIAL (expression
));
19242 /* A constant with literal type and is initialized
19243 with an expression that is value-dependent.
19245 Note that a non-dependent parenthesized initializer will have
19246 already been replaced with its constant value, so if we see
19247 a TREE_LIST it must be dependent. */
19248 if (DECL_INITIAL (expression
)
19249 && decl_constant_var_p (expression
)
19250 && (TREE_CODE (DECL_INITIAL (expression
)) == TREE_LIST
19251 || value_dependent_expression_p (DECL_INITIAL (expression
))))
19255 case DYNAMIC_CAST_EXPR
:
19256 case STATIC_CAST_EXPR
:
19257 case CONST_CAST_EXPR
:
19258 case REINTERPRET_CAST_EXPR
:
19260 /* These expressions are value-dependent if the type to which
19261 the cast occurs is dependent or the expression being casted
19262 is value-dependent. */
19264 tree type
= TREE_TYPE (expression
);
19266 if (dependent_type_p (type
))
19269 /* A functional cast has a list of operands. */
19270 expression
= TREE_OPERAND (expression
, 0);
19273 /* If there are no operands, it must be an expression such
19274 as "int()". This should not happen for aggregate types
19275 because it would form non-constant expressions. */
19276 gcc_assert (cxx_dialect
>= cxx0x
19277 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19282 if (TREE_CODE (expression
) == TREE_LIST
)
19283 return any_value_dependent_elements_p (expression
);
19285 return value_dependent_expression_p (expression
);
19291 /* A `sizeof' expression is value-dependent if the operand is
19292 type-dependent or is a pack expansion. */
19293 expression
= TREE_OPERAND (expression
, 0);
19294 if (PACK_EXPANSION_P (expression
))
19296 else if (TYPE_P (expression
))
19297 return dependent_type_p (expression
);
19298 return instantiation_dependent_expression_p (expression
);
19300 case AT_ENCODE_EXPR
:
19301 /* An 'encode' expression is value-dependent if the operand is
19303 expression
= TREE_OPERAND (expression
, 0);
19304 return dependent_type_p (expression
);
19306 case NOEXCEPT_EXPR
:
19307 expression
= TREE_OPERAND (expression
, 0);
19308 return instantiation_dependent_expression_p (expression
);
19311 /* instantiation_dependent_r treats this as dependent so that we
19312 check access at instantiation time, and all instantiation-dependent
19313 expressions should also be considered value-dependent. */
19316 case COMPONENT_REF
:
19317 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19318 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19320 case NONTYPE_ARGUMENT_PACK
:
19321 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19322 is value-dependent. */
19324 tree values
= ARGUMENT_PACK_ARGS (expression
);
19325 int i
, len
= TREE_VEC_LENGTH (values
);
19327 for (i
= 0; i
< len
; ++i
)
19328 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19336 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19337 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19338 || (type2
? dependent_type_p (type2
) : false));
19342 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19343 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19346 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19347 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19351 tree op
= TREE_OPERAND (expression
, 0);
19352 return (value_dependent_expression_p (op
)
19353 || has_value_dependent_address (op
));
19358 tree fn
= get_callee_fndecl (expression
);
19360 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19362 nargs
= call_expr_nargs (expression
);
19363 for (i
= 0; i
< nargs
; ++i
)
19365 tree op
= CALL_EXPR_ARG (expression
, i
);
19366 /* In a call to a constexpr member function, look through the
19367 implicit ADDR_EXPR on the object argument so that it doesn't
19368 cause the call to be considered value-dependent. We also
19369 look through it in potential_constant_expression. */
19370 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19371 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19372 && TREE_CODE (op
) == ADDR_EXPR
)
19373 op
= TREE_OPERAND (op
, 0);
19374 if (value_dependent_expression_p (op
))
19380 case TEMPLATE_ID_EXPR
:
19381 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19383 return type_dependent_expression_p (expression
);
19389 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19390 if (value_dependent_expression_p (val
))
19396 /* Treat a GNU statement expression as dependent to avoid crashing
19397 under fold_non_dependent_expr; it can't be constant. */
19401 /* A constant expression is value-dependent if any subexpression is
19402 value-dependent. */
19403 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19405 case tcc_reference
:
19407 case tcc_comparison
:
19409 case tcc_expression
:
19412 int i
, len
= cp_tree_operand_length (expression
);
19414 for (i
= 0; i
< len
; i
++)
19416 tree t
= TREE_OPERAND (expression
, i
);
19418 /* In some cases, some of the operands may be missing.l
19419 (For example, in the case of PREDECREMENT_EXPR, the
19420 amount to increment by may be missing.) That doesn't
19421 make the expression dependent. */
19422 if (t
&& value_dependent_expression_p (t
))
19433 /* The expression is not value-dependent. */
19437 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19438 [temp.dep.expr]. Note that an expression with no type is
19439 considered dependent. Other parts of the compiler arrange for an
19440 expression with type-dependent subexpressions to have no type, so
19441 this function doesn't have to be fully recursive. */
19444 type_dependent_expression_p (tree expression
)
19446 if (!processing_template_decl
)
19449 if (expression
== error_mark_node
)
19452 /* An unresolved name is always dependent. */
19453 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19454 || TREE_CODE (expression
) == USING_DECL
)
19457 /* Some expression forms are never type-dependent. */
19458 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19459 || TREE_CODE (expression
) == SIZEOF_EXPR
19460 || TREE_CODE (expression
) == ALIGNOF_EXPR
19461 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19462 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19463 || TREE_CODE (expression
) == TRAIT_EXPR
19464 || TREE_CODE (expression
) == TYPEID_EXPR
19465 || TREE_CODE (expression
) == DELETE_EXPR
19466 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19467 || TREE_CODE (expression
) == THROW_EXPR
)
19470 /* The types of these expressions depends only on the type to which
19471 the cast occurs. */
19472 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19473 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19474 || TREE_CODE (expression
) == CONST_CAST_EXPR
19475 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19476 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19477 || TREE_CODE (expression
) == CAST_EXPR
)
19478 return dependent_type_p (TREE_TYPE (expression
));
19480 /* The types of these expressions depends only on the type created
19481 by the expression. */
19482 if (TREE_CODE (expression
) == NEW_EXPR
19483 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19485 /* For NEW_EXPR tree nodes created inside a template, either
19486 the object type itself or a TREE_LIST may appear as the
19488 tree type
= TREE_OPERAND (expression
, 1);
19489 if (TREE_CODE (type
) == TREE_LIST
)
19490 /* This is an array type. We need to check array dimensions
19492 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19493 || value_dependent_expression_p
19494 (TREE_OPERAND (TREE_VALUE (type
), 1));
19496 return dependent_type_p (type
);
19499 if (TREE_CODE (expression
) == SCOPE_REF
)
19501 tree scope
= TREE_OPERAND (expression
, 0);
19502 tree name
= TREE_OPERAND (expression
, 1);
19504 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19505 contains an identifier associated by name lookup with one or more
19506 declarations declared with a dependent type, or...a
19507 nested-name-specifier or qualified-id that names a member of an
19508 unknown specialization. */
19509 return (type_dependent_expression_p (name
)
19510 || dependent_scope_p (scope
));
19513 if (TREE_CODE (expression
) == FUNCTION_DECL
19514 && DECL_LANG_SPECIFIC (expression
)
19515 && DECL_TEMPLATE_INFO (expression
)
19516 && (any_dependent_template_arguments_p
19517 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19520 if (TREE_CODE (expression
) == TEMPLATE_DECL
19521 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19524 if (TREE_CODE (expression
) == STMT_EXPR
)
19525 expression
= stmt_expr_value_expr (expression
);
19527 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19532 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19534 if (type_dependent_expression_p (elt
))
19540 /* A static data member of the current instantiation with incomplete
19541 array type is type-dependent, as the definition and specializations
19542 can have different bounds. */
19543 if (TREE_CODE (expression
) == VAR_DECL
19544 && DECL_CLASS_SCOPE_P (expression
)
19545 && dependent_type_p (DECL_CONTEXT (expression
))
19546 && VAR_HAD_UNKNOWN_BOUND (expression
))
19549 if (TREE_TYPE (expression
) == unknown_type_node
)
19551 if (TREE_CODE (expression
) == ADDR_EXPR
)
19552 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19553 if (TREE_CODE (expression
) == COMPONENT_REF
19554 || TREE_CODE (expression
) == OFFSET_REF
)
19556 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19558 expression
= TREE_OPERAND (expression
, 1);
19559 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19562 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19563 if (TREE_CODE (expression
) == SCOPE_REF
)
19566 if (BASELINK_P (expression
))
19567 expression
= BASELINK_FUNCTIONS (expression
);
19569 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19571 if (any_dependent_template_arguments_p
19572 (TREE_OPERAND (expression
, 1)))
19574 expression
= TREE_OPERAND (expression
, 0);
19576 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19577 || TREE_CODE (expression
) == FUNCTION_DECL
);
19581 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19583 expression
= OVL_NEXT (expression
);
19588 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19590 return (dependent_type_p (TREE_TYPE (expression
)));
19593 /* walk_tree callback function for instantiation_dependent_expression_p,
19594 below. Returns non-zero if a dependent subexpression is found. */
19597 instantiation_dependent_r (tree
*tp
, int *walk_subtrees
,
19602 /* We don't have to worry about decltype currently because decltype
19603 of an instantiation-dependent expr is a dependent type. This
19604 might change depending on the resolution of DR 1172. */
19605 *walk_subtrees
= false;
19608 enum tree_code code
= TREE_CODE (*tp
);
19611 /* Don't treat an argument list as dependent just because it has no
19617 case TEMPLATE_PARM_INDEX
:
19620 /* Handle expressions with type operands. */
19624 case AT_ENCODE_EXPR
:
19627 tree op
= TREE_OPERAND (*tp
, 0);
19630 if (dependent_type_p (op
)
19631 || (code
== TRAIT_EXPR
19632 && dependent_type_p (TREE_OPERAND (*tp
, 1))))
19636 *walk_subtrees
= false;
19643 case COMPONENT_REF
:
19644 if (TREE_CODE (TREE_OPERAND (*tp
, 1)) == IDENTIFIER_NODE
)
19645 /* In a template, finish_class_member_access_expr creates a
19646 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
19647 type-dependent, so that we can check access control at
19648 instantiation time (PR 42277). See also Core issue 1273. */
19653 /* Similarly, finish_qualified_id_expr builds up a SCOPE_REF in a
19654 template so that we can check access at instantiation time even
19655 though we know which member it resolves to. */
19662 if (type_dependent_expression_p (*tp
))
19668 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
19669 sense defined by the ABI:
19671 "An expression is instantiation-dependent if it is type-dependent
19672 or value-dependent, or it has a subexpression that is type-dependent
19673 or value-dependent." */
19676 instantiation_dependent_expression_p (tree expression
)
19680 if (!processing_template_decl
)
19683 if (expression
== error_mark_node
)
19686 result
= cp_walk_tree_without_duplicates (&expression
,
19687 instantiation_dependent_r
, NULL
);
19688 return result
!= NULL_TREE
;
19691 /* Like type_dependent_expression_p, but it also works while not processing
19692 a template definition, i.e. during substitution or mangling. */
19695 type_dependent_expression_p_push (tree expr
)
19698 ++processing_template_decl
;
19699 b
= type_dependent_expression_p (expr
);
19700 --processing_template_decl
;
19704 /* Returns TRUE if ARGS contains a type-dependent expression. */
19707 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
19712 FOR_EACH_VEC_ELT (tree
, args
, i
, arg
)
19714 if (type_dependent_expression_p (arg
))
19720 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19721 expressions) contains any type-dependent expressions. */
19724 any_type_dependent_elements_p (const_tree list
)
19726 for (; list
; list
= TREE_CHAIN (list
))
19727 if (value_dependent_expression_p (TREE_VALUE (list
)))
19733 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19734 expressions) contains any value-dependent expressions. */
19737 any_value_dependent_elements_p (const_tree list
)
19739 for (; list
; list
= TREE_CHAIN (list
))
19740 if (value_dependent_expression_p (TREE_VALUE (list
)))
19746 /* Returns TRUE if the ARG (a template argument) is dependent. */
19749 dependent_template_arg_p (tree arg
)
19751 if (!processing_template_decl
)
19754 /* Assume a template argument that was wrongly written by the user
19755 is dependent. This is consistent with what
19756 any_dependent_template_arguments_p [that calls this function]
19758 if (!arg
|| arg
== error_mark_node
)
19761 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
19762 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
19764 if (TREE_CODE (arg
) == TEMPLATE_DECL
19765 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19766 return dependent_template_p (arg
);
19767 else if (ARGUMENT_PACK_P (arg
))
19769 tree args
= ARGUMENT_PACK_ARGS (arg
);
19770 int i
, len
= TREE_VEC_LENGTH (args
);
19771 for (i
= 0; i
< len
; ++i
)
19773 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19779 else if (TYPE_P (arg
))
19780 return dependent_type_p (arg
);
19782 return (type_dependent_expression_p (arg
)
19783 || value_dependent_expression_p (arg
));
19786 /* Returns true if ARGS (a collection of template arguments) contains
19787 any types that require structural equality testing. */
19790 any_template_arguments_need_structural_equality_p (tree args
)
19797 if (args
== error_mark_node
)
19800 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19802 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19803 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19805 tree arg
= TREE_VEC_ELT (level
, j
);
19806 tree packed_args
= NULL_TREE
;
19809 if (ARGUMENT_PACK_P (arg
))
19811 /* Look inside the argument pack. */
19812 packed_args
= ARGUMENT_PACK_ARGS (arg
);
19813 len
= TREE_VEC_LENGTH (packed_args
);
19816 for (k
= 0; k
< len
; ++k
)
19819 arg
= TREE_VEC_ELT (packed_args
, k
);
19821 if (error_operand_p (arg
))
19823 else if (TREE_CODE (arg
) == TEMPLATE_DECL
19824 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19826 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
19828 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
19829 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
19838 /* Returns true if ARGS (a collection of template arguments) contains
19839 any dependent arguments. */
19842 any_dependent_template_arguments_p (const_tree args
)
19849 if (args
== error_mark_node
)
19852 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19854 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19855 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19856 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
19863 /* Returns TRUE if the template TMPL is dependent. */
19866 dependent_template_p (tree tmpl
)
19868 if (TREE_CODE (tmpl
) == OVERLOAD
)
19872 if (dependent_template_p (OVL_CURRENT (tmpl
)))
19874 tmpl
= OVL_NEXT (tmpl
);
19879 /* Template template parameters are dependent. */
19880 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
19881 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
19883 /* So are names that have not been looked up. */
19884 if (TREE_CODE (tmpl
) == SCOPE_REF
19885 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
19887 /* So are member templates of dependent classes. */
19888 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
19889 return dependent_type_p (DECL_CONTEXT (tmpl
));
19893 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19896 dependent_template_id_p (tree tmpl
, tree args
)
19898 return (dependent_template_p (tmpl
)
19899 || any_dependent_template_arguments_p (args
));
19902 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19906 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
19910 if (!processing_template_decl
)
19913 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
19915 tree decl
= TREE_VEC_ELT (declv
, i
);
19916 tree init
= TREE_VEC_ELT (initv
, i
);
19917 tree cond
= TREE_VEC_ELT (condv
, i
);
19918 tree incr
= TREE_VEC_ELT (incrv
, i
);
19920 if (type_dependent_expression_p (decl
))
19923 if (init
&& type_dependent_expression_p (init
))
19926 if (type_dependent_expression_p (cond
))
19929 if (COMPARISON_CLASS_P (cond
)
19930 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
19931 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
19934 if (TREE_CODE (incr
) == MODOP_EXPR
)
19936 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
19937 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
19940 else if (type_dependent_expression_p (incr
))
19942 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
19944 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
19946 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
19948 tree t
= TREE_OPERAND (incr
, 1);
19949 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
19950 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
19959 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
19960 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
19961 no such TYPE can be found. Note that this function peers inside
19962 uninstantiated templates and therefore should be used only in
19963 extremely limited situations. ONLY_CURRENT_P restricts this
19964 peering to the currently open classes hierarchy (which is required
19965 when comparing types). */
19968 resolve_typename_type (tree type
, bool only_current_p
)
19977 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
19979 scope
= TYPE_CONTEXT (type
);
19980 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
19981 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
19982 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
19983 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
19984 identifier of the TYPENAME_TYPE anymore.
19985 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
19986 TYPENAME_TYPE instead, we avoid messing up with a possible
19987 typedef variant case. */
19988 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
19990 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
19991 it first before we can figure out what NAME refers to. */
19992 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
19993 scope
= resolve_typename_type (scope
, only_current_p
);
19994 /* If we don't know what SCOPE refers to, then we cannot resolve the
19996 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
19998 /* If the SCOPE is a template type parameter, we have no way of
19999 resolving the name. */
20000 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20002 /* If the SCOPE is not the current instantiation, there's no reason
20003 to look inside it. */
20004 if (only_current_p
&& !currently_open_class (scope
))
20006 /* If this is a typedef, we don't want to look inside (c++/11987). */
20007 if (typedef_variant_p (type
))
20009 /* If SCOPE isn't the template itself, it will not have a valid
20010 TYPE_FIELDS list. */
20011 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20012 /* scope is either the template itself or a compatible instantiation
20013 like X<T>, so look up the name in the original template. */
20014 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20016 /* scope is a partial instantiation, so we can't do the lookup or we
20017 will lose the template arguments. */
20019 /* Enter the SCOPE so that name lookup will be resolved as if we
20020 were in the class definition. In particular, SCOPE will no
20021 longer be considered a dependent type. */
20022 pushed_scope
= push_scope (scope
);
20023 /* Look up the declaration. */
20024 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20025 tf_warning_or_error
);
20027 result
= NULL_TREE
;
20029 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20030 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20033 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
20034 && TREE_CODE (decl
) == TYPE_DECL
)
20036 result
= TREE_TYPE (decl
);
20037 if (result
== error_mark_node
)
20038 result
= NULL_TREE
;
20040 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20041 && DECL_CLASS_TEMPLATE_P (decl
))
20045 /* Obtain the template and the arguments. */
20046 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20047 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20048 /* Instantiate the template. */
20049 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20050 /*entering_scope=*/0,
20051 tf_error
| tf_user
);
20052 if (result
== error_mark_node
)
20053 result
= NULL_TREE
;
20056 /* Leave the SCOPE. */
20058 pop_scope (pushed_scope
);
20060 /* If we failed to resolve it, return the original typename. */
20064 /* If lookup found a typename type, resolve that too. */
20065 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20067 /* Ill-formed programs can cause infinite recursion here, so we
20068 must catch that. */
20069 TYPENAME_IS_RESOLVING_P (type
) = 1;
20070 result
= resolve_typename_type (result
, only_current_p
);
20071 TYPENAME_IS_RESOLVING_P (type
) = 0;
20074 /* Qualify the resulting type. */
20075 quals
= cp_type_quals (type
);
20077 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20082 /* EXPR is an expression which is not type-dependent. Return a proxy
20083 for EXPR that can be used to compute the types of larger
20084 expressions containing EXPR. */
20087 build_non_dependent_expr (tree expr
)
20091 #ifdef ENABLE_CHECKING
20092 /* Try to get a constant value for all non-type-dependent expressions in
20093 order to expose bugs in *_dependent_expression_p and constexpr. */
20094 if (cxx_dialect
>= cxx0x
)
20095 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20098 /* Preserve OVERLOADs; the functions must be available to resolve
20101 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20102 inner_expr
= stmt_expr_value_expr (inner_expr
);
20103 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20104 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20105 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20106 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20107 if (is_overloaded_fn (inner_expr
)
20108 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20110 /* There is no need to return a proxy for a variable. */
20111 if (TREE_CODE (expr
) == VAR_DECL
)
20113 /* Preserve string constants; conversions from string constants to
20114 "char *" are allowed, even though normally a "const char *"
20115 cannot be used to initialize a "char *". */
20116 if (TREE_CODE (expr
) == STRING_CST
)
20118 /* Preserve arithmetic constants, as an optimization -- there is no
20119 reason to create a new node. */
20120 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20122 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20123 There is at least one place where we want to know that a
20124 particular expression is a throw-expression: when checking a ?:
20125 expression, there are special rules if the second or third
20126 argument is a throw-expression. */
20127 if (TREE_CODE (expr
) == THROW_EXPR
)
20130 /* Don't wrap an initializer list, we need to be able to look inside. */
20131 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20134 /* Don't wrap a dummy object, we need to be able to test for it. */
20135 if (is_dummy_object (expr
))
20138 if (TREE_CODE (expr
) == COND_EXPR
)
20139 return build3 (COND_EXPR
,
20141 TREE_OPERAND (expr
, 0),
20142 (TREE_OPERAND (expr
, 1)
20143 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
20144 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
20145 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
20146 if (TREE_CODE (expr
) == COMPOUND_EXPR
20147 && !COMPOUND_EXPR_OVERLOADED (expr
))
20148 return build2 (COMPOUND_EXPR
,
20150 TREE_OPERAND (expr
, 0),
20151 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
20153 /* If the type is unknown, it can't really be non-dependent */
20154 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
20156 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20157 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
20160 /* ARGS is a vector of expressions as arguments to a function call.
20161 Replace the arguments with equivalent non-dependent expressions.
20162 This modifies ARGS in place. */
20165 make_args_non_dependent (VEC(tree
,gc
) *args
)
20170 FOR_EACH_VEC_ELT (tree
, args
, ix
, arg
)
20172 tree newarg
= build_non_dependent_expr (arg
);
20174 VEC_replace (tree
, args
, ix
, newarg
);
20178 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20179 with a level one deeper than the actual template parms. */
20184 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
20185 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
20186 TYPE_DECL
, get_identifier ("auto"), au
);
20187 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
20188 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
20189 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
20190 TYPE_NAME (au
), NULL_TREE
);
20191 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
20192 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
20193 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
20198 /* Given type ARG, return std::initializer_list<ARG>. */
20203 tree std_init_list
= namespace_binding
20204 (get_identifier ("initializer_list"), std_node
);
20206 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
20208 error ("deducing from brace-enclosed initializer list requires "
20209 "#include <initializer_list>");
20210 return error_mark_node
;
20212 argvec
= make_tree_vec (1);
20213 TREE_VEC_ELT (argvec
, 0) = arg
;
20214 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
20215 NULL_TREE
, 0, tf_warning_or_error
);
20218 /* Replace auto in TYPE with std::initializer_list<auto>. */
20221 listify_autos (tree type
, tree auto_node
)
20223 tree init_auto
= listify (auto_node
);
20224 tree argvec
= make_tree_vec (1);
20225 TREE_VEC_ELT (argvec
, 0) = init_auto
;
20226 if (processing_template_decl
)
20227 argvec
= add_to_template_args (current_template_args (), argvec
);
20228 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20231 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20232 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20235 do_auto_deduction (tree type
, tree init
, tree auto_node
)
20237 tree parms
, tparms
, targs
;
20241 if (init
== error_mark_node
)
20242 return error_mark_node
;
20244 if (type_dependent_expression_p (init
))
20245 /* Defining a subset of type-dependent expressions that we can deduce
20246 from ahead of time isn't worth the trouble. */
20249 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20250 with either a new invented type template parameter U or, if the
20251 initializer is a braced-init-list (8.5.4), with
20252 std::initializer_list<U>. */
20253 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
20254 type
= listify_autos (type
, auto_node
);
20256 init
= resolve_nondeduced_context (init
);
20258 parms
= build_tree_list (NULL_TREE
, type
);
20260 tparms
= make_tree_vec (1);
20261 targs
= make_tree_vec (1);
20262 TREE_VEC_ELT (tparms
, 0)
20263 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20264 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20265 DEDUCE_CALL
, LOOKUP_NORMAL
,
20266 /*explain_p=*/false);
20269 if (processing_template_decl
)
20270 /* Try again at instantiation time. */
20272 if (type
&& type
!= error_mark_node
)
20273 /* If type is error_mark_node a diagnostic must have been
20274 emitted by now. Also, having a mention to '<type error>'
20275 in the diagnostic is not really useful to the user. */
20277 if (cfun
&& auto_node
== current_function_auto_return_pattern
20278 && LAMBDA_FUNCTION_P (current_function_decl
))
20279 error ("unable to deduce lambda return type from %qE", init
);
20281 error ("unable to deduce %qT from %qE", type
, init
);
20283 return error_mark_node
;
20286 /* If the list of declarators contains more than one declarator, the type
20287 of each declared variable is determined as described above. If the
20288 type deduced for the template parameter U is not the same in each
20289 deduction, the program is ill-formed. */
20290 if (TREE_TYPE (auto_node
)
20291 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20293 if (cfun
&& auto_node
== current_function_auto_return_pattern
20294 && LAMBDA_FUNCTION_P (current_function_decl
))
20295 error ("inconsistent types %qT and %qT deduced for "
20296 "lambda return type", TREE_TYPE (auto_node
),
20297 TREE_VEC_ELT (targs
, 0));
20299 error ("inconsistent deduction for %qT: %qT and then %qT",
20300 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20301 return error_mark_node
;
20303 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20305 if (processing_template_decl
)
20306 targs
= add_to_template_args (current_template_args (), targs
);
20307 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20310 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20314 splice_late_return_type (tree type
, tree late_return_type
)
20318 if (late_return_type
== NULL_TREE
)
20320 argvec
= make_tree_vec (1);
20321 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20322 if (processing_template_parmlist
)
20323 /* For a late-specified return type in a template type-parameter, we
20324 need to add a dummy argument level for its parmlist. */
20325 argvec
= add_to_template_args
20326 (make_tree_vec (processing_template_parmlist
), argvec
);
20327 if (current_template_parms
)
20328 argvec
= add_to_template_args (current_template_args (), argvec
);
20329 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20332 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20335 is_auto (const_tree type
)
20337 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20338 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20344 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20345 appear as a type-specifier for the declaration in question, we don't
20346 have to look through the whole type. */
20349 type_uses_auto (tree type
)
20351 enum tree_code code
;
20352 if (is_auto (type
))
20355 code
= TREE_CODE (type
);
20357 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20358 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20359 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20360 return type_uses_auto (TREE_TYPE (type
));
20362 if (TYPE_PTRMEMFUNC_P (type
))
20363 return type_uses_auto (TREE_TYPE (TREE_TYPE
20364 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20369 /* For a given template T, return the vector of typedefs referenced
20370 in T for which access check is needed at T instantiation time.
20371 T is either a FUNCTION_DECL or a RECORD_TYPE.
20372 Those typedefs were added to T by the function
20373 append_type_to_template_for_access_check. */
20375 VEC(qualified_typedef_usage_t
,gc
)*
20376 get_types_needing_access_check (tree t
)
20379 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
20381 if (!t
|| t
== error_mark_node
)
20384 if (!(ti
= get_template_info (t
)))
20387 if (CLASS_TYPE_P (t
)
20388 || TREE_CODE (t
) == FUNCTION_DECL
)
20390 if (!TI_TEMPLATE (ti
))
20393 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20399 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20400 tied to T. That list of typedefs will be access checked at
20401 T instantiation time.
20402 T is either a FUNCTION_DECL or a RECORD_TYPE.
20403 TYPE_DECL is a TYPE_DECL node representing a typedef.
20404 SCOPE is the scope through which TYPE_DECL is accessed.
20405 LOCATION is the location of the usage point of TYPE_DECL.
20407 This function is a subroutine of
20408 append_type_to_template_for_access_check. */
20411 append_type_to_template_for_access_check_1 (tree t
,
20414 location_t location
)
20416 qualified_typedef_usage_t typedef_usage
;
20419 if (!t
|| t
== error_mark_node
)
20422 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20423 || CLASS_TYPE_P (t
))
20425 && TREE_CODE (type_decl
) == TYPE_DECL
20428 if (!(ti
= get_template_info (t
)))
20431 gcc_assert (TI_TEMPLATE (ti
));
20433 typedef_usage
.typedef_decl
= type_decl
;
20434 typedef_usage
.context
= scope
;
20435 typedef_usage
.locus
= location
;
20437 VEC_safe_push (qualified_typedef_usage_t
, gc
,
20438 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
20442 /* Append TYPE_DECL to the template TEMPL.
20443 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20444 At TEMPL instanciation time, TYPE_DECL will be checked to see
20445 if it can be accessed through SCOPE.
20446 LOCATION is the location of the usage point of TYPE_DECL.
20448 e.g. consider the following code snippet:
20455 template<class U> struct S
20457 C::myint mi; // <-- usage point of the typedef C::myint
20462 At S<char> instantiation time, we need to check the access of C::myint
20463 In other words, we need to check the access of the myint typedef through
20464 the C scope. For that purpose, this function will add the myint typedef
20465 and the scope C through which its being accessed to a list of typedefs
20466 tied to the template S. That list will be walked at template instantiation
20467 time and access check performed on each typedefs it contains.
20468 Note that this particular code snippet should yield an error because
20469 myint is private to C. */
20472 append_type_to_template_for_access_check (tree templ
,
20475 location_t location
)
20477 qualified_typedef_usage_t
*iter
;
20480 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20482 /* Make sure we don't append the type to the template twice. */
20483 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
20484 get_types_needing_access_check (templ
),
20486 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20489 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20493 /* Set up the hash tables for template instantiations. */
20496 init_template_processing (void)
20498 decl_specializations
= htab_create_ggc (37,
20499 hash_specialization
,
20500 eq_specializations
,
20502 type_specializations
= htab_create_ggc (37,
20503 hash_specialization
,
20504 eq_specializations
,
20508 /* Print stats about the template hash tables for -fstats. */
20511 print_template_statistics (void)
20513 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20514 "%f collisions\n", (long) htab_size (decl_specializations
),
20515 (long) htab_elements (decl_specializations
),
20516 htab_collisions (decl_specializations
));
20517 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20518 "%f collisions\n", (long) htab_size (type_specializations
),
20519 (long) htab_elements (type_specializations
),
20520 htab_collisions (type_specializations
));
20523 #include "gt-cp-pt.h"