1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t
) (tree
, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
{
57 struct pending_template
*next
;
58 struct tinst_level
*tinst
;
61 static GTY(()) struct pending_template
*pending_templates
;
62 static GTY(()) struct pending_template
*last_pending_template
;
64 int processing_template_parmlist
;
65 static int template_header_count
;
67 static GTY(()) tree saved_trees
;
68 static VEC(int,heap
) *inline_parm_levels
;
70 static GTY(()) struct tinst_level
*current_tinst_level
;
72 static GTY(()) tree saved_access_scope
;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr
;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
82 static struct pointer_map_t
*local_specializations
;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry
)))
92 htab_t decl_specializations
;
94 static GTY ((param_is (spec_entry
)))
95 htab_t type_specializations
;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 enum template_base_result
{
114 tbr_ambiguous_baseclass
,
118 static void push_access_scope (tree
);
119 static void pop_access_scope (tree
);
120 static void push_deduction_access_scope (tree
);
121 static void pop_deduction_access_scope (tree
);
122 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
123 unification_kind_t
, int,
125 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
126 unification_kind_t
, int, bool, bool);
127 static int unify (tree
, tree
, tree
, tree
, int, bool);
128 static void add_pending_template (tree
);
129 static tree
reopen_tinst_level (struct tinst_level
*);
130 static tree
tsubst_initializer_list (tree
, tree
);
131 static tree
get_class_bindings (tree
, tree
, tree
);
132 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
134 static void tsubst_enum (tree
, tree
, tree
);
135 static tree
add_to_template_args (tree
, tree
);
136 static tree
add_outermost_template_args (tree
, tree
);
137 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
138 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
140 static int type_unification_real (tree
, tree
, tree
, const tree
*,
141 unsigned int, int, unification_kind_t
, int,
143 static void note_template_header (int);
144 static tree
convert_nontype_argument_function (tree
, tree
);
145 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
146 static tree
convert_template_argument (tree
, tree
, tree
,
147 tsubst_flags_t
, int, tree
);
148 static int for_each_template_parm (tree
, tree_fn_t
, void*,
149 struct pointer_set_t
*, bool);
150 static tree
expand_template_argument_pack (tree
);
151 static tree
build_template_parm_index (int, int, int, int, tree
, tree
);
152 static bool inline_needs_template_parms (tree
);
153 static void push_inline_template_parms_recursive (tree
, int);
154 static tree
retrieve_local_specialization (tree
);
155 static void register_local_specialization (tree
, tree
);
156 static hashval_t
hash_specialization (const void *p
);
157 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
158 static int mark_template_parm (tree
, void *);
159 static int template_parm_this_level_p (tree
, void *);
160 static tree
tsubst_friend_function (tree
, tree
);
161 static tree
tsubst_friend_class (tree
, tree
);
162 static int can_complete_type_without_circularity (tree
);
163 static tree
get_bindings (tree
, tree
, tree
, bool);
164 static int template_decl_level (tree
);
165 static int check_cv_quals_for_unify (int, tree
, tree
);
166 static void template_parm_level_and_index (tree
, int*, int*);
167 static int unify_pack_expansion (tree
, tree
, tree
,
168 tree
, unification_kind_t
, bool, bool);
169 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
170 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
171 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
172 static void regenerate_decl_from_template (tree
, tree
);
173 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
174 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
175 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
176 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
177 static bool check_specialization_scope (void);
178 static tree
process_partial_specialization (tree
);
179 static void set_current_access_from_decl (tree
);
180 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
182 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
183 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
185 static bool template_template_parm_bindings_ok_p (tree
, tree
);
186 static int template_args_equal (tree
, tree
);
187 static void tsubst_default_arguments (tree
);
188 static tree
for_each_template_parm_r (tree
*, int *, void *);
189 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
190 static void copy_default_args_to_explicit_spec (tree
);
191 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
192 static bool dependent_template_arg_p (tree
);
193 static bool any_template_arguments_need_structural_equality_p (tree
);
194 static bool dependent_type_p_r (tree
);
195 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
196 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
198 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
199 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
200 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
202 static tree
listify (tree
);
203 static tree
listify_autos (tree
, tree
);
204 static tree
template_parm_to_arg (tree t
);
205 static bool arg_from_parm_pack_p (tree
, tree
);
206 static tree
current_template_args (void);
207 static tree
fixup_template_type_parm_type (tree
, int);
208 static tree
fixup_template_parm_index (tree
, tree
, int);
209 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
211 /* Make the current scope suitable for access checking when we are
212 processing T. T can be FUNCTION_DECL for instantiated function
213 template, or VAR_DECL for static member variable (need by
214 instantiate_decl). */
217 push_access_scope (tree t
)
219 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
220 || TREE_CODE (t
) == VAR_DECL
);
222 if (DECL_FRIEND_CONTEXT (t
))
223 push_nested_class (DECL_FRIEND_CONTEXT (t
));
224 else if (DECL_CLASS_SCOPE_P (t
))
225 push_nested_class (DECL_CONTEXT (t
));
227 push_to_top_level ();
229 if (TREE_CODE (t
) == FUNCTION_DECL
)
231 saved_access_scope
= tree_cons
232 (NULL_TREE
, current_function_decl
, saved_access_scope
);
233 current_function_decl
= t
;
237 /* Restore the scope set up by push_access_scope. T is the node we
241 pop_access_scope (tree t
)
243 if (TREE_CODE (t
) == FUNCTION_DECL
)
245 current_function_decl
= TREE_VALUE (saved_access_scope
);
246 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
249 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
252 pop_from_top_level ();
255 /* Do any processing required when DECL (a member template
256 declaration) is finished. Returns the TEMPLATE_DECL corresponding
257 to DECL, unless it is a specialization, in which case the DECL
258 itself is returned. */
261 finish_member_template_decl (tree decl
)
263 if (decl
== error_mark_node
)
264 return error_mark_node
;
266 gcc_assert (DECL_P (decl
));
268 if (TREE_CODE (decl
) == TYPE_DECL
)
272 type
= TREE_TYPE (decl
);
273 if (type
== error_mark_node
)
274 return error_mark_node
;
275 if (MAYBE_CLASS_TYPE_P (type
)
276 && CLASSTYPE_TEMPLATE_INFO (type
)
277 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
279 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
280 check_member_template (tmpl
);
285 else if (TREE_CODE (decl
) == FIELD_DECL
)
286 error ("data member %qD cannot be a member template", decl
);
287 else if (DECL_TEMPLATE_INFO (decl
))
289 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
291 check_member_template (DECL_TI_TEMPLATE (decl
));
292 return DECL_TI_TEMPLATE (decl
);
298 error ("invalid member template declaration %qD", decl
);
300 return error_mark_node
;
303 /* Create a template info node. */
306 build_template_info (tree template_decl
, tree template_args
)
308 tree result
= make_node (TEMPLATE_INFO
);
309 TI_TEMPLATE (result
) = template_decl
;
310 TI_ARGS (result
) = template_args
;
314 /* Return the template info node corresponding to T, whatever T is. */
317 get_template_info (const_tree t
)
319 tree tinfo
= NULL_TREE
;
321 if (!t
|| t
== error_mark_node
)
324 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
325 tinfo
= DECL_TEMPLATE_INFO (t
);
327 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
330 if (TAGGED_TYPE_P (t
))
331 tinfo
= TYPE_TEMPLATE_INFO (t
);
332 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
333 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
338 /* Returns the template nesting level of the indicated class TYPE.
348 A<T>::B<U> has depth two, while A<T> has depth one.
349 Both A<T>::B<int> and A<int>::B<U> have depth one, if
350 they are instantiations, not specializations.
352 This function is guaranteed to return 0 if passed NULL_TREE so
353 that, for example, `template_class_depth (current_class_type)' is
357 template_class_depth (tree type
)
362 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
363 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
364 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
366 tree tinfo
= get_template_info (type
);
368 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
369 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
376 /* Subroutine of maybe_begin_member_template_processing.
377 Returns true if processing DECL needs us to push template parms. */
380 inline_needs_template_parms (tree decl
)
382 if (! DECL_TEMPLATE_INFO (decl
))
385 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
386 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
389 /* Subroutine of maybe_begin_member_template_processing.
390 Push the template parms in PARMS, starting from LEVELS steps into the
391 chain, and ending at the beginning, since template parms are listed
395 push_inline_template_parms_recursive (tree parmlist
, int levels
)
397 tree parms
= TREE_VALUE (parmlist
);
401 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
403 ++processing_template_decl
;
404 current_template_parms
405 = tree_cons (size_int (processing_template_decl
),
406 parms
, current_template_parms
);
407 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
409 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
411 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
413 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
415 if (parm
== error_mark_node
)
418 gcc_assert (DECL_P (parm
));
420 switch (TREE_CODE (parm
))
429 /* Make a CONST_DECL as is done in process_template_parm.
430 It is ugly that we recreate this here; the original
431 version built in process_template_parm is no longer
433 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
434 CONST_DECL
, DECL_NAME (parm
),
436 DECL_ARTIFICIAL (decl
) = 1;
437 TREE_CONSTANT (decl
) = 1;
438 TREE_READONLY (decl
) = 1;
439 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
440 SET_DECL_TEMPLATE_PARM_P (decl
);
451 /* Restore the template parameter context for a member template or
452 a friend template defined in a class definition. */
455 maybe_begin_member_template_processing (tree decl
)
460 if (inline_needs_template_parms (decl
))
462 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
463 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
465 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
468 parms
= TREE_CHAIN (parms
);
471 push_inline_template_parms_recursive (parms
, levels
);
474 /* Remember how many levels of template parameters we pushed so that
475 we can pop them later. */
476 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
479 /* Undo the effects of maybe_begin_member_template_processing. */
482 maybe_end_member_template_processing (void)
487 if (VEC_length (int, inline_parm_levels
) == 0)
490 last
= VEC_pop (int, inline_parm_levels
);
491 for (i
= 0; i
< last
; ++i
)
493 --processing_template_decl
;
494 current_template_parms
= TREE_CHAIN (current_template_parms
);
499 /* Return a new template argument vector which contains all of ARGS,
500 but has as its innermost set of arguments the EXTRA_ARGS. */
503 add_to_template_args (tree args
, tree extra_args
)
510 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
513 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
514 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
516 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
517 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
519 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
520 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
525 /* Like add_to_template_args, but only the outermost ARGS are added to
526 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
527 (EXTRA_ARGS) levels are added. This function is used to combine
528 the template arguments from a partial instantiation with the
529 template arguments used to attain the full instantiation from the
530 partial instantiation. */
533 add_outermost_template_args (tree args
, tree extra_args
)
537 /* If there are more levels of EXTRA_ARGS than there are ARGS,
538 something very fishy is going on. */
539 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
541 /* If *all* the new arguments will be the EXTRA_ARGS, just return
543 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
546 /* For the moment, we make ARGS look like it contains fewer levels. */
547 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
549 new_args
= add_to_template_args (args
, extra_args
);
551 /* Now, we restore ARGS to its full dimensions. */
552 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
557 /* Return the N levels of innermost template arguments from the ARGS. */
560 get_innermost_template_args (tree args
, int n
)
568 /* If N is 1, just return the innermost set of template arguments. */
570 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
572 /* If we're not removing anything, just return the arguments we were
574 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
575 gcc_assert (extra_levels
>= 0);
576 if (extra_levels
== 0)
579 /* Make a new set of arguments, not containing the outer arguments. */
580 new_args
= make_tree_vec (n
);
581 for (i
= 1; i
<= n
; ++i
)
582 SET_TMPL_ARGS_LEVEL (new_args
, i
,
583 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
588 /* The inverse of get_innermost_template_args: Return all but the innermost
589 EXTRA_LEVELS levels of template arguments from the ARGS. */
592 strip_innermost_template_args (tree args
, int extra_levels
)
595 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
600 /* If N is 1, just return the outermost set of template arguments. */
602 return TMPL_ARGS_LEVEL (args
, 1);
604 /* If we're not removing anything, just return the arguments we were
606 gcc_assert (extra_levels
>= 0);
607 if (extra_levels
== 0)
610 /* Make a new set of arguments, not containing the inner arguments. */
611 new_args
= make_tree_vec (n
);
612 for (i
= 1; i
<= n
; ++i
)
613 SET_TMPL_ARGS_LEVEL (new_args
, i
,
614 TMPL_ARGS_LEVEL (args
, i
));
619 /* We've got a template header coming up; push to a new level for storing
623 begin_template_parm_list (void)
625 /* We use a non-tag-transparent scope here, which causes pushtag to
626 put tags in this scope, rather than in the enclosing class or
627 namespace scope. This is the right thing, since we want
628 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
629 global template class, push_template_decl handles putting the
630 TEMPLATE_DECL into top-level scope. For a nested template class,
633 template <class T> struct S1 {
634 template <class T> struct S2 {};
637 pushtag contains special code to call pushdecl_with_scope on the
638 TEMPLATE_DECL for S2. */
639 begin_scope (sk_template_parms
, NULL
);
640 ++processing_template_decl
;
641 ++processing_template_parmlist
;
642 note_template_header (0);
645 /* This routine is called when a specialization is declared. If it is
646 invalid to declare a specialization here, an error is reported and
647 false is returned, otherwise this routine will return true. */
650 check_specialization_scope (void)
652 tree scope
= current_scope ();
656 An explicit specialization shall be declared in the namespace of
657 which the template is a member, or, for member templates, in the
658 namespace of which the enclosing class or enclosing class
659 template is a member. An explicit specialization of a member
660 function, member class or static data member of a class template
661 shall be declared in the namespace of which the class template
663 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
665 error ("explicit specialization in non-namespace scope %qD", scope
);
671 In an explicit specialization declaration for a member of a class
672 template or a member template that appears in namespace scope,
673 the member template and some of its enclosing class templates may
674 remain unspecialized, except that the declaration shall not
675 explicitly specialize a class member template if its enclosing
676 class templates are not explicitly specialized as well. */
677 if (current_template_parms
)
679 error ("enclosing class templates are not explicitly specialized");
686 /* We've just seen template <>. */
689 begin_specialization (void)
691 begin_scope (sk_template_spec
, NULL
);
692 note_template_header (1);
693 return check_specialization_scope ();
696 /* Called at then end of processing a declaration preceded by
700 end_specialization (void)
703 reset_specialization ();
706 /* Any template <>'s that we have seen thus far are not referring to a
707 function specialization. */
710 reset_specialization (void)
712 processing_specialization
= 0;
713 template_header_count
= 0;
716 /* We've just seen a template header. If SPECIALIZATION is nonzero,
717 it was of the form template <>. */
720 note_template_header (int specialization
)
722 processing_specialization
= specialization
;
723 template_header_count
++;
726 /* We're beginning an explicit instantiation. */
729 begin_explicit_instantiation (void)
731 gcc_assert (!processing_explicit_instantiation
);
732 processing_explicit_instantiation
= true;
737 end_explicit_instantiation (void)
739 gcc_assert (processing_explicit_instantiation
);
740 processing_explicit_instantiation
= false;
743 /* An explicit specialization or partial specialization TMPL is being
744 declared. Check that the namespace in which the specialization is
745 occurring is permissible. Returns false iff it is invalid to
746 specialize TMPL in the current namespace. */
749 check_specialization_namespace (tree tmpl
)
751 tree tpl_ns
= decl_namespace_context (tmpl
);
755 An explicit specialization shall be declared in the namespace of
756 which the template is a member, or, for member templates, in the
757 namespace of which the enclosing class or enclosing class
758 template is a member. An explicit specialization of a member
759 function, member class or static data member of a class template
760 shall be declared in the namespace of which the class template is
762 if (current_scope() != DECL_CONTEXT (tmpl
)
763 && !at_namespace_scope_p ())
765 error ("specialization of %qD must appear at namespace scope", tmpl
);
768 if (is_associated_namespace (current_namespace
, tpl_ns
))
769 /* Same or super-using namespace. */
773 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
774 permerror (input_location
, " from definition of %q+#D", tmpl
);
779 /* SPEC is an explicit instantiation. Check that it is valid to
780 perform this explicit instantiation in the current namespace. */
783 check_explicit_instantiation_namespace (tree spec
)
787 /* DR 275: An explicit instantiation shall appear in an enclosing
788 namespace of its template. */
789 ns
= decl_namespace_context (spec
);
790 if (!is_ancestor (current_namespace
, ns
))
791 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
792 "(which does not enclose namespace %qD)",
793 spec
, current_namespace
, ns
);
796 /* The TYPE is being declared. If it is a template type, that means it
797 is a partial specialization. Do appropriate error-checking. */
800 maybe_process_partial_specialization (tree type
)
804 if (type
== error_mark_node
)
805 return error_mark_node
;
807 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
809 error ("name of class shadows template template parameter %qD",
811 return error_mark_node
;
814 context
= TYPE_CONTEXT (type
);
816 if ((CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
817 /* Consider non-class instantiations of alias templates as
820 && TYPE_TEMPLATE_INFO (type
)
821 && DECL_LANG_SPECIFIC (TYPE_NAME (type
))
822 && DECL_USE_TEMPLATE (TYPE_NAME (type
))))
824 /* This is for ordinary explicit specialization and partial
825 specialization of a template class such as:
827 template <> class C<int>;
831 template <class T> class C<T*>;
833 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
835 if (CLASS_TYPE_P (type
)
836 && CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
837 && !COMPLETE_TYPE_P (type
))
839 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
840 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
841 if (processing_template_decl
)
843 if (push_template_decl (TYPE_MAIN_DECL (type
))
845 return error_mark_node
;
848 else if (CLASS_TYPE_P (type
)
849 && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
850 error ("specialization of %qT after instantiation", type
);
852 if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
854 error ("partial specialization of alias template %qD",
855 TYPE_TI_TEMPLATE (type
));
856 return error_mark_node
;
859 else if (CLASS_TYPE_P (type
)
860 && !CLASSTYPE_USE_TEMPLATE (type
)
861 && CLASSTYPE_TEMPLATE_INFO (type
)
862 && context
&& CLASS_TYPE_P (context
)
863 && CLASSTYPE_TEMPLATE_INFO (context
))
865 /* This is for an explicit specialization of member class
866 template according to [temp.expl.spec/18]:
868 template <> template <class U> class C<int>::D;
870 The context `C<int>' must be an implicit instantiation.
871 Otherwise this is just a member class template declared
874 template <> class C<int> { template <class U> class D; };
875 template <> template <class U> class C<int>::D;
877 In the first case, `C<int>::D' is a specialization of `C<T>::D'
878 while in the second case, `C<int>::D' is a primary template
879 and `C<T>::D' may not exist. */
881 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
882 && !COMPLETE_TYPE_P (type
))
885 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
887 if (current_namespace
888 != decl_namespace_context (tmpl
))
890 permerror (input_location
, "specializing %q#T in different namespace", type
);
891 permerror (input_location
, " from definition of %q+#D", tmpl
);
894 /* Check for invalid specialization after instantiation:
896 template <> template <> class C<int>::D<int>;
897 template <> template <class U> class C<int>::D; */
899 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
900 t
; t
= TREE_CHAIN (t
))
902 tree inst
= TREE_VALUE (t
);
903 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
905 /* We already have a full specialization of this partial
906 instantiation. Reassign it to the new member
907 specialization template. */
912 elt
.tmpl
= most_general_template (tmpl
);
913 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
916 htab_remove_elt (type_specializations
, &elt
);
919 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
921 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
922 entry
= ggc_alloc_spec_entry ();
926 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
927 /* But if we've had an implicit instantiation, that's a
928 problem ([temp.expl.spec]/6). */
929 error ("specialization %qT after instantiation %qT",
933 /* Mark TYPE as a specialization. And as a result, we only
934 have one level of template argument for the innermost
936 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
937 CLASSTYPE_TI_ARGS (type
)
938 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
941 else if (processing_specialization
)
943 /* Someday C++0x may allow for enum template specialization. */
944 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
945 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
946 pedwarn (input_location
, OPT_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 (fn
);
1420 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1422 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1423 /* Dup decl failed, but this is a new definition. Set the
1424 line number so any errors match this new
1426 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1432 return duplicate_decls (spec
, fn
, is_friend
);
1434 /* A specialization must be declared in the same namespace as the
1435 template it is specializing. */
1436 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1437 && !check_specialization_namespace (tmpl
))
1438 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1440 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1442 spec_entry
*entry
= ggc_alloc_spec_entry ();
1443 gcc_assert (tmpl
&& args
&& spec
);
1446 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1447 && PRIMARY_TEMPLATE_P (tmpl
)
1448 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1449 /* TMPL is a forward declaration of a template function; keep a list
1450 of all specializations in case we need to reassign them to a friend
1451 template later in tsubst_friend_function. */
1452 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1453 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1459 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1460 TMPL and ARGS members, ignores SPEC. */
1463 eq_specializations (const void *p1
, const void *p2
)
1465 const spec_entry
*e1
= (const spec_entry
*)p1
;
1466 const spec_entry
*e2
= (const spec_entry
*)p2
;
1468 return (e1
->tmpl
== e2
->tmpl
1469 && comp_template_args (e1
->args
, e2
->args
));
1472 /* Returns a hash for a template TMPL and template arguments ARGS. */
1475 hash_tmpl_and_args (tree tmpl
, tree args
)
1477 hashval_t val
= DECL_UID (tmpl
);
1478 return iterative_hash_template_arg (args
, val
);
1481 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1485 hash_specialization (const void *p
)
1487 const spec_entry
*e
= (const spec_entry
*)p
;
1488 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1491 /* Recursively calculate a hash value for a template argument ARG, for use
1492 in the hash tables of template specializations. */
1495 iterative_hash_template_arg (tree arg
, hashval_t val
)
1497 unsigned HOST_WIDE_INT i
;
1498 enum tree_code code
;
1501 if (arg
== NULL_TREE
)
1502 return iterative_hash_object (arg
, val
);
1507 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1508 /* We can get one of these when re-hashing a previous entry in the middle
1509 of substituting into a pack expansion. Just look through it. */
1510 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1512 code
= TREE_CODE (arg
);
1513 tclass
= TREE_CODE_CLASS (code
);
1515 val
= iterative_hash_object (code
, val
);
1522 case IDENTIFIER_NODE
:
1523 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1527 int i
, len
= TREE_VEC_LENGTH (arg
);
1528 for (i
= 0; i
< len
; ++i
)
1529 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1533 case TYPE_PACK_EXPANSION
:
1534 case EXPR_PACK_EXPANSION
:
1535 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1536 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1538 case TYPE_ARGUMENT_PACK
:
1539 case NONTYPE_ARGUMENT_PACK
:
1540 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1543 for (; arg
; arg
= TREE_CHAIN (arg
))
1544 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1548 for (; arg
; arg
= OVL_NEXT (arg
))
1549 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1555 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1557 val
= iterative_hash_template_arg (field
, val
);
1558 val
= iterative_hash_template_arg (value
, val
);
1564 if (!DECL_ARTIFICIAL (arg
))
1566 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1567 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1569 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1572 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1575 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1576 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1578 case TEMPLATE_PARM_INDEX
:
1579 val
= iterative_hash_template_arg
1580 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1581 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1582 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1585 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1586 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1587 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1590 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1592 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1596 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1597 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1598 val
= iterative_hash_object (code
, val
);
1599 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1602 /* A lambda can't appear in a template arg, but don't crash on
1604 gcc_assert (seen_error ());
1608 case IMPLICIT_CONV_EXPR
:
1609 case STATIC_CAST_EXPR
:
1610 case REINTERPRET_CAST_EXPR
:
1611 case CONST_CAST_EXPR
:
1612 case DYNAMIC_CAST_EXPR
:
1614 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1615 /* Now hash operands as usual. */
1625 if (TYPE_CANONICAL (arg
))
1626 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1628 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1629 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1630 /* Otherwise just compare the types during lookup. */
1633 case tcc_declaration
:
1635 return iterative_hash_expr (arg
, val
);
1638 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1640 unsigned n
= cp_tree_operand_length (arg
);
1641 for (i
= 0; i
< n
; ++i
)
1642 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1650 /* Unregister the specialization SPEC as a specialization of TMPL.
1651 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1652 if the SPEC was listed as a specialization of TMPL.
1654 Note that SPEC has been ggc_freed, so we can't look inside it. */
1657 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1662 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1663 elt
.args
= TI_ARGS (tinfo
);
1664 elt
.spec
= NULL_TREE
;
1666 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1669 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1670 gcc_assert (new_spec
!= NULL_TREE
);
1671 entry
->spec
= new_spec
;
1678 /* Like register_specialization, but for local declarations. We are
1679 registering SPEC, an instantiation of TMPL. */
1682 register_local_specialization (tree spec
, tree tmpl
)
1686 slot
= pointer_map_insert (local_specializations
, tmpl
);
1690 /* TYPE is a class type. Returns true if TYPE is an explicitly
1691 specialized class. */
1694 explicit_class_specialization_p (tree type
)
1696 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1698 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1701 /* Print the list of functions at FNS, going through all the overloads
1702 for each element of the list. Alternatively, FNS can not be a
1703 TREE_LIST, in which case it will be printed together with all the
1706 MORE and *STR should respectively be FALSE and NULL when the function
1707 is called from the outside. They are used internally on recursive
1708 calls. print_candidates manages the two parameters and leaves NULL
1709 in *STR when it ends. */
1712 print_candidates_1 (tree fns
, bool more
, const char **str
)
1715 char *spaces
= NULL
;
1717 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1718 if (TREE_CODE (fn
) == TREE_LIST
)
1720 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1721 print_candidates_1 (TREE_VALUE (fn2
),
1722 TREE_CHAIN (fn2
) || more
, str
);
1728 /* Pick the prefix string. */
1729 if (!more
&& !OVL_NEXT (fns
))
1731 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1735 *str
= _("candidates are:");
1736 spaces
= get_spaces (*str
);
1738 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1739 *str
= spaces
? spaces
: *str
;
1749 /* Print the list of candidate FNS in an error message. FNS can also
1750 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1753 print_candidates (tree fns
)
1755 const char *str
= NULL
;
1756 print_candidates_1 (fns
, false, &str
);
1757 gcc_assert (str
== NULL
);
1760 /* Returns the template (one of the functions given by TEMPLATE_ID)
1761 which can be specialized to match the indicated DECL with the
1762 explicit template args given in TEMPLATE_ID. The DECL may be
1763 NULL_TREE if none is available. In that case, the functions in
1764 TEMPLATE_ID are non-members.
1766 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1767 specialization of a member template.
1769 The TEMPLATE_COUNT is the number of references to qualifying
1770 template classes that appeared in the name of the function. See
1771 check_explicit_specialization for a more accurate description.
1773 TSK indicates what kind of template declaration (if any) is being
1774 declared. TSK_TEMPLATE indicates that the declaration given by
1775 DECL, though a FUNCTION_DECL, has template parameters, and is
1776 therefore a template function.
1778 The template args (those explicitly specified and those deduced)
1779 are output in a newly created vector *TARGS_OUT.
1781 If it is impossible to determine the result, an error message is
1782 issued. The error_mark_node is returned to indicate failure. */
1785 determine_specialization (tree template_id
,
1788 int need_member_template
,
1794 tree explicit_targs
;
1795 tree candidates
= NULL_TREE
;
1796 /* A TREE_LIST of templates of which DECL may be a specialization.
1797 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1798 corresponding TREE_PURPOSE is the set of template arguments that,
1799 when used to instantiate the template, would produce a function
1800 with the signature of DECL. */
1801 tree templates
= NULL_TREE
;
1803 cp_binding_level
*b
;
1805 *targs_out
= NULL_TREE
;
1807 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1808 return error_mark_node
;
1810 fns
= TREE_OPERAND (template_id
, 0);
1811 explicit_targs
= TREE_OPERAND (template_id
, 1);
1813 if (fns
== error_mark_node
)
1814 return error_mark_node
;
1816 /* Check for baselinks. */
1817 if (BASELINK_P (fns
))
1818 fns
= BASELINK_FUNCTIONS (fns
);
1820 if (!is_overloaded_fn (fns
))
1822 error ("%qD is not a function template", fns
);
1823 return error_mark_node
;
1826 /* Count the number of template headers specified for this
1829 for (b
= current_binding_level
;
1830 b
->kind
== sk_template_parms
;
1834 for (; fns
; fns
= OVL_NEXT (fns
))
1836 tree fn
= OVL_CURRENT (fns
);
1838 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1840 tree decl_arg_types
;
1844 /* In case of explicit specialization, we need to check if
1845 the number of template headers appearing in the specialization
1846 is correct. This is usually done in check_explicit_specialization,
1847 but the check done there cannot be exhaustive when specializing
1848 member functions. Consider the following code:
1850 template <> void A<int>::f(int);
1851 template <> template <> void A<int>::f(int);
1853 Assuming that A<int> is not itself an explicit specialization
1854 already, the first line specializes "f" which is a non-template
1855 member function, whilst the second line specializes "f" which
1856 is a template member function. So both lines are syntactically
1857 correct, and check_explicit_specialization does not reject
1860 Here, we can do better, as we are matching the specialization
1861 against the declarations. We count the number of template
1862 headers, and we check if they match TEMPLATE_COUNT + 1
1863 (TEMPLATE_COUNT is the number of qualifying template classes,
1864 plus there must be another header for the member template
1867 Notice that if header_count is zero, this is not a
1868 specialization but rather a template instantiation, so there
1869 is no check we can perform here. */
1870 if (header_count
&& header_count
!= template_count
+ 1)
1873 /* Check that the number of template arguments at the
1874 innermost level for DECL is the same as for FN. */
1875 if (current_binding_level
->kind
== sk_template_parms
1876 && !current_binding_level
->explicit_spec_p
1877 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1878 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1879 (current_template_parms
))))
1882 /* DECL might be a specialization of FN. */
1883 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1884 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1886 /* For a non-static member function, we need to make sure
1887 that the const qualification is the same. Since
1888 get_bindings does not try to merge the "this" parameter,
1889 we must do the comparison explicitly. */
1890 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1891 && !same_type_p (TREE_VALUE (fn_arg_types
),
1892 TREE_VALUE (decl_arg_types
)))
1895 /* Skip the "this" parameter and, for constructors of
1896 classes with virtual bases, the VTT parameter. A
1897 full specialization of a constructor will have a VTT
1898 parameter, but a template never will. */
1900 = skip_artificial_parms_for (decl
, decl_arg_types
);
1902 = skip_artificial_parms_for (fn
, fn_arg_types
);
1904 /* Function templates cannot be specializations; there are
1905 no partial specializations of functions. Therefore, if
1906 the type of DECL does not match FN, there is no
1908 if (tsk
== tsk_template
)
1910 if (compparms (fn_arg_types
, decl_arg_types
))
1911 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1915 /* See whether this function might be a specialization of this
1917 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1920 /* We cannot deduce template arguments that when used to
1921 specialize TMPL will produce DECL. */
1924 /* Make sure that the deduced arguments actually work. */
1925 insttype
= tsubst (TREE_TYPE (fn
), targs
, tf_none
, NULL_TREE
);
1926 if (insttype
== error_mark_node
)
1929 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (insttype
));
1930 if (!compparms (fn_arg_types
, decl_arg_types
))
1933 /* Save this template, and the arguments deduced. */
1934 templates
= tree_cons (targs
, fn
, templates
);
1936 else if (need_member_template
)
1937 /* FN is an ordinary member function, and we need a
1938 specialization of a member template. */
1940 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1941 /* We can get IDENTIFIER_NODEs here in certain erroneous
1944 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1945 /* This is just an ordinary non-member function. Nothing can
1946 be a specialization of that. */
1948 else if (DECL_ARTIFICIAL (fn
))
1949 /* Cannot specialize functions that are created implicitly. */
1953 tree decl_arg_types
;
1955 /* This is an ordinary member function. However, since
1956 we're here, we can assume it's enclosing class is a
1957 template class. For example,
1959 template <typename T> struct S { void f(); };
1960 template <> void S<int>::f() {}
1962 Here, S<int>::f is a non-template, but S<int> is a
1963 template class. If FN has the same type as DECL, we
1964 might be in business. */
1966 if (!DECL_TEMPLATE_INFO (fn
))
1967 /* Its enclosing class is an explicit specialization
1968 of a template class. This is not a candidate. */
1971 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1972 TREE_TYPE (TREE_TYPE (fn
))))
1973 /* The return types differ. */
1976 /* Adjust the type of DECL in case FN is a static member. */
1977 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1978 if (DECL_STATIC_FUNCTION_P (fn
)
1979 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1980 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1982 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1985 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1989 if (templates
&& TREE_CHAIN (templates
))
1995 It is possible for a specialization with a given function
1996 signature to be instantiated from more than one function
1997 template. In such cases, explicit specification of the
1998 template arguments must be used to uniquely identify the
1999 function template specialization being specialized.
2001 Note that here, there's no suggestion that we're supposed to
2002 determine which of the candidate templates is most
2003 specialized. However, we, also have:
2007 Partial ordering of overloaded function template
2008 declarations is used in the following contexts to select
2009 the function template to which a function template
2010 specialization refers:
2012 -- when an explicit specialization refers to a function
2015 So, we do use the partial ordering rules, at least for now.
2016 This extension can only serve to make invalid programs valid,
2017 so it's safe. And, there is strong anecdotal evidence that
2018 the committee intended the partial ordering rules to apply;
2019 the EDG front end has that behavior, and John Spicer claims
2020 that the committee simply forgot to delete the wording in
2021 [temp.expl.spec]. */
2022 tree tmpl
= most_specialized_instantiation (templates
);
2023 if (tmpl
!= error_mark_node
)
2026 TREE_CHAIN (templates
) = NULL_TREE
;
2030 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2032 error ("template-id %qD for %q+D does not match any template "
2033 "declaration", template_id
, decl
);
2034 if (header_count
&& header_count
!= template_count
+ 1)
2035 inform (input_location
, "saw %d %<template<>%>, need %d for "
2036 "specializing a member function template",
2037 header_count
, template_count
+ 1);
2038 return error_mark_node
;
2040 else if ((templates
&& TREE_CHAIN (templates
))
2041 || (candidates
&& TREE_CHAIN (candidates
))
2042 || (templates
&& candidates
))
2044 error ("ambiguous template specialization %qD for %q+D",
2046 candidates
= chainon (candidates
, templates
);
2047 print_candidates (candidates
);
2048 return error_mark_node
;
2051 /* We have one, and exactly one, match. */
2054 tree fn
= TREE_VALUE (candidates
);
2055 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2056 /* DECL is a re-declaration or partial instantiation of a template
2058 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2060 /* It was a specialization of an ordinary member function in a
2062 return DECL_TI_TEMPLATE (fn
);
2065 /* It was a specialization of a template. */
2066 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2067 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2069 *targs_out
= copy_node (targs
);
2070 SET_TMPL_ARGS_LEVEL (*targs_out
,
2071 TMPL_ARGS_DEPTH (*targs_out
),
2072 TREE_PURPOSE (templates
));
2075 *targs_out
= TREE_PURPOSE (templates
);
2076 return TREE_VALUE (templates
);
2079 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2080 but with the default argument values filled in from those in the
2084 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2087 tree new_spec_types
;
2092 if (spec_types
== void_list_node
)
2093 return void_list_node
;
2095 /* Substitute into the rest of the list. */
2097 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2098 TREE_CHAIN (tmpl_types
));
2100 /* Add the default argument for this parameter. */
2101 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2102 TREE_VALUE (spec_types
),
2106 /* DECL is an explicit specialization. Replicate default arguments
2107 from the template it specializes. (That way, code like:
2109 template <class T> void f(T = 3);
2110 template <> void f(double);
2113 works, as required.) An alternative approach would be to look up
2114 the correct default arguments at the call-site, but this approach
2115 is consistent with how implicit instantiations are handled. */
2118 copy_default_args_to_explicit_spec (tree decl
)
2123 tree new_spec_types
;
2127 tree object_type
= NULL_TREE
;
2128 tree in_charge
= NULL_TREE
;
2129 tree vtt
= NULL_TREE
;
2131 /* See if there's anything we need to do. */
2132 tmpl
= DECL_TI_TEMPLATE (decl
);
2133 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2134 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2135 if (TREE_PURPOSE (t
))
2140 old_type
= TREE_TYPE (decl
);
2141 spec_types
= TYPE_ARG_TYPES (old_type
);
2143 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2145 /* Remove the this pointer, but remember the object's type for
2147 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2148 spec_types
= TREE_CHAIN (spec_types
);
2149 tmpl_types
= TREE_CHAIN (tmpl_types
);
2151 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2153 /* DECL may contain more parameters than TMPL due to the extra
2154 in-charge parameter in constructors and destructors. */
2155 in_charge
= spec_types
;
2156 spec_types
= TREE_CHAIN (spec_types
);
2158 if (DECL_HAS_VTT_PARM_P (decl
))
2161 spec_types
= TREE_CHAIN (spec_types
);
2165 /* Compute the merged default arguments. */
2167 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2169 /* Compute the new FUNCTION_TYPE. */
2173 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2178 /* Put the in-charge parameter back. */
2179 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2180 TREE_VALUE (in_charge
),
2183 new_type
= build_method_type_directly (object_type
,
2184 TREE_TYPE (old_type
),
2188 new_type
= build_function_type (TREE_TYPE (old_type
),
2190 new_type
= cp_build_type_attribute_variant (new_type
,
2191 TYPE_ATTRIBUTES (old_type
));
2192 new_type
= build_exception_variant (new_type
,
2193 TYPE_RAISES_EXCEPTIONS (old_type
));
2194 TREE_TYPE (decl
) = new_type
;
2197 /* Check to see if the function just declared, as indicated in
2198 DECLARATOR, and in DECL, is a specialization of a function
2199 template. We may also discover that the declaration is an explicit
2200 instantiation at this point.
2202 Returns DECL, or an equivalent declaration that should be used
2203 instead if all goes well. Issues an error message if something is
2204 amiss. Returns error_mark_node if the error is not easily
2207 FLAGS is a bitmask consisting of the following flags:
2209 2: The function has a definition.
2210 4: The function is a friend.
2212 The TEMPLATE_COUNT is the number of references to qualifying
2213 template classes that appeared in the name of the function. For
2216 template <class T> struct S { void f(); };
2219 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2220 classes are not counted in the TEMPLATE_COUNT, so that in
2222 template <class T> struct S {};
2223 template <> struct S<int> { void f(); }
2224 template <> void S<int>::f();
2226 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2227 invalid; there should be no template <>.)
2229 If the function is a specialization, it is marked as such via
2230 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2231 is set up correctly, and it is added to the list of specializations
2232 for that template. */
2235 check_explicit_specialization (tree declarator
,
2240 int have_def
= flags
& 2;
2241 int is_friend
= flags
& 4;
2242 int specialization
= 0;
2243 int explicit_instantiation
= 0;
2244 int member_specialization
= 0;
2245 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2246 tree dname
= DECL_NAME (decl
);
2251 if (!processing_specialization
)
2254 tsk
= tsk_excessive_parms
;
2257 tsk
= current_tmpl_spec_kind (template_count
);
2262 if (processing_specialization
)
2265 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2267 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2270 /* This could be something like:
2272 template <class T> void f(T);
2273 class S { friend void f<>(int); } */
2277 /* This case handles bogus declarations like template <>
2278 template <class T> void f<int>(); */
2280 error ("template-id %qD in declaration of primary template",
2287 case tsk_invalid_member_spec
:
2288 /* The error has already been reported in
2289 check_specialization_scope. */
2290 return error_mark_node
;
2292 case tsk_invalid_expl_inst
:
2293 error ("template parameter list used in explicit instantiation");
2299 error ("definition provided for explicit instantiation");
2301 explicit_instantiation
= 1;
2304 case tsk_excessive_parms
:
2305 case tsk_insufficient_parms
:
2306 if (tsk
== tsk_excessive_parms
)
2307 error ("too many template parameter lists in declaration of %qD",
2309 else if (template_header_count
)
2310 error("too few template parameter lists in declaration of %qD", decl
);
2312 error("explicit specialization of %qD must be introduced by "
2313 "%<template <>%>", decl
);
2317 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2319 member_specialization
= 1;
2325 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2327 /* This case handles bogus declarations like template <>
2328 template <class T> void f<int>(); */
2330 if (uses_template_parms (declarator
))
2331 error ("function template partial specialization %qD "
2332 "is not allowed", declarator
);
2334 error ("template-id %qD in declaration of primary template",
2339 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2340 /* This is a specialization of a member template, without
2341 specialization the containing class. Something like:
2343 template <class T> struct S {
2344 template <class U> void f (U);
2346 template <> template <class U> void S<int>::f(U) {}
2348 That's a specialization -- but of the entire template. */
2356 if (specialization
|| member_specialization
)
2358 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2359 for (; t
; t
= TREE_CHAIN (t
))
2360 if (TREE_PURPOSE (t
))
2362 permerror (input_location
,
2363 "default argument specified in explicit specialization");
2368 if (specialization
|| member_specialization
|| explicit_instantiation
)
2370 tree tmpl
= NULL_TREE
;
2371 tree targs
= NULL_TREE
;
2373 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2374 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2378 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2383 /* If there is no class context, the explicit instantiation
2384 must be at namespace scope. */
2385 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2387 /* Find the namespace binding, using the declaration
2389 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2391 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2393 error ("%qD is not a template function", dname
);
2394 fns
= error_mark_node
;
2398 tree fn
= OVL_CURRENT (fns
);
2399 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2400 CP_DECL_CONTEXT (fn
)))
2401 error ("%qD is not declared in %qD",
2402 decl
, current_namespace
);
2406 declarator
= lookup_template_function (fns
, NULL_TREE
);
2409 if (declarator
== error_mark_node
)
2410 return error_mark_node
;
2412 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2414 if (!explicit_instantiation
)
2415 /* A specialization in class scope. This is invalid,
2416 but the error will already have been flagged by
2417 check_specialization_scope. */
2418 return error_mark_node
;
2421 /* It's not valid to write an explicit instantiation in
2424 class C { template void f(); }
2426 This case is caught by the parser. However, on
2429 template class C { void f(); };
2431 (which is invalid) we can get here. The error will be
2438 else if (ctype
!= NULL_TREE
2439 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2442 /* Find the list of functions in ctype that have the same
2443 name as the declared function. */
2444 tree name
= TREE_OPERAND (declarator
, 0);
2445 tree fns
= NULL_TREE
;
2448 if (constructor_name_p (name
, ctype
))
2450 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2452 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2453 : !CLASSTYPE_DESTRUCTORS (ctype
))
2455 /* From [temp.expl.spec]:
2457 If such an explicit specialization for the member
2458 of a class template names an implicitly-declared
2459 special member function (clause _special_), the
2460 program is ill-formed.
2462 Similar language is found in [temp.explicit]. */
2463 error ("specialization of implicitly-declared special member function");
2464 return error_mark_node
;
2467 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2470 if (!DECL_CONV_FN_P (decl
))
2472 idx
= lookup_fnfields_1 (ctype
, name
);
2474 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2478 VEC(tree
,gc
) *methods
;
2481 /* For a type-conversion operator, we cannot do a
2482 name-based lookup. We might be looking for `operator
2483 int' which will be a specialization of `operator T'.
2484 So, we find *all* the conversion operators, and then
2485 select from them. */
2488 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2490 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2491 VEC_iterate (tree
, methods
, idx
, ovl
);
2494 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2495 /* There are no more conversion functions. */
2498 /* Glue all these conversion functions together
2499 with those we already have. */
2500 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2501 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2505 if (fns
== NULL_TREE
)
2507 error ("no member function %qD declared in %qT", name
, ctype
);
2508 return error_mark_node
;
2511 TREE_OPERAND (declarator
, 0) = fns
;
2514 /* Figure out what exactly is being specialized at this point.
2515 Note that for an explicit instantiation, even one for a
2516 member function, we cannot tell apriori whether the
2517 instantiation is for a member template, or just a member
2518 function of a template class. Even if a member template is
2519 being instantiated, the member template arguments may be
2520 elided if they can be deduced from the rest of the
2522 tmpl
= determine_specialization (declarator
, decl
,
2524 member_specialization
,
2528 if (!tmpl
|| tmpl
== error_mark_node
)
2529 /* We couldn't figure out what this declaration was
2531 return error_mark_node
;
2534 tree gen_tmpl
= most_general_template (tmpl
);
2536 if (explicit_instantiation
)
2538 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2539 is done by do_decl_instantiation later. */
2541 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2542 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2544 if (arg_depth
> parm_depth
)
2546 /* If TMPL is not the most general template (for
2547 example, if TMPL is a friend template that is
2548 injected into namespace scope), then there will
2549 be too many levels of TARGS. Remove some of them
2554 new_targs
= make_tree_vec (parm_depth
);
2555 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2556 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2557 = TREE_VEC_ELT (targs
, i
);
2561 return instantiate_template (tmpl
, targs
, tf_error
);
2564 /* If we thought that the DECL was a member function, but it
2565 turns out to be specializing a static member function,
2566 make DECL a static member function as well. */
2567 if (DECL_STATIC_FUNCTION_P (tmpl
)
2568 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2569 revert_static_member_fn (decl
);
2571 /* If this is a specialization of a member template of a
2572 template class, we want to return the TEMPLATE_DECL, not
2573 the specialization of it. */
2574 if (tsk
== tsk_template
)
2576 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2577 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2578 DECL_INITIAL (result
) = NULL_TREE
;
2582 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2583 DECL_SOURCE_LOCATION (result
)
2584 = DECL_SOURCE_LOCATION (decl
);
2585 /* We want to use the argument list specified in the
2586 definition, not in the original declaration. */
2587 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2588 for (parm
= DECL_ARGUMENTS (result
); parm
;
2589 parm
= DECL_CHAIN (parm
))
2590 DECL_CONTEXT (parm
) = result
;
2592 return register_specialization (tmpl
, gen_tmpl
, targs
,
2596 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2597 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2599 /* Inherit default function arguments from the template
2600 DECL is specializing. */
2601 copy_default_args_to_explicit_spec (decl
);
2603 /* This specialization has the same protection as the
2604 template it specializes. */
2605 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2606 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2608 /* 7.1.1-1 [dcl.stc]
2610 A storage-class-specifier shall not be specified in an
2611 explicit specialization...
2613 The parser rejects these, so unless action is taken here,
2614 explicit function specializations will always appear with
2617 The action recommended by the C++ CWG in response to C++
2618 defect report 605 is to make the storage class and linkage
2619 of the explicit specialization match the templated function:
2621 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2623 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2625 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2626 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2628 /* This specialization has the same linkage and visibility as
2629 the function template it specializes. */
2630 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2631 if (! TREE_PUBLIC (decl
))
2633 DECL_INTERFACE_KNOWN (decl
) = 1;
2634 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2636 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2637 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2639 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2640 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2644 /* If DECL is a friend declaration, declared using an
2645 unqualified name, the namespace associated with DECL may
2646 have been set incorrectly. For example, in:
2648 template <typename T> void f(T);
2650 struct S { friend void f<int>(int); }
2653 we will have set the DECL_CONTEXT for the friend
2654 declaration to N, rather than to the global namespace. */
2655 if (DECL_NAMESPACE_SCOPE_P (decl
))
2656 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2658 if (is_friend
&& !have_def
)
2659 /* This is not really a declaration of a specialization.
2660 It's just the name of an instantiation. But, it's not
2661 a request for an instantiation, either. */
2662 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2663 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2664 /* This is indeed a specialization. In case of constructors
2665 and destructors, we need in-charge and not-in-charge
2666 versions in V3 ABI. */
2667 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2669 /* Register this specialization so that we can find it
2671 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2678 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2679 parameters. These are represented in the same format used for
2680 DECL_TEMPLATE_PARMS. */
2683 comp_template_parms (const_tree parms1
, const_tree parms2
)
2688 if (parms1
== parms2
)
2691 for (p1
= parms1
, p2
= parms2
;
2692 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2693 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2695 tree t1
= TREE_VALUE (p1
);
2696 tree t2
= TREE_VALUE (p2
);
2699 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2700 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2702 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2705 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2707 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2708 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2710 /* If either of the template parameters are invalid, assume
2711 they match for the sake of error recovery. */
2712 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2715 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2718 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2719 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2720 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2722 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2727 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2728 /* One set of parameters has more parameters lists than the
2735 /* Determine whether PARM is a parameter pack. */
2738 template_parameter_pack_p (const_tree parm
)
2740 /* Determine if we have a non-type template parameter pack. */
2741 if (TREE_CODE (parm
) == PARM_DECL
)
2742 return (DECL_TEMPLATE_PARM_P (parm
)
2743 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2744 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2745 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2747 /* If this is a list of template parameters, we could get a
2748 TYPE_DECL or a TEMPLATE_DECL. */
2749 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2750 parm
= TREE_TYPE (parm
);
2752 /* Otherwise it must be a type template parameter. */
2753 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2754 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2755 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2758 /* Determine if T is a function parameter pack. */
2761 function_parameter_pack_p (const_tree t
)
2763 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2764 return FUNCTION_PARAMETER_PACK_P (t
);
2768 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2769 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2772 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2774 if (! primary_func_tmpl_inst
2775 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2776 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2779 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2782 /* Return true iff the function parameter PARAM_DECL was expanded
2783 from the function parameter pack PACK. */
2786 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2788 if (DECL_ARTIFICIAL (param_decl
)
2789 || !function_parameter_pack_p (pack
))
2792 /* The parameter pack and its pack arguments have the same
2794 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2797 /* Determine whether ARGS describes a variadic template args list,
2798 i.e., one that is terminated by a template argument pack. */
2801 template_args_variadic_p (tree args
)
2806 if (args
== NULL_TREE
)
2809 args
= INNERMOST_TEMPLATE_ARGS (args
);
2810 nargs
= TREE_VEC_LENGTH (args
);
2815 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2817 return ARGUMENT_PACK_P (last_parm
);
2820 /* Generate a new name for the parameter pack name NAME (an
2821 IDENTIFIER_NODE) that incorporates its */
2824 make_ith_pack_parameter_name (tree name
, int i
)
2826 /* Munge the name to include the parameter index. */
2827 #define NUMBUF_LEN 128
2828 char numbuf
[NUMBUF_LEN
];
2832 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2833 newname_len
= IDENTIFIER_LENGTH (name
)
2834 + strlen (numbuf
) + 2;
2835 newname
= (char*)alloca (newname_len
);
2836 snprintf (newname
, newname_len
,
2837 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2838 return get_identifier (newname
);
2841 /* Return true if T is a primary function, class or alias template
2845 primary_template_instantiation_p (const_tree t
)
2850 if (TREE_CODE (t
) == FUNCTION_DECL
)
2851 return DECL_LANG_SPECIFIC (t
)
2852 && DECL_TEMPLATE_INSTANTIATION (t
)
2853 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2854 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2855 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2856 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2858 && TYPE_TEMPLATE_INFO (t
)
2859 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
2860 && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t
)))
2865 /* Return true if PARM is a template template parameter. */
2868 template_template_parameter_p (const_tree parm
)
2870 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2873 /* Return true iff PARM is a DECL representing a type template
2877 template_type_parameter_p (const_tree parm
)
2880 && (TREE_CODE (parm
) == TYPE_DECL
2881 || TREE_CODE (parm
) == TEMPLATE_DECL
)
2882 && DECL_TEMPLATE_PARM_P (parm
));
2885 /* Return the template parameters of T if T is a
2886 primary template instantiation, NULL otherwise. */
2889 get_primary_template_innermost_parameters (const_tree t
)
2891 tree parms
= NULL
, template_info
= NULL
;
2893 if ((template_info
= get_template_info (t
))
2894 && primary_template_instantiation_p (t
))
2895 parms
= INNERMOST_TEMPLATE_PARMS
2896 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2901 /* Return the template parameters of the LEVELth level from the full list
2902 of template parameters PARMS. */
2905 get_template_parms_at_level (tree parms
, int level
)
2909 || TREE_CODE (parms
) != TREE_LIST
2910 || level
> TMPL_PARMS_DEPTH (parms
))
2913 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2914 if (TMPL_PARMS_DEPTH (p
) == level
)
2920 /* Returns the template arguments of T if T is a template instantiation,
2924 get_template_innermost_arguments (const_tree t
)
2926 tree args
= NULL
, template_info
= NULL
;
2928 if ((template_info
= get_template_info (t
))
2929 && TI_ARGS (template_info
))
2930 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2935 /* Return the argument pack elements of T if T is a template argument pack,
2939 get_template_argument_pack_elems (const_tree t
)
2941 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2942 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2945 return ARGUMENT_PACK_ARGS (t
);
2948 /* Structure used to track the progress of find_parameter_packs_r. */
2949 struct find_parameter_pack_data
2951 /* TREE_LIST that will contain all of the parameter packs found by
2953 tree
* parameter_packs
;
2955 /* Set of AST nodes that have been visited by the traversal. */
2956 struct pointer_set_t
*visited
;
2959 /* Identifies all of the argument packs that occur in a template
2960 argument and appends them to the TREE_LIST inside DATA, which is a
2961 find_parameter_pack_data structure. This is a subroutine of
2962 make_pack_expansion and uses_parameter_packs. */
2964 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2967 struct find_parameter_pack_data
* ppd
=
2968 (struct find_parameter_pack_data
*)data
;
2969 bool parameter_pack_p
= false;
2971 /* Handle type aliases/typedefs. */
2974 && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
2975 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2977 if (TYPE_TEMPLATE_INFO (t
))
2978 cp_walk_tree (&TYPE_TI_ARGS (t
),
2979 &find_parameter_packs_r
,
2985 /* Identify whether this is a parameter pack or not. */
2986 switch (TREE_CODE (t
))
2988 case TEMPLATE_PARM_INDEX
:
2989 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2990 parameter_pack_p
= true;
2993 case TEMPLATE_TYPE_PARM
:
2994 t
= TYPE_MAIN_VARIANT (t
);
2995 case TEMPLATE_TEMPLATE_PARM
:
2996 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2997 parameter_pack_p
= true;
3001 if (FUNCTION_PARAMETER_PACK_P (t
))
3003 /* We don't want to walk into the type of a PARM_DECL,
3004 because we don't want to see the type parameter pack. */
3006 parameter_pack_p
= true;
3011 parameter_pack_p
= true;
3014 /* Not a parameter pack. */
3018 if (parameter_pack_p
)
3020 /* Add this parameter pack to the list. */
3021 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3025 cp_walk_tree (&TYPE_CONTEXT (t
),
3026 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3028 /* This switch statement will return immediately if we don't find a
3030 switch (TREE_CODE (t
))
3032 case TEMPLATE_PARM_INDEX
:
3035 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3036 /* Check the template itself. */
3037 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3038 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3039 /* Check the template arguments. */
3040 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3045 case TEMPLATE_TYPE_PARM
:
3046 case TEMPLATE_TEMPLATE_PARM
:
3053 if (TYPE_PTRMEMFUNC_P (t
))
3059 if (TYPE_TEMPLATE_INFO (t
))
3060 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3061 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3068 cp_walk_tree (&TREE_TYPE (t
),
3069 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3073 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3078 case TYPE_PACK_EXPANSION
:
3079 case EXPR_PACK_EXPANSION
:
3084 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3089 case IDENTIFIER_NODE
:
3090 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3102 /* Determines if the expression or type T uses any parameter packs. */
3104 uses_parameter_packs (tree t
)
3106 tree parameter_packs
= NULL_TREE
;
3107 struct find_parameter_pack_data ppd
;
3108 ppd
.parameter_packs
= ¶meter_packs
;
3109 ppd
.visited
= pointer_set_create ();
3110 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3111 pointer_set_destroy (ppd
.visited
);
3112 return parameter_packs
!= NULL_TREE
;
3115 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3116 representation a base-class initializer into a parameter pack
3117 expansion. If all goes well, the resulting node will be an
3118 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3121 make_pack_expansion (tree arg
)
3124 tree parameter_packs
= NULL_TREE
;
3125 bool for_types
= false;
3126 struct find_parameter_pack_data ppd
;
3128 if (!arg
|| arg
== error_mark_node
)
3131 if (TREE_CODE (arg
) == TREE_LIST
)
3133 /* The only time we will see a TREE_LIST here is for a base
3134 class initializer. In this case, the TREE_PURPOSE will be a
3135 _TYPE node (representing the base class expansion we're
3136 initializing) and the TREE_VALUE will be a TREE_LIST
3137 containing the initialization arguments.
3139 The resulting expansion looks somewhat different from most
3140 expansions. Rather than returning just one _EXPANSION, we
3141 return a TREE_LIST whose TREE_PURPOSE is a
3142 TYPE_PACK_EXPANSION containing the bases that will be
3143 initialized. The TREE_VALUE will be identical to the
3144 original TREE_VALUE, which is a list of arguments that will
3145 be passed to each base. We do not introduce any new pack
3146 expansion nodes into the TREE_VALUE (although it is possible
3147 that some already exist), because the TREE_PURPOSE and
3148 TREE_VALUE all need to be expanded together with the same
3149 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3150 resulting TREE_PURPOSE will mention the parameter packs in
3151 both the bases and the arguments to the bases. */
3154 tree parameter_packs
= NULL_TREE
;
3156 /* Determine which parameter packs will be used by the base
3158 ppd
.visited
= pointer_set_create ();
3159 ppd
.parameter_packs
= ¶meter_packs
;
3160 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3163 if (parameter_packs
== NULL_TREE
)
3165 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3166 pointer_set_destroy (ppd
.visited
);
3167 return error_mark_node
;
3170 if (TREE_VALUE (arg
) != void_type_node
)
3172 /* Collect the sets of parameter packs used in each of the
3173 initialization arguments. */
3174 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3176 /* Determine which parameter packs will be expanded in this
3178 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3183 pointer_set_destroy (ppd
.visited
);
3185 /* Create the pack expansion type for the base type. */
3186 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3187 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3188 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3190 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3191 they will rarely be compared to anything. */
3192 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3194 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3197 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3200 /* Build the PACK_EXPANSION_* node. */
3202 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3203 : make_node (EXPR_PACK_EXPANSION
);
3204 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3205 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3207 /* Propagate type and const-expression information. */
3208 TREE_TYPE (result
) = TREE_TYPE (arg
);
3209 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3212 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3213 they will rarely be compared to anything. */
3214 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3216 /* Determine which parameter packs will be expanded. */
3217 ppd
.parameter_packs
= ¶meter_packs
;
3218 ppd
.visited
= pointer_set_create ();
3219 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3220 pointer_set_destroy (ppd
.visited
);
3222 /* Make sure we found some parameter packs. */
3223 if (parameter_packs
== NULL_TREE
)
3226 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3228 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3229 return error_mark_node
;
3231 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3233 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3238 /* Checks T for any "bare" parameter packs, which have not yet been
3239 expanded, and issues an error if any are found. This operation can
3240 only be done on full expressions or types (e.g., an expression
3241 statement, "if" condition, etc.), because we could have expressions like:
3243 foo(f(g(h(args)))...)
3245 where "args" is a parameter pack. check_for_bare_parameter_packs
3246 should not be called for the subexpressions args, h(args),
3247 g(h(args)), or f(g(h(args))), because we would produce erroneous
3250 Returns TRUE and emits an error if there were bare parameter packs,
3251 returns FALSE otherwise. */
3253 check_for_bare_parameter_packs (tree t
)
3255 tree parameter_packs
= NULL_TREE
;
3256 struct find_parameter_pack_data ppd
;
3258 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3261 if (TREE_CODE (t
) == TYPE_DECL
)
3264 ppd
.parameter_packs
= ¶meter_packs
;
3265 ppd
.visited
= pointer_set_create ();
3266 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3267 pointer_set_destroy (ppd
.visited
);
3269 if (parameter_packs
)
3271 error ("parameter packs not expanded with %<...%>:");
3272 while (parameter_packs
)
3274 tree pack
= TREE_VALUE (parameter_packs
);
3275 tree name
= NULL_TREE
;
3277 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3278 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3279 name
= TYPE_NAME (pack
);
3280 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3281 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3283 name
= DECL_NAME (pack
);
3286 inform (input_location
, " %qD", name
);
3288 inform (input_location
, " <anonymous>");
3290 parameter_packs
= TREE_CHAIN (parameter_packs
);
3299 /* Expand any parameter packs that occur in the template arguments in
3302 expand_template_argument_pack (tree args
)
3304 tree result_args
= NULL_TREE
;
3305 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3306 int num_result_args
= -1;
3307 int non_default_args_count
= -1;
3309 /* First, determine if we need to expand anything, and the number of
3310 slots we'll need. */
3311 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3313 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3314 if (arg
== NULL_TREE
)
3316 if (ARGUMENT_PACK_P (arg
))
3318 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3319 if (num_result_args
< 0)
3320 num_result_args
= in_arg
+ num_packed
;
3322 num_result_args
+= num_packed
;
3326 if (num_result_args
>= 0)
3331 /* If no expansion is necessary, we're done. */
3332 if (num_result_args
< 0)
3335 /* Expand arguments. */
3336 result_args
= make_tree_vec (num_result_args
);
3337 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3338 non_default_args_count
=
3339 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3340 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3342 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3343 if (ARGUMENT_PACK_P (arg
))
3345 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3346 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3347 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3348 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3349 if (non_default_args_count
> 0)
3350 non_default_args_count
+= num_packed
;
3354 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3358 if (non_default_args_count
>= 0)
3359 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3363 /* Checks if DECL shadows a template parameter.
3365 [temp.local]: A template-parameter shall not be redeclared within its
3366 scope (including nested scopes).
3368 Emits an error and returns TRUE if the DECL shadows a parameter,
3369 returns FALSE otherwise. */
3372 check_template_shadow (tree decl
)
3376 /* If we're not in a template, we can't possibly shadow a template
3378 if (!current_template_parms
)
3381 /* Figure out what we're shadowing. */
3382 if (TREE_CODE (decl
) == OVERLOAD
)
3383 decl
= OVL_CURRENT (decl
);
3384 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3386 /* If there's no previous binding for this name, we're not shadowing
3387 anything, let alone a template parameter. */
3391 /* If we're not shadowing a template parameter, we're done. Note
3392 that OLDDECL might be an OVERLOAD (or perhaps even an
3393 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3395 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3398 /* We check for decl != olddecl to avoid bogus errors for using a
3399 name inside a class. We check TPFI to avoid duplicate errors for
3400 inline member templates. */
3402 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3405 error ("declaration of %q+#D", decl
);
3406 error (" shadows template parm %q+#D", olddecl
);
3410 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3411 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3412 template parameters. */
3415 build_template_parm_index (int index
,
3422 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3423 TEMPLATE_PARM_IDX (t
) = index
;
3424 TEMPLATE_PARM_LEVEL (t
) = level
;
3425 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3426 TEMPLATE_PARM_NUM_SIBLINGS (t
) = num_siblings
;
3427 TEMPLATE_PARM_DECL (t
) = decl
;
3428 TREE_TYPE (t
) = type
;
3429 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3430 TREE_READONLY (t
) = TREE_READONLY (decl
);
3435 /* Find the canonical type parameter for the given template type
3436 parameter. Returns the canonical type parameter, which may be TYPE
3437 if no such parameter existed. */
3440 canonical_type_parameter (tree type
)
3443 int idx
= TEMPLATE_TYPE_IDX (type
);
3444 if (!canonical_template_parms
)
3445 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3447 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3448 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3450 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3451 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3452 list
= TREE_CHAIN (list
);
3455 return TREE_VALUE (list
);
3458 VEC_replace(tree
, canonical_template_parms
, idx
,
3459 tree_cons (NULL_TREE
, type
,
3460 VEC_index (tree
, canonical_template_parms
, idx
)));
3465 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3466 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3467 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3468 new one is created. */
3471 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3472 tsubst_flags_t complain
)
3474 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3475 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3476 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3477 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3479 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3482 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3483 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3484 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3485 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3486 DECL_ARTIFICIAL (decl
) = 1;
3487 SET_DECL_TEMPLATE_PARM_P (decl
);
3489 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3490 TEMPLATE_PARM_LEVEL (index
) - levels
,
3491 TEMPLATE_PARM_ORIG_LEVEL (index
),
3492 TEMPLATE_PARM_NUM_SIBLINGS (index
),
3494 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3495 TEMPLATE_PARM_PARAMETER_PACK (t
)
3496 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3498 /* Template template parameters need this. */
3499 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3500 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3501 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3505 return TEMPLATE_PARM_DESCENDANTS (index
);
3508 /* Process information from new template parameter PARM and append it
3509 to the LIST being built. This new parameter is a non-type
3510 parameter iff IS_NON_TYPE is true. This new parameter is a
3511 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3512 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3513 parameter list PARM belongs to. This is used used to create a
3514 proper canonical type for the type of PARM that is to be created,
3515 iff PARM is a type. If the size is not known, this parameter shall
3519 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3520 bool is_non_type
, bool is_parameter_pack
,
3521 unsigned num_template_parms
)
3528 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3529 defval
= TREE_PURPOSE (parm
);
3533 tree p
= tree_last (list
);
3535 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3538 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3539 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3541 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3551 parm
= TREE_VALUE (parm
);
3553 SET_DECL_TEMPLATE_PARM_P (parm
);
3555 if (TREE_TYPE (parm
) == error_mark_node
)
3557 err_parm_list
= build_tree_list (defval
, parm
);
3558 TREE_VALUE (err_parm_list
) = error_mark_node
;
3559 return chainon (list
, err_parm_list
);
3565 The top-level cv-qualifiers on the template-parameter are
3566 ignored when determining its type. */
3567 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3568 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
3570 err_parm_list
= build_tree_list (defval
, parm
);
3571 TREE_VALUE (err_parm_list
) = error_mark_node
;
3572 return chainon (list
, err_parm_list
);
3575 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3577 /* This template parameter is not a parameter pack, but it
3578 should be. Complain about "bare" parameter packs. */
3579 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3581 /* Recover by calling this a parameter pack. */
3582 is_parameter_pack
= true;
3586 /* A template parameter is not modifiable. */
3587 TREE_CONSTANT (parm
) = 1;
3588 TREE_READONLY (parm
) = 1;
3589 decl
= build_decl (parm_loc
,
3590 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3591 TREE_CONSTANT (decl
) = 1;
3592 TREE_READONLY (decl
) = 1;
3593 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3594 = build_template_parm_index (idx
, processing_template_decl
,
3595 processing_template_decl
,
3597 decl
, TREE_TYPE (parm
));
3599 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3600 = is_parameter_pack
;
3605 parm
= TREE_VALUE (TREE_VALUE (parm
));
3607 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3609 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3610 /* This is for distinguishing between real templates and template
3611 template parameters */
3612 TREE_TYPE (parm
) = t
;
3613 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3618 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3619 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3620 decl
= build_decl (parm_loc
,
3621 TYPE_DECL
, parm
, t
);
3624 TYPE_NAME (t
) = decl
;
3625 TYPE_STUB_DECL (t
) = decl
;
3627 TEMPLATE_TYPE_PARM_INDEX (t
)
3628 = build_template_parm_index (idx
, processing_template_decl
,
3629 processing_template_decl
,
3631 decl
, TREE_TYPE (parm
));
3632 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3633 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3635 DECL_ARTIFICIAL (decl
) = 1;
3636 SET_DECL_TEMPLATE_PARM_P (decl
);
3638 parm
= build_tree_list (defval
, parm
);
3639 return chainon (list
, parm
);
3642 /* The end of a template parameter list has been reached. Process the
3643 tree list into a parameter vector, converting each parameter into a more
3644 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3648 end_template_parm_list (tree parms
)
3652 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3654 current_template_parms
3655 = tree_cons (size_int (processing_template_decl
),
3656 saved_parmlist
, current_template_parms
);
3658 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3660 next
= TREE_CHAIN (parm
);
3661 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3662 TREE_CHAIN (parm
) = NULL_TREE
;
3665 --processing_template_parmlist
;
3667 return saved_parmlist
;
3670 /* Create a new type almost identical to TYPE but which has the
3671 following differences:
3673 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3674 template sibling parameters of T.
3676 2/ T has a new canonical type that matches the new number
3679 3/ From now on, T is going to be what lookups referring to the
3680 name of TYPE will return. No lookup should return TYPE anymore.
3682 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3684 This is a subroutine of fixup_template_parms. */
3687 fixup_template_type_parm_type (tree type
, int num_parms
)
3689 tree orig_idx
= TEMPLATE_TYPE_PARM_INDEX (type
), idx
;
3691 /* This is the decl which name is inserted into the symbol table for
3692 the template parm type. So whenever we lookup the type name, this
3693 is the DECL we get. */
3696 /* Do not fix up the type twice. */
3697 if (orig_idx
&& TEMPLATE_PARM_NUM_SIBLINGS (orig_idx
) != 0)
3700 t
= copy_type (type
);
3701 decl
= TYPE_NAME (t
);
3703 TYPE_MAIN_VARIANT (t
) = t
;
3704 TYPE_NEXT_VARIANT (t
)= NULL_TREE
;
3705 TYPE_POINTER_TO (t
) = 0;
3706 TYPE_REFERENCE_TO (t
) = 0;
3708 idx
= build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx
),
3709 TEMPLATE_PARM_LEVEL (orig_idx
),
3710 TEMPLATE_PARM_ORIG_LEVEL (orig_idx
),
3713 TEMPLATE_PARM_DESCENDANTS (idx
) = TEMPLATE_PARM_DESCENDANTS (orig_idx
);
3714 TEMPLATE_PARM_PARAMETER_PACK (idx
) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx
);
3715 TEMPLATE_TYPE_PARM_INDEX (t
) = idx
;
3717 TYPE_STUB_DECL (t
) = decl
;
3718 TEMPLATE_TYPE_DECL (t
) = decl
;
3719 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
3720 TREE_TYPE (DECL_TEMPLATE_RESULT (decl
)) = t
;
3722 /* Update the type associated to the type name stored in the symbol
3723 table. Now, whenever the type name is looked up, the resulting
3724 type is properly fixed up. */
3725 TREE_TYPE (decl
) = t
;
3727 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3732 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3733 identical to I, but that is fixed up as to:
3735 1/ carry the number of sibling parms (NUM_PARMS) of the template
3736 parm represented by I.
3738 2/ replace all references to template parm types declared before I
3739 (in the same template parm list as I) by references to template
3740 parm types contained in ARGS. ARGS should contain the list of
3741 template parms that have been fixed up so far, in a form suitable
3742 to be passed to tsubst.
3744 This is a subroutine of fixup_template_parms. */
3747 fixup_template_parm_index (tree i
, tree args
, int num_parms
)
3749 tree index
, decl
, type
;
3752 || TREE_CODE (i
) != TEMPLATE_PARM_INDEX
3753 /* Do not fix up the index twice. */
3754 || (TEMPLATE_PARM_NUM_SIBLINGS (i
) != 0))
3757 decl
= TEMPLATE_PARM_DECL (i
);
3758 type
= TREE_TYPE (decl
);
3760 index
= build_template_parm_index (TEMPLATE_PARM_IDX (i
),
3761 TEMPLATE_PARM_LEVEL (i
),
3762 TEMPLATE_PARM_ORIG_LEVEL (i
),
3766 TEMPLATE_PARM_DESCENDANTS (index
) = TEMPLATE_PARM_DESCENDANTS (i
);
3767 TEMPLATE_PARM_PARAMETER_PACK (index
) = TEMPLATE_PARM_PARAMETER_PACK (i
);
3769 type
= tsubst (type
, args
, tf_none
, NULL_TREE
);
3771 TREE_TYPE (decl
) = type
;
3772 TREE_TYPE (index
) = type
;
3778 This is a subroutine of fixup_template_parms.
3780 It computes the canonical type of the type of the template
3781 parameter PARM_DESC and update all references to that type so that
3782 they use the newly computed canonical type. No access check is
3783 performed during the fixup. PARM_DESC is a TREE_LIST which
3784 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3785 default argument of the template parm if any. IDX is the index of
3786 the template parameter, starting at 0. NUM_PARMS is the number of
3787 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3788 TREE_VEC containing the full set of template parameters in a form
3789 suitable to be passed to substs functions as their ARGS
3790 argument. This is what current_template_args returns for a given
3791 template. The innermost vector of args in ARGLIST is the set of
3792 template parms that have been fixed up so far. This function adds
3793 the fixed up parameter into that vector. */
3796 fixup_template_parm (tree parm_desc
,
3801 tree parm
= TREE_VALUE (parm_desc
);
3802 tree fixedup_args
= INNERMOST_TEMPLATE_ARGS (arglist
);
3804 push_deferring_access_checks (dk_no_check
);
3806 if (TREE_CODE (parm
) == TYPE_DECL
)
3808 /* PARM is a template type parameter. Fix up its type, add
3809 the fixed-up template parm to the vector of fixed-up
3810 template parms so far, and substitute the fixed-up
3811 template parms into the default argument of this
3814 fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3815 TREE_TYPE (parm
) = t
;
3817 TREE_VEC_ELT (fixedup_args
, idx
) = template_parm_to_arg (parm_desc
);
3819 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3821 /* PARM is a template template parameter. This is going to
3823 tree tparms
, targs
, innermost_args
, t
;
3826 /* First, fix up the parms of the template template parm
3827 because the parms are involved in defining the new canonical
3828 type of the template template parm. */
3830 /* So we need to substitute the template parm types that have
3831 been fixed up so far into the template parms of this template
3832 template parm. E.g, consider this:
3834 template<class T, template<T u> class TT> class S;
3836 In this case we want to substitute T into the
3837 template parameters of TT.
3839 So let's walk the template parms of PARM here, and
3840 tsubst ARGLIST into into each of the template
3843 /* For this substitution we need to build the full set of
3844 template parameters and use that as arguments for the
3845 tsubsting function. */
3846 tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
3848 /* This will contain the innermost parms of PARM into which
3849 we have substituted so far. */
3850 innermost_args
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
3851 targs
= add_to_template_args (arglist
, innermost_args
);
3852 for (j
= 0; j
< TREE_VEC_LENGTH (tparms
); ++j
)
3856 parameter
= TREE_VEC_ELT (tparms
, j
);
3858 /* INNERMOST_ARGS needs to have at least the same number
3859 of elements as the index PARAMETER, ortherwise
3860 tsubsting into PARAMETER will result in partially
3861 instantiating it, reducing its tempate parm
3862 level. Let's tactically fill INNERMOST_ARGS for that
3864 TREE_VEC_ELT (innermost_args
, j
) =
3865 template_parm_to_arg (parameter
);
3867 fixup_template_parm (parameter
, j
,
3868 TREE_VEC_LENGTH (tparms
),
3872 /* Now fix up the type of the template template parm. */
3874 t
= fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3875 TREE_TYPE (parm
) = t
;
3877 TREE_VEC_ELT (fixedup_args
, idx
) =
3878 template_parm_to_arg (parm_desc
);
3880 else if (TREE_CODE (parm
) == PARM_DECL
)
3882 /* PARM is a non-type template parameter. We need to:
3884 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3885 proper number of sibling parameters.
3887 * Make lookups of the template parameter return a reference
3888 to the fixed-up index. No lookup should return references
3889 to the former index anymore.
3891 * Substitute the template parms that got fixed up so far
3893 * into the type of PARM. */
3895 tree index
= DECL_INITIAL (parm
);
3897 /* PUSHED_DECL is the decl added to the symbol table with
3898 the name of the parameter. E,g:
3900 template<class T, T u> //#0
3901 auto my_function(T t) -> decltype(u); //#1
3903 Here, when looking up u at //#1, we get the decl of u
3904 resulting from the declaration in #0. This is what
3905 PUSHED_DECL is. We need to replace the reference to the
3906 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3907 fixed-up TEMPLATE_PARM_INDEX. */
3908 tree pushed_decl
= TEMPLATE_PARM_DECL (index
);
3910 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3911 fixup the type of PUSHED_DECL as well and luckily
3912 fixup_template_parm_index does it for us too. */
3913 tree fixed_up_index
=
3914 fixup_template_parm_index (index
, arglist
, num_parms
);
3916 DECL_INITIAL (pushed_decl
) = DECL_INITIAL (parm
) = fixed_up_index
;
3918 /* Add this fixed up PARM to the template parms we've fixed
3919 up so far and use that to substitute the fixed-up
3920 template parms into the type of PARM. */
3921 TREE_VEC_ELT (fixedup_args
, idx
) =
3922 template_parm_to_arg (parm_desc
);
3923 TREE_TYPE (parm
) = tsubst (TREE_TYPE (parm
), arglist
,
3924 tf_none
, NULL_TREE
);
3927 TREE_PURPOSE (parm_desc
) =
3928 tsubst_template_arg (TREE_PURPOSE (parm_desc
),
3929 arglist
, tf_none
, parm
);
3931 pop_deferring_access_checks ();
3934 /* Walk the current template parms and properly compute the canonical
3935 types of the dependent types created during
3936 cp_parser_template_parameter_list. */
3939 fixup_template_parms (void)
3946 parameter_vec
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3947 if (parameter_vec
== NULL_TREE
)
3950 num_parms
= TREE_VEC_LENGTH (parameter_vec
);
3952 /* This vector contains the current innermost template parms that
3953 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3954 to be passed to tsubst* functions as their ARGS argument. */
3955 fixedup_args
= make_tree_vec (num_parms
);
3957 /* This vector contains the full set of template parms in a form
3958 suitable to be passed to substs functions as their ARGS
3960 arglist
= current_template_args ();
3961 arglist
= add_outermost_template_args (arglist
, fixedup_args
);
3963 /* Let's do the proper fixup now. */
3964 for (i
= 0; i
< num_parms
; ++i
)
3965 fixup_template_parm (TREE_VEC_ELT (parameter_vec
, i
),
3966 i
, num_parms
, arglist
);
3969 /* end_template_decl is called after a template declaration is seen. */
3972 end_template_decl (void)
3974 reset_specialization ();
3976 if (! processing_template_decl
)
3979 /* This matches the pushlevel in begin_template_parm_list. */
3982 --processing_template_decl
;
3983 current_template_parms
= TREE_CHAIN (current_template_parms
);
3986 /* Takes a TREE_LIST representing a template parameter and convert it
3987 into an argument suitable to be passed to the type substitution
3988 functions. Note that If the TREE_LIST contains an error_mark
3989 node, the returned argument is error_mark_node. */
3992 template_parm_to_arg (tree t
)
3996 || TREE_CODE (t
) != TREE_LIST
)
3999 if (error_operand_p (TREE_VALUE (t
)))
4000 return error_mark_node
;
4004 if (TREE_CODE (t
) == TYPE_DECL
4005 || TREE_CODE (t
) == TEMPLATE_DECL
)
4009 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
4011 /* Turn this argument into a TYPE_ARGUMENT_PACK
4012 with a single element, which expands T. */
4013 tree vec
= make_tree_vec (1);
4014 #ifdef ENABLE_CHECKING
4015 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4016 (vec
, TREE_VEC_LENGTH (vec
));
4018 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4020 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4021 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4026 t
= DECL_INITIAL (t
);
4028 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4030 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4031 with a single element, which expands T. */
4032 tree vec
= make_tree_vec (1);
4033 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
4034 #ifdef ENABLE_CHECKING
4035 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4036 (vec
, TREE_VEC_LENGTH (vec
));
4038 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4040 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4041 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4042 TREE_TYPE (t
) = type
;
4048 /* This function returns TRUE if PARM_PACK is a template parameter
4049 pack and if ARG_PACK is what template_parm_to_arg returned when
4050 passed PARM_PACK. */
4053 arg_from_parm_pack_p (tree arg_pack
, tree parm_pack
)
4055 /* For clarity in the comments below let's use the representation
4056 argument_pack<elements>' to denote an argument pack and its
4059 In the 'if' block below, we want to detect cases where
4060 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
4061 check if ARG_PACK is an argument pack which sole element is
4062 the expansion of PARM_PACK. That argument pack is typically
4063 created by template_parm_to_arg when passed a parameter
4067 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
4068 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
4070 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
4071 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
4072 /* So we have an argument_pack<P...>. We want to test if P
4073 is actually PARM_PACK. We will not use cp_tree_equal to
4074 test P and PARM_PACK because during type fixup (by
4075 fixup_template_parm) P can be a pre-fixup version of a
4076 type and PARM_PACK be its post-fixup version.
4077 cp_tree_equal would consider them as different even
4078 though we would want to consider them compatible for our
4079 precise purpose here.
4081 Thus we are going to consider that P and PARM_PACK are
4082 compatible if they have the same DECL. */
4083 if ((/* If ARG_PACK is a type parameter pack named by the
4084 same DECL as parm_pack ... */
4086 && TYPE_P (parm_pack
)
4087 && TYPE_NAME (pattern
) == TYPE_NAME (parm_pack
))
4088 /* ... or if PARM_PACK is a non-type parameter named by the
4089 same DECL as ARG_PACK. Note that PARM_PACK being a
4090 non-type parameter means it's either a PARM_DECL or a
4091 TEMPLATE_PARM_INDEX. */
4092 || (TREE_CODE (pattern
) == TEMPLATE_PARM_INDEX
4093 && ((TREE_CODE (parm_pack
) == PARM_DECL
4094 && (TEMPLATE_PARM_DECL (pattern
)
4095 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack
))))
4096 || (TREE_CODE (parm_pack
) == TEMPLATE_PARM_INDEX
4097 && (TEMPLATE_PARM_DECL (pattern
)
4098 == TEMPLATE_PARM_DECL (parm_pack
))))))
4099 && template_parameter_pack_p (pattern
))
4105 /* Within the declaration of a template, return all levels of template
4106 parameters that apply. The template parameters are represented as
4107 a TREE_VEC, in the form documented in cp-tree.h for template
4111 current_template_args (void)
4114 tree args
= NULL_TREE
;
4115 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
4118 /* If there is only one level of template parameters, we do not
4119 create a TREE_VEC of TREE_VECs. Instead, we return a single
4120 TREE_VEC containing the arguments. */
4122 args
= make_tree_vec (length
);
4124 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
4126 tree a
= copy_node (TREE_VALUE (header
));
4129 TREE_TYPE (a
) = NULL_TREE
;
4130 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4131 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4133 #ifdef ENABLE_CHECKING
4134 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4138 TREE_VEC_ELT (args
, --l
) = a
;
4143 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
4144 /* This can happen for template parms of a template template
4147 template<template<class T, class U> class TT> struct S;
4149 Consider the level of the parms of TT; T and U both have
4150 level 2; TT has no template parm of level 1. So in this case
4151 the first element of full_template_args is NULL_TREE. If we
4152 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4153 of 2. This will make tsubst wrongly consider that T and U
4154 have level 1. Instead, let's create a dummy vector as the
4155 first element of full_template_args so that TMPL_ARG_DEPTH
4156 returns the correct depth for args. */
4157 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
4161 /* Update the declared TYPE by doing any lookups which were thought to be
4162 dependent, but are not now that we know the SCOPE of the declarator. */
4165 maybe_update_decl_type (tree orig_type
, tree scope
)
4167 tree type
= orig_type
;
4169 if (type
== NULL_TREE
)
4172 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4173 type
= TREE_TYPE (type
);
4175 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4176 && dependent_type_p (type
)
4177 /* Don't bother building up the args in this case. */
4178 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4180 /* tsubst in the args corresponding to the template parameters,
4181 including auto if present. Most things will be unchanged, but
4182 make_typename_type and tsubst_qualified_id will resolve
4183 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4184 tree args
= current_template_args ();
4185 tree auto_node
= type_uses_auto (type
);
4189 tree auto_vec
= make_tree_vec (1);
4190 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4191 args
= add_to_template_args (args
, auto_vec
);
4193 pushed
= push_scope (scope
);
4194 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4199 if (type
== error_mark_node
)
4202 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4204 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4207 type
= TYPE_NAME (type
);
4212 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4213 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4214 a member template. Used by push_template_decl below. */
4217 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4219 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4220 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4221 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4222 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4223 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4228 struct template_parm_data
4230 /* The level of the template parameters we are currently
4234 /* The index of the specialization argument we are currently
4238 /* An array whose size is the number of template parameters. The
4239 elements are nonzero if the parameter has been used in any one
4240 of the arguments processed so far. */
4243 /* An array whose size is the number of template arguments. The
4244 elements are nonzero if the argument makes use of template
4245 parameters of this level. */
4246 int* arg_uses_template_parms
;
4249 /* Subroutine of push_template_decl used to see if each template
4250 parameter in a partial specialization is used in the explicit
4251 argument list. If T is of the LEVEL given in DATA (which is
4252 treated as a template_parm_data*), then DATA->PARMS is marked
4256 mark_template_parm (tree t
, void* data
)
4260 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4262 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4264 level
= TEMPLATE_PARM_LEVEL (t
);
4265 idx
= TEMPLATE_PARM_IDX (t
);
4269 level
= TEMPLATE_TYPE_LEVEL (t
);
4270 idx
= TEMPLATE_TYPE_IDX (t
);
4273 if (level
== tpd
->level
)
4275 tpd
->parms
[idx
] = 1;
4276 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4279 /* Return zero so that for_each_template_parm will continue the
4280 traversal of the tree; we want to mark *every* template parm. */
4284 /* Process the partial specialization DECL. */
4287 process_partial_specialization (tree decl
)
4289 tree type
= TREE_TYPE (decl
);
4290 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4291 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4292 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4293 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4296 int nargs
= TREE_VEC_LENGTH (inner_args
);
4299 bool did_error_intro
= false;
4300 struct template_parm_data tpd
;
4301 struct template_parm_data tpd2
;
4303 gcc_assert (current_template_parms
);
4305 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4306 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4308 /* We check that each of the template parameters given in the
4309 partial specialization is used in the argument list to the
4310 specialization. For example:
4312 template <class T> struct S;
4313 template <class T> struct S<T*>;
4315 The second declaration is OK because `T*' uses the template
4316 parameter T, whereas
4318 template <class T> struct S<int>;
4320 is no good. Even trickier is:
4331 The S2<T> declaration is actually invalid; it is a
4332 full-specialization. Of course,
4335 struct S2<T (*)(U)>;
4337 or some such would have been OK. */
4338 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4339 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4340 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4342 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4343 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4344 for (i
= 0; i
< nargs
; ++i
)
4346 tpd
.current_arg
= i
;
4347 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4348 &mark_template_parm
,
4351 /*include_nondeduced_p=*/false);
4353 for (i
= 0; i
< ntparms
; ++i
)
4354 if (tpd
.parms
[i
] == 0)
4356 /* One of the template parms was not used in the
4358 if (!did_error_intro
)
4360 error ("template parameters not used in partial specialization:");
4361 did_error_intro
= true;
4364 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4367 if (did_error_intro
)
4368 return error_mark_node
;
4370 /* [temp.class.spec]
4372 The argument list of the specialization shall not be identical to
4373 the implicit argument list of the primary template. */
4374 if (comp_template_args
4376 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4378 error ("partial specialization %qT does not specialize any template arguments", type
);
4380 /* A partial specialization that replaces multiple parameters of the
4381 primary template with a pack expansion is less specialized for those
4383 if (nargs
< DECL_NTPARMS (maintmpl
))
4385 error ("partial specialization is not more specialized than the "
4386 "primary template because it replaces multiple parameters "
4387 "with a pack expansion");
4388 inform (DECL_SOURCE_LOCATION (maintmpl
), "primary template here");
4392 /* [temp.class.spec]
4394 A partially specialized non-type argument expression shall not
4395 involve template parameters of the partial specialization except
4396 when the argument expression is a simple identifier.
4398 The type of a template parameter corresponding to a specialized
4399 non-type argument shall not be dependent on a parameter of the
4402 Also, we verify that pack expansions only occur at the
4403 end of the argument list. */
4404 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4406 for (i
= 0; i
< nargs
; ++i
)
4408 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4409 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4410 tree packed_args
= NULL_TREE
;
4413 if (ARGUMENT_PACK_P (arg
))
4415 /* Extract the arguments from the argument pack. We'll be
4416 iterating over these in the following loop. */
4417 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4418 len
= TREE_VEC_LENGTH (packed_args
);
4421 for (j
= 0; j
< len
; j
++)
4424 /* Get the Jth argument in the parameter pack. */
4425 arg
= TREE_VEC_ELT (packed_args
, j
);
4427 if (PACK_EXPANSION_P (arg
))
4429 /* Pack expansions must come at the end of the
4431 if ((packed_args
&& j
< len
- 1)
4432 || (!packed_args
&& i
< nargs
- 1))
4434 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4435 error ("parameter pack argument %qE must be at the "
4436 "end of the template argument list", arg
);
4438 error ("parameter pack argument %qT must be at the "
4439 "end of the template argument list", arg
);
4443 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4444 /* We only care about the pattern. */
4445 arg
= PACK_EXPANSION_PATTERN (arg
);
4447 if (/* These first two lines are the `non-type' bit. */
4449 && TREE_CODE (arg
) != TEMPLATE_DECL
4450 /* This next line is the `argument expression is not just a
4451 simple identifier' condition and also the `specialized
4452 non-type argument' bit. */
4453 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4455 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4456 || (packed_args
&& uses_template_parms (arg
)))
4457 error ("template argument %qE involves template parameter(s)",
4461 /* Look at the corresponding template parameter,
4462 marking which template parameters its type depends
4464 tree type
= TREE_TYPE (parm
);
4468 /* We haven't yet initialized TPD2. Do so now. */
4469 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4470 /* The number of parameters here is the number in the
4471 main template, which, as checked in the assertion
4473 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4475 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4478 /* Mark the template parameters. But this time, we're
4479 looking for the template parameters of the main
4480 template, not in the specialization. */
4481 tpd2
.current_arg
= i
;
4482 tpd2
.arg_uses_template_parms
[i
] = 0;
4483 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4484 for_each_template_parm (type
,
4485 &mark_template_parm
,
4488 /*include_nondeduced_p=*/false);
4490 if (tpd2
.arg_uses_template_parms
[i
])
4492 /* The type depended on some template parameters.
4493 If they are fully specialized in the
4494 specialization, that's OK. */
4497 for (j
= 0; j
< nargs
; ++j
)
4498 if (tpd2
.parms
[j
] != 0
4499 && tpd
.arg_uses_template_parms
[j
])
4502 error_n (input_location
, count
,
4503 "type %qT of template argument %qE depends "
4504 "on a template parameter",
4505 "type %qT of template argument %qE depends "
4506 "on template parameters",
4515 /* We should only get here once. */
4516 gcc_assert (!COMPLETE_TYPE_P (type
));
4518 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4519 = tree_cons (specargs
, inner_parms
,
4520 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4521 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4523 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4524 inst
= TREE_CHAIN (inst
))
4526 tree inst_type
= TREE_VALUE (inst
);
4527 if (COMPLETE_TYPE_P (inst_type
)
4528 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4530 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4531 if (spec
&& TREE_TYPE (spec
) == type
)
4532 permerror (input_location
,
4533 "partial specialization of %qT after instantiation "
4534 "of %qT", type
, inst_type
);
4541 /* Check that a template declaration's use of default arguments and
4542 parameter packs is not invalid. Here, PARMS are the template
4543 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4544 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4548 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4549 declaration (but not a definition); 1 indicates a declaration, 2
4550 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4551 emitted for extraneous default arguments.
4553 Returns TRUE if there were no errors found, FALSE otherwise. */
4556 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
4557 int is_partial
, int is_friend_decl
)
4560 int last_level_to_check
;
4562 bool no_errors
= true;
4566 A default template-argument shall not be specified in a
4567 function template declaration or a function template definition, nor
4568 in the template-parameter-list of the definition of a member of a
4571 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4572 /* You can't have a function template declaration in a local
4573 scope, nor you can you define a member of a class template in a
4577 if (current_class_type
4578 && !TYPE_BEING_DEFINED (current_class_type
)
4579 && DECL_LANG_SPECIFIC (decl
)
4580 && DECL_DECLARES_FUNCTION_P (decl
)
4581 /* If this is either a friend defined in the scope of the class
4582 or a member function. */
4583 && (DECL_FUNCTION_MEMBER_P (decl
)
4584 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4585 : DECL_FRIEND_CONTEXT (decl
)
4586 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4588 /* And, if it was a member function, it really was defined in
4589 the scope of the class. */
4590 && (!DECL_FUNCTION_MEMBER_P (decl
)
4591 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4592 /* We already checked these parameters when the template was
4593 declared, so there's no need to do it again now. This function
4594 was defined in class scope, but we're processing it's body now
4595 that the class is complete. */
4598 /* Core issue 226 (C++0x only): the following only applies to class
4600 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4604 If a template-parameter has a default template-argument, all
4605 subsequent template-parameters shall have a default
4606 template-argument supplied. */
4607 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4609 tree inner_parms
= TREE_VALUE (parm_level
);
4610 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4611 int seen_def_arg_p
= 0;
4614 for (i
= 0; i
< ntparms
; ++i
)
4616 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4618 if (parm
== error_mark_node
)
4621 if (TREE_PURPOSE (parm
))
4623 else if (seen_def_arg_p
4624 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4626 error ("no default argument for %qD", TREE_VALUE (parm
));
4627 /* For better subsequent error-recovery, we indicate that
4628 there should have been a default argument. */
4629 TREE_PURPOSE (parm
) = error_mark_node
;
4635 /* Don't complain about an enclosing partial
4637 && parm_level
== parms
4638 && TREE_CODE (decl
) == TYPE_DECL
4640 && template_parameter_pack_p (TREE_VALUE (parm
)))
4642 /* A primary class template can only have one
4643 parameter pack, at the end of the template
4646 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4647 error ("parameter pack %qE must be at the end of the"
4648 " template parameter list", TREE_VALUE (parm
));
4650 error ("parameter pack %qT must be at the end of the"
4651 " template parameter list",
4652 TREE_TYPE (TREE_VALUE (parm
)));
4654 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4662 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4666 /* For an ordinary class template, default template arguments are
4667 allowed at the innermost level, e.g.:
4668 template <class T = int>
4670 but, in a partial specialization, they're not allowed even
4671 there, as we have in [temp.class.spec]:
4673 The template parameter list of a specialization shall not
4674 contain default template argument values.
4676 So, for a partial specialization, or for a function template
4677 (in C++98/C++03), we look at all of them. */
4680 /* But, for a primary class template that is not a partial
4681 specialization we look at all template parameters except the
4683 parms
= TREE_CHAIN (parms
);
4685 /* Figure out what error message to issue. */
4686 if (is_friend_decl
== 2)
4687 msg
= G_("default template arguments may not be used in function template "
4688 "friend re-declaration");
4689 else if (is_friend_decl
)
4690 msg
= G_("default template arguments may not be used in function template "
4691 "friend declarations");
4692 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4693 msg
= G_("default template arguments may not be used in function templates "
4694 "without -std=c++11 or -std=gnu++11");
4695 else if (is_partial
)
4696 msg
= G_("default template arguments may not be used in "
4697 "partial specializations");
4699 msg
= G_("default argument for template parameter for class enclosing %qD");
4701 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4702 /* If we're inside a class definition, there's no need to
4703 examine the parameters to the class itself. On the one
4704 hand, they will be checked when the class is defined, and,
4705 on the other, default arguments are valid in things like:
4706 template <class T = double>
4707 struct S { template <class U> void f(U); };
4708 Here the default argument for `S' has no bearing on the
4709 declaration of `f'. */
4710 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4712 /* Check everything. */
4713 last_level_to_check
= 0;
4715 for (parm_level
= parms
;
4716 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4717 parm_level
= TREE_CHAIN (parm_level
))
4719 tree inner_parms
= TREE_VALUE (parm_level
);
4723 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4724 for (i
= 0; i
< ntparms
; ++i
)
4726 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4729 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4734 if (is_friend_decl
== 2)
4741 /* Clear out the default argument so that we are not
4743 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4747 /* At this point, if we're still interested in issuing messages,
4748 they must apply to classes surrounding the object declared. */
4750 msg
= G_("default argument for template parameter for class "
4757 /* Worker for push_template_decl_real, called via
4758 for_each_template_parm. DATA is really an int, indicating the
4759 level of the parameters we are interested in. If T is a template
4760 parameter of that level, return nonzero. */
4763 template_parm_this_level_p (tree t
, void* data
)
4765 int this_level
= *(int *)data
;
4768 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4769 level
= TEMPLATE_PARM_LEVEL (t
);
4771 level
= TEMPLATE_TYPE_LEVEL (t
);
4772 return level
== this_level
;
4775 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4776 parameters given by current_template_args, or reuses a
4777 previously existing one, if appropriate. Returns the DECL, or an
4778 equivalent one, if it is replaced via a call to duplicate_decls.
4780 If IS_FRIEND is true, DECL is a friend declaration. */
4783 push_template_decl_real (tree decl
, bool is_friend
)
4791 int new_template_p
= 0;
4792 /* True if the template is a member template, in the sense of
4794 bool member_template_p
= false;
4796 if (decl
== error_mark_node
|| !current_template_parms
)
4797 return error_mark_node
;
4799 /* See if this is a partial specialization. */
4800 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4801 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4802 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4804 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4808 /* For a friend, we want the context of the friend function, not
4809 the type of which it is a friend. */
4810 ctx
= CP_DECL_CONTEXT (decl
);
4811 else if (CP_DECL_CONTEXT (decl
)
4812 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4813 /* In the case of a virtual function, we want the class in which
4815 ctx
= CP_DECL_CONTEXT (decl
);
4817 /* Otherwise, if we're currently defining some class, the DECL
4818 is assumed to be a member of the class. */
4819 ctx
= current_scope ();
4821 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4824 if (!DECL_CONTEXT (decl
))
4825 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4827 /* See if this is a primary template. */
4828 if (is_friend
&& ctx
)
4829 /* A friend template that specifies a class context, i.e.
4830 template <typename T> friend void A<T>::f();
4834 primary
= template_parm_scope_p ();
4838 if (DECL_CLASS_SCOPE_P (decl
))
4839 member_template_p
= true;
4840 if (TREE_CODE (decl
) == TYPE_DECL
4841 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4843 error ("template class without a name");
4844 return error_mark_node
;
4846 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4848 if (DECL_DESTRUCTOR_P (decl
))
4852 A destructor shall not be a member template. */
4853 error ("destructor %qD declared as member template", decl
);
4854 return error_mark_node
;
4856 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4857 && (!prototype_p (TREE_TYPE (decl
))
4858 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4859 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4860 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4861 == void_list_node
)))
4863 /* [basic.stc.dynamic.allocation]
4865 An allocation function can be a function
4866 template. ... Template allocation functions shall
4867 have two or more parameters. */
4868 error ("invalid template declaration of %qD", decl
);
4869 return error_mark_node
;
4872 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4873 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4875 else if (TREE_CODE (decl
) == TYPE_DECL
4876 && TYPE_DECL_ALIAS_P (decl
))
4877 /* alias-declaration */
4878 gcc_assert (!DECL_ARTIFICIAL (decl
));
4881 error ("template declaration of %q#D", decl
);
4882 return error_mark_node
;
4886 /* Check to see that the rules regarding the use of default
4887 arguments are not being violated. */
4888 check_default_tmpl_args (decl
, current_template_parms
,
4889 primary
, is_partial
, /*is_friend_decl=*/0);
4891 /* Ensure that there are no parameter packs in the type of this
4892 declaration that have not been expanded. */
4893 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4895 /* Check each of the arguments individually to see if there are
4896 any bare parameter packs. */
4897 tree type
= TREE_TYPE (decl
);
4898 tree arg
= DECL_ARGUMENTS (decl
);
4899 tree argtype
= TYPE_ARG_TYPES (type
);
4901 while (arg
&& argtype
)
4903 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4904 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4906 /* This is a PARM_DECL that contains unexpanded parameter
4907 packs. We have already complained about this in the
4908 check_for_bare_parameter_packs call, so just replace
4909 these types with ERROR_MARK_NODE. */
4910 TREE_TYPE (arg
) = error_mark_node
;
4911 TREE_VALUE (argtype
) = error_mark_node
;
4914 arg
= DECL_CHAIN (arg
);
4915 argtype
= TREE_CHAIN (argtype
);
4918 /* Check for bare parameter packs in the return type and the
4919 exception specifiers. */
4920 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4921 /* Errors were already issued, set return type to int
4922 as the frontend doesn't expect error_mark_node as
4924 TREE_TYPE (type
) = integer_type_node
;
4925 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4926 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4928 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4929 && TYPE_DECL_ALIAS_P (decl
))
4930 ? DECL_ORIGINAL_TYPE (decl
)
4931 : TREE_TYPE (decl
)))
4933 TREE_TYPE (decl
) = error_mark_node
;
4934 return error_mark_node
;
4938 return process_partial_specialization (decl
);
4940 args
= current_template_args ();
4943 || TREE_CODE (ctx
) == FUNCTION_DECL
4944 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4945 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4947 if (DECL_LANG_SPECIFIC (decl
)
4948 && DECL_TEMPLATE_INFO (decl
)
4949 && DECL_TI_TEMPLATE (decl
))
4950 tmpl
= DECL_TI_TEMPLATE (decl
);
4951 /* If DECL is a TYPE_DECL for a class-template, then there won't
4952 be DECL_LANG_SPECIFIC. The information equivalent to
4953 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4954 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4955 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4956 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4958 /* Since a template declaration already existed for this
4959 class-type, we must be redeclaring it here. Make sure
4960 that the redeclaration is valid. */
4961 redeclare_class_template (TREE_TYPE (decl
),
4962 current_template_parms
);
4963 /* We don't need to create a new TEMPLATE_DECL; just use the
4964 one we already had. */
4965 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4969 tmpl
= build_template_decl (decl
, current_template_parms
,
4973 if (DECL_LANG_SPECIFIC (decl
)
4974 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4976 /* A specialization of a member template of a template
4978 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4979 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4980 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4986 tree a
, t
, current
, parms
;
4988 tree tinfo
= get_template_info (decl
);
4992 error ("template definition of non-template %q#D", decl
);
4993 return error_mark_node
;
4996 tmpl
= TI_TEMPLATE (tinfo
);
4998 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4999 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
5000 && DECL_TEMPLATE_SPECIALIZATION (decl
)
5001 && DECL_MEMBER_TEMPLATE_P (tmpl
))
5005 /* The declaration is a specialization of a member
5006 template, declared outside the class. Therefore, the
5007 innermost template arguments will be NULL, so we
5008 replace them with the arguments determined by the
5009 earlier call to check_explicit_specialization. */
5010 args
= DECL_TI_ARGS (decl
);
5013 = build_template_decl (decl
, current_template_parms
,
5015 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
5016 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
5017 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
5018 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
5019 DECL_TEMPLATE_INFO (new_tmpl
)
5020 = build_template_info (tmpl
, args
);
5022 register_specialization (new_tmpl
,
5023 most_general_template (tmpl
),
5029 /* Make sure the template headers we got make sense. */
5031 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5032 i
= TMPL_PARMS_DEPTH (parms
);
5033 if (TMPL_ARGS_DEPTH (args
) != i
)
5035 error ("expected %d levels of template parms for %q#D, got %d",
5036 i
, decl
, TMPL_ARGS_DEPTH (args
));
5039 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5041 a
= TMPL_ARGS_LEVEL (args
, i
);
5042 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5044 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5046 if (current
== decl
)
5047 error ("got %d template parameters for %q#D",
5048 TREE_VEC_LENGTH (a
), decl
);
5050 error ("got %d template parameters for %q#T",
5051 TREE_VEC_LENGTH (a
), current
);
5052 error (" but %d required", TREE_VEC_LENGTH (t
));
5053 return error_mark_node
;
5056 if (current
== decl
)
5058 else if (current
== NULL_TREE
)
5059 /* Can happen in erroneous input. */
5062 current
= (TYPE_P (current
)
5063 ? TYPE_CONTEXT (current
)
5064 : DECL_CONTEXT (current
));
5067 /* Check that the parms are used in the appropriate qualifying scopes
5068 in the declarator. */
5069 if (!comp_template_args
5071 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5074 template arguments to %qD do not match original template %qD",
5075 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5076 if (!uses_template_parms (TI_ARGS (tinfo
)))
5077 inform (input_location
, "use template<> for an explicit specialization");
5078 /* Avoid crash in import_export_decl. */
5079 DECL_INTERFACE_KNOWN (decl
) = 1;
5080 return error_mark_node
;
5084 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5085 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5087 /* Push template declarations for global functions and types. Note
5088 that we do not try to push a global template friend declared in a
5089 template class; such a thing may well depend on the template
5090 parameters of the class. */
5091 if (new_template_p
&& !ctx
5092 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5094 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5095 if (tmpl
== error_mark_node
)
5096 return error_mark_node
;
5098 /* Hide template friend classes that haven't been declared yet. */
5099 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5101 DECL_ANTICIPATED (tmpl
) = 1;
5102 DECL_FRIEND_P (tmpl
) = 1;
5108 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5111 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5112 if (DECL_CONV_FN_P (tmpl
))
5114 int depth
= TMPL_PARMS_DEPTH (parms
);
5116 /* It is a conversion operator. See if the type converted to
5117 depends on innermost template operands. */
5119 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5121 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5124 /* Give template template parms a DECL_CONTEXT of the template
5125 for which they are a parameter. */
5126 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5127 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5129 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5130 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5131 DECL_CONTEXT (parm
) = tmpl
;
5135 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5136 back to its most general template. If TMPL is a specialization,
5137 ARGS may only have the innermost set of arguments. Add the missing
5138 argument levels if necessary. */
5139 if (DECL_TEMPLATE_INFO (tmpl
))
5140 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5142 info
= build_template_info (tmpl
, args
);
5144 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5145 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5148 if (primary
&& !DECL_LANG_SPECIFIC (decl
))
5149 retrofit_lang_decl (decl
);
5150 if (DECL_LANG_SPECIFIC (decl
))
5151 DECL_TEMPLATE_INFO (decl
) = info
;
5154 return DECL_TEMPLATE_RESULT (tmpl
);
5158 push_template_decl (tree decl
)
5160 return push_template_decl_real (decl
, false);
5163 /* Called when a class template TYPE is redeclared with the indicated
5164 template PARMS, e.g.:
5166 template <class T> struct S;
5167 template <class T> struct S {}; */
5170 redeclare_class_template (tree type
, tree parms
)
5176 if (!TYPE_TEMPLATE_INFO (type
))
5178 error ("%qT is not a template type", type
);
5182 tmpl
= TYPE_TI_TEMPLATE (type
);
5183 if (!PRIMARY_TEMPLATE_P (tmpl
))
5184 /* The type is nested in some template class. Nothing to worry
5185 about here; there are no new template parameters for the nested
5191 error ("template specifiers not specified in declaration of %qD",
5196 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5197 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5199 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5201 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5202 "redeclared with %d template parameter",
5203 "redeclared with %d template parameters",
5204 TREE_VEC_LENGTH (parms
));
5205 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5206 "previous declaration %q+D used %d template parameter",
5207 "previous declaration %q+D used %d template parameters",
5208 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5212 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5219 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5220 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5223 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5224 if (tmpl_parm
== error_mark_node
)
5227 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5228 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5229 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5231 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5233 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5234 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5235 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5236 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5237 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5238 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5239 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5240 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5241 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5243 error ("template parameter %q+#D", tmpl_parm
);
5244 error ("redeclared here as %q#D", parm
);
5248 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5250 /* We have in [temp.param]:
5252 A template-parameter may not be given default arguments
5253 by two different declarations in the same scope. */
5254 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5255 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5256 "original definition appeared here");
5260 if (parm_default
!= NULL_TREE
)
5261 /* Update the previous template parameters (which are the ones
5262 that will really count) with the new default value. */
5263 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5264 else if (tmpl_default
!= NULL_TREE
)
5265 /* Update the new parameters, too; they'll be used as the
5266 parameters for any members. */
5267 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5273 /* Simplify EXPR if it is a non-dependent expression. Returns the
5274 (possibly simplified) expression. */
5277 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5279 if (expr
== NULL_TREE
)
5282 /* If we're in a template, but EXPR isn't value dependent, simplify
5283 it. We're supposed to treat:
5285 template <typename T> void f(T[1 + 1]);
5286 template <typename T> void f(T[2]);
5288 as two declarations of the same function, for example. */
5289 if (processing_template_decl
5290 && !type_dependent_expression_p (expr
)
5291 && potential_constant_expression (expr
)
5292 && !value_dependent_expression_p (expr
))
5294 HOST_WIDE_INT saved_processing_template_decl
;
5296 saved_processing_template_decl
= processing_template_decl
;
5297 processing_template_decl
= 0;
5298 expr
= tsubst_copy_and_build (expr
,
5301 /*in_decl=*/NULL_TREE
,
5302 /*function_p=*/false,
5303 /*integral_constant_expression_p=*/true);
5304 processing_template_decl
= saved_processing_template_decl
;
5310 fold_non_dependent_expr (tree expr
)
5312 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5315 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5316 template declaration, or a TYPE_DECL for an alias declaration. */
5319 alias_type_or_template_p (tree t
)
5323 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5326 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5327 || DECL_ALIAS_TEMPLATE_P (t
));
5330 /* Return TRUE iff is a specialization of an alias template. */
5333 alias_template_specialization_p (tree t
)
5337 return (primary_template_instantiation_p (t
)
5338 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5341 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5342 must be a function or a pointer-to-function type, as specified
5343 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5344 and check that the resulting function has external linkage. */
5347 convert_nontype_argument_function (tree type
, tree expr
)
5351 linkage_kind linkage
;
5353 fn
= instantiate_type (type
, fns
, tf_none
);
5354 if (fn
== error_mark_node
)
5355 return error_mark_node
;
5358 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5359 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5360 if (BASELINK_P (fn_no_ptr
))
5361 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5363 /* [temp.arg.nontype]/1
5365 A template-argument for a non-type, non-template template-parameter
5368 -- the address of an object or function with external [C++11: or
5369 internal] linkage. */
5370 linkage
= decl_linkage (fn_no_ptr
);
5371 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5373 if (cxx_dialect
>= cxx0x
)
5374 error ("%qE is not a valid template argument for type %qT "
5375 "because %qD has no linkage",
5376 expr
, type
, fn_no_ptr
);
5378 error ("%qE is not a valid template argument for type %qT "
5379 "because %qD does not have external linkage",
5380 expr
, type
, fn_no_ptr
);
5387 /* Subroutine of convert_nontype_argument.
5388 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5389 Emit an error otherwise. */
5392 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5393 tsubst_flags_t complain
)
5396 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5398 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5400 if (complain
& tf_error
)
5402 error ("%qE is not a valid template argument for type %qT",
5404 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5409 /* Returns TRUE iff the address of OP is value-dependent.
5411 14.6.2.4 [temp.dep.temp]:
5412 A non-integral non-type template-argument is dependent if its type is
5413 dependent or it has either of the following forms
5416 and contains a nested-name-specifier which specifies a class-name that
5417 names a dependent type.
5419 We generalize this to just say that the address of a member of a
5420 dependent class is value-dependent; the above doesn't cover the
5421 address of a static data member named with an unqualified-id. */
5424 has_value_dependent_address (tree op
)
5426 /* We could use get_inner_reference here, but there's no need;
5427 this is only relevant for template non-type arguments, which
5428 can only be expressed as &id-expression. */
5431 tree ctx
= CP_DECL_CONTEXT (op
);
5432 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5439 /* The next set of functions are used for providing helpful explanatory
5440 diagnostics for failed overload resolution. Their messages should be
5441 indented by two spaces for consistency with the messages in
5445 unify_success (bool explain_p ATTRIBUTE_UNUSED
)
5451 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5454 inform (input_location
,
5455 " couldn't deduce template parameter %qD", parm
);
5460 unify_invalid (bool explain_p ATTRIBUTE_UNUSED
)
5466 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5469 inform (input_location
,
5470 " types %qT and %qT have incompatible cv-qualifiers",
5476 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5479 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5484 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5487 inform (input_location
,
5488 " template parameter %qD is not a parameter pack, but "
5495 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5498 inform (input_location
,
5499 " template argument %qE does not match "
5500 "pointer-to-member constant %qE",
5506 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5509 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5514 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5517 inform (input_location
,
5518 " inconsistent parameter pack deduction with %qT and %qT",
5524 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5529 inform (input_location
,
5530 " deduced conflicting types for parameter %qT (%qT and %qT)",
5531 parm
, first
, second
);
5533 inform (input_location
,
5534 " deduced conflicting values for non-type parameter "
5535 "%qE (%qE and %qE)", parm
, first
, second
);
5541 unify_vla_arg (bool explain_p
, tree arg
)
5544 inform (input_location
,
5545 " variable-sized array type %qT is not "
5546 "a valid template argument",
5552 unify_method_type_error (bool explain_p
, tree arg
)
5555 inform (input_location
,
5556 " member function type %qT is not a valid template argument",
5562 unify_arity (bool explain_p
, int have
, int wanted
)
5565 inform_n (input_location
, wanted
,
5566 " candidate expects %d argument, %d provided",
5567 " candidate expects %d arguments, %d provided",
5573 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5575 return unify_arity (explain_p
, have
, wanted
);
5579 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5581 return unify_arity (explain_p
, have
, wanted
);
5585 unify_arg_conversion (bool explain_p
, tree to_type
,
5586 tree from_type
, tree arg
)
5589 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5590 arg
, from_type
, to_type
);
5595 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5596 tree parm
, tree arg
)
5601 case tbr_ambiguous_baseclass
:
5602 inform (input_location
, " %qT is an ambiguous base class of %qT",
5606 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5613 unify_inconsistent_template_template_parameters (bool explain_p
)
5616 inform (input_location
,
5617 " template parameters of a template template argument are "
5618 "inconsistent with other deduced template arguments");
5623 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5626 inform (input_location
,
5627 " can't deduce a template for %qT from non-template type %qT",
5633 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5636 inform (input_location
,
5637 " template argument %qE does not match %qD", arg
, parm
);
5642 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5645 inform (input_location
,
5646 " could not resolve address from overloaded function %qE",
5651 /* Attempt to convert the non-type template parameter EXPR to the
5652 indicated TYPE. If the conversion is successful, return the
5653 converted value. If the conversion is unsuccessful, return
5654 NULL_TREE if we issued an error message, or error_mark_node if we
5655 did not. We issue error messages for out-and-out bad template
5656 parameters, but not simply because the conversion failed, since we
5657 might be just trying to do argument deduction. Both TYPE and EXPR
5658 must be non-dependent.
5660 The conversion follows the special rules described in
5661 [temp.arg.nontype], and it is much more strict than an implicit
5664 This function is called twice for each template argument (see
5665 lookup_template_class for a more accurate description of this
5666 problem). This means that we need to handle expressions which
5667 are not valid in a C++ source, but can be created from the
5668 first call (for instance, casts to perform conversions). These
5669 hacks can go away after we fix the double coercion problem. */
5672 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5676 /* Detect immediately string literals as invalid non-type argument.
5677 This special-case is not needed for correctness (we would easily
5678 catch this later), but only to provide better diagnostic for this
5679 common user mistake. As suggested by DR 100, we do not mention
5680 linkage issues in the diagnostic as this is not the point. */
5681 /* FIXME we're making this OK. */
5682 if (TREE_CODE (expr
) == STRING_CST
)
5684 if (complain
& tf_error
)
5685 error ("%qE is not a valid template argument for type %qT "
5686 "because string literals can never be used in this context",
5691 /* Add the ADDR_EXPR now for the benefit of
5692 value_dependent_expression_p. */
5693 if (TYPE_PTROBV_P (type
)
5694 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5696 expr
= decay_conversion (expr
, complain
);
5697 if (expr
== error_mark_node
)
5698 return error_mark_node
;
5701 /* If we are in a template, EXPR may be non-dependent, but still
5702 have a syntactic, rather than semantic, form. For example, EXPR
5703 might be a SCOPE_REF, rather than the VAR_DECL to which the
5704 SCOPE_REF refers. Preserving the qualifying scope is necessary
5705 so that access checking can be performed when the template is
5706 instantiated -- but here we need the resolved form so that we can
5707 convert the argument. */
5708 if (TYPE_REF_OBJ_P (type
)
5709 && has_value_dependent_address (expr
))
5710 /* If we want the address and it's value-dependent, don't fold. */;
5711 else if (!type_unknown_p (expr
))
5712 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5713 if (error_operand_p (expr
))
5714 return error_mark_node
;
5715 expr_type
= TREE_TYPE (expr
);
5716 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5717 expr
= mark_lvalue_use (expr
);
5719 expr
= mark_rvalue_use (expr
);
5721 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5722 to a non-type argument of "nullptr". */
5723 if (expr
== nullptr_node
5724 && (TYPE_PTR_P (type
) || TYPE_PTR_TO_MEMBER_P (type
)))
5725 expr
= convert (type
, expr
);
5727 /* In C++11, integral or enumeration non-type template arguments can be
5728 arbitrary constant expressions. Pointer and pointer to
5729 member arguments can be general constant expressions that evaluate
5730 to a null value, but otherwise still need to be of a specific form. */
5731 if (cxx_dialect
>= cxx0x
)
5733 if (TREE_CODE (expr
) == PTRMEM_CST
)
5734 /* A PTRMEM_CST is already constant, and a valid template
5735 argument for a parameter of pointer to member type, we just want
5736 to leave it in that form rather than lower it to a
5738 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5739 expr
= maybe_constant_value (expr
);
5740 else if (TYPE_PTR_P (type
)
5741 || TYPE_PTR_TO_MEMBER_P (type
))
5743 tree folded
= maybe_constant_value (expr
);
5744 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5745 : null_member_pointer_value_p (folded
))
5750 /* HACK: Due to double coercion, we can get a
5751 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5752 which is the tree that we built on the first call (see
5753 below when coercing to reference to object or to reference to
5754 function). We just strip everything and get to the arg.
5755 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5757 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5759 tree probe_type
, probe
= expr
;
5760 if (REFERENCE_REF_P (probe
))
5761 probe
= TREE_OPERAND (probe
, 0);
5762 probe_type
= TREE_TYPE (probe
);
5763 if (TREE_CODE (probe
) == NOP_EXPR
)
5765 /* ??? Maybe we could use convert_from_reference here, but we
5766 would need to relax its constraints because the NOP_EXPR
5767 could actually change the type to something more cv-qualified,
5768 and this is not folded by convert_from_reference. */
5769 tree addr
= TREE_OPERAND (probe
, 0);
5770 gcc_assert (TREE_CODE (probe_type
) == REFERENCE_TYPE
);
5771 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5772 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5773 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5774 (TREE_TYPE (probe_type
),
5775 TREE_TYPE (TREE_TYPE (addr
))));
5777 expr
= TREE_OPERAND (addr
, 0);
5778 expr_type
= TREE_TYPE (expr
);
5782 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5783 parameter is a pointer to object, through decay and
5784 qualification conversion. Let's strip everything. */
5785 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5788 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5789 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5790 /* Skip the ADDR_EXPR only if it is part of the decay for
5791 an array. Otherwise, it is part of the original argument
5792 in the source code. */
5793 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5794 expr
= TREE_OPERAND (expr
, 0);
5795 expr_type
= TREE_TYPE (expr
);
5798 /* [temp.arg.nontype]/5, bullet 1
5800 For a non-type template-parameter of integral or enumeration type,
5801 integral promotions (_conv.prom_) and integral conversions
5802 (_conv.integral_) are applied. */
5803 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5805 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5806 t
= maybe_constant_value (t
);
5807 if (t
!= error_mark_node
)
5810 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5811 return error_mark_node
;
5813 /* Notice that there are constant expressions like '4 % 0' which
5814 do not fold into integer constants. */
5815 if (TREE_CODE (expr
) != INTEGER_CST
)
5817 if (complain
& tf_error
)
5819 int errs
= errorcount
, warns
= warningcount
;
5820 if (processing_template_decl
5821 && !require_potential_constant_expression (expr
))
5823 expr
= cxx_constant_value (expr
);
5824 if (errorcount
> errs
|| warningcount
> warns
)
5825 inform (EXPR_LOC_OR_HERE (expr
),
5826 "in template argument for type %qT ", type
);
5827 if (expr
== error_mark_node
)
5829 /* else cxx_constant_value complained but gave us
5830 a real constant, so go ahead. */
5831 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5837 /* [temp.arg.nontype]/5, bullet 2
5839 For a non-type template-parameter of type pointer to object,
5840 qualification conversions (_conv.qual_) and the array-to-pointer
5841 conversion (_conv.array_) are applied. */
5842 else if (TYPE_PTROBV_P (type
))
5844 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5846 A template-argument for a non-type, non-template template-parameter
5847 shall be one of: [...]
5849 -- the name of a non-type template-parameter;
5850 -- the address of an object or function with external linkage, [...]
5851 expressed as "& id-expression" where the & is optional if the name
5852 refers to a function or array, or if the corresponding
5853 template-parameter is a reference.
5855 Here, we do not care about functions, as they are invalid anyway
5856 for a parameter of type pointer-to-object. */
5858 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5859 /* Non-type template parameters are OK. */
5861 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5862 /* Null pointer values are OK in C++11. */;
5863 else if (TREE_CODE (expr
) != ADDR_EXPR
5864 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5866 if (TREE_CODE (expr
) == VAR_DECL
)
5868 error ("%qD is not a valid template argument "
5869 "because %qD is a variable, not the address of "
5874 /* Other values, like integer constants, might be valid
5875 non-type arguments of some other type. */
5876 return error_mark_node
;
5882 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5883 ? TREE_OPERAND (expr
, 0) : expr
);
5884 if (TREE_CODE (decl
) != VAR_DECL
)
5886 error ("%qE is not a valid template argument of type %qT "
5887 "because %qE is not a variable",
5891 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5893 error ("%qE is not a valid template argument of type %qT "
5894 "because %qD does not have external linkage",
5898 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5900 error ("%qE is not a valid template argument of type %qT "
5901 "because %qD has no linkage",
5907 expr
= decay_conversion (expr
, complain
);
5908 if (expr
== error_mark_node
)
5909 return error_mark_node
;
5911 expr
= perform_qualification_conversions (type
, expr
);
5912 if (expr
== error_mark_node
)
5913 return error_mark_node
;
5915 /* [temp.arg.nontype]/5, bullet 3
5917 For a non-type template-parameter of type reference to object, no
5918 conversions apply. The type referred to by the reference may be more
5919 cv-qualified than the (otherwise identical) type of the
5920 template-argument. The template-parameter is bound directly to the
5921 template-argument, which must be an lvalue. */
5922 else if (TYPE_REF_OBJ_P (type
))
5924 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5926 return error_mark_node
;
5928 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5930 error ("%qE is not a valid template argument for type %qT "
5931 "because of conflicts in cv-qualification", expr
, type
);
5935 if (!real_lvalue_p (expr
))
5937 error ("%qE is not a valid template argument for type %qT "
5938 "because it is not an lvalue", expr
, type
);
5942 /* [temp.arg.nontype]/1
5944 A template-argument for a non-type, non-template template-parameter
5945 shall be one of: [...]
5947 -- the address of an object or function with external linkage. */
5948 if (TREE_CODE (expr
) == INDIRECT_REF
5949 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5951 expr
= TREE_OPERAND (expr
, 0);
5954 error ("%q#D is not a valid template argument for type %qT "
5955 "because a reference variable does not have a constant "
5956 "address", expr
, type
);
5963 error ("%qE is not a valid template argument for type %qT "
5964 "because it is not an object with external linkage",
5969 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5971 error ("%qE is not a valid template argument for type %qT "
5972 "because object %qD has not external linkage",
5977 expr
= build_nop (type
, build_address (expr
));
5979 /* [temp.arg.nontype]/5, bullet 4
5981 For a non-type template-parameter of type pointer to function, only
5982 the function-to-pointer conversion (_conv.func_) is applied. If the
5983 template-argument represents a set of overloaded functions (or a
5984 pointer to such), the matching function is selected from the set
5986 else if (TYPE_PTRFN_P (type
))
5988 /* If the argument is a template-id, we might not have enough
5989 context information to decay the pointer. */
5990 if (!type_unknown_p (expr_type
))
5992 expr
= decay_conversion (expr
, complain
);
5993 if (expr
== error_mark_node
)
5994 return error_mark_node
;
5997 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5998 /* Null pointer values are OK in C++11. */
5999 return perform_qualification_conversions (type
, expr
);
6001 expr
= convert_nontype_argument_function (type
, expr
);
6002 if (!expr
|| expr
== error_mark_node
)
6005 if (TREE_CODE (expr
) != ADDR_EXPR
)
6007 error ("%qE is not a valid template argument for type %qT", expr
, type
);
6008 error ("it must be the address of a function with external linkage");
6012 /* [temp.arg.nontype]/5, bullet 5
6014 For a non-type template-parameter of type reference to function, no
6015 conversions apply. If the template-argument represents a set of
6016 overloaded functions, the matching function is selected from the set
6018 else if (TYPE_REFFN_P (type
))
6020 if (TREE_CODE (expr
) == ADDR_EXPR
)
6022 error ("%qE is not a valid template argument for type %qT "
6023 "because it is a pointer", expr
, type
);
6024 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
6028 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
6029 if (!expr
|| expr
== error_mark_node
)
6032 expr
= build_nop (type
, build_address (expr
));
6034 /* [temp.arg.nontype]/5, bullet 6
6036 For a non-type template-parameter of type pointer to member function,
6037 no conversions apply. If the template-argument represents a set of
6038 overloaded member functions, the matching member function is selected
6039 from the set (_over.over_). */
6040 else if (TYPE_PTRMEMFUNC_P (type
))
6042 expr
= instantiate_type (type
, expr
, tf_none
);
6043 if (expr
== error_mark_node
)
6044 return error_mark_node
;
6046 /* [temp.arg.nontype] bullet 1 says the pointer to member
6047 expression must be a pointer-to-member constant. */
6048 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6049 return error_mark_node
;
6051 /* There is no way to disable standard conversions in
6052 resolve_address_of_overloaded_function (called by
6053 instantiate_type). It is possible that the call succeeded by
6054 converting &B::I to &D::I (where B is a base of D), so we need
6055 to reject this conversion here.
6057 Actually, even if there was a way to disable standard conversions,
6058 it would still be better to reject them here so that we can
6059 provide a superior diagnostic. */
6060 if (!same_type_p (TREE_TYPE (expr
), type
))
6062 error ("%qE is not a valid template argument for type %qT "
6063 "because it is of type %qT", expr
, type
,
6065 /* If we are just one standard conversion off, explain. */
6066 if (can_convert (type
, TREE_TYPE (expr
), complain
))
6067 inform (input_location
,
6068 "standard conversions are not allowed in this context");
6072 /* [temp.arg.nontype]/5, bullet 7
6074 For a non-type template-parameter of type pointer to data member,
6075 qualification conversions (_conv.qual_) are applied. */
6076 else if (TYPE_PTRMEM_P (type
))
6078 /* [temp.arg.nontype] bullet 1 says the pointer to member
6079 expression must be a pointer-to-member constant. */
6080 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6081 return error_mark_node
;
6083 expr
= perform_qualification_conversions (type
, expr
);
6084 if (expr
== error_mark_node
)
6087 else if (NULLPTR_TYPE_P (type
))
6089 if (expr
!= nullptr_node
)
6091 error ("%qE is not a valid template argument for type %qT "
6092 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6097 /* A template non-type parameter must be one of the above. */
6101 /* Sanity check: did we actually convert the argument to the
6103 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6104 (type
, TREE_TYPE (expr
)));
6108 /* Subroutine of coerce_template_template_parms, which returns 1 if
6109 PARM_PARM and ARG_PARM match using the rule for the template
6110 parameters of template template parameters. Both PARM and ARG are
6111 template parameters; the rest of the arguments are the same as for
6112 coerce_template_template_parms.
6115 coerce_template_template_parm (tree parm
,
6117 tsubst_flags_t complain
,
6121 if (arg
== NULL_TREE
|| arg
== error_mark_node
6122 || parm
== NULL_TREE
|| parm
== error_mark_node
)
6125 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6128 switch (TREE_CODE (parm
))
6131 /* We encounter instantiations of templates like
6132 template <template <template <class> class> class TT>
6135 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6136 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6138 if (!coerce_template_template_parms
6139 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6145 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6146 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6147 /* Argument is a parameter pack but parameter is not. */
6152 /* The tsubst call is used to handle cases such as
6154 template <int> class C {};
6155 template <class T, template <T> class TT> class D {};
6158 i.e. the parameter list of TT depends on earlier parameters. */
6159 if (!uses_template_parms (TREE_TYPE (arg
))
6161 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6165 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6166 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6167 /* Argument is a parameter pack but parameter is not. */
6180 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6181 template template parameters. Both PARM_PARMS and ARG_PARMS are
6182 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6185 Consider the example:
6186 template <class T> class A;
6187 template<template <class U> class TT> class B;
6189 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6190 the parameters to A, and OUTER_ARGS contains A. */
6193 coerce_template_template_parms (tree parm_parms
,
6195 tsubst_flags_t complain
,
6199 int nparms
, nargs
, i
;
6203 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6204 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6206 nparms
= TREE_VEC_LENGTH (parm_parms
);
6207 nargs
= TREE_VEC_LENGTH (arg_parms
);
6209 /* Determine whether we have a parameter pack at the end of the
6210 template template parameter's template parameter list. */
6211 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6213 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6215 if (parm
== error_mark_node
)
6218 switch (TREE_CODE (parm
))
6222 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6227 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6237 && !(variadic_p
&& nargs
>= nparms
- 1))
6240 /* Check all of the template parameters except the parameter pack at
6241 the end (if any). */
6242 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6244 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6245 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6248 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6249 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6251 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6259 /* Check each of the template parameters in the template
6260 argument against the template parameter pack at the end of
6261 the template template parameter. */
6262 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6265 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6267 for (; i
< nargs
; ++i
)
6269 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6272 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6274 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6283 /* Verifies that the deduced template arguments (in TARGS) for the
6284 template template parameters (in TPARMS) represent valid bindings,
6285 by comparing the template parameter list of each template argument
6286 to the template parameter list of its corresponding template
6287 template parameter, in accordance with DR150. This
6288 routine can only be called after all template arguments have been
6289 deduced. It will return TRUE if all of the template template
6290 parameter bindings are okay, FALSE otherwise. */
6292 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6294 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6297 /* We're dealing with template parms in this process. */
6298 ++processing_template_decl
;
6300 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6302 for (i
= 0; i
< ntparms
; ++i
)
6304 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6305 tree targ
= TREE_VEC_ELT (targs
, i
);
6307 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6309 tree packed_args
= NULL_TREE
;
6312 if (ARGUMENT_PACK_P (targ
))
6314 /* Look inside the argument pack. */
6315 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6316 len
= TREE_VEC_LENGTH (packed_args
);
6319 for (idx
= 0; idx
< len
; ++idx
)
6321 tree targ_parms
= NULL_TREE
;
6324 /* Extract the next argument from the argument
6326 targ
= TREE_VEC_ELT (packed_args
, idx
);
6328 if (PACK_EXPANSION_P (targ
))
6329 /* Look at the pattern of the pack expansion. */
6330 targ
= PACK_EXPANSION_PATTERN (targ
);
6332 /* Extract the template parameters from the template
6334 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6335 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6336 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6337 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6339 /* Verify that we can coerce the template template
6340 parameters from the template argument to the template
6341 parameter. This requires an exact match. */
6343 && !coerce_template_template_parms
6344 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6359 --processing_template_decl
;
6363 /* Since type attributes aren't mangled, we need to strip them from
6364 template type arguments. */
6367 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6370 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6372 mv
= TYPE_MAIN_VARIANT (arg
);
6373 arg
= strip_typedefs (arg
);
6374 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6375 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6377 if (complain
& tf_warning
)
6378 warning (0, "ignoring attributes on template argument %qT", arg
);
6379 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6380 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6385 /* Convert the indicated template ARG as necessary to match the
6386 indicated template PARM. Returns the converted ARG, or
6387 error_mark_node if the conversion was unsuccessful. Error and
6388 warning messages are issued under control of COMPLAIN. This
6389 conversion is for the Ith parameter in the parameter list. ARGS is
6390 the full set of template arguments deduced so far. */
6393 convert_template_argument (tree parm
,
6396 tsubst_flags_t complain
,
6402 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6404 if (TREE_CODE (arg
) == TREE_LIST
6405 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6407 /* The template argument was the name of some
6408 member function. That's usually
6409 invalid, but static members are OK. In any
6410 case, grab the underlying fields/functions
6411 and issue an error later if required. */
6412 orig_arg
= TREE_VALUE (arg
);
6413 TREE_TYPE (arg
) = unknown_type_node
;
6418 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6419 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6420 || requires_tmpl_type
);
6422 /* When determining whether an argument pack expansion is a template,
6423 look at the pattern. */
6424 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6425 arg
= PACK_EXPANSION_PATTERN (arg
);
6427 /* Deal with an injected-class-name used as a template template arg. */
6428 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6430 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6431 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6433 if (cxx_dialect
>= cxx0x
)
6434 /* OK under DR 1004. */;
6435 else if (complain
& tf_warning_or_error
)
6436 pedwarn (input_location
, OPT_Wpedantic
, "injected-class-name %qD"
6437 " used as template template argument", TYPE_NAME (arg
));
6438 else if (flag_pedantic_errors
)
6446 ((TREE_CODE (arg
) == TEMPLATE_DECL
6447 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6448 || (requires_tmpl_type
&& TREE_CODE (arg
) == TYPE_ARGUMENT_PACK
)
6449 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6450 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6453 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6454 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6455 arg
= TYPE_STUB_DECL (arg
);
6457 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6459 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6460 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6462 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6464 if (complain
& tf_error
)
6465 error ("invalid use of destructor %qE as a type", orig_arg
);
6466 return error_mark_node
;
6469 permerror (input_location
,
6470 "to refer to a type member of a template parameter, "
6471 "use %<typename %E%>", orig_arg
);
6473 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6474 TREE_OPERAND (arg
, 1),
6476 complain
& tf_error
);
6480 if (is_type
!= requires_type
)
6484 if (complain
& tf_error
)
6486 error ("type/value mismatch at argument %d in template "
6487 "parameter list for %qD",
6490 error (" expected a constant of type %qT, got %qT",
6492 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6493 else if (requires_tmpl_type
)
6494 error (" expected a class template, got %qE", orig_arg
);
6496 error (" expected a type, got %qE", orig_arg
);
6499 return error_mark_node
;
6501 if (is_tmpl_type
^ requires_tmpl_type
)
6503 if (in_decl
&& (complain
& tf_error
))
6505 error ("type/value mismatch at argument %d in template "
6506 "parameter list for %qD",
6509 error (" expected a type, got %qT", DECL_NAME (arg
));
6511 error (" expected a class template, got %qT", orig_arg
);
6513 return error_mark_node
;
6518 if (requires_tmpl_type
)
6520 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6522 else if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6523 /* The number of argument required is not known yet.
6524 Just accept it for now. */
6525 val
= TREE_TYPE (arg
);
6528 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6531 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6533 if (coerce_template_template_parms (parmparm
, argparm
,
6539 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6541 if (val
!= error_mark_node
)
6543 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6544 val
= TREE_TYPE (val
);
6545 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6546 val
= make_pack_expansion (val
);
6551 if (in_decl
&& (complain
& tf_error
))
6553 error ("type/value mismatch at argument %d in "
6554 "template parameter list for %qD",
6556 error (" expected a template of type %qD, got %qT",
6560 val
= error_mark_node
;
6566 /* We only form one instance of each template specialization.
6567 Therefore, if we use a non-canonical variant (i.e., a
6568 typedef), any future messages referring to the type will use
6569 the typedef, which is confusing if those future uses do not
6570 themselves also use the typedef. */
6572 val
= canonicalize_type_argument (val
, complain
);
6576 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6578 if (invalid_nontype_parm_type_p (t
, complain
))
6579 return error_mark_node
;
6581 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6583 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6587 /* Not sure if this is reachable, but it doesn't hurt
6589 error ("type mismatch in nontype parameter pack");
6590 val
= error_mark_node
;
6593 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6594 /* We used to call digest_init here. However, digest_init
6595 will report errors, which we don't want when complain
6596 is zero. More importantly, digest_init will try too
6597 hard to convert things: for example, `0' should not be
6598 converted to pointer type at this point according to
6599 the standard. Accepting this is not merely an
6600 extension, since deciding whether or not these
6601 conversions can occur is part of determining which
6602 function template to call, or whether a given explicit
6603 argument specification is valid. */
6604 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6608 if (val
== NULL_TREE
)
6609 val
= error_mark_node
;
6610 else if (val
== error_mark_node
&& (complain
& tf_error
))
6611 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6613 if (TREE_CODE (val
) == SCOPE_REF
)
6615 /* Strip typedefs from the SCOPE_REF. */
6616 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6617 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6619 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6620 QUALIFIED_NAME_IS_TEMPLATE (val
));
6627 /* Coerces the remaining template arguments in INNER_ARGS (from
6628 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6629 Returns the coerced argument pack. PARM_IDX is the position of this
6630 parameter in the template parameter list. ARGS is the original
6631 template argument list. */
6633 coerce_template_parameter_pack (tree parms
,
6641 tsubst_flags_t complain
)
6643 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6644 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6647 tree packed_types
= NULL_TREE
;
6649 if (arg_idx
> nargs
)
6652 packed_args
= make_tree_vec (nargs
- arg_idx
);
6654 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6655 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6657 /* When the template parameter is a non-type template
6658 parameter pack whose type uses parameter packs, we need
6659 to look at each of the template arguments
6660 separately. Build a vector of the types for these
6661 non-type template parameters in PACKED_TYPES. */
6663 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6664 packed_types
= tsubst_pack_expansion (expansion
, args
,
6667 if (packed_types
== error_mark_node
)
6668 return error_mark_node
;
6670 /* Check that we have the right number of arguments. */
6672 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6673 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6676 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6677 error ("wrong number of template arguments (%d, should be %d)",
6678 nargs
, needed_parms
);
6679 return error_mark_node
;
6682 /* If we aren't able to check the actual arguments now
6683 (because they haven't been expanded yet), we can at least
6684 verify that all of the types used for the non-type
6685 template parameter pack are, in fact, valid for non-type
6686 template parameters. */
6688 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6690 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6691 for (j
= 0; j
< len
; ++j
)
6693 tree t
= TREE_VEC_ELT (packed_types
, j
);
6694 if (invalid_nontype_parm_type_p (t
, complain
))
6695 return error_mark_node
;
6700 /* Convert the remaining arguments, which will be a part of the
6701 parameter pack "parm". */
6702 for (; arg_idx
< nargs
; ++arg_idx
)
6704 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6705 tree actual_parm
= TREE_VALUE (parm
);
6707 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6709 /* When we have a vector of types (corresponding to the
6710 non-type template parameter pack that uses parameter
6711 packs in its type, as mention above), and the
6712 argument is not an expansion (which expands to a
6713 currently unknown number of arguments), clone the
6714 parm and give it the next type in PACKED_TYPES. */
6715 actual_parm
= copy_node (actual_parm
);
6716 TREE_TYPE (actual_parm
) =
6717 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6720 if (arg
== error_mark_node
)
6722 if (complain
& tf_error
)
6723 error ("template argument %d is invalid", arg_idx
+ 1);
6726 arg
= convert_template_argument (actual_parm
,
6727 arg
, new_args
, complain
, parm_idx
,
6729 if (arg
== error_mark_node
)
6731 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6734 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6735 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6736 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6739 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6740 TREE_TYPE (argument_pack
)
6741 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6742 TREE_CONSTANT (argument_pack
) = 1;
6745 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6746 #ifdef ENABLE_CHECKING
6747 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6748 TREE_VEC_LENGTH (packed_args
));
6750 return argument_pack
;
6753 /* Returns true if the template argument vector ARGS contains
6754 any pack expansions, false otherwise. */
6757 any_pack_expanson_args_p (tree args
)
6761 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
6762 if (PACK_EXPANSION_P (TREE_VEC_ELT (args
, i
)))
6767 /* Convert all template arguments to their appropriate types, and
6768 return a vector containing the innermost resulting template
6769 arguments. If any error occurs, return error_mark_node. Error and
6770 warning messages are issued under control of COMPLAIN.
6772 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6773 for arguments not specified in ARGS. Otherwise, if
6774 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6775 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6776 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6780 coerce_template_parms (tree parms
,
6783 tsubst_flags_t complain
,
6784 bool require_all_args
,
6785 bool use_default_args
)
6787 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6790 tree new_inner_args
;
6791 int saved_unevaluated_operand
;
6792 int saved_inhibit_evaluation_warnings
;
6794 /* When used as a boolean value, indicates whether this is a
6795 variadic template parameter list. Since it's an int, we can also
6796 subtract it from nparms to get the number of non-variadic
6799 int post_variadic_parms
= 0;
6801 if (args
== error_mark_node
)
6802 return error_mark_node
;
6804 nparms
= TREE_VEC_LENGTH (parms
);
6806 /* Determine if there are any parameter packs. */
6807 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6809 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6811 ++post_variadic_parms
;
6812 if (template_parameter_pack_p (tparm
))
6816 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6817 /* If there are no parameters that follow a parameter pack, we need to
6818 expand any argument packs so that we can deduce a parameter pack from
6819 some non-packed args followed by an argument pack, as in variadic85.C.
6820 If there are such parameters, we need to leave argument packs intact
6821 so the arguments are assigned properly. This can happen when dealing
6822 with a nested class inside a partial specialization of a class
6823 template, as in variadic92.C, or when deducing a template parameter pack
6824 from a sub-declarator, as in variadic114.C. */
6825 if (!post_variadic_parms
)
6826 inner_args
= expand_template_argument_pack (inner_args
);
6828 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6829 if ((nargs
> nparms
&& !variadic_p
)
6830 || (nargs
< nparms
- variadic_p
6832 && !any_pack_expanson_args_p (inner_args
)
6833 && (!use_default_args
6834 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6835 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6837 if (complain
& tf_error
)
6841 nparms
-= variadic_p
;
6842 error ("wrong number of template arguments "
6843 "(%d, should be %d or more)", nargs
, nparms
);
6846 error ("wrong number of template arguments "
6847 "(%d, should be %d)", nargs
, nparms
);
6850 error ("provided for %q+D", in_decl
);
6853 return error_mark_node
;
6856 /* We need to evaluate the template arguments, even though this
6857 template-id may be nested within a "sizeof". */
6858 saved_unevaluated_operand
= cp_unevaluated_operand
;
6859 cp_unevaluated_operand
= 0;
6860 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6861 c_inhibit_evaluation_warnings
= 0;
6862 new_inner_args
= make_tree_vec (nparms
);
6863 new_args
= add_outermost_template_args (args
, new_inner_args
);
6864 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6869 /* Get the Ith template parameter. */
6870 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6872 if (parm
== error_mark_node
)
6874 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6878 /* Calculate the next argument. */
6879 if (arg_idx
< nargs
)
6880 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6884 if (template_parameter_pack_p (TREE_VALUE (parm
))
6885 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6887 /* All remaining arguments will be placed in the
6888 template parameter pack PARM. */
6889 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6890 inner_args
, arg_idx
,
6894 /* Store this argument. */
6895 if (arg
== error_mark_node
)
6897 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6899 /* We are done with all of the arguments. */
6906 if (PACK_EXPANSION_P (arg
))
6908 /* We don't know how many args we have yet, just
6909 use the unconverted ones for now. */
6910 new_inner_args
= inner_args
;
6914 else if (require_all_args
)
6916 /* There must be a default arg in this case. */
6917 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6919 /* The position of the first default template argument,
6920 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6922 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6923 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6928 if (arg
== error_mark_node
)
6930 if (complain
& tf_error
)
6931 error ("template argument %d is invalid", arg_idx
+ 1);
6934 /* This only occurs if there was an error in the template
6935 parameter list itself (which we would already have
6936 reported) that we are trying to recover from, e.g., a class
6937 template with a parameter list such as
6938 template<typename..., typename>. */
6941 arg
= convert_template_argument (TREE_VALUE (parm
),
6942 arg
, new_args
, complain
,
6945 if (arg
== error_mark_node
)
6947 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6949 cp_unevaluated_operand
= saved_unevaluated_operand
;
6950 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6953 return error_mark_node
;
6955 #ifdef ENABLE_CHECKING
6956 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6957 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6958 TREE_VEC_LENGTH (new_inner_args
));
6961 return new_inner_args
;
6964 /* Returns 1 if template args OT and NT are equivalent. */
6967 template_args_equal (tree ot
, tree nt
)
6971 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6974 if (TREE_CODE (nt
) == TREE_VEC
)
6975 /* For member templates */
6976 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6977 else if (PACK_EXPANSION_P (ot
))
6978 return (PACK_EXPANSION_P (nt
)
6979 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6980 PACK_EXPANSION_PATTERN (nt
))
6981 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6982 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6983 else if (ARGUMENT_PACK_P (ot
))
6988 if (!ARGUMENT_PACK_P (nt
))
6991 opack
= ARGUMENT_PACK_ARGS (ot
);
6992 npack
= ARGUMENT_PACK_ARGS (nt
);
6993 len
= TREE_VEC_LENGTH (opack
);
6994 if (TREE_VEC_LENGTH (npack
) != len
)
6996 for (i
= 0; i
< len
; ++i
)
6997 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6998 TREE_VEC_ELT (npack
, i
)))
7002 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
7004 /* We get here probably because we are in the middle of substituting
7005 into the pattern of a pack expansion. In that case the
7006 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
7007 interested in. So we want to use the initial pack argument for
7009 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
7010 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
7011 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
7012 return template_args_equal (ot
, nt
);
7014 else if (TYPE_P (nt
))
7015 return TYPE_P (ot
) && same_type_p (ot
, nt
);
7016 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
7019 return cp_tree_equal (ot
, nt
);
7022 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
7023 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
7024 NEWARG_PTR with the offending arguments if they are non-NULL. */
7027 comp_template_args_with_info (tree oldargs
, tree newargs
,
7028 tree
*oldarg_ptr
, tree
*newarg_ptr
)
7032 if (oldargs
== newargs
)
7035 if (!oldargs
|| !newargs
)
7038 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
7041 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
7043 tree nt
= TREE_VEC_ELT (newargs
, i
);
7044 tree ot
= TREE_VEC_ELT (oldargs
, i
);
7046 if (! template_args_equal (ot
, nt
))
7048 if (oldarg_ptr
!= NULL
)
7050 if (newarg_ptr
!= NULL
)
7058 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7059 of template arguments. Returns 0 otherwise. */
7062 comp_template_args (tree oldargs
, tree newargs
)
7064 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7068 add_pending_template (tree d
)
7070 tree ti
= (TYPE_P (d
)
7071 ? CLASSTYPE_TEMPLATE_INFO (d
)
7072 : DECL_TEMPLATE_INFO (d
));
7073 struct pending_template
*pt
;
7076 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7079 /* We are called both from instantiate_decl, where we've already had a
7080 tinst_level pushed, and instantiate_template, where we haven't.
7082 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7085 push_tinst_level (d
);
7087 pt
= ggc_alloc_pending_template ();
7089 pt
->tinst
= current_tinst_level
;
7090 if (last_pending_template
)
7091 last_pending_template
->next
= pt
;
7093 pending_templates
= pt
;
7095 last_pending_template
= pt
;
7097 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7104 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7105 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7106 documentation for TEMPLATE_ID_EXPR. */
7109 lookup_template_function (tree fns
, tree arglist
)
7113 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7114 return error_mark_node
;
7116 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7118 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
7120 error ("%q#D is not a function template", fns
);
7121 return error_mark_node
;
7124 if (BASELINK_P (fns
))
7126 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7128 BASELINK_FUNCTIONS (fns
),
7133 type
= TREE_TYPE (fns
);
7134 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7135 type
= unknown_type_node
;
7137 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7140 /* Within the scope of a template class S<T>, the name S gets bound
7141 (in build_self_reference) to a TYPE_DECL for the class, not a
7142 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7143 or one of its enclosing classes, and that type is a template,
7144 return the associated TEMPLATE_DECL. Otherwise, the original
7147 Also handle the case when DECL is a TREE_LIST of ambiguous
7148 injected-class-names from different bases. */
7151 maybe_get_template_decl_from_type_decl (tree decl
)
7153 if (decl
== NULL_TREE
)
7156 /* DR 176: A lookup that finds an injected-class-name (10.2
7157 [class.member.lookup]) can result in an ambiguity in certain cases
7158 (for example, if it is found in more than one base class). If all of
7159 the injected-class-names that are found refer to specializations of
7160 the same class template, and if the name is followed by a
7161 template-argument-list, the reference refers to the class template
7162 itself and not a specialization thereof, and is not ambiguous. */
7163 if (TREE_CODE (decl
) == TREE_LIST
)
7165 tree t
, tmpl
= NULL_TREE
;
7166 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7168 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7171 else if (tmpl
!= elt
)
7174 if (tmpl
&& t
== NULL_TREE
)
7180 return (decl
!= NULL_TREE
7181 && DECL_SELF_REFERENCE_P (decl
)
7182 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7183 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7186 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7187 parameters, find the desired type.
7189 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7191 IN_DECL, if non-NULL, is the template declaration we are trying to
7194 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7195 the class we are looking up.
7197 Issue error and warning messages under control of COMPLAIN.
7199 If the template class is really a local class in a template
7200 function, then the FUNCTION_CONTEXT is the function in which it is
7203 ??? Note that this function is currently called *twice* for each
7204 template-id: the first time from the parser, while creating the
7205 incomplete type (finish_template_type), and the second type during the
7206 real instantiation (instantiate_template_class). This is surely something
7207 that we want to avoid. It also causes some problems with argument
7208 coercion (see convert_nontype_argument for more information on this). */
7211 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7212 int entering_scope
, tsubst_flags_t complain
)
7214 tree templ
= NULL_TREE
, parmlist
;
7221 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
7223 tree value
= innermost_non_namespace_value (d1
);
7224 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7229 push_decl_namespace (context
);
7230 templ
= lookup_name (d1
);
7231 templ
= maybe_get_template_decl_from_type_decl (templ
);
7233 pop_decl_namespace ();
7236 context
= DECL_CONTEXT (templ
);
7238 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7240 tree type
= TREE_TYPE (d1
);
7242 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7243 an implicit typename for the second A. Deal with it. */
7244 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7245 type
= TREE_TYPE (type
);
7247 if (CLASSTYPE_TEMPLATE_INFO (type
))
7249 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7250 d1
= DECL_NAME (templ
);
7253 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7254 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7256 templ
= TYPE_TI_TEMPLATE (d1
);
7257 d1
= DECL_NAME (templ
);
7259 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7260 && DECL_TEMPLATE_RESULT (d1
)
7261 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7264 d1
= DECL_NAME (templ
);
7265 context
= DECL_CONTEXT (templ
);
7268 /* Issue an error message if we didn't find a template. */
7271 if (complain
& tf_error
)
7272 error ("%qT is not a template", d1
);
7273 return error_mark_node
;
7276 if (TREE_CODE (templ
) != TEMPLATE_DECL
7277 /* Make sure it's a user visible template, if it was named by
7279 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7280 && !PRIMARY_TEMPLATE_P (templ
)))
7282 if (complain
& tf_error
)
7284 error ("non-template type %qT used as a template", d1
);
7286 error ("for template declaration %q+D", in_decl
);
7288 return error_mark_node
;
7291 complain
&= ~tf_user
;
7293 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7295 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7296 template arguments */
7302 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7304 /* Consider an example where a template template parameter declared as
7306 template <class T, class U = std::allocator<T> > class TT
7308 The template parameter level of T and U are one level larger than
7309 of TT. To proper process the default argument of U, say when an
7310 instantiation `TT<int>' is seen, we need to build the full
7311 arguments containing {int} as the innermost level. Outer levels,
7312 available when not appearing as default template argument, can be
7313 obtained from the arguments of the enclosing template.
7315 Suppose that TT is later substituted with std::vector. The above
7316 instantiation is `TT<int, std::allocator<T> >' with TT at
7317 level 1, and T at level 2, while the template arguments at level 1
7318 becomes {std::vector} and the inner level 2 is {int}. */
7320 outer
= DECL_CONTEXT (templ
);
7322 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7323 else if (current_template_parms
)
7324 /* This is an argument of the current template, so we haven't set
7325 DECL_CONTEXT yet. */
7326 outer
= current_template_args ();
7329 arglist
= add_to_template_args (outer
, arglist
);
7331 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7333 /*require_all_args=*/true,
7334 /*use_default_args=*/true);
7335 if (arglist2
== error_mark_node
7336 || (!uses_template_parms (arglist2
)
7337 && check_instantiated_args (templ
, arglist2
, complain
)))
7338 return error_mark_node
;
7340 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7345 tree template_type
= TREE_TYPE (templ
);
7348 tree found
= NULL_TREE
;
7351 int is_dependent_type
;
7352 int use_partial_inst_tmpl
= false;
7354 if (template_type
== error_mark_node
)
7355 /* An error occured while building the template TEMPL, and a
7356 diagnostic has most certainly been emitted for that
7357 already. Let's propagate that error. */
7358 return error_mark_node
;
7360 gen_tmpl
= most_general_template (templ
);
7361 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7362 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7363 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7365 if (arg_depth
== 1 && parm_depth
> 1)
7367 /* We've been given an incomplete set of template arguments.
7370 template <class T> struct S1 {
7371 template <class U> struct S2 {};
7372 template <class U> struct S2<U*> {};
7375 we will be called with an ARGLIST of `U*', but the
7376 TEMPLATE will be `template <class T> template
7377 <class U> struct S1<T>::S2'. We must fill in the missing
7380 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7382 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7385 /* Now we should have enough arguments. */
7386 gcc_assert (parm_depth
== arg_depth
);
7388 /* From here on, we're only interested in the most general
7391 /* Calculate the BOUND_ARGS. These will be the args that are
7392 actually tsubst'd into the definition to create the
7396 /* We have multiple levels of arguments to coerce, at once. */
7398 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7400 tree bound_args
= make_tree_vec (parm_depth
);
7402 for (i
= saved_depth
,
7403 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7404 i
> 0 && t
!= NULL_TREE
;
7405 --i
, t
= TREE_CHAIN (t
))
7408 if (i
== saved_depth
)
7409 a
= coerce_template_parms (TREE_VALUE (t
),
7412 /*require_all_args=*/true,
7413 /*use_default_args=*/true);
7415 /* Outer levels should have already been coerced. */
7416 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7418 /* Don't process further if one of the levels fails. */
7419 if (a
== error_mark_node
)
7421 /* Restore the ARGLIST to its full size. */
7422 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7423 return error_mark_node
;
7426 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7428 /* We temporarily reduce the length of the ARGLIST so
7429 that coerce_template_parms will see only the arguments
7430 corresponding to the template parameters it is
7432 TREE_VEC_LENGTH (arglist
)--;
7435 /* Restore the ARGLIST to its full size. */
7436 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7438 arglist
= bound_args
;
7442 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7443 INNERMOST_TEMPLATE_ARGS (arglist
),
7446 /*require_all_args=*/true,
7447 /*use_default_args=*/true);
7449 if (arglist
== error_mark_node
)
7450 /* We were unable to bind the arguments. */
7451 return error_mark_node
;
7453 /* In the scope of a template class, explicit references to the
7454 template class refer to the type of the template, not any
7455 instantiation of it. For example, in:
7457 template <class T> class C { void f(C<T>); }
7459 the `C<T>' is just the same as `C'. Outside of the
7460 class, however, such a reference is an instantiation. */
7462 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7463 || currently_open_class (template_type
))
7464 /* comp_template_args is expensive, check it last. */
7465 && comp_template_args (TYPE_TI_ARGS (template_type
),
7467 return template_type
;
7469 /* If we already have this specialization, return it. */
7470 elt
.tmpl
= gen_tmpl
;
7472 hash
= hash_specialization (&elt
);
7473 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7479 is_dependent_type
= uses_template_parms (arglist
);
7481 /* If the deduced arguments are invalid, then the binding
7483 if (!is_dependent_type
7484 && check_instantiated_args (gen_tmpl
,
7485 INNERMOST_TEMPLATE_ARGS (arglist
),
7487 return error_mark_node
;
7489 if (!is_dependent_type
7490 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7491 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7492 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7494 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7495 DECL_NAME (gen_tmpl
),
7496 /*tag_scope=*/ts_global
);
7500 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7502 if (context
== error_mark_node
)
7503 return error_mark_node
;
7506 context
= global_namespace
;
7508 /* Create the type. */
7509 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7511 if (!is_dependent_type
)
7513 set_current_access_from_decl (TYPE_NAME (template_type
));
7514 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7515 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7516 arglist
, complain
, in_decl
),
7517 SCOPED_ENUM_P (template_type
), NULL
);
7521 /* We don't want to call start_enum for this type, since
7522 the values for the enumeration constants may involve
7523 template parameters. And, no one should be interested
7524 in the enumeration constants for such a type. */
7525 t
= cxx_make_type (ENUMERAL_TYPE
);
7526 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7528 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7529 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7530 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7532 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7534 /* The user referred to a specialization of an alias
7535 template represented by GEN_TMPL.
7537 [temp.alias]/2 says:
7539 When a template-id refers to the specialization of an
7540 alias template, it is equivalent to the associated
7541 type obtained by substitution of its
7542 template-arguments for the template-parameters in the
7543 type-id of the alias template. */
7545 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7546 /* Note that the call above (by indirectly calling
7547 register_specialization in tsubst_decl) registers the
7548 TYPE_DECL representing the specialization of the alias
7549 template. So next time someone substitutes ARGLIST for
7550 the template parms into the alias template (GEN_TMPL),
7551 she'll get that TYPE_DECL back. */
7553 if (t
== error_mark_node
)
7556 else if (CLASS_TYPE_P (template_type
))
7558 t
= make_class_type (TREE_CODE (template_type
));
7559 CLASSTYPE_DECLARED_CLASS (t
)
7560 = CLASSTYPE_DECLARED_CLASS (template_type
);
7561 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7562 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7564 /* A local class. Make sure the decl gets registered properly. */
7565 if (context
== current_function_decl
)
7566 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7568 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7569 /* This instantiation is another name for the primary
7570 template type. Set the TYPE_CANONICAL field
7572 TYPE_CANONICAL (t
) = template_type
;
7573 else if (any_template_arguments_need_structural_equality_p (arglist
))
7574 /* Some of the template arguments require structural
7575 equality testing, so this template class requires
7576 structural equality testing. */
7577 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7582 /* If we called start_enum or pushtag above, this information
7583 will already be set up. */
7586 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7588 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7589 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7590 DECL_SOURCE_LOCATION (type_decl
)
7591 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7594 type_decl
= TYPE_NAME (t
);
7596 if (CLASS_TYPE_P (template_type
))
7598 TREE_PRIVATE (type_decl
)
7599 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
7600 TREE_PROTECTED (type_decl
)
7601 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
7602 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7604 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7605 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7609 /* Let's consider the explicit specialization of a member
7610 of a class template specialization that is implicitely instantiated,
7615 template<class U> struct M {}; //#0
7620 struct S<int>::M<char> //#1
7624 [temp.expl.spec]/4 says this is valid.
7626 In this case, when we write:
7629 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7632 When we encounter #1, we want to store the partial instantiation
7633 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7635 For all cases other than this "explicit specialization of member of a
7636 class template", we just want to store the most general template into
7637 the CLASSTYPE_TI_TEMPLATE of M.
7639 This case of "explicit specialization of member of a class template"
7641 1/ the enclosing class is an instantiation of, and therefore not
7642 the same as, the context of the most general template, and
7643 2/ we aren't looking at the partial instantiation itself, i.e.
7644 the innermost arguments are not the same as the innermost parms of
7645 the most general template.
7647 So it's only when 1/ and 2/ happens that we want to use the partial
7648 instantiation of the member template in lieu of its most general
7651 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7652 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7653 /* the enclosing class must be an instantiation... */
7654 && CLASS_TYPE_P (context
)
7655 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7657 tree partial_inst_args
;
7658 TREE_VEC_LENGTH (arglist
)--;
7659 ++processing_template_decl
;
7661 tsubst (INNERMOST_TEMPLATE_ARGS
7662 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7663 arglist
, complain
, NULL_TREE
);
7664 --processing_template_decl
;
7665 TREE_VEC_LENGTH (arglist
)++;
7666 use_partial_inst_tmpl
=
7667 /*...and we must not be looking at the partial instantiation
7669 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7673 if (!use_partial_inst_tmpl
)
7674 /* This case is easy; there are no member templates involved. */
7678 /* This is a full instantiation of a member template. Find
7679 the partial instantiation of which this is an instance. */
7681 /* Temporarily reduce by one the number of levels in the ARGLIST
7682 so as to avoid comparing the last set of arguments. */
7683 TREE_VEC_LENGTH (arglist
)--;
7684 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7685 TREE_VEC_LENGTH (arglist
)++;
7686 /* FOUND is either a proper class type, or an alias
7687 template specialization. In the later case, it's a
7688 TYPE_DECL, resulting from the substituting of arguments
7689 for parameters in the TYPE_DECL of the alias template
7690 done earlier. So be careful while getting the template
7692 found
= TREE_CODE (found
) == TYPE_DECL
7693 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7694 : CLASSTYPE_TI_TEMPLATE (found
);
7697 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7700 slot
= htab_find_slot_with_hash (type_specializations
,
7701 &elt
, hash
, INSERT
);
7702 entry
= ggc_alloc_spec_entry ();
7706 /* Note this use of the partial instantiation so we can check it
7707 later in maybe_process_partial_specialization. */
7708 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7709 = tree_cons (arglist
, t
,
7710 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7712 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7713 /* Now that the type has been registered on the instantiations
7714 list, we set up the enumerators. Because the enumeration
7715 constants may involve the enumeration type itself, we make
7716 sure to register the type first, and then create the
7717 constants. That way, doing tsubst_expr for the enumeration
7718 constants won't result in recursive calls here; we'll find
7719 the instantiation and exit above. */
7720 tsubst_enum (template_type
, t
, arglist
);
7722 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7723 /* If the type makes use of template parameters, the
7724 code that generates debugging information will crash. */
7725 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7727 /* Possibly limit visibility based on template args. */
7728 TREE_PUBLIC (type_decl
) = 1;
7729 determine_visibility (type_decl
);
7735 /* Wrapper for lookup_template_class_1. */
7738 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7739 int entering_scope
, tsubst_flags_t complain
)
7742 timevar_push (TV_TEMPLATE_INST
);
7743 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7744 entering_scope
, complain
);
7745 timevar_pop (TV_TEMPLATE_INST
);
7753 /* True when we should also visit template parameters that occur in
7754 non-deduced contexts. */
7755 bool include_nondeduced_p
;
7756 struct pointer_set_t
*visited
;
7759 /* Called from for_each_template_parm via walk_tree. */
7762 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7765 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7766 tree_fn_t fn
= pfd
->fn
;
7767 void *data
= pfd
->data
;
7770 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7771 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7772 pfd
->include_nondeduced_p
))
7773 return error_mark_node
;
7775 switch (TREE_CODE (t
))
7778 if (TYPE_PTRMEMFUNC_P (t
))
7784 if (!TYPE_TEMPLATE_INFO (t
))
7786 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7787 fn
, data
, pfd
->visited
,
7788 pfd
->include_nondeduced_p
))
7789 return error_mark_node
;
7793 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7794 fn
, data
, pfd
->visited
,
7795 pfd
->include_nondeduced_p
)
7796 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7797 fn
, data
, pfd
->visited
,
7798 pfd
->include_nondeduced_p
))
7799 return error_mark_node
;
7803 /* Since we're not going to walk subtrees, we have to do this
7805 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7806 pfd
->visited
, pfd
->include_nondeduced_p
))
7807 return error_mark_node
;
7811 /* Check the return type. */
7812 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7813 pfd
->include_nondeduced_p
))
7814 return error_mark_node
;
7816 /* Check the parameter types. Since default arguments are not
7817 instantiated until they are needed, the TYPE_ARG_TYPES may
7818 contain expressions that involve template parameters. But,
7819 no-one should be looking at them yet. And, once they're
7820 instantiated, they don't contain template parameters, so
7821 there's no point in looking at them then, either. */
7825 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7826 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7827 pfd
->visited
, pfd
->include_nondeduced_p
))
7828 return error_mark_node
;
7830 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7831 want walk_tree walking into them itself. */
7837 case UNDERLYING_TYPE
:
7838 if (pfd
->include_nondeduced_p
7839 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7841 pfd
->include_nondeduced_p
))
7842 return error_mark_node
;
7847 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7848 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7849 pfd
->visited
, pfd
->include_nondeduced_p
))
7850 return error_mark_node
;
7855 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7856 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7857 pfd
->visited
, pfd
->include_nondeduced_p
))
7858 return error_mark_node
;
7859 if (DECL_CONTEXT (t
)
7860 && pfd
->include_nondeduced_p
7861 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7862 pfd
->visited
, pfd
->include_nondeduced_p
))
7863 return error_mark_node
;
7866 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7867 /* Record template parameters such as `T' inside `TT<T>'. */
7868 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7869 pfd
->include_nondeduced_p
))
7870 return error_mark_node
;
7873 case TEMPLATE_TEMPLATE_PARM
:
7874 case TEMPLATE_TYPE_PARM
:
7875 case TEMPLATE_PARM_INDEX
:
7876 if (fn
&& (*fn
)(t
, data
))
7877 return error_mark_node
;
7879 return error_mark_node
;
7883 /* A template template parameter is encountered. */
7884 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7885 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7886 pfd
->include_nondeduced_p
))
7887 return error_mark_node
;
7889 /* Already substituted template template parameter */
7895 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7897 pfd
->include_nondeduced_p
))
7898 return error_mark_node
;
7902 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7903 && pfd
->include_nondeduced_p
7904 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7905 (TREE_TYPE (t
)), fn
, data
,
7906 pfd
->visited
, pfd
->include_nondeduced_p
))
7907 return error_mark_node
;
7912 /* If there's no type, then this thing must be some expression
7913 involving template parameters. */
7914 if (!fn
&& !TREE_TYPE (t
))
7915 return error_mark_node
;
7920 case IMPLICIT_CONV_EXPR
:
7921 case REINTERPRET_CAST_EXPR
:
7922 case CONST_CAST_EXPR
:
7923 case STATIC_CAST_EXPR
:
7924 case DYNAMIC_CAST_EXPR
:
7928 case PSEUDO_DTOR_EXPR
:
7930 return error_mark_node
;
7937 /* We didn't find any template parameters we liked. */
7941 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7942 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7943 call FN with the parameter and the DATA.
7944 If FN returns nonzero, the iteration is terminated, and
7945 for_each_template_parm returns 1. Otherwise, the iteration
7946 continues. If FN never returns a nonzero value, the value
7947 returned by for_each_template_parm is 0. If FN is NULL, it is
7948 considered to be the function which always returns 1.
7950 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7951 parameters that occur in non-deduced contexts. When false, only
7952 visits those template parameters that can be deduced. */
7955 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7956 struct pointer_set_t
*visited
,
7957 bool include_nondeduced_p
)
7959 struct pair_fn_data pfd
;
7965 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7967 /* Walk the tree. (Conceptually, we would like to walk without
7968 duplicates, but for_each_template_parm_r recursively calls
7969 for_each_template_parm, so we would need to reorganize a fair
7970 bit to use walk_tree_without_duplicates, so we keep our own
7973 pfd
.visited
= visited
;
7975 pfd
.visited
= pointer_set_create ();
7976 result
= cp_walk_tree (&t
,
7977 for_each_template_parm_r
,
7979 pfd
.visited
) != NULL_TREE
;
7984 pointer_set_destroy (pfd
.visited
);
7991 /* Returns true if T depends on any template parameter. */
7994 uses_template_parms (tree t
)
7997 int saved_processing_template_decl
;
7999 saved_processing_template_decl
= processing_template_decl
;
8000 if (!saved_processing_template_decl
)
8001 processing_template_decl
= 1;
8003 dependent_p
= dependent_type_p (t
);
8004 else if (TREE_CODE (t
) == TREE_VEC
)
8005 dependent_p
= any_dependent_template_arguments_p (t
);
8006 else if (TREE_CODE (t
) == TREE_LIST
)
8007 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
8008 || uses_template_parms (TREE_CHAIN (t
)));
8009 else if (TREE_CODE (t
) == TYPE_DECL
)
8010 dependent_p
= dependent_type_p (TREE_TYPE (t
));
8013 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
8014 || TREE_CODE (t
) == OVERLOAD
8016 || TREE_CODE (t
) == IDENTIFIER_NODE
8017 || TREE_CODE (t
) == TRAIT_EXPR
8018 || TREE_CODE (t
) == CONSTRUCTOR
8019 || CONSTANT_CLASS_P (t
))
8020 dependent_p
= (type_dependent_expression_p (t
)
8021 || value_dependent_expression_p (t
));
8024 gcc_assert (t
== error_mark_node
);
8025 dependent_p
= false;
8028 processing_template_decl
= saved_processing_template_decl
;
8033 /* Returns true if T depends on any template parameter with level LEVEL. */
8036 uses_template_parms_level (tree t
, int level
)
8038 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
8039 /*include_nondeduced_p=*/true);
8042 /* Returns TRUE iff INST is an instantiation we don't need to do in an
8043 ill-formed translation unit, i.e. a variable or function that isn't
8044 usable in a constant expression. */
8047 neglectable_inst_p (tree d
)
8050 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
8051 : decl_maybe_constant_var_p (d
)));
8054 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8055 neglectable and instantiated from within an erroneous instantiation. */
8058 limit_bad_template_recursion (tree decl
)
8060 struct tinst_level
*lev
= current_tinst_level
;
8061 int errs
= errorcount
+ sorrycount
;
8062 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8065 for (; lev
; lev
= lev
->next
)
8066 if (neglectable_inst_p (lev
->decl
))
8069 return (lev
&& errs
> lev
->errors
);
8072 static int tinst_depth
;
8073 extern int max_tinst_depth
;
8074 #ifdef GATHER_STATISTICS
8077 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8079 /* We're starting to instantiate D; record the template instantiation context
8080 for diagnostics and to restore it later. */
8083 push_tinst_level (tree d
)
8085 struct tinst_level
*new_level
;
8087 if (tinst_depth
>= max_tinst_depth
)
8089 last_error_tinst_level
= current_tinst_level
;
8090 if (TREE_CODE (d
) == TREE_LIST
)
8091 error ("template instantiation depth exceeds maximum of %d (use "
8092 "-ftemplate-depth= to increase the maximum) substituting %qS",
8093 max_tinst_depth
, d
);
8095 error ("template instantiation depth exceeds maximum of %d (use "
8096 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8097 max_tinst_depth
, d
);
8099 print_instantiation_context ();
8104 /* If the current instantiation caused problems, don't let it instantiate
8105 anything else. Do allow deduction substitution and decls usable in
8106 constant expressions. */
8107 if (limit_bad_template_recursion (d
))
8110 new_level
= ggc_alloc_tinst_level ();
8111 new_level
->decl
= d
;
8112 new_level
->locus
= input_location
;
8113 new_level
->errors
= errorcount
+sorrycount
;
8114 new_level
->in_system_header_p
= in_system_header
;
8115 new_level
->next
= current_tinst_level
;
8116 current_tinst_level
= new_level
;
8119 #ifdef GATHER_STATISTICS
8120 if (tinst_depth
> depth_reached
)
8121 depth_reached
= tinst_depth
;
8127 /* We're done instantiating this template; return to the instantiation
8131 pop_tinst_level (void)
8133 /* Restore the filename and line number stashed away when we started
8134 this instantiation. */
8135 input_location
= current_tinst_level
->locus
;
8136 current_tinst_level
= current_tinst_level
->next
;
8140 /* We're instantiating a deferred template; restore the template
8141 instantiation context in which the instantiation was requested, which
8142 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8145 reopen_tinst_level (struct tinst_level
*level
)
8147 struct tinst_level
*t
;
8150 for (t
= level
; t
; t
= t
->next
)
8153 current_tinst_level
= level
;
8155 if (current_tinst_level
)
8156 current_tinst_level
->errors
= errorcount
+sorrycount
;
8160 /* Returns the TINST_LEVEL which gives the original instantiation
8163 struct tinst_level
*
8164 outermost_tinst_level (void)
8166 struct tinst_level
*level
= current_tinst_level
;
8169 level
= level
->next
;
8173 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8174 vector of template arguments, as for tsubst.
8176 Returns an appropriate tsubst'd friend declaration. */
8179 tsubst_friend_function (tree decl
, tree args
)
8183 if (TREE_CODE (decl
) == FUNCTION_DECL
8184 && DECL_TEMPLATE_INSTANTIATION (decl
)
8185 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8186 /* This was a friend declared with an explicit template
8187 argument list, e.g.:
8191 to indicate that f was a template instantiation, not a new
8192 function declaration. Now, we have to figure out what
8193 instantiation of what template. */
8195 tree template_id
, arglist
, fns
;
8198 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8200 /* Friend functions are looked up in the containing namespace scope.
8201 We must enter that scope, to avoid finding member functions of the
8202 current class with same name. */
8203 push_nested_namespace (ns
);
8204 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8205 tf_warning_or_error
, NULL_TREE
,
8206 /*integral_constant_expression_p=*/false);
8207 pop_nested_namespace (ns
);
8208 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8209 tf_warning_or_error
, NULL_TREE
);
8210 template_id
= lookup_template_function (fns
, arglist
);
8212 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8213 tmpl
= determine_specialization (template_id
, new_friend
,
8215 /*need_member_template=*/0,
8216 TREE_VEC_LENGTH (args
),
8218 return instantiate_template (tmpl
, new_args
, tf_error
);
8221 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8223 /* The NEW_FRIEND will look like an instantiation, to the
8224 compiler, but is not an instantiation from the point of view of
8225 the language. For example, we might have had:
8227 template <class T> struct S {
8228 template <class U> friend void f(T, U);
8231 Then, in S<int>, template <class U> void f(int, U) is not an
8232 instantiation of anything. */
8233 if (new_friend
== error_mark_node
)
8234 return error_mark_node
;
8236 DECL_USE_TEMPLATE (new_friend
) = 0;
8237 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8239 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8240 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8241 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8244 /* The mangled name for the NEW_FRIEND is incorrect. The function
8245 is not a template instantiation and should not be mangled like
8246 one. Therefore, we forget the mangling here; we'll recompute it
8247 later if we need it. */
8248 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8250 SET_DECL_RTL (new_friend
, NULL
);
8251 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8254 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8257 tree new_friend_template_info
;
8258 tree new_friend_result_template_info
;
8260 int new_friend_is_defn
;
8262 /* We must save some information from NEW_FRIEND before calling
8263 duplicate decls since that function will free NEW_FRIEND if
8265 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8266 new_friend_is_defn
=
8267 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8268 (template_for_substitution (new_friend
)))
8270 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8272 /* This declaration is a `primary' template. */
8273 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8275 new_friend_result_template_info
8276 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8279 new_friend_result_template_info
= NULL_TREE
;
8281 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8282 if (new_friend_is_defn
)
8283 DECL_INITIAL (new_friend
) = error_mark_node
;
8285 /* Inside pushdecl_namespace_level, we will push into the
8286 current namespace. However, the friend function should go
8287 into the namespace of the template. */
8288 ns
= decl_namespace_context (new_friend
);
8289 push_nested_namespace (ns
);
8290 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8291 pop_nested_namespace (ns
);
8293 if (old_decl
== error_mark_node
)
8294 return error_mark_node
;
8296 if (old_decl
!= new_friend
)
8298 /* This new friend declaration matched an existing
8299 declaration. For example, given:
8301 template <class T> void f(T);
8302 template <class U> class C {
8303 template <class T> friend void f(T) {}
8306 the friend declaration actually provides the definition
8307 of `f', once C has been instantiated for some type. So,
8308 old_decl will be the out-of-class template declaration,
8309 while new_friend is the in-class definition.
8311 But, if `f' was called before this point, the
8312 instantiation of `f' will have DECL_TI_ARGS corresponding
8313 to `T' but not to `U', references to which might appear
8314 in the definition of `f'. Previously, the most general
8315 template for an instantiation of `f' was the out-of-class
8316 version; now it is the in-class version. Therefore, we
8317 run through all specialization of `f', adding to their
8318 DECL_TI_ARGS appropriately. In particular, they need a
8319 new set of outer arguments, corresponding to the
8320 arguments for this class instantiation.
8322 The same situation can arise with something like this:
8325 template <class T> class C {
8329 when `C<int>' is instantiated. Now, `f(int)' is defined
8332 if (!new_friend_is_defn
)
8333 /* On the other hand, if the in-class declaration does
8334 *not* provide a definition, then we don't want to alter
8335 existing definitions. We can just leave everything
8340 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8341 tree new_args
= TI_ARGS (new_friend_template_info
);
8343 /* Overwrite whatever template info was there before, if
8344 any, with the new template information pertaining to
8346 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8348 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8350 /* We should have called reregister_specialization in
8352 gcc_assert (retrieve_specialization (new_template
,
8356 /* Instantiate it if the global has already been used. */
8357 if (DECL_ODR_USED (old_decl
))
8358 instantiate_decl (old_decl
, /*defer_ok=*/true,
8359 /*expl_inst_class_mem_p=*/false);
8365 /* Indicate that the old function template is a partial
8367 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8368 = new_friend_result_template_info
;
8370 gcc_assert (new_template
8371 == most_general_template (new_template
));
8372 gcc_assert (new_template
!= old_decl
);
8374 /* Reassign any specializations already in the hash table
8375 to the new more general template, and add the
8376 additional template args. */
8377 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8381 tree spec
= TREE_VALUE (t
);
8384 elt
.tmpl
= old_decl
;
8385 elt
.args
= DECL_TI_ARGS (spec
);
8386 elt
.spec
= NULL_TREE
;
8388 htab_remove_elt (decl_specializations
, &elt
);
8391 = add_outermost_template_args (new_args
,
8392 DECL_TI_ARGS (spec
));
8394 register_specialization
8395 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8398 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8402 /* The information from NEW_FRIEND has been merged into OLD_DECL
8403 by duplicate_decls. */
8404 new_friend
= old_decl
;
8409 tree context
= DECL_CONTEXT (new_friend
);
8413 template <class T> class C {
8414 template <class U> friend void C1<U>::f (); // case 1
8415 friend void C2<T>::f (); // case 2
8417 we only need to make sure CONTEXT is a complete type for
8418 case 2. To distinguish between the two cases, we note that
8419 CONTEXT of case 1 remains dependent type after tsubst while
8420 this isn't true for case 2. */
8421 ++processing_template_decl
;
8422 dependent_p
= dependent_type_p (context
);
8423 --processing_template_decl
;
8426 && !complete_type_or_else (context
, NULL_TREE
))
8427 return error_mark_node
;
8429 if (COMPLETE_TYPE_P (context
))
8431 /* Check to see that the declaration is really present, and,
8432 possibly obtain an improved declaration. */
8433 tree fn
= check_classfn (context
,
8434 new_friend
, NULL_TREE
);
8444 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8445 template arguments, as for tsubst.
8447 Returns an appropriate tsubst'd friend type or error_mark_node on
8451 tsubst_friend_class (tree friend_tmpl
, tree args
)
8457 context
= CP_DECL_CONTEXT (friend_tmpl
);
8459 if (context
!= global_namespace
)
8461 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8462 push_nested_namespace (context
);
8464 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8467 /* Look for a class template declaration. We look for hidden names
8468 because two friend declarations of the same template are the
8469 same. For example, in:
8472 template <typename> friend class F;
8474 template <typename> struct B {
8475 template <typename> friend class F;
8478 both F templates are the same. */
8479 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8480 /*block_p=*/true, 0,
8481 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
8483 /* But, if we don't find one, it might be because we're in a
8484 situation like this:
8492 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8493 for `S<int>', not the TEMPLATE_DECL. */
8494 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8496 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8497 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8500 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8502 /* The friend template has already been declared. Just
8503 check to see that the declarations match, and install any new
8504 default parameters. We must tsubst the default parameters,
8505 of course. We only need the innermost template parameters
8506 because that is all that redeclare_class_template will look
8508 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8509 > TMPL_ARGS_DEPTH (args
))
8512 location_t saved_input_location
;
8513 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8514 args
, tf_warning_or_error
);
8516 saved_input_location
= input_location
;
8517 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8518 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8519 input_location
= saved_input_location
;
8523 friend_type
= TREE_TYPE (tmpl
);
8527 /* The friend template has not already been declared. In this
8528 case, the instantiation of the template class will cause the
8529 injection of this template into the global scope. */
8530 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8531 if (tmpl
== error_mark_node
)
8532 return error_mark_node
;
8534 /* The new TMPL is not an instantiation of anything, so we
8535 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8536 the new type because that is supposed to be the corresponding
8537 template decl, i.e., TMPL. */
8538 DECL_USE_TEMPLATE (tmpl
) = 0;
8539 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8540 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8541 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8542 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8544 /* Inject this template into the global scope. */
8545 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8548 if (context
!= global_namespace
)
8550 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8551 pop_nested_namespace (context
);
8553 pop_nested_class ();
8559 /* Returns zero if TYPE cannot be completed later due to circularity.
8560 Otherwise returns one. */
8563 can_complete_type_without_circularity (tree type
)
8565 if (type
== NULL_TREE
|| type
== error_mark_node
)
8567 else if (COMPLETE_TYPE_P (type
))
8569 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8570 return can_complete_type_without_circularity (TREE_TYPE (type
));
8571 else if (CLASS_TYPE_P (type
)
8572 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8578 /* Apply any attributes which had to be deferred until instantiation
8579 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8580 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8583 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8584 tree args
, tsubst_flags_t complain
, tree in_decl
)
8586 tree last_dep
= NULL_TREE
;
8590 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8591 if (ATTR_IS_DEPENDENT (t
))
8594 attributes
= copy_list (attributes
);
8598 if (DECL_P (*decl_p
))
8600 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8602 p
= &DECL_ATTRIBUTES (*decl_p
);
8605 p
= &TYPE_ATTRIBUTES (*decl_p
);
8609 tree late_attrs
= NULL_TREE
;
8610 tree
*q
= &late_attrs
;
8612 for (*p
= attributes
; *p
; )
8615 if (ATTR_IS_DEPENDENT (t
))
8617 *p
= TREE_CHAIN (t
);
8618 TREE_CHAIN (t
) = NULL_TREE
;
8619 /* If the first attribute argument is an identifier, don't
8620 pass it through tsubst. Attributes like mode, format,
8621 cleanup and several target specific attributes expect it
8624 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8625 && TREE_VALUE (TREE_VALUE (t
))
8626 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8627 == IDENTIFIER_NODE
))
8630 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8632 /*integral_constant_expression_p=*/false);
8633 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8635 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8640 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8641 /*integral_constant_expression_p=*/false);
8643 q
= &TREE_CHAIN (t
);
8646 p
= &TREE_CHAIN (t
);
8649 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8653 /* Perform (or defer) access check for typedefs that were referenced
8654 from within the template TMPL code.
8655 This is a subroutine of instantiate_template and instantiate_class_template.
8656 TMPL is the template to consider and TARGS is the list of arguments of
8660 perform_typedefs_access_check (tree tmpl
, tree targs
)
8662 location_t saved_location
;
8664 qualified_typedef_usage_t
*iter
;
8667 || (!CLASS_TYPE_P (tmpl
)
8668 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8671 saved_location
= input_location
;
8672 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
8673 get_types_needing_access_check (tmpl
),
8676 tree type_decl
= iter
->typedef_decl
;
8677 tree type_scope
= iter
->context
;
8679 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8682 if (uses_template_parms (type_decl
))
8683 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8684 if (uses_template_parms (type_scope
))
8685 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8687 /* Make access check error messages point to the location
8688 of the use of the typedef. */
8689 input_location
= iter
->locus
;
8690 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8691 type_decl
, type_decl
);
8693 input_location
= saved_location
;
8697 instantiate_class_template_1 (tree type
)
8699 tree templ
, args
, pattern
, t
, member
;
8703 unsigned int saved_maximum_field_alignment
;
8705 if (type
== error_mark_node
)
8706 return error_mark_node
;
8708 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8709 || uses_template_parms (type
))
8712 /* Figure out which template is being instantiated. */
8713 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8714 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8716 /* Determine what specialization of the original template to
8718 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8719 if (t
== error_mark_node
)
8721 TYPE_BEING_DEFINED (type
) = 1;
8722 return error_mark_node
;
8726 /* This TYPE is actually an instantiation of a partial
8727 specialization. We replace the innermost set of ARGS with
8728 the arguments appropriate for substitution. For example,
8731 template <class T> struct S {};
8732 template <class T> struct S<T*> {};
8734 and supposing that we are instantiating S<int*>, ARGS will
8735 presently be {int*} -- but we need {int}. */
8736 pattern
= TREE_TYPE (t
);
8737 args
= TREE_PURPOSE (t
);
8741 pattern
= TREE_TYPE (templ
);
8742 args
= CLASSTYPE_TI_ARGS (type
);
8745 /* If the template we're instantiating is incomplete, then clearly
8746 there's nothing we can do. */
8747 if (!COMPLETE_TYPE_P (pattern
))
8750 /* If we've recursively instantiated too many templates, stop. */
8751 if (! push_tinst_level (type
))
8754 /* Now we're really doing the instantiation. Mark the type as in
8755 the process of being defined. */
8756 TYPE_BEING_DEFINED (type
) = 1;
8758 /* We may be in the middle of deferred access check. Disable
8760 push_deferring_access_checks (dk_no_deferred
);
8762 push_to_top_level ();
8763 /* Use #pragma pack from the template context. */
8764 saved_maximum_field_alignment
= maximum_field_alignment
;
8765 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8767 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8769 /* Set the input location to the most specialized template definition.
8770 This is needed if tsubsting causes an error. */
8771 typedecl
= TYPE_MAIN_DECL (pattern
);
8772 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8773 DECL_SOURCE_LOCATION (typedecl
);
8775 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8776 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8777 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8778 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8779 if (ANON_AGGR_TYPE_P (pattern
))
8780 SET_ANON_AGGR_TYPE_P (type
);
8781 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8783 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8784 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8785 /* Adjust visibility for template arguments. */
8786 determine_visibility (TYPE_MAIN_DECL (type
));
8788 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8790 pbinfo
= TYPE_BINFO (pattern
);
8792 /* We should never instantiate a nested class before its enclosing
8793 class; we need to look up the nested class by name before we can
8794 instantiate it, and that lookup should instantiate the enclosing
8796 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8797 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8799 base_list
= NULL_TREE
;
8800 if (BINFO_N_BASE_BINFOS (pbinfo
))
8806 /* We must enter the scope containing the type, as that is where
8807 the accessibility of types named in dependent bases are
8809 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8811 /* Substitute into each of the bases to determine the actual
8813 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8816 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8817 tree expanded_bases
= NULL_TREE
;
8820 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8823 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8824 args
, tf_error
, NULL_TREE
);
8825 if (expanded_bases
== error_mark_node
)
8828 len
= TREE_VEC_LENGTH (expanded_bases
);
8831 for (idx
= 0; idx
< len
; idx
++)
8834 /* Extract the already-expanded base class. */
8835 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8837 /* Substitute to figure out the base class. */
8838 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8841 if (base
== error_mark_node
)
8844 base_list
= tree_cons (access
, base
, base_list
);
8845 if (BINFO_VIRTUAL_P (pbase_binfo
))
8846 TREE_TYPE (base_list
) = integer_type_node
;
8850 /* The list is now in reverse order; correct that. */
8851 base_list
= nreverse (base_list
);
8854 pop_scope (pushed_scope
);
8856 /* Now call xref_basetypes to set up all the base-class
8858 xref_basetypes (type
, base_list
);
8860 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8861 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8862 args
, tf_error
, NULL_TREE
);
8863 fixup_attribute_variants (type
);
8865 /* Now that our base classes are set up, enter the scope of the
8866 class, so that name lookups into base classes, etc. will work
8867 correctly. This is precisely analogous to what we do in
8868 begin_class_definition when defining an ordinary non-template
8869 class, except we also need to push the enclosing classes. */
8870 push_nested_class (type
);
8872 /* Now members are processed in the order of declaration. */
8873 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8874 member
; member
= TREE_CHAIN (member
))
8876 tree t
= TREE_VALUE (member
);
8878 if (TREE_PURPOSE (member
))
8882 /* Build new CLASSTYPE_NESTED_UTDS. */
8885 bool class_template_p
;
8887 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8888 && TYPE_LANG_SPECIFIC (t
)
8889 && CLASSTYPE_IS_TEMPLATE (t
));
8890 /* If the member is a class template, then -- even after
8891 substitution -- there may be dependent types in the
8892 template argument list for the class. We increment
8893 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8894 that function will assume that no types are dependent
8895 when outside of a template. */
8896 if (class_template_p
)
8897 ++processing_template_decl
;
8898 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8899 if (class_template_p
)
8900 --processing_template_decl
;
8901 if (newtag
== error_mark_node
)
8904 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8906 tree name
= TYPE_IDENTIFIER (t
);
8908 if (class_template_p
)
8909 /* Unfortunately, lookup_template_class sets
8910 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8911 instantiation (i.e., for the type of a member
8912 template class nested within a template class.)
8913 This behavior is required for
8914 maybe_process_partial_specialization to work
8915 correctly, but is not accurate in this case;
8916 the TAG is not an instantiation of anything.
8917 (The corresponding TEMPLATE_DECL is an
8918 instantiation, but the TYPE is not.) */
8919 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8921 /* Now, we call pushtag to put this NEWTAG into the scope of
8922 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8923 pushtag calling push_template_decl. We don't have to do
8924 this for enums because it will already have been done in
8927 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8928 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8931 else if (TREE_CODE (t
) == FUNCTION_DECL
8932 || DECL_FUNCTION_TEMPLATE_P (t
))
8934 /* Build new TYPE_METHODS. */
8937 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8938 ++processing_template_decl
;
8939 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8940 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8941 --processing_template_decl
;
8942 set_current_access_from_decl (r
);
8943 finish_member_declaration (r
);
8944 /* Instantiate members marked with attribute used. */
8945 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8950 /* Build new TYPE_FIELDS. */
8951 if (TREE_CODE (t
) == STATIC_ASSERT
)
8955 ++c_inhibit_evaluation_warnings
;
8957 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8958 tf_warning_or_error
, NULL_TREE
,
8959 /*integral_constant_expression_p=*/true);
8960 --c_inhibit_evaluation_warnings
;
8962 finish_static_assert (condition
,
8963 STATIC_ASSERT_MESSAGE (t
),
8964 STATIC_ASSERT_SOURCE_LOCATION (t
),
8967 else if (TREE_CODE (t
) != CONST_DECL
)
8971 /* The file and line for this declaration, to
8972 assist in error message reporting. Since we
8973 called push_tinst_level above, we don't need to
8975 input_location
= DECL_SOURCE_LOCATION (t
);
8977 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8978 ++processing_template_decl
;
8979 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8980 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8981 --processing_template_decl
;
8982 if (TREE_CODE (r
) == VAR_DECL
)
8986 [t]he initialization (and any associated
8987 side-effects) of a static data member does
8988 not occur unless the static data member is
8989 itself used in a way that requires the
8990 definition of the static data member to
8993 Therefore, we do not substitute into the
8994 initialized for the static data member here. */
8995 finish_static_data_member_decl
8998 /*init_const_expr_p=*/false,
8999 /*asmspec_tree=*/NULL_TREE
,
9001 /* Instantiate members marked with attribute used. */
9002 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
9005 else if (TREE_CODE (r
) == FIELD_DECL
)
9007 /* Determine whether R has a valid type and can be
9008 completed later. If R is invalid, then it is
9009 replaced by error_mark_node so that it will not be
9010 added to TYPE_FIELDS. */
9011 tree rtype
= TREE_TYPE (r
);
9012 if (can_complete_type_without_circularity (rtype
))
9013 complete_type (rtype
);
9015 if (!COMPLETE_TYPE_P (rtype
))
9017 cxx_incomplete_type_error (r
, rtype
);
9018 r
= error_mark_node
;
9022 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
9023 such a thing will already have been added to the field
9024 list by tsubst_enum in finish_member_declaration in the
9025 CLASSTYPE_NESTED_UTDS case above. */
9026 if (!(TREE_CODE (r
) == TYPE_DECL
9027 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9028 && DECL_ARTIFICIAL (r
)))
9030 set_current_access_from_decl (r
);
9031 finish_member_declaration (r
);
9038 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
9040 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9042 tree friend_type
= t
;
9043 bool adjust_processing_template_decl
= false;
9045 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9047 /* template <class T> friend class C; */
9048 friend_type
= tsubst_friend_class (friend_type
, args
);
9049 adjust_processing_template_decl
= true;
9051 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9053 /* template <class T> friend class C::D; */
9054 friend_type
= tsubst (friend_type
, args
,
9055 tf_warning_or_error
, NULL_TREE
);
9056 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9057 friend_type
= TREE_TYPE (friend_type
);
9058 adjust_processing_template_decl
= true;
9060 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9061 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9063 /* This could be either
9067 when dependent_type_p is false or
9069 template <class U> friend class T::C;
9072 friend_type
= tsubst (friend_type
, args
,
9073 tf_warning_or_error
, NULL_TREE
);
9074 /* Bump processing_template_decl for correct
9075 dependent_type_p calculation. */
9076 ++processing_template_decl
;
9077 if (dependent_type_p (friend_type
))
9078 adjust_processing_template_decl
= true;
9079 --processing_template_decl
;
9081 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9082 && hidden_name_p (TYPE_NAME (friend_type
)))
9086 where C hasn't been declared yet. Let's lookup name
9087 from namespace scope directly, bypassing any name that
9088 come from dependent base class. */
9089 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9091 /* The call to xref_tag_from_type does injection for friend
9093 push_nested_namespace (ns
);
9095 xref_tag_from_type (friend_type
, NULL_TREE
,
9096 /*tag_scope=*/ts_current
);
9097 pop_nested_namespace (ns
);
9099 else if (uses_template_parms (friend_type
))
9100 /* friend class C<T>; */
9101 friend_type
= tsubst (friend_type
, args
,
9102 tf_warning_or_error
, NULL_TREE
);
9107 where C is already declared or
9109 friend class C<int>;
9111 We don't have to do anything in these cases. */
9113 if (adjust_processing_template_decl
)
9114 /* Trick make_friend_class into realizing that the friend
9115 we're adding is a template, not an ordinary class. It's
9116 important that we use make_friend_class since it will
9117 perform some error-checking and output cross-reference
9119 ++processing_template_decl
;
9121 if (friend_type
!= error_mark_node
)
9122 make_friend_class (type
, friend_type
, /*complain=*/false);
9124 if (adjust_processing_template_decl
)
9125 --processing_template_decl
;
9129 /* Build new DECL_FRIENDLIST. */
9132 /* The file and line for this declaration, to
9133 assist in error message reporting. Since we
9134 called push_tinst_level above, we don't need to
9136 input_location
= DECL_SOURCE_LOCATION (t
);
9138 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9140 ++processing_template_decl
;
9141 push_deferring_access_checks (dk_no_check
);
9144 r
= tsubst_friend_function (t
, args
);
9145 add_friend (type
, r
, /*complain=*/false);
9146 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9148 pop_deferring_access_checks ();
9149 --processing_template_decl
;
9155 if (CLASSTYPE_LAMBDA_EXPR (type
))
9157 tree decl
= lambda_function (type
);
9160 instantiate_decl (decl
, false, false);
9161 maybe_add_lambda_conv_op (type
);
9164 gcc_assert (errorcount
);
9167 /* Set the file and line number information to whatever is given for
9168 the class itself. This puts error messages involving generated
9169 implicit functions at a predictable point, and the same point
9170 that would be used for non-template classes. */
9171 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9173 unreverse_member_declarations (type
);
9174 finish_struct_1 (type
);
9175 TYPE_BEING_DEFINED (type
) = 0;
9177 /* We don't instantiate default arguments for member functions. 14.7.1:
9179 The implicit instantiation of a class template specialization causes
9180 the implicit instantiation of the declarations, but not of the
9181 definitions or default arguments, of the class member functions,
9182 member classes, static data members and member templates.... */
9184 /* Some typedefs referenced from within the template code need to be access
9185 checked at template instantiation time, i.e now. These types were
9186 added to the template at parsing time. Let's get those and perform
9187 the access checks then. */
9188 perform_typedefs_access_check (pattern
, args
);
9189 perform_deferred_access_checks ();
9190 pop_nested_class ();
9191 maximum_field_alignment
= saved_maximum_field_alignment
;
9192 pop_from_top_level ();
9193 pop_deferring_access_checks ();
9196 /* The vtable for a template class can be emitted in any translation
9197 unit in which the class is instantiated. When there is no key
9198 method, however, finish_struct_1 will already have added TYPE to
9199 the keyed_classes list. */
9200 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9201 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9206 /* Wrapper for instantiate_class_template_1. */
9209 instantiate_class_template (tree type
)
9212 timevar_push (TV_TEMPLATE_INST
);
9213 ret
= instantiate_class_template_1 (type
);
9214 timevar_pop (TV_TEMPLATE_INST
);
9219 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9225 else if (TYPE_P (t
))
9226 r
= tsubst (t
, args
, complain
, in_decl
);
9229 if (!(complain
& tf_warning
))
9230 ++c_inhibit_evaluation_warnings
;
9231 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9232 /*integral_constant_expression_p=*/true);
9233 if (!(complain
& tf_warning
))
9234 --c_inhibit_evaluation_warnings
;
9235 /* Preserve the raw-reference nature of T. */
9236 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9237 && REFERENCE_REF_P (r
))
9238 r
= TREE_OPERAND (r
, 0);
9243 /* Given a function parameter pack TMPL_PARM and some function parameters
9244 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9245 and set *SPEC_P to point at the next point in the list. */
9248 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9250 /* Collect all of the extra "packed" parameters into an
9254 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9255 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9256 tree spec_parm
= *spec_p
;
9259 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9261 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9264 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9265 parmvec
= make_tree_vec (len
);
9266 parmtypevec
= make_tree_vec (len
);
9267 spec_parm
= *spec_p
;
9268 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9270 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9271 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9274 /* Build the argument packs. */
9275 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9276 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9277 TREE_TYPE (argpack
) = argtypepack
;
9278 *spec_p
= spec_parm
;
9283 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9284 NONTYPE_ARGUMENT_PACK. */
9287 make_fnparm_pack (tree spec_parm
)
9289 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9292 /* Substitute ARGS into T, which is an pack expansion
9293 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9294 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9295 (if only a partial substitution could be performed) or
9296 ERROR_MARK_NODE if there was an error. */
9298 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9302 tree pack
, packs
= NULL_TREE
;
9303 bool unsubstituted_packs
= false;
9304 bool real_packs
= false;
9305 int missing_level
= 0;
9308 struct pointer_map_t
*saved_local_specializations
= NULL
;
9309 bool need_local_specializations
= false;
9312 gcc_assert (PACK_EXPANSION_P (t
));
9313 pattern
= PACK_EXPANSION_PATTERN (t
);
9315 /* Add in any args remembered from an earlier partial instantiation. */
9316 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9318 levels
= TMPL_ARGS_DEPTH (args
);
9320 /* Determine the argument packs that will instantiate the parameter
9321 packs used in the expansion expression. While we're at it,
9322 compute the number of arguments to be expanded and make sure it
9324 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9325 pack
= TREE_CHAIN (pack
))
9327 tree parm_pack
= TREE_VALUE (pack
);
9328 tree arg_pack
= NULL_TREE
;
9329 tree orig_arg
= NULL_TREE
;
9332 if (TREE_CODE (parm_pack
) == BASES
)
9334 if (BASES_DIRECT (parm_pack
))
9335 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9336 args
, complain
, in_decl
, false));
9338 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9339 args
, complain
, in_decl
, false));
9341 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9343 if (PACK_EXPANSION_LOCAL_P (t
))
9344 arg_pack
= retrieve_local_specialization (parm_pack
);
9347 /* We can't rely on local_specializations for a parameter
9348 name used later in a function declaration (such as in a
9349 late-specified return type). Even if it exists, it might
9350 have the wrong value for a recursive call. Just make a
9351 dummy decl, since it's only used for its type. */
9352 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9353 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9354 /* Partial instantiation of the parm_pack, we can't build
9355 up an argument pack yet. */
9356 arg_pack
= NULL_TREE
;
9358 arg_pack
= make_fnparm_pack (arg_pack
);
9359 need_local_specializations
= true;
9365 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9367 if (level
<= levels
)
9368 arg_pack
= TMPL_ARG (args
, level
, idx
);
9371 orig_arg
= arg_pack
;
9372 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9373 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9375 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9376 /* This can only happen if we forget to expand an argument
9377 pack somewhere else. Just return an error, silently. */
9379 result
= make_tree_vec (1);
9380 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9384 if (arg_from_parm_pack_p (arg_pack
, parm_pack
))
9385 /* The argument pack that the parameter maps to is just an
9386 expansion of the parameter itself, such as one would find
9387 in the implicit typedef of a class inside the class itself.
9388 Consider this parameter "unsubstituted", so that we will
9389 maintain the outer pack expansion. */
9390 arg_pack
= NULL_TREE
;
9395 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9397 /* Don't bother trying to do a partial substitution with
9398 incomplete packs; we'll try again after deduction. */
9399 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9404 else if (len
!= my_len
)
9406 if (!(complain
& tf_error
))
9407 /* Fail quietly. */;
9408 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9409 error ("mismatched argument pack lengths while expanding "
9413 error ("mismatched argument pack lengths while expanding "
9416 return error_mark_node
;
9419 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
9420 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
),
9422 /* This isn't a real argument pack yet. */;
9426 /* Keep track of the parameter packs and their corresponding
9428 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9429 TREE_TYPE (packs
) = orig_arg
;
9433 /* We can't substitute for this parameter pack. We use a flag as
9434 well as the missing_level counter because function parameter
9435 packs don't have a level. */
9436 unsubstituted_packs
= true;
9437 if (!missing_level
|| missing_level
> level
)
9438 missing_level
= level
;
9442 /* We cannot expand this expansion expression, because we don't have
9443 all of the argument packs we need. */
9444 if (unsubstituted_packs
)
9448 /* We got some full packs, but we can't substitute them in until we
9449 have values for all the packs. So remember these until then. */
9452 t
= make_pack_expansion (pattern
);
9454 /* The call to add_to_template_args above assumes no overlap
9455 between saved args and new args, so prune away any fake
9456 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9457 if (missing_level
&& levels
>= missing_level
)
9459 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
)
9460 && missing_level
> 1);
9461 TREE_VEC_LENGTH (args
) = missing_level
- 1;
9462 save_args
= copy_node (args
);
9463 TREE_VEC_LENGTH (args
) = levels
;
9468 PACK_EXPANSION_EXTRA_ARGS (t
) = save_args
;
9472 /* There were no real arguments, we're just replacing a parameter
9473 pack with another version of itself. Substitute into the
9474 pattern and return a PACK_EXPANSION_*. The caller will need to
9476 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9477 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9478 /*integral_constant_expression_p=*/false);
9480 t
= tsubst (pattern
, args
, complain
, in_decl
);
9481 t
= make_pack_expansion (t
);
9486 /* We could not find any argument packs that work. */
9488 return error_mark_node
;
9490 if (need_local_specializations
)
9492 /* We're in a late-specified return type, so create our own local
9493 specializations map; the current map is either NULL or (in the
9494 case of recursive unification) might have bindings that we don't
9495 want to use or alter. */
9496 saved_local_specializations
= local_specializations
;
9497 local_specializations
= pointer_map_create ();
9500 /* For each argument in each argument pack, substitute into the
9502 result
= make_tree_vec (len
);
9503 for (i
= 0; i
< len
; ++i
)
9505 /* For parameter pack, change the substitution of the parameter
9506 pack to the ith argument in its argument pack, then expand
9508 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9510 tree parm
= TREE_PURPOSE (pack
);
9513 /* Select the Ith argument from the pack. */
9514 if (TREE_CODE (parm
) == PARM_DECL
)
9518 arg
= make_node (ARGUMENT_PACK_SELECT
);
9519 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9521 register_local_specialization (arg
, parm
);
9524 arg
= retrieve_local_specialization (parm
);
9529 template_parm_level_and_index (parm
, &level
, &idx
);
9533 arg
= make_node (ARGUMENT_PACK_SELECT
);
9534 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9535 /* Update the corresponding argument. */
9536 TMPL_ARG (args
, level
, idx
) = arg
;
9539 /* Re-use the ARGUMENT_PACK_SELECT. */
9540 arg
= TMPL_ARG (args
, level
, idx
);
9542 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
9545 /* Substitute into the PATTERN with the altered arguments. */
9546 if (!TYPE_P (pattern
))
9547 TREE_VEC_ELT (result
, i
) =
9548 tsubst_expr (pattern
, args
, complain
, in_decl
,
9549 /*integral_constant_expression_p=*/false);
9551 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
9553 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
9555 result
= error_mark_node
;
9560 /* Update ARGS to restore the substitution from parameter packs to
9561 their argument packs. */
9562 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9564 tree parm
= TREE_PURPOSE (pack
);
9566 if (TREE_CODE (parm
) == PARM_DECL
)
9567 register_local_specialization (TREE_TYPE (pack
), parm
);
9571 template_parm_level_and_index (parm
, &level
, &idx
);
9573 /* Update the corresponding argument. */
9574 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9575 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9578 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9582 if (need_local_specializations
)
9584 pointer_map_destroy (local_specializations
);
9585 local_specializations
= saved_local_specializations
;
9591 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9592 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9593 parameter packs; all parms generated from a function parameter pack will
9594 have the same DECL_PARM_INDEX. */
9597 get_pattern_parm (tree parm
, tree tmpl
)
9599 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9602 if (DECL_ARTIFICIAL (parm
))
9604 for (patparm
= DECL_ARGUMENTS (pattern
);
9605 patparm
; patparm
= DECL_CHAIN (patparm
))
9606 if (DECL_ARTIFICIAL (patparm
)
9607 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9612 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9613 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9614 gcc_assert (DECL_PARM_INDEX (patparm
)
9615 == DECL_PARM_INDEX (parm
));
9621 /* Substitute ARGS into the vector or list of template arguments T. */
9624 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9627 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9630 if (t
== error_mark_node
)
9631 return error_mark_node
;
9633 len
= TREE_VEC_LENGTH (t
);
9634 elts
= XALLOCAVEC (tree
, len
);
9636 for (i
= 0; i
< len
; i
++)
9638 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9641 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9642 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9643 else if (PACK_EXPANSION_P (orig_arg
))
9645 /* Substitute into an expansion expression. */
9646 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9648 if (TREE_CODE (new_arg
) == TREE_VEC
)
9649 /* Add to the expanded length adjustment the number of
9650 expanded arguments. We subtract one from this
9651 measurement, because the argument pack expression
9652 itself is already counted as 1 in
9653 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9654 the argument pack is empty. */
9655 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9657 else if (ARGUMENT_PACK_P (orig_arg
))
9659 /* Substitute into each of the arguments. */
9660 new_arg
= TYPE_P (orig_arg
)
9661 ? cxx_make_type (TREE_CODE (orig_arg
))
9662 : make_node (TREE_CODE (orig_arg
));
9664 SET_ARGUMENT_PACK_ARGS (
9666 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9667 args
, complain
, in_decl
));
9669 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9670 new_arg
= error_mark_node
;
9672 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9673 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9675 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9677 if (TREE_TYPE (new_arg
) == error_mark_node
)
9678 new_arg
= error_mark_node
;
9682 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9684 if (new_arg
== error_mark_node
)
9685 return error_mark_node
;
9688 if (new_arg
!= orig_arg
)
9695 /* Make space for the expanded arguments coming from template
9697 t
= make_tree_vec (len
+ expanded_len_adjust
);
9698 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9699 arguments for a member template.
9700 In that case each TREE_VEC in ORIG_T represents a level of template
9701 arguments, and ORIG_T won't carry any non defaulted argument count.
9702 It will rather be the nested TREE_VECs that will carry one.
9703 In other words, ORIG_T carries a non defaulted argument count only
9704 if it doesn't contain any nested TREE_VEC. */
9705 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9707 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9708 count
+= expanded_len_adjust
;
9709 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9711 for (i
= 0, out
= 0; i
< len
; i
++)
9713 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9714 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9715 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9719 /* Now expand the template argument pack "in place". */
9720 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9721 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9725 TREE_VEC_ELT (t
, out
) = elts
[i
];
9733 /* Return the result of substituting ARGS into the template parameters
9734 given by PARMS. If there are m levels of ARGS and m + n levels of
9735 PARMS, then the result will contain n levels of PARMS. For
9736 example, if PARMS is `template <class T> template <class U>
9737 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9738 result will be `template <int*, double, class V>'. */
9741 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9746 /* When substituting into a template, we must set
9747 PROCESSING_TEMPLATE_DECL as the template parameters may be
9748 dependent if they are based on one-another, and the dependency
9749 predicates are short-circuit outside of templates. */
9750 ++processing_template_decl
;
9752 for (new_parms
= &r
;
9753 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9754 new_parms
= &(TREE_CHAIN (*new_parms
)),
9755 parms
= TREE_CHAIN (parms
))
9758 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9761 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9765 if (parms
== error_mark_node
)
9768 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9770 if (tuple
== error_mark_node
)
9773 TREE_VEC_ELT (new_vec
, i
) =
9774 tsubst_template_parm (tuple
, args
, complain
);
9778 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9779 - TMPL_ARGS_DEPTH (args
)),
9780 new_vec
, NULL_TREE
);
9783 --processing_template_decl
;
9788 /* Return the result of substituting ARGS into one template parameter
9789 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9790 parameter and which TREE_PURPOSE is the default argument of the
9791 template parameter. */
9794 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9796 tree default_value
, parm_decl
;
9798 if (args
== NULL_TREE
9800 || t
== error_mark_node
)
9803 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9805 default_value
= TREE_PURPOSE (t
);
9806 parm_decl
= TREE_VALUE (t
);
9808 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9809 if (TREE_CODE (parm_decl
) == PARM_DECL
9810 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9811 parm_decl
= error_mark_node
;
9812 default_value
= tsubst_template_arg (default_value
, args
,
9813 complain
, NULL_TREE
);
9815 return build_tree_list (default_value
, parm_decl
);
9818 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9819 type T. If T is not an aggregate or enumeration type, it is
9820 handled as if by tsubst. IN_DECL is as for tsubst. If
9821 ENTERING_SCOPE is nonzero, T is the context for a template which
9822 we are presently tsubst'ing. Return the substituted value. */
9825 tsubst_aggr_type (tree t
,
9827 tsubst_flags_t complain
,
9834 switch (TREE_CODE (t
))
9837 if (TYPE_PTRMEMFUNC_P (t
))
9838 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9840 /* Else fall through. */
9843 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9848 int saved_unevaluated_operand
;
9849 int saved_inhibit_evaluation_warnings
;
9851 /* In "sizeof(X<I>)" we need to evaluate "I". */
9852 saved_unevaluated_operand
= cp_unevaluated_operand
;
9853 cp_unevaluated_operand
= 0;
9854 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9855 c_inhibit_evaluation_warnings
= 0;
9857 /* First, determine the context for the type we are looking
9859 context
= TYPE_CONTEXT (t
);
9860 if (context
&& TYPE_P (context
))
9862 context
= tsubst_aggr_type (context
, args
, complain
,
9863 in_decl
, /*entering_scope=*/1);
9864 /* If context is a nested class inside a class template,
9865 it may still need to be instantiated (c++/33959). */
9866 context
= complete_type (context
);
9869 /* Then, figure out what arguments are appropriate for the
9870 type we are trying to find. For example, given:
9872 template <class T> struct S;
9873 template <class T, class U> void f(T, U) { S<U> su; }
9875 and supposing that we are instantiating f<int, double>,
9876 then our ARGS will be {int, double}, but, when looking up
9877 S we only want {double}. */
9878 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9880 if (argvec
== error_mark_node
)
9881 r
= error_mark_node
;
9884 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9885 entering_scope
, complain
);
9886 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9889 cp_unevaluated_operand
= saved_unevaluated_operand
;
9890 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9895 /* This is not a template type, so there's nothing to do. */
9899 return tsubst (t
, args
, complain
, in_decl
);
9903 /* Substitute into the default argument ARG (a default argument for
9904 FN), which has the indicated TYPE. */
9907 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9909 tree saved_class_ptr
= NULL_TREE
;
9910 tree saved_class_ref
= NULL_TREE
;
9912 /* This can happen in invalid code. */
9913 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9916 /* This default argument came from a template. Instantiate the
9917 default argument here, not in tsubst. In the case of
9926 we must be careful to do name lookup in the scope of S<T>,
9927 rather than in the current class. */
9928 push_access_scope (fn
);
9929 /* The "this" pointer is not valid in a default argument. */
9932 saved_class_ptr
= current_class_ptr
;
9933 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9934 saved_class_ref
= current_class_ref
;
9935 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9938 push_deferring_access_checks(dk_no_deferred
);
9939 /* The default argument expression may cause implicitly defined
9940 member functions to be synthesized, which will result in garbage
9941 collection. We must treat this situation as if we were within
9942 the body of function so as to avoid collecting live data on the
9945 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9946 tf_warning_or_error
, NULL_TREE
,
9947 /*integral_constant_expression_p=*/false);
9949 pop_deferring_access_checks();
9951 /* Restore the "this" pointer. */
9954 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9955 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9958 /* Make sure the default argument is reasonable. */
9959 arg
= check_default_argument (type
, arg
);
9961 pop_access_scope (fn
);
9966 /* Substitute into all the default arguments for FN. */
9969 tsubst_default_arguments (tree fn
)
9974 tmpl_args
= DECL_TI_ARGS (fn
);
9976 /* If this function is not yet instantiated, we certainly don't need
9977 its default arguments. */
9978 if (uses_template_parms (tmpl_args
))
9980 /* Don't do this again for clones. */
9981 if (DECL_CLONED_FUNCTION_P (fn
))
9984 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9986 arg
= TREE_CHAIN (arg
))
9987 if (TREE_PURPOSE (arg
))
9988 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9990 TREE_PURPOSE (arg
));
9993 /* Substitute the ARGS into the T, which is a _DECL. Return the
9994 result of the substitution. Issue error and warning messages under
9995 control of COMPLAIN. */
9998 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
10000 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
10001 location_t saved_loc
;
10002 tree r
= NULL_TREE
;
10004 hashval_t hash
= 0;
10006 /* Set the filename and linenumber to improve error-reporting. */
10007 saved_loc
= input_location
;
10008 input_location
= DECL_SOURCE_LOCATION (t
);
10010 switch (TREE_CODE (t
))
10012 case TEMPLATE_DECL
:
10014 /* We can get here when processing a member function template,
10015 member class template, or template template parameter. */
10016 tree decl
= DECL_TEMPLATE_RESULT (t
);
10021 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10023 /* Template template parameter is treated here. */
10024 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10025 if (new_type
== error_mark_node
)
10026 RETURN (error_mark_node
);
10029 DECL_CHAIN (r
) = NULL_TREE
;
10030 TREE_TYPE (r
) = new_type
;
10031 DECL_TEMPLATE_RESULT (r
)
10032 = build_decl (DECL_SOURCE_LOCATION (decl
),
10033 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10034 DECL_TEMPLATE_PARMS (r
)
10035 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10037 TYPE_NAME (new_type
) = r
;
10041 /* We might already have an instance of this template.
10042 The ARGS are for the surrounding class type, so the
10043 full args contain the tsubst'd args for the context,
10044 plus the innermost args from the template decl. */
10045 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10046 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10047 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10048 /* Because this is a template, the arguments will still be
10049 dependent, even after substitution. If
10050 PROCESSING_TEMPLATE_DECL is not set, the dependency
10051 predicates will short-circuit. */
10052 ++processing_template_decl
;
10053 full_args
= tsubst_template_args (tmpl_args
, args
,
10054 complain
, in_decl
);
10055 --processing_template_decl
;
10056 if (full_args
== error_mark_node
)
10057 RETURN (error_mark_node
);
10059 /* If this is a default template template argument,
10060 tsubst might not have changed anything. */
10061 if (full_args
== tmpl_args
)
10064 hash
= hash_tmpl_and_args (t
, full_args
);
10065 spec
= retrieve_specialization (t
, full_args
, hash
);
10066 if (spec
!= NULL_TREE
)
10072 /* Make a new template decl. It will be similar to the
10073 original, but will record the current template arguments.
10074 We also create a new function declaration, which is just
10075 like the old one, but points to this new template, rather
10076 than the old one. */
10078 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10079 DECL_CHAIN (r
) = NULL_TREE
;
10081 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10083 if (TREE_CODE (decl
) == TYPE_DECL
10084 && !TYPE_DECL_ALIAS_P (decl
))
10087 ++processing_template_decl
;
10088 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10089 --processing_template_decl
;
10090 if (new_type
== error_mark_node
)
10091 RETURN (error_mark_node
);
10093 TREE_TYPE (r
) = new_type
;
10094 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10095 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10096 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10097 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10102 ++processing_template_decl
;
10103 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10104 --processing_template_decl
;
10105 if (new_decl
== error_mark_node
)
10106 RETURN (error_mark_node
);
10108 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10109 DECL_TI_TEMPLATE (new_decl
) = r
;
10110 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10111 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10112 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10115 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10116 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10117 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10119 /* The template parameters for this new template are all the
10120 template parameters for the old template, except the
10121 outermost level of parameters. */
10122 DECL_TEMPLATE_PARMS (r
)
10123 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10126 if (PRIMARY_TEMPLATE_P (t
))
10127 DECL_PRIMARY_TEMPLATE (r
) = r
;
10129 if (TREE_CODE (decl
) != TYPE_DECL
)
10130 /* Record this non-type partial instantiation. */
10131 register_specialization (r
, t
,
10132 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10137 case FUNCTION_DECL
:
10140 tree argvec
= NULL_TREE
;
10148 /* Nobody should be tsubst'ing into non-template functions. */
10149 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10151 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10156 /* If T is not dependent, just return it. We have to
10157 increment PROCESSING_TEMPLATE_DECL because
10158 value_dependent_expression_p assumes that nothing is
10159 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10160 ++processing_template_decl
;
10161 dependent_p
= value_dependent_expression_p (t
);
10162 --processing_template_decl
;
10166 /* Calculate the most general template of which R is a
10167 specialization, and the complete set of arguments used to
10169 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10170 argvec
= tsubst_template_args (DECL_TI_ARGS
10171 (DECL_TEMPLATE_RESULT
10172 (DECL_TI_TEMPLATE (t
))),
10173 args
, complain
, in_decl
);
10174 if (argvec
== error_mark_node
)
10175 RETURN (error_mark_node
);
10177 /* Check to see if we already have this specialization. */
10178 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10179 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10187 /* We can see more levels of arguments than parameters if
10188 there was a specialization of a member template, like
10191 template <class T> struct S { template <class U> void f(); }
10192 template <> template <class U> void S<int>::f(U);
10194 Here, we'll be substituting into the specialization,
10195 because that's where we can find the code we actually
10196 want to generate, but we'll have enough arguments for
10197 the most general template.
10199 We also deal with the peculiar case:
10201 template <class T> struct S {
10202 template <class U> friend void f();
10204 template <class U> void f() {}
10206 template void f<double>();
10208 Here, the ARGS for the instantiation of will be {int,
10209 double}. But, we only need as many ARGS as there are
10210 levels of template parameters in CODE_PATTERN. We are
10211 careful not to get fooled into reducing the ARGS in
10214 template <class T> struct S { template <class U> void f(U); }
10215 template <class T> template <> void S<T>::f(int) {}
10217 which we can spot because the pattern will be a
10218 specialization in this case. */
10219 args_depth
= TMPL_ARGS_DEPTH (args
);
10221 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10222 if (args_depth
> parms_depth
10223 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10224 args
= get_innermost_template_args (args
, parms_depth
);
10228 /* This special case arises when we have something like this:
10230 template <class T> struct S {
10231 friend void f<int>(int, double);
10234 Here, the DECL_TI_TEMPLATE for the friend declaration
10235 will be an IDENTIFIER_NODE. We are being called from
10236 tsubst_friend_function, and we want only to create a
10237 new decl (R) with appropriate types so that we can call
10238 determine_specialization. */
10239 gen_tmpl
= NULL_TREE
;
10242 if (DECL_CLASS_SCOPE_P (t
))
10244 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10248 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10249 complain
, t
, /*entering_scope=*/1);
10254 ctx
= DECL_CONTEXT (t
);
10256 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10257 if (type
== error_mark_node
)
10258 RETURN (error_mark_node
);
10260 /* We do NOT check for matching decls pushed separately at this
10261 point, as they may not represent instantiations of this
10262 template, and in any case are considered separate under the
10265 DECL_USE_TEMPLATE (r
) = 0;
10266 TREE_TYPE (r
) = type
;
10267 /* Clear out the mangled name and RTL for the instantiation. */
10268 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10269 SET_DECL_RTL (r
, NULL
);
10270 /* Leave DECL_INITIAL set on deleted instantiations. */
10271 if (!DECL_DELETED_FN (r
))
10272 DECL_INITIAL (r
) = NULL_TREE
;
10273 DECL_CONTEXT (r
) = ctx
;
10275 if (member
&& DECL_CONV_FN_P (r
))
10276 /* Type-conversion operator. Reconstruct the name, in
10277 case it's the name of one of the template's parameters. */
10278 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10280 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10282 DECL_RESULT (r
) = NULL_TREE
;
10284 TREE_STATIC (r
) = 0;
10285 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10286 DECL_EXTERNAL (r
) = 1;
10287 /* If this is an instantiation of a function with internal
10288 linkage, we already know what object file linkage will be
10289 assigned to the instantiation. */
10290 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10291 DECL_DEFER_OUTPUT (r
) = 0;
10292 DECL_CHAIN (r
) = NULL_TREE
;
10293 DECL_PENDING_INLINE_INFO (r
) = 0;
10294 DECL_PENDING_INLINE_P (r
) = 0;
10295 DECL_SAVED_TREE (r
) = NULL_TREE
;
10296 DECL_STRUCT_FUNCTION (r
) = NULL
;
10298 /* We'll re-clone as appropriate in instantiate_template. */
10299 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10301 /* If we aren't complaining now, return on error before we register
10302 the specialization so that we'll complain eventually. */
10303 if ((complain
& tf_error
) == 0
10304 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10305 && !grok_op_properties (r
, /*complain=*/false))
10306 RETURN (error_mark_node
);
10308 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10309 this in the special friend case mentioned above where
10310 GEN_TMPL is NULL. */
10313 DECL_TEMPLATE_INFO (r
)
10314 = build_template_info (gen_tmpl
, argvec
);
10315 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10316 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10318 /* We're not supposed to instantiate default arguments
10319 until they are called, for a template. But, for a
10322 template <class T> void f ()
10323 { extern void g(int i = T()); }
10325 we should do the substitution when the template is
10326 instantiated. We handle the member function case in
10327 instantiate_class_template since the default arguments
10328 might refer to other members of the class. */
10330 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10331 && !uses_template_parms (argvec
))
10332 tsubst_default_arguments (r
);
10335 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10337 /* Copy the list of befriending classes. */
10338 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10340 friends
= &TREE_CHAIN (*friends
))
10342 *friends
= copy_node (*friends
);
10343 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10348 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10350 maybe_retrofit_in_chrg (r
);
10351 if (DECL_CONSTRUCTOR_P (r
))
10352 grok_ctor_properties (ctx
, r
);
10353 /* If this is an instantiation of a member template, clone it.
10354 If it isn't, that'll be handled by
10355 clone_constructors_and_destructors. */
10356 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10357 clone_function_decl (r
, /*update_method_vec_p=*/0);
10359 else if ((complain
& tf_error
) != 0
10360 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10361 && !grok_op_properties (r
, /*complain=*/true))
10362 RETURN (error_mark_node
);
10364 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10365 SET_DECL_FRIEND_CONTEXT (r
,
10366 tsubst (DECL_FRIEND_CONTEXT (t
),
10367 args
, complain
, in_decl
));
10369 /* Possibly limit visibility based on template args. */
10370 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10371 if (DECL_VISIBILITY_SPECIFIED (t
))
10373 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10374 DECL_ATTRIBUTES (r
)
10375 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10377 determine_visibility (r
);
10378 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10379 && !processing_template_decl
)
10380 defaulted_late_check (r
);
10382 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10383 args
, complain
, in_decl
);
10389 tree type
= NULL_TREE
;
10391 tree expanded_types
= NULL_TREE
;
10392 tree prev_r
= NULL_TREE
;
10393 tree first_r
= NULL_TREE
;
10395 if (FUNCTION_PARAMETER_PACK_P (t
))
10397 /* If there is a local specialization that isn't a
10398 parameter pack, it means that we're doing a "simple"
10399 substitution from inside tsubst_pack_expansion. Just
10400 return the local specialization (which will be a single
10402 tree spec
= retrieve_local_specialization (t
);
10404 && TREE_CODE (spec
) == PARM_DECL
10405 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10408 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10409 the parameters in this function parameter pack. */
10410 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10411 complain
, in_decl
);
10412 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10414 len
= TREE_VEC_LENGTH (expanded_types
);
10416 /* Zero-length parameter packs are boring. Just substitute
10419 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10424 /* All we did was update the type. Make a note of that. */
10425 type
= expanded_types
;
10426 expanded_types
= NULL_TREE
;
10430 /* Loop through all of the parameter's we'll build. When T is
10431 a function parameter pack, LEN is the number of expanded
10432 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10434 for (i
= 0; i
< len
; ++i
)
10438 if (DECL_TEMPLATE_PARM_P (t
))
10439 SET_DECL_TEMPLATE_PARM_P (r
);
10441 if (expanded_types
)
10442 /* We're on the Ith parameter of the function parameter
10445 /* An argument of a function parameter pack is not a parameter
10447 FUNCTION_PARAMETER_PACK_P (r
) = false;
10449 /* Get the Ith type. */
10450 type
= TREE_VEC_ELT (expanded_types
, i
);
10453 /* Rename the parameter to include the index. */
10455 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10458 /* We're dealing with a normal parameter. */
10459 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10461 type
= type_decays_to (type
);
10462 TREE_TYPE (r
) = type
;
10463 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10465 if (DECL_INITIAL (r
))
10467 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10468 DECL_INITIAL (r
) = TREE_TYPE (r
);
10470 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10471 complain
, in_decl
);
10474 DECL_CONTEXT (r
) = NULL_TREE
;
10476 if (!DECL_TEMPLATE_PARM_P (r
))
10477 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10479 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10480 args
, complain
, in_decl
);
10482 /* Keep track of the first new parameter we
10483 generate. That's what will be returned to the
10488 /* Build a proper chain of parameters when substituting
10489 into a function parameter pack. */
10491 DECL_CHAIN (prev_r
) = r
;
10494 if (DECL_CHAIN (t
))
10495 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10496 complain
, DECL_CHAIN (t
));
10498 /* FIRST_R contains the start of the chain we've built. */
10508 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10509 if (type
== error_mark_node
)
10510 RETURN (error_mark_node
);
10511 TREE_TYPE (r
) = type
;
10512 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10514 if (DECL_C_BIT_FIELD (r
))
10515 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10516 non-bit-fields DECL_INITIAL is a non-static data member
10517 initializer, which gets deferred instantiation. */
10519 = tsubst_expr (DECL_INITIAL (t
), args
,
10521 /*integral_constant_expression_p=*/true);
10522 else if (DECL_INITIAL (t
))
10524 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10525 NSDMI in perform_member_init. Still set DECL_INITIAL
10526 so that we know there is one. */
10527 DECL_INITIAL (r
) = void_zero_node
;
10528 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10529 retrofit_lang_decl (r
);
10530 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10532 /* We don't have to set DECL_CONTEXT here; it is set by
10533 finish_member_declaration. */
10534 DECL_CHAIN (r
) = NULL_TREE
;
10535 if (VOID_TYPE_P (type
))
10536 error ("instantiation of %q+D as type %qT", r
, type
);
10538 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10539 args
, complain
, in_decl
);
10544 /* We reach here only for member using decls. */
10545 if (DECL_DEPENDENT_P (t
))
10547 r
= do_class_using_decl
10548 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
10549 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
10551 r
= error_mark_node
;
10554 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10555 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10561 DECL_CHAIN (r
) = NULL_TREE
;
10568 tree argvec
= NULL_TREE
;
10569 tree gen_tmpl
= NULL_TREE
;
10571 tree tmpl
= NULL_TREE
;
10573 tree type
= NULL_TREE
;
10576 if (TREE_CODE (t
) == TYPE_DECL
10577 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10579 /* If this is the canonical decl, we don't have to
10580 mess with instantiations, and often we can't (for
10581 typename, template type parms and such). Note that
10582 TYPE_NAME is not correct for the above test if
10583 we've copied the type for a typedef. */
10584 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10585 if (type
== error_mark_node
)
10586 RETURN (error_mark_node
);
10587 r
= TYPE_NAME (type
);
10591 /* Check to see if we already have the specialization we
10594 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10596 /* T is a static data member or namespace-scope entity.
10597 We have to substitute into namespace-scope variables
10598 (even though such entities are never templates) because
10601 template <class T> void f() { extern T t; }
10603 where the entity referenced is not known until
10604 instantiation time. */
10606 ctx
= DECL_CONTEXT (t
);
10607 if (DECL_CLASS_SCOPE_P (t
))
10609 ctx
= tsubst_aggr_type (ctx
, args
,
10611 in_decl
, /*entering_scope=*/1);
10612 /* If CTX is unchanged, then T is in fact the
10613 specialization we want. That situation occurs when
10614 referencing a static data member within in its own
10615 class. We can use pointer equality, rather than
10616 same_type_p, because DECL_CONTEXT is always
10618 if (ctx
== DECL_CONTEXT (t
)
10619 && (TREE_CODE (t
) != TYPE_DECL
10620 /* ... unless T is a member template; in which
10621 case our caller can be willing to create a
10622 specialization of that template represented
10624 || !(DECL_TI_TEMPLATE (t
)
10625 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10631 tmpl
= DECL_TI_TEMPLATE (t
);
10632 gen_tmpl
= most_general_template (tmpl
);
10633 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10634 if (argvec
== error_mark_node
)
10635 RETURN (error_mark_node
);
10636 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10637 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10642 /* A local variable. */
10644 /* Subsequent calls to pushdecl will fill this in. */
10646 spec
= retrieve_local_specialization (t
);
10648 /* If we already have the specialization we need, there is
10649 nothing more to do. */
10656 /* Create a new node for the specialization we need. */
10658 if (type
== NULL_TREE
)
10660 if (is_typedef_decl (t
))
10661 type
= DECL_ORIGINAL_TYPE (t
);
10663 type
= TREE_TYPE (t
);
10664 if (TREE_CODE (t
) == VAR_DECL
10665 && VAR_HAD_UNKNOWN_BOUND (t
)
10666 && type
!= error_mark_node
)
10667 type
= strip_array_domain (type
);
10668 type
= tsubst (type
, args
, complain
, in_decl
);
10670 if (TREE_CODE (r
) == VAR_DECL
)
10672 /* Even if the original location is out of scope, the
10673 newly substituted one is not. */
10674 DECL_DEAD_FOR_LOCAL (r
) = 0;
10675 DECL_INITIALIZED_P (r
) = 0;
10676 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10677 if (type
== error_mark_node
)
10678 RETURN (error_mark_node
);
10679 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10681 /* It may seem that this case cannot occur, since:
10686 declares a function, not a variable. However:
10689 template <typename T> void g() { T t; }
10690 template void g<f>();
10692 is an attempt to declare a variable with function
10694 error ("variable %qD has function type",
10695 /* R is not yet sufficiently initialized, so we
10696 just use its name. */
10698 RETURN (error_mark_node
);
10700 type
= complete_type (type
);
10701 /* Wait until cp_finish_decl to set this again, to handle
10702 circular dependency (template/instantiate6.C). */
10703 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10704 type
= check_var_type (DECL_NAME (r
), type
);
10706 if (DECL_HAS_VALUE_EXPR_P (t
))
10708 tree ve
= DECL_VALUE_EXPR (t
);
10709 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10710 /*constant_expression_p=*/false);
10711 if (REFERENCE_REF_P (ve
))
10713 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10714 ve
= TREE_OPERAND (ve
, 0);
10716 SET_DECL_VALUE_EXPR (r
, ve
);
10719 else if (DECL_SELF_REFERENCE_P (t
))
10720 SET_DECL_SELF_REFERENCE_P (r
);
10721 TREE_TYPE (r
) = type
;
10722 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10723 DECL_CONTEXT (r
) = ctx
;
10724 /* Clear out the mangled name and RTL for the instantiation. */
10725 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10726 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10727 SET_DECL_RTL (r
, NULL
);
10728 /* The initializer must not be expanded until it is required;
10729 see [temp.inst]. */
10730 DECL_INITIAL (r
) = NULL_TREE
;
10731 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10732 SET_DECL_RTL (r
, NULL
);
10733 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10734 if (TREE_CODE (r
) == VAR_DECL
)
10736 /* Possibly limit visibility based on template args. */
10737 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10738 if (DECL_VISIBILITY_SPECIFIED (t
))
10740 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10741 DECL_ATTRIBUTES (r
)
10742 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10744 determine_visibility (r
);
10749 /* A static data member declaration is always marked
10750 external when it is declared in-class, even if an
10751 initializer is present. We mimic the non-template
10752 processing here. */
10753 DECL_EXTERNAL (r
) = 1;
10755 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10756 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10757 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10759 else if (cp_unevaluated_operand
)
10761 /* We're substituting this var in a decltype outside of its
10762 scope, such as for a lambda return type. Don't add it to
10763 local_specializations, do perform auto deduction. */
10764 tree auto_node
= type_uses_auto (type
);
10768 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10769 /*constant_expression_p=*/false);
10770 init
= resolve_nondeduced_context (init
);
10771 TREE_TYPE (r
) = type
10772 = do_auto_deduction (type
, init
, auto_node
);
10776 register_local_specialization (r
, t
);
10778 DECL_CHAIN (r
) = NULL_TREE
;
10780 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10782 args
, complain
, in_decl
);
10784 /* Preserve a typedef that names a type. */
10785 if (is_typedef_decl (r
))
10787 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10788 set_underlying_type (r
);
10791 layout_decl (r
, 0);
10796 gcc_unreachable ();
10801 /* Restore the file and line information. */
10802 input_location
= saved_loc
;
10807 /* Substitute into the ARG_TYPES of a function type. */
10810 tsubst_arg_types (tree arg_types
,
10812 tsubst_flags_t complain
,
10815 tree remaining_arg_types
;
10816 tree type
= NULL_TREE
;
10818 tree expanded_args
= NULL_TREE
;
10821 if (!arg_types
|| arg_types
== void_list_node
)
10824 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10825 args
, complain
, in_decl
);
10826 if (remaining_arg_types
== error_mark_node
)
10827 return error_mark_node
;
10829 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10831 /* For a pack expansion, perform substitution on the
10832 entire expression. Later on, we'll handle the arguments
10834 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10835 args
, complain
, in_decl
);
10837 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10838 /* So that we'll spin through the parameters, one by one. */
10839 i
= TREE_VEC_LENGTH (expanded_args
);
10842 /* We only partially substituted into the parameter
10843 pack. Our type is TYPE_PACK_EXPANSION. */
10844 type
= expanded_args
;
10845 expanded_args
= NULL_TREE
;
10853 type
= TREE_VEC_ELT (expanded_args
, i
);
10855 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10857 if (type
== error_mark_node
)
10858 return error_mark_node
;
10859 if (VOID_TYPE_P (type
))
10861 if (complain
& tf_error
)
10863 error ("invalid parameter type %qT", type
);
10865 error ("in declaration %q+D", in_decl
);
10867 return error_mark_node
;
10870 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10871 top-level qualifiers as required. */
10872 type
= cv_unqualified (type_decays_to (type
));
10874 /* We do not substitute into default arguments here. The standard
10875 mandates that they be instantiated only when needed, which is
10876 done in build_over_call. */
10877 default_arg
= TREE_PURPOSE (arg_types
);
10879 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10881 /* We've instantiated a template before its default arguments
10882 have been parsed. This can happen for a nested template
10883 class, and is not an error unless we require the default
10884 argument in a call of this function. */
10885 remaining_arg_types
=
10886 tree_cons (default_arg
, type
, remaining_arg_types
);
10887 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
10888 remaining_arg_types
);
10891 remaining_arg_types
=
10892 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10895 return remaining_arg_types
;
10898 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10899 *not* handle the exception-specification for FNTYPE, because the
10900 initial substitution of explicitly provided template parameters
10901 during argument deduction forbids substitution into the
10902 exception-specification:
10906 All references in the function type of the function template to the
10907 corresponding template parameters are replaced by the specified tem-
10908 plate argument values. If a substitution in a template parameter or
10909 in the function type of the function template results in an invalid
10910 type, type deduction fails. [Note: The equivalent substitution in
10911 exception specifications is done only when the function is instanti-
10912 ated, at which point a program is ill-formed if the substitution
10913 results in an invalid type.] */
10916 tsubst_function_type (tree t
,
10918 tsubst_flags_t complain
,
10925 /* The TYPE_CONTEXT is not used for function/method types. */
10926 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10928 /* Substitute the return type. */
10929 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10930 if (return_type
== error_mark_node
)
10931 return error_mark_node
;
10932 /* The standard does not presently indicate that creation of a
10933 function type with an invalid return type is a deduction failure.
10934 However, that is clearly analogous to creating an array of "void"
10935 or a reference to a reference. This is core issue #486. */
10936 if (TREE_CODE (return_type
) == ARRAY_TYPE
10937 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10939 if (complain
& tf_error
)
10941 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10942 error ("function returning an array");
10944 error ("function returning a function");
10946 return error_mark_node
;
10949 /* Substitute the argument types. */
10950 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
10951 complain
, in_decl
);
10952 if (arg_types
== error_mark_node
)
10953 return error_mark_node
;
10955 /* Construct a new type node and return it. */
10956 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10958 fntype
= build_function_type (return_type
, arg_types
);
10959 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10963 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10964 if (! MAYBE_CLASS_TYPE_P (r
))
10968 Type deduction may fail for any of the following
10971 -- Attempting to create "pointer to member of T" when T
10972 is not a class type. */
10973 if (complain
& tf_error
)
10974 error ("creating pointer to member function of non-class type %qT",
10976 return error_mark_node
;
10979 fntype
= build_method_type_directly (r
, return_type
,
10980 TREE_CHAIN (arg_types
));
10982 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10987 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10988 ARGS into that specification, and return the substituted
10989 specification. If there is no specification, return NULL_TREE. */
10992 tsubst_exception_specification (tree fntype
,
10994 tsubst_flags_t complain
,
11001 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
11002 new_specs
= NULL_TREE
;
11003 if (specs
&& TREE_PURPOSE (specs
))
11005 /* A noexcept-specifier. */
11006 tree expr
= TREE_PURPOSE (specs
);
11007 if (expr
== boolean_true_node
|| expr
== boolean_false_node
)
11011 /* Defer instantiation of noexcept-specifiers to avoid
11012 excessive instantiations (c++/49107). */
11013 new_specs
= make_node (DEFERRED_NOEXCEPT
);
11014 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
11016 /* We already partially instantiated this member template,
11017 so combine the new args with the old. */
11018 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
11019 = DEFERRED_NOEXCEPT_PATTERN (expr
);
11020 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11021 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11025 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11026 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11030 new_specs
= tsubst_copy_and_build
11031 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11032 /*integral_constant_expression_p=*/true);
11033 new_specs
= build_noexcept_spec (new_specs
, complain
);
11037 if (! TREE_VALUE (specs
))
11044 tree expanded_specs
= NULL_TREE
;
11046 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11048 /* Expand the pack expansion type. */
11049 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11053 if (expanded_specs
== error_mark_node
)
11054 return error_mark_node
;
11055 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11056 len
= TREE_VEC_LENGTH (expanded_specs
);
11059 /* We're substituting into a member template, so
11060 we got a TYPE_PACK_EXPANSION back. Add that
11061 expansion and move on. */
11062 gcc_assert (TREE_CODE (expanded_specs
)
11063 == TYPE_PACK_EXPANSION
);
11064 new_specs
= add_exception_specifier (new_specs
,
11067 specs
= TREE_CHAIN (specs
);
11072 for (i
= 0; i
< len
; ++i
)
11074 if (expanded_specs
)
11075 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11077 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11078 if (spec
== error_mark_node
)
11080 new_specs
= add_exception_specifier (new_specs
, spec
,
11084 specs
= TREE_CHAIN (specs
);
11090 /* Take the tree structure T and replace template parameters used
11091 therein with the argument vector ARGS. IN_DECL is an associated
11092 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11093 Issue error and warning messages under control of COMPLAIN. Note
11094 that we must be relatively non-tolerant of extensions here, in
11095 order to preserve conformance; if we allow substitutions that
11096 should not be allowed, we may allow argument deductions that should
11097 not succeed, and therefore report ambiguous overload situations
11098 where there are none. In theory, we could allow the substitution,
11099 but indicate that it should have failed, and allow our caller to
11100 make sure that the right thing happens, but we don't try to do this
11103 This function is used for dealing with types, decls and the like;
11104 for expressions, use tsubst_expr or tsubst_copy. */
11107 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11109 enum tree_code code
;
11110 tree type
, r
= NULL_TREE
;
11112 if (t
== NULL_TREE
|| t
== error_mark_node
11113 || t
== integer_type_node
11114 || t
== void_type_node
11115 || t
== char_type_node
11116 || t
== unknown_type_node
11117 || TREE_CODE (t
) == NAMESPACE_DECL
11118 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11122 return tsubst_decl (t
, args
, complain
);
11124 if (args
== NULL_TREE
)
11127 code
= TREE_CODE (t
);
11129 if (code
== IDENTIFIER_NODE
)
11130 type
= IDENTIFIER_TYPE_VALUE (t
);
11132 type
= TREE_TYPE (t
);
11134 gcc_assert (type
!= unknown_type_node
);
11136 /* Reuse typedefs. We need to do this to handle dependent attributes,
11137 such as attribute aligned. */
11139 && typedef_variant_p (t
))
11141 tree decl
= TYPE_NAME (t
);
11143 if (TYPE_DECL_ALIAS_P (decl
)
11144 && DECL_LANG_SPECIFIC (decl
)
11145 && DECL_TEMPLATE_INFO (decl
)
11146 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
11148 /* DECL represents an alias template and we want to
11149 instantiate it. Let's substitute our arguments for the
11150 template parameters into the declaration and get the
11152 r
= tsubst (decl
, args
, complain
, decl
);
11154 else if (DECL_CLASS_SCOPE_P (decl
)
11155 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11156 && uses_template_parms (DECL_CONTEXT (decl
)))
11158 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11159 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11160 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11162 else if (DECL_FUNCTION_SCOPE_P (decl
)
11163 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11164 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11165 r
= retrieve_local_specialization (decl
);
11167 /* The typedef is from a non-template context. */
11173 r
= cp_build_qualified_type_real
11174 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11175 complain
| tf_ignore_bad_quals
);
11179 /* We don't have an instantiation yet, so drop the typedef. */
11180 t
= DECL_ORIGINAL_TYPE (decl
);
11184 && code
!= TYPENAME_TYPE
11185 && code
!= TEMPLATE_TYPE_PARM
11186 && code
!= IDENTIFIER_NODE
11187 && code
!= FUNCTION_TYPE
11188 && code
!= METHOD_TYPE
)
11189 type
= tsubst (type
, args
, complain
, in_decl
);
11190 if (type
== error_mark_node
)
11191 return error_mark_node
;
11197 case ENUMERAL_TYPE
:
11198 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11199 /*entering_scope=*/0);
11202 case IDENTIFIER_NODE
:
11213 if (t
== integer_type_node
)
11216 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11217 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11221 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11223 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11224 /*integral_constant_expression_p=*/false);
11226 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11228 if (TREE_CODE (max
) == NOP_EXPR
11229 && TREE_SIDE_EFFECTS (omax
)
11230 && !TREE_TYPE (max
))
11231 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11233 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11234 with TREE_SIDE_EFFECTS that indicates this is not an integral
11235 constant expression. */
11236 if (processing_template_decl
11237 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11239 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11240 TREE_SIDE_EFFECTS (max
) = 1;
11243 return compute_array_index_type (NULL_TREE
, max
, complain
);
11246 case TEMPLATE_TYPE_PARM
:
11247 case TEMPLATE_TEMPLATE_PARM
:
11248 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11249 case TEMPLATE_PARM_INDEX
:
11254 tree arg
= NULL_TREE
;
11258 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11259 template_parm_level_and_index (t
, &level
, &idx
);
11261 levels
= TMPL_ARGS_DEPTH (args
);
11262 if (level
<= levels
)
11264 arg
= TMPL_ARG (args
, level
, idx
);
11266 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11267 /* See through ARGUMENT_PACK_SELECT arguments. */
11268 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11271 if (arg
== error_mark_node
)
11272 return error_mark_node
;
11273 else if (arg
!= NULL_TREE
)
11275 if (ARGUMENT_PACK_P (arg
))
11276 /* If ARG is an argument pack, we don't actually want to
11277 perform a substitution here, because substitutions
11278 for argument packs are only done
11279 element-by-element. We can get to this point when
11280 substituting the type of a non-type template
11281 parameter pack, when that type actually contains
11282 template parameter packs from an outer template, e.g.,
11284 template<typename... Types> struct A {
11285 template<Types... Values> struct B { };
11289 if (code
== TEMPLATE_TYPE_PARM
)
11292 gcc_assert (TYPE_P (arg
));
11294 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11296 return cp_build_qualified_type_real
11297 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11299 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11301 /* We are processing a type constructed from a
11302 template template parameter. */
11303 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11304 args
, complain
, in_decl
);
11305 if (argvec
== error_mark_node
)
11306 return error_mark_node
;
11308 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11309 || TREE_CODE (arg
) == TEMPLATE_DECL
11310 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11312 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11313 /* Consider this code:
11315 template <template <class> class Template>
11317 template <class Arg> using Bind = Template<Arg>;
11320 template <template <class> class Template, class Arg>
11321 using Instantiate = Template<Arg>; //#0
11323 template <template <class> class Template,
11326 Instantiate<Internal<Template>::template Bind,
11329 When #1 is parsed, the
11330 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11331 parameter `Template' in #0 matches the
11332 UNBOUND_CLASS_TEMPLATE representing the argument
11333 `Internal<Template>::template Bind'; We then want
11334 to assemble the type `Bind<Argument>' that can't
11335 be fully created right now, because
11336 `Internal<Template>' not being complete, the Bind
11337 template cannot be looked up in that context. So
11338 we need to "store" `Bind<Argument>' for later
11339 when the context of Bind becomes complete. Let's
11340 store that in a TYPENAME_TYPE. */
11341 return make_typename_type (TYPE_CONTEXT (arg
),
11342 build_nt (TEMPLATE_ID_EXPR
,
11343 TYPE_IDENTIFIER (arg
),
11348 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11349 are resolving nested-types in the signature of a
11350 member function templates. Otherwise ARG is a
11351 TEMPLATE_DECL and is the real template to be
11353 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11354 arg
= TYPE_NAME (arg
);
11356 r
= lookup_template_class (arg
,
11358 DECL_CONTEXT (arg
),
11359 /*entering_scope=*/0,
11361 return cp_build_qualified_type_real
11362 (r
, cp_type_quals (t
), complain
);
11365 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11366 return convert_from_reference (unshare_expr (arg
));
11370 /* This can happen during the attempted tsubst'ing in
11371 unify. This means that we don't yet have any information
11372 about the template parameter in question. */
11375 /* Early in template argument deduction substitution, we don't
11376 want to reduce the level of 'auto', or it will be confused
11377 with a normal template parm in subsequent deduction. */
11378 if (is_auto (t
) && (complain
& tf_partial
))
11381 /* If we get here, we must have been looking at a parm for a
11382 more deeply nested template. Make a new version of this
11383 template parameter, but with a lower level. */
11386 case TEMPLATE_TYPE_PARM
:
11387 case TEMPLATE_TEMPLATE_PARM
:
11388 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11389 if (cp_type_quals (t
))
11391 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11392 r
= cp_build_qualified_type_real
11393 (r
, cp_type_quals (t
),
11394 complain
| (code
== TEMPLATE_TYPE_PARM
11395 ? tf_ignore_bad_quals
: 0));
11400 TEMPLATE_TYPE_PARM_INDEX (r
)
11401 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11402 r
, levels
, args
, complain
);
11403 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11404 TYPE_MAIN_VARIANT (r
) = r
;
11405 TYPE_POINTER_TO (r
) = NULL_TREE
;
11406 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11408 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11409 /* We have reduced the level of the template
11410 template parameter, but not the levels of its
11411 template parameters, so canonical_type_parameter
11412 will not be able to find the canonical template
11413 template parameter for this level. Thus, we
11414 require structural equality checking to compare
11415 TEMPLATE_TEMPLATE_PARMs. */
11416 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11417 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11418 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11420 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11422 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11424 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11425 complain
, in_decl
);
11426 if (argvec
== error_mark_node
)
11427 return error_mark_node
;
11429 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11430 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11435 case TEMPLATE_PARM_INDEX
:
11436 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11440 gcc_unreachable ();
11448 tree purpose
, value
, chain
;
11450 if (t
== void_list_node
)
11453 purpose
= TREE_PURPOSE (t
);
11456 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11457 if (purpose
== error_mark_node
)
11458 return error_mark_node
;
11460 value
= TREE_VALUE (t
);
11463 value
= tsubst (value
, args
, complain
, in_decl
);
11464 if (value
== error_mark_node
)
11465 return error_mark_node
;
11467 chain
= TREE_CHAIN (t
);
11468 if (chain
&& chain
!= void_type_node
)
11470 chain
= tsubst (chain
, args
, complain
, in_decl
);
11471 if (chain
== error_mark_node
)
11472 return error_mark_node
;
11474 if (purpose
== TREE_PURPOSE (t
)
11475 && value
== TREE_VALUE (t
)
11476 && chain
== TREE_CHAIN (t
))
11478 return hash_tree_cons (purpose
, value
, chain
);
11482 /* We should never be tsubsting a binfo. */
11483 gcc_unreachable ();
11486 /* A vector of template arguments. */
11487 gcc_assert (!type
);
11488 return tsubst_template_args (t
, args
, complain
, in_decl
);
11491 case REFERENCE_TYPE
:
11493 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11498 Type deduction may fail for any of the following
11501 -- Attempting to create a pointer to reference type.
11502 -- Attempting to create a reference to a reference type or
11503 a reference to void.
11505 Core issue 106 says that creating a reference to a reference
11506 during instantiation is no longer a cause for failure. We
11507 only enforce this check in strict C++98 mode. */
11508 if ((TREE_CODE (type
) == REFERENCE_TYPE
11509 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11510 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11512 static location_t last_loc
;
11514 /* We keep track of the last time we issued this error
11515 message to avoid spewing a ton of messages during a
11516 single bad template instantiation. */
11517 if (complain
& tf_error
11518 && last_loc
!= input_location
)
11520 if (TREE_CODE (type
) == VOID_TYPE
)
11521 error ("forming reference to void");
11522 else if (code
== POINTER_TYPE
)
11523 error ("forming pointer to reference type %qT", type
);
11525 error ("forming reference to reference type %qT", type
);
11526 last_loc
= input_location
;
11529 return error_mark_node
;
11531 else if (code
== POINTER_TYPE
)
11533 r
= build_pointer_type (type
);
11534 if (TREE_CODE (type
) == METHOD_TYPE
)
11535 r
= build_ptrmemfunc_type (r
);
11537 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11538 /* In C++0x, during template argument substitution, when there is an
11539 attempt to create a reference to a reference type, reference
11540 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11542 "If a template-argument for a template-parameter T names a type
11543 that is a reference to a type A, an attempt to create the type
11544 'lvalue reference to cv T' creates the type 'lvalue reference to
11545 A,' while an attempt to create the type type rvalue reference to
11546 cv T' creates the type T"
11548 r
= cp_build_reference_type
11550 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11552 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11553 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11555 if (r
!= error_mark_node
)
11556 /* Will this ever be needed for TYPE_..._TO values? */
11563 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11564 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11568 Type deduction may fail for any of the following
11571 -- Attempting to create "pointer to member of T" when T
11572 is not a class type. */
11573 if (complain
& tf_error
)
11574 error ("creating pointer to member of non-class type %qT", r
);
11575 return error_mark_node
;
11577 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11579 if (complain
& tf_error
)
11580 error ("creating pointer to member reference type %qT", type
);
11581 return error_mark_node
;
11583 if (TREE_CODE (type
) == VOID_TYPE
)
11585 if (complain
& tf_error
)
11586 error ("creating pointer to member of type void");
11587 return error_mark_node
;
11589 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11590 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11592 /* The type of the implicit object parameter gets its
11593 cv-qualifiers from the FUNCTION_TYPE. */
11595 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11596 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11597 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11601 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11605 case FUNCTION_TYPE
:
11610 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11611 if (fntype
== error_mark_node
)
11612 return error_mark_node
;
11614 /* Substitute the exception specification. */
11615 specs
= tsubst_exception_specification (t
, args
, complain
,
11616 in_decl
, /*defer_ok*/true);
11617 if (specs
== error_mark_node
)
11618 return error_mark_node
;
11620 fntype
= build_exception_variant (fntype
, specs
);
11625 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11626 if (domain
== error_mark_node
)
11627 return error_mark_node
;
11629 /* As an optimization, we avoid regenerating the array type if
11630 it will obviously be the same as T. */
11631 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11634 /* These checks should match the ones in grokdeclarator.
11638 The deduction may fail for any of the following reasons:
11640 -- Attempting to create an array with an element type that
11641 is void, a function type, or a reference type, or [DR337]
11642 an abstract class type. */
11643 if (TREE_CODE (type
) == VOID_TYPE
11644 || TREE_CODE (type
) == FUNCTION_TYPE
11645 || TREE_CODE (type
) == REFERENCE_TYPE
)
11647 if (complain
& tf_error
)
11648 error ("creating array of %qT", type
);
11649 return error_mark_node
;
11651 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
11653 if (complain
& tf_error
)
11654 error ("creating array of %qT, which is an abstract class type",
11656 return error_mark_node
;
11659 r
= build_cplus_array_type (type
, domain
);
11661 if (TYPE_USER_ALIGN (t
))
11663 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11664 TYPE_USER_ALIGN (r
) = 1;
11670 case TYPENAME_TYPE
:
11672 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11673 in_decl
, /*entering_scope=*/1);
11674 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11675 complain
, in_decl
);
11677 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11678 return error_mark_node
;
11680 if (!MAYBE_CLASS_TYPE_P (ctx
))
11682 if (complain
& tf_error
)
11683 error ("%qT is not a class, struct, or union type", ctx
);
11684 return error_mark_node
;
11686 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11688 /* Normally, make_typename_type does not require that the CTX
11689 have complete type in order to allow things like:
11691 template <class T> struct S { typename S<T>::X Y; };
11693 But, such constructs have already been resolved by this
11694 point, so here CTX really should have complete type, unless
11695 it's a partial instantiation. */
11696 ctx
= complete_type (ctx
);
11697 if (!COMPLETE_TYPE_P (ctx
))
11699 if (complain
& tf_error
)
11700 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11701 return error_mark_node
;
11705 f
= make_typename_type (ctx
, f
, typename_type
,
11706 (complain
& tf_error
) | tf_keep_type_decl
);
11707 if (f
== error_mark_node
)
11709 if (TREE_CODE (f
) == TYPE_DECL
)
11711 complain
|= tf_ignore_bad_quals
;
11715 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11717 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11719 if (complain
& tf_error
)
11720 error ("%qT resolves to %qT, which is not an enumeration type",
11723 return error_mark_node
;
11725 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11727 if (complain
& tf_error
)
11728 error ("%qT resolves to %qT, which is is not a class type",
11731 return error_mark_node
;
11735 return cp_build_qualified_type_real
11736 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11739 case UNBOUND_CLASS_TEMPLATE
:
11741 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11742 in_decl
, /*entering_scope=*/1);
11743 tree name
= TYPE_IDENTIFIER (t
);
11744 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11746 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11747 return error_mark_node
;
11750 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11751 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11758 ++cp_unevaluated_operand
;
11759 ++c_inhibit_evaluation_warnings
;
11761 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11763 /*integral_constant_expression_p=*/false);
11765 --cp_unevaluated_operand
;
11766 --c_inhibit_evaluation_warnings
;
11768 type
= finish_typeof (type
);
11769 return cp_build_qualified_type_real (type
,
11771 | cp_type_quals (type
),
11775 case DECLTYPE_TYPE
:
11779 ++cp_unevaluated_operand
;
11780 ++c_inhibit_evaluation_warnings
;
11782 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11784 /*integral_constant_expression_p=*/false);
11786 --cp_unevaluated_operand
;
11787 --c_inhibit_evaluation_warnings
;
11789 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11790 type
= lambda_capture_field_type (type
);
11791 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11792 type
= lambda_proxy_type (type
);
11794 type
= finish_decltype_type
11795 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11796 return cp_build_qualified_type_real (type
,
11798 | cp_type_quals (type
),
11802 case UNDERLYING_TYPE
:
11804 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11805 complain
, in_decl
);
11806 return finish_underlying_type (type
);
11809 case TYPE_ARGUMENT_PACK
:
11810 case NONTYPE_ARGUMENT_PACK
:
11812 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11814 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11818 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11820 /* For template nontype argument packs, also substitute into
11822 if (code
== NONTYPE_ARGUMENT_PACK
)
11823 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11841 /* We should use one of the expression tsubsts for these codes. */
11842 gcc_unreachable ();
11845 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11846 return error_mark_node
;
11850 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11851 type of the expression on the left-hand side of the "." or "->"
11855 tsubst_baselink (tree baselink
, tree object_type
,
11856 tree args
, tsubst_flags_t complain
, tree in_decl
)
11859 tree qualifying_scope
;
11862 tree template_args
= 0;
11863 bool template_id_p
= false;
11864 bool qualified
= BASELINK_QUALIFIED_P (baselink
);
11866 /* A baselink indicates a function from a base class. Both the
11867 BASELINK_ACCESS_BINFO and the base class referenced may
11868 indicate bases of the template class, rather than the
11869 instantiated class. In addition, lookups that were not
11870 ambiguous before may be ambiguous now. Therefore, we perform
11871 the lookup again. */
11872 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11873 qualifying_scope
= tsubst (qualifying_scope
, args
,
11874 complain
, in_decl
);
11875 fns
= BASELINK_FUNCTIONS (baselink
);
11876 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11877 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11879 template_id_p
= true;
11880 template_args
= TREE_OPERAND (fns
, 1);
11881 fns
= TREE_OPERAND (fns
, 0);
11883 template_args
= tsubst_template_args (template_args
, args
,
11884 complain
, in_decl
);
11886 name
= DECL_NAME (get_first_fn (fns
));
11887 if (IDENTIFIER_TYPENAME_P (name
))
11888 name
= mangle_conv_op_name_for_type (optype
);
11889 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11891 return error_mark_node
;
11893 /* If lookup found a single function, mark it as used at this
11894 point. (If it lookup found multiple functions the one selected
11895 later by overload resolution will be marked as used at that
11897 if (BASELINK_P (baselink
))
11898 fns
= BASELINK_FUNCTIONS (baselink
);
11899 if (!template_id_p
&& !really_overloaded_fn (fns
))
11900 mark_used (OVL_CURRENT (fns
));
11902 /* Add back the template arguments, if present. */
11903 if (BASELINK_P (baselink
) && template_id_p
)
11904 BASELINK_FUNCTIONS (baselink
)
11905 = build_nt (TEMPLATE_ID_EXPR
,
11906 BASELINK_FUNCTIONS (baselink
),
11908 /* Update the conversion operator type. */
11909 BASELINK_OPTYPE (baselink
) = optype
;
11912 object_type
= current_class_type
;
11915 baselink
= adjust_result_of_qualified_name_lookup (baselink
,
11921 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11922 true if the qualified-id will be a postfix-expression in-and-of
11923 itself; false if more of the postfix-expression follows the
11924 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11928 tsubst_qualified_id (tree qualified_id
, tree args
,
11929 tsubst_flags_t complain
, tree in_decl
,
11930 bool done
, bool address_p
)
11936 tree template_args
;
11938 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11940 /* Figure out what name to look up. */
11941 name
= TREE_OPERAND (qualified_id
, 1);
11942 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11944 is_template
= true;
11945 template_args
= TREE_OPERAND (name
, 1);
11947 template_args
= tsubst_template_args (template_args
, args
,
11948 complain
, in_decl
);
11949 name
= TREE_OPERAND (name
, 0);
11953 is_template
= false;
11954 template_args
= NULL_TREE
;
11957 /* Substitute into the qualifying scope. When there are no ARGS, we
11958 are just trying to simplify a non-dependent expression. In that
11959 case the qualifying scope may be dependent, and, in any case,
11960 substituting will not help. */
11961 scope
= TREE_OPERAND (qualified_id
, 0);
11964 scope
= tsubst (scope
, args
, complain
, in_decl
);
11965 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11970 if (dependent_scope_p (scope
))
11973 expr
= build_min_nt (TEMPLATE_ID_EXPR
, expr
, template_args
);
11974 return build_qualified_name (NULL_TREE
, scope
, expr
,
11975 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11978 if (!BASELINK_P (name
) && !DECL_P (expr
))
11980 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11982 /* A BIT_NOT_EXPR is used to represent a destructor. */
11983 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11985 error ("qualifying type %qT does not match destructor name ~%qT",
11986 scope
, TREE_OPERAND (expr
, 0));
11987 expr
= error_mark_node
;
11990 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11991 /*is_type_p=*/0, false);
11994 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11995 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11996 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11998 if (complain
& tf_error
)
12000 error ("dependent-name %qE is parsed as a non-type, but "
12001 "instantiation yields a type", qualified_id
);
12002 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
12004 return error_mark_node
;
12010 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
12012 /* Remember that there was a reference to this entity. */
12016 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
12018 if (complain
& tf_error
)
12019 qualified_name_lookup_error (scope
,
12020 TREE_OPERAND (qualified_id
, 1),
12021 expr
, input_location
);
12022 return error_mark_node
;
12026 expr
= lookup_template_function (expr
, template_args
);
12028 if (expr
== error_mark_node
&& complain
& tf_error
)
12029 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
12030 expr
, input_location
);
12031 else if (TYPE_P (scope
))
12033 expr
= (adjust_result_of_qualified_name_lookup
12034 (expr
, scope
, current_class_type
));
12035 expr
= (finish_qualified_id_expr
12036 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12037 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12038 /*template_arg_p=*/false));
12041 /* Expressions do not generally have reference type. */
12042 if (TREE_CODE (expr
) != SCOPE_REF
12043 /* However, if we're about to form a pointer-to-member, we just
12044 want the referenced member referenced. */
12045 && TREE_CODE (expr
) != OFFSET_REF
)
12046 expr
= convert_from_reference (expr
);
12051 /* Like tsubst, but deals with expressions. This function just replaces
12052 template parms; to finish processing the resultant expression, use
12053 tsubst_copy_and_build or tsubst_expr. */
12056 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12058 enum tree_code code
;
12061 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12064 code
= TREE_CODE (t
);
12069 r
= retrieve_local_specialization (t
);
12071 if (r
== NULL_TREE
)
12075 /* We get here for a use of 'this' in an NSDMI. */
12076 if (DECL_NAME (t
) == this_identifier
12077 && at_function_scope_p ()
12078 && DECL_CONSTRUCTOR_P (current_function_decl
))
12079 return current_class_ptr
;
12081 /* This can happen for a parameter name used later in a function
12082 declaration (such as in a late-specified return type). Just
12083 make a dummy decl, since it's only used for its type. */
12084 gcc_assert (cp_unevaluated_operand
!= 0);
12085 /* We copy T because want to tsubst the PARM_DECL only,
12086 not the following PARM_DECLs that are chained to T. */
12088 r
= tsubst_decl (c
, args
, complain
);
12089 if (r
== NULL_TREE
)
12090 return error_mark_node
;
12091 /* Give it the template pattern as its context; its true context
12092 hasn't been instantiated yet and this is good enough for
12094 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12097 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12098 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12107 if (DECL_TEMPLATE_PARM_P (t
))
12108 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12109 /* There is no need to substitute into namespace-scope
12111 if (DECL_NAMESPACE_SCOPE_P (t
))
12113 /* If ARGS is NULL, then T is known to be non-dependent. */
12114 if (args
== NULL_TREE
)
12115 return integral_constant_value (t
);
12117 /* Unfortunately, we cannot just call lookup_name here.
12120 template <int I> int f() {
12122 struct S { void g() { E e = a; } };
12125 When we instantiate f<7>::S::g(), say, lookup_name is not
12126 clever enough to find f<7>::a. */
12128 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
12129 /*entering_scope=*/0);
12131 for (v
= TYPE_VALUES (enum_type
);
12133 v
= TREE_CHAIN (v
))
12134 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12135 return TREE_VALUE (v
);
12137 /* We didn't find the name. That should never happen; if
12138 name-lookup found it during preliminary parsing, we
12139 should find it again here during instantiation. */
12140 gcc_unreachable ();
12145 if (DECL_CONTEXT (t
))
12149 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12150 /*entering_scope=*/1);
12151 if (ctx
!= DECL_CONTEXT (t
))
12153 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12156 if (complain
& tf_error
)
12157 error ("using invalid field %qD", t
);
12158 return error_mark_node
;
12167 case FUNCTION_DECL
:
12168 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12169 || local_variable_p (t
))
12170 t
= tsubst (t
, args
, complain
, in_decl
);
12174 case NAMESPACE_DECL
:
12178 /* An OVERLOAD will always be a non-dependent overload set; an
12179 overload set from function scope will just be represented with an
12180 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12181 gcc_assert (!uses_template_parms (t
));
12185 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
12187 case TEMPLATE_DECL
:
12188 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12189 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12190 args
, complain
, in_decl
);
12191 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12192 return tsubst (t
, args
, complain
, in_decl
);
12193 else if (DECL_CLASS_SCOPE_P (t
)
12194 && uses_template_parms (DECL_CONTEXT (t
)))
12196 /* Template template argument like the following example need
12199 template <template <class> class TT> struct C {};
12200 template <class T> struct D {
12201 template <class U> struct E {};
12206 We are processing the template argument `E' in #1 for
12207 the template instantiation #2. Originally, `E' is a
12208 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12209 have to substitute this with one having context `D<int>'. */
12211 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12212 return lookup_field (context
, DECL_NAME(t
), 0, false);
12215 /* Ordinary template template argument. */
12219 case REINTERPRET_CAST_EXPR
:
12220 case CONST_CAST_EXPR
:
12221 case STATIC_CAST_EXPR
:
12222 case DYNAMIC_CAST_EXPR
:
12223 case IMPLICIT_CONV_EXPR
:
12227 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12228 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12231 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12237 ++cp_unevaluated_operand
;
12238 ++c_inhibit_evaluation_warnings
;
12239 /* We only want to compute the number of arguments. */
12240 expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
12241 complain
, in_decl
);
12242 --cp_unevaluated_operand
;
12243 --c_inhibit_evaluation_warnings
;
12245 if (TREE_CODE (expanded
) == TREE_VEC
)
12246 len
= TREE_VEC_LENGTH (expanded
);
12248 if (expanded
== error_mark_node
)
12249 return error_mark_node
;
12250 else if (PACK_EXPANSION_P (expanded
)
12251 || (TREE_CODE (expanded
) == TREE_VEC
12253 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12255 if (TREE_CODE (expanded
) == TREE_VEC
)
12256 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12258 if (TYPE_P (expanded
))
12259 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12260 complain
& tf_error
);
12262 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12263 complain
& tf_error
);
12266 return build_int_cst (size_type_node
, len
);
12272 case TRUTH_NOT_EXPR
:
12275 case UNARY_PLUS_EXPR
: /* Unary + */
12277 case AT_ENCODE_EXPR
:
12281 case REALPART_EXPR
:
12282 case IMAGPART_EXPR
:
12284 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12285 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12287 case COMPONENT_REF
:
12292 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12293 name
= TREE_OPERAND (t
, 1);
12294 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12296 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12297 complain
, in_decl
);
12298 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12300 else if (TREE_CODE (name
) == SCOPE_REF
12301 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12303 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12304 complain
, in_decl
);
12305 name
= TREE_OPERAND (name
, 1);
12306 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12307 complain
, in_decl
);
12308 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12309 name
= build_qualified_name (/*type=*/NULL_TREE
,
12311 /*template_p=*/false);
12313 else if (BASELINK_P (name
))
12314 name
= tsubst_baselink (name
,
12315 non_reference (TREE_TYPE (object
)),
12319 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12320 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12326 case TRUNC_DIV_EXPR
:
12327 case CEIL_DIV_EXPR
:
12328 case FLOOR_DIV_EXPR
:
12329 case ROUND_DIV_EXPR
:
12330 case EXACT_DIV_EXPR
:
12334 case TRUNC_MOD_EXPR
:
12335 case FLOOR_MOD_EXPR
:
12336 case TRUTH_ANDIF_EXPR
:
12337 case TRUTH_ORIF_EXPR
:
12338 case TRUTH_AND_EXPR
:
12339 case TRUTH_OR_EXPR
:
12352 case COMPOUND_EXPR
:
12355 case PREDECREMENT_EXPR
:
12356 case PREINCREMENT_EXPR
:
12357 case POSTDECREMENT_EXPR
:
12358 case POSTINCREMENT_EXPR
:
12360 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12361 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12364 return build_qualified_name (/*type=*/NULL_TREE
,
12365 tsubst_copy (TREE_OPERAND (t
, 0),
12366 args
, complain
, in_decl
),
12367 tsubst_copy (TREE_OPERAND (t
, 1),
12368 args
, complain
, in_decl
),
12369 QUALIFIED_NAME_IS_TEMPLATE (t
));
12374 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12375 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12376 NULL_TREE
, NULL_TREE
);
12380 int n
= VL_EXP_OPERAND_LENGTH (t
);
12381 tree result
= build_vl_exp (CALL_EXPR
, n
);
12383 for (i
= 0; i
< n
; i
++)
12384 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12385 complain
, in_decl
);
12391 case PSEUDO_DTOR_EXPR
:
12394 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12395 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12396 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12397 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12404 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12405 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12406 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12407 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12414 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12415 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12416 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12417 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12421 case TEMPLATE_ID_EXPR
:
12423 /* Substituted template arguments */
12424 tree fn
= TREE_OPERAND (t
, 0);
12425 tree targs
= TREE_OPERAND (t
, 1);
12427 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12429 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12431 return lookup_template_function (fn
, targs
);
12436 tree purpose
, value
, chain
;
12438 if (t
== void_list_node
)
12441 purpose
= TREE_PURPOSE (t
);
12443 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12444 value
= TREE_VALUE (t
);
12446 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12447 chain
= TREE_CHAIN (t
);
12448 if (chain
&& chain
!= void_type_node
)
12449 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12450 if (purpose
== TREE_PURPOSE (t
)
12451 && value
== TREE_VALUE (t
)
12452 && chain
== TREE_CHAIN (t
))
12454 return tree_cons (purpose
, value
, chain
);
12459 case ENUMERAL_TYPE
:
12461 case TEMPLATE_TYPE_PARM
:
12462 case TEMPLATE_TEMPLATE_PARM
:
12463 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12464 case TEMPLATE_PARM_INDEX
:
12466 case REFERENCE_TYPE
:
12468 case FUNCTION_TYPE
:
12471 case TYPENAME_TYPE
:
12472 case UNBOUND_CLASS_TEMPLATE
:
12474 case DECLTYPE_TYPE
:
12476 return tsubst (t
, args
, complain
, in_decl
);
12478 case IDENTIFIER_NODE
:
12479 if (IDENTIFIER_TYPENAME_P (t
))
12481 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12482 return mangle_conv_op_name_for_type (new_type
);
12488 /* This is handled by tsubst_copy_and_build. */
12489 gcc_unreachable ();
12492 return build_x_va_arg (EXPR_LOCATION (t
),
12493 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12495 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12497 case CLEANUP_POINT_EXPR
:
12498 /* We shouldn't have built any of these during initial template
12499 generation. Instead, they should be built during instantiation
12500 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12501 gcc_unreachable ();
12505 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12506 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12507 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12508 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12509 mark_used (TREE_OPERAND (r
, 1));
12512 case EXPR_PACK_EXPANSION
:
12513 error ("invalid use of pack expansion expression");
12514 return error_mark_node
;
12516 case NONTYPE_ARGUMENT_PACK
:
12517 error ("use %<...%> to expand argument pack");
12518 return error_mark_node
;
12525 /* Instantiate any typedefs in the type. */
12526 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12527 r
= fold_convert (type
, t
);
12528 gcc_assert (TREE_CODE (r
) == code
);
12533 /* These can sometimes show up in a partial instantiation, but never
12534 involve template parms. */
12535 gcc_assert (!uses_template_parms (t
));
12539 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12540 gcc_checking_assert (false);
12545 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12548 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12551 tree new_clauses
= NULL
, nc
, oc
;
12553 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12555 nc
= copy_node (oc
);
12556 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12559 switch (OMP_CLAUSE_CODE (nc
))
12561 case OMP_CLAUSE_LASTPRIVATE
:
12562 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12564 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12565 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12566 in_decl
, /*integral_constant_expression_p=*/false);
12567 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12568 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12571 case OMP_CLAUSE_PRIVATE
:
12572 case OMP_CLAUSE_SHARED
:
12573 case OMP_CLAUSE_FIRSTPRIVATE
:
12574 case OMP_CLAUSE_REDUCTION
:
12575 case OMP_CLAUSE_COPYIN
:
12576 case OMP_CLAUSE_COPYPRIVATE
:
12577 case OMP_CLAUSE_IF
:
12578 case OMP_CLAUSE_NUM_THREADS
:
12579 case OMP_CLAUSE_SCHEDULE
:
12580 case OMP_CLAUSE_COLLAPSE
:
12581 case OMP_CLAUSE_FINAL
:
12582 OMP_CLAUSE_OPERAND (nc
, 0)
12583 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12584 in_decl
, /*integral_constant_expression_p=*/false);
12586 case OMP_CLAUSE_NOWAIT
:
12587 case OMP_CLAUSE_ORDERED
:
12588 case OMP_CLAUSE_DEFAULT
:
12589 case OMP_CLAUSE_UNTIED
:
12590 case OMP_CLAUSE_MERGEABLE
:
12593 gcc_unreachable ();
12597 return finish_omp_clauses (nreverse (new_clauses
));
12600 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12603 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12606 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12608 tree purpose
, value
, chain
;
12613 if (TREE_CODE (t
) != TREE_LIST
)
12614 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12615 /*function_p=*/false,
12616 /*integral_constant_expression_p=*/false);
12618 if (t
== void_list_node
)
12621 purpose
= TREE_PURPOSE (t
);
12623 purpose
= RECUR (purpose
);
12624 value
= TREE_VALUE (t
);
12627 if (TREE_CODE (value
) != LABEL_DECL
)
12628 value
= RECUR (value
);
12631 value
= lookup_label (DECL_NAME (value
));
12632 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12633 TREE_USED (value
) = 1;
12636 chain
= TREE_CHAIN (t
);
12637 if (chain
&& chain
!= void_type_node
)
12638 chain
= RECUR (chain
);
12639 return tree_cons (purpose
, value
, chain
);
12643 /* Substitute one OMP_FOR iterator. */
12646 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12647 tree condv
, tree incrv
, tree
*clauses
,
12648 tree args
, tsubst_flags_t complain
, tree in_decl
,
12649 bool integral_constant_expression_p
)
12651 #define RECUR(NODE) \
12652 tsubst_expr ((NODE), args, complain, in_decl, \
12653 integral_constant_expression_p)
12654 tree decl
, init
, cond
, incr
, auto_node
;
12656 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12657 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12658 decl
= RECUR (TREE_OPERAND (init
, 0));
12659 init
= TREE_OPERAND (init
, 1);
12660 auto_node
= type_uses_auto (TREE_TYPE (decl
));
12661 if (auto_node
&& init
)
12663 tree init_expr
= init
;
12664 if (TREE_CODE (init_expr
) == DECL_EXPR
)
12665 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
12666 init_expr
= RECUR (init_expr
);
12668 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
12670 gcc_assert (!type_dependent_expression_p (decl
));
12672 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12674 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12675 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12676 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12677 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12678 RECUR (TREE_OPERAND (incr
, 1)),
12681 incr
= RECUR (incr
);
12682 TREE_VEC_ELT (declv
, i
) = decl
;
12683 TREE_VEC_ELT (initv
, i
) = init
;
12684 TREE_VEC_ELT (condv
, i
) = cond
;
12685 TREE_VEC_ELT (incrv
, i
) = incr
;
12689 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
12692 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12694 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12695 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12696 && OMP_CLAUSE_DECL (c
) == decl
)
12698 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12699 && OMP_CLAUSE_DECL (c
) == decl
)
12700 error ("iteration variable %qD should not be firstprivate", decl
);
12701 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12702 && OMP_CLAUSE_DECL (c
) == decl
)
12703 error ("iteration variable %qD should not be reduction", decl
);
12707 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12708 OMP_CLAUSE_DECL (c
) = decl
;
12709 c
= finish_omp_clauses (c
);
12712 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12717 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12718 if (COMPARISON_CLASS_P (cond
))
12719 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12720 RECUR (TREE_OPERAND (cond
, 0)),
12721 RECUR (TREE_OPERAND (cond
, 1)));
12723 cond
= RECUR (cond
);
12724 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12725 switch (TREE_CODE (incr
))
12727 case PREINCREMENT_EXPR
:
12728 case PREDECREMENT_EXPR
:
12729 case POSTINCREMENT_EXPR
:
12730 case POSTDECREMENT_EXPR
:
12731 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12732 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12735 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12736 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12738 tree rhs
= TREE_OPERAND (incr
, 1);
12739 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12740 RECUR (TREE_OPERAND (incr
, 0)),
12741 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12742 RECUR (TREE_OPERAND (rhs
, 0)),
12743 RECUR (TREE_OPERAND (rhs
, 1))));
12746 incr
= RECUR (incr
);
12749 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12750 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12752 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12753 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12754 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12755 TREE_TYPE (decl
), lhs
,
12756 RECUR (TREE_OPERAND (incr
, 2))));
12758 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12759 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12760 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12762 tree rhs
= TREE_OPERAND (incr
, 2);
12763 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12764 RECUR (TREE_OPERAND (incr
, 0)),
12765 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12766 RECUR (TREE_OPERAND (rhs
, 0)),
12767 RECUR (TREE_OPERAND (rhs
, 1))));
12770 incr
= RECUR (incr
);
12773 incr
= RECUR (incr
);
12777 TREE_VEC_ELT (declv
, i
) = decl
;
12778 TREE_VEC_ELT (initv
, i
) = init
;
12779 TREE_VEC_ELT (condv
, i
) = cond
;
12780 TREE_VEC_ELT (incrv
, i
) = incr
;
12784 /* Like tsubst_copy for expressions, etc. but also does semantic
12788 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12789 bool integral_constant_expression_p
)
12791 #define RECUR(NODE) \
12792 tsubst_expr ((NODE), args, complain, in_decl, \
12793 integral_constant_expression_p)
12797 if (t
== NULL_TREE
|| t
== error_mark_node
)
12800 if (EXPR_HAS_LOCATION (t
))
12801 input_location
= EXPR_LOCATION (t
);
12802 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12803 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12805 switch (TREE_CODE (t
))
12807 case STATEMENT_LIST
:
12809 tree_stmt_iterator i
;
12810 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12811 RECUR (tsi_stmt (i
));
12815 case CTOR_INITIALIZER
:
12816 finish_mem_initializers (tsubst_initializer_list
12817 (TREE_OPERAND (t
, 0), args
));
12821 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12825 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12826 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12827 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12829 finish_expr_stmt (tmp
);
12833 do_using_directive (USING_STMT_NAMESPACE (t
));
12838 tree decl
, pattern_decl
;
12841 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12842 if (TREE_CODE (decl
) == LABEL_DECL
)
12843 finish_label_decl (DECL_NAME (decl
));
12844 else if (TREE_CODE (decl
) == USING_DECL
)
12846 tree scope
= USING_DECL_SCOPE (decl
);
12847 tree name
= DECL_NAME (decl
);
12850 scope
= tsubst (scope
, args
, complain
, in_decl
);
12851 decl
= lookup_qualified_name (scope
, name
,
12852 /*is_type_p=*/false,
12853 /*complain=*/false);
12854 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12855 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12857 do_local_using_decl (decl
, scope
, name
);
12861 init
= DECL_INITIAL (decl
);
12862 decl
= tsubst (decl
, args
, complain
, in_decl
);
12863 if (decl
!= error_mark_node
)
12865 /* By marking the declaration as instantiated, we avoid
12866 trying to instantiate it. Since instantiate_decl can't
12867 handle local variables, and since we've already done
12868 all that needs to be done, that's the right thing to
12870 if (TREE_CODE (decl
) == VAR_DECL
)
12871 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12872 if (TREE_CODE (decl
) == VAR_DECL
12873 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12874 /* Anonymous aggregates are a special case. */
12875 finish_anon_union (decl
);
12876 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12878 DECL_CONTEXT (decl
) = current_function_decl
;
12879 insert_capture_proxy (decl
);
12883 int const_init
= false;
12884 maybe_push_decl (decl
);
12885 if (TREE_CODE (decl
) == VAR_DECL
12886 && DECL_PRETTY_FUNCTION_P (decl
))
12888 /* For __PRETTY_FUNCTION__ we have to adjust the
12890 const char *const name
12891 = cxx_printable_name (current_function_decl
, 2);
12892 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12896 tree t
= RECUR (init
);
12900 /* If we had an initializer but it
12901 instantiated to nothing,
12902 value-initialize the object. This will
12903 only occur when the initializer was a
12904 pack expansion where the parameter packs
12905 used in that expansion were of length
12907 init
= build_value_init (TREE_TYPE (decl
),
12909 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12910 init
= get_target_expr_sfinae (init
, complain
);
12916 if (TREE_CODE (decl
) == VAR_DECL
)
12917 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12919 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12924 /* A DECL_EXPR can also be used as an expression, in the condition
12925 clause of an if/for/while construct. */
12930 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12931 RECUR (FOR_INIT_STMT (t
));
12932 finish_for_init_stmt (stmt
);
12933 tmp
= RECUR (FOR_COND (t
));
12934 finish_for_cond (tmp
, stmt
);
12935 tmp
= RECUR (FOR_EXPR (t
));
12936 finish_for_expr (tmp
, stmt
);
12937 RECUR (FOR_BODY (t
));
12938 finish_for_stmt (stmt
);
12941 case RANGE_FOR_STMT
:
12944 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12945 decl
= RANGE_FOR_DECL (t
);
12946 decl
= tsubst (decl
, args
, complain
, in_decl
);
12947 maybe_push_decl (decl
);
12948 expr
= RECUR (RANGE_FOR_EXPR (t
));
12949 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12950 RECUR (RANGE_FOR_BODY (t
));
12951 finish_for_stmt (stmt
);
12956 stmt
= begin_while_stmt ();
12957 tmp
= RECUR (WHILE_COND (t
));
12958 finish_while_stmt_cond (tmp
, stmt
);
12959 RECUR (WHILE_BODY (t
));
12960 finish_while_stmt (stmt
);
12964 stmt
= begin_do_stmt ();
12965 RECUR (DO_BODY (t
));
12966 finish_do_body (stmt
);
12967 tmp
= RECUR (DO_COND (t
));
12968 finish_do_stmt (tmp
, stmt
);
12972 stmt
= begin_if_stmt ();
12973 tmp
= RECUR (IF_COND (t
));
12974 finish_if_stmt_cond (tmp
, stmt
);
12975 RECUR (THEN_CLAUSE (t
));
12976 finish_then_clause (stmt
);
12978 if (ELSE_CLAUSE (t
))
12980 begin_else_clause (stmt
);
12981 RECUR (ELSE_CLAUSE (t
));
12982 finish_else_clause (stmt
);
12985 finish_if_stmt (stmt
);
12989 if (BIND_EXPR_BODY_BLOCK (t
))
12990 stmt
= begin_function_body ();
12992 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12993 ? BCS_TRY_BLOCK
: 0);
12995 RECUR (BIND_EXPR_BODY (t
));
12997 if (BIND_EXPR_BODY_BLOCK (t
))
12998 finish_function_body (stmt
);
13000 finish_compound_stmt (stmt
);
13004 finish_break_stmt ();
13007 case CONTINUE_STMT
:
13008 finish_continue_stmt ();
13012 stmt
= begin_switch_stmt ();
13013 tmp
= RECUR (SWITCH_STMT_COND (t
));
13014 finish_switch_cond (tmp
, stmt
);
13015 RECUR (SWITCH_STMT_BODY (t
));
13016 finish_switch_stmt (stmt
);
13019 case CASE_LABEL_EXPR
:
13020 finish_case_label (EXPR_LOCATION (t
),
13021 RECUR (CASE_LOW (t
)),
13022 RECUR (CASE_HIGH (t
)));
13027 tree decl
= LABEL_EXPR_LABEL (t
);
13030 label
= finish_label_stmt (DECL_NAME (decl
));
13031 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
13032 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13037 tmp
= GOTO_DESTINATION (t
);
13038 if (TREE_CODE (tmp
) != LABEL_DECL
)
13039 /* Computed goto's must be tsubst'd into. On the other hand,
13040 non-computed gotos must not be; the identifier in question
13041 will have no binding. */
13044 tmp
= DECL_NAME (tmp
);
13045 finish_goto_stmt (tmp
);
13049 tmp
= finish_asm_stmt
13050 (ASM_VOLATILE_P (t
),
13051 RECUR (ASM_STRING (t
)),
13052 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13053 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13054 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13055 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13057 tree asm_expr
= tmp
;
13058 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13059 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13060 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13067 stmt
= begin_try_block ();
13068 RECUR (TRY_STMTS (t
));
13069 finish_cleanup_try_block (stmt
);
13070 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13074 tree compound_stmt
= NULL_TREE
;
13076 if (FN_TRY_BLOCK_P (t
))
13077 stmt
= begin_function_try_block (&compound_stmt
);
13079 stmt
= begin_try_block ();
13081 RECUR (TRY_STMTS (t
));
13083 if (FN_TRY_BLOCK_P (t
))
13084 finish_function_try_block (stmt
);
13086 finish_try_block (stmt
);
13088 RECUR (TRY_HANDLERS (t
));
13089 if (FN_TRY_BLOCK_P (t
))
13090 finish_function_handler_sequence (stmt
, compound_stmt
);
13092 finish_handler_sequence (stmt
);
13098 tree decl
= HANDLER_PARMS (t
);
13102 decl
= tsubst (decl
, args
, complain
, in_decl
);
13103 /* Prevent instantiate_decl from trying to instantiate
13104 this variable. We've already done all that needs to be
13106 if (decl
!= error_mark_node
)
13107 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13109 stmt
= begin_handler ();
13110 finish_handler_parms (decl
, stmt
);
13111 RECUR (HANDLER_BODY (t
));
13112 finish_handler (stmt
);
13117 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13120 case STATIC_ASSERT
:
13124 ++c_inhibit_evaluation_warnings
;
13126 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13129 /*integral_constant_expression_p=*/true);
13130 --c_inhibit_evaluation_warnings
;
13132 finish_static_assert (condition
,
13133 STATIC_ASSERT_MESSAGE (t
),
13134 STATIC_ASSERT_SOURCE_LOCATION (t
),
13135 /*member_p=*/false);
13140 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
13141 args
, complain
, in_decl
);
13142 stmt
= begin_omp_parallel ();
13143 RECUR (OMP_PARALLEL_BODY (t
));
13144 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13145 = OMP_PARALLEL_COMBINED (t
);
13149 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
13150 args
, complain
, in_decl
);
13151 stmt
= begin_omp_task ();
13152 RECUR (OMP_TASK_BODY (t
));
13153 finish_omp_task (tmp
, stmt
);
13158 tree clauses
, body
, pre_body
;
13159 tree declv
, initv
, condv
, incrv
;
13162 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
13163 args
, complain
, in_decl
);
13164 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13165 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13166 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13167 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13169 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13170 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13171 &clauses
, args
, complain
, in_decl
,
13172 integral_constant_expression_p
);
13174 stmt
= begin_omp_structured_block ();
13176 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
13177 if (TREE_VEC_ELT (initv
, i
) == NULL
13178 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
13179 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
13180 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
13182 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
13183 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
13184 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
13188 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
13189 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
13190 gcc_assert (init
!= NULL
);
13191 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
13192 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
13194 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
13197 pre_body
= push_stmt_list ();
13198 RECUR (OMP_FOR_PRE_BODY (t
));
13199 pre_body
= pop_stmt_list (pre_body
);
13201 body
= push_stmt_list ();
13202 RECUR (OMP_FOR_BODY (t
));
13203 body
= pop_stmt_list (body
);
13205 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13206 body
, pre_body
, clauses
);
13208 add_stmt (finish_omp_structured_block (stmt
));
13214 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13215 stmt
= push_stmt_list ();
13216 RECUR (OMP_BODY (t
));
13217 stmt
= pop_stmt_list (stmt
);
13220 OMP_BODY (t
) = stmt
;
13221 OMP_CLAUSES (t
) = tmp
;
13229 stmt
= push_stmt_list ();
13230 RECUR (OMP_BODY (t
));
13231 stmt
= pop_stmt_list (stmt
);
13234 OMP_BODY (t
) = stmt
;
13239 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13240 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13242 tree op1
= TREE_OPERAND (t
, 1);
13243 tree rhs1
= NULL_TREE
;
13245 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13247 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13248 op1
= TREE_OPERAND (op1
, 1);
13250 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13251 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13252 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13253 NULL_TREE
, NULL_TREE
, rhs1
);
13257 tree op1
= TREE_OPERAND (t
, 1);
13258 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13259 tree rhs1
= NULL_TREE
;
13260 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13261 enum tree_code opcode
= NOP_EXPR
;
13262 if (code
== OMP_ATOMIC_READ
)
13264 v
= RECUR (TREE_OPERAND (op1
, 0));
13265 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13267 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13268 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13270 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13271 v
= RECUR (TREE_OPERAND (op1
, 0));
13272 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13273 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13275 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13276 op11
= TREE_OPERAND (op11
, 1);
13278 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13279 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13280 opcode
= TREE_CODE (op11
);
13285 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13286 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13288 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13292 case TRANSACTION_EXPR
:
13295 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13296 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13298 if (TRANSACTION_EXPR_IS_STMT (t
))
13300 tree body
= TRANSACTION_EXPR_BODY (t
);
13301 tree noex
= NULL_TREE
;
13302 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13304 noex
= MUST_NOT_THROW_COND (body
);
13305 if (noex
== NULL_TREE
)
13306 noex
= boolean_true_node
;
13307 body
= TREE_OPERAND (body
, 0);
13309 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13311 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13315 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13316 RECUR (TRANSACTION_EXPR_BODY (t
)),
13323 case MUST_NOT_THROW_EXPR
:
13324 return build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13325 RECUR (MUST_NOT_THROW_COND (t
)));
13327 case EXPR_PACK_EXPANSION
:
13328 error ("invalid use of pack expansion expression");
13329 return error_mark_node
;
13331 case NONTYPE_ARGUMENT_PACK
:
13332 error ("use %<...%> to expand argument pack");
13333 return error_mark_node
;
13336 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13338 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13339 /*function_p=*/false,
13340 integral_constant_expression_p
);
13347 /* T is a postfix-expression that is not being used in a function
13348 call. Return the substituted version of T. */
13351 tsubst_non_call_postfix_expression (tree t
, tree args
,
13352 tsubst_flags_t complain
,
13355 if (TREE_CODE (t
) == SCOPE_REF
)
13356 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13357 /*done=*/false, /*address_p=*/false);
13359 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13360 /*function_p=*/false,
13361 /*integral_constant_expression_p=*/false);
13366 /* Like tsubst but deals with expressions and performs semantic
13367 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13370 tsubst_copy_and_build (tree t
,
13372 tsubst_flags_t complain
,
13375 bool integral_constant_expression_p
)
13377 #define RECUR(NODE) \
13378 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13379 /*function_p=*/false, \
13380 integral_constant_expression_p)
13384 if (t
== NULL_TREE
|| t
== error_mark_node
)
13387 switch (TREE_CODE (t
))
13391 /* Fall through. */
13392 case IDENTIFIER_NODE
:
13396 bool non_integral_constant_expression_p
;
13397 const char *error_msg
;
13399 if (IDENTIFIER_TYPENAME_P (t
))
13401 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13402 t
= mangle_conv_op_name_for_type (new_type
);
13405 /* Look up the name. */
13406 decl
= lookup_name (t
);
13408 /* By convention, expressions use ERROR_MARK_NODE to indicate
13409 failure, not NULL_TREE. */
13410 if (decl
== NULL_TREE
)
13411 decl
= error_mark_node
;
13413 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13415 integral_constant_expression_p
,
13416 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13417 &non_integral_constant_expression_p
,
13418 /*template_p=*/false,
13420 /*address_p=*/false,
13421 /*template_arg_p=*/false,
13426 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13428 if (complain
& tf_error
)
13429 unqualified_name_lookup_error (decl
);
13430 decl
= error_mark_node
;
13435 case TEMPLATE_ID_EXPR
:
13438 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13439 tree targs
= TREE_OPERAND (t
, 1);
13442 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13444 if (TREE_CODE (templ
) == COMPONENT_REF
)
13446 object
= TREE_OPERAND (templ
, 0);
13447 templ
= TREE_OPERAND (templ
, 1);
13450 object
= NULL_TREE
;
13451 templ
= lookup_template_function (templ
, targs
);
13454 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13455 object
, templ
, NULL_TREE
);
13457 return baselink_for_fns (templ
);
13462 tree r
= RECUR (TREE_OPERAND (t
, 0));
13464 if (REFERENCE_REF_P (t
))
13466 /* A type conversion to reference type will be enclosed in
13467 such an indirect ref, but the substitution of the cast
13468 will have also added such an indirect ref. */
13469 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13470 r
= convert_from_reference (r
);
13473 r
= build_x_indirect_ref (input_location
, r
, RO_UNARY_STAR
, complain
);
13479 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13480 RECUR (TREE_OPERAND (t
, 0)));
13482 case IMPLICIT_CONV_EXPR
:
13484 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13485 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13486 int flags
= LOOKUP_IMPLICIT
;
13487 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13488 flags
= LOOKUP_NORMAL
;
13489 return perform_implicit_conversion_flags (type
, expr
, complain
,
13496 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13497 RECUR (TREE_OPERAND (t
, 0)));
13500 case REINTERPRET_CAST_EXPR
:
13501 case CONST_CAST_EXPR
:
13502 case DYNAMIC_CAST_EXPR
:
13503 case STATIC_CAST_EXPR
:
13506 tree op
, r
= NULL_TREE
;
13508 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13509 if (integral_constant_expression_p
13510 && !cast_valid_in_integral_constant_expression_p (type
))
13512 if (complain
& tf_error
)
13513 error ("a cast to a type other than an integral or "
13514 "enumeration type cannot appear in a constant-expression");
13515 return error_mark_node
;
13518 op
= RECUR (TREE_OPERAND (t
, 0));
13520 ++c_inhibit_evaluation_warnings
;
13521 switch (TREE_CODE (t
))
13524 r
= build_functional_cast (type
, op
, complain
);
13526 case REINTERPRET_CAST_EXPR
:
13527 r
= build_reinterpret_cast (type
, op
, complain
);
13529 case CONST_CAST_EXPR
:
13530 r
= build_const_cast (type
, op
, complain
);
13532 case DYNAMIC_CAST_EXPR
:
13533 r
= build_dynamic_cast (type
, op
, complain
);
13535 case STATIC_CAST_EXPR
:
13536 r
= build_static_cast (type
, op
, complain
);
13539 gcc_unreachable ();
13541 --c_inhibit_evaluation_warnings
;
13546 case POSTDECREMENT_EXPR
:
13547 case POSTINCREMENT_EXPR
:
13548 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13549 args
, complain
, in_decl
);
13550 return build_x_unary_op (input_location
, TREE_CODE (t
), op1
, complain
);
13552 case PREDECREMENT_EXPR
:
13553 case PREINCREMENT_EXPR
:
13557 case TRUTH_NOT_EXPR
:
13558 case UNARY_PLUS_EXPR
: /* Unary + */
13559 case REALPART_EXPR
:
13560 case IMAGPART_EXPR
:
13561 return build_x_unary_op (input_location
, TREE_CODE (t
),
13562 RECUR (TREE_OPERAND (t
, 0)), complain
);
13564 case FIX_TRUNC_EXPR
:
13565 return cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13569 op1
= TREE_OPERAND (t
, 0);
13570 if (TREE_CODE (op1
) == LABEL_DECL
)
13571 return finish_label_address_expr (DECL_NAME (op1
),
13572 EXPR_LOCATION (op1
));
13573 if (TREE_CODE (op1
) == SCOPE_REF
)
13574 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13575 /*done=*/true, /*address_p=*/true);
13577 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13579 return build_x_unary_op (input_location
, ADDR_EXPR
, op1
, complain
);
13584 case TRUNC_DIV_EXPR
:
13585 case CEIL_DIV_EXPR
:
13586 case FLOOR_DIV_EXPR
:
13587 case ROUND_DIV_EXPR
:
13588 case EXACT_DIV_EXPR
:
13592 case TRUNC_MOD_EXPR
:
13593 case FLOOR_MOD_EXPR
:
13594 case TRUTH_ANDIF_EXPR
:
13595 case TRUTH_ORIF_EXPR
:
13596 case TRUTH_AND_EXPR
:
13597 case TRUTH_OR_EXPR
:
13613 tree r
= build_x_binary_op
13614 (input_location
, TREE_CODE (t
),
13615 RECUR (TREE_OPERAND (t
, 0)),
13616 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13618 : TREE_CODE (TREE_OPERAND (t
, 0))),
13619 RECUR (TREE_OPERAND (t
, 1)),
13620 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13622 : TREE_CODE (TREE_OPERAND (t
, 1))),
13625 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13626 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13631 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13632 /*address_p=*/false);
13634 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13635 args
, complain
, in_decl
);
13636 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
13639 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13640 return tsubst_copy (t
, args
, complain
, in_decl
);
13644 op1
= TREE_OPERAND (t
, 0);
13647 /* When there are no ARGS, we are trying to evaluate a
13648 non-dependent expression from the parser. Trying to do
13649 the substitutions may not work. */
13651 op1
= TREE_TYPE (op1
);
13655 ++cp_unevaluated_operand
;
13656 ++c_inhibit_evaluation_warnings
;
13657 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13658 /*function_p=*/false,
13659 /*integral_constant_expression_p=*/false);
13660 --cp_unevaluated_operand
;
13661 --c_inhibit_evaluation_warnings
;
13664 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13665 complain
& tf_error
);
13667 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13668 complain
& tf_error
);
13670 case AT_ENCODE_EXPR
:
13672 op1
= TREE_OPERAND (t
, 0);
13673 ++cp_unevaluated_operand
;
13674 ++c_inhibit_evaluation_warnings
;
13675 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13676 /*function_p=*/false,
13677 /*integral_constant_expression_p=*/false);
13678 --cp_unevaluated_operand
;
13679 --c_inhibit_evaluation_warnings
;
13680 return objc_build_encode_expr (op1
);
13683 case NOEXCEPT_EXPR
:
13684 op1
= TREE_OPERAND (t
, 0);
13685 ++cp_unevaluated_operand
;
13686 ++c_inhibit_evaluation_warnings
;
13687 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13688 /*function_p=*/false,
13689 /*integral_constant_expression_p=*/false);
13690 --cp_unevaluated_operand
;
13691 --c_inhibit_evaluation_warnings
;
13692 return finish_noexcept_expr (op1
, complain
);
13696 tree r
= build_x_modify_expr
13697 (RECUR (TREE_OPERAND (t
, 0)),
13698 TREE_CODE (TREE_OPERAND (t
, 1)),
13699 RECUR (TREE_OPERAND (t
, 2)),
13701 /* TREE_NO_WARNING must be set if either the expression was
13702 parenthesized or it uses an operator such as >>= rather
13703 than plain assignment. In the former case, it was already
13704 set and must be copied. In the latter case,
13705 build_x_modify_expr sets it and it must not be reset
13707 if (TREE_NO_WARNING (t
))
13708 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13713 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13714 args
, complain
, in_decl
);
13715 /* Remember that there was a reference to this entity. */
13718 return build_x_arrow (input_location
, op1
, complain
);
13722 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13723 tree init
= RECUR (TREE_OPERAND (t
, 3));
13724 VEC(tree
,gc
) *placement_vec
;
13725 VEC(tree
,gc
) *init_vec
;
13728 if (placement
== NULL_TREE
)
13729 placement_vec
= NULL
;
13732 placement_vec
= make_tree_vector ();
13733 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13734 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
13737 /* If there was an initializer in the original tree, but it
13738 instantiated to an empty list, then we should pass a
13739 non-NULL empty vector to tell build_new that it was an
13740 empty initializer() rather than no initializer. This can
13741 only happen when the initializer is a pack expansion whose
13742 parameter packs are of length zero. */
13743 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13747 init_vec
= make_tree_vector ();
13748 if (init
== void_zero_node
)
13749 gcc_assert (init_vec
!= NULL
);
13752 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13753 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
13757 ret
= build_new (&placement_vec
,
13758 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13759 RECUR (TREE_OPERAND (t
, 2)),
13761 NEW_EXPR_USE_GLOBAL (t
),
13764 if (placement_vec
!= NULL
)
13765 release_tree_vector (placement_vec
);
13766 if (init_vec
!= NULL
)
13767 release_tree_vector (init_vec
);
13773 return delete_sanity
13774 (RECUR (TREE_OPERAND (t
, 0)),
13775 RECUR (TREE_OPERAND (t
, 1)),
13776 DELETE_EXPR_USE_VEC (t
),
13777 DELETE_EXPR_USE_GLOBAL (t
),
13780 case COMPOUND_EXPR
:
13781 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
13782 RECUR (TREE_OPERAND (t
, 1)),
13788 VEC(tree
,gc
) *call_args
;
13789 unsigned int nargs
, i
;
13794 function
= CALL_EXPR_FN (t
);
13795 /* When we parsed the expression, we determined whether or
13796 not Koenig lookup should be performed. */
13797 koenig_p
= KOENIG_LOOKUP_P (t
);
13798 if (TREE_CODE (function
) == SCOPE_REF
)
13800 qualified_p
= true;
13801 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13803 /*address_p=*/false);
13805 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13807 /* Do nothing; calling tsubst_copy_and_build on an identifier
13808 would incorrectly perform unqualified lookup again.
13810 Note that we can also have an IDENTIFIER_NODE if the earlier
13811 unqualified lookup found a member function; in that case
13812 koenig_p will be false and we do want to do the lookup
13813 again to find the instantiated member function.
13815 FIXME but doing that causes c++/15272, so we need to stop
13816 using IDENTIFIER_NODE in that situation. */
13817 qualified_p
= false;
13821 if (TREE_CODE (function
) == COMPONENT_REF
)
13823 tree op
= TREE_OPERAND (function
, 1);
13825 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13826 || (BASELINK_P (op
)
13827 && BASELINK_QUALIFIED_P (op
)));
13830 qualified_p
= false;
13832 function
= tsubst_copy_and_build (function
, args
, complain
,
13835 integral_constant_expression_p
);
13837 if (BASELINK_P (function
))
13838 qualified_p
= true;
13841 nargs
= call_expr_nargs (t
);
13842 call_args
= make_tree_vector ();
13843 for (i
= 0; i
< nargs
; ++i
)
13845 tree arg
= CALL_EXPR_ARG (t
, i
);
13847 if (!PACK_EXPANSION_P (arg
))
13848 VEC_safe_push (tree
, gc
, call_args
,
13849 RECUR (CALL_EXPR_ARG (t
, i
)));
13852 /* Expand the pack expansion and push each entry onto
13854 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13855 if (TREE_CODE (arg
) == TREE_VEC
)
13857 unsigned int len
, j
;
13859 len
= TREE_VEC_LENGTH (arg
);
13860 for (j
= 0; j
< len
; ++j
)
13862 tree value
= TREE_VEC_ELT (arg
, j
);
13863 if (value
!= NULL_TREE
)
13864 value
= convert_from_reference (value
);
13865 VEC_safe_push (tree
, gc
, call_args
, value
);
13870 /* A partial substitution. Add one entry. */
13871 VEC_safe_push (tree
, gc
, call_args
, arg
);
13876 /* We do not perform argument-dependent lookup if normal
13877 lookup finds a non-function, in accordance with the
13878 expected resolution of DR 218. */
13880 && ((is_overloaded_fn (function
)
13881 /* If lookup found a member function, the Koenig lookup is
13882 not appropriate, even if an unqualified-name was used
13883 to denote the function. */
13884 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13885 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13886 /* Only do this when substitution turns a dependent call
13887 into a non-dependent call. */
13888 && type_dependent_expression_p_push (t
)
13889 && !any_type_dependent_arguments_p (call_args
))
13890 function
= perform_koenig_lookup (function
, call_args
, false,
13893 if (TREE_CODE (function
) == IDENTIFIER_NODE
13894 && !any_type_dependent_arguments_p (call_args
))
13896 if (koenig_p
&& (complain
& tf_warning_or_error
))
13898 /* For backwards compatibility and good diagnostics, try
13899 the unqualified lookup again if we aren't in SFINAE
13901 tree unq
= (tsubst_copy_and_build
13902 (function
, args
, complain
, in_decl
, true,
13903 integral_constant_expression_p
));
13904 if (unq
== error_mark_node
)
13905 return error_mark_node
;
13907 if (unq
!= function
)
13910 if (TREE_CODE (fn
) == INDIRECT_REF
)
13911 fn
= TREE_OPERAND (fn
, 0);
13912 if (TREE_CODE (fn
) == COMPONENT_REF
)
13913 fn
= TREE_OPERAND (fn
, 1);
13914 if (is_overloaded_fn (fn
))
13915 fn
= get_first_fn (fn
);
13916 permerror (EXPR_LOC_OR_HERE (t
),
13917 "%qD was not declared in this scope, "
13918 "and no declarations were found by "
13919 "argument-dependent lookup at the point "
13920 "of instantiation", function
);
13922 /* Can't say anything more. */;
13923 else if (DECL_CLASS_SCOPE_P (fn
))
13925 inform (EXPR_LOC_OR_HERE (t
),
13926 "declarations in dependent base %qT are "
13927 "not found by unqualified lookup",
13928 DECL_CLASS_CONTEXT (fn
));
13929 if (current_class_ptr
)
13930 inform (EXPR_LOC_OR_HERE (t
),
13931 "use %<this->%D%> instead", function
);
13933 inform (EXPR_LOC_OR_HERE (t
),
13934 "use %<%T::%D%> instead",
13935 current_class_name
, function
);
13938 inform (0, "%q+D declared here, later in the "
13939 "translation unit", fn
);
13943 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13945 unqualified_name_lookup_error (function
);
13946 release_tree_vector (call_args
);
13947 return error_mark_node
;
13951 /* Remember that there was a reference to this entity. */
13952 if (DECL_P (function
))
13953 mark_used (function
);
13955 if (TREE_CODE (function
) == OFFSET_REF
)
13956 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
13957 else if (TREE_CODE (function
) == COMPONENT_REF
)
13959 tree instance
= TREE_OPERAND (function
, 0);
13960 tree fn
= TREE_OPERAND (function
, 1);
13962 if (processing_template_decl
13963 && (type_dependent_expression_p (instance
)
13964 || (!BASELINK_P (fn
)
13965 && TREE_CODE (fn
) != FIELD_DECL
)
13966 || type_dependent_expression_p (fn
)
13967 || any_type_dependent_arguments_p (call_args
)))
13968 ret
= build_nt_call_vec (function
, call_args
);
13969 else if (!BASELINK_P (fn
))
13970 ret
= finish_call_expr (function
, &call_args
,
13971 /*disallow_virtual=*/false,
13972 /*koenig_p=*/false,
13975 ret
= (build_new_method_call
13977 &call_args
, NULL_TREE
,
13978 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
13983 ret
= finish_call_expr (function
, &call_args
,
13984 /*disallow_virtual=*/qualified_p
,
13988 release_tree_vector (call_args
);
13995 tree cond
= RECUR (TREE_OPERAND (t
, 0));
13998 if (TREE_CODE (cond
) == INTEGER_CST
)
14000 if (integer_zerop (cond
))
14002 ++c_inhibit_evaluation_warnings
;
14003 exp1
= RECUR (TREE_OPERAND (t
, 1));
14004 --c_inhibit_evaluation_warnings
;
14005 exp2
= RECUR (TREE_OPERAND (t
, 2));
14009 exp1
= RECUR (TREE_OPERAND (t
, 1));
14010 ++c_inhibit_evaluation_warnings
;
14011 exp2
= RECUR (TREE_OPERAND (t
, 2));
14012 --c_inhibit_evaluation_warnings
;
14017 exp1
= RECUR (TREE_OPERAND (t
, 1));
14018 exp2
= RECUR (TREE_OPERAND (t
, 2));
14021 return build_x_conditional_expr (cond
, exp1
, exp2
, complain
);
14024 case PSEUDO_DTOR_EXPR
:
14025 return finish_pseudo_destructor_expr
14026 (RECUR (TREE_OPERAND (t
, 0)),
14027 RECUR (TREE_OPERAND (t
, 1)),
14028 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
14032 tree purpose
, value
, chain
;
14034 if (t
== void_list_node
)
14037 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
14038 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
14040 /* We have pack expansions, so expand those and
14041 create a new list out of it. */
14042 tree purposevec
= NULL_TREE
;
14043 tree valuevec
= NULL_TREE
;
14047 /* Expand the argument expressions. */
14048 if (TREE_PURPOSE (t
))
14049 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
14050 complain
, in_decl
);
14051 if (TREE_VALUE (t
))
14052 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
14053 complain
, in_decl
);
14055 /* Build the rest of the list. */
14056 chain
= TREE_CHAIN (t
);
14057 if (chain
&& chain
!= void_type_node
)
14058 chain
= RECUR (chain
);
14060 /* Determine the number of arguments. */
14061 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
14063 len
= TREE_VEC_LENGTH (purposevec
);
14064 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
14066 else if (TREE_CODE (valuevec
) == TREE_VEC
)
14067 len
= TREE_VEC_LENGTH (valuevec
);
14070 /* Since we only performed a partial substitution into
14071 the argument pack, we only return a single list
14073 if (purposevec
== TREE_PURPOSE (t
)
14074 && valuevec
== TREE_VALUE (t
)
14075 && chain
== TREE_CHAIN (t
))
14078 return tree_cons (purposevec
, valuevec
, chain
);
14081 /* Convert the argument vectors into a TREE_LIST */
14085 /* Grab the Ith values. */
14087 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14090 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14093 /* Build the list (backwards). */
14094 chain
= tree_cons (purpose
, value
, chain
);
14100 purpose
= TREE_PURPOSE (t
);
14102 purpose
= RECUR (purpose
);
14103 value
= TREE_VALUE (t
);
14105 value
= RECUR (value
);
14106 chain
= TREE_CHAIN (t
);
14107 if (chain
&& chain
!= void_type_node
)
14108 chain
= RECUR (chain
);
14109 if (purpose
== TREE_PURPOSE (t
)
14110 && value
== TREE_VALUE (t
)
14111 && chain
== TREE_CHAIN (t
))
14113 return tree_cons (purpose
, value
, chain
);
14116 case COMPONENT_REF
:
14122 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14123 args
, complain
, in_decl
);
14124 /* Remember that there was a reference to this entity. */
14125 if (DECL_P (object
))
14126 mark_used (object
);
14127 object_type
= TREE_TYPE (object
);
14129 member
= TREE_OPERAND (t
, 1);
14130 if (BASELINK_P (member
))
14131 member
= tsubst_baselink (member
,
14132 non_reference (TREE_TYPE (object
)),
14133 args
, complain
, in_decl
);
14135 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14136 if (member
== error_mark_node
)
14137 return error_mark_node
;
14139 if (type_dependent_expression_p (object
))
14140 /* We can't do much here. */;
14141 else if (!CLASS_TYPE_P (object_type
))
14143 if (SCALAR_TYPE_P (object_type
))
14145 tree s
= NULL_TREE
;
14146 tree dtor
= member
;
14148 if (TREE_CODE (dtor
) == SCOPE_REF
)
14150 s
= TREE_OPERAND (dtor
, 0);
14151 dtor
= TREE_OPERAND (dtor
, 1);
14153 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14155 dtor
= TREE_OPERAND (dtor
, 0);
14157 return finish_pseudo_destructor_expr (object
, s
, dtor
);
14161 else if (TREE_CODE (member
) == SCOPE_REF
14162 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14164 /* Lookup the template functions now that we know what the
14166 tree scope
= TREE_OPERAND (member
, 0);
14167 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14168 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14169 member
= lookup_qualified_name (scope
, tmpl
,
14170 /*is_type_p=*/false,
14171 /*complain=*/false);
14172 if (BASELINK_P (member
))
14174 BASELINK_FUNCTIONS (member
)
14175 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14177 member
= (adjust_result_of_qualified_name_lookup
14178 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14183 qualified_name_lookup_error (scope
, tmpl
, member
,
14185 return error_mark_node
;
14188 else if (TREE_CODE (member
) == SCOPE_REF
14189 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14190 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14192 if (complain
& tf_error
)
14194 if (TYPE_P (TREE_OPERAND (member
, 0)))
14195 error ("%qT is not a class or namespace",
14196 TREE_OPERAND (member
, 0));
14198 error ("%qD is not a class or namespace",
14199 TREE_OPERAND (member
, 0));
14201 return error_mark_node
;
14203 else if (TREE_CODE (member
) == FIELD_DECL
)
14204 return finish_non_static_data_member (member
, object
, NULL_TREE
);
14206 return finish_class_member_access_expr (object
, member
,
14207 /*template_p=*/false,
14213 (RECUR (TREE_OPERAND (t
, 0)));
14217 VEC(constructor_elt
,gc
) *n
;
14218 constructor_elt
*ce
;
14219 unsigned HOST_WIDE_INT idx
;
14220 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14221 bool process_index_p
;
14223 bool need_copy_p
= false;
14226 if (type
== error_mark_node
)
14227 return error_mark_node
;
14229 /* digest_init will do the wrong thing if we let it. */
14230 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14233 /* We do not want to process the index of aggregate
14234 initializers as they are identifier nodes which will be
14235 looked up by digest_init. */
14236 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14238 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
14239 newlen
= VEC_length (constructor_elt
, n
);
14240 FOR_EACH_VEC_ELT (constructor_elt
, n
, idx
, ce
)
14242 if (ce
->index
&& process_index_p
)
14243 ce
->index
= RECUR (ce
->index
);
14245 if (PACK_EXPANSION_P (ce
->value
))
14247 /* Substitute into the pack expansion. */
14248 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14251 if (ce
->value
== error_mark_node
14252 || PACK_EXPANSION_P (ce
->value
))
14254 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14255 /* Just move the argument into place. */
14256 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14259 /* Update the length of the final CONSTRUCTOR
14260 arguments vector, and note that we will need to
14262 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14263 need_copy_p
= true;
14267 ce
->value
= RECUR (ce
->value
);
14272 VEC(constructor_elt
,gc
) *old_n
= n
;
14274 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
14275 FOR_EACH_VEC_ELT (constructor_elt
, old_n
, idx
, ce
)
14277 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14279 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14280 for (i
= 0; i
< len
; ++i
)
14281 CONSTRUCTOR_APPEND_ELT (n
, 0,
14282 TREE_VEC_ELT (ce
->value
, i
));
14285 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14289 r
= build_constructor (init_list_type_node
, n
);
14290 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14292 if (TREE_HAS_CONSTRUCTOR (t
))
14293 return finish_compound_literal (type
, r
, complain
);
14295 TREE_TYPE (r
) = type
;
14301 tree operand_0
= TREE_OPERAND (t
, 0);
14302 if (TYPE_P (operand_0
))
14304 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14305 return get_typeid (operand_0
);
14309 operand_0
= RECUR (operand_0
);
14310 return build_typeid (operand_0
);
14321 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14323 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14324 /* If the original type was a reference, we'll be wrapped in
14325 the appropriate INDIRECT_REF. */
14326 r
= convert_from_reference (r
);
14331 return build_x_va_arg (EXPR_LOCATION (t
),
14332 RECUR (TREE_OPERAND (t
, 0)),
14333 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
14335 case OFFSETOF_EXPR
:
14336 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
14340 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14341 complain
, in_decl
);
14343 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14345 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14347 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
14352 tree old_stmt_expr
= cur_stmt_expr
;
14353 tree stmt_expr
= begin_stmt_expr ();
14355 cur_stmt_expr
= stmt_expr
;
14356 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14357 integral_constant_expression_p
);
14358 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14359 cur_stmt_expr
= old_stmt_expr
;
14361 /* If the resulting list of expression statement is empty,
14362 fold it further into void_zero_node. */
14363 if (empty_expr_stmt_p (stmt_expr
))
14364 stmt_expr
= void_zero_node
;
14370 t
= tsubst_copy (t
, args
, complain
, in_decl
);
14371 /* As in finish_id_expression, we resolve enumeration constants
14372 to their underlying values. */
14373 if (TREE_CODE (t
) == CONST_DECL
&& !processing_template_decl
)
14375 used_types_insert (TREE_TYPE (t
));
14376 return DECL_INITIAL (t
);
14382 tree r
= build_lambda_expr ();
14384 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14385 LAMBDA_EXPR_CLOSURE (r
) = type
;
14386 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14388 LAMBDA_EXPR_LOCATION (r
)
14389 = LAMBDA_EXPR_LOCATION (t
);
14390 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14391 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14392 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14393 LAMBDA_EXPR_DISCRIMINATOR (r
)
14394 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14395 LAMBDA_EXPR_EXTRA_SCOPE (r
)
14396 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t
));
14397 LAMBDA_EXPR_RETURN_TYPE (r
)
14398 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14400 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14401 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14403 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14404 determine_visibility (TYPE_NAME (type
));
14405 /* Now that we know visibility, instantiate the type so we have a
14406 declaration of the op() for later calls to lambda_function. */
14407 complete_type (type
);
14409 /* The capture list refers to closure members, so this needs to
14410 wait until after we finish instantiating the type. */
14411 LAMBDA_EXPR_CAPTURE_LIST (r
)
14412 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
14414 return build_lambda_object (r
);
14418 /* We can get here for a constant initializer of non-dependent type.
14419 FIXME stop folding in cp_parser_initializer_clause. */
14420 gcc_assert (TREE_CONSTANT (t
));
14422 tree r
= get_target_expr (RECUR (TARGET_EXPR_INITIAL (t
)));
14423 TREE_CONSTANT (r
) = true;
14427 case TRANSACTION_EXPR
:
14428 return tsubst_expr(t
, args
, complain
, in_decl
,
14429 integral_constant_expression_p
);
14432 /* Handle Objective-C++ constructs, if appropriate. */
14435 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14436 in_decl
, /*function_p=*/false);
14440 return tsubst_copy (t
, args
, complain
, in_decl
);
14446 /* Verify that the instantiated ARGS are valid. For type arguments,
14447 make sure that the type's linkage is ok. For non-type arguments,
14448 make sure they are constants if they are integral or enumerations.
14449 Emit an error under control of COMPLAIN, and return TRUE on error. */
14452 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14454 if (ARGUMENT_PACK_P (t
))
14456 tree vec
= ARGUMENT_PACK_ARGS (t
);
14457 int len
= TREE_VEC_LENGTH (vec
);
14458 bool result
= false;
14461 for (i
= 0; i
< len
; ++i
)
14462 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14466 else if (TYPE_P (t
))
14468 /* [basic.link]: A name with no linkage (notably, the name
14469 of a class or enumeration declared in a local scope)
14470 shall not be used to declare an entity with linkage.
14471 This implies that names with no linkage cannot be used as
14474 DR 757 relaxes this restriction for C++0x. */
14475 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14476 : no_linkage_check (t
, /*relaxed_p=*/false));
14480 /* DR 488 makes use of a type with no linkage cause
14481 type deduction to fail. */
14482 if (complain
& tf_error
)
14484 if (TYPE_ANONYMOUS_P (nt
))
14485 error ("%qT is/uses anonymous type", t
);
14487 error ("template argument for %qD uses local type %qT",
14492 /* In order to avoid all sorts of complications, we do not
14493 allow variably-modified types as template arguments. */
14494 else if (variably_modified_type_p (t
, NULL_TREE
))
14496 if (complain
& tf_error
)
14497 error ("%qT is a variably modified type", t
);
14501 /* A non-type argument of integral or enumerated type must be a
14503 else if (TREE_TYPE (t
)
14504 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14505 && !TREE_CONSTANT (t
))
14507 if (complain
& tf_error
)
14508 error ("integral expression %qE is not constant", t
);
14515 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14517 int ix
, len
= DECL_NTPARMS (tmpl
);
14518 bool result
= false;
14520 for (ix
= 0; ix
!= len
; ix
++)
14522 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14525 if (result
&& (complain
& tf_error
))
14526 error (" trying to instantiate %qD", tmpl
);
14530 /* In C++0x, it's possible to have a function template whose type depends
14531 on itself recursively. This is most obvious with decltype, but can also
14532 occur with enumeration scope (c++/48969). So we need to catch infinite
14533 recursion and reject the substitution at deduction time; this function
14534 will return error_mark_node for any repeated substitution.
14536 This also catches excessive recursion such as when f<N> depends on
14537 f<N-1> across all integers, and returns error_mark_node for all the
14538 substitutions back up to the initial one.
14540 This is, of course, not reentrant. */
14543 deduction_tsubst_fntype (tree fn
, tree targs
, tsubst_flags_t complain
)
14545 static bool excessive_deduction_depth
;
14546 static int deduction_depth
;
14547 struct pending_template
*old_last_pend
= last_pending_template
;
14548 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14550 tree fntype
= TREE_TYPE (fn
);
14554 if (excessive_deduction_depth
)
14555 return error_mark_node
;
14557 tinst
= build_tree_list (fn
, targs
);
14558 if (!push_tinst_level (tinst
))
14560 excessive_deduction_depth
= true;
14562 return error_mark_node
;
14565 input_location
= DECL_SOURCE_LOCATION (fn
);
14567 push_deduction_access_scope (fn
);
14568 r
= tsubst (fntype
, targs
, complain
, NULL_TREE
);
14569 pop_deduction_access_scope (fn
);
14572 if (excessive_deduction_depth
)
14574 r
= error_mark_node
;
14575 if (deduction_depth
== 0)
14576 /* Reset once we're all the way out. */
14577 excessive_deduction_depth
= false;
14580 pop_tinst_level ();
14581 /* We can't free this if a pending_template entry or last_error_tinst_level
14582 is pointing at it. */
14583 if (last_pending_template
== old_last_pend
14584 && last_error_tinst_level
== old_error_tinst
)
14589 /* Instantiate the indicated variable or function template TMPL with
14590 the template arguments in TARG_PTR. */
14593 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14595 tree targ_ptr
= orig_args
;
14600 if (tmpl
== error_mark_node
)
14601 return error_mark_node
;
14603 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14605 /* If this function is a clone, handle it specially. */
14606 if (DECL_CLONED_FUNCTION_P (tmpl
))
14611 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14612 DECL_CLONED_FUNCTION. */
14613 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14614 targ_ptr
, complain
);
14615 if (spec
== error_mark_node
)
14616 return error_mark_node
;
14618 /* Look for the clone. */
14619 FOR_EACH_CLONE (clone
, spec
)
14620 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14622 /* We should always have found the clone by now. */
14623 gcc_unreachable ();
14627 /* Check to see if we already have this specialization. */
14628 gen_tmpl
= most_general_template (tmpl
);
14629 if (tmpl
!= gen_tmpl
)
14630 /* The TMPL is a partial instantiation. To get a full set of
14631 arguments we must add the arguments used to perform the
14632 partial instantiation. */
14633 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14636 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14637 but it doesn't seem to be on the hot path. */
14638 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14640 gcc_assert (tmpl
== gen_tmpl
14641 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14643 || fndecl
== NULL_TREE
);
14645 if (spec
!= NULL_TREE
)
14648 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14650 return error_mark_node
;
14652 /* We are building a FUNCTION_DECL, during which the access of its
14653 parameters and return types have to be checked. However this
14654 FUNCTION_DECL which is the desired context for access checking
14655 is not built yet. We solve this chicken-and-egg problem by
14656 deferring all checks until we have the FUNCTION_DECL. */
14657 push_deferring_access_checks (dk_deferred
);
14659 /* Instantiation of the function happens in the context of the function
14660 template, not the context of the overload resolution we're doing. */
14661 push_to_top_level ();
14662 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14664 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14665 complain
, gen_tmpl
);
14666 push_nested_class (ctx
);
14668 /* Substitute template parameters to obtain the specialization. */
14669 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14670 targ_ptr
, complain
, gen_tmpl
);
14671 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14672 pop_nested_class ();
14673 pop_from_top_level ();
14675 if (fndecl
== error_mark_node
)
14676 return error_mark_node
;
14678 /* Now we know the specialization, compute access previously
14680 push_access_scope (fndecl
);
14682 /* Some typedefs referenced from within the template code need to be access
14683 checked at template instantiation time, i.e now. These types were
14684 added to the template at parsing time. Let's get those and perfom
14685 the acces checks then. */
14686 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl
), targ_ptr
);
14687 perform_deferred_access_checks ();
14688 pop_access_scope (fndecl
);
14689 pop_deferring_access_checks ();
14691 /* The DECL_TI_TEMPLATE should always be the immediate parent
14692 template, not the most general template. */
14693 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14695 /* If we've just instantiated the main entry point for a function,
14696 instantiate all the alternate entry points as well. We do this
14697 by cloning the instantiation of the main entry point, not by
14698 instantiating the template clones. */
14699 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14700 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14705 /* Wrapper for instantiate_template_1. */
14708 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14711 timevar_push (TV_TEMPLATE_INST
);
14712 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14713 timevar_pop (TV_TEMPLATE_INST
);
14717 /* We're going to do deduction substitution on the type of TMPL, a function
14718 template. In C++11 mode, push into that access scope. In C++03 mode,
14719 disable access checking. */
14722 push_deduction_access_scope (tree tmpl
)
14724 if (cxx_dialect
>= cxx0x
)
14726 int ptd
= processing_template_decl
;
14727 push_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
14728 /* Preserve processing_template_decl across push_to_top_level. */
14729 if (ptd
&& !processing_template_decl
)
14730 ++processing_template_decl
;
14733 push_deferring_access_checks (dk_no_check
);
14736 /* And pop back out. */
14739 pop_deduction_access_scope (tree tmpl
)
14741 if (cxx_dialect
>= cxx0x
)
14742 pop_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
14744 pop_deferring_access_checks ();
14747 /* PARM is a template parameter pack for FN. Returns true iff
14748 PARM is used in a deducible way in the argument list of FN. */
14751 pack_deducible_p (tree parm
, tree fn
)
14753 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14754 for (; t
; t
= TREE_CHAIN (t
))
14756 tree type
= TREE_VALUE (t
);
14758 if (!PACK_EXPANSION_P (type
))
14760 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14761 packs
; packs
= TREE_CHAIN (packs
))
14762 if (TREE_VALUE (packs
) == parm
)
14764 /* The template parameter pack is used in a function parameter
14765 pack. If this is the end of the parameter list, the
14766 template parameter pack is deducible. */
14767 if (TREE_CHAIN (t
) == void_list_node
)
14770 /* Otherwise, not. Well, it could be deduced from
14771 a non-pack parameter, but doing so would end up with
14772 a deduction mismatch, so don't bother. */
14776 /* The template parameter pack isn't used in any function parameter
14777 packs, but it might be used deeper, e.g. tuple<Args...>. */
14781 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14782 NARGS elements of the arguments that are being used when calling
14783 it. TARGS is a vector into which the deduced template arguments
14786 Return zero for success, 2 for an incomplete match that doesn't resolve
14787 all the types, and 1 for complete failure. An error message will be
14788 printed only for an incomplete match.
14790 If FN is a conversion operator, or we are trying to produce a specific
14791 specialization, RETURN_TYPE is the return type desired.
14793 The EXPLICIT_TARGS are explicit template arguments provided via a
14796 The parameter STRICT is one of:
14799 We are deducing arguments for a function call, as in
14800 [temp.deduct.call].
14803 We are deducing arguments for a conversion function, as in
14804 [temp.deduct.conv].
14807 We are deducing arguments when doing an explicit instantiation
14808 as in [temp.explicit], when determining an explicit specialization
14809 as in [temp.expl.spec], or when taking the address of a function
14810 template, as in [temp.deduct.funcaddr]. */
14813 fn_type_unification (tree fn
,
14814 tree explicit_targs
,
14817 unsigned int nargs
,
14819 unification_kind_t strict
,
14827 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14829 fntype
= TREE_TYPE (fn
);
14830 if (explicit_targs
)
14834 The specified template arguments must match the template
14835 parameters in kind (i.e., type, nontype, template), and there
14836 must not be more arguments than there are parameters;
14837 otherwise type deduction fails.
14839 Nontype arguments must match the types of the corresponding
14840 nontype template parameters, or must be convertible to the
14841 types of the corresponding nontype parameters as specified in
14842 _temp.arg.nontype_, otherwise type deduction fails.
14844 All references in the function type of the function template
14845 to the corresponding template parameters are replaced by the
14846 specified template argument values. If a substitution in a
14847 template parameter or in the function type of the function
14848 template results in an invalid type, type deduction fails. */
14849 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14850 int i
, len
= TREE_VEC_LENGTH (tparms
);
14851 tree converted_args
;
14852 bool incomplete
= false;
14854 if (explicit_targs
== error_mark_node
)
14855 return unify_invalid (explain_p
);
14858 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14860 ? tf_warning_or_error
14862 /*require_all_args=*/false,
14863 /*use_default_args=*/false));
14864 if (converted_args
== error_mark_node
)
14867 /* Substitute the explicit args into the function type. This is
14868 necessary so that, for instance, explicitly declared function
14869 arguments can match null pointed constants. If we were given
14870 an incomplete set of explicit args, we must not do semantic
14871 processing during substitution as we could create partial
14873 for (i
= 0; i
< len
; i
++)
14875 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14876 bool parameter_pack
= false;
14877 tree targ
= TREE_VEC_ELT (converted_args
, i
);
14879 /* Dig out the actual parm. */
14880 if (TREE_CODE (parm
) == TYPE_DECL
14881 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14883 parm
= TREE_TYPE (parm
);
14884 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14886 else if (TREE_CODE (parm
) == PARM_DECL
)
14888 parm
= DECL_INITIAL (parm
);
14889 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14892 if (!parameter_pack
&& targ
== NULL_TREE
)
14893 /* No explicit argument for this template parameter. */
14896 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
14898 /* Mark the argument pack as "incomplete". We could
14899 still deduce more arguments during unification.
14900 We remove this mark in type_unification_real. */
14903 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
14904 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
14905 = ARGUMENT_PACK_ARGS (targ
);
14908 /* We have some incomplete argument packs. */
14913 processing_template_decl
+= incomplete
;
14914 fntype
= deduction_tsubst_fntype (fn
, converted_args
,
14916 ? tf_warning_or_error
14917 : tf_none
) | tf_partial
);
14918 processing_template_decl
-= incomplete
;
14920 if (fntype
== error_mark_node
)
14923 /* Place the explicitly specified arguments in TARGS. */
14924 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
14925 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
14928 /* Never do unification on the 'this' parameter. */
14929 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
14935 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
14936 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
14937 new_args
[0] = return_type
;
14938 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
14943 /* We allow incomplete unification without an error message here
14944 because the standard doesn't seem to explicitly prohibit it. Our
14945 callers must be ready to deal with unification failures in any
14947 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
14948 targs
, parms
, args
, nargs
, /*subr=*/0,
14949 strict
, flags
, explain_p
);
14951 /* Now that we have bindings for all of the template arguments,
14952 ensure that the arguments deduced for the template template
14953 parameters have compatible template parameter lists. We cannot
14954 check this property before we have deduced all template
14955 arguments, because the template parameter types of a template
14956 template parameter might depend on prior template parameters
14957 deduced after the template template parameter. The following
14958 ill-formed example illustrates this issue:
14960 template<typename T, template<T> class C> void f(C<5>, T);
14962 template<int N> struct X {};
14965 f(X<5>(), 5l); // error: template argument deduction fails
14968 The template parameter list of 'C' depends on the template type
14969 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14970 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14971 time that we deduce 'C'. */
14973 && !template_template_parm_bindings_ok_p
14974 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
14975 return unify_inconsistent_template_template_parameters (explain_p
);
14978 /* All is well so far. Now, check:
14982 When all template arguments have been deduced, all uses of
14983 template parameters in nondeduced contexts are replaced with
14984 the corresponding deduced argument values. If the
14985 substitution results in an invalid type, as described above,
14986 type deduction fails. */
14988 tree substed
= deduction_tsubst_fntype (fn
, targs
,
14990 ? tf_warning_or_error
14992 if (substed
== error_mark_node
)
14995 /* If we're looking for an exact match, check that what we got
14996 is indeed an exact match. It might not be if some template
14997 parameters are used in non-deduced contexts. */
14998 if (strict
== DEDUCE_EXACT
)
15003 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
15005 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
15006 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
15007 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
15008 return unify_type_mismatch (explain_p
, args
[i
],
15009 TREE_VALUE (sarg
));
15016 /* Adjust types before performing type deduction, as described in
15017 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
15018 sections are symmetric. PARM is the type of a function parameter
15019 or the return type of the conversion function. ARG is the type of
15020 the argument passed to the call, or the type of the value
15021 initialized with the result of the conversion function.
15022 ARG_EXPR is the original argument expression, which may be null. */
15025 maybe_adjust_types_for_deduction (unification_kind_t strict
,
15039 /* Swap PARM and ARG throughout the remainder of this
15040 function; the handling is precisely symmetric since PARM
15041 will initialize ARG rather than vice versa. */
15049 /* Core issue #873: Do the DR606 thing (see below) for these cases,
15050 too, but here handle it by stripping the reference from PARM
15051 rather than by adding it to ARG. */
15052 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15053 && TYPE_REF_IS_RVALUE (*parm
)
15054 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15055 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15056 && TREE_CODE (*arg
) == REFERENCE_TYPE
15057 && !TYPE_REF_IS_RVALUE (*arg
))
15058 *parm
= TREE_TYPE (*parm
);
15059 /* Nothing else to do in this case. */
15063 gcc_unreachable ();
15066 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15068 /* [temp.deduct.call]
15070 If P is not a reference type:
15072 --If A is an array type, the pointer type produced by the
15073 array-to-pointer standard conversion (_conv.array_) is
15074 used in place of A for type deduction; otherwise,
15076 --If A is a function type, the pointer type produced by
15077 the function-to-pointer standard conversion
15078 (_conv.func_) is used in place of A for type deduction;
15081 --If A is a cv-qualified type, the top level
15082 cv-qualifiers of A's type are ignored for type
15084 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15085 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15086 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15087 *arg
= build_pointer_type (*arg
);
15089 *arg
= TYPE_MAIN_VARIANT (*arg
);
15092 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15093 of the form T&&, where T is a template parameter, and the argument
15094 is an lvalue, T is deduced as A& */
15095 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15096 && TYPE_REF_IS_RVALUE (*parm
)
15097 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15098 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15099 && (arg_expr
? real_lvalue_p (arg_expr
)
15100 /* try_one_overload doesn't provide an arg_expr, but
15101 functions are always lvalues. */
15102 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15103 *arg
= build_reference_type (*arg
);
15105 /* [temp.deduct.call]
15107 If P is a cv-qualified type, the top level cv-qualifiers
15108 of P's type are ignored for type deduction. If P is a
15109 reference type, the type referred to by P is used for
15111 *parm
= TYPE_MAIN_VARIANT (*parm
);
15112 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15114 *parm
= TREE_TYPE (*parm
);
15115 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15118 /* DR 322. For conversion deduction, remove a reference type on parm
15119 too (which has been swapped into ARG). */
15120 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15121 *arg
= TREE_TYPE (*arg
);
15126 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15127 template which does contain any deducible template parameters; check if
15128 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15129 unify_one_argument. */
15132 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15133 int flags
, bool explain_p
)
15138 type
= TREE_TYPE (arg
);
15142 if (same_type_p (parm
, type
))
15143 return unify_success (explain_p
);
15145 if (strict
== DEDUCE_CONV
)
15147 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
,
15148 explain_p
? tf_warning_or_error
: tf_none
))
15149 return unify_success (explain_p
);
15151 else if (strict
!= DEDUCE_EXACT
)
15153 if (can_convert_arg (parm
, type
,
15154 TYPE_P (arg
) ? NULL_TREE
: arg
,
15155 flags
, explain_p
? tf_warning_or_error
: tf_none
))
15156 return unify_success (explain_p
);
15159 if (strict
== DEDUCE_EXACT
)
15160 return unify_type_mismatch (explain_p
, parm
, arg
);
15162 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15165 /* Subroutine of type_unification_real and unify_pack_expansion to
15166 handle unification of a single P/A pair. Parameters are as
15167 for those functions. */
15170 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15171 int subr
, unification_kind_t strict
, int flags
,
15174 tree arg_expr
= NULL_TREE
;
15177 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15178 return unify_invalid (explain_p
);
15179 if (arg
== unknown_type_node
)
15180 /* We can't deduce anything from this, but we might get all the
15181 template args from other function args. */
15182 return unify_success (explain_p
);
15184 /* FIXME uses_deducible_template_parms */
15185 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15186 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15192 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15193 | UNIFY_ALLOW_MORE_CV_QUAL
15194 | UNIFY_ALLOW_DERIVED
);
15198 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15202 arg_strict
= UNIFY_ALLOW_NONE
;
15206 gcc_unreachable ();
15209 /* We only do these transformations if this is the top-level
15210 parameter_type_list in a call or declaration matching; in other
15211 situations (nested function declarators, template argument lists) we
15212 won't be comparing a type to an expression, and we don't do any type
15218 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15219 if (type_unknown_p (arg
))
15221 /* [temp.deduct.type] A template-argument can be
15222 deduced from a pointer to function or pointer
15223 to member function argument if the set of
15224 overloaded functions does not contain function
15225 templates and at most one of a set of
15226 overloaded functions provides a unique
15229 if (resolve_overloaded_unification
15230 (tparms
, targs
, parm
, arg
, strict
,
15231 arg_strict
, explain_p
))
15232 return unify_success (explain_p
);
15233 return unify_overload_resolution_failure (explain_p
, arg
);
15237 arg
= unlowered_expr_type (arg
);
15238 if (arg
== error_mark_node
)
15239 return unify_invalid (explain_p
);
15243 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15246 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15247 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15249 /* For deduction from an init-list we need the actual list. */
15250 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15252 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15255 /* Most parms like fn_type_unification.
15257 If SUBR is 1, we're being called recursively (to unify the
15258 arguments of a function or method parameter of a function
15262 type_unification_real (tree tparms
,
15266 unsigned int xnargs
,
15268 unification_kind_t strict
,
15274 int ntparms
= TREE_VEC_LENGTH (tparms
);
15275 int saw_undeduced
= 0;
15278 unsigned int nargs
;
15281 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15282 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15283 gcc_assert (ntparms
> 0);
15285 /* Reset the number of non-defaulted template arguments contained
15287 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15295 while (parms
&& parms
!= void_list_node
15298 parm
= TREE_VALUE (parms
);
15300 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15301 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15302 /* For a function parameter pack that occurs at the end of the
15303 parameter-declaration-list, the type A of each remaining
15304 argument of the call is compared with the type P of the
15305 declarator-id of the function parameter pack. */
15308 parms
= TREE_CHAIN (parms
);
15310 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15311 /* For a function parameter pack that does not occur at the
15312 end of the parameter-declaration-list, the type of the
15313 parameter pack is a non-deduced context. */
15319 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15325 && parms
!= void_list_node
15326 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15328 /* Unify the remaining arguments with the pack expansion type. */
15330 tree parmvec
= make_tree_vec (1);
15332 /* Allocate a TREE_VEC and copy in all of the arguments */
15333 argvec
= make_tree_vec (nargs
- ia
);
15334 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15335 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15337 /* Copy the parameter into parmvec. */
15338 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15339 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15340 /*subr=*/subr
, explain_p
))
15343 /* Advance to the end of the list of parameters. */
15344 parms
= TREE_CHAIN (parms
);
15347 /* Fail if we've reached the end of the parm list, and more args
15348 are present, and the parm list isn't variadic. */
15349 if (ia
< nargs
&& parms
== void_list_node
)
15350 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15351 /* Fail if parms are left and they don't have default values. */
15352 if (parms
&& parms
!= void_list_node
15353 && TREE_PURPOSE (parms
) == NULL_TREE
)
15355 unsigned int count
= nargs
;
15357 while (p
&& p
!= void_list_node
)
15360 p
= TREE_CHAIN (p
);
15362 return unify_too_few_arguments (explain_p
, ia
, count
);
15367 tsubst_flags_t complain
= (explain_p
15368 ? tf_warning_or_error
15371 /* Check to see if we need another pass before we start clearing
15372 ARGUMENT_PACK_INCOMPLETE_P. */
15373 for (i
= 0; i
< ntparms
; i
++)
15375 tree targ
= TREE_VEC_ELT (targs
, i
);
15376 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15378 if (targ
|| tparm
== error_mark_node
)
15380 tparm
= TREE_VALUE (tparm
);
15382 /* If this is an undeduced nontype parameter that depends on
15383 a type parameter, try another pass; its type may have been
15384 deduced from a later argument than the one from which
15385 this parameter can be deduced. */
15386 if (TREE_CODE (tparm
) == PARM_DECL
15387 && uses_template_parms (TREE_TYPE (tparm
))
15388 && !saw_undeduced
++)
15392 for (i
= 0; i
< ntparms
; i
++)
15394 tree targ
= TREE_VEC_ELT (targs
, i
);
15395 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15397 /* Clear the "incomplete" flags on all argument packs now so that
15398 substituting them into later default arguments works. */
15399 if (targ
&& ARGUMENT_PACK_P (targ
))
15401 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15402 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15405 if (targ
|| tparm
== error_mark_node
)
15407 tparm
= TREE_VALUE (tparm
);
15409 /* Core issue #226 (C++0x) [temp.deduct]:
15411 If a template argument has not been deduced, its
15412 default template argument, if any, is used.
15414 When we are in C++98 mode, TREE_PURPOSE will either
15415 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15416 to explicitly check cxx_dialect here. */
15417 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15419 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15420 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15421 location_t save_loc
= input_location
;
15423 input_location
= DECL_SOURCE_LOCATION (parm
);
15424 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15425 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15427 input_location
= save_loc
;
15428 if (arg
== error_mark_node
)
15432 TREE_VEC_ELT (targs
, i
) = arg
;
15433 /* The position of the first default template argument,
15434 is also the number of non-defaulted arguments in TARGS.
15436 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15437 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15442 /* If the type parameter is a parameter pack, then it will
15443 be deduced to an empty parameter pack. */
15444 if (template_parameter_pack_p (tparm
))
15448 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15450 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15451 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15452 TREE_CONSTANT (arg
) = 1;
15455 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15457 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15459 TREE_VEC_ELT (targs
, i
) = arg
;
15463 return unify_parameter_deduction_failure (explain_p
, tparm
);
15466 #ifdef ENABLE_CHECKING
15467 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15468 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15471 return unify_success (explain_p
);
15474 /* Subroutine of type_unification_real. Args are like the variables
15475 at the call site. ARG is an overloaded function (or template-id);
15476 we try deducing template args from each of the overloads, and if
15477 only one succeeds, we go with that. Modifies TARGS and returns
15478 true on success. */
15481 resolve_overloaded_unification (tree tparms
,
15485 unification_kind_t strict
,
15489 tree tempargs
= copy_node (targs
);
15491 tree goodfn
= NULL_TREE
;
15494 if (TREE_CODE (arg
) == ADDR_EXPR
)
15496 arg
= TREE_OPERAND (arg
, 0);
15502 if (TREE_CODE (arg
) == COMPONENT_REF
)
15503 /* Handle `&x' where `x' is some static or non-static member
15505 arg
= TREE_OPERAND (arg
, 1);
15507 if (TREE_CODE (arg
) == OFFSET_REF
)
15508 arg
= TREE_OPERAND (arg
, 1);
15510 /* Strip baselink information. */
15511 if (BASELINK_P (arg
))
15512 arg
= BASELINK_FUNCTIONS (arg
);
15514 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15516 /* If we got some explicit template args, we need to plug them into
15517 the affected templates before we try to unify, in case the
15518 explicit args will completely resolve the templates in question. */
15521 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15522 arg
= TREE_OPERAND (arg
, 0);
15524 for (; arg
; arg
= OVL_NEXT (arg
))
15526 tree fn
= OVL_CURRENT (arg
);
15527 tree subargs
, elem
;
15529 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15532 ++processing_template_decl
;
15533 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
15534 expl_subargs
, /*check_ret=*/false);
15535 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
15537 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15538 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15539 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15540 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15548 --processing_template_decl
;
15550 /* If no templates (or more than one) are fully resolved by the
15551 explicit arguments, this template-id is a non-deduced context; it
15552 could still be OK if we deduce all template arguments for the
15553 enclosing call through other arguments. */
15557 else if (TREE_CODE (arg
) != OVERLOAD
15558 && TREE_CODE (arg
) != FUNCTION_DECL
)
15559 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15560 -- but the deduction does not succeed because the expression is
15561 not just the function on its own. */
15564 for (; arg
; arg
= OVL_NEXT (arg
))
15565 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15566 TREE_TYPE (OVL_CURRENT (arg
)),
15567 strict
, sub_strict
, addr_p
, explain_p
)
15568 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15570 goodfn
= OVL_CURRENT (arg
);
15574 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15575 to function or pointer to member function argument if the set of
15576 overloaded functions does not contain function templates and at most
15577 one of a set of overloaded functions provides a unique match.
15579 So if we found multiple possibilities, we return success but don't
15580 deduce anything. */
15584 int i
= TREE_VEC_LENGTH (targs
);
15586 if (TREE_VEC_ELT (tempargs
, i
))
15587 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15595 /* Core DR 115: In contexts where deduction is done and fails, or in
15596 contexts where deduction is not done, if a template argument list is
15597 specified and it, along with any default template arguments, identifies
15598 a single function template specialization, then the template-id is an
15599 lvalue for the function template specialization. */
15602 resolve_nondeduced_context (tree orig_expr
)
15604 tree expr
, offset
, baselink
;
15607 if (!type_unknown_p (orig_expr
))
15612 offset
= NULL_TREE
;
15613 baselink
= NULL_TREE
;
15615 if (TREE_CODE (expr
) == ADDR_EXPR
)
15617 expr
= TREE_OPERAND (expr
, 0);
15620 if (TREE_CODE (expr
) == OFFSET_REF
)
15623 expr
= TREE_OPERAND (expr
, 1);
15625 if (BASELINK_P (expr
))
15628 expr
= BASELINK_FUNCTIONS (expr
);
15631 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15634 tree goodfn
= NULL_TREE
;
15636 /* If we got some explicit template args, we need to plug them into
15637 the affected templates before we try to unify, in case the
15638 explicit args will completely resolve the templates in question. */
15640 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15641 tree arg
= TREE_OPERAND (expr
, 0);
15642 tree badfn
= NULL_TREE
;
15643 tree badargs
= NULL_TREE
;
15645 for (; arg
; arg
= OVL_NEXT (arg
))
15647 tree fn
= OVL_CURRENT (arg
);
15648 tree subargs
, elem
;
15650 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15653 ++processing_template_decl
;
15654 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
15655 expl_subargs
, /*check_ret=*/false);
15656 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
15658 elem
= instantiate_template (fn
, subargs
, tf_none
);
15659 if (elem
== error_mark_node
)
15664 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15670 --processing_template_decl
;
15674 mark_used (goodfn
);
15677 expr
= build_baselink (BASELINK_BINFO (baselink
),
15678 BASELINK_ACCESS_BINFO (baselink
),
15679 expr
, BASELINK_OPTYPE (baselink
));
15683 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15684 expr
= build_offset_ref (base
, expr
, addr
);
15687 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15690 else if (good
== 0 && badargs
)
15691 /* There were no good options and at least one bad one, so let the
15692 user know what the problem is. */
15693 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15698 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15699 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15700 different overloads deduce different arguments for a given parm.
15701 ADDR_P is true if the expression for which deduction is being
15702 performed was of the form "& fn" rather than simply "fn".
15704 Returns 1 on success. */
15707 try_one_overload (tree tparms
,
15712 unification_kind_t strict
,
15721 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15722 to function or pointer to member function argument if the set of
15723 overloaded functions does not contain function templates and at most
15724 one of a set of overloaded functions provides a unique match.
15726 So if this is a template, just return success. */
15728 if (uses_template_parms (arg
))
15731 if (TREE_CODE (arg
) == METHOD_TYPE
)
15732 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15734 arg
= build_pointer_type (arg
);
15736 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15738 /* We don't copy orig_targs for this because if we have already deduced
15739 some template args from previous args, unify would complain when we
15740 try to deduce a template parameter for the same argument, even though
15741 there isn't really a conflict. */
15742 nargs
= TREE_VEC_LENGTH (targs
);
15743 tempargs
= make_tree_vec (nargs
);
15745 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15748 /* First make sure we didn't deduce anything that conflicts with
15749 explicitly specified args. */
15750 for (i
= nargs
; i
--; )
15752 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15753 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15757 else if (uses_template_parms (elt
))
15758 /* Since we're unifying against ourselves, we will fill in
15759 template args used in the function parm list with our own
15760 template parms. Discard them. */
15761 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15762 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15766 for (i
= nargs
; i
--; )
15768 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15771 TREE_VEC_ELT (targs
, i
) = elt
;
15777 /* PARM is a template class (perhaps with unbound template
15778 parameters). ARG is a fully instantiated type. If ARG can be
15779 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15780 TARGS are as for unify. */
15783 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15786 tree copy_of_targs
;
15788 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15789 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15790 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15793 /* We need to make a new template argument vector for the call to
15794 unify. If we used TARGS, we'd clutter it up with the result of
15795 the attempted unification, even if this class didn't work out.
15796 We also don't want to commit ourselves to all the unifications
15797 we've already done, since unification is supposed to be done on
15798 an argument-by-argument basis. In other words, consider the
15799 following pathological case:
15801 template <int I, int J, int K>
15804 template <int I, int J>
15805 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15807 template <int I, int J, int K>
15808 void f(S<I, J, K>, S<I, I, I>);
15817 Now, by the time we consider the unification involving `s2', we
15818 already know that we must have `f<0, 0, 0>'. But, even though
15819 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15820 because there are two ways to unify base classes of S<0, 1, 2>
15821 with S<I, I, I>. If we kept the already deduced knowledge, we
15822 would reject the possibility I=1. */
15823 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15825 /* If unification failed, we're done. */
15826 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15827 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15833 /* Given a template type PARM and a class type ARG, find the unique
15834 base type in ARG that is an instance of PARM. We do not examine
15835 ARG itself; only its base-classes. If there is not exactly one
15836 appropriate base class, return NULL_TREE. PARM may be the type of
15837 a partial specialization, as well as a plain template type. Used
15840 static enum template_base_result
15841 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15842 bool explain_p
, tree
*result
)
15844 tree rval
= NULL_TREE
;
15847 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15849 binfo
= TYPE_BINFO (complete_type (arg
));
15852 /* The type could not be completed. */
15853 *result
= NULL_TREE
;
15854 return tbr_incomplete_type
;
15857 /* Walk in inheritance graph order. The search order is not
15858 important, and this avoids multiple walks of virtual bases. */
15859 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15861 tree r
= try_class_unification (tparms
, targs
, parm
,
15862 BINFO_TYPE (binfo
), explain_p
);
15866 /* If there is more than one satisfactory baseclass, then:
15870 If they yield more than one possible deduced A, the type
15874 if (rval
&& !same_type_p (r
, rval
))
15876 *result
= NULL_TREE
;
15877 return tbr_ambiguous_baseclass
;
15885 return tbr_success
;
15888 /* Returns the level of DECL, which declares a template parameter. */
15891 template_decl_level (tree decl
)
15893 switch (TREE_CODE (decl
))
15896 case TEMPLATE_DECL
:
15897 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
15900 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
15903 gcc_unreachable ();
15908 /* Decide whether ARG can be unified with PARM, considering only the
15909 cv-qualifiers of each type, given STRICT as documented for unify.
15910 Returns nonzero iff the unification is OK on that basis. */
15913 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
15915 int arg_quals
= cp_type_quals (arg
);
15916 int parm_quals
= cp_type_quals (parm
);
15918 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15919 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15921 /* Although a CVR qualifier is ignored when being applied to a
15922 substituted template parameter ([8.3.2]/1 for example), that
15923 does not allow us to unify "const T" with "int&" because both
15924 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15925 It is ok when we're allowing additional CV qualifiers
15926 at the outer level [14.8.2.1]/3,1st bullet. */
15927 if ((TREE_CODE (arg
) == REFERENCE_TYPE
15928 || TREE_CODE (arg
) == FUNCTION_TYPE
15929 || TREE_CODE (arg
) == METHOD_TYPE
)
15930 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
15933 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
15934 && (parm_quals
& TYPE_QUAL_RESTRICT
))
15938 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15939 && (arg_quals
& parm_quals
) != parm_quals
)
15942 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
15943 && (parm_quals
& arg_quals
) != arg_quals
)
15949 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15951 template_parm_level_and_index (tree parm
, int* level
, int* index
)
15953 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15954 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15955 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15957 *index
= TEMPLATE_TYPE_IDX (parm
);
15958 *level
= TEMPLATE_TYPE_LEVEL (parm
);
15962 *index
= TEMPLATE_PARM_IDX (parm
);
15963 *level
= TEMPLATE_PARM_LEVEL (parm
);
15967 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15969 if (unify (TP, TA, P, A, S, EP)) \
15973 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15974 expansion at the end of PACKED_PARMS. Returns 0 if the type
15975 deduction succeeds, 1 otherwise. STRICT is the same as in
15976 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15977 call argument list. We'll need to adjust the arguments to make them
15978 types. SUBR tells us if this is from a recursive call to
15979 type_unification_real, or for comparing two template argument
15983 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
15984 tree packed_args
, unification_kind_t strict
,
15985 bool subr
, bool explain_p
)
15988 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
15989 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
15990 tree pack
, packs
= NULL_TREE
;
15991 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
15992 int len
= TREE_VEC_LENGTH (packed_args
);
15994 /* Determine the parameter packs we will be deducing from the
15995 pattern, and record their current deductions. */
15996 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
15997 pack
; pack
= TREE_CHAIN (pack
))
15999 tree parm_pack
= TREE_VALUE (pack
);
16002 /* Determine the index and level of this parameter pack. */
16003 template_parm_level_and_index (parm_pack
, &level
, &idx
);
16005 /* Keep track of the parameter packs and their corresponding
16007 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
16008 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
16011 /* Loop through all of the arguments that have not yet been
16012 unified and unify each with the pattern. */
16013 for (i
= start
; i
< len
; i
++)
16016 bool any_explicit
= false;
16017 tree arg
= TREE_VEC_ELT (packed_args
, i
);
16019 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
16020 or the element of its argument pack at the current index if
16021 this argument was explicitly specified. */
16022 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16026 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16029 if (TREE_VALUE (pack
)
16030 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
16031 && (i
< TREE_VEC_LENGTH (pargs
)))
16033 any_explicit
= true;
16034 arg
= TREE_VEC_ELT (pargs
, i
);
16036 TMPL_ARG (targs
, level
, idx
) = arg
;
16039 /* If we had explicit template arguments, substitute them into the
16040 pattern before deduction. */
16043 /* Some arguments might still be unspecified or dependent. */
16045 ++processing_template_decl
;
16046 dependent
= any_dependent_template_arguments_p (targs
);
16048 --processing_template_decl
;
16049 parm
= tsubst (pattern
, targs
,
16050 explain_p
? tf_warning_or_error
: tf_none
,
16053 --processing_template_decl
;
16054 if (parm
== error_mark_node
)
16060 /* Unify the pattern with the current argument. */
16061 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
16062 LOOKUP_IMPLICIT
, explain_p
))
16065 /* For each parameter pack, collect the deduced value. */
16066 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16069 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16071 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16072 TMPL_ARG (targs
, level
, idx
);
16076 /* Verify that the results of unification with the parameter packs
16077 produce results consistent with what we've seen before, and make
16078 the deduced argument packs available. */
16079 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16081 tree old_pack
= TREE_VALUE (pack
);
16082 tree new_args
= TREE_TYPE (pack
);
16083 int i
, len
= TREE_VEC_LENGTH (new_args
);
16085 bool nondeduced_p
= false;
16087 /* By default keep the original deduced argument pack.
16088 If necessary, more specific code is going to update the
16089 resulting deduced argument later down in this function. */
16090 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16091 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16093 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16094 actually deduce anything. */
16095 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16096 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16097 nondeduced_p
= true;
16101 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16103 /* If we had fewer function args than explicit template args,
16104 just use the explicits. */
16105 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16106 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16107 if (len
< explicit_len
)
16108 new_args
= explicit_args
;
16114 /* Build the deduced *_ARGUMENT_PACK. */
16115 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16117 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16118 TREE_TYPE (result
) =
16119 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16120 TREE_CONSTANT (result
) = 1;
16123 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16125 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16127 /* Note the deduced argument packs for this parameter
16129 TMPL_ARG (targs
, level
, idx
) = result
;
16131 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16132 && (ARGUMENT_PACK_ARGS (old_pack
)
16133 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16135 /* We only had the explicitly-provided arguments before, but
16136 now we have a complete set of arguments. */
16137 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16139 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16140 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16141 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16145 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16146 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16148 if (!comp_template_args_with_info (old_args
, new_args
,
16149 &bad_old_arg
, &bad_new_arg
))
16150 /* Inconsistent unification of this parameter pack. */
16151 return unify_parameter_pack_inconsistent (explain_p
,
16157 return unify_success (explain_p
);
16160 /* Deduce the value of template parameters. TPARMS is the (innermost)
16161 set of template parameters to a template. TARGS is the bindings
16162 for those template parameters, as determined thus far; TARGS may
16163 include template arguments for outer levels of template parameters
16164 as well. PARM is a parameter to a template function, or a
16165 subcomponent of that parameter; ARG is the corresponding argument.
16166 This function attempts to match PARM with ARG in a manner
16167 consistent with the existing assignments in TARGS. If more values
16168 are deduced, then TARGS is updated.
16170 Returns 0 if the type deduction succeeds, 1 otherwise. The
16171 parameter STRICT is a bitwise or of the following flags:
16174 Require an exact match between PARM and ARG.
16175 UNIFY_ALLOW_MORE_CV_QUAL:
16176 Allow the deduced ARG to be more cv-qualified (by qualification
16177 conversion) than ARG.
16178 UNIFY_ALLOW_LESS_CV_QUAL:
16179 Allow the deduced ARG to be less cv-qualified than ARG.
16180 UNIFY_ALLOW_DERIVED:
16181 Allow the deduced ARG to be a template base class of ARG,
16182 or a pointer to a template base class of the type pointed to by
16184 UNIFY_ALLOW_INTEGER:
16185 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16186 case for more information.
16187 UNIFY_ALLOW_OUTER_LEVEL:
16188 This is the outermost level of a deduction. Used to determine validity
16189 of qualification conversions. A valid qualification conversion must
16190 have const qualified pointers leading up to the inner type which
16191 requires additional CV quals, except at the outer level, where const
16192 is not required [conv.qual]. It would be normal to set this flag in
16193 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16194 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16195 This is the outermost level of a deduction, and PARM can be more CV
16196 qualified at this point.
16197 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16198 This is the outermost level of a deduction, and PARM can be less CV
16199 qualified at this point. */
16202 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16208 int strict_in
= strict
;
16210 /* I don't think this will do the right thing with respect to types.
16211 But the only case I've seen it in so far has been array bounds, where
16212 signedness is the only information lost, and I think that will be
16214 while (TREE_CODE (parm
) == NOP_EXPR
)
16215 parm
= TREE_OPERAND (parm
, 0);
16217 if (arg
== error_mark_node
)
16218 return unify_invalid (explain_p
);
16219 if (arg
== unknown_type_node
16220 || arg
== init_list_type_node
)
16221 /* We can't deduce anything from this, but we might get all the
16222 template args from other function args. */
16223 return unify_success (explain_p
);
16225 /* If PARM uses template parameters, then we can't bail out here,
16226 even if ARG == PARM, since we won't record unifications for the
16227 template parameters. We might need them if we're trying to
16228 figure out which of two things is more specialized. */
16229 if (arg
== parm
&& !uses_template_parms (parm
))
16230 return unify_success (explain_p
);
16232 /* Handle init lists early, so the rest of the function can assume
16233 we're dealing with a type. */
16234 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16238 tree orig_parm
= parm
;
16240 /* Replace T with std::initializer_list<T> for deduction. */
16241 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16242 && flag_deduce_init_list
)
16243 parm
= listify (parm
);
16245 if (!is_std_init_list (parm
))
16246 /* We can only deduce from an initializer list argument if the
16247 parameter is std::initializer_list; otherwise this is a
16248 non-deduced context. */
16249 return unify_success (explain_p
);
16251 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16253 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16255 int elt_strict
= strict
;
16257 if (elt
== error_mark_node
)
16258 return unify_invalid (explain_p
);
16260 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16262 tree type
= TREE_TYPE (elt
);
16263 /* It should only be possible to get here for a call. */
16264 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16265 elt_strict
|= maybe_adjust_types_for_deduction
16266 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16270 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16274 /* If the std::initializer_list<T> deduction worked, replace the
16275 deduced A with std::initializer_list<A>. */
16276 if (orig_parm
!= parm
)
16278 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16279 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16280 targ
= listify (targ
);
16281 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16283 return unify_success (explain_p
);
16286 /* Immediately reject some pairs that won't unify because of
16287 cv-qualification mismatches. */
16288 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16290 /* It is the elements of the array which hold the cv quals of an array
16291 type, and the elements might be template type parms. We'll check
16292 when we recurse. */
16293 && TREE_CODE (arg
) != ARRAY_TYPE
16294 /* We check the cv-qualifiers when unifying with template type
16295 parameters below. We want to allow ARG `const T' to unify with
16296 PARM `T' for example, when computing which of two templates
16297 is more specialized, for example. */
16298 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16299 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16300 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16302 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16303 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16304 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16305 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16306 strict
&= ~UNIFY_ALLOW_DERIVED
;
16307 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16308 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16310 switch (TREE_CODE (parm
))
16312 case TYPENAME_TYPE
:
16314 case UNBOUND_CLASS_TEMPLATE
:
16315 /* In a type which contains a nested-name-specifier, template
16316 argument values cannot be deduced for template parameters used
16317 within the nested-name-specifier. */
16318 return unify_success (explain_p
);
16320 case TEMPLATE_TYPE_PARM
:
16321 case TEMPLATE_TEMPLATE_PARM
:
16322 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16323 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16324 if (tparm
== error_mark_node
)
16325 return unify_invalid (explain_p
);
16327 if (TEMPLATE_TYPE_LEVEL (parm
)
16328 != template_decl_level (tparm
))
16329 /* The PARM is not one we're trying to unify. Just check
16330 to see if it matches ARG. */
16332 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16333 && (is_auto (parm
) ? is_auto (arg
)
16334 : same_type_p (parm
, arg
)))
16335 return unify_success (explain_p
);
16337 return unify_type_mismatch (explain_p
, parm
, arg
);
16339 idx
= TEMPLATE_TYPE_IDX (parm
);
16340 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16341 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16342 if (tparm
== error_mark_node
)
16343 return unify_invalid (explain_p
);
16345 /* Check for mixed types and values. */
16346 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16347 && TREE_CODE (tparm
) != TYPE_DECL
)
16348 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16349 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16350 gcc_unreachable ();
16352 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16354 /* ARG must be constructed from a template class or a template
16355 template parameter. */
16356 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16357 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16358 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16361 tree parmvec
= TYPE_TI_ARGS (parm
);
16362 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16363 tree full_argvec
= add_to_template_args (targs
, argvec
);
16365 = DECL_INNERMOST_TEMPLATE_PARMS
16366 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16368 int parm_variadic_p
= 0;
16370 /* The resolution to DR150 makes clear that default
16371 arguments for an N-argument may not be used to bind T
16372 to a template template parameter with fewer than N
16373 parameters. It is not safe to permit the binding of
16374 default arguments as an extension, as that may change
16375 the meaning of a conforming program. Consider:
16377 struct Dense { static const unsigned int dim = 1; };
16379 template <template <typename> class View,
16381 void operator+(float, View<Block> const&);
16383 template <typename Block,
16384 unsigned int Dim = Block::dim>
16385 struct Lvalue_proxy { operator float() const; };
16389 Lvalue_proxy<Dense> p;
16394 Here, if Lvalue_proxy is permitted to bind to View, then
16395 the global operator+ will be used; if they are not, the
16396 Lvalue_proxy will be converted to float. */
16397 if (coerce_template_parms (parm_parms
,
16399 TYPE_TI_TEMPLATE (parm
),
16401 ? tf_warning_or_error
16403 /*require_all_args=*/true,
16404 /*use_default_args=*/false)
16405 == error_mark_node
)
16408 /* Deduce arguments T, i from TT<T> or TT<i>.
16409 We check each element of PARMVEC and ARGVEC individually
16410 rather than the whole TREE_VEC since they can have
16411 different number of elements. */
16413 parmvec
= expand_template_argument_pack (parmvec
);
16414 argvec
= expand_template_argument_pack (argvec
);
16416 len
= TREE_VEC_LENGTH (parmvec
);
16418 /* Check if the parameters end in a pack, making them
16421 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16422 parm_variadic_p
= 1;
16424 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16425 return unify_too_few_arguments (explain_p
,
16426 TREE_VEC_LENGTH (argvec
), len
);
16428 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16430 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16431 TREE_VEC_ELT (parmvec
, i
),
16432 TREE_VEC_ELT (argvec
, i
),
16433 UNIFY_ALLOW_NONE
, explain_p
);
16436 if (parm_variadic_p
16437 && unify_pack_expansion (tparms
, targs
,
16440 /*subr=*/true, explain_p
))
16443 arg
= TYPE_TI_TEMPLATE (arg
);
16445 /* Fall through to deduce template name. */
16448 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16449 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16451 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16453 /* Simple cases: Value already set, does match or doesn't. */
16454 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16455 return unify_success (explain_p
);
16457 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16461 /* If PARM is `const T' and ARG is only `int', we don't have
16462 a match unless we are allowing additional qualification.
16463 If ARG is `const int' and PARM is just `T' that's OK;
16464 that binds `const int' to `T'. */
16465 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16467 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16469 /* Consider the case where ARG is `const volatile int' and
16470 PARM is `const T'. Then, T should be `volatile int'. */
16471 arg
= cp_build_qualified_type_real
16472 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16473 if (arg
== error_mark_node
)
16474 return unify_invalid (explain_p
);
16476 /* Simple cases: Value already set, does match or doesn't. */
16477 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16478 return unify_success (explain_p
);
16480 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16482 /* Make sure that ARG is not a variable-sized array. (Note
16483 that were talking about variable-sized arrays (like
16484 `int[n]'), rather than arrays of unknown size (like
16485 `int[]').) We'll get very confused by such a type since
16486 the bound of the array is not constant, and therefore
16487 not mangleable. Besides, such types are not allowed in
16488 ISO C++, so we can do as we please here. We do allow
16489 them for 'auto' deduction, since that isn't ABI-exposed. */
16490 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16491 return unify_vla_arg (explain_p
, arg
);
16493 /* Strip typedefs as in convert_template_argument. */
16494 arg
= canonicalize_type_argument (arg
, tf_none
);
16497 /* If ARG is a parameter pack or an expansion, we cannot unify
16498 against it unless PARM is also a parameter pack. */
16499 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16500 && !template_parameter_pack_p (parm
))
16501 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16503 /* If the argument deduction results is a METHOD_TYPE,
16504 then there is a problem.
16505 METHOD_TYPE doesn't map to any real C++ type the result of
16506 the deduction can not be of that type. */
16507 if (TREE_CODE (arg
) == METHOD_TYPE
)
16508 return unify_method_type_error (explain_p
, arg
);
16510 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16511 return unify_success (explain_p
);
16513 case TEMPLATE_PARM_INDEX
:
16514 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16515 if (tparm
== error_mark_node
)
16516 return unify_invalid (explain_p
);
16518 if (TEMPLATE_PARM_LEVEL (parm
)
16519 != template_decl_level (tparm
))
16521 /* The PARM is not one we're trying to unify. Just check
16522 to see if it matches ARG. */
16523 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16524 && cp_tree_equal (parm
, arg
));
16526 unify_expression_unequal (explain_p
, parm
, arg
);
16530 idx
= TEMPLATE_PARM_IDX (parm
);
16531 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16535 int x
= !cp_tree_equal (targ
, arg
);
16537 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16541 /* [temp.deduct.type] If, in the declaration of a function template
16542 with a non-type template-parameter, the non-type
16543 template-parameter is used in an expression in the function
16544 parameter-list and, if the corresponding template-argument is
16545 deduced, the template-argument type shall match the type of the
16546 template-parameter exactly, except that a template-argument
16547 deduced from an array bound may be of any integral type.
16548 The non-type parameter might use already deduced type parameters. */
16549 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16550 if (!TREE_TYPE (arg
))
16551 /* Template-parameter dependent expression. Just accept it for now.
16552 It will later be processed in convert_template_argument. */
16554 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16556 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16557 && (TREE_CODE (tparm
) == INTEGER_TYPE
16558 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16559 /* Convert the ARG to the type of PARM; the deduced non-type
16560 template argument must exactly match the types of the
16561 corresponding parameter. */
16562 arg
= fold (build_nop (tparm
, arg
));
16563 else if (uses_template_parms (tparm
))
16564 /* We haven't deduced the type of this parameter yet. Try again
16566 return unify_success (explain_p
);
16568 return unify_type_mismatch (explain_p
, tparm
, arg
);
16570 /* If ARG is a parameter pack or an expansion, we cannot unify
16571 against it unless PARM is also a parameter pack. */
16572 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16573 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16574 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16576 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16577 return unify_success (explain_p
);
16581 /* A pointer-to-member constant can be unified only with
16582 another constant. */
16583 if (TREE_CODE (arg
) != PTRMEM_CST
)
16584 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16586 /* Just unify the class member. It would be useless (and possibly
16587 wrong, depending on the strict flags) to unify also
16588 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16589 arg refer to the same variable, even if through different
16590 classes. For instance:
16592 struct A { int x; };
16595 Unification of &A::x and &B::x must succeed. */
16596 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16597 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16602 if (TREE_CODE (arg
) != POINTER_TYPE
)
16603 return unify_type_mismatch (explain_p
, parm
, arg
);
16605 /* [temp.deduct.call]
16607 A can be another pointer or pointer to member type that can
16608 be converted to the deduced A via a qualification
16609 conversion (_conv.qual_).
16611 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16612 This will allow for additional cv-qualification of the
16613 pointed-to types if appropriate. */
16615 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16616 /* The derived-to-base conversion only persists through one
16617 level of pointers. */
16618 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16620 return unify (tparms
, targs
, TREE_TYPE (parm
),
16621 TREE_TYPE (arg
), strict
, explain_p
);
16624 case REFERENCE_TYPE
:
16625 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16626 return unify_type_mismatch (explain_p
, parm
, arg
);
16627 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16628 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16631 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16632 return unify_type_mismatch (explain_p
, parm
, arg
);
16633 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16634 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16635 return unify_type_mismatch (explain_p
, parm
, arg
);
16636 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16643 /* Our representation of array types uses "N - 1" as the
16644 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16645 not an integer constant. We cannot unify arbitrarily
16646 complex expressions, so we eliminate the MINUS_EXPRs
16648 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16649 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16652 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16653 parm_max
= TREE_OPERAND (parm_max
, 0);
16655 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16656 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16659 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16660 trying to unify the type of a variable with the type
16661 of a template parameter. For example:
16663 template <unsigned int N>
16664 void f (char (&) [N]);
16671 Here, the type of the ARG will be "int [g(i)]", and
16672 may be a SAVE_EXPR, etc. */
16673 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16674 return unify_vla_arg (explain_p
, arg
);
16675 arg_max
= TREE_OPERAND (arg_max
, 0);
16678 /* If only one of the bounds used a MINUS_EXPR, compensate
16679 by adding one to the other bound. */
16680 if (parm_cst
&& !arg_cst
)
16681 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16685 else if (arg_cst
&& !parm_cst
)
16686 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16691 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16692 UNIFY_ALLOW_INTEGER
, explain_p
);
16694 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16695 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16702 case ENUMERAL_TYPE
:
16705 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16706 return unify_type_mismatch (explain_p
, parm
, arg
);
16708 /* We have already checked cv-qualification at the top of the
16710 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16711 return unify_type_mismatch (explain_p
, parm
, arg
);
16713 /* As far as unification is concerned, this wins. Later checks
16714 will invalidate it if necessary. */
16715 return unify_success (explain_p
);
16717 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16718 /* Type INTEGER_CST can come from ordinary constant template args. */
16720 while (TREE_CODE (arg
) == NOP_EXPR
)
16721 arg
= TREE_OPERAND (arg
, 0);
16723 if (TREE_CODE (arg
) != INTEGER_CST
)
16724 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16725 return (tree_int_cst_equal (parm
, arg
)
16726 ? unify_success (explain_p
)
16727 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16731 int i
, len
, argslen
;
16732 int parm_variadic_p
= 0;
16734 if (TREE_CODE (arg
) != TREE_VEC
)
16735 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16737 len
= TREE_VEC_LENGTH (parm
);
16738 argslen
= TREE_VEC_LENGTH (arg
);
16740 /* Check for pack expansions in the parameters. */
16741 for (i
= 0; i
< len
; ++i
)
16743 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16746 /* We can unify against something with a trailing
16748 parm_variadic_p
= 1;
16750 /* [temp.deduct.type]/9: If the template argument list of
16751 P contains a pack expansion that is not the last
16752 template argument, the entire template argument list
16753 is a non-deduced context. */
16754 return unify_success (explain_p
);
16758 /* If we don't have enough arguments to satisfy the parameters
16759 (not counting the pack expression at the end), or we have
16760 too many arguments for a parameter list that doesn't end in
16761 a pack expression, we can't unify. */
16762 if (parm_variadic_p
16763 ? argslen
< len
- parm_variadic_p
16765 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16767 /* Unify all of the parameters that precede the (optional)
16768 pack expression. */
16769 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16771 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16772 TREE_VEC_ELT (parm
, i
),
16773 TREE_VEC_ELT (arg
, i
),
16774 UNIFY_ALLOW_NONE
, explain_p
);
16776 if (parm_variadic_p
)
16777 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16779 /*subr=*/true, explain_p
);
16780 return unify_success (explain_p
);
16785 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16786 return unify_type_mismatch (explain_p
, parm
, arg
);
16788 if (TYPE_PTRMEMFUNC_P (parm
))
16790 if (!TYPE_PTRMEMFUNC_P (arg
))
16791 return unify_type_mismatch (explain_p
, parm
, arg
);
16793 return unify (tparms
, targs
,
16794 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16795 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16796 strict
, explain_p
);
16799 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16801 tree t
= NULL_TREE
;
16803 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16805 /* First, we try to unify the PARM and ARG directly. */
16806 t
= try_class_unification (tparms
, targs
,
16807 parm
, arg
, explain_p
);
16811 /* Fallback to the special case allowed in
16812 [temp.deduct.call]:
16814 If P is a class, and P has the form
16815 template-id, then A can be a derived class of
16816 the deduced A. Likewise, if P is a pointer to
16817 a class of the form template-id, A can be a
16818 pointer to a derived class pointed to by the
16820 enum template_base_result r
;
16821 r
= get_template_base (tparms
, targs
, parm
, arg
,
16825 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16828 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16829 && (CLASSTYPE_TI_TEMPLATE (parm
)
16830 == CLASSTYPE_TI_TEMPLATE (arg
)))
16831 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16832 Then, we should unify `int' and `U'. */
16835 /* There's no chance of unification succeeding. */
16836 return unify_type_mismatch (explain_p
, parm
, arg
);
16838 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16839 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16841 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16842 return unify_type_mismatch (explain_p
, parm
, arg
);
16843 return unify_success (explain_p
);
16846 case FUNCTION_TYPE
:
16848 unsigned int nargs
;
16853 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16854 return unify_type_mismatch (explain_p
, parm
, arg
);
16856 /* CV qualifications for methods can never be deduced, they must
16857 match exactly. We need to check them explicitly here,
16858 because type_unification_real treats them as any other
16859 cv-qualified parameter. */
16860 if (TREE_CODE (parm
) == METHOD_TYPE
16861 && (!check_cv_quals_for_unify
16863 class_of_this_parm (arg
),
16864 class_of_this_parm (parm
))))
16865 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16867 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
16868 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
16870 nargs
= list_length (TYPE_ARG_TYPES (arg
));
16871 args
= XALLOCAVEC (tree
, nargs
);
16872 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
16873 a
!= NULL_TREE
&& a
!= void_list_node
;
16874 a
= TREE_CHAIN (a
), ++i
)
16875 args
[i
] = TREE_VALUE (a
);
16878 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
16879 args
, nargs
, 1, DEDUCE_EXACT
,
16880 LOOKUP_NORMAL
, explain_p
);
16884 /* Unify a pointer to member with a pointer to member function, which
16885 deduces the type of the member as a function type. */
16886 if (TYPE_PTRMEMFUNC_P (arg
))
16891 /* Check top-level cv qualifiers */
16892 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
16893 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16895 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16896 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
16897 UNIFY_ALLOW_NONE
, explain_p
);
16899 /* Determine the type of the function we are unifying against. */
16900 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
16902 build_function_type (TREE_TYPE (method_type
),
16903 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
16905 /* Extract the cv-qualifiers of the member function from the
16906 implicit object parameter and place them on the function
16907 type to be restored later. */
16908 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
16909 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
16912 if (TREE_CODE (arg
) != OFFSET_TYPE
)
16913 return unify_type_mismatch (explain_p
, parm
, arg
);
16914 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16915 TYPE_OFFSET_BASETYPE (arg
),
16916 UNIFY_ALLOW_NONE
, explain_p
);
16917 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16918 strict
, explain_p
);
16921 if (DECL_TEMPLATE_PARM_P (parm
))
16922 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
16923 if (arg
!= integral_constant_value (parm
))
16924 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16925 return unify_success (explain_p
);
16928 case TEMPLATE_DECL
:
16929 /* Matched cases are handled by the ARG == PARM test above. */
16930 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16933 /* A non-type template parameter that is a variable should be a
16934 an integral constant, in which case, it whould have been
16935 folded into its (constant) value. So we should not be getting
16936 a variable here. */
16937 gcc_unreachable ();
16939 case TYPE_ARGUMENT_PACK
:
16940 case NONTYPE_ARGUMENT_PACK
:
16941 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
16942 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
16945 case DECLTYPE_TYPE
:
16946 case UNDERLYING_TYPE
:
16947 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16948 or UNDERLYING_TYPE nodes. */
16949 return unify_success (explain_p
);
16952 /* Unification fails if we hit an error node. */
16953 return unify_invalid (explain_p
);
16956 /* An unresolved overload is a nondeduced context. */
16957 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
16958 return unify_success (explain_p
);
16959 gcc_assert (EXPR_P (parm
));
16961 /* We must be looking at an expression. This can happen with
16965 void foo(S<I>, S<I + 2>);
16967 This is a "nondeduced context":
16971 The nondeduced contexts are:
16973 --A type that is a template-id in which one or more of
16974 the template-arguments is an expression that references
16975 a template-parameter.
16977 In these cases, we assume deduction succeeded, but don't
16978 actually infer any unifications. */
16980 if (!uses_template_parms (parm
)
16981 && !template_args_equal (parm
, arg
))
16982 return unify_expression_unequal (explain_p
, parm
, arg
);
16984 return unify_success (explain_p
);
16987 #undef RECUR_AND_CHECK_FAILURE
16989 /* Note that DECL can be defined in this translation unit, if
16993 mark_definable (tree decl
)
16996 DECL_NOT_REALLY_EXTERN (decl
) = 1;
16997 FOR_EACH_CLONE (clone
, decl
)
16998 DECL_NOT_REALLY_EXTERN (clone
) = 1;
17001 /* Called if RESULT is explicitly instantiated, or is a member of an
17002 explicitly instantiated class. */
17005 mark_decl_instantiated (tree result
, int extern_p
)
17007 SET_DECL_EXPLICIT_INSTANTIATION (result
);
17009 /* If this entity has already been written out, it's too late to
17010 make any modifications. */
17011 if (TREE_ASM_WRITTEN (result
))
17014 if (TREE_CODE (result
) != FUNCTION_DECL
)
17015 /* The TREE_PUBLIC flag for function declarations will have been
17016 set correctly by tsubst. */
17017 TREE_PUBLIC (result
) = 1;
17019 /* This might have been set by an earlier implicit instantiation. */
17020 DECL_COMDAT (result
) = 0;
17023 DECL_NOT_REALLY_EXTERN (result
) = 0;
17026 mark_definable (result
);
17027 /* Always make artificials weak. */
17028 if (DECL_ARTIFICIAL (result
) && flag_weak
)
17029 comdat_linkage (result
);
17030 /* For WIN32 we also want to put explicit instantiations in
17031 linkonce sections. */
17032 else if (TREE_PUBLIC (result
))
17033 maybe_make_one_only (result
);
17036 /* If EXTERN_P, then this function will not be emitted -- unless
17037 followed by an explicit instantiation, at which point its linkage
17038 will be adjusted. If !EXTERN_P, then this function will be
17039 emitted here. In neither circumstance do we want
17040 import_export_decl to adjust the linkage. */
17041 DECL_INTERFACE_KNOWN (result
) = 1;
17044 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
17045 important template arguments. If any are missing, we check whether
17046 they're important by using error_mark_node for substituting into any
17047 args that were used for partial ordering (the ones between ARGS and END)
17048 and seeing if it bubbles up. */
17051 check_undeduced_parms (tree targs
, tree args
, tree end
)
17053 bool found
= false;
17055 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
17056 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
17059 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
17063 for (; args
!= end
; args
= TREE_CHAIN (args
))
17065 tree substed
= tsubst (TREE_VALUE (args
), targs
, tf_none
, NULL_TREE
);
17066 if (substed
== error_mark_node
)
17073 /* Given two function templates PAT1 and PAT2, return:
17075 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17076 -1 if PAT2 is more specialized than PAT1.
17077 0 if neither is more specialized.
17079 LEN indicates the number of parameters we should consider
17080 (defaulted parameters should not be considered).
17082 The 1998 std underspecified function template partial ordering, and
17083 DR214 addresses the issue. We take pairs of arguments, one from
17084 each of the templates, and deduce them against each other. One of
17085 the templates will be more specialized if all the *other*
17086 template's arguments deduce against its arguments and at least one
17087 of its arguments *does* *not* deduce against the other template's
17088 corresponding argument. Deduction is done as for class templates.
17089 The arguments used in deduction have reference and top level cv
17090 qualifiers removed. Iff both arguments were originally reference
17091 types *and* deduction succeeds in both directions, the template
17092 with the more cv-qualified argument wins for that pairing (if
17093 neither is more cv-qualified, they both are equal). Unlike regular
17094 deduction, after all the arguments have been deduced in this way,
17095 we do *not* verify the deduced template argument values can be
17096 substituted into non-deduced contexts.
17098 The logic can be a bit confusing here, because we look at deduce1 and
17099 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17100 can find template arguments for pat1 to make arg1 look like arg2, that
17101 means that arg2 is at least as specialized as arg1. */
17104 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17106 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17107 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17108 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17109 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17110 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17111 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17112 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17113 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17114 tree origs1
, origs2
;
17115 bool lose1
= false;
17116 bool lose2
= false;
17118 /* Remove the this parameter from non-static member functions. If
17119 one is a non-static member function and the other is not a static
17120 member function, remove the first parameter from that function
17121 also. This situation occurs for operator functions where we
17122 locate both a member function (with this pointer) and non-member
17123 operator (with explicit first operand). */
17124 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17126 len
--; /* LEN is the number of significant arguments for DECL1 */
17127 args1
= TREE_CHAIN (args1
);
17128 if (!DECL_STATIC_FUNCTION_P (decl2
))
17129 args2
= TREE_CHAIN (args2
);
17131 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17133 args2
= TREE_CHAIN (args2
);
17134 if (!DECL_STATIC_FUNCTION_P (decl1
))
17137 args1
= TREE_CHAIN (args1
);
17141 /* If only one is a conversion operator, they are unordered. */
17142 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17145 /* Consider the return type for a conversion function */
17146 if (DECL_CONV_FN_P (decl1
))
17148 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17149 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17153 processing_template_decl
++;
17159 /* Stop when an ellipsis is seen. */
17160 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17162 tree arg1
= TREE_VALUE (args1
);
17163 tree arg2
= TREE_VALUE (args2
);
17164 int deduce1
, deduce2
;
17168 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17169 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17171 /* When both arguments are pack expansions, we need only
17172 unify the patterns themselves. */
17173 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17174 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17176 /* This is the last comparison we need to do. */
17180 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17182 arg1
= TREE_TYPE (arg1
);
17183 quals1
= cp_type_quals (arg1
);
17186 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17188 arg2
= TREE_TYPE (arg2
);
17189 quals2
= cp_type_quals (arg2
);
17192 arg1
= TYPE_MAIN_VARIANT (arg1
);
17193 arg2
= TYPE_MAIN_VARIANT (arg2
);
17195 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17197 int i
, len2
= list_length (args2
);
17198 tree parmvec
= make_tree_vec (1);
17199 tree argvec
= make_tree_vec (len2
);
17202 /* Setup the parameter vector, which contains only ARG1. */
17203 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17205 /* Setup the argument vector, which contains the remaining
17207 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17208 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17210 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17211 argvec
, DEDUCE_EXACT
,
17212 /*subr=*/true, /*explain_p=*/false)
17215 /* We cannot deduce in the other direction, because ARG1 is
17216 a pack expansion but ARG2 is not. */
17219 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17221 int i
, len1
= list_length (args1
);
17222 tree parmvec
= make_tree_vec (1);
17223 tree argvec
= make_tree_vec (len1
);
17226 /* Setup the parameter vector, which contains only ARG1. */
17227 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17229 /* Setup the argument vector, which contains the remaining
17231 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17232 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17234 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17235 argvec
, DEDUCE_EXACT
,
17236 /*subr=*/true, /*explain_p=*/false)
17239 /* We cannot deduce in the other direction, because ARG2 is
17240 a pack expansion but ARG1 is not.*/
17246 /* The normal case, where neither argument is a pack
17248 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17249 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17251 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17252 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17256 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17257 arg2, then arg2 is not as specialized as arg1. */
17263 /* "If, for a given type, deduction succeeds in both directions
17264 (i.e., the types are identical after the transformations above)
17265 and if the type from the argument template is more cv-qualified
17266 than the type from the parameter template (as described above)
17267 that type is considered to be more specialized than the other. If
17268 neither type is more cv-qualified than the other then neither type
17269 is more specialized than the other." */
17271 if (deduce1
&& deduce2
17272 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17274 if ((quals1
& quals2
) == quals2
)
17276 if ((quals1
& quals2
) == quals1
)
17280 if (lose1
&& lose2
)
17281 /* We've failed to deduce something in either direction.
17282 These must be unordered. */
17285 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17286 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17287 /* We have already processed all of the arguments in our
17288 handing of the pack expansion type. */
17291 args1
= TREE_CHAIN (args1
);
17292 args2
= TREE_CHAIN (args2
);
17295 /* "In most cases, all template parameters must have values in order for
17296 deduction to succeed, but for partial ordering purposes a template
17297 parameter may remain without a value provided it is not used in the
17298 types being used for partial ordering."
17300 Thus, if we are missing any of the targs1 we need to substitute into
17301 origs1, then pat2 is not as specialized as pat1. This can happen when
17302 there is a nondeduced context. */
17303 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17305 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17308 processing_template_decl
--;
17310 /* All things being equal, if the next argument is a pack expansion
17311 for one function but not for the other, prefer the
17312 non-variadic function. FIXME this is bogus; see c++/41958. */
17314 && args1
&& TREE_VALUE (args1
)
17315 && args2
&& TREE_VALUE (args2
))
17317 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17318 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17321 if (lose1
== lose2
)
17329 /* Determine which of two partial specializations is more specialized.
17331 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17332 to the first partial specialization. The TREE_VALUE is the
17333 innermost set of template parameters for the partial
17334 specialization. PAT2 is similar, but for the second template.
17336 Return 1 if the first partial specialization is more specialized;
17337 -1 if the second is more specialized; 0 if neither is more
17340 See [temp.class.order] for information about determining which of
17341 two templates is more specialized. */
17344 more_specialized_class (tree pat1
, tree pat2
)
17349 bool any_deductions
= false;
17351 tmpl1
= TREE_TYPE (pat1
);
17352 tmpl2
= TREE_TYPE (pat2
);
17354 /* Just like what happens for functions, if we are ordering between
17355 different class template specializations, we may encounter dependent
17356 types in the arguments, and we need our dependency check functions
17357 to behave correctly. */
17358 ++processing_template_decl
;
17359 targs
= get_class_bindings (TREE_VALUE (pat1
),
17360 CLASSTYPE_TI_ARGS (tmpl1
),
17361 CLASSTYPE_TI_ARGS (tmpl2
));
17365 any_deductions
= true;
17368 targs
= get_class_bindings (TREE_VALUE (pat2
),
17369 CLASSTYPE_TI_ARGS (tmpl2
),
17370 CLASSTYPE_TI_ARGS (tmpl1
));
17374 any_deductions
= true;
17376 --processing_template_decl
;
17378 /* In the case of a tie where at least one of the class templates
17379 has a parameter pack at the end, the template with the most
17380 non-packed parameters wins. */
17383 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17384 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17386 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17387 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17388 int len1
= TREE_VEC_LENGTH (args1
);
17389 int len2
= TREE_VEC_LENGTH (args2
);
17391 /* We don't count the pack expansion at the end. */
17392 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17394 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17399 else if (len1
< len2
)
17406 /* Return the template arguments that will produce the function signature
17407 DECL from the function template FN, with the explicit template
17408 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17409 also match. Return NULL_TREE if no satisfactory arguments could be
17413 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17415 int ntparms
= DECL_NTPARMS (fn
);
17416 tree targs
= make_tree_vec (ntparms
);
17418 tree decl_arg_types
;
17420 unsigned int nargs
, ix
;
17423 /* Substitute the explicit template arguments into the type of DECL.
17424 The call to fn_type_unification will handle substitution into the
17426 decl_type
= TREE_TYPE (decl
);
17427 if (explicit_args
&& decl
== DECL_TEMPLATE_RESULT (fn
))
17430 tree converted_args
;
17432 if (DECL_TEMPLATE_INFO (decl
))
17433 tmpl
= DECL_TI_TEMPLATE (decl
);
17435 /* We can get here for some invalid specializations. */
17439 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17440 explicit_args
, NULL_TREE
,
17442 /*require_all_args=*/false,
17443 /*use_default_args=*/false);
17444 if (converted_args
== error_mark_node
)
17447 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
17448 if (decl_type
== error_mark_node
)
17452 /* Never do unification on the 'this' parameter. */
17453 decl_arg_types
= skip_artificial_parms_for (decl
,
17454 TYPE_ARG_TYPES (decl_type
));
17456 nargs
= list_length (decl_arg_types
);
17457 args
= XALLOCAVEC (tree
, nargs
);
17458 for (arg
= decl_arg_types
, ix
= 0;
17459 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17460 arg
= TREE_CHAIN (arg
), ++ix
)
17461 args
[ix
] = TREE_VALUE (arg
);
17463 if (fn_type_unification (fn
, explicit_args
, targs
,
17465 (check_rettype
|| DECL_CONV_FN_P (fn
)
17466 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17467 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false))
17473 /* Return the innermost template arguments that, when applied to a
17474 template specialization whose innermost template parameters are
17475 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17478 For example, suppose we have:
17480 template <class T, class U> struct S {};
17481 template <class T> struct S<T*, int> {};
17483 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17484 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17485 int}. The resulting vector will be {double}, indicating that `T'
17486 is bound to `double'. */
17489 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
17491 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17493 tree innermost_deduced_args
;
17495 innermost_deduced_args
= make_tree_vec (ntparms
);
17496 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17498 deduced_args
= copy_node (args
);
17499 SET_TMPL_ARGS_LEVEL (deduced_args
,
17500 TMPL_ARGS_DEPTH (deduced_args
),
17501 innermost_deduced_args
);
17504 deduced_args
= innermost_deduced_args
;
17506 if (unify (tparms
, deduced_args
,
17507 INNERMOST_TEMPLATE_ARGS (spec_args
),
17508 INNERMOST_TEMPLATE_ARGS (args
),
17509 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17512 for (i
= 0; i
< ntparms
; ++i
)
17513 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17516 /* Verify that nondeduced template arguments agree with the type
17517 obtained from argument deduction.
17521 struct A { typedef int X; };
17522 template <class T, class U> struct C {};
17523 template <class T> struct C<T, typename T::X> {};
17525 Then with the instantiation `C<A, int>', we can deduce that
17526 `T' is `A' but unify () does not check whether `typename T::X'
17528 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17529 if (spec_args
== error_mark_node
17530 /* We only need to check the innermost arguments; the other
17531 arguments will always agree. */
17532 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17533 INNERMOST_TEMPLATE_ARGS (args
)))
17536 /* Now that we have bindings for all of the template arguments,
17537 ensure that the arguments deduced for the template template
17538 parameters have compatible template parameter lists. See the use
17539 of template_template_parm_bindings_ok_p in fn_type_unification
17540 for more information. */
17541 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17544 return deduced_args
;
17547 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17548 Return the TREE_LIST node with the most specialized template, if
17549 any. If there is no most specialized template, the error_mark_node
17552 Note that this function does not look at, or modify, the
17553 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17554 returned is one of the elements of INSTANTIATIONS, callers may
17555 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17556 and retrieve it from the value returned. */
17559 most_specialized_instantiation (tree templates
)
17563 ++processing_template_decl
;
17566 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17570 if (get_bindings (TREE_VALUE (champ
),
17571 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17572 NULL_TREE
, /*check_ret=*/true))
17575 if (get_bindings (TREE_VALUE (fn
),
17576 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17577 NULL_TREE
, /*check_ret=*/true))
17584 /* Equally specialized, move to next function. If there
17585 is no next function, nothing's most specialized. */
17586 fn
= TREE_CHAIN (fn
);
17594 /* Now verify that champ is better than everything earlier in the
17595 instantiation list. */
17596 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17597 if (get_bindings (TREE_VALUE (champ
),
17598 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17599 NULL_TREE
, /*check_ret=*/true)
17600 || !get_bindings (TREE_VALUE (fn
),
17601 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17602 NULL_TREE
, /*check_ret=*/true))
17608 processing_template_decl
--;
17611 return error_mark_node
;
17616 /* If DECL is a specialization of some template, return the most
17617 general such template. Otherwise, returns NULL_TREE.
17619 For example, given:
17621 template <class T> struct S { template <class U> void f(U); };
17623 if TMPL is `template <class U> void S<int>::f(U)' this will return
17624 the full template. This function will not trace past partial
17625 specializations, however. For example, given in addition:
17627 template <class T> struct S<T*> { template <class U> void f(U); };
17629 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17630 `template <class T> template <class U> S<T*>::f(U)'. */
17633 most_general_template (tree decl
)
17635 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17636 an immediate specialization. */
17637 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17639 if (DECL_TEMPLATE_INFO (decl
)) {
17640 decl
= DECL_TI_TEMPLATE (decl
);
17642 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17643 template friend. */
17644 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17650 /* Look for more and more general templates. */
17651 while (DECL_TEMPLATE_INFO (decl
))
17653 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17654 (See cp-tree.h for details.) */
17655 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17658 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17659 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17662 /* Stop if we run into an explicitly specialized class template. */
17663 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17664 && DECL_CONTEXT (decl
)
17665 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17668 decl
= DECL_TI_TEMPLATE (decl
);
17674 /* Return the most specialized of the class template partial
17675 specializations of TMPL which can produce TYPE, a specialization of
17676 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17677 a _TYPE node corresponding to the partial specialization, while the
17678 TREE_PURPOSE is the set of template arguments that must be
17679 substituted into the TREE_TYPE in order to generate TYPE.
17681 If the choice of partial specialization is ambiguous, a diagnostic
17682 is issued, and the error_mark_node is returned. If there are no
17683 partial specializations of TMPL matching TYPE, then NULL_TREE is
17687 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17689 tree list
= NULL_TREE
;
17695 tree outer_args
= NULL_TREE
;
17697 tmpl
= most_general_template (tmpl
);
17698 args
= CLASSTYPE_TI_ARGS (type
);
17700 /* For determining which partial specialization to use, only the
17701 innermost args are interesting. */
17702 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17704 outer_args
= strip_innermost_template_args (args
, 1);
17705 args
= INNERMOST_TEMPLATE_ARGS (args
);
17708 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17710 tree partial_spec_args
;
17712 tree parms
= TREE_VALUE (t
);
17714 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17716 ++processing_template_decl
;
17722 /* Discard the outer levels of args, and then substitute in the
17723 template args from the enclosing class. */
17724 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17725 partial_spec_args
= tsubst_template_args
17726 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17728 /* PARMS already refers to just the innermost parms, but the
17729 template parms in partial_spec_args had their levels lowered
17730 by tsubst, so we need to do the same for the parm list. We
17731 can't just tsubst the TREE_VEC itself, as tsubst wants to
17732 treat a TREE_VEC as an argument vector. */
17733 parms
= copy_node (parms
);
17734 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17735 TREE_VEC_ELT (parms
, i
) =
17736 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17740 partial_spec_args
=
17741 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17742 add_to_template_args (outer_args
,
17743 partial_spec_args
),
17745 /*require_all_args=*/true,
17746 /*use_default_args=*/true);
17748 --processing_template_decl
;
17750 if (partial_spec_args
== error_mark_node
)
17751 return error_mark_node
;
17753 spec_args
= get_class_bindings (parms
,
17759 spec_args
= add_to_template_args (outer_args
, spec_args
);
17760 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17761 TREE_TYPE (list
) = TREE_TYPE (t
);
17768 ambiguous_p
= false;
17771 t
= TREE_CHAIN (t
);
17772 for (; t
; t
= TREE_CHAIN (t
))
17774 fate
= more_specialized_class (champ
, t
);
17781 t
= TREE_CHAIN (t
);
17784 ambiguous_p
= true;
17793 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17795 fate
= more_specialized_class (champ
, t
);
17798 ambiguous_p
= true;
17806 char *spaces
= NULL
;
17807 if (!(complain
& tf_error
))
17808 return error_mark_node
;
17809 error ("ambiguous class template instantiation for %q#T", type
);
17810 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17811 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17813 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17814 spaces
= spaces
? spaces
: get_spaces (str
);
17817 return error_mark_node
;
17823 /* Explicitly instantiate DECL. */
17826 do_decl_instantiation (tree decl
, tree storage
)
17828 tree result
= NULL_TREE
;
17831 if (!decl
|| decl
== error_mark_node
)
17832 /* An error occurred, for which grokdeclarator has already issued
17833 an appropriate message. */
17835 else if (! DECL_LANG_SPECIFIC (decl
))
17837 error ("explicit instantiation of non-template %q#D", decl
);
17840 else if (TREE_CODE (decl
) == VAR_DECL
)
17842 /* There is an asymmetry here in the way VAR_DECLs and
17843 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17844 the latter, the DECL we get back will be marked as a
17845 template instantiation, and the appropriate
17846 DECL_TEMPLATE_INFO will be set up. This does not happen for
17847 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17848 should handle VAR_DECLs as it currently handles
17850 if (!DECL_CLASS_SCOPE_P (decl
))
17852 error ("%qD is not a static data member of a class template", decl
);
17855 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17856 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17858 error ("no matching template for %qD found", decl
);
17861 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17863 error ("type %qT for explicit instantiation %qD does not match "
17864 "declared type %qT", TREE_TYPE (result
), decl
,
17869 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17871 error ("explicit instantiation of %q#D", decl
);
17877 /* Check for various error cases. Note that if the explicit
17878 instantiation is valid the RESULT will currently be marked as an
17879 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17880 until we get here. */
17882 if (DECL_TEMPLATE_SPECIALIZATION (result
))
17884 /* DR 259 [temp.spec].
17886 Both an explicit instantiation and a declaration of an explicit
17887 specialization shall not appear in a program unless the explicit
17888 instantiation follows a declaration of the explicit specialization.
17890 For a given set of template parameters, if an explicit
17891 instantiation of a template appears after a declaration of an
17892 explicit specialization for that template, the explicit
17893 instantiation has no effect. */
17896 else if (DECL_EXPLICIT_INSTANTIATION (result
))
17900 No program shall explicitly instantiate any template more
17903 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17904 the first instantiation was `extern' and the second is not,
17905 and EXTERN_P for the opposite case. */
17906 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
17907 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
17908 /* If an "extern" explicit instantiation follows an ordinary
17909 explicit instantiation, the template is instantiated. */
17913 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
17915 error ("no matching template for %qD found", result
);
17918 else if (!DECL_TEMPLATE_INFO (result
))
17920 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
17924 if (storage
== NULL_TREE
)
17926 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17928 if (!in_system_header
&& (cxx_dialect
== cxx98
))
17929 pedwarn (input_location
, OPT_Wpedantic
,
17930 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17935 error ("storage class %qD applied to template instantiation", storage
);
17937 check_explicit_instantiation_namespace (result
);
17938 mark_decl_instantiated (result
, extern_p
);
17940 instantiate_decl (result
, /*defer_ok=*/1,
17941 /*expl_inst_class_mem_p=*/false);
17945 mark_class_instantiated (tree t
, int extern_p
)
17947 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
17948 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
17949 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
17950 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
17953 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
17954 rest_of_type_compilation (t
, 1);
17958 /* Called from do_type_instantiation through binding_table_foreach to
17959 do recursive instantiation for the type bound in ENTRY. */
17961 bt_instantiate_type_proc (binding_entry entry
, void *data
)
17963 tree storage
= *(tree
*) data
;
17965 if (MAYBE_CLASS_TYPE_P (entry
->type
)
17966 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
17967 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
17970 /* Called from do_type_instantiation to instantiate a member
17971 (a member function or a static member variable) of an
17972 explicitly instantiated class template. */
17974 instantiate_class_member (tree decl
, int extern_p
)
17976 mark_decl_instantiated (decl
, extern_p
);
17978 instantiate_decl (decl
, /*defer_ok=*/1,
17979 /*expl_inst_class_mem_p=*/true);
17982 /* Perform an explicit instantiation of template class T. STORAGE, if
17983 non-null, is the RID for extern, inline or static. COMPLAIN is
17984 nonzero if this is called from the parser, zero if called recursively,
17985 since the standard is unclear (as detailed below). */
17988 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
17993 int previous_instantiation_extern_p
= 0;
17995 if (TREE_CODE (t
) == TYPE_DECL
)
17998 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
18001 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
18003 error ("explicit instantiation of non-class template %qD", tmpl
);
18005 error ("explicit instantiation of non-template type %qT", t
);
18011 if (!COMPLETE_TYPE_P (t
))
18013 if (complain
& tf_error
)
18014 error ("explicit instantiation of %q#T before definition of template",
18019 if (storage
!= NULL_TREE
)
18021 if (!in_system_header
)
18023 if (storage
== ridpointers
[(int) RID_EXTERN
])
18025 if (cxx_dialect
== cxx98
)
18026 pedwarn (input_location
, OPT_Wpedantic
,
18027 "ISO C++ 1998 forbids the use of %<extern%> on "
18028 "explicit instantiations");
18031 pedwarn (input_location
, OPT_Wpedantic
,
18032 "ISO C++ forbids the use of %qE"
18033 " on explicit instantiations", storage
);
18036 if (storage
== ridpointers
[(int) RID_INLINE
])
18038 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18040 else if (storage
== ridpointers
[(int) RID_STATIC
])
18044 error ("storage class %qD applied to template instantiation",
18050 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18052 /* DR 259 [temp.spec].
18054 Both an explicit instantiation and a declaration of an explicit
18055 specialization shall not appear in a program unless the explicit
18056 instantiation follows a declaration of the explicit specialization.
18058 For a given set of template parameters, if an explicit
18059 instantiation of a template appears after a declaration of an
18060 explicit specialization for that template, the explicit
18061 instantiation has no effect. */
18064 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18068 No program shall explicitly instantiate any template more
18071 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18072 instantiation was `extern'. If EXTERN_P then the second is.
18073 These cases are OK. */
18074 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18076 if (!previous_instantiation_extern_p
&& !extern_p
18077 && (complain
& tf_error
))
18078 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18080 /* If we've already instantiated the template, just return now. */
18081 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18085 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18086 mark_class_instantiated (t
, extern_p
);
18094 /* In contrast to implicit instantiation, where only the
18095 declarations, and not the definitions, of members are
18096 instantiated, we have here:
18100 The explicit instantiation of a class template specialization
18101 implies the instantiation of all of its members not
18102 previously explicitly specialized in the translation unit
18103 containing the explicit instantiation.
18105 Of course, we can't instantiate member template classes, since
18106 we don't have any arguments for them. Note that the standard
18107 is unclear on whether the instantiation of the members are
18108 *explicit* instantiations or not. However, the most natural
18109 interpretation is that it should be an explicit instantiation. */
18112 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18113 if (TREE_CODE (tmp
) == FUNCTION_DECL
18114 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18115 instantiate_class_member (tmp
, extern_p
);
18117 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18118 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
18119 instantiate_class_member (tmp
, extern_p
);
18121 if (CLASSTYPE_NESTED_UTDS (t
))
18122 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18123 bt_instantiate_type_proc
, &storage
);
18127 /* Given a function DECL, which is a specialization of TMPL, modify
18128 DECL to be a re-instantiation of TMPL with the same template
18129 arguments. TMPL should be the template into which tsubst'ing
18130 should occur for DECL, not the most general template.
18132 One reason for doing this is a scenario like this:
18135 void f(const T&, int i);
18137 void g() { f(3, 7); }
18140 void f(const T& t, const int i) { }
18142 Note that when the template is first instantiated, with
18143 instantiate_template, the resulting DECL will have no name for the
18144 first parameter, and the wrong type for the second. So, when we go
18145 to instantiate the DECL, we regenerate it. */
18148 regenerate_decl_from_template (tree decl
, tree tmpl
)
18150 /* The arguments used to instantiate DECL, from the most general
18155 args
= DECL_TI_ARGS (decl
);
18156 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18158 /* Make sure that we can see identifiers, and compute access
18160 push_access_scope (decl
);
18162 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18170 args_depth
= TMPL_ARGS_DEPTH (args
);
18171 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18172 if (args_depth
> parms_depth
)
18173 args
= get_innermost_template_args (args
, parms_depth
);
18175 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18176 args
, tf_error
, NULL_TREE
,
18177 /*defer_ok*/false);
18178 if (specs
&& specs
!= error_mark_node
)
18179 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18182 /* Merge parameter declarations. */
18183 decl_parm
= skip_artificial_parms_for (decl
,
18184 DECL_ARGUMENTS (decl
));
18186 = skip_artificial_parms_for (code_pattern
,
18187 DECL_ARGUMENTS (code_pattern
));
18188 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18193 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18194 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18195 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18197 parm_type
= type_decays_to (parm_type
);
18198 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18199 TREE_TYPE (decl_parm
) = parm_type
;
18200 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18201 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18203 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18204 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18206 decl_parm
= DECL_CHAIN (decl_parm
);
18207 pattern_parm
= DECL_CHAIN (pattern_parm
);
18209 /* Merge any parameters that match with the function parameter
18211 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18214 tree expanded_types
;
18215 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18216 the parameters in this function parameter pack. */
18217 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18218 args
, tf_error
, NULL_TREE
);
18219 len
= TREE_VEC_LENGTH (expanded_types
);
18220 for (i
= 0; i
< len
; i
++)
18225 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18226 /* Rename the parameter to include the index. */
18227 DECL_NAME (decl_parm
) =
18228 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18229 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18230 parm_type
= type_decays_to (parm_type
);
18231 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18232 TREE_TYPE (decl_parm
) = parm_type
;
18233 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18234 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18236 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18237 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18239 decl_parm
= DECL_CHAIN (decl_parm
);
18242 /* Merge additional specifiers from the CODE_PATTERN. */
18243 if (DECL_DECLARED_INLINE_P (code_pattern
)
18244 && !DECL_DECLARED_INLINE_P (decl
))
18245 DECL_DECLARED_INLINE_P (decl
) = 1;
18247 else if (TREE_CODE (decl
) == VAR_DECL
)
18249 DECL_INITIAL (decl
) =
18250 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18251 tf_error
, DECL_TI_TEMPLATE (decl
),
18252 /*integral_constant_expression_p=*/false);
18253 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18254 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18255 tf_error
, DECL_TI_TEMPLATE (decl
));
18258 gcc_unreachable ();
18260 pop_access_scope (decl
);
18263 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18264 substituted to get DECL. */
18267 template_for_substitution (tree decl
)
18269 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18271 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18272 for the instantiation. This is not always the most general
18273 template. Consider, for example:
18276 struct S { template <class U> void f();
18277 template <> void f<int>(); };
18279 and an instantiation of S<double>::f<int>. We want TD to be the
18280 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18281 while (/* An instantiation cannot have a definition, so we need a
18282 more general template. */
18283 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18284 /* We must also deal with friend templates. Given:
18286 template <class T> struct S {
18287 template <class U> friend void f() {};
18290 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18291 so far as the language is concerned, but that's still
18292 where we get the pattern for the instantiation from. On
18293 other hand, if the definition comes outside the class, say:
18295 template <class T> struct S {
18296 template <class U> friend void f();
18298 template <class U> friend void f() {}
18300 we don't need to look any further. That's what the check for
18301 DECL_INITIAL is for. */
18302 || (TREE_CODE (decl
) == FUNCTION_DECL
18303 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18304 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18306 /* The present template, TD, should not be a definition. If it
18307 were a definition, we should be using it! Note that we
18308 cannot restructure the loop to just keep going until we find
18309 a template with a definition, since that might go too far if
18310 a specialization was declared, but not defined. */
18311 gcc_assert (TREE_CODE (decl
) != VAR_DECL
18312 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18314 /* Fetch the more general template. */
18315 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18321 /* Returns true if we need to instantiate this template instance even if we
18322 know we aren't going to emit it.. */
18325 always_instantiate_p (tree decl
)
18327 /* We always instantiate inline functions so that we can inline them. An
18328 explicit instantiation declaration prohibits implicit instantiation of
18329 non-inline functions. With high levels of optimization, we would
18330 normally inline non-inline functions -- but we're not allowed to do
18331 that for "extern template" functions. Therefore, we check
18332 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18333 return ((TREE_CODE (decl
) == FUNCTION_DECL
18334 && (DECL_DECLARED_INLINE_P (decl
)
18335 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl
)))))
18336 /* And we need to instantiate static data members so that
18337 their initializers are available in integral constant
18339 || (TREE_CODE (decl
) == VAR_DECL
18340 && decl_maybe_constant_var_p (decl
)));
18343 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18344 instantiate it now, modifying TREE_TYPE (fn). */
18347 maybe_instantiate_noexcept (tree fn
)
18349 tree fntype
, spec
, noex
, clone
;
18351 if (DECL_CLONED_FUNCTION_P (fn
))
18352 fn
= DECL_CLONED_FUNCTION (fn
);
18353 fntype
= TREE_TYPE (fn
);
18354 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18356 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18359 noex
= TREE_PURPOSE (spec
);
18361 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18363 if (push_tinst_level (fn
))
18365 push_access_scope (fn
);
18366 input_location
= DECL_SOURCE_LOCATION (fn
);
18367 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18368 DEFERRED_NOEXCEPT_ARGS (noex
),
18369 tf_warning_or_error
, fn
,
18370 /*function_p=*/false,
18371 /*integral_constant_expression_p=*/true);
18372 pop_access_scope (fn
);
18373 pop_tinst_level ();
18374 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18375 if (spec
== error_mark_node
)
18376 spec
= noexcept_false_spec
;
18379 spec
= noexcept_false_spec
;
18383 /* This is an implicitly declared function, so NOEX is a list of
18384 other functions to evaluate and merge. */
18386 spec
= noexcept_true_spec
;
18387 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18389 tree fn
= OVL_CURRENT (elt
);
18391 maybe_instantiate_noexcept (fn
);
18392 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18393 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18397 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18399 FOR_EACH_CLONE (clone
, fn
)
18401 if (TREE_TYPE (clone
) == fntype
)
18402 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18404 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18408 /* Produce the definition of D, a _DECL generated from a template. If
18409 DEFER_OK is nonzero, then we don't have to actually do the
18410 instantiation now; we just have to do it sometime. Normally it is
18411 an error if this is an explicit instantiation but D is undefined.
18412 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18413 explicitly instantiated class template. */
18416 instantiate_decl (tree d
, int defer_ok
,
18417 bool expl_inst_class_mem_p
)
18419 tree tmpl
= DECL_TI_TEMPLATE (d
);
18426 bool pattern_defined
;
18428 location_t saved_loc
= input_location
;
18431 /* This function should only be used to instantiate templates for
18432 functions and static member variables. */
18433 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18434 || TREE_CODE (d
) == VAR_DECL
);
18436 /* Variables are never deferred; if instantiation is required, they
18437 are instantiated right away. That allows for better code in the
18438 case that an expression refers to the value of the variable --
18439 if the variable has a constant value the referring expression can
18440 take advantage of that fact. */
18441 if (TREE_CODE (d
) == VAR_DECL
18442 || DECL_DECLARED_CONSTEXPR_P (d
))
18445 /* Don't instantiate cloned functions. Instead, instantiate the
18446 functions they cloned. */
18447 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18448 d
= DECL_CLONED_FUNCTION (d
);
18450 if (DECL_TEMPLATE_INSTANTIATED (d
)
18451 || (TREE_CODE (d
) == FUNCTION_DECL
18452 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18453 || DECL_TEMPLATE_SPECIALIZATION (d
))
18454 /* D has already been instantiated or explicitly specialized, so
18455 there's nothing for us to do here.
18457 It might seem reasonable to check whether or not D is an explicit
18458 instantiation, and, if so, stop here. But when an explicit
18459 instantiation is deferred until the end of the compilation,
18460 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18461 the instantiation. */
18464 /* Check to see whether we know that this template will be
18465 instantiated in some other file, as with "extern template"
18467 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18469 /* In general, we do not instantiate such templates. */
18470 if (external_p
&& !always_instantiate_p (d
))
18473 gen_tmpl
= most_general_template (tmpl
);
18474 gen_args
= DECL_TI_ARGS (d
);
18476 if (tmpl
!= gen_tmpl
)
18477 /* We should already have the extra args. */
18478 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18479 == TMPL_ARGS_DEPTH (gen_args
));
18480 /* And what's in the hash table should match D. */
18481 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18482 || spec
== NULL_TREE
);
18484 /* This needs to happen before any tsubsting. */
18485 if (! push_tinst_level (d
))
18488 timevar_push (TV_TEMPLATE_INST
);
18490 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18491 for the instantiation. */
18492 td
= template_for_substitution (d
);
18493 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18495 /* We should never be trying to instantiate a member of a class
18496 template or partial specialization. */
18497 gcc_assert (d
!= code_pattern
);
18499 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18500 || DECL_TEMPLATE_SPECIALIZATION (td
))
18501 /* In the case of a friend template whose definition is provided
18502 outside the class, we may have too many arguments. Drop the
18503 ones we don't need. The same is true for specializations. */
18504 args
= get_innermost_template_args
18505 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18509 if (TREE_CODE (d
) == FUNCTION_DECL
)
18510 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18511 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18513 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18515 /* We may be in the middle of deferred access check. Disable it now. */
18516 push_deferring_access_checks (dk_no_deferred
);
18518 /* Unless an explicit instantiation directive has already determined
18519 the linkage of D, remember that a definition is available for
18521 if (pattern_defined
18522 && !DECL_INTERFACE_KNOWN (d
)
18523 && !DECL_NOT_REALLY_EXTERN (d
))
18524 mark_definable (d
);
18526 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18527 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18528 input_location
= DECL_SOURCE_LOCATION (d
);
18530 /* If D is a member of an explicitly instantiated class template,
18531 and no definition is available, treat it like an implicit
18533 if (!pattern_defined
&& expl_inst_class_mem_p
18534 && DECL_EXPLICIT_INSTANTIATION (d
))
18536 /* Leave linkage flags alone on instantiations with anonymous
18538 if (TREE_PUBLIC (d
))
18540 DECL_NOT_REALLY_EXTERN (d
) = 0;
18541 DECL_INTERFACE_KNOWN (d
) = 0;
18543 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18546 if (TREE_CODE (d
) == FUNCTION_DECL
)
18547 maybe_instantiate_noexcept (d
);
18549 /* Recheck the substitutions to obtain any warning messages
18550 about ignoring cv qualifiers. Don't do this for artificial decls,
18551 as it breaks the context-sensitive substitution for lambda op(). */
18552 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
18554 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18555 tree type
= TREE_TYPE (gen
);
18557 /* Make sure that we can see identifiers, and compute access
18558 correctly. D is already the target FUNCTION_DECL with the
18560 push_access_scope (d
);
18562 if (TREE_CODE (gen
) == FUNCTION_DECL
)
18564 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
18565 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
18566 d
, /*defer_ok*/true);
18567 /* Don't simply tsubst the function type, as that will give
18568 duplicate warnings about poor parameter qualifications.
18569 The function arguments are the same as the decl_arguments
18570 without the top level cv qualifiers. */
18571 type
= TREE_TYPE (type
);
18573 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
18575 pop_access_scope (d
);
18578 /* Defer all other templates, unless we have been explicitly
18579 forbidden from doing so. */
18580 if (/* If there is no definition, we cannot instantiate the
18583 /* If it's OK to postpone instantiation, do so. */
18585 /* If this is a static data member that will be defined
18586 elsewhere, we don't want to instantiate the entire data
18587 member, but we do want to instantiate the initializer so that
18588 we can substitute that elsewhere. */
18589 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18591 /* The definition of the static data member is now required so
18592 we must substitute the initializer. */
18593 if (TREE_CODE (d
) == VAR_DECL
18594 && !DECL_INITIAL (d
)
18595 && DECL_INITIAL (code_pattern
))
18599 bool const_init
= false;
18601 ns
= decl_namespace_context (d
);
18602 push_nested_namespace (ns
);
18603 push_nested_class (DECL_CONTEXT (d
));
18604 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18606 tf_warning_or_error
, NULL_TREE
,
18607 /*integral_constant_expression_p=*/false);
18608 /* Make sure the initializer is still constant, in case of
18609 circular dependency (template/instantiate6.C). */
18611 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18612 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18613 /*asmspec_tree=*/NULL_TREE
,
18614 LOOKUP_ONLYCONVERTING
);
18615 pop_nested_class ();
18616 pop_nested_namespace (ns
);
18619 /* We restore the source position here because it's used by
18620 add_pending_template. */
18621 input_location
= saved_loc
;
18623 if (at_eof
&& !pattern_defined
18624 && DECL_EXPLICIT_INSTANTIATION (d
)
18625 && DECL_NOT_REALLY_EXTERN (d
))
18628 The definition of a non-exported function template, a
18629 non-exported member function template, or a non-exported
18630 member function or static data member of a class template
18631 shall be present in every translation unit in which it is
18632 explicitly instantiated. */
18633 permerror (input_location
, "explicit instantiation of %qD "
18634 "but no definition available", d
);
18636 /* If we're in unevaluated context, we just wanted to get the
18637 constant value; this isn't an odr use, so don't queue
18638 a full instantiation. */
18639 if (cp_unevaluated_operand
!= 0)
18641 /* ??? Historically, we have instantiated inline functions, even
18642 when marked as "extern template". */
18643 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18644 add_pending_template (d
);
18647 /* Tell the repository that D is available in this translation unit
18648 -- and see if it is supposed to be instantiated here. */
18649 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18651 /* In a PCH file, despite the fact that the repository hasn't
18652 requested instantiation in the PCH it is still possible that
18653 an instantiation will be required in a file that includes the
18656 add_pending_template (d
);
18657 /* Instantiate inline functions so that the inliner can do its
18658 job, even though we'll not be emitting a copy of this
18660 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18664 need_push
= !cfun
|| !global_bindings_p ();
18666 push_to_top_level ();
18668 /* Mark D as instantiated so that recursive calls to
18669 instantiate_decl do not try to instantiate it again. */
18670 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18672 /* Regenerate the declaration in case the template has been modified
18673 by a subsequent redeclaration. */
18674 regenerate_decl_from_template (d
, td
);
18676 /* We already set the file and line above. Reset them now in case
18677 they changed as a result of calling regenerate_decl_from_template. */
18678 input_location
= DECL_SOURCE_LOCATION (d
);
18680 if (TREE_CODE (d
) == VAR_DECL
)
18683 bool const_init
= false;
18685 /* Clear out DECL_RTL; whatever was there before may not be right
18686 since we've reset the type of the declaration. */
18687 SET_DECL_RTL (d
, NULL
);
18688 DECL_IN_AGGR_P (d
) = 0;
18690 /* The initializer is placed in DECL_INITIAL by
18691 regenerate_decl_from_template so we don't need to
18692 push/pop_access_scope again here. Pull it out so that
18693 cp_finish_decl can process it. */
18694 init
= DECL_INITIAL (d
);
18695 DECL_INITIAL (d
) = NULL_TREE
;
18696 DECL_INITIALIZED_P (d
) = 0;
18698 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18699 initializer. That function will defer actual emission until
18700 we have a chance to determine linkage. */
18701 DECL_EXTERNAL (d
) = 0;
18703 /* Enter the scope of D so that access-checking works correctly. */
18704 push_nested_class (DECL_CONTEXT (d
));
18705 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18706 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18707 pop_nested_class ();
18709 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18710 synthesize_method (d
);
18711 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18713 struct pointer_map_t
*saved_local_specializations
;
18718 /* Save away the current list, in case we are instantiating one
18719 template from within the body of another. */
18720 saved_local_specializations
= local_specializations
;
18722 /* Set up the list of local specializations. */
18723 local_specializations
= pointer_map_create ();
18725 /* Set up context. */
18726 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18728 /* Create substitution entries for the parameters. */
18729 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18730 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18731 spec_parm
= DECL_ARGUMENTS (d
);
18732 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18734 register_local_specialization (spec_parm
, tmpl_parm
);
18735 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18736 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18738 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18740 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18742 register_local_specialization (spec_parm
, tmpl_parm
);
18743 spec_parm
= DECL_CHAIN (spec_parm
);
18747 /* Register the (value) argument pack as a specialization of
18748 TMPL_PARM, then move on. */
18749 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18750 register_local_specialization (argpack
, tmpl_parm
);
18753 gcc_assert (!spec_parm
);
18755 /* Substitute into the body of the function. */
18756 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18757 tf_warning_or_error
, tmpl
,
18758 /*integral_constant_expression_p=*/false);
18760 /* Set the current input_location to the end of the function
18761 so that finish_function knows where we are. */
18762 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18764 /* We don't need the local specializations any more. */
18765 pointer_map_destroy (local_specializations
);
18766 local_specializations
= saved_local_specializations
;
18768 /* Finish the function. */
18769 d
= finish_function (0);
18770 expand_or_defer_fn (d
);
18773 /* We're not deferring instantiation any more. */
18774 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18777 pop_from_top_level ();
18780 input_location
= saved_loc
;
18781 pop_deferring_access_checks ();
18782 pop_tinst_level ();
18784 timevar_pop (TV_TEMPLATE_INST
);
18789 /* Run through the list of templates that we wish we could
18790 instantiate, and instantiate any we can. RETRIES is the
18791 number of times we retry pending template instantiation. */
18794 instantiate_pending_templates (int retries
)
18797 location_t saved_loc
= input_location
;
18799 /* Instantiating templates may trigger vtable generation. This in turn
18800 may require further template instantiations. We place a limit here
18801 to avoid infinite loop. */
18802 if (pending_templates
&& retries
>= max_tinst_depth
)
18804 tree decl
= pending_templates
->tinst
->decl
;
18806 error ("template instantiation depth exceeds maximum of %d"
18807 " instantiating %q+D, possibly from virtual table generation"
18808 " (use -ftemplate-depth= to increase the maximum)",
18809 max_tinst_depth
, decl
);
18810 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18811 /* Pretend that we defined it. */
18812 DECL_INITIAL (decl
) = error_mark_node
;
18818 struct pending_template
**t
= &pending_templates
;
18819 struct pending_template
*last
= NULL
;
18823 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18824 bool complete
= false;
18826 if (TYPE_P (instantiation
))
18830 if (!COMPLETE_TYPE_P (instantiation
))
18832 instantiate_class_template (instantiation
);
18833 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18834 for (fn
= TYPE_METHODS (instantiation
);
18836 fn
= TREE_CHAIN (fn
))
18837 if (! DECL_ARTIFICIAL (fn
))
18838 instantiate_decl (fn
,
18840 /*expl_inst_class_mem_p=*/false);
18841 if (COMPLETE_TYPE_P (instantiation
))
18845 complete
= COMPLETE_TYPE_P (instantiation
);
18849 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18850 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18853 = instantiate_decl (instantiation
,
18855 /*expl_inst_class_mem_p=*/false);
18856 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18860 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18861 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18865 /* If INSTANTIATION has been instantiated, then we don't
18866 need to consider it again in the future. */
18874 current_tinst_level
= NULL
;
18876 last_pending_template
= last
;
18878 while (reconsider
);
18880 input_location
= saved_loc
;
18883 /* Substitute ARGVEC into T, which is a list of initializers for
18884 either base class or a non-static data member. The TREE_PURPOSEs
18885 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18886 instantiate_decl. */
18889 tsubst_initializer_list (tree t
, tree argvec
)
18891 tree inits
= NULL_TREE
;
18893 for (; t
; t
= TREE_CHAIN (t
))
18897 tree expanded_bases
= NULL_TREE
;
18898 tree expanded_arguments
= NULL_TREE
;
18901 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
18906 /* Expand the base class expansion type into separate base
18908 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
18909 tf_warning_or_error
,
18911 if (expanded_bases
== error_mark_node
)
18914 /* We'll be building separate TREE_LISTs of arguments for
18916 len
= TREE_VEC_LENGTH (expanded_bases
);
18917 expanded_arguments
= make_tree_vec (len
);
18918 for (i
= 0; i
< len
; i
++)
18919 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
18921 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18922 expand each argument in the TREE_VALUE of t. */
18923 expr
= make_node (EXPR_PACK_EXPANSION
);
18924 PACK_EXPANSION_LOCAL_P (expr
) = true;
18925 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
18926 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
18928 if (TREE_VALUE (t
) == void_type_node
)
18929 /* VOID_TYPE_NODE is used to indicate
18930 value-initialization. */
18932 for (i
= 0; i
< len
; i
++)
18933 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
18937 /* Substitute parameter packs into each argument in the
18939 in_base_initializer
= 1;
18940 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
18942 tree expanded_exprs
;
18944 /* Expand the argument. */
18945 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
18947 = tsubst_pack_expansion (expr
, argvec
,
18948 tf_warning_or_error
,
18950 if (expanded_exprs
== error_mark_node
)
18953 /* Prepend each of the expanded expressions to the
18954 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18955 for (i
= 0; i
< len
; i
++)
18957 TREE_VEC_ELT (expanded_arguments
, i
) =
18958 tree_cons (NULL_TREE
,
18959 TREE_VEC_ELT (expanded_exprs
, i
),
18960 TREE_VEC_ELT (expanded_arguments
, i
));
18963 in_base_initializer
= 0;
18965 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18966 since we built them backwards. */
18967 for (i
= 0; i
< len
; i
++)
18969 TREE_VEC_ELT (expanded_arguments
, i
) =
18970 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
18975 for (i
= 0; i
< len
; ++i
)
18977 if (expanded_bases
)
18979 decl
= TREE_VEC_ELT (expanded_bases
, i
);
18980 decl
= expand_member_init (decl
);
18981 init
= TREE_VEC_ELT (expanded_arguments
, i
);
18986 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
18987 tf_warning_or_error
, NULL_TREE
);
18989 decl
= expand_member_init (decl
);
18990 if (decl
&& !DECL_P (decl
))
18991 in_base_initializer
= 1;
18993 init
= TREE_VALUE (t
);
18995 if (init
!= void_type_node
)
18996 init
= tsubst_expr (init
, argvec
,
18997 tf_warning_or_error
, NULL_TREE
,
18998 /*integral_constant_expression_p=*/false);
18999 if (init
== NULL_TREE
&& tmp
!= NULL_TREE
)
19000 /* If we had an initializer but it instantiated to nothing,
19001 value-initialize the object. This will only occur when
19002 the initializer was a pack expansion where the parameter
19003 packs used in that expansion were of length zero. */
19004 init
= void_type_node
;
19005 in_base_initializer
= 0;
19010 init
= build_tree_list (decl
, init
);
19011 TREE_CHAIN (init
) = inits
;
19019 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
19022 set_current_access_from_decl (tree decl
)
19024 if (TREE_PRIVATE (decl
))
19025 current_access_specifier
= access_private_node
;
19026 else if (TREE_PROTECTED (decl
))
19027 current_access_specifier
= access_protected_node
;
19029 current_access_specifier
= access_public_node
;
19032 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
19033 is the instantiation (which should have been created with
19034 start_enum) and ARGS are the template arguments to use. */
19037 tsubst_enum (tree tag
, tree newtag
, tree args
)
19041 if (SCOPED_ENUM_P (newtag
))
19042 begin_scope (sk_scoped_enum
, newtag
);
19044 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19049 decl
= TREE_VALUE (e
);
19050 /* Note that in a template enum, the TREE_VALUE is the
19051 CONST_DECL, not the corresponding INTEGER_CST. */
19052 value
= tsubst_expr (DECL_INITIAL (decl
),
19053 args
, tf_warning_or_error
, NULL_TREE
,
19054 /*integral_constant_expression_p=*/true);
19056 /* Give this enumeration constant the correct access. */
19057 set_current_access_from_decl (decl
);
19059 /* Actually build the enumerator itself. */
19061 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19064 if (SCOPED_ENUM_P (newtag
))
19067 finish_enum_value_list (newtag
);
19068 finish_enum (newtag
);
19070 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19071 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19074 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19075 its type -- but without substituting the innermost set of template
19076 arguments. So, innermost set of template parameters will appear in
19080 get_mostly_instantiated_function_type (tree decl
)
19088 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19089 targs
= DECL_TI_ARGS (decl
);
19090 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19091 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19093 /* There should be as many levels of arguments as there are levels
19095 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19097 fn_type
= TREE_TYPE (tmpl
);
19099 if (parm_depth
== 1)
19100 /* No substitution is necessary. */
19107 /* Replace the innermost level of the TARGS with NULL_TREEs to
19108 let tsubst know not to substitute for those parameters. */
19109 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19110 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19111 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19112 TMPL_ARGS_LEVEL (targs
, i
));
19113 SET_TMPL_ARGS_LEVEL (partial_args
,
19114 TMPL_ARGS_DEPTH (targs
),
19115 make_tree_vec (DECL_NTPARMS (tmpl
)));
19117 /* Make sure that we can see identifiers, and compute access
19119 push_access_scope (decl
);
19121 ++processing_template_decl
;
19122 /* Now, do the (partial) substitution to figure out the
19123 appropriate function type. */
19124 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
19125 --processing_template_decl
;
19127 /* Substitute into the template parameters to obtain the real
19128 innermost set of parameters. This step is important if the
19129 innermost set of template parameters contains value
19130 parameters whose types depend on outer template parameters. */
19131 TREE_VEC_LENGTH (partial_args
)--;
19132 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
19134 pop_access_scope (decl
);
19140 /* Return truthvalue if we're processing a template different from
19141 the last one involved in diagnostics. */
19143 problematic_instantiation_changed (void)
19145 return current_tinst_level
!= last_error_tinst_level
;
19148 /* Remember current template involved in diagnostics. */
19150 record_last_problematic_instantiation (void)
19152 last_error_tinst_level
= current_tinst_level
;
19155 struct tinst_level
*
19156 current_instantiation (void)
19158 return current_tinst_level
;
19161 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19162 type. Return zero for ok, nonzero for disallowed. Issue error and
19163 warning messages under control of COMPLAIN. */
19166 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19168 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19170 else if (POINTER_TYPE_P (type
))
19172 else if (TYPE_PTR_TO_MEMBER_P (type
))
19174 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19176 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19178 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19180 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19183 if (complain
& tf_error
)
19185 if (type
== error_mark_node
)
19186 inform (input_location
, "invalid template non-type parameter");
19188 error ("%q#T is not a valid type for a template non-type parameter",
19194 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19195 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19198 dependent_type_p_r (tree type
)
19204 A type is dependent if it is:
19206 -- a template parameter. Template template parameters are types
19207 for us (since TYPE_P holds true for them) so we handle
19209 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19210 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19212 /* -- a qualified-id with a nested-name-specifier which contains a
19213 class-name that names a dependent type or whose unqualified-id
19214 names a dependent type. */
19215 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19217 /* -- a cv-qualified type where the cv-unqualified type is
19219 type
= TYPE_MAIN_VARIANT (type
);
19220 /* -- a compound type constructed from any dependent type. */
19221 if (TYPE_PTR_TO_MEMBER_P (type
))
19222 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19223 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19225 else if (TREE_CODE (type
) == POINTER_TYPE
19226 || TREE_CODE (type
) == REFERENCE_TYPE
)
19227 return dependent_type_p (TREE_TYPE (type
));
19228 else if (TREE_CODE (type
) == FUNCTION_TYPE
19229 || TREE_CODE (type
) == METHOD_TYPE
)
19233 if (dependent_type_p (TREE_TYPE (type
)))
19235 for (arg_type
= TYPE_ARG_TYPES (type
);
19237 arg_type
= TREE_CHAIN (arg_type
))
19238 if (dependent_type_p (TREE_VALUE (arg_type
)))
19242 /* -- an array type constructed from any dependent type or whose
19243 size is specified by a constant expression that is
19246 We checked for type- and value-dependence of the bounds in
19247 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19248 if (TREE_CODE (type
) == ARRAY_TYPE
)
19250 if (TYPE_DOMAIN (type
)
19251 && dependent_type_p (TYPE_DOMAIN (type
)))
19253 return dependent_type_p (TREE_TYPE (type
));
19256 /* -- a template-id in which either the template name is a template
19258 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19260 /* ... or any of the template arguments is a dependent type or
19261 an expression that is type-dependent or value-dependent. */
19262 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19263 && (any_dependent_template_arguments_p
19264 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19267 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19268 dependent; if the argument of the `typeof' expression is not
19269 type-dependent, then it should already been have resolved. */
19270 if (TREE_CODE (type
) == TYPEOF_TYPE
19271 || TREE_CODE (type
) == DECLTYPE_TYPE
19272 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19275 /* A template argument pack is dependent if any of its packed
19277 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19279 tree args
= ARGUMENT_PACK_ARGS (type
);
19280 int i
, len
= TREE_VEC_LENGTH (args
);
19281 for (i
= 0; i
< len
; ++i
)
19282 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19286 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19287 be template parameters. */
19288 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19291 /* The standard does not specifically mention types that are local
19292 to template functions or local classes, but they should be
19293 considered dependent too. For example:
19295 template <int I> void f() {
19300 The size of `E' cannot be known until the value of `I' has been
19301 determined. Therefore, `E' must be considered dependent. */
19302 scope
= TYPE_CONTEXT (type
);
19303 if (scope
&& TYPE_P (scope
))
19304 return dependent_type_p (scope
);
19305 /* Don't use type_dependent_expression_p here, as it can lead
19306 to infinite recursion trying to determine whether a lambda
19307 nested in a lambda is dependent (c++/47687). */
19308 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19309 && DECL_LANG_SPECIFIC (scope
)
19310 && DECL_TEMPLATE_INFO (scope
)
19311 && (any_dependent_template_arguments_p
19312 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19315 /* Other types are non-dependent. */
19319 /* Returns TRUE if TYPE is dependent, in the sense of
19320 [temp.dep.type]. Note that a NULL type is considered dependent. */
19323 dependent_type_p (tree type
)
19325 /* If there are no template parameters in scope, then there can't be
19326 any dependent types. */
19327 if (!processing_template_decl
)
19329 /* If we are not processing a template, then nobody should be
19330 providing us with a dependent type. */
19332 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19336 /* If the type is NULL, we have not computed a type for the entity
19337 in question; in that case, the type is dependent. */
19341 /* Erroneous types can be considered non-dependent. */
19342 if (type
== error_mark_node
)
19345 /* If we have not already computed the appropriate value for TYPE,
19347 if (!TYPE_DEPENDENT_P_VALID (type
))
19349 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19350 TYPE_DEPENDENT_P_VALID (type
) = 1;
19353 return TYPE_DEPENDENT_P (type
);
19356 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19357 lookup. In other words, a dependent type that is not the current
19361 dependent_scope_p (tree scope
)
19363 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19364 && !currently_open_class (scope
));
19367 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19368 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19371 /* Note that this predicate is not appropriate for general expressions;
19372 only constant expressions (that satisfy potential_constant_expression)
19373 can be tested for value dependence.
19375 We should really also have a predicate for "instantiation-dependent".
19377 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
19378 (what about instantiation-dependent constant-expressions?)
19379 is_late_template_attribute: defer if instantiation-dependent.
19380 compute_array_index_type: proceed if constant and not t- or v-dependent
19381 if instantiation-dependent, need to remember full expression
19382 uses_template_parms: FIXME - need to audit callers
19383 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
19384 dependent_type_p [array_type]: dependent if index type is dependent
19386 static_assert - instantiation-dependent */
19389 value_dependent_expression_p (tree expression
)
19391 if (!processing_template_decl
)
19394 /* A name declared with a dependent type. */
19395 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19398 switch (TREE_CODE (expression
))
19400 case IDENTIFIER_NODE
:
19401 /* A name that has not been looked up -- must be dependent. */
19404 case TEMPLATE_PARM_INDEX
:
19405 /* A non-type template parm. */
19409 /* A non-type template parm. */
19410 if (DECL_TEMPLATE_PARM_P (expression
))
19412 return value_dependent_expression_p (DECL_INITIAL (expression
));
19415 /* A constant with literal type and is initialized
19416 with an expression that is value-dependent. */
19417 if (DECL_INITIAL (expression
)
19418 && decl_constant_var_p (expression
)
19419 && value_dependent_expression_p (DECL_INITIAL (expression
)))
19423 case DYNAMIC_CAST_EXPR
:
19424 case STATIC_CAST_EXPR
:
19425 case CONST_CAST_EXPR
:
19426 case REINTERPRET_CAST_EXPR
:
19428 /* These expressions are value-dependent if the type to which
19429 the cast occurs is dependent or the expression being casted
19430 is value-dependent. */
19432 tree type
= TREE_TYPE (expression
);
19434 if (dependent_type_p (type
))
19437 /* A functional cast has a list of operands. */
19438 expression
= TREE_OPERAND (expression
, 0);
19441 /* If there are no operands, it must be an expression such
19442 as "int()". This should not happen for aggregate types
19443 because it would form non-constant expressions. */
19444 gcc_assert (cxx_dialect
>= cxx0x
19445 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19450 if (TREE_CODE (expression
) == TREE_LIST
)
19451 return any_value_dependent_elements_p (expression
);
19453 return value_dependent_expression_p (expression
);
19459 /* A `sizeof' expression is value-dependent if the operand is
19460 type-dependent or is a pack expansion. */
19461 expression
= TREE_OPERAND (expression
, 0);
19462 if (PACK_EXPANSION_P (expression
))
19464 else if (TYPE_P (expression
))
19465 return dependent_type_p (expression
);
19466 return type_dependent_expression_p (expression
);
19468 case AT_ENCODE_EXPR
:
19469 /* An 'encode' expression is value-dependent if the operand is
19471 expression
= TREE_OPERAND (expression
, 0);
19472 return dependent_type_p (expression
);
19474 case NOEXCEPT_EXPR
:
19475 expression
= TREE_OPERAND (expression
, 0);
19476 return type_dependent_expression_p (expression
);
19480 tree name
= TREE_OPERAND (expression
, 1);
19481 return value_dependent_expression_p (name
);
19484 case COMPONENT_REF
:
19485 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19486 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19488 case NONTYPE_ARGUMENT_PACK
:
19489 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19490 is value-dependent. */
19492 tree values
= ARGUMENT_PACK_ARGS (expression
);
19493 int i
, len
= TREE_VEC_LENGTH (values
);
19495 for (i
= 0; i
< len
; ++i
)
19496 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19504 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19505 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19506 || (type2
? dependent_type_p (type2
) : false));
19510 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19511 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19514 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19515 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19519 tree op
= TREE_OPERAND (expression
, 0);
19520 return (value_dependent_expression_p (op
)
19521 || has_value_dependent_address (op
));
19526 tree fn
= get_callee_fndecl (expression
);
19528 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19530 nargs
= call_expr_nargs (expression
);
19531 for (i
= 0; i
< nargs
; ++i
)
19533 tree op
= CALL_EXPR_ARG (expression
, i
);
19534 /* In a call to a constexpr member function, look through the
19535 implicit ADDR_EXPR on the object argument so that it doesn't
19536 cause the call to be considered value-dependent. We also
19537 look through it in potential_constant_expression. */
19538 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19539 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19540 && TREE_CODE (op
) == ADDR_EXPR
)
19541 op
= TREE_OPERAND (op
, 0);
19542 if (value_dependent_expression_p (op
))
19548 case TEMPLATE_ID_EXPR
:
19549 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19551 return type_dependent_expression_p (expression
);
19557 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19558 if (value_dependent_expression_p (val
))
19564 /* Treat a GNU statement expression as dependent to avoid crashing
19565 under fold_non_dependent_expr; it can't be constant. */
19569 /* A constant expression is value-dependent if any subexpression is
19570 value-dependent. */
19571 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19573 case tcc_reference
:
19575 case tcc_comparison
:
19577 case tcc_expression
:
19580 int i
, len
= cp_tree_operand_length (expression
);
19582 for (i
= 0; i
< len
; i
++)
19584 tree t
= TREE_OPERAND (expression
, i
);
19586 /* In some cases, some of the operands may be missing.l
19587 (For example, in the case of PREDECREMENT_EXPR, the
19588 amount to increment by may be missing.) That doesn't
19589 make the expression dependent. */
19590 if (t
&& value_dependent_expression_p (t
))
19601 /* The expression is not value-dependent. */
19605 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19606 [temp.dep.expr]. Note that an expression with no type is
19607 considered dependent. Other parts of the compiler arrange for an
19608 expression with type-dependent subexpressions to have no type, so
19609 this function doesn't have to be fully recursive. */
19612 type_dependent_expression_p (tree expression
)
19614 if (!processing_template_decl
)
19617 if (expression
== error_mark_node
)
19620 /* An unresolved name is always dependent. */
19621 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19622 || TREE_CODE (expression
) == USING_DECL
)
19625 /* Some expression forms are never type-dependent. */
19626 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19627 || TREE_CODE (expression
) == SIZEOF_EXPR
19628 || TREE_CODE (expression
) == ALIGNOF_EXPR
19629 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19630 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19631 || TREE_CODE (expression
) == TRAIT_EXPR
19632 || TREE_CODE (expression
) == TYPEID_EXPR
19633 || TREE_CODE (expression
) == DELETE_EXPR
19634 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19635 || TREE_CODE (expression
) == THROW_EXPR
)
19638 /* The types of these expressions depends only on the type to which
19639 the cast occurs. */
19640 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19641 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19642 || TREE_CODE (expression
) == CONST_CAST_EXPR
19643 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19644 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19645 || TREE_CODE (expression
) == CAST_EXPR
)
19646 return dependent_type_p (TREE_TYPE (expression
));
19648 /* The types of these expressions depends only on the type created
19649 by the expression. */
19650 if (TREE_CODE (expression
) == NEW_EXPR
19651 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19653 /* For NEW_EXPR tree nodes created inside a template, either
19654 the object type itself or a TREE_LIST may appear as the
19656 tree type
= TREE_OPERAND (expression
, 1);
19657 if (TREE_CODE (type
) == TREE_LIST
)
19658 /* This is an array type. We need to check array dimensions
19660 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19661 || value_dependent_expression_p
19662 (TREE_OPERAND (TREE_VALUE (type
), 1));
19664 return dependent_type_p (type
);
19667 if (TREE_CODE (expression
) == SCOPE_REF
)
19669 tree scope
= TREE_OPERAND (expression
, 0);
19670 tree name
= TREE_OPERAND (expression
, 1);
19672 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19673 contains an identifier associated by name lookup with one or more
19674 declarations declared with a dependent type, or...a
19675 nested-name-specifier or qualified-id that names a member of an
19676 unknown specialization. */
19677 return (type_dependent_expression_p (name
)
19678 || dependent_scope_p (scope
));
19681 if (TREE_CODE (expression
) == FUNCTION_DECL
19682 && DECL_LANG_SPECIFIC (expression
)
19683 && DECL_TEMPLATE_INFO (expression
)
19684 && (any_dependent_template_arguments_p
19685 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19688 if (TREE_CODE (expression
) == TEMPLATE_DECL
19689 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19692 if (TREE_CODE (expression
) == STMT_EXPR
)
19693 expression
= stmt_expr_value_expr (expression
);
19695 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19700 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19702 if (type_dependent_expression_p (elt
))
19708 /* A static data member of the current instantiation with incomplete
19709 array type is type-dependent, as the definition and specializations
19710 can have different bounds. */
19711 if (TREE_CODE (expression
) == VAR_DECL
19712 && DECL_CLASS_SCOPE_P (expression
)
19713 && dependent_type_p (DECL_CONTEXT (expression
))
19714 && VAR_HAD_UNKNOWN_BOUND (expression
))
19717 if (TREE_TYPE (expression
) == unknown_type_node
)
19719 if (TREE_CODE (expression
) == ADDR_EXPR
)
19720 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19721 if (TREE_CODE (expression
) == COMPONENT_REF
19722 || TREE_CODE (expression
) == OFFSET_REF
)
19724 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19726 expression
= TREE_OPERAND (expression
, 1);
19727 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19730 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19731 if (TREE_CODE (expression
) == SCOPE_REF
)
19734 if (BASELINK_P (expression
))
19735 expression
= BASELINK_FUNCTIONS (expression
);
19737 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19739 if (any_dependent_template_arguments_p
19740 (TREE_OPERAND (expression
, 1)))
19742 expression
= TREE_OPERAND (expression
, 0);
19744 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19745 || TREE_CODE (expression
) == FUNCTION_DECL
);
19749 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19751 expression
= OVL_NEXT (expression
);
19756 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19758 return (dependent_type_p (TREE_TYPE (expression
)));
19761 /* Like type_dependent_expression_p, but it also works while not processing
19762 a template definition, i.e. during substitution or mangling. */
19765 type_dependent_expression_p_push (tree expr
)
19768 ++processing_template_decl
;
19769 b
= type_dependent_expression_p (expr
);
19770 --processing_template_decl
;
19774 /* Returns TRUE if ARGS contains a type-dependent expression. */
19777 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
19782 FOR_EACH_VEC_ELT (tree
, args
, i
, arg
)
19784 if (type_dependent_expression_p (arg
))
19790 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19791 expressions) contains any type-dependent expressions. */
19794 any_type_dependent_elements_p (const_tree list
)
19796 for (; list
; list
= TREE_CHAIN (list
))
19797 if (value_dependent_expression_p (TREE_VALUE (list
)))
19803 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19804 expressions) contains any value-dependent expressions. */
19807 any_value_dependent_elements_p (const_tree list
)
19809 for (; list
; list
= TREE_CHAIN (list
))
19810 if (value_dependent_expression_p (TREE_VALUE (list
)))
19816 /* Returns TRUE if the ARG (a template argument) is dependent. */
19819 dependent_template_arg_p (tree arg
)
19821 if (!processing_template_decl
)
19824 /* Assume a template argument that was wrongly written by the user
19825 is dependent. This is consistent with what
19826 any_dependent_template_arguments_p [that calls this function]
19828 if (!arg
|| arg
== error_mark_node
)
19831 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
19832 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
19834 if (TREE_CODE (arg
) == TEMPLATE_DECL
19835 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19836 return dependent_template_p (arg
);
19837 else if (ARGUMENT_PACK_P (arg
))
19839 tree args
= ARGUMENT_PACK_ARGS (arg
);
19840 int i
, len
= TREE_VEC_LENGTH (args
);
19841 for (i
= 0; i
< len
; ++i
)
19843 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19849 else if (TYPE_P (arg
))
19850 return dependent_type_p (arg
);
19852 return (type_dependent_expression_p (arg
)
19853 || value_dependent_expression_p (arg
));
19856 /* Returns true if ARGS (a collection of template arguments) contains
19857 any types that require structural equality testing. */
19860 any_template_arguments_need_structural_equality_p (tree args
)
19867 if (args
== error_mark_node
)
19870 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19872 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19873 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19875 tree arg
= TREE_VEC_ELT (level
, j
);
19876 tree packed_args
= NULL_TREE
;
19879 if (ARGUMENT_PACK_P (arg
))
19881 /* Look inside the argument pack. */
19882 packed_args
= ARGUMENT_PACK_ARGS (arg
);
19883 len
= TREE_VEC_LENGTH (packed_args
);
19886 for (k
= 0; k
< len
; ++k
)
19889 arg
= TREE_VEC_ELT (packed_args
, k
);
19891 if (error_operand_p (arg
))
19893 else if (TREE_CODE (arg
) == TEMPLATE_DECL
19894 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19896 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
19898 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
19899 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
19908 /* Returns true if ARGS (a collection of template arguments) contains
19909 any dependent arguments. */
19912 any_dependent_template_arguments_p (const_tree args
)
19919 if (args
== error_mark_node
)
19922 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19924 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19925 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19926 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
19933 /* Returns TRUE if the template TMPL is dependent. */
19936 dependent_template_p (tree tmpl
)
19938 if (TREE_CODE (tmpl
) == OVERLOAD
)
19942 if (dependent_template_p (OVL_CURRENT (tmpl
)))
19944 tmpl
= OVL_NEXT (tmpl
);
19949 /* Template template parameters are dependent. */
19950 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
19951 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
19953 /* So are names that have not been looked up. */
19954 if (TREE_CODE (tmpl
) == SCOPE_REF
19955 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
19957 /* So are member templates of dependent classes. */
19958 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
19959 return dependent_type_p (DECL_CONTEXT (tmpl
));
19963 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19966 dependent_template_id_p (tree tmpl
, tree args
)
19968 return (dependent_template_p (tmpl
)
19969 || any_dependent_template_arguments_p (args
));
19972 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19976 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
19980 if (!processing_template_decl
)
19983 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
19985 tree decl
= TREE_VEC_ELT (declv
, i
);
19986 tree init
= TREE_VEC_ELT (initv
, i
);
19987 tree cond
= TREE_VEC_ELT (condv
, i
);
19988 tree incr
= TREE_VEC_ELT (incrv
, i
);
19990 if (type_dependent_expression_p (decl
))
19993 if (init
&& type_dependent_expression_p (init
))
19996 if (type_dependent_expression_p (cond
))
19999 if (COMPARISON_CLASS_P (cond
)
20000 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
20001 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
20004 if (TREE_CODE (incr
) == MODOP_EXPR
)
20006 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
20007 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
20010 else if (type_dependent_expression_p (incr
))
20012 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
20014 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
20016 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
20018 tree t
= TREE_OPERAND (incr
, 1);
20019 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
20020 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
20029 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
20030 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
20031 no such TYPE can be found. Note that this function peers inside
20032 uninstantiated templates and therefore should be used only in
20033 extremely limited situations. ONLY_CURRENT_P restricts this
20034 peering to the currently open classes hierarchy (which is required
20035 when comparing types). */
20038 resolve_typename_type (tree type
, bool only_current_p
)
20047 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
20049 scope
= TYPE_CONTEXT (type
);
20050 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20051 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20052 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20053 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20054 identifier of the TYPENAME_TYPE anymore.
20055 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20056 TYPENAME_TYPE instead, we avoid messing up with a possible
20057 typedef variant case. */
20058 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
20060 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20061 it first before we can figure out what NAME refers to. */
20062 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20063 scope
= resolve_typename_type (scope
, only_current_p
);
20064 /* If we don't know what SCOPE refers to, then we cannot resolve the
20066 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20068 /* If the SCOPE is a template type parameter, we have no way of
20069 resolving the name. */
20070 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20072 /* If the SCOPE is not the current instantiation, there's no reason
20073 to look inside it. */
20074 if (only_current_p
&& !currently_open_class (scope
))
20076 /* If this is a typedef, we don't want to look inside (c++/11987). */
20077 if (typedef_variant_p (type
))
20079 /* If SCOPE isn't the template itself, it will not have a valid
20080 TYPE_FIELDS list. */
20081 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20082 /* scope is either the template itself or a compatible instantiation
20083 like X<T>, so look up the name in the original template. */
20084 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20086 /* scope is a partial instantiation, so we can't do the lookup or we
20087 will lose the template arguments. */
20089 /* Enter the SCOPE so that name lookup will be resolved as if we
20090 were in the class definition. In particular, SCOPE will no
20091 longer be considered a dependent type. */
20092 pushed_scope
= push_scope (scope
);
20093 /* Look up the declaration. */
20094 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20095 tf_warning_or_error
);
20097 result
= NULL_TREE
;
20099 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20100 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20103 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
20104 && TREE_CODE (decl
) == TYPE_DECL
)
20106 result
= TREE_TYPE (decl
);
20107 if (result
== error_mark_node
)
20108 result
= NULL_TREE
;
20110 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20111 && DECL_CLASS_TEMPLATE_P (decl
))
20115 /* Obtain the template and the arguments. */
20116 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20117 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20118 /* Instantiate the template. */
20119 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20120 /*entering_scope=*/0,
20121 tf_error
| tf_user
);
20122 if (result
== error_mark_node
)
20123 result
= NULL_TREE
;
20126 /* Leave the SCOPE. */
20128 pop_scope (pushed_scope
);
20130 /* If we failed to resolve it, return the original typename. */
20134 /* If lookup found a typename type, resolve that too. */
20135 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20137 /* Ill-formed programs can cause infinite recursion here, so we
20138 must catch that. */
20139 TYPENAME_IS_RESOLVING_P (type
) = 1;
20140 result
= resolve_typename_type (result
, only_current_p
);
20141 TYPENAME_IS_RESOLVING_P (type
) = 0;
20144 /* Qualify the resulting type. */
20145 quals
= cp_type_quals (type
);
20147 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20152 /* EXPR is an expression which is not type-dependent. Return a proxy
20153 for EXPR that can be used to compute the types of larger
20154 expressions containing EXPR. */
20157 build_non_dependent_expr (tree expr
)
20161 #ifdef ENABLE_CHECKING
20162 /* Try to get a constant value for all non-type-dependent expressions in
20163 order to expose bugs in *_dependent_expression_p and constexpr. */
20164 if (cxx_dialect
>= cxx0x
)
20165 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20168 /* Preserve OVERLOADs; the functions must be available to resolve
20171 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20172 inner_expr
= stmt_expr_value_expr (inner_expr
);
20173 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20174 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20175 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20176 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20177 if (is_overloaded_fn (inner_expr
)
20178 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20180 /* There is no need to return a proxy for a variable. */
20181 if (TREE_CODE (expr
) == VAR_DECL
)
20183 /* Preserve string constants; conversions from string constants to
20184 "char *" are allowed, even though normally a "const char *"
20185 cannot be used to initialize a "char *". */
20186 if (TREE_CODE (expr
) == STRING_CST
)
20188 /* Preserve arithmetic constants, as an optimization -- there is no
20189 reason to create a new node. */
20190 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20192 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20193 There is at least one place where we want to know that a
20194 particular expression is a throw-expression: when checking a ?:
20195 expression, there are special rules if the second or third
20196 argument is a throw-expression. */
20197 if (TREE_CODE (expr
) == THROW_EXPR
)
20200 /* Don't wrap an initializer list, we need to be able to look inside. */
20201 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20204 if (TREE_CODE (expr
) == COND_EXPR
)
20205 return build3 (COND_EXPR
,
20207 TREE_OPERAND (expr
, 0),
20208 (TREE_OPERAND (expr
, 1)
20209 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
20210 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
20211 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
20212 if (TREE_CODE (expr
) == COMPOUND_EXPR
20213 && !COMPOUND_EXPR_OVERLOADED (expr
))
20214 return build2 (COMPOUND_EXPR
,
20216 TREE_OPERAND (expr
, 0),
20217 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
20219 /* If the type is unknown, it can't really be non-dependent */
20220 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
20222 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20223 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
20226 /* ARGS is a vector of expressions as arguments to a function call.
20227 Replace the arguments with equivalent non-dependent expressions.
20228 This modifies ARGS in place. */
20231 make_args_non_dependent (VEC(tree
,gc
) *args
)
20236 FOR_EACH_VEC_ELT (tree
, args
, ix
, arg
)
20238 tree newarg
= build_non_dependent_expr (arg
);
20240 VEC_replace (tree
, args
, ix
, newarg
);
20244 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20245 with a level one deeper than the actual template parms. */
20250 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
20251 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
20252 TYPE_DECL
, get_identifier ("auto"), au
);
20253 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
20254 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
20255 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
20256 0, TYPE_NAME (au
), NULL_TREE
);
20257 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
20258 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
20259 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
20264 /* Given type ARG, return std::initializer_list<ARG>. */
20269 tree std_init_list
= namespace_binding
20270 (get_identifier ("initializer_list"), std_node
);
20272 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
20274 error ("deducing from brace-enclosed initializer list requires "
20275 "#include <initializer_list>");
20276 return error_mark_node
;
20278 argvec
= make_tree_vec (1);
20279 TREE_VEC_ELT (argvec
, 0) = arg
;
20280 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
20281 NULL_TREE
, 0, tf_warning_or_error
);
20284 /* Replace auto in TYPE with std::initializer_list<auto>. */
20287 listify_autos (tree type
, tree auto_node
)
20289 tree init_auto
= listify (auto_node
);
20290 tree argvec
= make_tree_vec (1);
20291 TREE_VEC_ELT (argvec
, 0) = init_auto
;
20292 if (processing_template_decl
)
20293 argvec
= add_to_template_args (current_template_args (), argvec
);
20294 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20297 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20298 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20301 do_auto_deduction (tree type
, tree init
, tree auto_node
)
20303 tree parms
, tparms
, targs
;
20307 if (init
== error_mark_node
)
20308 return error_mark_node
;
20310 if (processing_template_decl
20311 && (TREE_TYPE (init
) == NULL_TREE
20312 || BRACE_ENCLOSED_INITIALIZER_P (init
)))
20313 /* Not enough information to try this yet. */
20316 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20317 with either a new invented type template parameter U or, if the
20318 initializer is a braced-init-list (8.5.4), with
20319 std::initializer_list<U>. */
20320 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
20321 type
= listify_autos (type
, auto_node
);
20323 init
= resolve_nondeduced_context (init
);
20325 parms
= build_tree_list (NULL_TREE
, type
);
20327 tparms
= make_tree_vec (1);
20328 targs
= make_tree_vec (1);
20329 TREE_VEC_ELT (tparms
, 0)
20330 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20331 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20332 DEDUCE_CALL
, LOOKUP_NORMAL
,
20333 /*explain_p=*/false);
20336 if (processing_template_decl
)
20337 /* Try again at instantiation time. */
20339 if (type
&& type
!= error_mark_node
)
20340 /* If type is error_mark_node a diagnostic must have been
20341 emitted by now. Also, having a mention to '<type error>'
20342 in the diagnostic is not really useful to the user. */
20344 if (cfun
&& auto_node
== current_function_auto_return_pattern
20345 && LAMBDA_FUNCTION_P (current_function_decl
))
20346 error ("unable to deduce lambda return type from %qE", init
);
20348 error ("unable to deduce %qT from %qE", type
, init
);
20350 return error_mark_node
;
20353 /* If the list of declarators contains more than one declarator, the type
20354 of each declared variable is determined as described above. If the
20355 type deduced for the template parameter U is not the same in each
20356 deduction, the program is ill-formed. */
20357 if (TREE_TYPE (auto_node
)
20358 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20360 if (cfun
&& auto_node
== current_function_auto_return_pattern
20361 && LAMBDA_FUNCTION_P (current_function_decl
))
20362 error ("inconsistent types %qT and %qT deduced for "
20363 "lambda return type", TREE_TYPE (auto_node
),
20364 TREE_VEC_ELT (targs
, 0));
20366 error ("inconsistent deduction for %qT: %qT and then %qT",
20367 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20368 return error_mark_node
;
20370 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20372 if (processing_template_decl
)
20373 targs
= add_to_template_args (current_template_args (), targs
);
20374 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20377 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20381 splice_late_return_type (tree type
, tree late_return_type
)
20385 if (late_return_type
== NULL_TREE
)
20387 argvec
= make_tree_vec (1);
20388 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20389 if (processing_template_parmlist
)
20390 /* For a late-specified return type in a template type-parameter, we
20391 need to add a dummy argument level for its parmlist. */
20392 argvec
= add_to_template_args
20393 (make_tree_vec (processing_template_parmlist
), argvec
);
20394 if (current_template_parms
)
20395 argvec
= add_to_template_args (current_template_args (), argvec
);
20396 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20399 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20402 is_auto (const_tree type
)
20404 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20405 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20411 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20412 appear as a type-specifier for the declaration in question, we don't
20413 have to look through the whole type. */
20416 type_uses_auto (tree type
)
20418 enum tree_code code
;
20419 if (is_auto (type
))
20422 code
= TREE_CODE (type
);
20424 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20425 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20426 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20427 return type_uses_auto (TREE_TYPE (type
));
20429 if (TYPE_PTRMEMFUNC_P (type
))
20430 return type_uses_auto (TREE_TYPE (TREE_TYPE
20431 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20436 /* For a given template T, return the vector of typedefs referenced
20437 in T for which access check is needed at T instantiation time.
20438 T is either a FUNCTION_DECL or a RECORD_TYPE.
20439 Those typedefs were added to T by the function
20440 append_type_to_template_for_access_check. */
20442 VEC(qualified_typedef_usage_t
,gc
)*
20443 get_types_needing_access_check (tree t
)
20446 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
20448 if (!t
|| t
== error_mark_node
)
20451 if (!(ti
= get_template_info (t
)))
20454 if (CLASS_TYPE_P (t
)
20455 || TREE_CODE (t
) == FUNCTION_DECL
)
20457 if (!TI_TEMPLATE (ti
))
20460 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20466 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20467 tied to T. That list of typedefs will be access checked at
20468 T instantiation time.
20469 T is either a FUNCTION_DECL or a RECORD_TYPE.
20470 TYPE_DECL is a TYPE_DECL node representing a typedef.
20471 SCOPE is the scope through which TYPE_DECL is accessed.
20472 LOCATION is the location of the usage point of TYPE_DECL.
20474 This function is a subroutine of
20475 append_type_to_template_for_access_check. */
20478 append_type_to_template_for_access_check_1 (tree t
,
20481 location_t location
)
20483 qualified_typedef_usage_t typedef_usage
;
20486 if (!t
|| t
== error_mark_node
)
20489 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20490 || CLASS_TYPE_P (t
))
20492 && TREE_CODE (type_decl
) == TYPE_DECL
20495 if (!(ti
= get_template_info (t
)))
20498 gcc_assert (TI_TEMPLATE (ti
));
20500 typedef_usage
.typedef_decl
= type_decl
;
20501 typedef_usage
.context
= scope
;
20502 typedef_usage
.locus
= location
;
20504 VEC_safe_push (qualified_typedef_usage_t
, gc
,
20505 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
20509 /* Append TYPE_DECL to the template TEMPL.
20510 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20511 At TEMPL instanciation time, TYPE_DECL will be checked to see
20512 if it can be accessed through SCOPE.
20513 LOCATION is the location of the usage point of TYPE_DECL.
20515 e.g. consider the following code snippet:
20522 template<class U> struct S
20524 C::myint mi; // <-- usage point of the typedef C::myint
20529 At S<char> instantiation time, we need to check the access of C::myint
20530 In other words, we need to check the access of the myint typedef through
20531 the C scope. For that purpose, this function will add the myint typedef
20532 and the scope C through which its being accessed to a list of typedefs
20533 tied to the template S. That list will be walked at template instantiation
20534 time and access check performed on each typedefs it contains.
20535 Note that this particular code snippet should yield an error because
20536 myint is private to C. */
20539 append_type_to_template_for_access_check (tree templ
,
20542 location_t location
)
20544 qualified_typedef_usage_t
*iter
;
20547 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20549 /* Make sure we don't append the type to the template twice. */
20550 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
20551 get_types_needing_access_check (templ
),
20553 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20556 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20560 /* Set up the hash tables for template instantiations. */
20563 init_template_processing (void)
20565 decl_specializations
= htab_create_ggc (37,
20566 hash_specialization
,
20567 eq_specializations
,
20569 type_specializations
= htab_create_ggc (37,
20570 hash_specialization
,
20571 eq_specializations
,
20575 /* Print stats about the template hash tables for -fstats. */
20578 print_template_statistics (void)
20580 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20581 "%f collisions\n", (long) htab_size (decl_specializations
),
20582 (long) htab_elements (decl_specializations
),
20583 htab_collisions (decl_specializations
));
20584 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20585 "%f collisions\n", (long) htab_size (type_specializations
),
20586 (long) htab_elements (type_specializations
),
20587 htab_collisions (type_specializations
));
20590 #include "gt-cp-pt.h"