1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
6 Rewritten by Jason Merrill (jason@cygnus.com).
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 /* Known bugs or deficiencies include:
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
31 #include "coretypes.h"
35 #include "pointer-set.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "tree-inline.h"
46 #include "tree-iterator.h"
49 /* The type of functions taking a tree, and some additional data, and
51 typedef int (*tree_fn_t
) (tree
, void*);
53 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
54 instantiations have been deferred, either because their definitions
55 were not yet available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
{
57 struct pending_template
*next
;
58 struct tinst_level
*tinst
;
61 static GTY(()) struct pending_template
*pending_templates
;
62 static GTY(()) struct pending_template
*last_pending_template
;
64 int processing_template_parmlist
;
65 static int template_header_count
;
67 static GTY(()) tree saved_trees
;
68 static VEC(int,heap
) *inline_parm_levels
;
70 static GTY(()) struct tinst_level
*current_tinst_level
;
72 static GTY(()) tree saved_access_scope
;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr
;
79 /* A map from local variable declarations in the body of the template
80 presently being instantiated to the corresponding instantiated
82 static struct pointer_map_t
*local_specializations
;
84 typedef struct GTY(()) spec_entry
91 static GTY ((param_is (spec_entry
)))
92 htab_t decl_specializations
;
94 static GTY ((param_is (spec_entry
)))
95 htab_t type_specializations
;
97 /* Contains canonical template parameter types. The vector is indexed by
98 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
99 TREE_LIST, whose TREE_VALUEs contain the canonical template
100 parameters of various types and levels. */
101 static GTY(()) VEC(tree
,gc
) *canonical_template_parms
;
103 #define UNIFY_ALLOW_NONE 0
104 #define UNIFY_ALLOW_MORE_CV_QUAL 1
105 #define UNIFY_ALLOW_LESS_CV_QUAL 2
106 #define UNIFY_ALLOW_DERIVED 4
107 #define UNIFY_ALLOW_INTEGER 8
108 #define UNIFY_ALLOW_OUTER_LEVEL 16
109 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
110 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
112 enum template_base_result
{
114 tbr_ambiguous_baseclass
,
118 static void push_access_scope (tree
);
119 static void pop_access_scope (tree
);
120 static void push_deduction_access_scope (tree
);
121 static void pop_deduction_access_scope (tree
);
122 static bool resolve_overloaded_unification (tree
, tree
, tree
, tree
,
123 unification_kind_t
, int,
125 static int try_one_overload (tree
, tree
, tree
, tree
, tree
,
126 unification_kind_t
, int, bool, bool);
127 static int unify (tree
, tree
, tree
, tree
, int, bool);
128 static void add_pending_template (tree
);
129 static tree
reopen_tinst_level (struct tinst_level
*);
130 static tree
tsubst_initializer_list (tree
, tree
);
131 static tree
get_class_bindings (tree
, tree
, tree
);
132 static tree
coerce_template_parms (tree
, tree
, tree
, tsubst_flags_t
,
134 static void tsubst_enum (tree
, tree
, tree
);
135 static tree
add_to_template_args (tree
, tree
);
136 static tree
add_outermost_template_args (tree
, tree
);
137 static bool check_instantiated_args (tree
, tree
, tsubst_flags_t
);
138 static int maybe_adjust_types_for_deduction (unification_kind_t
, tree
*, tree
*,
140 static int type_unification_real (tree
, tree
, tree
, const tree
*,
141 unsigned int, int, unification_kind_t
, int,
143 static void note_template_header (int);
144 static tree
convert_nontype_argument_function (tree
, tree
);
145 static tree
convert_nontype_argument (tree
, tree
, tsubst_flags_t
);
146 static tree
convert_template_argument (tree
, tree
, tree
,
147 tsubst_flags_t
, int, tree
);
148 static int for_each_template_parm (tree
, tree_fn_t
, void*,
149 struct pointer_set_t
*, bool);
150 static tree
expand_template_argument_pack (tree
);
151 static tree
build_template_parm_index (int, int, int, int, tree
, tree
);
152 static bool inline_needs_template_parms (tree
);
153 static void push_inline_template_parms_recursive (tree
, int);
154 static tree
retrieve_local_specialization (tree
);
155 static void register_local_specialization (tree
, tree
);
156 static hashval_t
hash_specialization (const void *p
);
157 static tree
reduce_template_parm_level (tree
, tree
, int, tree
, tsubst_flags_t
);
158 static int mark_template_parm (tree
, void *);
159 static int template_parm_this_level_p (tree
, void *);
160 static tree
tsubst_friend_function (tree
, tree
);
161 static tree
tsubst_friend_class (tree
, tree
);
162 static int can_complete_type_without_circularity (tree
);
163 static tree
get_bindings (tree
, tree
, tree
, bool);
164 static int template_decl_level (tree
);
165 static int check_cv_quals_for_unify (int, tree
, tree
);
166 static void template_parm_level_and_index (tree
, int*, int*);
167 static int unify_pack_expansion (tree
, tree
, tree
,
168 tree
, unification_kind_t
, bool, bool);
169 static tree
tsubst_template_arg (tree
, tree
, tsubst_flags_t
, tree
);
170 static tree
tsubst_template_args (tree
, tree
, tsubst_flags_t
, tree
);
171 static tree
tsubst_template_parms (tree
, tree
, tsubst_flags_t
);
172 static void regenerate_decl_from_template (tree
, tree
);
173 static tree
most_specialized_class (tree
, tree
, tsubst_flags_t
);
174 static tree
tsubst_aggr_type (tree
, tree
, tsubst_flags_t
, tree
, int);
175 static tree
tsubst_arg_types (tree
, tree
, tsubst_flags_t
, tree
);
176 static tree
tsubst_function_type (tree
, tree
, tsubst_flags_t
, tree
);
177 static bool check_specialization_scope (void);
178 static tree
process_partial_specialization (tree
);
179 static void set_current_access_from_decl (tree
);
180 static enum template_base_result
get_template_base (tree
, tree
, tree
, tree
,
182 static tree
try_class_unification (tree
, tree
, tree
, tree
, bool);
183 static int coerce_template_template_parms (tree
, tree
, tsubst_flags_t
,
185 static bool template_template_parm_bindings_ok_p (tree
, tree
);
186 static int template_args_equal (tree
, tree
);
187 static void tsubst_default_arguments (tree
);
188 static tree
for_each_template_parm_r (tree
*, int *, void *);
189 static tree
copy_default_args_to_explicit_spec_1 (tree
, tree
);
190 static void copy_default_args_to_explicit_spec (tree
);
191 static int invalid_nontype_parm_type_p (tree
, tsubst_flags_t
);
192 static bool dependent_template_arg_p (tree
);
193 static bool any_template_arguments_need_structural_equality_p (tree
);
194 static bool dependent_type_p_r (tree
);
195 static tree
tsubst_expr (tree
, tree
, tsubst_flags_t
, tree
, bool);
196 static tree
tsubst_copy (tree
, tree
, tsubst_flags_t
, tree
);
197 static tree
tsubst_pack_expansion (tree
, tree
, tsubst_flags_t
, tree
);
198 static tree
tsubst_decl (tree
, tree
, tsubst_flags_t
);
199 static void perform_typedefs_access_check (tree tmpl
, tree targs
);
200 static void append_type_to_template_for_access_check_1 (tree
, tree
, tree
,
202 static tree
listify (tree
);
203 static tree
listify_autos (tree
, tree
);
204 static tree
template_parm_to_arg (tree t
);
205 static bool arg_from_parm_pack_p (tree
, tree
);
206 static tree
current_template_args (void);
207 static tree
fixup_template_type_parm_type (tree
, int);
208 static tree
fixup_template_parm_index (tree
, tree
, int);
209 static tree
tsubst_template_parm (tree
, tree
, tsubst_flags_t
);
211 /* Make the current scope suitable for access checking when we are
212 processing T. T can be FUNCTION_DECL for instantiated function
213 template, or VAR_DECL for static member variable (need by
214 instantiate_decl). */
217 push_access_scope (tree t
)
219 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
220 || TREE_CODE (t
) == VAR_DECL
);
222 if (DECL_FRIEND_CONTEXT (t
))
223 push_nested_class (DECL_FRIEND_CONTEXT (t
));
224 else if (DECL_CLASS_SCOPE_P (t
))
225 push_nested_class (DECL_CONTEXT (t
));
227 push_to_top_level ();
229 if (TREE_CODE (t
) == FUNCTION_DECL
)
231 saved_access_scope
= tree_cons
232 (NULL_TREE
, current_function_decl
, saved_access_scope
);
233 current_function_decl
= t
;
237 /* Restore the scope set up by push_access_scope. T is the node we
241 pop_access_scope (tree t
)
243 if (TREE_CODE (t
) == FUNCTION_DECL
)
245 current_function_decl
= TREE_VALUE (saved_access_scope
);
246 saved_access_scope
= TREE_CHAIN (saved_access_scope
);
249 if (DECL_FRIEND_CONTEXT (t
) || DECL_CLASS_SCOPE_P (t
))
252 pop_from_top_level ();
255 /* Do any processing required when DECL (a member template
256 declaration) is finished. Returns the TEMPLATE_DECL corresponding
257 to DECL, unless it is a specialization, in which case the DECL
258 itself is returned. */
261 finish_member_template_decl (tree decl
)
263 if (decl
== error_mark_node
)
264 return error_mark_node
;
266 gcc_assert (DECL_P (decl
));
268 if (TREE_CODE (decl
) == TYPE_DECL
)
272 type
= TREE_TYPE (decl
);
273 if (type
== error_mark_node
)
274 return error_mark_node
;
275 if (MAYBE_CLASS_TYPE_P (type
)
276 && CLASSTYPE_TEMPLATE_INFO (type
)
277 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
279 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
280 check_member_template (tmpl
);
285 else if (TREE_CODE (decl
) == FIELD_DECL
)
286 error ("data member %qD cannot be a member template", decl
);
287 else if (DECL_TEMPLATE_INFO (decl
))
289 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
291 check_member_template (DECL_TI_TEMPLATE (decl
));
292 return DECL_TI_TEMPLATE (decl
);
298 error ("invalid member template declaration %qD", decl
);
300 return error_mark_node
;
303 /* Create a template info node. */
306 build_template_info (tree template_decl
, tree template_args
)
308 tree result
= make_node (TEMPLATE_INFO
);
309 TI_TEMPLATE (result
) = template_decl
;
310 TI_ARGS (result
) = template_args
;
314 /* Return the template info node corresponding to T, whatever T is. */
317 get_template_info (const_tree t
)
319 tree tinfo
= NULL_TREE
;
321 if (!t
|| t
== error_mark_node
)
324 if (DECL_P (t
) && DECL_LANG_SPECIFIC (t
))
325 tinfo
= DECL_TEMPLATE_INFO (t
);
327 if (!tinfo
&& DECL_IMPLICIT_TYPEDEF_P (t
))
330 if (TAGGED_TYPE_P (t
))
331 tinfo
= TYPE_TEMPLATE_INFO (t
);
332 else if (TREE_CODE (t
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
333 tinfo
= TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t
);
338 /* Returns the template nesting level of the indicated class TYPE.
348 A<T>::B<U> has depth two, while A<T> has depth one.
349 Both A<T>::B<int> and A<int>::B<U> have depth one, if
350 they are instantiations, not specializations.
352 This function is guaranteed to return 0 if passed NULL_TREE so
353 that, for example, `template_class_depth (current_class_type)' is
357 template_class_depth (tree type
)
362 type
&& TREE_CODE (type
) != NAMESPACE_DECL
;
363 type
= (TREE_CODE (type
) == FUNCTION_DECL
)
364 ? CP_DECL_CONTEXT (type
) : CP_TYPE_CONTEXT (type
))
366 tree tinfo
= get_template_info (type
);
368 if (tinfo
&& PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
369 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo
))))
376 /* Subroutine of maybe_begin_member_template_processing.
377 Returns true if processing DECL needs us to push template parms. */
380 inline_needs_template_parms (tree decl
)
382 if (! DECL_TEMPLATE_INFO (decl
))
385 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl
)))
386 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
389 /* Subroutine of maybe_begin_member_template_processing.
390 Push the template parms in PARMS, starting from LEVELS steps into the
391 chain, and ending at the beginning, since template parms are listed
395 push_inline_template_parms_recursive (tree parmlist
, int levels
)
397 tree parms
= TREE_VALUE (parmlist
);
401 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
403 ++processing_template_decl
;
404 current_template_parms
405 = tree_cons (size_int (processing_template_decl
),
406 parms
, current_template_parms
);
407 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
409 begin_scope (TREE_VEC_LENGTH (parms
) ? sk_template_parms
: sk_template_spec
,
411 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
413 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
415 if (parm
== error_mark_node
)
418 gcc_assert (DECL_P (parm
));
420 switch (TREE_CODE (parm
))
429 /* Make a CONST_DECL as is done in process_template_parm.
430 It is ugly that we recreate this here; the original
431 version built in process_template_parm is no longer
433 tree decl
= build_decl (DECL_SOURCE_LOCATION (parm
),
434 CONST_DECL
, DECL_NAME (parm
),
436 DECL_ARTIFICIAL (decl
) = 1;
437 TREE_CONSTANT (decl
) = 1;
438 TREE_READONLY (decl
) = 1;
439 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
440 SET_DECL_TEMPLATE_PARM_P (decl
);
451 /* Restore the template parameter context for a member template or
452 a friend template defined in a class definition. */
455 maybe_begin_member_template_processing (tree decl
)
460 if (inline_needs_template_parms (decl
))
462 parms
= DECL_TEMPLATE_PARMS (most_general_template (decl
));
463 levels
= TMPL_PARMS_DEPTH (parms
) - processing_template_decl
;
465 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
468 parms
= TREE_CHAIN (parms
);
471 push_inline_template_parms_recursive (parms
, levels
);
474 /* Remember how many levels of template parameters we pushed so that
475 we can pop them later. */
476 VEC_safe_push (int, heap
, inline_parm_levels
, levels
);
479 /* Undo the effects of maybe_begin_member_template_processing. */
482 maybe_end_member_template_processing (void)
487 if (VEC_length (int, inline_parm_levels
) == 0)
490 last
= VEC_pop (int, inline_parm_levels
);
491 for (i
= 0; i
< last
; ++i
)
493 --processing_template_decl
;
494 current_template_parms
= TREE_CHAIN (current_template_parms
);
499 /* Return a new template argument vector which contains all of ARGS,
500 but has as its innermost set of arguments the EXTRA_ARGS. */
503 add_to_template_args (tree args
, tree extra_args
)
510 if (args
== NULL_TREE
|| extra_args
== error_mark_node
)
513 extra_depth
= TMPL_ARGS_DEPTH (extra_args
);
514 new_args
= make_tree_vec (TMPL_ARGS_DEPTH (args
) + extra_depth
);
516 for (i
= 1; i
<= TMPL_ARGS_DEPTH (args
); ++i
)
517 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (args
, i
));
519 for (j
= 1; j
<= extra_depth
; ++j
, ++i
)
520 SET_TMPL_ARGS_LEVEL (new_args
, i
, TMPL_ARGS_LEVEL (extra_args
, j
));
525 /* Like add_to_template_args, but only the outermost ARGS are added to
526 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
527 (EXTRA_ARGS) levels are added. This function is used to combine
528 the template arguments from a partial instantiation with the
529 template arguments used to attain the full instantiation from the
530 partial instantiation. */
533 add_outermost_template_args (tree args
, tree extra_args
)
537 /* If there are more levels of EXTRA_ARGS than there are ARGS,
538 something very fishy is going on. */
539 gcc_assert (TMPL_ARGS_DEPTH (args
) >= TMPL_ARGS_DEPTH (extra_args
));
541 /* If *all* the new arguments will be the EXTRA_ARGS, just return
543 if (TMPL_ARGS_DEPTH (args
) == TMPL_ARGS_DEPTH (extra_args
))
546 /* For the moment, we make ARGS look like it contains fewer levels. */
547 TREE_VEC_LENGTH (args
) -= TMPL_ARGS_DEPTH (extra_args
);
549 new_args
= add_to_template_args (args
, extra_args
);
551 /* Now, we restore ARGS to its full dimensions. */
552 TREE_VEC_LENGTH (args
) += TMPL_ARGS_DEPTH (extra_args
);
557 /* Return the N levels of innermost template arguments from the ARGS. */
560 get_innermost_template_args (tree args
, int n
)
568 /* If N is 1, just return the innermost set of template arguments. */
570 return TMPL_ARGS_LEVEL (args
, TMPL_ARGS_DEPTH (args
));
572 /* If we're not removing anything, just return the arguments we were
574 extra_levels
= TMPL_ARGS_DEPTH (args
) - n
;
575 gcc_assert (extra_levels
>= 0);
576 if (extra_levels
== 0)
579 /* Make a new set of arguments, not containing the outer arguments. */
580 new_args
= make_tree_vec (n
);
581 for (i
= 1; i
<= n
; ++i
)
582 SET_TMPL_ARGS_LEVEL (new_args
, i
,
583 TMPL_ARGS_LEVEL (args
, i
+ extra_levels
));
588 /* The inverse of get_innermost_template_args: Return all but the innermost
589 EXTRA_LEVELS levels of template arguments from the ARGS. */
592 strip_innermost_template_args (tree args
, int extra_levels
)
595 int n
= TMPL_ARGS_DEPTH (args
) - extra_levels
;
600 /* If N is 1, just return the outermost set of template arguments. */
602 return TMPL_ARGS_LEVEL (args
, 1);
604 /* If we're not removing anything, just return the arguments we were
606 gcc_assert (extra_levels
>= 0);
607 if (extra_levels
== 0)
610 /* Make a new set of arguments, not containing the inner arguments. */
611 new_args
= make_tree_vec (n
);
612 for (i
= 1; i
<= n
; ++i
)
613 SET_TMPL_ARGS_LEVEL (new_args
, i
,
614 TMPL_ARGS_LEVEL (args
, i
));
619 /* We've got a template header coming up; push to a new level for storing
623 begin_template_parm_list (void)
625 /* We use a non-tag-transparent scope here, which causes pushtag to
626 put tags in this scope, rather than in the enclosing class or
627 namespace scope. This is the right thing, since we want
628 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
629 global template class, push_template_decl handles putting the
630 TEMPLATE_DECL into top-level scope. For a nested template class,
633 template <class T> struct S1 {
634 template <class T> struct S2 {};
637 pushtag contains special code to call pushdecl_with_scope on the
638 TEMPLATE_DECL for S2. */
639 begin_scope (sk_template_parms
, NULL
);
640 ++processing_template_decl
;
641 ++processing_template_parmlist
;
642 note_template_header (0);
645 /* This routine is called when a specialization is declared. If it is
646 invalid to declare a specialization here, an error is reported and
647 false is returned, otherwise this routine will return true. */
650 check_specialization_scope (void)
652 tree scope
= current_scope ();
656 An explicit specialization shall be declared in the namespace of
657 which the template is a member, or, for member templates, in the
658 namespace of which the enclosing class or enclosing class
659 template is a member. An explicit specialization of a member
660 function, member class or static data member of a class template
661 shall be declared in the namespace of which the class template
663 if (scope
&& TREE_CODE (scope
) != NAMESPACE_DECL
)
665 error ("explicit specialization in non-namespace scope %qD", scope
);
671 In an explicit specialization declaration for a member of a class
672 template or a member template that appears in namespace scope,
673 the member template and some of its enclosing class templates may
674 remain unspecialized, except that the declaration shall not
675 explicitly specialize a class member template if its enclosing
676 class templates are not explicitly specialized as well. */
677 if (current_template_parms
)
679 error ("enclosing class templates are not explicitly specialized");
686 /* We've just seen template <>. */
689 begin_specialization (void)
691 begin_scope (sk_template_spec
, NULL
);
692 note_template_header (1);
693 return check_specialization_scope ();
696 /* Called at then end of processing a declaration preceded by
700 end_specialization (void)
703 reset_specialization ();
706 /* Any template <>'s that we have seen thus far are not referring to a
707 function specialization. */
710 reset_specialization (void)
712 processing_specialization
= 0;
713 template_header_count
= 0;
716 /* We've just seen a template header. If SPECIALIZATION is nonzero,
717 it was of the form template <>. */
720 note_template_header (int specialization
)
722 processing_specialization
= specialization
;
723 template_header_count
++;
726 /* We're beginning an explicit instantiation. */
729 begin_explicit_instantiation (void)
731 gcc_assert (!processing_explicit_instantiation
);
732 processing_explicit_instantiation
= true;
737 end_explicit_instantiation (void)
739 gcc_assert (processing_explicit_instantiation
);
740 processing_explicit_instantiation
= false;
743 /* An explicit specialization or partial specialization TMPL is being
744 declared. Check that the namespace in which the specialization is
745 occurring is permissible. Returns false iff it is invalid to
746 specialize TMPL in the current namespace. */
749 check_specialization_namespace (tree tmpl
)
751 tree tpl_ns
= decl_namespace_context (tmpl
);
755 An explicit specialization shall be declared in the namespace of
756 which the template is a member, or, for member templates, in the
757 namespace of which the enclosing class or enclosing class
758 template is a member. An explicit specialization of a member
759 function, member class or static data member of a class template
760 shall be declared in the namespace of which the class template is
762 if (current_scope() != DECL_CONTEXT (tmpl
)
763 && !at_namespace_scope_p ())
765 error ("specialization of %qD must appear at namespace scope", tmpl
);
768 if (is_associated_namespace (current_namespace
, tpl_ns
))
769 /* Same or super-using namespace. */
773 permerror (input_location
, "specialization of %qD in different namespace", tmpl
);
774 permerror (input_location
, " from definition of %q+#D", tmpl
);
779 /* SPEC is an explicit instantiation. Check that it is valid to
780 perform this explicit instantiation in the current namespace. */
783 check_explicit_instantiation_namespace (tree spec
)
787 /* DR 275: An explicit instantiation shall appear in an enclosing
788 namespace of its template. */
789 ns
= decl_namespace_context (spec
);
790 if (!is_ancestor (current_namespace
, ns
))
791 permerror (input_location
, "explicit instantiation of %qD in namespace %qD "
792 "(which does not enclose namespace %qD)",
793 spec
, current_namespace
, ns
);
796 /* The TYPE is being declared. If it is a template type, that means it
797 is a partial specialization. Do appropriate error-checking. */
800 maybe_process_partial_specialization (tree type
)
804 if (type
== error_mark_node
)
805 return error_mark_node
;
807 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
809 error ("name of class shadows template template parameter %qD",
811 return error_mark_node
;
814 context
= TYPE_CONTEXT (type
);
816 if ((CLASS_TYPE_P (type
) && CLASSTYPE_USE_TEMPLATE (type
))
817 /* Consider non-class instantiations of alias templates as
820 && TYPE_TEMPLATE_INFO (type
)
821 && DECL_LANG_SPECIFIC (TYPE_NAME (type
))
822 && DECL_USE_TEMPLATE (TYPE_NAME (type
))))
824 /* This is for ordinary explicit specialization and partial
825 specialization of a template class such as:
827 template <> class C<int>;
831 template <class T> class C<T*>;
833 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
835 if (CLASS_TYPE_P (type
)
836 && CLASSTYPE_IMPLICIT_INSTANTIATION (type
)
837 && !COMPLETE_TYPE_P (type
))
839 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type
));
840 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
841 if (processing_template_decl
)
843 if (push_template_decl (TYPE_MAIN_DECL (type
))
845 return error_mark_node
;
848 else if (CLASS_TYPE_P (type
)
849 && CLASSTYPE_TEMPLATE_INSTANTIATION (type
))
850 error ("specialization of %qT after instantiation", type
);
852 if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type
)))
854 error ("partial specialization of alias template %qD",
855 TYPE_TI_TEMPLATE (type
));
856 return error_mark_node
;
859 else if (CLASS_TYPE_P (type
)
860 && !CLASSTYPE_USE_TEMPLATE (type
)
861 && CLASSTYPE_TEMPLATE_INFO (type
)
862 && context
&& CLASS_TYPE_P (context
)
863 && CLASSTYPE_TEMPLATE_INFO (context
))
865 /* This is for an explicit specialization of member class
866 template according to [temp.expl.spec/18]:
868 template <> template <class U> class C<int>::D;
870 The context `C<int>' must be an implicit instantiation.
871 Otherwise this is just a member class template declared
874 template <> class C<int> { template <class U> class D; };
875 template <> template <class U> class C<int>::D;
877 In the first case, `C<int>::D' is a specialization of `C<T>::D'
878 while in the second case, `C<int>::D' is a primary template
879 and `C<T>::D' may not exist. */
881 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context
)
882 && !COMPLETE_TYPE_P (type
))
885 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
887 if (current_namespace
888 != decl_namespace_context (tmpl
))
890 permerror (input_location
, "specializing %q#T in different namespace", type
);
891 permerror (input_location
, " from definition of %q+#D", tmpl
);
894 /* Check for invalid specialization after instantiation:
896 template <> template <> class C<int>::D<int>;
897 template <> template <class U> class C<int>::D; */
899 for (t
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
900 t
; t
= TREE_CHAIN (t
))
902 tree inst
= TREE_VALUE (t
);
903 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst
))
905 /* We already have a full specialization of this partial
906 instantiation. Reassign it to the new member
907 specialization template. */
912 elt
.tmpl
= most_general_template (tmpl
);
913 elt
.args
= CLASSTYPE_TI_ARGS (inst
);
916 htab_remove_elt (type_specializations
, &elt
);
919 elt
.args
= INNERMOST_TEMPLATE_ARGS (elt
.args
);
921 slot
= htab_find_slot (type_specializations
, &elt
, INSERT
);
922 entry
= ggc_alloc_spec_entry ();
926 else if (COMPLETE_OR_OPEN_TYPE_P (inst
))
927 /* But if we've had an implicit instantiation, that's a
928 problem ([temp.expl.spec]/6). */
929 error ("specialization %qT after instantiation %qT",
933 /* Mark TYPE as a specialization. And as a result, we only
934 have one level of template argument for the innermost
936 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type
);
937 CLASSTYPE_TI_ARGS (type
)
938 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
941 else if (processing_specialization
)
943 /* Someday C++0x may allow for enum template specialization. */
944 if (cxx_dialect
> cxx98
&& TREE_CODE (type
) == ENUMERAL_TYPE
945 && CLASS_TYPE_P (context
) && CLASSTYPE_USE_TEMPLATE (context
))
946 pedwarn (input_location
, OPT_pedantic
, "template specialization "
947 "of %qD not allowed by ISO C++", type
);
950 error ("explicit specialization of non-template %qT", type
);
951 return error_mark_node
;
958 /* Returns nonzero if we can optimize the retrieval of specializations
959 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
960 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
963 optimize_specialization_lookup_p (tree tmpl
)
965 return (DECL_FUNCTION_TEMPLATE_P (tmpl
)
966 && DECL_CLASS_SCOPE_P (tmpl
)
967 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
969 && CLASS_TYPE_P (DECL_CONTEXT (tmpl
))
970 /* The optimized lookup depends on the fact that the
971 template arguments for the member function template apply
972 purely to the containing class, which is not true if the
973 containing class is an explicit or partial
975 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl
))
976 && !DECL_MEMBER_TEMPLATE_P (tmpl
)
977 && !DECL_CONV_FN_P (tmpl
)
978 /* It is possible to have a template that is not a member
979 template and is not a member of a template class:
981 template <typename T>
982 struct S { friend A::f(); };
984 Here, the friend function is a template, but the context does
985 not have template information. The optimized lookup relies
986 on having ARGS be the template arguments for both the class
987 and the function template. */
988 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl
)));
991 /* Retrieve the specialization (in the sense of [temp.spec] - a
992 specialization is either an instantiation or an explicit
993 specialization) of TMPL for the given template ARGS. If there is
994 no such specialization, return NULL_TREE. The ARGS are a vector of
995 arguments, or a vector of vectors of arguments, in the case of
996 templates with more than one level of parameters.
998 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
999 then we search for a partial specialization matching ARGS. This
1000 parameter is ignored if TMPL is not a class template. */
1003 retrieve_specialization (tree tmpl
, tree args
, hashval_t hash
)
1005 if (args
== error_mark_node
)
1008 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
1010 /* There should be as many levels of arguments as there are
1011 levels of parameters. */
1012 gcc_assert (TMPL_ARGS_DEPTH (args
)
1013 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
1015 if (optimize_specialization_lookup_p (tmpl
))
1017 tree class_template
;
1018 tree class_specialization
;
1019 VEC(tree
,gc
) *methods
;
1023 /* The template arguments actually apply to the containing
1024 class. Find the class specialization with those
1026 class_template
= CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl
));
1027 class_specialization
1028 = retrieve_specialization (class_template
, args
, 0);
1029 if (!class_specialization
)
1031 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1032 for the specialization. */
1033 idx
= class_method_index_for_fn (class_specialization
, tmpl
);
1036 /* Iterate through the methods with the indicated name, looking
1037 for the one that has an instance of TMPL. */
1038 methods
= CLASSTYPE_METHOD_VEC (class_specialization
);
1039 for (fns
= VEC_index (tree
, methods
, idx
); fns
; fns
= OVL_NEXT (fns
))
1041 tree fn
= OVL_CURRENT (fns
);
1042 if (DECL_TEMPLATE_INFO (fn
) && DECL_TI_TEMPLATE (fn
) == tmpl
1043 /* using-declarations can add base methods to the method vec,
1044 and we don't want those here. */
1045 && DECL_CONTEXT (fn
) == class_specialization
)
1054 htab_t specializations
;
1058 elt
.spec
= NULL_TREE
;
1060 if (DECL_CLASS_TEMPLATE_P (tmpl
))
1061 specializations
= type_specializations
;
1063 specializations
= decl_specializations
;
1066 hash
= hash_specialization (&elt
);
1067 found
= (spec_entry
*) htab_find_with_hash (specializations
, &elt
, hash
);
1075 /* Like retrieve_specialization, but for local declarations. */
1078 retrieve_local_specialization (tree tmpl
)
1082 if (local_specializations
== NULL
)
1085 slot
= pointer_map_contains (local_specializations
, tmpl
);
1086 return slot
? (tree
) *slot
: NULL_TREE
;
1089 /* Returns nonzero iff DECL is a specialization of TMPL. */
1092 is_specialization_of (tree decl
, tree tmpl
)
1096 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1100 t
= DECL_TEMPLATE_INFO (t
) ? DECL_TI_TEMPLATE (t
) : NULL_TREE
)
1106 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
1108 for (t
= TREE_TYPE (decl
);
1110 t
= CLASSTYPE_USE_TEMPLATE (t
)
1111 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t
)) : NULL_TREE
)
1112 if (same_type_ignoring_top_level_qualifiers_p (t
, TREE_TYPE (tmpl
)))
1119 /* Returns nonzero iff DECL is a specialization of friend declaration
1120 FRIEND_DECL according to [temp.friend]. */
1123 is_specialization_of_friend (tree decl
, tree friend_decl
)
1125 bool need_template
= true;
1128 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
1129 || TREE_CODE (decl
) == TYPE_DECL
);
1131 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1132 of a template class, we want to check if DECL is a specialization
1134 if (TREE_CODE (friend_decl
) == FUNCTION_DECL
1135 && DECL_TEMPLATE_INFO (friend_decl
)
1136 && !DECL_USE_TEMPLATE (friend_decl
))
1138 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1139 friend_decl
= DECL_TI_TEMPLATE (friend_decl
);
1140 need_template
= false;
1142 else if (TREE_CODE (friend_decl
) == TEMPLATE_DECL
1143 && !PRIMARY_TEMPLATE_P (friend_decl
))
1144 need_template
= false;
1146 /* There is nothing to do if this is not a template friend. */
1147 if (TREE_CODE (friend_decl
) != TEMPLATE_DECL
)
1150 if (is_specialization_of (decl
, friend_decl
))
1154 A member of a class template may be declared to be a friend of a
1155 non-template class. In this case, the corresponding member of
1156 every specialization of the class template is a friend of the
1157 class granting friendship.
1159 For example, given a template friend declaration
1161 template <class T> friend void A<T>::f();
1163 the member function below is considered a friend
1165 template <> struct A<int> {
1169 For this type of template friend, TEMPLATE_DEPTH below will be
1170 nonzero. To determine if DECL is a friend of FRIEND, we first
1171 check if the enclosing class is a specialization of another. */
1173 template_depth
= template_class_depth (CP_DECL_CONTEXT (friend_decl
));
1175 && DECL_CLASS_SCOPE_P (decl
)
1176 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl
)),
1177 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl
))))
1179 /* Next, we check the members themselves. In order to handle
1180 a few tricky cases, such as when FRIEND_DECL's are
1182 template <class T> friend void A<T>::g(T t);
1183 template <class T> template <T t> friend void A<T>::h();
1187 void A<int>::g(int);
1188 template <int> void A<int>::h();
1190 we need to figure out ARGS, the template arguments from
1191 the context of DECL. This is required for template substitution
1192 of `T' in the function parameter of `g' and template parameter
1193 of `h' in the above examples. Here ARGS corresponds to `int'. */
1195 tree context
= DECL_CONTEXT (decl
);
1196 tree args
= NULL_TREE
;
1197 int current_depth
= 0;
1199 while (current_depth
< template_depth
)
1201 if (CLASSTYPE_TEMPLATE_INFO (context
))
1203 if (current_depth
== 0)
1204 args
= TYPE_TI_ARGS (context
);
1206 args
= add_to_template_args (TYPE_TI_ARGS (context
), args
);
1209 context
= TYPE_CONTEXT (context
);
1212 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1217 tree friend_args_type
;
1218 tree decl_args_type
;
1220 /* Make sure that both DECL and FRIEND_DECL are templates or
1222 is_template
= DECL_TEMPLATE_INFO (decl
)
1223 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
));
1224 if (need_template
^ is_template
)
1226 else if (is_template
)
1228 /* If both are templates, check template parameter list. */
1230 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1232 if (!comp_template_parms
1233 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
)),
1237 decl_type
= TREE_TYPE (DECL_TI_TEMPLATE (decl
));
1240 decl_type
= TREE_TYPE (decl
);
1242 friend_type
= tsubst_function_type (TREE_TYPE (friend_decl
), args
,
1243 tf_none
, NULL_TREE
);
1244 if (friend_type
== error_mark_node
)
1247 /* Check if return types match. */
1248 if (!same_type_p (TREE_TYPE (decl_type
), TREE_TYPE (friend_type
)))
1251 /* Check if function parameter types match, ignoring the
1252 `this' parameter. */
1253 friend_args_type
= TYPE_ARG_TYPES (friend_type
);
1254 decl_args_type
= TYPE_ARG_TYPES (decl_type
);
1255 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl
))
1256 friend_args_type
= TREE_CHAIN (friend_args_type
);
1257 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1258 decl_args_type
= TREE_CHAIN (decl_args_type
);
1260 return compparms (decl_args_type
, friend_args_type
);
1264 /* DECL is a TYPE_DECL */
1266 tree decl_type
= TREE_TYPE (decl
);
1268 /* Make sure that both DECL and FRIEND_DECL are templates or
1271 = CLASSTYPE_TEMPLATE_INFO (decl_type
)
1272 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type
));
1274 if (need_template
^ is_template
)
1276 else if (is_template
)
1279 /* If both are templates, check the name of the two
1280 TEMPLATE_DECL's first because is_friend didn't. */
1281 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type
))
1282 != DECL_NAME (friend_decl
))
1285 /* Now check template parameter list. */
1287 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl
),
1289 return comp_template_parms
1290 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type
)),
1294 return (DECL_NAME (decl
)
1295 == DECL_NAME (friend_decl
));
1301 /* Register the specialization SPEC as a specialization of TMPL with
1302 the indicated ARGS. IS_FRIEND indicates whether the specialization
1303 is actually just a friend declaration. Returns SPEC, or an
1304 equivalent prior declaration, if available. */
1307 register_specialization (tree spec
, tree tmpl
, tree args
, bool is_friend
,
1314 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
&& DECL_P (spec
));
1316 if (TREE_CODE (spec
) == FUNCTION_DECL
1317 && uses_template_parms (DECL_TI_ARGS (spec
)))
1318 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1319 register it; we want the corresponding TEMPLATE_DECL instead.
1320 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1321 the more obvious `uses_template_parms (spec)' to avoid problems
1322 with default function arguments. In particular, given
1323 something like this:
1325 template <class T> void f(T t1, T t = T())
1327 the default argument expression is not substituted for in an
1328 instantiation unless and until it is actually needed. */
1331 if (optimize_specialization_lookup_p (tmpl
))
1332 /* We don't put these specializations in the hash table, but we might
1333 want to give an error about a mismatch. */
1334 fn
= retrieve_specialization (tmpl
, args
, 0);
1342 hash
= hash_specialization (&elt
);
1345 htab_find_slot_with_hash (decl_specializations
, &elt
, hash
, INSERT
);
1347 fn
= ((spec_entry
*) *slot
)->spec
;
1352 /* We can sometimes try to re-register a specialization that we've
1353 already got. In particular, regenerate_decl_from_template calls
1354 duplicate_decls which will update the specialization list. But,
1355 we'll still get called again here anyhow. It's more convenient
1356 to simply allow this than to try to prevent it. */
1359 else if (fn
&& DECL_TEMPLATE_SPECIALIZATION (spec
))
1361 if (DECL_TEMPLATE_INSTANTIATION (fn
))
1363 if (DECL_ODR_USED (fn
)
1364 || DECL_EXPLICIT_INSTANTIATION (fn
))
1366 error ("specialization of %qD after instantiation",
1368 return error_mark_node
;
1373 /* This situation should occur only if the first
1374 specialization is an implicit instantiation, the
1375 second is an explicit specialization, and the
1376 implicit instantiation has not yet been used. That
1377 situation can occur if we have implicitly
1378 instantiated a member function and then specialized
1381 We can also wind up here if a friend declaration that
1382 looked like an instantiation turns out to be a
1385 template <class T> void foo(T);
1386 class S { friend void foo<>(int) };
1387 template <> void foo(int);
1389 We transform the existing DECL in place so that any
1390 pointers to it become pointers to the updated
1393 If there was a definition for the template, but not
1394 for the specialization, we want this to look as if
1395 there were no definition, and vice versa. */
1396 DECL_INITIAL (fn
) = NULL_TREE
;
1397 duplicate_decls (spec
, fn
, is_friend
);
1398 /* The call to duplicate_decls will have applied
1401 An explicit specialization of a function template
1402 is inline only if it is explicitly declared to be,
1403 and independently of whether its function template
1406 to the primary function; now copy the inline bits to
1407 the various clones. */
1408 FOR_EACH_CLONE (clone
, fn
)
1410 DECL_DECLARED_INLINE_P (clone
)
1411 = DECL_DECLARED_INLINE_P (fn
);
1412 DECL_SOURCE_LOCATION (clone
)
1413 = DECL_SOURCE_LOCATION (fn
);
1415 check_specialization_namespace (fn
);
1420 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
1422 if (!duplicate_decls (spec
, fn
, is_friend
) && DECL_INITIAL (spec
))
1423 /* Dup decl failed, but this is a new definition. Set the
1424 line number so any errors match this new
1426 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (spec
);
1432 return duplicate_decls (spec
, fn
, is_friend
);
1434 /* A specialization must be declared in the same namespace as the
1435 template it is specializing. */
1436 if (DECL_TEMPLATE_SPECIALIZATION (spec
)
1437 && !check_specialization_namespace (tmpl
))
1438 DECL_CONTEXT (spec
) = DECL_CONTEXT (tmpl
);
1440 if (slot
!= NULL
/* !optimize_specialization_lookup_p (tmpl) */)
1442 spec_entry
*entry
= ggc_alloc_spec_entry ();
1443 gcc_assert (tmpl
&& args
&& spec
);
1446 if (TREE_CODE (spec
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (spec
)
1447 && PRIMARY_TEMPLATE_P (tmpl
)
1448 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl
)) == NULL_TREE
)
1449 /* TMPL is a forward declaration of a template function; keep a list
1450 of all specializations in case we need to reassign them to a friend
1451 template later in tsubst_friend_function. */
1452 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
1453 = tree_cons (args
, spec
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1459 /* Returns true iff two spec_entry nodes are equivalent. Only compares the
1460 TMPL and ARGS members, ignores SPEC. */
1463 eq_specializations (const void *p1
, const void *p2
)
1465 const spec_entry
*e1
= (const spec_entry
*)p1
;
1466 const spec_entry
*e2
= (const spec_entry
*)p2
;
1468 return (e1
->tmpl
== e2
->tmpl
1469 && comp_template_args (e1
->args
, e2
->args
));
1472 /* Returns a hash for a template TMPL and template arguments ARGS. */
1475 hash_tmpl_and_args (tree tmpl
, tree args
)
1477 hashval_t val
= DECL_UID (tmpl
);
1478 return iterative_hash_template_arg (args
, val
);
1481 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1485 hash_specialization (const void *p
)
1487 const spec_entry
*e
= (const spec_entry
*)p
;
1488 return hash_tmpl_and_args (e
->tmpl
, e
->args
);
1491 /* Recursively calculate a hash value for a template argument ARG, for use
1492 in the hash tables of template specializations. */
1495 iterative_hash_template_arg (tree arg
, hashval_t val
)
1497 unsigned HOST_WIDE_INT i
;
1498 enum tree_code code
;
1501 if (arg
== NULL_TREE
)
1502 return iterative_hash_object (arg
, val
);
1507 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
1508 /* We can get one of these when re-hashing a previous entry in the middle
1509 of substituting into a pack expansion. Just look through it. */
1510 arg
= ARGUMENT_PACK_SELECT_FROM_PACK (arg
);
1512 code
= TREE_CODE (arg
);
1513 tclass
= TREE_CODE_CLASS (code
);
1515 val
= iterative_hash_object (code
, val
);
1522 case IDENTIFIER_NODE
:
1523 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg
), val
);
1527 int i
, len
= TREE_VEC_LENGTH (arg
);
1528 for (i
= 0; i
< len
; ++i
)
1529 val
= iterative_hash_template_arg (TREE_VEC_ELT (arg
, i
), val
);
1533 case TYPE_PACK_EXPANSION
:
1534 case EXPR_PACK_EXPANSION
:
1535 val
= iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg
), val
);
1536 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg
), val
);
1538 case TYPE_ARGUMENT_PACK
:
1539 case NONTYPE_ARGUMENT_PACK
:
1540 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg
), val
);
1543 for (; arg
; arg
= TREE_CHAIN (arg
))
1544 val
= iterative_hash_template_arg (TREE_VALUE (arg
), val
);
1548 for (; arg
; arg
= OVL_NEXT (arg
))
1549 val
= iterative_hash_template_arg (OVL_CURRENT (arg
), val
);
1555 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg
), i
, field
, value
)
1557 val
= iterative_hash_template_arg (field
, val
);
1558 val
= iterative_hash_template_arg (value
, val
);
1564 if (!DECL_ARTIFICIAL (arg
))
1566 val
= iterative_hash_object (DECL_PARM_INDEX (arg
), val
);
1567 val
= iterative_hash_object (DECL_PARM_LEVEL (arg
), val
);
1569 return iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1572 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg
), val
);
1575 val
= iterative_hash_template_arg (PTRMEM_CST_CLASS (arg
), val
);
1576 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg
), val
);
1578 case TEMPLATE_PARM_INDEX
:
1579 val
= iterative_hash_template_arg
1580 (TREE_TYPE (TEMPLATE_PARM_DECL (arg
)), val
);
1581 val
= iterative_hash_object (TEMPLATE_PARM_LEVEL (arg
), val
);
1582 return iterative_hash_object (TEMPLATE_PARM_IDX (arg
), val
);
1585 val
= iterative_hash_object (TRAIT_EXPR_KIND (arg
), val
);
1586 val
= iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg
), val
);
1587 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg
), val
);
1590 val
= iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg
)),
1592 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg
)),
1596 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, 0), val
);
1597 code
= TREE_CODE (TREE_OPERAND (arg
, 1));
1598 val
= iterative_hash_object (code
, val
);
1599 return iterative_hash_template_arg (TREE_OPERAND (arg
, 2), val
);
1602 /* A lambda can't appear in a template arg, but don't crash on
1604 gcc_assert (seen_error ());
1608 case IMPLICIT_CONV_EXPR
:
1609 case STATIC_CAST_EXPR
:
1610 case REINTERPRET_CAST_EXPR
:
1611 case CONST_CAST_EXPR
:
1612 case DYNAMIC_CAST_EXPR
:
1614 val
= iterative_hash_template_arg (TREE_TYPE (arg
), val
);
1615 /* Now hash operands as usual. */
1625 if (TYPE_CANONICAL (arg
))
1626 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg
)),
1628 else if (TREE_CODE (arg
) == DECLTYPE_TYPE
)
1629 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg
), val
);
1630 /* Otherwise just compare the types during lookup. */
1633 case tcc_declaration
:
1635 return iterative_hash_expr (arg
, val
);
1638 gcc_assert (IS_EXPR_CODE_CLASS (tclass
));
1640 unsigned n
= cp_tree_operand_length (arg
);
1641 for (i
= 0; i
< n
; ++i
)
1642 val
= iterative_hash_template_arg (TREE_OPERAND (arg
, i
), val
);
1650 /* Unregister the specialization SPEC as a specialization of TMPL.
1651 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1652 if the SPEC was listed as a specialization of TMPL.
1654 Note that SPEC has been ggc_freed, so we can't look inside it. */
1657 reregister_specialization (tree spec
, tree tinfo
, tree new_spec
)
1662 elt
.tmpl
= most_general_template (TI_TEMPLATE (tinfo
));
1663 elt
.args
= TI_ARGS (tinfo
);
1664 elt
.spec
= NULL_TREE
;
1666 entry
= (spec_entry
*) htab_find (decl_specializations
, &elt
);
1669 gcc_assert (entry
->spec
== spec
|| entry
->spec
== new_spec
);
1670 gcc_assert (new_spec
!= NULL_TREE
);
1671 entry
->spec
= new_spec
;
1678 /* Like register_specialization, but for local declarations. We are
1679 registering SPEC, an instantiation of TMPL. */
1682 register_local_specialization (tree spec
, tree tmpl
)
1686 slot
= pointer_map_insert (local_specializations
, tmpl
);
1690 /* TYPE is a class type. Returns true if TYPE is an explicitly
1691 specialized class. */
1694 explicit_class_specialization_p (tree type
)
1696 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type
))
1698 return !uses_template_parms (CLASSTYPE_TI_ARGS (type
));
1701 /* Print the list of functions at FNS, going through all the overloads
1702 for each element of the list. Alternatively, FNS can not be a
1703 TREE_LIST, in which case it will be printed together with all the
1706 MORE and *STR should respectively be FALSE and NULL when the function
1707 is called from the outside. They are used internally on recursive
1708 calls. print_candidates manages the two parameters and leaves NULL
1709 in *STR when it ends. */
1712 print_candidates_1 (tree fns
, bool more
, const char **str
)
1715 char *spaces
= NULL
;
1717 for (fn
= fns
; fn
; fn
= OVL_NEXT (fn
))
1718 if (TREE_CODE (fn
) == TREE_LIST
)
1720 for (fn2
= fn
; fn2
!= NULL_TREE
; fn2
= TREE_CHAIN (fn2
))
1721 print_candidates_1 (TREE_VALUE (fn2
),
1722 TREE_CHAIN (fn2
) || more
, str
);
1728 /* Pick the prefix string. */
1729 if (!more
&& !OVL_NEXT (fns
))
1731 error ("candidate is: %+#D", OVL_CURRENT (fn
));
1735 *str
= _("candidates are:");
1736 spaces
= get_spaces (*str
);
1738 error ("%s %+#D", *str
, OVL_CURRENT (fn
));
1739 *str
= spaces
? spaces
: *str
;
1749 /* Print the list of candidate FNS in an error message. FNS can also
1750 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1753 print_candidates (tree fns
)
1755 const char *str
= NULL
;
1756 print_candidates_1 (fns
, false, &str
);
1757 gcc_assert (str
== NULL
);
1760 /* Returns the template (one of the functions given by TEMPLATE_ID)
1761 which can be specialized to match the indicated DECL with the
1762 explicit template args given in TEMPLATE_ID. The DECL may be
1763 NULL_TREE if none is available. In that case, the functions in
1764 TEMPLATE_ID are non-members.
1766 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
1767 specialization of a member template.
1769 The TEMPLATE_COUNT is the number of references to qualifying
1770 template classes that appeared in the name of the function. See
1771 check_explicit_specialization for a more accurate description.
1773 TSK indicates what kind of template declaration (if any) is being
1774 declared. TSK_TEMPLATE indicates that the declaration given by
1775 DECL, though a FUNCTION_DECL, has template parameters, and is
1776 therefore a template function.
1778 The template args (those explicitly specified and those deduced)
1779 are output in a newly created vector *TARGS_OUT.
1781 If it is impossible to determine the result, an error message is
1782 issued. The error_mark_node is returned to indicate failure. */
1785 determine_specialization (tree template_id
,
1788 int need_member_template
,
1794 tree explicit_targs
;
1795 tree candidates
= NULL_TREE
;
1796 /* A TREE_LIST of templates of which DECL may be a specialization.
1797 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1798 corresponding TREE_PURPOSE is the set of template arguments that,
1799 when used to instantiate the template, would produce a function
1800 with the signature of DECL. */
1801 tree templates
= NULL_TREE
;
1803 cp_binding_level
*b
;
1805 *targs_out
= NULL_TREE
;
1807 if (template_id
== error_mark_node
|| decl
== error_mark_node
)
1808 return error_mark_node
;
1810 fns
= TREE_OPERAND (template_id
, 0);
1811 explicit_targs
= TREE_OPERAND (template_id
, 1);
1813 if (fns
== error_mark_node
)
1814 return error_mark_node
;
1816 /* Check for baselinks. */
1817 if (BASELINK_P (fns
))
1818 fns
= BASELINK_FUNCTIONS (fns
);
1820 if (!is_overloaded_fn (fns
))
1822 error ("%qD is not a function template", fns
);
1823 return error_mark_node
;
1826 /* Count the number of template headers specified for this
1829 for (b
= current_binding_level
;
1830 b
->kind
== sk_template_parms
;
1834 for (; fns
; fns
= OVL_NEXT (fns
))
1836 tree fn
= OVL_CURRENT (fns
);
1838 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
1840 tree decl_arg_types
;
1843 /* In case of explicit specialization, we need to check if
1844 the number of template headers appearing in the specialization
1845 is correct. This is usually done in check_explicit_specialization,
1846 but the check done there cannot be exhaustive when specializing
1847 member functions. Consider the following code:
1849 template <> void A<int>::f(int);
1850 template <> template <> void A<int>::f(int);
1852 Assuming that A<int> is not itself an explicit specialization
1853 already, the first line specializes "f" which is a non-template
1854 member function, whilst the second line specializes "f" which
1855 is a template member function. So both lines are syntactically
1856 correct, and check_explicit_specialization does not reject
1859 Here, we can do better, as we are matching the specialization
1860 against the declarations. We count the number of template
1861 headers, and we check if they match TEMPLATE_COUNT + 1
1862 (TEMPLATE_COUNT is the number of qualifying template classes,
1863 plus there must be another header for the member template
1866 Notice that if header_count is zero, this is not a
1867 specialization but rather a template instantiation, so there
1868 is no check we can perform here. */
1869 if (header_count
&& header_count
!= template_count
+ 1)
1872 /* Check that the number of template arguments at the
1873 innermost level for DECL is the same as for FN. */
1874 if (current_binding_level
->kind
== sk_template_parms
1875 && !current_binding_level
->explicit_spec_p
1876 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn
))
1877 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
1878 (current_template_parms
))))
1881 /* DECL might be a specialization of FN. */
1882 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1883 fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
1885 /* For a non-static member function, we need to make sure
1886 that the const qualification is the same. Since
1887 get_bindings does not try to merge the "this" parameter,
1888 we must do the comparison explicitly. */
1889 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
1890 && !same_type_p (TREE_VALUE (fn_arg_types
),
1891 TREE_VALUE (decl_arg_types
)))
1894 /* Skip the "this" parameter and, for constructors of
1895 classes with virtual bases, the VTT parameter. A
1896 full specialization of a constructor will have a VTT
1897 parameter, but a template never will. */
1899 = skip_artificial_parms_for (decl
, decl_arg_types
);
1901 = skip_artificial_parms_for (fn
, fn_arg_types
);
1903 /* Check that the number of function parameters matches.
1905 template <class T> void f(int i = 0);
1906 template <> void f<int>();
1907 The specialization f<int> is invalid but is not caught
1908 by get_bindings below. */
1909 if (list_length (fn_arg_types
) != list_length (decl_arg_types
))
1912 /* Function templates cannot be specializations; there are
1913 no partial specializations of functions. Therefore, if
1914 the type of DECL does not match FN, there is no
1916 if (tsk
== tsk_template
)
1918 if (compparms (fn_arg_types
, decl_arg_types
))
1919 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1923 /* See whether this function might be a specialization of this
1925 targs
= get_bindings (fn
, decl
, explicit_targs
, /*check_ret=*/true);
1928 /* We cannot deduce template arguments that when used to
1929 specialize TMPL will produce DECL. */
1932 /* Save this template, and the arguments deduced. */
1933 templates
= tree_cons (targs
, fn
, templates
);
1935 else if (need_member_template
)
1936 /* FN is an ordinary member function, and we need a
1937 specialization of a member template. */
1939 else if (TREE_CODE (fn
) != FUNCTION_DECL
)
1940 /* We can get IDENTIFIER_NODEs here in certain erroneous
1943 else if (!DECL_FUNCTION_MEMBER_P (fn
))
1944 /* This is just an ordinary non-member function. Nothing can
1945 be a specialization of that. */
1947 else if (DECL_ARTIFICIAL (fn
))
1948 /* Cannot specialize functions that are created implicitly. */
1952 tree decl_arg_types
;
1954 /* This is an ordinary member function. However, since
1955 we're here, we can assume it's enclosing class is a
1956 template class. For example,
1958 template <typename T> struct S { void f(); };
1959 template <> void S<int>::f() {}
1961 Here, S<int>::f is a non-template, but S<int> is a
1962 template class. If FN has the same type as DECL, we
1963 might be in business. */
1965 if (!DECL_TEMPLATE_INFO (fn
))
1966 /* Its enclosing class is an explicit specialization
1967 of a template class. This is not a candidate. */
1970 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl
)),
1971 TREE_TYPE (TREE_TYPE (fn
))))
1972 /* The return types differ. */
1975 /* Adjust the type of DECL in case FN is a static member. */
1976 decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
1977 if (DECL_STATIC_FUNCTION_P (fn
)
1978 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1979 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
1981 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
1984 candidates
= tree_cons (NULL_TREE
, fn
, candidates
);
1988 if (templates
&& TREE_CHAIN (templates
))
1994 It is possible for a specialization with a given function
1995 signature to be instantiated from more than one function
1996 template. In such cases, explicit specification of the
1997 template arguments must be used to uniquely identify the
1998 function template specialization being specialized.
2000 Note that here, there's no suggestion that we're supposed to
2001 determine which of the candidate templates is most
2002 specialized. However, we, also have:
2006 Partial ordering of overloaded function template
2007 declarations is used in the following contexts to select
2008 the function template to which a function template
2009 specialization refers:
2011 -- when an explicit specialization refers to a function
2014 So, we do use the partial ordering rules, at least for now.
2015 This extension can only serve to make invalid programs valid,
2016 so it's safe. And, there is strong anecdotal evidence that
2017 the committee intended the partial ordering rules to apply;
2018 the EDG front end has that behavior, and John Spicer claims
2019 that the committee simply forgot to delete the wording in
2020 [temp.expl.spec]. */
2021 tree tmpl
= most_specialized_instantiation (templates
);
2022 if (tmpl
!= error_mark_node
)
2025 TREE_CHAIN (templates
) = NULL_TREE
;
2029 if (templates
== NULL_TREE
&& candidates
== NULL_TREE
)
2031 error ("template-id %qD for %q+D does not match any template "
2032 "declaration", template_id
, decl
);
2033 if (header_count
&& header_count
!= template_count
+ 1)
2034 inform (input_location
, "saw %d %<template<>%>, need %d for "
2035 "specializing a member function template",
2036 header_count
, template_count
+ 1);
2037 return error_mark_node
;
2039 else if ((templates
&& TREE_CHAIN (templates
))
2040 || (candidates
&& TREE_CHAIN (candidates
))
2041 || (templates
&& candidates
))
2043 error ("ambiguous template specialization %qD for %q+D",
2045 candidates
= chainon (candidates
, templates
);
2046 print_candidates (candidates
);
2047 return error_mark_node
;
2050 /* We have one, and exactly one, match. */
2053 tree fn
= TREE_VALUE (candidates
);
2054 *targs_out
= copy_node (DECL_TI_ARGS (fn
));
2055 /* DECL is a re-declaration or partial instantiation of a template
2057 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2059 /* It was a specialization of an ordinary member function in a
2061 return DECL_TI_TEMPLATE (fn
);
2064 /* It was a specialization of a template. */
2065 targs
= DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates
)));
2066 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs
))
2068 *targs_out
= copy_node (targs
);
2069 SET_TMPL_ARGS_LEVEL (*targs_out
,
2070 TMPL_ARGS_DEPTH (*targs_out
),
2071 TREE_PURPOSE (templates
));
2074 *targs_out
= TREE_PURPOSE (templates
);
2075 return TREE_VALUE (templates
);
2078 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2079 but with the default argument values filled in from those in the
2083 copy_default_args_to_explicit_spec_1 (tree spec_types
,
2086 tree new_spec_types
;
2091 if (spec_types
== void_list_node
)
2092 return void_list_node
;
2094 /* Substitute into the rest of the list. */
2096 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types
),
2097 TREE_CHAIN (tmpl_types
));
2099 /* Add the default argument for this parameter. */
2100 return hash_tree_cons (TREE_PURPOSE (tmpl_types
),
2101 TREE_VALUE (spec_types
),
2105 /* DECL is an explicit specialization. Replicate default arguments
2106 from the template it specializes. (That way, code like:
2108 template <class T> void f(T = 3);
2109 template <> void f(double);
2112 works, as required.) An alternative approach would be to look up
2113 the correct default arguments at the call-site, but this approach
2114 is consistent with how implicit instantiations are handled. */
2117 copy_default_args_to_explicit_spec (tree decl
)
2122 tree new_spec_types
;
2126 tree object_type
= NULL_TREE
;
2127 tree in_charge
= NULL_TREE
;
2128 tree vtt
= NULL_TREE
;
2130 /* See if there's anything we need to do. */
2131 tmpl
= DECL_TI_TEMPLATE (decl
);
2132 tmpl_types
= TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl
)));
2133 for (t
= tmpl_types
; t
; t
= TREE_CHAIN (t
))
2134 if (TREE_PURPOSE (t
))
2139 old_type
= TREE_TYPE (decl
);
2140 spec_types
= TYPE_ARG_TYPES (old_type
);
2142 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2144 /* Remove the this pointer, but remember the object's type for
2146 object_type
= TREE_TYPE (TREE_VALUE (spec_types
));
2147 spec_types
= TREE_CHAIN (spec_types
);
2148 tmpl_types
= TREE_CHAIN (tmpl_types
);
2150 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
2152 /* DECL may contain more parameters than TMPL due to the extra
2153 in-charge parameter in constructors and destructors. */
2154 in_charge
= spec_types
;
2155 spec_types
= TREE_CHAIN (spec_types
);
2157 if (DECL_HAS_VTT_PARM_P (decl
))
2160 spec_types
= TREE_CHAIN (spec_types
);
2164 /* Compute the merged default arguments. */
2166 copy_default_args_to_explicit_spec_1 (spec_types
, tmpl_types
);
2168 /* Compute the new FUNCTION_TYPE. */
2172 new_spec_types
= hash_tree_cons (TREE_PURPOSE (vtt
),
2177 /* Put the in-charge parameter back. */
2178 new_spec_types
= hash_tree_cons (TREE_PURPOSE (in_charge
),
2179 TREE_VALUE (in_charge
),
2182 new_type
= build_method_type_directly (object_type
,
2183 TREE_TYPE (old_type
),
2187 new_type
= build_function_type (TREE_TYPE (old_type
),
2189 new_type
= cp_build_type_attribute_variant (new_type
,
2190 TYPE_ATTRIBUTES (old_type
));
2191 new_type
= build_exception_variant (new_type
,
2192 TYPE_RAISES_EXCEPTIONS (old_type
));
2193 TREE_TYPE (decl
) = new_type
;
2196 /* Check to see if the function just declared, as indicated in
2197 DECLARATOR, and in DECL, is a specialization of a function
2198 template. We may also discover that the declaration is an explicit
2199 instantiation at this point.
2201 Returns DECL, or an equivalent declaration that should be used
2202 instead if all goes well. Issues an error message if something is
2203 amiss. Returns error_mark_node if the error is not easily
2206 FLAGS is a bitmask consisting of the following flags:
2208 2: The function has a definition.
2209 4: The function is a friend.
2211 The TEMPLATE_COUNT is the number of references to qualifying
2212 template classes that appeared in the name of the function. For
2215 template <class T> struct S { void f(); };
2218 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2219 classes are not counted in the TEMPLATE_COUNT, so that in
2221 template <class T> struct S {};
2222 template <> struct S<int> { void f(); }
2223 template <> void S<int>::f();
2225 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2226 invalid; there should be no template <>.)
2228 If the function is a specialization, it is marked as such via
2229 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2230 is set up correctly, and it is added to the list of specializations
2231 for that template. */
2234 check_explicit_specialization (tree declarator
,
2239 int have_def
= flags
& 2;
2240 int is_friend
= flags
& 4;
2241 int specialization
= 0;
2242 int explicit_instantiation
= 0;
2243 int member_specialization
= 0;
2244 tree ctype
= DECL_CLASS_CONTEXT (decl
);
2245 tree dname
= DECL_NAME (decl
);
2250 if (!processing_specialization
)
2253 tsk
= tsk_excessive_parms
;
2256 tsk
= current_tmpl_spec_kind (template_count
);
2261 if (processing_specialization
)
2264 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2266 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2269 /* This could be something like:
2271 template <class T> void f(T);
2272 class S { friend void f<>(int); } */
2276 /* This case handles bogus declarations like template <>
2277 template <class T> void f<int>(); */
2279 error ("template-id %qD in declaration of primary template",
2286 case tsk_invalid_member_spec
:
2287 /* The error has already been reported in
2288 check_specialization_scope. */
2289 return error_mark_node
;
2291 case tsk_invalid_expl_inst
:
2292 error ("template parameter list used in explicit instantiation");
2298 error ("definition provided for explicit instantiation");
2300 explicit_instantiation
= 1;
2303 case tsk_excessive_parms
:
2304 case tsk_insufficient_parms
:
2305 if (tsk
== tsk_excessive_parms
)
2306 error ("too many template parameter lists in declaration of %qD",
2308 else if (template_header_count
)
2309 error("too few template parameter lists in declaration of %qD", decl
);
2311 error("explicit specialization of %qD must be introduced by "
2312 "%<template <>%>", decl
);
2316 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
2318 member_specialization
= 1;
2324 if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
2326 /* This case handles bogus declarations like template <>
2327 template <class T> void f<int>(); */
2329 if (uses_template_parms (declarator
))
2330 error ("function template partial specialization %qD "
2331 "is not allowed", declarator
);
2333 error ("template-id %qD in declaration of primary template",
2338 if (ctype
&& CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2339 /* This is a specialization of a member template, without
2340 specialization the containing class. Something like:
2342 template <class T> struct S {
2343 template <class U> void f (U);
2345 template <> template <class U> void S<int>::f(U) {}
2347 That's a specialization -- but of the entire template. */
2355 if (specialization
|| member_specialization
)
2357 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
2358 for (; t
; t
= TREE_CHAIN (t
))
2359 if (TREE_PURPOSE (t
))
2361 permerror (input_location
,
2362 "default argument specified in explicit specialization");
2367 if (specialization
|| member_specialization
|| explicit_instantiation
)
2369 tree tmpl
= NULL_TREE
;
2370 tree targs
= NULL_TREE
;
2372 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2373 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
2377 gcc_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
);
2382 /* If there is no class context, the explicit instantiation
2383 must be at namespace scope. */
2384 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl
));
2386 /* Find the namespace binding, using the declaration
2388 fns
= lookup_qualified_name (CP_DECL_CONTEXT (decl
), dname
,
2390 if (fns
== error_mark_node
|| !is_overloaded_fn (fns
))
2392 error ("%qD is not a template function", dname
);
2393 fns
= error_mark_node
;
2397 tree fn
= OVL_CURRENT (fns
);
2398 if (!is_associated_namespace (CP_DECL_CONTEXT (decl
),
2399 CP_DECL_CONTEXT (fn
)))
2400 error ("%qD is not declared in %qD",
2401 decl
, current_namespace
);
2405 declarator
= lookup_template_function (fns
, NULL_TREE
);
2408 if (declarator
== error_mark_node
)
2409 return error_mark_node
;
2411 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
2413 if (!explicit_instantiation
)
2414 /* A specialization in class scope. This is invalid,
2415 but the error will already have been flagged by
2416 check_specialization_scope. */
2417 return error_mark_node
;
2420 /* It's not valid to write an explicit instantiation in
2423 class C { template void f(); }
2425 This case is caught by the parser. However, on
2428 template class C { void f(); };
2430 (which is invalid) we can get here. The error will be
2437 else if (ctype
!= NULL_TREE
2438 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
2441 /* Find the list of functions in ctype that have the same
2442 name as the declared function. */
2443 tree name
= TREE_OPERAND (declarator
, 0);
2444 tree fns
= NULL_TREE
;
2447 if (constructor_name_p (name
, ctype
))
2449 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
2451 if (is_constructor
? !TYPE_HAS_USER_CONSTRUCTOR (ctype
)
2452 : !CLASSTYPE_DESTRUCTORS (ctype
))
2454 /* From [temp.expl.spec]:
2456 If such an explicit specialization for the member
2457 of a class template names an implicitly-declared
2458 special member function (clause _special_), the
2459 program is ill-formed.
2461 Similar language is found in [temp.explicit]. */
2462 error ("specialization of implicitly-declared special member function");
2463 return error_mark_node
;
2466 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
2469 if (!DECL_CONV_FN_P (decl
))
2471 idx
= lookup_fnfields_1 (ctype
, name
);
2473 fns
= VEC_index (tree
, CLASSTYPE_METHOD_VEC (ctype
), idx
);
2477 VEC(tree
,gc
) *methods
;
2480 /* For a type-conversion operator, we cannot do a
2481 name-based lookup. We might be looking for `operator
2482 int' which will be a specialization of `operator T'.
2483 So, we find *all* the conversion operators, and then
2484 select from them. */
2487 methods
= CLASSTYPE_METHOD_VEC (ctype
);
2489 for (idx
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
2490 VEC_iterate (tree
, methods
, idx
, ovl
);
2493 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl
)))
2494 /* There are no more conversion functions. */
2497 /* Glue all these conversion functions together
2498 with those we already have. */
2499 for (; ovl
; ovl
= OVL_NEXT (ovl
))
2500 fns
= ovl_cons (OVL_CURRENT (ovl
), fns
);
2504 if (fns
== NULL_TREE
)
2506 error ("no member function %qD declared in %qT", name
, ctype
);
2507 return error_mark_node
;
2510 TREE_OPERAND (declarator
, 0) = fns
;
2513 /* Figure out what exactly is being specialized at this point.
2514 Note that for an explicit instantiation, even one for a
2515 member function, we cannot tell apriori whether the
2516 instantiation is for a member template, or just a member
2517 function of a template class. Even if a member template is
2518 being instantiated, the member template arguments may be
2519 elided if they can be deduced from the rest of the
2521 tmpl
= determine_specialization (declarator
, decl
,
2523 member_specialization
,
2527 if (!tmpl
|| tmpl
== error_mark_node
)
2528 /* We couldn't figure out what this declaration was
2530 return error_mark_node
;
2533 tree gen_tmpl
= most_general_template (tmpl
);
2535 if (explicit_instantiation
)
2537 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2538 is done by do_decl_instantiation later. */
2540 int arg_depth
= TMPL_ARGS_DEPTH (targs
);
2541 int parm_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
2543 if (arg_depth
> parm_depth
)
2545 /* If TMPL is not the most general template (for
2546 example, if TMPL is a friend template that is
2547 injected into namespace scope), then there will
2548 be too many levels of TARGS. Remove some of them
2553 new_targs
= make_tree_vec (parm_depth
);
2554 for (i
= arg_depth
- parm_depth
; i
< arg_depth
; ++i
)
2555 TREE_VEC_ELT (new_targs
, i
- (arg_depth
- parm_depth
))
2556 = TREE_VEC_ELT (targs
, i
);
2560 return instantiate_template (tmpl
, targs
, tf_error
);
2563 /* If we thought that the DECL was a member function, but it
2564 turns out to be specializing a static member function,
2565 make DECL a static member function as well. */
2566 if (DECL_STATIC_FUNCTION_P (tmpl
)
2567 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
2568 revert_static_member_fn (decl
);
2570 /* If this is a specialization of a member template of a
2571 template class, we want to return the TEMPLATE_DECL, not
2572 the specialization of it. */
2573 if (tsk
== tsk_template
)
2575 tree result
= DECL_TEMPLATE_RESULT (tmpl
);
2576 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
2577 DECL_INITIAL (result
) = NULL_TREE
;
2581 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
2582 DECL_SOURCE_LOCATION (result
)
2583 = DECL_SOURCE_LOCATION (decl
);
2584 /* We want to use the argument list specified in the
2585 definition, not in the original declaration. */
2586 DECL_ARGUMENTS (result
) = DECL_ARGUMENTS (decl
);
2587 for (parm
= DECL_ARGUMENTS (result
); parm
;
2588 parm
= DECL_CHAIN (parm
))
2589 DECL_CONTEXT (parm
) = result
;
2591 return register_specialization (tmpl
, gen_tmpl
, targs
,
2595 /* Set up the DECL_TEMPLATE_INFO for DECL. */
2596 DECL_TEMPLATE_INFO (decl
) = build_template_info (tmpl
, targs
);
2598 /* Inherit default function arguments from the template
2599 DECL is specializing. */
2600 copy_default_args_to_explicit_spec (decl
);
2602 /* This specialization has the same protection as the
2603 template it specializes. */
2604 TREE_PRIVATE (decl
) = TREE_PRIVATE (gen_tmpl
);
2605 TREE_PROTECTED (decl
) = TREE_PROTECTED (gen_tmpl
);
2607 /* 7.1.1-1 [dcl.stc]
2609 A storage-class-specifier shall not be specified in an
2610 explicit specialization...
2612 The parser rejects these, so unless action is taken here,
2613 explicit function specializations will always appear with
2616 The action recommended by the C++ CWG in response to C++
2617 defect report 605 is to make the storage class and linkage
2618 of the explicit specialization match the templated function:
2620 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2622 if (tsk
== tsk_expl_spec
&& DECL_FUNCTION_TEMPLATE_P (gen_tmpl
))
2624 tree tmpl_func
= DECL_TEMPLATE_RESULT (gen_tmpl
);
2625 gcc_assert (TREE_CODE (tmpl_func
) == FUNCTION_DECL
);
2627 /* This specialization has the same linkage and visibility as
2628 the function template it specializes. */
2629 TREE_PUBLIC (decl
) = TREE_PUBLIC (tmpl_func
);
2630 if (! TREE_PUBLIC (decl
))
2632 DECL_INTERFACE_KNOWN (decl
) = 1;
2633 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2635 DECL_THIS_STATIC (decl
) = DECL_THIS_STATIC (tmpl_func
);
2636 if (DECL_VISIBILITY_SPECIFIED (tmpl_func
))
2638 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
2639 DECL_VISIBILITY (decl
) = DECL_VISIBILITY (tmpl_func
);
2643 /* If DECL is a friend declaration, declared using an
2644 unqualified name, the namespace associated with DECL may
2645 have been set incorrectly. For example, in:
2647 template <typename T> void f(T);
2649 struct S { friend void f<int>(int); }
2652 we will have set the DECL_CONTEXT for the friend
2653 declaration to N, rather than to the global namespace. */
2654 if (DECL_NAMESPACE_SCOPE_P (decl
))
2655 DECL_CONTEXT (decl
) = DECL_CONTEXT (tmpl
);
2657 if (is_friend
&& !have_def
)
2658 /* This is not really a declaration of a specialization.
2659 It's just the name of an instantiation. But, it's not
2660 a request for an instantiation, either. */
2661 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
2662 else if (DECL_CONSTRUCTOR_P (decl
) || DECL_DESTRUCTOR_P (decl
))
2663 /* This is indeed a specialization. In case of constructors
2664 and destructors, we need in-charge and not-in-charge
2665 versions in V3 ABI. */
2666 clone_function_decl (decl
, /*update_method_vec_p=*/0);
2668 /* Register this specialization so that we can find it
2670 decl
= register_specialization (decl
, gen_tmpl
, targs
, is_friend
, 0);
2677 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2678 parameters. These are represented in the same format used for
2679 DECL_TEMPLATE_PARMS. */
2682 comp_template_parms (const_tree parms1
, const_tree parms2
)
2687 if (parms1
== parms2
)
2690 for (p1
= parms1
, p2
= parms2
;
2691 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
2692 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
2694 tree t1
= TREE_VALUE (p1
);
2695 tree t2
= TREE_VALUE (p2
);
2698 gcc_assert (TREE_CODE (t1
) == TREE_VEC
);
2699 gcc_assert (TREE_CODE (t2
) == TREE_VEC
);
2701 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
2704 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
2706 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
2707 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
2709 /* If either of the template parameters are invalid, assume
2710 they match for the sake of error recovery. */
2711 if (parm1
== error_mark_node
|| parm2
== error_mark_node
)
2714 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
2717 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
2718 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1
)
2719 == TEMPLATE_TYPE_PARAMETER_PACK (parm2
)))
2721 else if (!same_type_p (TREE_TYPE (parm1
), TREE_TYPE (parm2
)))
2726 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
2727 /* One set of parameters has more parameters lists than the
2734 /* Determine whether PARM is a parameter pack. */
2737 template_parameter_pack_p (const_tree parm
)
2739 /* Determine if we have a non-type template parameter pack. */
2740 if (TREE_CODE (parm
) == PARM_DECL
)
2741 return (DECL_TEMPLATE_PARM_P (parm
)
2742 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)));
2743 if (TREE_CODE (parm
) == TEMPLATE_PARM_INDEX
)
2744 return TEMPLATE_PARM_PARAMETER_PACK (parm
);
2746 /* If this is a list of template parameters, we could get a
2747 TYPE_DECL or a TEMPLATE_DECL. */
2748 if (TREE_CODE (parm
) == TYPE_DECL
|| TREE_CODE (parm
) == TEMPLATE_DECL
)
2749 parm
= TREE_TYPE (parm
);
2751 /* Otherwise it must be a type template parameter. */
2752 return ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
2753 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
2754 && TEMPLATE_TYPE_PARAMETER_PACK (parm
));
2757 /* Determine if T is a function parameter pack. */
2760 function_parameter_pack_p (const_tree t
)
2762 if (t
&& TREE_CODE (t
) == PARM_DECL
)
2763 return FUNCTION_PARAMETER_PACK_P (t
);
2767 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
2768 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
2771 get_function_template_decl (const_tree primary_func_tmpl_inst
)
2773 if (! primary_func_tmpl_inst
2774 || TREE_CODE (primary_func_tmpl_inst
) != FUNCTION_DECL
2775 || ! primary_template_instantiation_p (primary_func_tmpl_inst
))
2778 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst
));
2781 /* Return true iff the function parameter PARAM_DECL was expanded
2782 from the function parameter pack PACK. */
2785 function_parameter_expanded_from_pack_p (tree param_decl
, tree pack
)
2787 if (DECL_ARTIFICIAL (param_decl
)
2788 || !function_parameter_pack_p (pack
))
2791 /* The parameter pack and its pack arguments have the same
2793 return DECL_PARM_INDEX (pack
) == DECL_PARM_INDEX (param_decl
);
2796 /* Determine whether ARGS describes a variadic template args list,
2797 i.e., one that is terminated by a template argument pack. */
2800 template_args_variadic_p (tree args
)
2805 if (args
== NULL_TREE
)
2808 args
= INNERMOST_TEMPLATE_ARGS (args
);
2809 nargs
= TREE_VEC_LENGTH (args
);
2814 last_parm
= TREE_VEC_ELT (args
, nargs
- 1);
2816 return ARGUMENT_PACK_P (last_parm
);
2819 /* Generate a new name for the parameter pack name NAME (an
2820 IDENTIFIER_NODE) that incorporates its */
2823 make_ith_pack_parameter_name (tree name
, int i
)
2825 /* Munge the name to include the parameter index. */
2826 #define NUMBUF_LEN 128
2827 char numbuf
[NUMBUF_LEN
];
2831 snprintf (numbuf
, NUMBUF_LEN
, "%i", i
);
2832 newname_len
= IDENTIFIER_LENGTH (name
)
2833 + strlen (numbuf
) + 2;
2834 newname
= (char*)alloca (newname_len
);
2835 snprintf (newname
, newname_len
,
2836 "%s#%i", IDENTIFIER_POINTER (name
), i
);
2837 return get_identifier (newname
);
2840 /* Return true if T is a primary function, class or alias template
2844 primary_template_instantiation_p (const_tree t
)
2849 if (TREE_CODE (t
) == FUNCTION_DECL
)
2850 return DECL_LANG_SPECIFIC (t
)
2851 && DECL_TEMPLATE_INSTANTIATION (t
)
2852 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t
));
2853 else if (CLASS_TYPE_P (t
) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2854 return CLASSTYPE_TEMPLATE_INSTANTIATION (t
)
2855 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
));
2857 && TYPE_TEMPLATE_INFO (t
)
2858 && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t
))
2859 && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t
)))
2864 /* Return true if PARM is a template template parameter. */
2867 template_template_parameter_p (const_tree parm
)
2869 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm
);
2872 /* Return the template parameters of T if T is a
2873 primary template instantiation, NULL otherwise. */
2876 get_primary_template_innermost_parameters (const_tree t
)
2878 tree parms
= NULL
, template_info
= NULL
;
2880 if ((template_info
= get_template_info (t
))
2881 && primary_template_instantiation_p (t
))
2882 parms
= INNERMOST_TEMPLATE_PARMS
2883 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info
)));
2888 /* Return the template parameters of the LEVELth level from the full list
2889 of template parameters PARMS. */
2892 get_template_parms_at_level (tree parms
, int level
)
2896 || TREE_CODE (parms
) != TREE_LIST
2897 || level
> TMPL_PARMS_DEPTH (parms
))
2900 for (p
= parms
; p
; p
= TREE_CHAIN (p
))
2901 if (TMPL_PARMS_DEPTH (p
) == level
)
2907 /* Returns the template arguments of T if T is a template instantiation,
2911 get_template_innermost_arguments (const_tree t
)
2913 tree args
= NULL
, template_info
= NULL
;
2915 if ((template_info
= get_template_info (t
))
2916 && TI_ARGS (template_info
))
2917 args
= INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info
));
2922 /* Return the argument pack elements of T if T is a template argument pack,
2926 get_template_argument_pack_elems (const_tree t
)
2928 if (TREE_CODE (t
) != TYPE_ARGUMENT_PACK
2929 && TREE_CODE (t
) != NONTYPE_ARGUMENT_PACK
)
2932 return ARGUMENT_PACK_ARGS (t
);
2935 /* Structure used to track the progress of find_parameter_packs_r. */
2936 struct find_parameter_pack_data
2938 /* TREE_LIST that will contain all of the parameter packs found by
2940 tree
* parameter_packs
;
2942 /* Set of AST nodes that have been visited by the traversal. */
2943 struct pointer_set_t
*visited
;
2946 /* Identifies all of the argument packs that occur in a template
2947 argument and appends them to the TREE_LIST inside DATA, which is a
2948 find_parameter_pack_data structure. This is a subroutine of
2949 make_pack_expansion and uses_parameter_packs. */
2951 find_parameter_packs_r (tree
*tp
, int *walk_subtrees
, void* data
)
2954 struct find_parameter_pack_data
* ppd
=
2955 (struct find_parameter_pack_data
*)data
;
2956 bool parameter_pack_p
= false;
2958 /* Handle type aliases/typedefs. */
2961 && TREE_CODE (TYPE_NAME (t
)) == TYPE_DECL
2962 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
2964 if (TYPE_TEMPLATE_INFO (t
))
2965 cp_walk_tree (&TYPE_TI_ARGS (t
),
2966 &find_parameter_packs_r
,
2972 /* Identify whether this is a parameter pack or not. */
2973 switch (TREE_CODE (t
))
2975 case TEMPLATE_PARM_INDEX
:
2976 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
2977 parameter_pack_p
= true;
2980 case TEMPLATE_TYPE_PARM
:
2981 t
= TYPE_MAIN_VARIANT (t
);
2982 case TEMPLATE_TEMPLATE_PARM
:
2983 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
2984 parameter_pack_p
= true;
2988 if (FUNCTION_PARAMETER_PACK_P (t
))
2990 /* We don't want to walk into the type of a PARM_DECL,
2991 because we don't want to see the type parameter pack. */
2993 parameter_pack_p
= true;
2998 parameter_pack_p
= true;
3001 /* Not a parameter pack. */
3005 if (parameter_pack_p
)
3007 /* Add this parameter pack to the list. */
3008 *ppd
->parameter_packs
= tree_cons (NULL_TREE
, t
, *ppd
->parameter_packs
);
3012 cp_walk_tree (&TYPE_CONTEXT (t
),
3013 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3015 /* This switch statement will return immediately if we don't find a
3017 switch (TREE_CODE (t
))
3019 case TEMPLATE_PARM_INDEX
:
3022 case BOUND_TEMPLATE_TEMPLATE_PARM
:
3023 /* Check the template itself. */
3024 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t
)),
3025 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3026 /* Check the template arguments. */
3027 cp_walk_tree (&TYPE_TI_ARGS (t
), &find_parameter_packs_r
, ppd
,
3032 case TEMPLATE_TYPE_PARM
:
3033 case TEMPLATE_TEMPLATE_PARM
:
3040 if (TYPE_PTRMEMFUNC_P (t
))
3046 if (TYPE_TEMPLATE_INFO (t
))
3047 cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
3048 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3055 cp_walk_tree (&TREE_TYPE (t
),
3056 &find_parameter_packs_r
, ppd
, ppd
->visited
);
3060 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t
), &find_parameter_packs_r
,
3065 case TYPE_PACK_EXPANSION
:
3066 case EXPR_PACK_EXPANSION
:
3071 cp_walk_tree (&TYPE_MAX_VALUE (t
), &find_parameter_packs_r
,
3076 case IDENTIFIER_NODE
:
3077 cp_walk_tree (&TREE_TYPE (t
), &find_parameter_packs_r
, ppd
,
3089 /* Determines if the expression or type T uses any parameter packs. */
3091 uses_parameter_packs (tree t
)
3093 tree parameter_packs
= NULL_TREE
;
3094 struct find_parameter_pack_data ppd
;
3095 ppd
.parameter_packs
= ¶meter_packs
;
3096 ppd
.visited
= pointer_set_create ();
3097 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3098 pointer_set_destroy (ppd
.visited
);
3099 return parameter_packs
!= NULL_TREE
;
3102 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3103 representation a base-class initializer into a parameter pack
3104 expansion. If all goes well, the resulting node will be an
3105 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3108 make_pack_expansion (tree arg
)
3111 tree parameter_packs
= NULL_TREE
;
3112 bool for_types
= false;
3113 struct find_parameter_pack_data ppd
;
3115 if (!arg
|| arg
== error_mark_node
)
3118 if (TREE_CODE (arg
) == TREE_LIST
)
3120 /* The only time we will see a TREE_LIST here is for a base
3121 class initializer. In this case, the TREE_PURPOSE will be a
3122 _TYPE node (representing the base class expansion we're
3123 initializing) and the TREE_VALUE will be a TREE_LIST
3124 containing the initialization arguments.
3126 The resulting expansion looks somewhat different from most
3127 expansions. Rather than returning just one _EXPANSION, we
3128 return a TREE_LIST whose TREE_PURPOSE is a
3129 TYPE_PACK_EXPANSION containing the bases that will be
3130 initialized. The TREE_VALUE will be identical to the
3131 original TREE_VALUE, which is a list of arguments that will
3132 be passed to each base. We do not introduce any new pack
3133 expansion nodes into the TREE_VALUE (although it is possible
3134 that some already exist), because the TREE_PURPOSE and
3135 TREE_VALUE all need to be expanded together with the same
3136 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3137 resulting TREE_PURPOSE will mention the parameter packs in
3138 both the bases and the arguments to the bases. */
3141 tree parameter_packs
= NULL_TREE
;
3143 /* Determine which parameter packs will be used by the base
3145 ppd
.visited
= pointer_set_create ();
3146 ppd
.parameter_packs
= ¶meter_packs
;
3147 cp_walk_tree (&TREE_PURPOSE (arg
), &find_parameter_packs_r
,
3150 if (parameter_packs
== NULL_TREE
)
3152 error ("base initializer expansion %<%T%> contains no parameter packs", arg
);
3153 pointer_set_destroy (ppd
.visited
);
3154 return error_mark_node
;
3157 if (TREE_VALUE (arg
) != void_type_node
)
3159 /* Collect the sets of parameter packs used in each of the
3160 initialization arguments. */
3161 for (value
= TREE_VALUE (arg
); value
; value
= TREE_CHAIN (value
))
3163 /* Determine which parameter packs will be expanded in this
3165 cp_walk_tree (&TREE_VALUE (value
), &find_parameter_packs_r
,
3170 pointer_set_destroy (ppd
.visited
);
3172 /* Create the pack expansion type for the base type. */
3173 purpose
= cxx_make_type (TYPE_PACK_EXPANSION
);
3174 SET_PACK_EXPANSION_PATTERN (purpose
, TREE_PURPOSE (arg
));
3175 PACK_EXPANSION_PARAMETER_PACKS (purpose
) = parameter_packs
;
3177 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3178 they will rarely be compared to anything. */
3179 SET_TYPE_STRUCTURAL_EQUALITY (purpose
);
3181 return tree_cons (purpose
, TREE_VALUE (arg
), NULL_TREE
);
3184 if (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
)
3187 /* Build the PACK_EXPANSION_* node. */
3189 ? cxx_make_type (TYPE_PACK_EXPANSION
)
3190 : make_node (EXPR_PACK_EXPANSION
);
3191 SET_PACK_EXPANSION_PATTERN (result
, arg
);
3192 if (TREE_CODE (result
) == EXPR_PACK_EXPANSION
)
3194 /* Propagate type and const-expression information. */
3195 TREE_TYPE (result
) = TREE_TYPE (arg
);
3196 TREE_CONSTANT (result
) = TREE_CONSTANT (arg
);
3199 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3200 they will rarely be compared to anything. */
3201 SET_TYPE_STRUCTURAL_EQUALITY (result
);
3203 /* Determine which parameter packs will be expanded. */
3204 ppd
.parameter_packs
= ¶meter_packs
;
3205 ppd
.visited
= pointer_set_create ();
3206 cp_walk_tree (&arg
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3207 pointer_set_destroy (ppd
.visited
);
3209 /* Make sure we found some parameter packs. */
3210 if (parameter_packs
== NULL_TREE
)
3213 error ("expansion pattern %<%T%> contains no argument packs", arg
);
3215 error ("expansion pattern %<%E%> contains no argument packs", arg
);
3216 return error_mark_node
;
3218 PACK_EXPANSION_PARAMETER_PACKS (result
) = parameter_packs
;
3220 PACK_EXPANSION_LOCAL_P (result
) = at_function_scope_p ();
3225 /* Checks T for any "bare" parameter packs, which have not yet been
3226 expanded, and issues an error if any are found. This operation can
3227 only be done on full expressions or types (e.g., an expression
3228 statement, "if" condition, etc.), because we could have expressions like:
3230 foo(f(g(h(args)))...)
3232 where "args" is a parameter pack. check_for_bare_parameter_packs
3233 should not be called for the subexpressions args, h(args),
3234 g(h(args)), or f(g(h(args))), because we would produce erroneous
3237 Returns TRUE and emits an error if there were bare parameter packs,
3238 returns FALSE otherwise. */
3240 check_for_bare_parameter_packs (tree t
)
3242 tree parameter_packs
= NULL_TREE
;
3243 struct find_parameter_pack_data ppd
;
3245 if (!processing_template_decl
|| !t
|| t
== error_mark_node
)
3248 if (TREE_CODE (t
) == TYPE_DECL
)
3251 ppd
.parameter_packs
= ¶meter_packs
;
3252 ppd
.visited
= pointer_set_create ();
3253 cp_walk_tree (&t
, &find_parameter_packs_r
, &ppd
, ppd
.visited
);
3254 pointer_set_destroy (ppd
.visited
);
3256 if (parameter_packs
)
3258 error ("parameter packs not expanded with %<...%>:");
3259 while (parameter_packs
)
3261 tree pack
= TREE_VALUE (parameter_packs
);
3262 tree name
= NULL_TREE
;
3264 if (TREE_CODE (pack
) == TEMPLATE_TYPE_PARM
3265 || TREE_CODE (pack
) == TEMPLATE_TEMPLATE_PARM
)
3266 name
= TYPE_NAME (pack
);
3267 else if (TREE_CODE (pack
) == TEMPLATE_PARM_INDEX
)
3268 name
= DECL_NAME (TEMPLATE_PARM_DECL (pack
));
3270 name
= DECL_NAME (pack
);
3273 inform (input_location
, " %qD", name
);
3275 inform (input_location
, " <anonymous>");
3277 parameter_packs
= TREE_CHAIN (parameter_packs
);
3286 /* Expand any parameter packs that occur in the template arguments in
3289 expand_template_argument_pack (tree args
)
3291 tree result_args
= NULL_TREE
;
3292 int in_arg
, out_arg
= 0, nargs
= args
? TREE_VEC_LENGTH (args
) : 0;
3293 int num_result_args
= -1;
3294 int non_default_args_count
= -1;
3296 /* First, determine if we need to expand anything, and the number of
3297 slots we'll need. */
3298 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3300 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3301 if (arg
== NULL_TREE
)
3303 if (ARGUMENT_PACK_P (arg
))
3305 int num_packed
= TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg
));
3306 if (num_result_args
< 0)
3307 num_result_args
= in_arg
+ num_packed
;
3309 num_result_args
+= num_packed
;
3313 if (num_result_args
>= 0)
3318 /* If no expansion is necessary, we're done. */
3319 if (num_result_args
< 0)
3322 /* Expand arguments. */
3323 result_args
= make_tree_vec (num_result_args
);
3324 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
))
3325 non_default_args_count
=
3326 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args
);
3327 for (in_arg
= 0; in_arg
< nargs
; ++in_arg
)
3329 tree arg
= TREE_VEC_ELT (args
, in_arg
);
3330 if (ARGUMENT_PACK_P (arg
))
3332 tree packed
= ARGUMENT_PACK_ARGS (arg
);
3333 int i
, num_packed
= TREE_VEC_LENGTH (packed
);
3334 for (i
= 0; i
< num_packed
; ++i
, ++out_arg
)
3335 TREE_VEC_ELT (result_args
, out_arg
) = TREE_VEC_ELT(packed
, i
);
3336 if (non_default_args_count
> 0)
3337 non_default_args_count
+= num_packed
;
3341 TREE_VEC_ELT (result_args
, out_arg
) = arg
;
3345 if (non_default_args_count
>= 0)
3346 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args
, non_default_args_count
);
3350 /* Checks if DECL shadows a template parameter.
3352 [temp.local]: A template-parameter shall not be redeclared within its
3353 scope (including nested scopes).
3355 Emits an error and returns TRUE if the DECL shadows a parameter,
3356 returns FALSE otherwise. */
3359 check_template_shadow (tree decl
)
3363 /* If we're not in a template, we can't possibly shadow a template
3365 if (!current_template_parms
)
3368 /* Figure out what we're shadowing. */
3369 if (TREE_CODE (decl
) == OVERLOAD
)
3370 decl
= OVL_CURRENT (decl
);
3371 olddecl
= innermost_non_namespace_value (DECL_NAME (decl
));
3373 /* If there's no previous binding for this name, we're not shadowing
3374 anything, let alone a template parameter. */
3378 /* If we're not shadowing a template parameter, we're done. Note
3379 that OLDDECL might be an OVERLOAD (or perhaps even an
3380 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3382 if (!DECL_P (olddecl
) || !DECL_TEMPLATE_PARM_P (olddecl
))
3385 /* We check for decl != olddecl to avoid bogus errors for using a
3386 name inside a class. We check TPFI to avoid duplicate errors for
3387 inline member templates. */
3389 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
3392 error ("declaration of %q+#D", decl
);
3393 error (" shadows template parm %q+#D", olddecl
);
3397 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3398 ORIG_LEVEL, DECL, and TYPE. NUM_SIBLINGS is the total number of
3399 template parameters. */
3402 build_template_parm_index (int index
,
3409 tree t
= make_node (TEMPLATE_PARM_INDEX
);
3410 TEMPLATE_PARM_IDX (t
) = index
;
3411 TEMPLATE_PARM_LEVEL (t
) = level
;
3412 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
3413 TEMPLATE_PARM_NUM_SIBLINGS (t
) = num_siblings
;
3414 TEMPLATE_PARM_DECL (t
) = decl
;
3415 TREE_TYPE (t
) = type
;
3416 TREE_CONSTANT (t
) = TREE_CONSTANT (decl
);
3417 TREE_READONLY (t
) = TREE_READONLY (decl
);
3422 /* Find the canonical type parameter for the given template type
3423 parameter. Returns the canonical type parameter, which may be TYPE
3424 if no such parameter existed. */
3427 canonical_type_parameter (tree type
)
3430 int idx
= TEMPLATE_TYPE_IDX (type
);
3431 if (!canonical_template_parms
)
3432 canonical_template_parms
= VEC_alloc (tree
, gc
, idx
+1);
3434 while (VEC_length (tree
, canonical_template_parms
) <= (unsigned)idx
)
3435 VEC_safe_push (tree
, gc
, canonical_template_parms
, NULL_TREE
);
3437 list
= VEC_index (tree
, canonical_template_parms
, idx
);
3438 while (list
&& !comptypes (type
, TREE_VALUE (list
), COMPARE_STRUCTURAL
))
3439 list
= TREE_CHAIN (list
);
3442 return TREE_VALUE (list
);
3445 VEC_replace(tree
, canonical_template_parms
, idx
,
3446 tree_cons (NULL_TREE
, type
,
3447 VEC_index (tree
, canonical_template_parms
, idx
)));
3452 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3453 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3454 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3455 new one is created. */
3458 reduce_template_parm_level (tree index
, tree type
, int levels
, tree args
,
3459 tsubst_flags_t complain
)
3461 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
3462 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
3463 != TEMPLATE_PARM_LEVEL (index
) - levels
)
3464 || !same_type_p (type
, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index
))))
3466 tree orig_decl
= TEMPLATE_PARM_DECL (index
);
3469 decl
= build_decl (DECL_SOURCE_LOCATION (orig_decl
),
3470 TREE_CODE (orig_decl
), DECL_NAME (orig_decl
), type
);
3471 TREE_CONSTANT (decl
) = TREE_CONSTANT (orig_decl
);
3472 TREE_READONLY (decl
) = TREE_READONLY (orig_decl
);
3473 DECL_ARTIFICIAL (decl
) = 1;
3474 SET_DECL_TEMPLATE_PARM_P (decl
);
3476 t
= build_template_parm_index (TEMPLATE_PARM_IDX (index
),
3477 TEMPLATE_PARM_LEVEL (index
) - levels
,
3478 TEMPLATE_PARM_ORIG_LEVEL (index
),
3479 TEMPLATE_PARM_NUM_SIBLINGS (index
),
3481 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
3482 TEMPLATE_PARM_PARAMETER_PACK (t
)
3483 = TEMPLATE_PARM_PARAMETER_PACK (index
);
3485 /* Template template parameters need this. */
3486 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3487 DECL_TEMPLATE_PARMS (decl
) = tsubst_template_parms
3488 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
)),
3492 return TEMPLATE_PARM_DESCENDANTS (index
);
3495 /* Process information from new template parameter PARM and append it
3496 to the LIST being built. This new parameter is a non-type
3497 parameter iff IS_NON_TYPE is true. This new parameter is a
3498 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
3499 is in PARM_LOC. NUM_TEMPLATE_PARMS is the size of the template
3500 parameter list PARM belongs to. This is used used to create a
3501 proper canonical type for the type of PARM that is to be created,
3502 iff PARM is a type. If the size is not known, this parameter shall
3506 process_template_parm (tree list
, location_t parm_loc
, tree parm
,
3507 bool is_non_type
, bool is_parameter_pack
,
3508 unsigned num_template_parms
)
3515 gcc_assert (TREE_CODE (parm
) == TREE_LIST
);
3516 defval
= TREE_PURPOSE (parm
);
3520 tree p
= tree_last (list
);
3522 if (p
&& TREE_VALUE (p
) != error_mark_node
)
3525 if (TREE_CODE (p
) == TYPE_DECL
|| TREE_CODE (p
) == TEMPLATE_DECL
)
3526 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
3528 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
3538 parm
= TREE_VALUE (parm
);
3540 SET_DECL_TEMPLATE_PARM_P (parm
);
3542 if (TREE_TYPE (parm
) == error_mark_node
)
3544 err_parm_list
= build_tree_list (defval
, parm
);
3545 TREE_VALUE (err_parm_list
) = error_mark_node
;
3546 return chainon (list
, err_parm_list
);
3552 The top-level cv-qualifiers on the template-parameter are
3553 ignored when determining its type. */
3554 TREE_TYPE (parm
) = TYPE_MAIN_VARIANT (TREE_TYPE (parm
));
3555 if (invalid_nontype_parm_type_p (TREE_TYPE (parm
), 1))
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
);
3562 if (uses_parameter_packs (TREE_TYPE (parm
)) && !is_parameter_pack
)
3564 /* This template parameter is not a parameter pack, but it
3565 should be. Complain about "bare" parameter packs. */
3566 check_for_bare_parameter_packs (TREE_TYPE (parm
));
3568 /* Recover by calling this a parameter pack. */
3569 is_parameter_pack
= true;
3573 /* A template parameter is not modifiable. */
3574 TREE_CONSTANT (parm
) = 1;
3575 TREE_READONLY (parm
) = 1;
3576 decl
= build_decl (parm_loc
,
3577 CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
3578 TREE_CONSTANT (decl
) = 1;
3579 TREE_READONLY (decl
) = 1;
3580 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
3581 = build_template_parm_index (idx
, processing_template_decl
,
3582 processing_template_decl
,
3584 decl
, TREE_TYPE (parm
));
3586 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
))
3587 = is_parameter_pack
;
3592 parm
= TREE_VALUE (TREE_VALUE (parm
));
3594 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
3596 t
= cxx_make_type (TEMPLATE_TEMPLATE_PARM
);
3597 /* This is for distinguishing between real templates and template
3598 template parameters */
3599 TREE_TYPE (parm
) = t
;
3600 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
3605 t
= cxx_make_type (TEMPLATE_TYPE_PARM
);
3606 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
3607 decl
= build_decl (parm_loc
,
3608 TYPE_DECL
, parm
, t
);
3611 TYPE_NAME (t
) = decl
;
3612 TYPE_STUB_DECL (t
) = decl
;
3614 TEMPLATE_TYPE_PARM_INDEX (t
)
3615 = build_template_parm_index (idx
, processing_template_decl
,
3616 processing_template_decl
,
3618 decl
, TREE_TYPE (parm
));
3619 TEMPLATE_TYPE_PARAMETER_PACK (t
) = is_parameter_pack
;
3620 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3622 DECL_ARTIFICIAL (decl
) = 1;
3623 SET_DECL_TEMPLATE_PARM_P (decl
);
3625 parm
= build_tree_list (defval
, parm
);
3626 return chainon (list
, parm
);
3629 /* The end of a template parameter list has been reached. Process the
3630 tree list into a parameter vector, converting each parameter into a more
3631 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3635 end_template_parm_list (tree parms
)
3639 tree saved_parmlist
= make_tree_vec (list_length (parms
));
3641 current_template_parms
3642 = tree_cons (size_int (processing_template_decl
),
3643 saved_parmlist
, current_template_parms
);
3645 for (parm
= parms
, nparms
= 0; parm
; parm
= next
, nparms
++)
3647 next
= TREE_CHAIN (parm
);
3648 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
3649 TREE_CHAIN (parm
) = NULL_TREE
;
3652 --processing_template_parmlist
;
3654 return saved_parmlist
;
3657 /* Create a new type almost identical to TYPE but which has the
3658 following differences:
3660 1/ T has a new TEMPLATE_PARM_INDEX that carries the new number of
3661 template sibling parameters of T.
3663 2/ T has a new canonical type that matches the new number
3666 3/ From now on, T is going to be what lookups referring to the
3667 name of TYPE will return. No lookup should return TYPE anymore.
3669 NUM_PARMS is the new number of sibling parms TYPE belongs to.
3671 This is a subroutine of fixup_template_parms. */
3674 fixup_template_type_parm_type (tree type
, int num_parms
)
3676 tree orig_idx
= TEMPLATE_TYPE_PARM_INDEX (type
), idx
;
3678 /* This is the decl which name is inserted into the symbol table for
3679 the template parm type. So whenever we lookup the type name, this
3680 is the DECL we get. */
3683 /* Do not fix up the type twice. */
3684 if (orig_idx
&& TEMPLATE_PARM_NUM_SIBLINGS (orig_idx
) != 0)
3687 t
= copy_type (type
);
3688 decl
= TYPE_NAME (t
);
3690 TYPE_MAIN_VARIANT (t
) = t
;
3691 TYPE_NEXT_VARIANT (t
)= NULL_TREE
;
3692 TYPE_POINTER_TO (t
) = 0;
3693 TYPE_REFERENCE_TO (t
) = 0;
3695 idx
= build_template_parm_index (TEMPLATE_PARM_IDX (orig_idx
),
3696 TEMPLATE_PARM_LEVEL (orig_idx
),
3697 TEMPLATE_PARM_ORIG_LEVEL (orig_idx
),
3700 TEMPLATE_PARM_DESCENDANTS (idx
) = TEMPLATE_PARM_DESCENDANTS (orig_idx
);
3701 TEMPLATE_PARM_PARAMETER_PACK (idx
) = TEMPLATE_PARM_PARAMETER_PACK (orig_idx
);
3702 TEMPLATE_TYPE_PARM_INDEX (t
) = idx
;
3704 TYPE_STUB_DECL (t
) = decl
;
3705 TEMPLATE_TYPE_DECL (t
) = decl
;
3706 if (TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
3707 TREE_TYPE (DECL_TEMPLATE_RESULT (decl
)) = t
;
3709 /* Update the type associated to the type name stored in the symbol
3710 table. Now, whenever the type name is looked up, the resulting
3711 type is properly fixed up. */
3712 TREE_TYPE (decl
) = t
;
3714 TYPE_CANONICAL (t
) = canonical_type_parameter (t
);
3719 /* Create and return a new TEMPLATE_PARM_INDEX that is almost
3720 identical to I, but that is fixed up as to:
3722 1/ carry the number of sibling parms (NUM_PARMS) of the template
3723 parm represented by I.
3725 2/ replace all references to template parm types declared before I
3726 (in the same template parm list as I) by references to template
3727 parm types contained in ARGS. ARGS should contain the list of
3728 template parms that have been fixed up so far, in a form suitable
3729 to be passed to tsubst.
3731 This is a subroutine of fixup_template_parms. */
3734 fixup_template_parm_index (tree i
, tree args
, int num_parms
)
3736 tree index
, decl
, type
;
3739 || TREE_CODE (i
) != TEMPLATE_PARM_INDEX
3740 /* Do not fix up the index twice. */
3741 || (TEMPLATE_PARM_NUM_SIBLINGS (i
) != 0))
3744 decl
= TEMPLATE_PARM_DECL (i
);
3745 type
= TREE_TYPE (decl
);
3747 index
= build_template_parm_index (TEMPLATE_PARM_IDX (i
),
3748 TEMPLATE_PARM_LEVEL (i
),
3749 TEMPLATE_PARM_ORIG_LEVEL (i
),
3753 TEMPLATE_PARM_DESCENDANTS (index
) = TEMPLATE_PARM_DESCENDANTS (i
);
3754 TEMPLATE_PARM_PARAMETER_PACK (index
) = TEMPLATE_PARM_PARAMETER_PACK (i
);
3756 type
= tsubst (type
, args
, tf_none
, NULL_TREE
);
3758 TREE_TYPE (decl
) = type
;
3759 TREE_TYPE (index
) = type
;
3765 This is a subroutine of fixup_template_parms.
3767 It computes the canonical type of the type of the template
3768 parameter PARM_DESC and update all references to that type so that
3769 they use the newly computed canonical type. No access check is
3770 performed during the fixup. PARM_DESC is a TREE_LIST which
3771 TREE_VALUE is the template parameter and its TREE_PURPOSE is the
3772 default argument of the template parm if any. IDX is the index of
3773 the template parameter, starting at 0. NUM_PARMS is the number of
3774 template parameters in the set PARM_DESC belongs to. ARGLIST is a
3775 TREE_VEC containing the full set of template parameters in a form
3776 suitable to be passed to substs functions as their ARGS
3777 argument. This is what current_template_args returns for a given
3778 template. The innermost vector of args in ARGLIST is the set of
3779 template parms that have been fixed up so far. This function adds
3780 the fixed up parameter into that vector. */
3783 fixup_template_parm (tree parm_desc
,
3788 tree parm
= TREE_VALUE (parm_desc
);
3789 tree fixedup_args
= INNERMOST_TEMPLATE_ARGS (arglist
);
3791 push_deferring_access_checks (dk_no_check
);
3793 if (TREE_CODE (parm
) == TYPE_DECL
)
3795 /* PARM is a template type parameter. Fix up its type, add
3796 the fixed-up template parm to the vector of fixed-up
3797 template parms so far, and substitute the fixed-up
3798 template parms into the default argument of this
3801 fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3802 TREE_TYPE (parm
) = t
;
3804 TREE_VEC_ELT (fixedup_args
, idx
) = template_parm_to_arg (parm_desc
);
3806 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
3808 /* PARM is a template template parameter. This is going to
3810 tree tparms
, targs
, innermost_args
, t
;
3813 /* First, fix up the parms of the template template parm
3814 because the parms are involved in defining the new canonical
3815 type of the template template parm. */
3817 /* So we need to substitute the template parm types that have
3818 been fixed up so far into the template parms of this template
3819 template parm. E.g, consider this:
3821 template<class T, template<T u> class TT> class S;
3823 In this case we want to substitute T into the
3824 template parameters of TT.
3826 So let's walk the template parms of PARM here, and
3827 tsubst ARGLIST into into each of the template
3830 /* For this substitution we need to build the full set of
3831 template parameters and use that as arguments for the
3832 tsubsting function. */
3833 tparms
= INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm
));
3835 /* This will contain the innermost parms of PARM into which
3836 we have substituted so far. */
3837 innermost_args
= make_tree_vec (TREE_VEC_LENGTH (tparms
));
3838 targs
= add_to_template_args (arglist
, innermost_args
);
3839 for (j
= 0; j
< TREE_VEC_LENGTH (tparms
); ++j
)
3843 parameter
= TREE_VEC_ELT (tparms
, j
);
3845 /* INNERMOST_ARGS needs to have at least the same number
3846 of elements as the index PARAMETER, ortherwise
3847 tsubsting into PARAMETER will result in partially
3848 instantiating it, reducing its tempate parm
3849 level. Let's tactically fill INNERMOST_ARGS for that
3851 TREE_VEC_ELT (innermost_args
, j
) =
3852 template_parm_to_arg (parameter
);
3854 fixup_template_parm (parameter
, j
,
3855 TREE_VEC_LENGTH (tparms
),
3859 /* Now fix up the type of the template template parm. */
3861 t
= fixup_template_type_parm_type (TREE_TYPE (parm
), num_parms
);
3862 TREE_TYPE (parm
) = t
;
3864 TREE_VEC_ELT (fixedup_args
, idx
) =
3865 template_parm_to_arg (parm_desc
);
3867 else if (TREE_CODE (parm
) == PARM_DECL
)
3869 /* PARM is a non-type template parameter. We need to:
3871 * Fix up its TEMPLATE_PARM_INDEX to make it carry the
3872 proper number of sibling parameters.
3874 * Make lookups of the template parameter return a reference
3875 to the fixed-up index. No lookup should return references
3876 to the former index anymore.
3878 * Substitute the template parms that got fixed up so far
3880 * into the type of PARM. */
3882 tree index
= DECL_INITIAL (parm
);
3884 /* PUSHED_DECL is the decl added to the symbol table with
3885 the name of the parameter. E,g:
3887 template<class T, T u> //#0
3888 auto my_function(T t) -> decltype(u); //#1
3890 Here, when looking up u at //#1, we get the decl of u
3891 resulting from the declaration in #0. This is what
3892 PUSHED_DECL is. We need to replace the reference to the
3893 old TEMPLATE_PARM_INDEX carried by PUSHED_DECL by the
3894 fixed-up TEMPLATE_PARM_INDEX. */
3895 tree pushed_decl
= TEMPLATE_PARM_DECL (index
);
3897 /* Let's fix up the TEMPLATE_PARM_INDEX then. Note that we must
3898 fixup the type of PUSHED_DECL as well and luckily
3899 fixup_template_parm_index does it for us too. */
3900 tree fixed_up_index
=
3901 fixup_template_parm_index (index
, arglist
, num_parms
);
3903 DECL_INITIAL (pushed_decl
) = DECL_INITIAL (parm
) = fixed_up_index
;
3905 /* Add this fixed up PARM to the template parms we've fixed
3906 up so far and use that to substitute the fixed-up
3907 template parms into the type of PARM. */
3908 TREE_VEC_ELT (fixedup_args
, idx
) =
3909 template_parm_to_arg (parm_desc
);
3910 TREE_TYPE (parm
) = tsubst (TREE_TYPE (parm
), arglist
,
3911 tf_none
, NULL_TREE
);
3914 TREE_PURPOSE (parm_desc
) =
3915 tsubst_template_arg (TREE_PURPOSE (parm_desc
),
3916 arglist
, tf_none
, parm
);
3918 pop_deferring_access_checks ();
3921 /* Walk the current template parms and properly compute the canonical
3922 types of the dependent types created during
3923 cp_parser_template_parameter_list. */
3926 fixup_template_parms (void)
3933 parameter_vec
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
3934 if (parameter_vec
== NULL_TREE
)
3937 num_parms
= TREE_VEC_LENGTH (parameter_vec
);
3939 /* This vector contains the current innermost template parms that
3940 have been fixed up so far. The form of FIXEDUP_ARGS is suitable
3941 to be passed to tsubst* functions as their ARGS argument. */
3942 fixedup_args
= make_tree_vec (num_parms
);
3944 /* This vector contains the full set of template parms in a form
3945 suitable to be passed to substs functions as their ARGS
3947 arglist
= current_template_args ();
3948 arglist
= add_outermost_template_args (arglist
, fixedup_args
);
3950 /* Let's do the proper fixup now. */
3951 for (i
= 0; i
< num_parms
; ++i
)
3952 fixup_template_parm (TREE_VEC_ELT (parameter_vec
, i
),
3953 i
, num_parms
, arglist
);
3956 /* end_template_decl is called after a template declaration is seen. */
3959 end_template_decl (void)
3961 reset_specialization ();
3963 if (! processing_template_decl
)
3966 /* This matches the pushlevel in begin_template_parm_list. */
3969 --processing_template_decl
;
3970 current_template_parms
= TREE_CHAIN (current_template_parms
);
3973 /* Takes a TREE_LIST representing a template parameter and convert it
3974 into an argument suitable to be passed to the type substitution
3975 functions. Note that If the TREE_LIST contains an error_mark
3976 node, the returned argument is error_mark_node. */
3979 template_parm_to_arg (tree t
)
3983 || TREE_CODE (t
) != TREE_LIST
)
3986 if (error_operand_p (TREE_VALUE (t
)))
3987 return error_mark_node
;
3991 if (TREE_CODE (t
) == TYPE_DECL
3992 || TREE_CODE (t
) == TEMPLATE_DECL
)
3996 if (TEMPLATE_TYPE_PARAMETER_PACK (t
))
3998 /* Turn this argument into a TYPE_ARGUMENT_PACK
3999 with a single element, which expands T. */
4000 tree vec
= make_tree_vec (1);
4001 #ifdef ENABLE_CHECKING
4002 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4003 (vec
, TREE_VEC_LENGTH (vec
));
4005 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4007 t
= cxx_make_type (TYPE_ARGUMENT_PACK
);
4008 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4013 t
= DECL_INITIAL (t
);
4015 if (TEMPLATE_PARM_PARAMETER_PACK (t
))
4017 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4018 with a single element, which expands T. */
4019 tree vec
= make_tree_vec (1);
4020 tree type
= TREE_TYPE (TEMPLATE_PARM_DECL (t
));
4021 #ifdef ENABLE_CHECKING
4022 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
4023 (vec
, TREE_VEC_LENGTH (vec
));
4025 TREE_VEC_ELT (vec
, 0) = make_pack_expansion (t
);
4027 t
= make_node (NONTYPE_ARGUMENT_PACK
);
4028 SET_ARGUMENT_PACK_ARGS (t
, vec
);
4029 TREE_TYPE (t
) = type
;
4035 /* This function returns TRUE if PARM_PACK is a template parameter
4036 pack and if ARG_PACK is what template_parm_to_arg returned when
4037 passed PARM_PACK. */
4040 arg_from_parm_pack_p (tree arg_pack
, tree parm_pack
)
4042 /* For clarity in the comments below let's use the representation
4043 argument_pack<elements>' to denote an argument pack and its
4046 In the 'if' block below, we want to detect cases where
4047 ARG_PACK is argument_pack<PARM_PACK...>. I.e, we want to
4048 check if ARG_PACK is an argument pack which sole element is
4049 the expansion of PARM_PACK. That argument pack is typically
4050 created by template_parm_to_arg when passed a parameter
4054 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
4055 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0)))
4057 tree expansion
= TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
), 0);
4058 tree pattern
= PACK_EXPANSION_PATTERN (expansion
);
4059 /* So we have an argument_pack<P...>. We want to test if P
4060 is actually PARM_PACK. We will not use cp_tree_equal to
4061 test P and PARM_PACK because during type fixup (by
4062 fixup_template_parm) P can be a pre-fixup version of a
4063 type and PARM_PACK be its post-fixup version.
4064 cp_tree_equal would consider them as different even
4065 though we would want to consider them compatible for our
4066 precise purpose here.
4068 Thus we are going to consider that P and PARM_PACK are
4069 compatible if they have the same DECL. */
4070 if ((/* If ARG_PACK is a type parameter pack named by the
4071 same DECL as parm_pack ... */
4073 && TYPE_P (parm_pack
)
4074 && TYPE_NAME (pattern
) == TYPE_NAME (parm_pack
))
4075 /* ... or if PARM_PACK is a non-type parameter named by the
4076 same DECL as ARG_PACK. Note that PARM_PACK being a
4077 non-type parameter means it's either a PARM_DECL or a
4078 TEMPLATE_PARM_INDEX. */
4079 || (TREE_CODE (pattern
) == TEMPLATE_PARM_INDEX
4080 && ((TREE_CODE (parm_pack
) == PARM_DECL
4081 && (TEMPLATE_PARM_DECL (pattern
)
4082 == TEMPLATE_PARM_DECL (DECL_INITIAL (parm_pack
))))
4083 || (TREE_CODE (parm_pack
) == TEMPLATE_PARM_INDEX
4084 && (TEMPLATE_PARM_DECL (pattern
)
4085 == TEMPLATE_PARM_DECL (parm_pack
))))))
4086 && template_parameter_pack_p (pattern
))
4092 /* Within the declaration of a template, return all levels of template
4093 parameters that apply. The template parameters are represented as
4094 a TREE_VEC, in the form documented in cp-tree.h for template
4098 current_template_args (void)
4101 tree args
= NULL_TREE
;
4102 int length
= TMPL_PARMS_DEPTH (current_template_parms
);
4105 /* If there is only one level of template parameters, we do not
4106 create a TREE_VEC of TREE_VECs. Instead, we return a single
4107 TREE_VEC containing the arguments. */
4109 args
= make_tree_vec (length
);
4111 for (header
= current_template_parms
; header
; header
= TREE_CHAIN (header
))
4113 tree a
= copy_node (TREE_VALUE (header
));
4116 TREE_TYPE (a
) = NULL_TREE
;
4117 for (i
= TREE_VEC_LENGTH (a
) - 1; i
>= 0; --i
)
4118 TREE_VEC_ELT (a
, i
) = template_parm_to_arg (TREE_VEC_ELT (a
, i
));
4120 #ifdef ENABLE_CHECKING
4121 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a
, TREE_VEC_LENGTH (a
));
4125 TREE_VEC_ELT (args
, --l
) = a
;
4130 if (length
> 1 && TREE_VEC_ELT (args
, 0) == NULL_TREE
)
4131 /* This can happen for template parms of a template template
4134 template<template<class T, class U> class TT> struct S;
4136 Consider the level of the parms of TT; T and U both have
4137 level 2; TT has no template parm of level 1. So in this case
4138 the first element of full_template_args is NULL_TREE. If we
4139 leave it like this TMPL_ARG_DEPTH on args returns 1 instead
4140 of 2. This will make tsubst wrongly consider that T and U
4141 have level 1. Instead, let's create a dummy vector as the
4142 first element of full_template_args so that TMPL_ARG_DEPTH
4143 returns the correct depth for args. */
4144 TREE_VEC_ELT (args
, 0) = make_tree_vec (1);
4148 /* Update the declared TYPE by doing any lookups which were thought to be
4149 dependent, but are not now that we know the SCOPE of the declarator. */
4152 maybe_update_decl_type (tree orig_type
, tree scope
)
4154 tree type
= orig_type
;
4156 if (type
== NULL_TREE
)
4159 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4160 type
= TREE_TYPE (type
);
4162 if (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
4163 && dependent_type_p (type
)
4164 /* Don't bother building up the args in this case. */
4165 && TREE_CODE (type
) != TEMPLATE_TYPE_PARM
)
4167 /* tsubst in the args corresponding to the template parameters,
4168 including auto if present. Most things will be unchanged, but
4169 make_typename_type and tsubst_qualified_id will resolve
4170 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4171 tree args
= current_template_args ();
4172 tree auto_node
= type_uses_auto (type
);
4176 tree auto_vec
= make_tree_vec (1);
4177 TREE_VEC_ELT (auto_vec
, 0) = auto_node
;
4178 args
= add_to_template_args (args
, auto_vec
);
4180 pushed
= push_scope (scope
);
4181 type
= tsubst (type
, args
, tf_warning_or_error
, NULL_TREE
);
4186 if (type
== error_mark_node
)
4189 if (TREE_CODE (orig_type
) == TYPE_DECL
)
4191 if (same_type_p (type
, TREE_TYPE (orig_type
)))
4194 type
= TYPE_NAME (type
);
4199 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4200 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
4201 a member template. Used by push_template_decl below. */
4204 build_template_decl (tree decl
, tree parms
, bool member_template_p
)
4206 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
4207 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
4208 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
4209 DECL_SOURCE_LOCATION (tmpl
) = DECL_SOURCE_LOCATION (decl
);
4210 DECL_MEMBER_TEMPLATE_P (tmpl
) = member_template_p
;
4215 struct template_parm_data
4217 /* The level of the template parameters we are currently
4221 /* The index of the specialization argument we are currently
4225 /* An array whose size is the number of template parameters. The
4226 elements are nonzero if the parameter has been used in any one
4227 of the arguments processed so far. */
4230 /* An array whose size is the number of template arguments. The
4231 elements are nonzero if the argument makes use of template
4232 parameters of this level. */
4233 int* arg_uses_template_parms
;
4236 /* Subroutine of push_template_decl used to see if each template
4237 parameter in a partial specialization is used in the explicit
4238 argument list. If T is of the LEVEL given in DATA (which is
4239 treated as a template_parm_data*), then DATA->PARMS is marked
4243 mark_template_parm (tree t
, void* data
)
4247 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
4249 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4251 level
= TEMPLATE_PARM_LEVEL (t
);
4252 idx
= TEMPLATE_PARM_IDX (t
);
4256 level
= TEMPLATE_TYPE_LEVEL (t
);
4257 idx
= TEMPLATE_TYPE_IDX (t
);
4260 if (level
== tpd
->level
)
4262 tpd
->parms
[idx
] = 1;
4263 tpd
->arg_uses_template_parms
[tpd
->current_arg
] = 1;
4266 /* Return zero so that for_each_template_parm will continue the
4267 traversal of the tree; we want to mark *every* template parm. */
4271 /* Process the partial specialization DECL. */
4274 process_partial_specialization (tree decl
)
4276 tree type
= TREE_TYPE (decl
);
4277 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
4278 tree specargs
= CLASSTYPE_TI_ARGS (type
);
4279 tree inner_args
= INNERMOST_TEMPLATE_ARGS (specargs
);
4280 tree main_inner_parms
= DECL_INNERMOST_TEMPLATE_PARMS (maintmpl
);
4283 int nargs
= TREE_VEC_LENGTH (inner_args
);
4286 bool did_error_intro
= false;
4287 struct template_parm_data tpd
;
4288 struct template_parm_data tpd2
;
4290 gcc_assert (current_template_parms
);
4292 inner_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
4293 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4295 /* We check that each of the template parameters given in the
4296 partial specialization is used in the argument list to the
4297 specialization. For example:
4299 template <class T> struct S;
4300 template <class T> struct S<T*>;
4302 The second declaration is OK because `T*' uses the template
4303 parameter T, whereas
4305 template <class T> struct S<int>;
4307 is no good. Even trickier is:
4318 The S2<T> declaration is actually invalid; it is a
4319 full-specialization. Of course,
4322 struct S2<T (*)(U)>;
4324 or some such would have been OK. */
4325 tpd
.level
= TMPL_PARMS_DEPTH (current_template_parms
);
4326 tpd
.parms
= XALLOCAVEC (int, ntparms
);
4327 memset (tpd
.parms
, 0, sizeof (int) * ntparms
);
4329 tpd
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4330 memset (tpd
.arg_uses_template_parms
, 0, sizeof (int) * nargs
);
4331 for (i
= 0; i
< nargs
; ++i
)
4333 tpd
.current_arg
= i
;
4334 for_each_template_parm (TREE_VEC_ELT (inner_args
, i
),
4335 &mark_template_parm
,
4338 /*include_nondeduced_p=*/false);
4340 for (i
= 0; i
< ntparms
; ++i
)
4341 if (tpd
.parms
[i
] == 0)
4343 /* One of the template parms was not used in the
4345 if (!did_error_intro
)
4347 error ("template parameters not used in partial specialization:");
4348 did_error_intro
= true;
4351 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
)));
4354 if (did_error_intro
)
4355 return error_mark_node
;
4357 /* [temp.class.spec]
4359 The argument list of the specialization shall not be identical to
4360 the implicit argument list of the primary template. */
4361 if (comp_template_args
4363 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
4365 error ("partial specialization %qT does not specialize any template arguments", type
);
4367 /* [temp.class.spec]
4369 A partially specialized non-type argument expression shall not
4370 involve template parameters of the partial specialization except
4371 when the argument expression is a simple identifier.
4373 The type of a template parameter corresponding to a specialized
4374 non-type argument shall not be dependent on a parameter of the
4377 Also, we verify that pack expansions only occur at the
4378 end of the argument list. */
4379 gcc_assert (nargs
== DECL_NTPARMS (maintmpl
));
4381 for (i
= 0; i
< nargs
; ++i
)
4383 tree parm
= TREE_VALUE (TREE_VEC_ELT (main_inner_parms
, i
));
4384 tree arg
= TREE_VEC_ELT (inner_args
, i
);
4385 tree packed_args
= NULL_TREE
;
4388 if (ARGUMENT_PACK_P (arg
))
4390 /* Extract the arguments from the argument pack. We'll be
4391 iterating over these in the following loop. */
4392 packed_args
= ARGUMENT_PACK_ARGS (arg
);
4393 len
= TREE_VEC_LENGTH (packed_args
);
4396 for (j
= 0; j
< len
; j
++)
4399 /* Get the Jth argument in the parameter pack. */
4400 arg
= TREE_VEC_ELT (packed_args
, j
);
4402 if (PACK_EXPANSION_P (arg
))
4404 /* Pack expansions must come at the end of the
4406 if ((packed_args
&& j
< len
- 1)
4407 || (!packed_args
&& i
< nargs
- 1))
4409 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4410 error ("parameter pack argument %qE must be at the "
4411 "end of the template argument list", arg
);
4413 error ("parameter pack argument %qT must be at the "
4414 "end of the template argument list", arg
);
4418 if (TREE_CODE (arg
) == EXPR_PACK_EXPANSION
)
4419 /* We only care about the pattern. */
4420 arg
= PACK_EXPANSION_PATTERN (arg
);
4422 if (/* These first two lines are the `non-type' bit. */
4424 && TREE_CODE (arg
) != TEMPLATE_DECL
4425 /* This next line is the `argument expression is not just a
4426 simple identifier' condition and also the `specialized
4427 non-type argument' bit. */
4428 && TREE_CODE (arg
) != TEMPLATE_PARM_INDEX
)
4430 if ((!packed_args
&& tpd
.arg_uses_template_parms
[i
])
4431 || (packed_args
&& uses_template_parms (arg
)))
4432 error ("template argument %qE involves template parameter(s)",
4436 /* Look at the corresponding template parameter,
4437 marking which template parameters its type depends
4439 tree type
= TREE_TYPE (parm
);
4443 /* We haven't yet initialized TPD2. Do so now. */
4444 tpd2
.arg_uses_template_parms
= XALLOCAVEC (int, nargs
);
4445 /* The number of parameters here is the number in the
4446 main template, which, as checked in the assertion
4448 tpd2
.parms
= XALLOCAVEC (int, nargs
);
4450 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl
));
4453 /* Mark the template parameters. But this time, we're
4454 looking for the template parameters of the main
4455 template, not in the specialization. */
4456 tpd2
.current_arg
= i
;
4457 tpd2
.arg_uses_template_parms
[i
] = 0;
4458 memset (tpd2
.parms
, 0, sizeof (int) * nargs
);
4459 for_each_template_parm (type
,
4460 &mark_template_parm
,
4463 /*include_nondeduced_p=*/false);
4465 if (tpd2
.arg_uses_template_parms
[i
])
4467 /* The type depended on some template parameters.
4468 If they are fully specialized in the
4469 specialization, that's OK. */
4472 for (j
= 0; j
< nargs
; ++j
)
4473 if (tpd2
.parms
[j
] != 0
4474 && tpd
.arg_uses_template_parms
[j
])
4477 error_n (input_location
, count
,
4478 "type %qT of template argument %qE depends "
4479 "on a template parameter",
4480 "type %qT of template argument %qE depends "
4481 "on template parameters",
4490 /* We should only get here once. */
4491 gcc_assert (!COMPLETE_TYPE_P (type
));
4493 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)
4494 = tree_cons (specargs
, inner_parms
,
4495 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
4496 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
4498 for (inst
= DECL_TEMPLATE_INSTANTIATIONS (maintmpl
); inst
;
4499 inst
= TREE_CHAIN (inst
))
4501 tree inst_type
= TREE_VALUE (inst
);
4502 if (COMPLETE_TYPE_P (inst_type
)
4503 && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type
))
4505 tree spec
= most_specialized_class (inst_type
, maintmpl
, tf_none
);
4506 if (spec
&& TREE_TYPE (spec
) == type
)
4507 permerror (input_location
,
4508 "partial specialization of %qT after instantiation "
4509 "of %qT", type
, inst_type
);
4516 /* Check that a template declaration's use of default arguments and
4517 parameter packs is not invalid. Here, PARMS are the template
4518 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
4519 a primary template. IS_PARTIAL is nonzero if DECL is a partial
4523 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4524 declaration (but not a definition); 1 indicates a declaration, 2
4525 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4526 emitted for extraneous default arguments.
4528 Returns TRUE if there were no errors found, FALSE otherwise. */
4531 check_default_tmpl_args (tree decl
, tree parms
, int is_primary
,
4532 int is_partial
, int is_friend_decl
)
4535 int last_level_to_check
;
4537 bool no_errors
= true;
4541 A default template-argument shall not be specified in a
4542 function template declaration or a function template definition, nor
4543 in the template-parameter-list of the definition of a member of a
4546 if (TREE_CODE (CP_DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
4547 /* You can't have a function template declaration in a local
4548 scope, nor you can you define a member of a class template in a
4552 if (current_class_type
4553 && !TYPE_BEING_DEFINED (current_class_type
)
4554 && DECL_LANG_SPECIFIC (decl
)
4555 && DECL_DECLARES_FUNCTION_P (decl
)
4556 /* If this is either a friend defined in the scope of the class
4557 or a member function. */
4558 && (DECL_FUNCTION_MEMBER_P (decl
)
4559 ? same_type_p (DECL_CONTEXT (decl
), current_class_type
)
4560 : DECL_FRIEND_CONTEXT (decl
)
4561 ? same_type_p (DECL_FRIEND_CONTEXT (decl
), current_class_type
)
4563 /* And, if it was a member function, it really was defined in
4564 the scope of the class. */
4565 && (!DECL_FUNCTION_MEMBER_P (decl
)
4566 || DECL_INITIALIZED_IN_CLASS_P (decl
)))
4567 /* We already checked these parameters when the template was
4568 declared, so there's no need to do it again now. This function
4569 was defined in class scope, but we're processing it's body now
4570 that the class is complete. */
4573 /* Core issue 226 (C++0x only): the following only applies to class
4575 if ((cxx_dialect
== cxx98
) || TREE_CODE (decl
) != FUNCTION_DECL
)
4579 If a template-parameter has a default template-argument, all
4580 subsequent template-parameters shall have a default
4581 template-argument supplied. */
4582 for (parm_level
= parms
; parm_level
; parm_level
= TREE_CHAIN (parm_level
))
4584 tree inner_parms
= TREE_VALUE (parm_level
);
4585 int ntparms
= TREE_VEC_LENGTH (inner_parms
);
4586 int seen_def_arg_p
= 0;
4589 for (i
= 0; i
< ntparms
; ++i
)
4591 tree parm
= TREE_VEC_ELT (inner_parms
, i
);
4593 if (parm
== error_mark_node
)
4596 if (TREE_PURPOSE (parm
))
4598 else if (seen_def_arg_p
4599 && !template_parameter_pack_p (TREE_VALUE (parm
)))
4601 error ("no default argument for %qD", TREE_VALUE (parm
));
4602 /* For better subsequent error-recovery, we indicate that
4603 there should have been a default argument. */
4604 TREE_PURPOSE (parm
) = error_mark_node
;
4610 /* Don't complain about an enclosing partial
4612 && parm_level
== parms
4613 && TREE_CODE (decl
) == TYPE_DECL
4615 && template_parameter_pack_p (TREE_VALUE (parm
)))
4617 /* A primary class template can only have one
4618 parameter pack, at the end of the template
4621 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
)
4622 error ("parameter pack %qE must be at the end of the"
4623 " template parameter list", TREE_VALUE (parm
));
4625 error ("parameter pack %qT must be at the end of the"
4626 " template parameter list",
4627 TREE_TYPE (TREE_VALUE (parm
)));
4629 TREE_VALUE (TREE_VEC_ELT (inner_parms
, i
))
4637 if (((cxx_dialect
== cxx98
) && TREE_CODE (decl
) != TYPE_DECL
)
4641 /* For an ordinary class template, default template arguments are
4642 allowed at the innermost level, e.g.:
4643 template <class T = int>
4645 but, in a partial specialization, they're not allowed even
4646 there, as we have in [temp.class.spec]:
4648 The template parameter list of a specialization shall not
4649 contain default template argument values.
4651 So, for a partial specialization, or for a function template
4652 (in C++98/C++03), we look at all of them. */
4655 /* But, for a primary class template that is not a partial
4656 specialization we look at all template parameters except the
4658 parms
= TREE_CHAIN (parms
);
4660 /* Figure out what error message to issue. */
4661 if (is_friend_decl
== 2)
4662 msg
= G_("default template arguments may not be used in function template "
4663 "friend re-declaration");
4664 else if (is_friend_decl
)
4665 msg
= G_("default template arguments may not be used in function template "
4666 "friend declarations");
4667 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& (cxx_dialect
== cxx98
))
4668 msg
= G_("default template arguments may not be used in function templates "
4669 "without -std=c++11 or -std=gnu++11");
4670 else if (is_partial
)
4671 msg
= G_("default template arguments may not be used in "
4672 "partial specializations");
4674 msg
= G_("default argument for template parameter for class enclosing %qD");
4676 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
4677 /* If we're inside a class definition, there's no need to
4678 examine the parameters to the class itself. On the one
4679 hand, they will be checked when the class is defined, and,
4680 on the other, default arguments are valid in things like:
4681 template <class T = double>
4682 struct S { template <class U> void f(U); };
4683 Here the default argument for `S' has no bearing on the
4684 declaration of `f'. */
4685 last_level_to_check
= template_class_depth (current_class_type
) + 1;
4687 /* Check everything. */
4688 last_level_to_check
= 0;
4690 for (parm_level
= parms
;
4691 parm_level
&& TMPL_PARMS_DEPTH (parm_level
) >= last_level_to_check
;
4692 parm_level
= TREE_CHAIN (parm_level
))
4694 tree inner_parms
= TREE_VALUE (parm_level
);
4698 ntparms
= TREE_VEC_LENGTH (inner_parms
);
4699 for (i
= 0; i
< ntparms
; ++i
)
4701 if (TREE_VEC_ELT (inner_parms
, i
) == error_mark_node
)
4704 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)))
4709 if (is_friend_decl
== 2)
4716 /* Clear out the default argument so that we are not
4718 TREE_PURPOSE (TREE_VEC_ELT (inner_parms
, i
)) = NULL_TREE
;
4722 /* At this point, if we're still interested in issuing messages,
4723 they must apply to classes surrounding the object declared. */
4725 msg
= G_("default argument for template parameter for class "
4732 /* Worker for push_template_decl_real, called via
4733 for_each_template_parm. DATA is really an int, indicating the
4734 level of the parameters we are interested in. If T is a template
4735 parameter of that level, return nonzero. */
4738 template_parm_this_level_p (tree t
, void* data
)
4740 int this_level
= *(int *)data
;
4743 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
4744 level
= TEMPLATE_PARM_LEVEL (t
);
4746 level
= TEMPLATE_TYPE_LEVEL (t
);
4747 return level
== this_level
;
4750 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
4751 parameters given by current_template_args, or reuses a
4752 previously existing one, if appropriate. Returns the DECL, or an
4753 equivalent one, if it is replaced via a call to duplicate_decls.
4755 If IS_FRIEND is true, DECL is a friend declaration. */
4758 push_template_decl_real (tree decl
, bool is_friend
)
4766 int new_template_p
= 0;
4767 /* True if the template is a member template, in the sense of
4769 bool member_template_p
= false;
4771 if (decl
== error_mark_node
|| !current_template_parms
)
4772 return error_mark_node
;
4774 /* See if this is a partial specialization. */
4775 is_partial
= (DECL_IMPLICIT_TYPEDEF_P (decl
)
4776 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
4777 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)));
4779 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
))
4783 /* For a friend, we want the context of the friend function, not
4784 the type of which it is a friend. */
4785 ctx
= CP_DECL_CONTEXT (decl
);
4786 else if (CP_DECL_CONTEXT (decl
)
4787 && TREE_CODE (CP_DECL_CONTEXT (decl
)) != NAMESPACE_DECL
)
4788 /* In the case of a virtual function, we want the class in which
4790 ctx
= CP_DECL_CONTEXT (decl
);
4792 /* Otherwise, if we're currently defining some class, the DECL
4793 is assumed to be a member of the class. */
4794 ctx
= current_scope ();
4796 if (ctx
&& TREE_CODE (ctx
) == NAMESPACE_DECL
)
4799 if (!DECL_CONTEXT (decl
))
4800 DECL_CONTEXT (decl
) = FROB_CONTEXT (current_namespace
);
4802 /* See if this is a primary template. */
4803 if (is_friend
&& ctx
)
4804 /* A friend template that specifies a class context, i.e.
4805 template <typename T> friend void A<T>::f();
4809 primary
= template_parm_scope_p ();
4813 if (DECL_CLASS_SCOPE_P (decl
))
4814 member_template_p
= true;
4815 if (TREE_CODE (decl
) == TYPE_DECL
4816 && ANON_AGGRNAME_P (DECL_NAME (decl
)))
4818 error ("template class without a name");
4819 return error_mark_node
;
4821 else if (TREE_CODE (decl
) == FUNCTION_DECL
)
4823 if (DECL_DESTRUCTOR_P (decl
))
4827 A destructor shall not be a member template. */
4828 error ("destructor %qD declared as member template", decl
);
4829 return error_mark_node
;
4831 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl
))
4832 && (!prototype_p (TREE_TYPE (decl
))
4833 || TYPE_ARG_TYPES (TREE_TYPE (decl
)) == void_list_node
4834 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
4835 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl
))))
4836 == void_list_node
)))
4838 /* [basic.stc.dynamic.allocation]
4840 An allocation function can be a function
4841 template. ... Template allocation functions shall
4842 have two or more parameters. */
4843 error ("invalid template declaration of %qD", decl
);
4844 return error_mark_node
;
4847 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4848 && CLASS_TYPE_P (TREE_TYPE (decl
)))
4850 else if (TREE_CODE (decl
) == TYPE_DECL
4851 && TYPE_DECL_ALIAS_P (decl
))
4852 /* alias-declaration */
4853 gcc_assert (!DECL_ARTIFICIAL (decl
));
4856 error ("template declaration of %q#D", decl
);
4857 return error_mark_node
;
4861 /* Check to see that the rules regarding the use of default
4862 arguments are not being violated. */
4863 check_default_tmpl_args (decl
, current_template_parms
,
4864 primary
, is_partial
, /*is_friend_decl=*/0);
4866 /* Ensure that there are no parameter packs in the type of this
4867 declaration that have not been expanded. */
4868 if (TREE_CODE (decl
) == FUNCTION_DECL
)
4870 /* Check each of the arguments individually to see if there are
4871 any bare parameter packs. */
4872 tree type
= TREE_TYPE (decl
);
4873 tree arg
= DECL_ARGUMENTS (decl
);
4874 tree argtype
= TYPE_ARG_TYPES (type
);
4876 while (arg
&& argtype
)
4878 if (!FUNCTION_PARAMETER_PACK_P (arg
)
4879 && check_for_bare_parameter_packs (TREE_TYPE (arg
)))
4881 /* This is a PARM_DECL that contains unexpanded parameter
4882 packs. We have already complained about this in the
4883 check_for_bare_parameter_packs call, so just replace
4884 these types with ERROR_MARK_NODE. */
4885 TREE_TYPE (arg
) = error_mark_node
;
4886 TREE_VALUE (argtype
) = error_mark_node
;
4889 arg
= DECL_CHAIN (arg
);
4890 argtype
= TREE_CHAIN (argtype
);
4893 /* Check for bare parameter packs in the return type and the
4894 exception specifiers. */
4895 if (check_for_bare_parameter_packs (TREE_TYPE (type
)))
4896 /* Errors were already issued, set return type to int
4897 as the frontend doesn't expect error_mark_node as
4899 TREE_TYPE (type
) = integer_type_node
;
4900 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type
)))
4901 TYPE_RAISES_EXCEPTIONS (type
) = NULL_TREE
;
4903 else if (check_for_bare_parameter_packs ((TREE_CODE (decl
) == TYPE_DECL
4904 && TYPE_DECL_ALIAS_P (decl
))
4905 ? DECL_ORIGINAL_TYPE (decl
)
4906 : TREE_TYPE (decl
)))
4908 TREE_TYPE (decl
) = error_mark_node
;
4909 return error_mark_node
;
4913 return process_partial_specialization (decl
);
4915 args
= current_template_args ();
4918 || TREE_CODE (ctx
) == FUNCTION_DECL
4919 || (CLASS_TYPE_P (ctx
) && TYPE_BEING_DEFINED (ctx
))
4920 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
4922 if (DECL_LANG_SPECIFIC (decl
)
4923 && DECL_TEMPLATE_INFO (decl
)
4924 && DECL_TI_TEMPLATE (decl
))
4925 tmpl
= DECL_TI_TEMPLATE (decl
);
4926 /* If DECL is a TYPE_DECL for a class-template, then there won't
4927 be DECL_LANG_SPECIFIC. The information equivalent to
4928 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
4929 else if (DECL_IMPLICIT_TYPEDEF_P (decl
)
4930 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl
))
4931 && TYPE_TI_TEMPLATE (TREE_TYPE (decl
)))
4933 /* Since a template declaration already existed for this
4934 class-type, we must be redeclaring it here. Make sure
4935 that the redeclaration is valid. */
4936 redeclare_class_template (TREE_TYPE (decl
),
4937 current_template_parms
);
4938 /* We don't need to create a new TEMPLATE_DECL; just use the
4939 one we already had. */
4940 tmpl
= TYPE_TI_TEMPLATE (TREE_TYPE (decl
));
4944 tmpl
= build_template_decl (decl
, current_template_parms
,
4948 if (DECL_LANG_SPECIFIC (decl
)
4949 && DECL_TEMPLATE_SPECIALIZATION (decl
))
4951 /* A specialization of a member template of a template
4953 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
4954 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
4955 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
4961 tree a
, t
, current
, parms
;
4963 tree tinfo
= get_template_info (decl
);
4967 error ("template definition of non-template %q#D", decl
);
4968 return error_mark_node
;
4971 tmpl
= TI_TEMPLATE (tinfo
);
4973 if (DECL_FUNCTION_TEMPLATE_P (tmpl
)
4974 && DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
4975 && DECL_TEMPLATE_SPECIALIZATION (decl
)
4976 && DECL_MEMBER_TEMPLATE_P (tmpl
))
4980 /* The declaration is a specialization of a member
4981 template, declared outside the class. Therefore, the
4982 innermost template arguments will be NULL, so we
4983 replace them with the arguments determined by the
4984 earlier call to check_explicit_specialization. */
4985 args
= DECL_TI_ARGS (decl
);
4988 = build_template_decl (decl
, current_template_parms
,
4990 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
4991 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
4992 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
4993 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
4994 DECL_TEMPLATE_INFO (new_tmpl
)
4995 = build_template_info (tmpl
, args
);
4997 register_specialization (new_tmpl
,
4998 most_general_template (tmpl
),
5004 /* Make sure the template headers we got make sense. */
5006 parms
= DECL_TEMPLATE_PARMS (tmpl
);
5007 i
= TMPL_PARMS_DEPTH (parms
);
5008 if (TMPL_ARGS_DEPTH (args
) != i
)
5010 error ("expected %d levels of template parms for %q#D, got %d",
5011 i
, decl
, TMPL_ARGS_DEPTH (args
));
5014 for (current
= decl
; i
> 0; --i
, parms
= TREE_CHAIN (parms
))
5016 a
= TMPL_ARGS_LEVEL (args
, i
);
5017 t
= INNERMOST_TEMPLATE_PARMS (parms
);
5019 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
5021 if (current
== decl
)
5022 error ("got %d template parameters for %q#D",
5023 TREE_VEC_LENGTH (a
), decl
);
5025 error ("got %d template parameters for %q#T",
5026 TREE_VEC_LENGTH (a
), current
);
5027 error (" but %d required", TREE_VEC_LENGTH (t
));
5028 return error_mark_node
;
5031 if (current
== decl
)
5033 else if (current
== NULL_TREE
)
5034 /* Can happen in erroneous input. */
5037 current
= (TYPE_P (current
)
5038 ? TYPE_CONTEXT (current
)
5039 : DECL_CONTEXT (current
));
5042 /* Check that the parms are used in the appropriate qualifying scopes
5043 in the declarator. */
5044 if (!comp_template_args
5046 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl
)))))
5049 template arguments to %qD do not match original template %qD",
5050 decl
, DECL_TEMPLATE_RESULT (tmpl
));
5051 if (!uses_template_parms (TI_ARGS (tinfo
)))
5052 inform (input_location
, "use template<> for an explicit specialization");
5053 /* Avoid crash in import_export_decl. */
5054 DECL_INTERFACE_KNOWN (decl
) = 1;
5055 return error_mark_node
;
5059 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
5060 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
5062 /* Push template declarations for global functions and types. Note
5063 that we do not try to push a global template friend declared in a
5064 template class; such a thing may well depend on the template
5065 parameters of the class. */
5066 if (new_template_p
&& !ctx
5067 && !(is_friend
&& template_class_depth (current_class_type
) > 0))
5069 tmpl
= pushdecl_namespace_level (tmpl
, is_friend
);
5070 if (tmpl
== error_mark_node
)
5071 return error_mark_node
;
5073 /* Hide template friend classes that haven't been declared yet. */
5074 if (is_friend
&& TREE_CODE (decl
) == TYPE_DECL
)
5076 DECL_ANTICIPATED (tmpl
) = 1;
5077 DECL_FRIEND_P (tmpl
) = 1;
5083 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
5086 DECL_PRIMARY_TEMPLATE (tmpl
) = tmpl
;
5087 if (DECL_CONV_FN_P (tmpl
))
5089 int depth
= TMPL_PARMS_DEPTH (parms
);
5091 /* It is a conversion operator. See if the type converted to
5092 depends on innermost template operands. */
5094 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl
)),
5096 DECL_TEMPLATE_CONV_FN_P (tmpl
) = 1;
5099 /* Give template template parms a DECL_CONTEXT of the template
5100 for which they are a parameter. */
5101 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5102 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
5104 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5105 if (TREE_CODE (parm
) == TEMPLATE_DECL
)
5106 DECL_CONTEXT (parm
) = tmpl
;
5110 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5111 back to its most general template. If TMPL is a specialization,
5112 ARGS may only have the innermost set of arguments. Add the missing
5113 argument levels if necessary. */
5114 if (DECL_TEMPLATE_INFO (tmpl
))
5115 args
= add_outermost_template_args (DECL_TI_ARGS (tmpl
), args
);
5117 info
= build_template_info (tmpl
, args
);
5119 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
5120 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
), info
);
5123 if (primary
&& !DECL_LANG_SPECIFIC (decl
))
5124 retrofit_lang_decl (decl
);
5125 if (DECL_LANG_SPECIFIC (decl
))
5126 DECL_TEMPLATE_INFO (decl
) = info
;
5129 return DECL_TEMPLATE_RESULT (tmpl
);
5133 push_template_decl (tree decl
)
5135 return push_template_decl_real (decl
, false);
5138 /* Called when a class template TYPE is redeclared with the indicated
5139 template PARMS, e.g.:
5141 template <class T> struct S;
5142 template <class T> struct S {}; */
5145 redeclare_class_template (tree type
, tree parms
)
5151 if (!TYPE_TEMPLATE_INFO (type
))
5153 error ("%qT is not a template type", type
);
5157 tmpl
= TYPE_TI_TEMPLATE (type
);
5158 if (!PRIMARY_TEMPLATE_P (tmpl
))
5159 /* The type is nested in some template class. Nothing to worry
5160 about here; there are no new template parameters for the nested
5166 error ("template specifiers not specified in declaration of %qD",
5171 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
5172 tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
5174 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
5176 error_n (input_location
, TREE_VEC_LENGTH (parms
),
5177 "redeclared with %d template parameter",
5178 "redeclared with %d template parameters",
5179 TREE_VEC_LENGTH (parms
));
5180 inform_n (input_location
, TREE_VEC_LENGTH (tmpl_parms
),
5181 "previous declaration %q+D used %d template parameter",
5182 "previous declaration %q+D used %d template parameters",
5183 tmpl
, TREE_VEC_LENGTH (tmpl_parms
));
5187 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
5194 if (TREE_VEC_ELT (tmpl_parms
, i
) == error_mark_node
5195 || TREE_VEC_ELT (parms
, i
) == error_mark_node
)
5198 tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
5199 if (tmpl_parm
== error_mark_node
)
5202 parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
5203 tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
5204 parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
5206 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5208 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
)
5209 || (TREE_CODE (tmpl_parm
) != TYPE_DECL
5210 && !same_type_p (TREE_TYPE (tmpl_parm
), TREE_TYPE (parm
)))
5211 || (TREE_CODE (tmpl_parm
) != PARM_DECL
5212 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm
))
5213 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
))))
5214 || (TREE_CODE (tmpl_parm
) == PARM_DECL
5215 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm
))
5216 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))))
5218 error ("template parameter %q+#D", tmpl_parm
);
5219 error ("redeclared here as %q#D", parm
);
5223 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
5225 /* We have in [temp.param]:
5227 A template-parameter may not be given default arguments
5228 by two different declarations in the same scope. */
5229 error_at (input_location
, "redefinition of default argument for %q#D", parm
);
5230 inform (DECL_SOURCE_LOCATION (tmpl_parm
),
5231 "original definition appeared here");
5235 if (parm_default
!= NULL_TREE
)
5236 /* Update the previous template parameters (which are the ones
5237 that will really count) with the new default value. */
5238 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
5239 else if (tmpl_default
!= NULL_TREE
)
5240 /* Update the new parameters, too; they'll be used as the
5241 parameters for any members. */
5242 TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)) = tmpl_default
;
5248 /* Simplify EXPR if it is a non-dependent expression. Returns the
5249 (possibly simplified) expression. */
5252 fold_non_dependent_expr_sfinae (tree expr
, tsubst_flags_t complain
)
5254 if (expr
== NULL_TREE
)
5257 /* If we're in a template, but EXPR isn't value dependent, simplify
5258 it. We're supposed to treat:
5260 template <typename T> void f(T[1 + 1]);
5261 template <typename T> void f(T[2]);
5263 as two declarations of the same function, for example. */
5264 if (processing_template_decl
5265 && !type_dependent_expression_p (expr
)
5266 && potential_constant_expression (expr
)
5267 && !value_dependent_expression_p (expr
))
5269 HOST_WIDE_INT saved_processing_template_decl
;
5271 saved_processing_template_decl
= processing_template_decl
;
5272 processing_template_decl
= 0;
5273 expr
= tsubst_copy_and_build (expr
,
5276 /*in_decl=*/NULL_TREE
,
5277 /*function_p=*/false,
5278 /*integral_constant_expression_p=*/true);
5279 processing_template_decl
= saved_processing_template_decl
;
5285 fold_non_dependent_expr (tree expr
)
5287 return fold_non_dependent_expr_sfinae (expr
, tf_error
);
5290 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5291 template declaration, or a TYPE_DECL for an alias declaration. */
5294 alias_type_or_template_p (tree t
)
5298 return ((TREE_CODE (t
) == TYPE_DECL
&& TYPE_DECL_ALIAS_P (t
))
5301 && TYPE_DECL_ALIAS_P (TYPE_NAME (t
)))
5302 || DECL_ALIAS_TEMPLATE_P (t
));
5305 /* Return TRUE iff is a specialization of an alias template. */
5308 alias_template_specialization_p (tree t
)
5312 return (primary_template_instantiation_p (t
)
5313 && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t
)));
5316 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5317 must be a function or a pointer-to-function type, as specified
5318 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5319 and check that the resulting function has external linkage. */
5322 convert_nontype_argument_function (tree type
, tree expr
)
5326 linkage_kind linkage
;
5328 fn
= instantiate_type (type
, fns
, tf_none
);
5329 if (fn
== error_mark_node
)
5330 return error_mark_node
;
5333 if (TREE_CODE (fn_no_ptr
) == ADDR_EXPR
)
5334 fn_no_ptr
= TREE_OPERAND (fn_no_ptr
, 0);
5335 if (BASELINK_P (fn_no_ptr
))
5336 fn_no_ptr
= BASELINK_FUNCTIONS (fn_no_ptr
);
5338 /* [temp.arg.nontype]/1
5340 A template-argument for a non-type, non-template template-parameter
5343 -- the address of an object or function with external [C++11: or
5344 internal] linkage. */
5345 linkage
= decl_linkage (fn_no_ptr
);
5346 if (cxx_dialect
>= cxx0x
? linkage
== lk_none
: linkage
!= lk_external
)
5348 if (cxx_dialect
>= cxx0x
)
5349 error ("%qE is not a valid template argument for type %qT "
5350 "because %qD has no linkage",
5351 expr
, type
, fn_no_ptr
);
5353 error ("%qE is not a valid template argument for type %qT "
5354 "because %qD does not have external linkage",
5355 expr
, type
, fn_no_ptr
);
5362 /* Subroutine of convert_nontype_argument.
5363 Check if EXPR of type TYPE is a valid pointer-to-member constant.
5364 Emit an error otherwise. */
5367 check_valid_ptrmem_cst_expr (tree type
, tree expr
,
5368 tsubst_flags_t complain
)
5371 if (expr
&& (null_ptr_cst_p (expr
) || TREE_CODE (expr
) == PTRMEM_CST
))
5373 if (cxx_dialect
>= cxx0x
&& null_member_pointer_value_p (expr
))
5375 if (complain
& tf_error
)
5377 error ("%qE is not a valid template argument for type %qT",
5379 error ("it must be a pointer-to-member of the form %<&X::Y%>");
5384 /* Returns TRUE iff the address of OP is value-dependent.
5386 14.6.2.4 [temp.dep.temp]:
5387 A non-integral non-type template-argument is dependent if its type is
5388 dependent or it has either of the following forms
5391 and contains a nested-name-specifier which specifies a class-name that
5392 names a dependent type.
5394 We generalize this to just say that the address of a member of a
5395 dependent class is value-dependent; the above doesn't cover the
5396 address of a static data member named with an unqualified-id. */
5399 has_value_dependent_address (tree op
)
5401 /* We could use get_inner_reference here, but there's no need;
5402 this is only relevant for template non-type arguments, which
5403 can only be expressed as &id-expression. */
5406 tree ctx
= CP_DECL_CONTEXT (op
);
5407 if (TYPE_P (ctx
) && dependent_type_p (ctx
))
5414 /* The next set of functions are used for providing helpful explanatory
5415 diagnostics for failed overload resolution. Their messages should be
5416 indented by two spaces for consistency with the messages in
5420 unify_success (bool explain_p ATTRIBUTE_UNUSED
)
5426 unify_parameter_deduction_failure (bool explain_p
, tree parm
)
5429 inform (input_location
,
5430 " couldn't deduce template parameter %qD", parm
);
5435 unify_invalid (bool explain_p ATTRIBUTE_UNUSED
)
5441 unify_cv_qual_mismatch (bool explain_p
, tree parm
, tree arg
)
5444 inform (input_location
,
5445 " types %qT and %qT have incompatible cv-qualifiers",
5451 unify_type_mismatch (bool explain_p
, tree parm
, tree arg
)
5454 inform (input_location
, " mismatched types %qT and %qT", parm
, arg
);
5459 unify_parameter_pack_mismatch (bool explain_p
, tree parm
, tree arg
)
5462 inform (input_location
,
5463 " template parameter %qD is not a parameter pack, but "
5470 unify_ptrmem_cst_mismatch (bool explain_p
, tree parm
, tree arg
)
5473 inform (input_location
,
5474 " template argument %qE does not match "
5475 "pointer-to-member constant %qE",
5481 unify_expression_unequal (bool explain_p
, tree parm
, tree arg
)
5484 inform (input_location
, " %qE is not equivalent to %qE", parm
, arg
);
5489 unify_parameter_pack_inconsistent (bool explain_p
, tree old_arg
, tree new_arg
)
5492 inform (input_location
,
5493 " inconsistent parameter pack deduction with %qT and %qT",
5499 unify_inconsistency (bool explain_p
, tree parm
, tree first
, tree second
)
5504 inform (input_location
,
5505 " deduced conflicting types for parameter %qT (%qT and %qT)",
5506 parm
, first
, second
);
5508 inform (input_location
,
5509 " deduced conflicting values for non-type parameter "
5510 "%qE (%qE and %qE)", parm
, first
, second
);
5516 unify_vla_arg (bool explain_p
, tree arg
)
5519 inform (input_location
,
5520 " variable-sized array type %qT is not "
5521 "a valid template argument",
5527 unify_method_type_error (bool explain_p
, tree arg
)
5530 inform (input_location
,
5531 " member function type %qT is not a valid template argument",
5537 unify_arity (bool explain_p
, int have
, int wanted
)
5540 inform_n (input_location
, wanted
,
5541 " candidate expects %d argument, %d provided",
5542 " candidate expects %d arguments, %d provided",
5548 unify_too_many_arguments (bool explain_p
, int have
, int wanted
)
5550 return unify_arity (explain_p
, have
, wanted
);
5554 unify_too_few_arguments (bool explain_p
, int have
, int wanted
)
5556 return unify_arity (explain_p
, have
, wanted
);
5560 unify_arg_conversion (bool explain_p
, tree to_type
,
5561 tree from_type
, tree arg
)
5564 inform (input_location
, " cannot convert %qE (type %qT) to type %qT",
5565 arg
, from_type
, to_type
);
5570 unify_no_common_base (bool explain_p
, enum template_base_result r
,
5571 tree parm
, tree arg
)
5576 case tbr_ambiguous_baseclass
:
5577 inform (input_location
, " %qT is an ambiguous base class of %qT",
5581 inform (input_location
, " %qT is not derived from %qT", arg
, parm
);
5588 unify_inconsistent_template_template_parameters (bool explain_p
)
5591 inform (input_location
,
5592 " template parameters of a template template argument are "
5593 "inconsistent with other deduced template arguments");
5598 unify_template_deduction_failure (bool explain_p
, tree parm
, tree arg
)
5601 inform (input_location
,
5602 " can't deduce a template for %qT from non-template type %qT",
5608 unify_template_argument_mismatch (bool explain_p
, tree parm
, tree arg
)
5611 inform (input_location
,
5612 " template argument %qE does not match %qD", arg
, parm
);
5617 unify_overload_resolution_failure (bool explain_p
, tree arg
)
5620 inform (input_location
,
5621 " could not resolve address from overloaded function %qE",
5626 /* Attempt to convert the non-type template parameter EXPR to the
5627 indicated TYPE. If the conversion is successful, return the
5628 converted value. If the conversion is unsuccessful, return
5629 NULL_TREE if we issued an error message, or error_mark_node if we
5630 did not. We issue error messages for out-and-out bad template
5631 parameters, but not simply because the conversion failed, since we
5632 might be just trying to do argument deduction. Both TYPE and EXPR
5633 must be non-dependent.
5635 The conversion follows the special rules described in
5636 [temp.arg.nontype], and it is much more strict than an implicit
5639 This function is called twice for each template argument (see
5640 lookup_template_class for a more accurate description of this
5641 problem). This means that we need to handle expressions which
5642 are not valid in a C++ source, but can be created from the
5643 first call (for instance, casts to perform conversions). These
5644 hacks can go away after we fix the double coercion problem. */
5647 convert_nontype_argument (tree type
, tree expr
, tsubst_flags_t complain
)
5651 /* Detect immediately string literals as invalid non-type argument.
5652 This special-case is not needed for correctness (we would easily
5653 catch this later), but only to provide better diagnostic for this
5654 common user mistake. As suggested by DR 100, we do not mention
5655 linkage issues in the diagnostic as this is not the point. */
5656 /* FIXME we're making this OK. */
5657 if (TREE_CODE (expr
) == STRING_CST
)
5659 if (complain
& tf_error
)
5660 error ("%qE is not a valid template argument for type %qT "
5661 "because string literals can never be used in this context",
5666 /* Add the ADDR_EXPR now for the benefit of
5667 value_dependent_expression_p. */
5668 if (TYPE_PTROBV_P (type
)
5669 && TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
)
5670 expr
= decay_conversion (expr
);
5672 /* If we are in a template, EXPR may be non-dependent, but still
5673 have a syntactic, rather than semantic, form. For example, EXPR
5674 might be a SCOPE_REF, rather than the VAR_DECL to which the
5675 SCOPE_REF refers. Preserving the qualifying scope is necessary
5676 so that access checking can be performed when the template is
5677 instantiated -- but here we need the resolved form so that we can
5678 convert the argument. */
5679 if (TYPE_REF_OBJ_P (type
)
5680 && has_value_dependent_address (expr
))
5681 /* If we want the address and it's value-dependent, don't fold. */;
5682 else if (!type_unknown_p (expr
))
5683 expr
= fold_non_dependent_expr_sfinae (expr
, complain
);
5684 if (error_operand_p (expr
))
5685 return error_mark_node
;
5686 expr_type
= TREE_TYPE (expr
);
5687 if (TREE_CODE (type
) == REFERENCE_TYPE
)
5688 expr
= mark_lvalue_use (expr
);
5690 expr
= mark_rvalue_use (expr
);
5692 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
5693 to a non-type argument of "nullptr". */
5694 if (expr
== nullptr_node
5695 && (TYPE_PTR_P (type
) || TYPE_PTR_TO_MEMBER_P (type
)))
5696 expr
= convert (type
, expr
);
5698 /* In C++11, integral or enumeration non-type template arguments can be
5699 arbitrary constant expressions. Pointer and pointer to
5700 member arguments can be general constant expressions that evaluate
5701 to a null value, but otherwise still need to be of a specific form. */
5702 if (cxx_dialect
>= cxx0x
)
5704 if (TREE_CODE (expr
) == PTRMEM_CST
)
5705 /* A PTRMEM_CST is already constant, and a valid template
5706 argument for a parameter of pointer to member type, we just want
5707 to leave it in that form rather than lower it to a
5709 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5710 expr
= maybe_constant_value (expr
);
5711 else if (TYPE_PTR_P (type
)
5712 || TYPE_PTR_TO_MEMBER_P (type
))
5714 tree folded
= maybe_constant_value (expr
);
5715 if (TYPE_PTR_P (type
) ? integer_zerop (folded
)
5716 : null_member_pointer_value_p (folded
))
5721 /* HACK: Due to double coercion, we can get a
5722 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
5723 which is the tree that we built on the first call (see
5724 below when coercing to reference to object or to reference to
5725 function). We just strip everything and get to the arg.
5726 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
5728 if (TYPE_REF_OBJ_P (type
) || TYPE_REFFN_P (type
))
5730 tree probe_type
, probe
= expr
;
5731 if (REFERENCE_REF_P (probe
))
5732 probe
= TREE_OPERAND (probe
, 0);
5733 probe_type
= TREE_TYPE (probe
);
5734 if (TREE_CODE (probe
) == NOP_EXPR
)
5736 /* ??? Maybe we could use convert_from_reference here, but we
5737 would need to relax its constraints because the NOP_EXPR
5738 could actually change the type to something more cv-qualified,
5739 and this is not folded by convert_from_reference. */
5740 tree addr
= TREE_OPERAND (probe
, 0);
5741 gcc_assert (TREE_CODE (probe_type
) == REFERENCE_TYPE
);
5742 gcc_assert (TREE_CODE (addr
) == ADDR_EXPR
);
5743 gcc_assert (TREE_CODE (TREE_TYPE (addr
)) == POINTER_TYPE
);
5744 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5745 (TREE_TYPE (probe_type
),
5746 TREE_TYPE (TREE_TYPE (addr
))));
5748 expr
= TREE_OPERAND (addr
, 0);
5749 expr_type
= TREE_TYPE (expr
);
5753 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
5754 parameter is a pointer to object, through decay and
5755 qualification conversion. Let's strip everything. */
5756 else if (TREE_CODE (expr
) == NOP_EXPR
&& TYPE_PTROBV_P (type
))
5759 gcc_assert (TREE_CODE (expr
) == ADDR_EXPR
);
5760 gcc_assert (TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
);
5761 /* Skip the ADDR_EXPR only if it is part of the decay for
5762 an array. Otherwise, it is part of the original argument
5763 in the source code. */
5764 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == ARRAY_TYPE
)
5765 expr
= TREE_OPERAND (expr
, 0);
5766 expr_type
= TREE_TYPE (expr
);
5769 /* [temp.arg.nontype]/5, bullet 1
5771 For a non-type template-parameter of integral or enumeration type,
5772 integral promotions (_conv.prom_) and integral conversions
5773 (_conv.integral_) are applied. */
5774 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
5776 tree t
= build_integral_nontype_arg_conv (type
, expr
, complain
);
5777 t
= maybe_constant_value (t
);
5778 if (t
!= error_mark_node
)
5781 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (expr
)))
5782 return error_mark_node
;
5784 /* Notice that there are constant expressions like '4 % 0' which
5785 do not fold into integer constants. */
5786 if (TREE_CODE (expr
) != INTEGER_CST
)
5788 if (complain
& tf_error
)
5790 int errs
= errorcount
, warns
= warningcount
;
5791 if (processing_template_decl
5792 && !require_potential_constant_expression (expr
))
5794 expr
= cxx_constant_value (expr
);
5795 if (errorcount
> errs
|| warningcount
> warns
)
5796 inform (EXPR_LOC_OR_HERE (expr
),
5797 "in template argument for type %qT ", type
);
5798 if (expr
== error_mark_node
)
5800 /* else cxx_constant_value complained but gave us
5801 a real constant, so go ahead. */
5802 gcc_assert (TREE_CODE (expr
) == INTEGER_CST
);
5808 /* [temp.arg.nontype]/5, bullet 2
5810 For a non-type template-parameter of type pointer to object,
5811 qualification conversions (_conv.qual_) and the array-to-pointer
5812 conversion (_conv.array_) are applied. */
5813 else if (TYPE_PTROBV_P (type
))
5815 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
5817 A template-argument for a non-type, non-template template-parameter
5818 shall be one of: [...]
5820 -- the name of a non-type template-parameter;
5821 -- the address of an object or function with external linkage, [...]
5822 expressed as "& id-expression" where the & is optional if the name
5823 refers to a function or array, or if the corresponding
5824 template-parameter is a reference.
5826 Here, we do not care about functions, as they are invalid anyway
5827 for a parameter of type pointer-to-object. */
5829 if (DECL_P (expr
) && DECL_TEMPLATE_PARM_P (expr
))
5830 /* Non-type template parameters are OK. */
5832 else if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5833 /* Null pointer values are OK in C++11. */;
5834 else if (TREE_CODE (expr
) != ADDR_EXPR
5835 && TREE_CODE (expr_type
) != ARRAY_TYPE
)
5837 if (TREE_CODE (expr
) == VAR_DECL
)
5839 error ("%qD is not a valid template argument "
5840 "because %qD is a variable, not the address of "
5845 /* Other values, like integer constants, might be valid
5846 non-type arguments of some other type. */
5847 return error_mark_node
;
5853 decl
= ((TREE_CODE (expr
) == ADDR_EXPR
)
5854 ? TREE_OPERAND (expr
, 0) : expr
);
5855 if (TREE_CODE (decl
) != VAR_DECL
)
5857 error ("%qE is not a valid template argument of type %qT "
5858 "because %qE is not a variable",
5862 else if (cxx_dialect
< cxx0x
&& !DECL_EXTERNAL_LINKAGE_P (decl
))
5864 error ("%qE is not a valid template argument of type %qT "
5865 "because %qD does not have external linkage",
5869 else if (cxx_dialect
>= cxx0x
&& decl_linkage (decl
) == lk_none
)
5871 error ("%qE is not a valid template argument of type %qT "
5872 "because %qD has no linkage",
5878 expr
= decay_conversion (expr
);
5879 if (expr
== error_mark_node
)
5880 return error_mark_node
;
5882 expr
= perform_qualification_conversions (type
, expr
);
5883 if (expr
== error_mark_node
)
5884 return error_mark_node
;
5886 /* [temp.arg.nontype]/5, bullet 3
5888 For a non-type template-parameter of type reference to object, no
5889 conversions apply. The type referred to by the reference may be more
5890 cv-qualified than the (otherwise identical) type of the
5891 template-argument. The template-parameter is bound directly to the
5892 template-argument, which must be an lvalue. */
5893 else if (TYPE_REF_OBJ_P (type
))
5895 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type
),
5897 return error_mark_node
;
5899 if (!at_least_as_qualified_p (TREE_TYPE (type
), expr_type
))
5901 error ("%qE is not a valid template argument for type %qT "
5902 "because of conflicts in cv-qualification", expr
, type
);
5906 if (!real_lvalue_p (expr
))
5908 error ("%qE is not a valid template argument for type %qT "
5909 "because it is not an lvalue", expr
, type
);
5913 /* [temp.arg.nontype]/1
5915 A template-argument for a non-type, non-template template-parameter
5916 shall be one of: [...]
5918 -- the address of an object or function with external linkage. */
5919 if (TREE_CODE (expr
) == INDIRECT_REF
5920 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr
, 0))))
5922 expr
= TREE_OPERAND (expr
, 0);
5925 error ("%q#D is not a valid template argument for type %qT "
5926 "because a reference variable does not have a constant "
5927 "address", expr
, type
);
5934 error ("%qE is not a valid template argument for type %qT "
5935 "because it is not an object with external linkage",
5940 if (!DECL_EXTERNAL_LINKAGE_P (expr
))
5942 error ("%qE is not a valid template argument for type %qT "
5943 "because object %qD has not external linkage",
5948 expr
= build_nop (type
, build_address (expr
));
5950 /* [temp.arg.nontype]/5, bullet 4
5952 For a non-type template-parameter of type pointer to function, only
5953 the function-to-pointer conversion (_conv.func_) is applied. If the
5954 template-argument represents a set of overloaded functions (or a
5955 pointer to such), the matching function is selected from the set
5957 else if (TYPE_PTRFN_P (type
))
5959 /* If the argument is a template-id, we might not have enough
5960 context information to decay the pointer. */
5961 if (!type_unknown_p (expr_type
))
5963 expr
= decay_conversion (expr
);
5964 if (expr
== error_mark_node
)
5965 return error_mark_node
;
5968 if (cxx_dialect
>= cxx0x
&& integer_zerop (expr
))
5969 /* Null pointer values are OK in C++11. */
5970 return perform_qualification_conversions (type
, expr
);
5972 expr
= convert_nontype_argument_function (type
, expr
);
5973 if (!expr
|| expr
== error_mark_node
)
5976 if (TREE_CODE (expr
) != ADDR_EXPR
)
5978 error ("%qE is not a valid template argument for type %qT", expr
, type
);
5979 error ("it must be the address of a function with external linkage");
5983 /* [temp.arg.nontype]/5, bullet 5
5985 For a non-type template-parameter of type reference to function, no
5986 conversions apply. If the template-argument represents a set of
5987 overloaded functions, the matching function is selected from the set
5989 else if (TYPE_REFFN_P (type
))
5991 if (TREE_CODE (expr
) == ADDR_EXPR
)
5993 error ("%qE is not a valid template argument for type %qT "
5994 "because it is a pointer", expr
, type
);
5995 inform (input_location
, "try using %qE instead", TREE_OPERAND (expr
, 0));
5999 expr
= convert_nontype_argument_function (TREE_TYPE (type
), expr
);
6000 if (!expr
|| expr
== error_mark_node
)
6003 expr
= build_nop (type
, build_address (expr
));
6005 /* [temp.arg.nontype]/5, bullet 6
6007 For a non-type template-parameter of type pointer to member function,
6008 no conversions apply. If the template-argument represents a set of
6009 overloaded member functions, the matching member function is selected
6010 from the set (_over.over_). */
6011 else if (TYPE_PTRMEMFUNC_P (type
))
6013 expr
= instantiate_type (type
, expr
, tf_none
);
6014 if (expr
== error_mark_node
)
6015 return error_mark_node
;
6017 /* [temp.arg.nontype] bullet 1 says the pointer to member
6018 expression must be a pointer-to-member constant. */
6019 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6020 return error_mark_node
;
6022 /* There is no way to disable standard conversions in
6023 resolve_address_of_overloaded_function (called by
6024 instantiate_type). It is possible that the call succeeded by
6025 converting &B::I to &D::I (where B is a base of D), so we need
6026 to reject this conversion here.
6028 Actually, even if there was a way to disable standard conversions,
6029 it would still be better to reject them here so that we can
6030 provide a superior diagnostic. */
6031 if (!same_type_p (TREE_TYPE (expr
), type
))
6033 error ("%qE is not a valid template argument for type %qT "
6034 "because it is of type %qT", expr
, type
,
6036 /* If we are just one standard conversion off, explain. */
6037 if (can_convert (type
, TREE_TYPE (expr
)))
6038 inform (input_location
,
6039 "standard conversions are not allowed in this context");
6043 /* [temp.arg.nontype]/5, bullet 7
6045 For a non-type template-parameter of type pointer to data member,
6046 qualification conversions (_conv.qual_) are applied. */
6047 else if (TYPE_PTRMEM_P (type
))
6049 /* [temp.arg.nontype] bullet 1 says the pointer to member
6050 expression must be a pointer-to-member constant. */
6051 if (!check_valid_ptrmem_cst_expr (type
, expr
, complain
))
6052 return error_mark_node
;
6054 expr
= perform_qualification_conversions (type
, expr
);
6055 if (expr
== error_mark_node
)
6058 else if (NULLPTR_TYPE_P (type
))
6060 if (expr
!= nullptr_node
)
6062 error ("%qE is not a valid template argument for type %qT "
6063 "because it is of type %qT", expr
, type
, TREE_TYPE (expr
));
6068 /* A template non-type parameter must be one of the above. */
6072 /* Sanity check: did we actually convert the argument to the
6074 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6075 (type
, TREE_TYPE (expr
)));
6079 /* Subroutine of coerce_template_template_parms, which returns 1 if
6080 PARM_PARM and ARG_PARM match using the rule for the template
6081 parameters of template template parameters. Both PARM and ARG are
6082 template parameters; the rest of the arguments are the same as for
6083 coerce_template_template_parms.
6086 coerce_template_template_parm (tree parm
,
6088 tsubst_flags_t complain
,
6092 if (arg
== NULL_TREE
|| arg
== error_mark_node
6093 || parm
== NULL_TREE
|| parm
== error_mark_node
)
6096 if (TREE_CODE (arg
) != TREE_CODE (parm
))
6099 switch (TREE_CODE (parm
))
6102 /* We encounter instantiations of templates like
6103 template <template <template <class> class> class TT>
6106 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6107 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6109 if (!coerce_template_template_parms
6110 (parmparm
, argparm
, complain
, in_decl
, outer_args
))
6116 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg
))
6117 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6118 /* Argument is a parameter pack but parameter is not. */
6123 /* The tsubst call is used to handle cases such as
6125 template <int> class C {};
6126 template <class T, template <T> class TT> class D {};
6129 i.e. the parameter list of TT depends on earlier parameters. */
6130 if (!uses_template_parms (TREE_TYPE (arg
))
6132 (tsubst (TREE_TYPE (parm
), outer_args
, complain
, in_decl
),
6136 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg
))
6137 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6138 /* Argument is a parameter pack but parameter is not. */
6151 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
6152 template template parameters. Both PARM_PARMS and ARG_PARMS are
6153 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
6156 Consider the example:
6157 template <class T> class A;
6158 template<template <class U> class TT> class B;
6160 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
6161 the parameters to A, and OUTER_ARGS contains A. */
6164 coerce_template_template_parms (tree parm_parms
,
6166 tsubst_flags_t complain
,
6170 int nparms
, nargs
, i
;
6174 gcc_assert (TREE_CODE (parm_parms
) == TREE_VEC
);
6175 gcc_assert (TREE_CODE (arg_parms
) == TREE_VEC
);
6177 nparms
= TREE_VEC_LENGTH (parm_parms
);
6178 nargs
= TREE_VEC_LENGTH (arg_parms
);
6180 /* Determine whether we have a parameter pack at the end of the
6181 template template parameter's template parameter list. */
6182 if (TREE_VEC_ELT (parm_parms
, nparms
- 1) != error_mark_node
)
6184 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, nparms
- 1));
6186 if (parm
== error_mark_node
)
6189 switch (TREE_CODE (parm
))
6193 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm
)))
6198 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
6208 && !(variadic_p
&& nargs
>= nparms
- 1))
6211 /* Check all of the template parameters except the parameter pack at
6212 the end (if any). */
6213 for (i
= 0; i
< nparms
- variadic_p
; ++i
)
6215 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
6216 || TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6219 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6220 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6222 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6230 /* Check each of the template parameters in the template
6231 argument against the template parameter pack at the end of
6232 the template template parameter. */
6233 if (TREE_VEC_ELT (parm_parms
, i
) == error_mark_node
)
6236 parm
= TREE_VALUE (TREE_VEC_ELT (parm_parms
, i
));
6238 for (; i
< nargs
; ++i
)
6240 if (TREE_VEC_ELT (arg_parms
, i
) == error_mark_node
)
6243 arg
= TREE_VALUE (TREE_VEC_ELT (arg_parms
, i
));
6245 if (!coerce_template_template_parm (parm
, arg
, complain
, in_decl
,
6254 /* Verifies that the deduced template arguments (in TARGS) for the
6255 template template parameters (in TPARMS) represent valid bindings,
6256 by comparing the template parameter list of each template argument
6257 to the template parameter list of its corresponding template
6258 template parameter, in accordance with DR150. This
6259 routine can only be called after all template arguments have been
6260 deduced. It will return TRUE if all of the template template
6261 parameter bindings are okay, FALSE otherwise. */
6263 template_template_parm_bindings_ok_p (tree tparms
, tree targs
)
6265 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6268 /* We're dealing with template parms in this process. */
6269 ++processing_template_decl
;
6271 targs
= INNERMOST_TEMPLATE_ARGS (targs
);
6273 for (i
= 0; i
< ntparms
; ++i
)
6275 tree tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
6276 tree targ
= TREE_VEC_ELT (targs
, i
);
6278 if (TREE_CODE (tparm
) == TEMPLATE_DECL
&& targ
)
6280 tree packed_args
= NULL_TREE
;
6283 if (ARGUMENT_PACK_P (targ
))
6285 /* Look inside the argument pack. */
6286 packed_args
= ARGUMENT_PACK_ARGS (targ
);
6287 len
= TREE_VEC_LENGTH (packed_args
);
6290 for (idx
= 0; idx
< len
; ++idx
)
6292 tree targ_parms
= NULL_TREE
;
6295 /* Extract the next argument from the argument
6297 targ
= TREE_VEC_ELT (packed_args
, idx
);
6299 if (PACK_EXPANSION_P (targ
))
6300 /* Look at the pattern of the pack expansion. */
6301 targ
= PACK_EXPANSION_PATTERN (targ
);
6303 /* Extract the template parameters from the template
6305 if (TREE_CODE (targ
) == TEMPLATE_DECL
)
6306 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (targ
);
6307 else if (TREE_CODE (targ
) == TEMPLATE_TEMPLATE_PARM
)
6308 targ_parms
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ
));
6310 /* Verify that we can coerce the template template
6311 parameters from the template argument to the template
6312 parameter. This requires an exact match. */
6314 && !coerce_template_template_parms
6315 (DECL_INNERMOST_TEMPLATE_PARMS (tparm
),
6330 --processing_template_decl
;
6334 /* Since type attributes aren't mangled, we need to strip them from
6335 template type arguments. */
6338 canonicalize_type_argument (tree arg
, tsubst_flags_t complain
)
6341 if (!arg
|| arg
== error_mark_node
|| arg
== TYPE_CANONICAL (arg
))
6343 mv
= TYPE_MAIN_VARIANT (arg
);
6344 arg
= strip_typedefs (arg
);
6345 if (TYPE_ALIGN (arg
) != TYPE_ALIGN (mv
)
6346 || TYPE_ATTRIBUTES (arg
) != TYPE_ATTRIBUTES (mv
))
6348 if (complain
& tf_warning
)
6349 warning (0, "ignoring attributes on template argument %qT", arg
);
6350 arg
= build_aligned_type (arg
, TYPE_ALIGN (mv
));
6351 arg
= cp_build_type_attribute_variant (arg
, TYPE_ATTRIBUTES (mv
));
6356 /* Convert the indicated template ARG as necessary to match the
6357 indicated template PARM. Returns the converted ARG, or
6358 error_mark_node if the conversion was unsuccessful. Error and
6359 warning messages are issued under control of COMPLAIN. This
6360 conversion is for the Ith parameter in the parameter list. ARGS is
6361 the full set of template arguments deduced so far. */
6364 convert_template_argument (tree parm
,
6367 tsubst_flags_t complain
,
6373 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
6375 if (TREE_CODE (arg
) == TREE_LIST
6376 && TREE_CODE (TREE_VALUE (arg
)) == OFFSET_REF
)
6378 /* The template argument was the name of some
6379 member function. That's usually
6380 invalid, but static members are OK. In any
6381 case, grab the underlying fields/functions
6382 and issue an error later if required. */
6383 orig_arg
= TREE_VALUE (arg
);
6384 TREE_TYPE (arg
) = unknown_type_node
;
6389 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
6390 requires_type
= (TREE_CODE (parm
) == TYPE_DECL
6391 || requires_tmpl_type
);
6393 /* When determining whether an argument pack expansion is a template,
6394 look at the pattern. */
6395 if (TREE_CODE (arg
) == TYPE_PACK_EXPANSION
)
6396 arg
= PACK_EXPANSION_PATTERN (arg
);
6398 /* Deal with an injected-class-name used as a template template arg. */
6399 if (requires_tmpl_type
&& CLASS_TYPE_P (arg
))
6401 tree t
= maybe_get_template_decl_from_type_decl (TYPE_NAME (arg
));
6402 if (TREE_CODE (t
) == TEMPLATE_DECL
)
6404 if (cxx_dialect
>= cxx0x
)
6405 /* OK under DR 1004. */;
6406 else if (complain
& tf_warning_or_error
)
6407 pedwarn (input_location
, OPT_pedantic
, "injected-class-name %qD"
6408 " used as template template argument", TYPE_NAME (arg
));
6409 else if (flag_pedantic_errors
)
6417 ((TREE_CODE (arg
) == TEMPLATE_DECL
6418 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
6419 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6420 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
6423 && (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
6424 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
))
6425 arg
= TYPE_STUB_DECL (arg
);
6427 is_type
= TYPE_P (arg
) || is_tmpl_type
;
6429 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
6430 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
6432 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == BIT_NOT_EXPR
)
6434 if (complain
& tf_error
)
6435 error ("invalid use of destructor %qE as a type", orig_arg
);
6436 return error_mark_node
;
6439 permerror (input_location
,
6440 "to refer to a type member of a template parameter, "
6441 "use %<typename %E%>", orig_arg
);
6443 orig_arg
= make_typename_type (TREE_OPERAND (arg
, 0),
6444 TREE_OPERAND (arg
, 1),
6446 complain
& tf_error
);
6450 if (is_type
!= requires_type
)
6454 if (complain
& tf_error
)
6456 error ("type/value mismatch at argument %d in template "
6457 "parameter list for %qD",
6460 error (" expected a constant of type %qT, got %qT",
6462 (DECL_P (arg
) ? DECL_NAME (arg
) : orig_arg
));
6463 else if (requires_tmpl_type
)
6464 error (" expected a class template, got %qE", orig_arg
);
6466 error (" expected a type, got %qE", orig_arg
);
6469 return error_mark_node
;
6471 if (is_tmpl_type
^ requires_tmpl_type
)
6473 if (in_decl
&& (complain
& tf_error
))
6475 error ("type/value mismatch at argument %d in template "
6476 "parameter list for %qD",
6479 error (" expected a type, got %qT", DECL_NAME (arg
));
6481 error (" expected a class template, got %qT", orig_arg
);
6483 return error_mark_node
;
6488 if (requires_tmpl_type
)
6490 if (TREE_CODE (TREE_TYPE (arg
)) == UNBOUND_CLASS_TEMPLATE
)
6491 /* The number of argument required is not known yet.
6492 Just accept it for now. */
6493 val
= TREE_TYPE (arg
);
6496 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
6499 argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
6501 if (coerce_template_template_parms (parmparm
, argparm
,
6507 /* TEMPLATE_TEMPLATE_PARM node is preferred over
6509 if (val
!= error_mark_node
)
6511 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
6512 val
= TREE_TYPE (val
);
6513 if (TREE_CODE (orig_arg
) == TYPE_PACK_EXPANSION
)
6514 val
= make_pack_expansion (val
);
6519 if (in_decl
&& (complain
& tf_error
))
6521 error ("type/value mismatch at argument %d in "
6522 "template parameter list for %qD",
6524 error (" expected a template of type %qD, got %qT",
6528 val
= error_mark_node
;
6534 /* We only form one instance of each template specialization.
6535 Therefore, if we use a non-canonical variant (i.e., a
6536 typedef), any future messages referring to the type will use
6537 the typedef, which is confusing if those future uses do not
6538 themselves also use the typedef. */
6540 val
= canonicalize_type_argument (val
, complain
);
6544 tree t
= tsubst (TREE_TYPE (parm
), args
, complain
, in_decl
);
6546 if (invalid_nontype_parm_type_p (t
, complain
))
6547 return error_mark_node
;
6549 if (template_parameter_pack_p (parm
) && ARGUMENT_PACK_P (orig_arg
))
6551 if (same_type_p (t
, TREE_TYPE (orig_arg
)))
6555 /* Not sure if this is reachable, but it doesn't hurt
6557 error ("type mismatch in nontype parameter pack");
6558 val
= error_mark_node
;
6561 else if (!uses_template_parms (orig_arg
) && !uses_template_parms (t
))
6562 /* We used to call digest_init here. However, digest_init
6563 will report errors, which we don't want when complain
6564 is zero. More importantly, digest_init will try too
6565 hard to convert things: for example, `0' should not be
6566 converted to pointer type at this point according to
6567 the standard. Accepting this is not merely an
6568 extension, since deciding whether or not these
6569 conversions can occur is part of determining which
6570 function template to call, or whether a given explicit
6571 argument specification is valid. */
6572 val
= convert_nontype_argument (t
, orig_arg
, complain
);
6576 if (val
== NULL_TREE
)
6577 val
= error_mark_node
;
6578 else if (val
== error_mark_node
&& (complain
& tf_error
))
6579 error ("could not convert template argument %qE to %qT", orig_arg
, t
);
6581 if (TREE_CODE (val
) == SCOPE_REF
)
6583 /* Strip typedefs from the SCOPE_REF. */
6584 tree type
= canonicalize_type_argument (TREE_TYPE (val
), complain
);
6585 tree scope
= canonicalize_type_argument (TREE_OPERAND (val
, 0),
6587 val
= build_qualified_name (type
, scope
, TREE_OPERAND (val
, 1),
6588 QUALIFIED_NAME_IS_TEMPLATE (val
));
6595 /* Coerces the remaining template arguments in INNER_ARGS (from
6596 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
6597 Returns the coerced argument pack. PARM_IDX is the position of this
6598 parameter in the template parameter list. ARGS is the original
6599 template argument list. */
6601 coerce_template_parameter_pack (tree parms
,
6609 tsubst_flags_t complain
)
6611 tree parm
= TREE_VEC_ELT (parms
, parm_idx
);
6612 int nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6615 tree packed_types
= NULL_TREE
;
6617 if (arg_idx
> nargs
)
6620 packed_args
= make_tree_vec (nargs
- arg_idx
);
6622 if (TREE_CODE (TREE_VALUE (parm
)) == PARM_DECL
6623 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm
))))
6625 /* When the template parameter is a non-type template
6626 parameter pack whose type uses parameter packs, we need
6627 to look at each of the template arguments
6628 separately. Build a vector of the types for these
6629 non-type template parameters in PACKED_TYPES. */
6631 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm
)));
6632 packed_types
= tsubst_pack_expansion (expansion
, args
,
6635 if (packed_types
== error_mark_node
)
6636 return error_mark_node
;
6638 /* Check that we have the right number of arguments. */
6640 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
))
6641 && nargs
- arg_idx
!= TREE_VEC_LENGTH (packed_types
))
6644 = TREE_VEC_LENGTH (parms
) - 1 + TREE_VEC_LENGTH (packed_types
);
6645 error ("wrong number of template arguments (%d, should be %d)",
6646 nargs
, needed_parms
);
6647 return error_mark_node
;
6650 /* If we aren't able to check the actual arguments now
6651 (because they haven't been expanded yet), we can at least
6652 verify that all of the types used for the non-type
6653 template parameter pack are, in fact, valid for non-type
6654 template parameters. */
6656 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args
, arg_idx
)))
6658 int j
, len
= TREE_VEC_LENGTH (packed_types
);
6659 for (j
= 0; j
< len
; ++j
)
6661 tree t
= TREE_VEC_ELT (packed_types
, j
);
6662 if (invalid_nontype_parm_type_p (t
, complain
))
6663 return error_mark_node
;
6668 /* Convert the remaining arguments, which will be a part of the
6669 parameter pack "parm". */
6670 for (; arg_idx
< nargs
; ++arg_idx
)
6672 tree arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6673 tree actual_parm
= TREE_VALUE (parm
);
6675 if (packed_types
&& !PACK_EXPANSION_P (arg
))
6677 /* When we have a vector of types (corresponding to the
6678 non-type template parameter pack that uses parameter
6679 packs in its type, as mention above), and the
6680 argument is not an expansion (which expands to a
6681 currently unknown number of arguments), clone the
6682 parm and give it the next type in PACKED_TYPES. */
6683 actual_parm
= copy_node (actual_parm
);
6684 TREE_TYPE (actual_parm
) =
6685 TREE_VEC_ELT (packed_types
, arg_idx
- parm_idx
);
6688 if (arg
!= error_mark_node
)
6689 arg
= convert_template_argument (actual_parm
,
6690 arg
, new_args
, complain
, parm_idx
,
6692 if (arg
== error_mark_node
)
6694 TREE_VEC_ELT (packed_args
, arg_idx
- parm_idx
) = arg
;
6697 if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
6698 || TREE_CODE (TREE_VALUE (parm
)) == TEMPLATE_DECL
)
6699 argument_pack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
6702 argument_pack
= make_node (NONTYPE_ARGUMENT_PACK
);
6703 TREE_TYPE (argument_pack
)
6704 = tsubst (TREE_TYPE (TREE_VALUE (parm
)), new_args
, complain
, in_decl
);
6705 TREE_CONSTANT (argument_pack
) = 1;
6708 SET_ARGUMENT_PACK_ARGS (argument_pack
, packed_args
);
6709 #ifdef ENABLE_CHECKING
6710 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args
,
6711 TREE_VEC_LENGTH (packed_args
));
6713 return argument_pack
;
6716 /* Convert all template arguments to their appropriate types, and
6717 return a vector containing the innermost resulting template
6718 arguments. If any error occurs, return error_mark_node. Error and
6719 warning messages are issued under control of COMPLAIN.
6721 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
6722 for arguments not specified in ARGS. Otherwise, if
6723 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
6724 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
6725 USE_DEFAULT_ARGS is false, then all arguments must be specified in
6729 coerce_template_parms (tree parms
,
6732 tsubst_flags_t complain
,
6733 bool require_all_args
,
6734 bool use_default_args
)
6736 int nparms
, nargs
, parm_idx
, arg_idx
, lost
= 0;
6739 tree new_inner_args
;
6740 int saved_unevaluated_operand
;
6741 int saved_inhibit_evaluation_warnings
;
6743 /* When used as a boolean value, indicates whether this is a
6744 variadic template parameter list. Since it's an int, we can also
6745 subtract it from nparms to get the number of non-variadic
6748 int post_variadic_parms
= 0;
6750 if (args
== error_mark_node
)
6751 return error_mark_node
;
6753 nparms
= TREE_VEC_LENGTH (parms
);
6755 /* Determine if there are any parameter packs. */
6756 for (parm_idx
= 0; parm_idx
< nparms
; ++parm_idx
)
6758 tree tparm
= TREE_VALUE (TREE_VEC_ELT (parms
, parm_idx
));
6760 ++post_variadic_parms
;
6761 if (template_parameter_pack_p (tparm
))
6765 inner_args
= INNERMOST_TEMPLATE_ARGS (args
);
6766 /* If there are no parameters that follow a parameter pack, we need to
6767 expand any argument packs so that we can deduce a parameter pack from
6768 some non-packed args followed by an argument pack, as in variadic85.C.
6769 If there are such parameters, we need to leave argument packs intact
6770 so the arguments are assigned properly. This can happen when dealing
6771 with a nested class inside a partial specialization of a class
6772 template, as in variadic92.C, or when deducing a template parameter pack
6773 from a sub-declarator, as in variadic114.C. */
6774 if (!post_variadic_parms
)
6775 inner_args
= expand_template_argument_pack (inner_args
);
6777 nargs
= inner_args
? NUM_TMPL_ARGS (inner_args
) : 0;
6778 if ((nargs
> nparms
&& !variadic_p
)
6779 || (nargs
< nparms
- variadic_p
6781 && (!use_default_args
6782 || (TREE_VEC_ELT (parms
, nargs
) != error_mark_node
6783 && !TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
))))))
6785 if (complain
& tf_error
)
6789 nparms
-= variadic_p
;
6790 error ("wrong number of template arguments "
6791 "(%d, should be %d or more)", nargs
, nparms
);
6794 error ("wrong number of template arguments "
6795 "(%d, should be %d)", nargs
, nparms
);
6798 error ("provided for %q+D", in_decl
);
6801 return error_mark_node
;
6804 /* We need to evaluate the template arguments, even though this
6805 template-id may be nested within a "sizeof". */
6806 saved_unevaluated_operand
= cp_unevaluated_operand
;
6807 cp_unevaluated_operand
= 0;
6808 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6809 c_inhibit_evaluation_warnings
= 0;
6810 new_inner_args
= make_tree_vec (nparms
);
6811 new_args
= add_outermost_template_args (args
, new_inner_args
);
6812 for (parm_idx
= 0, arg_idx
= 0; parm_idx
< nparms
; parm_idx
++, arg_idx
++)
6817 /* Get the Ith template parameter. */
6818 parm
= TREE_VEC_ELT (parms
, parm_idx
);
6820 if (parm
== error_mark_node
)
6822 TREE_VEC_ELT (new_inner_args
, arg_idx
) = error_mark_node
;
6826 /* Calculate the next argument. */
6827 if (arg_idx
< nargs
)
6828 arg
= TREE_VEC_ELT (inner_args
, arg_idx
);
6832 if (template_parameter_pack_p (TREE_VALUE (parm
))
6833 && !(arg
&& ARGUMENT_PACK_P (arg
)))
6835 /* All remaining arguments will be placed in the
6836 template parameter pack PARM. */
6837 arg
= coerce_template_parameter_pack (parms
, parm_idx
, args
,
6838 inner_args
, arg_idx
,
6842 /* Store this argument. */
6843 if (arg
== error_mark_node
)
6845 TREE_VEC_ELT (new_inner_args
, parm_idx
) = arg
;
6847 /* We are done with all of the arguments. */
6854 if (PACK_EXPANSION_P (arg
))
6856 /* We don't know how many args we have yet, just
6857 use the unconverted ones for now. */
6858 new_inner_args
= args
;
6862 else if (require_all_args
)
6864 /* There must be a default arg in this case. */
6865 arg
= tsubst_template_arg (TREE_PURPOSE (parm
), new_args
,
6867 /* The position of the first default template argument,
6868 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
6870 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6871 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
, arg_idx
);
6876 if (arg
== error_mark_node
)
6878 if (complain
& tf_error
)
6879 error ("template argument %d is invalid", arg_idx
+ 1);
6882 /* This only occurs if there was an error in the template
6883 parameter list itself (which we would already have
6884 reported) that we are trying to recover from, e.g., a class
6885 template with a parameter list such as
6886 template<typename..., typename>. */
6889 arg
= convert_template_argument (TREE_VALUE (parm
),
6890 arg
, new_args
, complain
,
6893 if (arg
== error_mark_node
)
6895 TREE_VEC_ELT (new_inner_args
, arg_idx
) = arg
;
6897 cp_unevaluated_operand
= saved_unevaluated_operand
;
6898 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
6901 return error_mark_node
;
6903 #ifdef ENABLE_CHECKING
6904 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
))
6905 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args
,
6906 TREE_VEC_LENGTH (new_inner_args
));
6909 return new_inner_args
;
6912 /* Returns 1 if template args OT and NT are equivalent. */
6915 template_args_equal (tree ot
, tree nt
)
6919 if (nt
== NULL_TREE
|| ot
== NULL_TREE
)
6922 if (TREE_CODE (nt
) == TREE_VEC
)
6923 /* For member templates */
6924 return TREE_CODE (ot
) == TREE_VEC
&& comp_template_args (ot
, nt
);
6925 else if (PACK_EXPANSION_P (ot
))
6926 return (PACK_EXPANSION_P (nt
)
6927 && template_args_equal (PACK_EXPANSION_PATTERN (ot
),
6928 PACK_EXPANSION_PATTERN (nt
))
6929 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot
),
6930 PACK_EXPANSION_EXTRA_ARGS (nt
)));
6931 else if (ARGUMENT_PACK_P (ot
))
6936 if (!ARGUMENT_PACK_P (nt
))
6939 opack
= ARGUMENT_PACK_ARGS (ot
);
6940 npack
= ARGUMENT_PACK_ARGS (nt
);
6941 len
= TREE_VEC_LENGTH (opack
);
6942 if (TREE_VEC_LENGTH (npack
) != len
)
6944 for (i
= 0; i
< len
; ++i
)
6945 if (!template_args_equal (TREE_VEC_ELT (opack
, i
),
6946 TREE_VEC_ELT (npack
, i
)))
6950 else if (ot
&& TREE_CODE (ot
) == ARGUMENT_PACK_SELECT
)
6952 /* We get here probably because we are in the middle of substituting
6953 into the pattern of a pack expansion. In that case the
6954 ARGUMENT_PACK_SELECT temporarily replaces the pack argument we are
6955 interested in. So we want to use the initial pack argument for
6957 ot
= ARGUMENT_PACK_SELECT_FROM_PACK (ot
);
6958 if (nt
&& TREE_CODE (nt
) == ARGUMENT_PACK_SELECT
)
6959 nt
= ARGUMENT_PACK_SELECT_FROM_PACK (nt
);
6960 return template_args_equal (ot
, nt
);
6962 else if (TYPE_P (nt
))
6963 return TYPE_P (ot
) && same_type_p (ot
, nt
);
6964 else if (TREE_CODE (ot
) == TREE_VEC
|| TYPE_P (ot
))
6967 return cp_tree_equal (ot
, nt
);
6970 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
6971 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
6972 NEWARG_PTR with the offending arguments if they are non-NULL. */
6975 comp_template_args_with_info (tree oldargs
, tree newargs
,
6976 tree
*oldarg_ptr
, tree
*newarg_ptr
)
6980 if (oldargs
== newargs
)
6983 if (!oldargs
|| !newargs
)
6986 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
6989 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
6991 tree nt
= TREE_VEC_ELT (newargs
, i
);
6992 tree ot
= TREE_VEC_ELT (oldargs
, i
);
6994 if (! template_args_equal (ot
, nt
))
6996 if (oldarg_ptr
!= NULL
)
6998 if (newarg_ptr
!= NULL
)
7006 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
7007 of template arguments. Returns 0 otherwise. */
7010 comp_template_args (tree oldargs
, tree newargs
)
7012 return comp_template_args_with_info (oldargs
, newargs
, NULL
, NULL
);
7016 add_pending_template (tree d
)
7018 tree ti
= (TYPE_P (d
)
7019 ? CLASSTYPE_TEMPLATE_INFO (d
)
7020 : DECL_TEMPLATE_INFO (d
));
7021 struct pending_template
*pt
;
7024 if (TI_PENDING_TEMPLATE_FLAG (ti
))
7027 /* We are called both from instantiate_decl, where we've already had a
7028 tinst_level pushed, and instantiate_template, where we haven't.
7030 level
= !current_tinst_level
|| current_tinst_level
->decl
!= d
;
7033 push_tinst_level (d
);
7035 pt
= ggc_alloc_pending_template ();
7037 pt
->tinst
= current_tinst_level
;
7038 if (last_pending_template
)
7039 last_pending_template
->next
= pt
;
7041 pending_templates
= pt
;
7043 last_pending_template
= pt
;
7045 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
7052 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
7053 ARGLIST. Valid choices for FNS are given in the cp-tree.def
7054 documentation for TEMPLATE_ID_EXPR. */
7057 lookup_template_function (tree fns
, tree arglist
)
7061 if (fns
== error_mark_node
|| arglist
== error_mark_node
)
7062 return error_mark_node
;
7064 gcc_assert (!arglist
|| TREE_CODE (arglist
) == TREE_VEC
);
7066 if (!is_overloaded_fn (fns
) && TREE_CODE (fns
) != IDENTIFIER_NODE
)
7068 error ("%q#D is not a function template", fns
);
7069 return error_mark_node
;
7072 if (BASELINK_P (fns
))
7074 BASELINK_FUNCTIONS (fns
) = build2 (TEMPLATE_ID_EXPR
,
7076 BASELINK_FUNCTIONS (fns
),
7081 type
= TREE_TYPE (fns
);
7082 if (TREE_CODE (fns
) == OVERLOAD
|| !type
)
7083 type
= unknown_type_node
;
7085 return build2 (TEMPLATE_ID_EXPR
, type
, fns
, arglist
);
7088 /* Within the scope of a template class S<T>, the name S gets bound
7089 (in build_self_reference) to a TYPE_DECL for the class, not a
7090 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
7091 or one of its enclosing classes, and that type is a template,
7092 return the associated TEMPLATE_DECL. Otherwise, the original
7095 Also handle the case when DECL is a TREE_LIST of ambiguous
7096 injected-class-names from different bases. */
7099 maybe_get_template_decl_from_type_decl (tree decl
)
7101 if (decl
== NULL_TREE
)
7104 /* DR 176: A lookup that finds an injected-class-name (10.2
7105 [class.member.lookup]) can result in an ambiguity in certain cases
7106 (for example, if it is found in more than one base class). If all of
7107 the injected-class-names that are found refer to specializations of
7108 the same class template, and if the name is followed by a
7109 template-argument-list, the reference refers to the class template
7110 itself and not a specialization thereof, and is not ambiguous. */
7111 if (TREE_CODE (decl
) == TREE_LIST
)
7113 tree t
, tmpl
= NULL_TREE
;
7114 for (t
= decl
; t
; t
= TREE_CHAIN (t
))
7116 tree elt
= maybe_get_template_decl_from_type_decl (TREE_VALUE (t
));
7119 else if (tmpl
!= elt
)
7122 if (tmpl
&& t
== NULL_TREE
)
7128 return (decl
!= NULL_TREE
7129 && DECL_SELF_REFERENCE_P (decl
)
7130 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
7131 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
7134 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
7135 parameters, find the desired type.
7137 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
7139 IN_DECL, if non-NULL, is the template declaration we are trying to
7142 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
7143 the class we are looking up.
7145 Issue error and warning messages under control of COMPLAIN.
7147 If the template class is really a local class in a template
7148 function, then the FUNCTION_CONTEXT is the function in which it is
7151 ??? Note that this function is currently called *twice* for each
7152 template-id: the first time from the parser, while creating the
7153 incomplete type (finish_template_type), and the second type during the
7154 real instantiation (instantiate_template_class). This is surely something
7155 that we want to avoid. It also causes some problems with argument
7156 coercion (see convert_nontype_argument for more information on this). */
7159 lookup_template_class_1 (tree d1
, tree arglist
, tree in_decl
, tree context
,
7160 int entering_scope
, tsubst_flags_t complain
)
7162 tree templ
= NULL_TREE
, parmlist
;
7169 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
7171 tree value
= innermost_non_namespace_value (d1
);
7172 if (value
&& DECL_TEMPLATE_TEMPLATE_PARM_P (value
))
7177 push_decl_namespace (context
);
7178 templ
= lookup_name (d1
);
7179 templ
= maybe_get_template_decl_from_type_decl (templ
);
7181 pop_decl_namespace ();
7184 context
= DECL_CONTEXT (templ
);
7186 else if (TREE_CODE (d1
) == TYPE_DECL
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (d1
)))
7188 tree type
= TREE_TYPE (d1
);
7190 /* If we are declaring a constructor, say A<T>::A<T>, we will get
7191 an implicit typename for the second A. Deal with it. */
7192 if (TREE_CODE (type
) == TYPENAME_TYPE
&& TREE_TYPE (type
))
7193 type
= TREE_TYPE (type
);
7195 if (CLASSTYPE_TEMPLATE_INFO (type
))
7197 templ
= CLASSTYPE_TI_TEMPLATE (type
);
7198 d1
= DECL_NAME (templ
);
7201 else if (TREE_CODE (d1
) == ENUMERAL_TYPE
7202 || (TYPE_P (d1
) && MAYBE_CLASS_TYPE_P (d1
)))
7204 templ
= TYPE_TI_TEMPLATE (d1
);
7205 d1
= DECL_NAME (templ
);
7207 else if (TREE_CODE (d1
) == TEMPLATE_DECL
7208 && DECL_TEMPLATE_RESULT (d1
)
7209 && TREE_CODE (DECL_TEMPLATE_RESULT (d1
)) == TYPE_DECL
)
7212 d1
= DECL_NAME (templ
);
7213 context
= DECL_CONTEXT (templ
);
7216 /* Issue an error message if we didn't find a template. */
7219 if (complain
& tf_error
)
7220 error ("%qT is not a template", d1
);
7221 return error_mark_node
;
7224 if (TREE_CODE (templ
) != TEMPLATE_DECL
7225 /* Make sure it's a user visible template, if it was named by
7227 || ((complain
& tf_user
) && !DECL_TEMPLATE_PARM_P (templ
)
7228 && !PRIMARY_TEMPLATE_P (templ
)))
7230 if (complain
& tf_error
)
7232 error ("non-template type %qT used as a template", d1
);
7234 error ("for template declaration %q+D", in_decl
);
7236 return error_mark_node
;
7239 complain
&= ~tf_user
;
7241 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
7243 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
7244 template arguments */
7250 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (templ
);
7252 /* Consider an example where a template template parameter declared as
7254 template <class T, class U = std::allocator<T> > class TT
7256 The template parameter level of T and U are one level larger than
7257 of TT. To proper process the default argument of U, say when an
7258 instantiation `TT<int>' is seen, we need to build the full
7259 arguments containing {int} as the innermost level. Outer levels,
7260 available when not appearing as default template argument, can be
7261 obtained from the arguments of the enclosing template.
7263 Suppose that TT is later substituted with std::vector. The above
7264 instantiation is `TT<int, std::allocator<T> >' with TT at
7265 level 1, and T at level 2, while the template arguments at level 1
7266 becomes {std::vector} and the inner level 2 is {int}. */
7268 outer
= DECL_CONTEXT (templ
);
7270 outer
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer
)));
7271 else if (current_template_parms
)
7272 /* This is an argument of the current template, so we haven't set
7273 DECL_CONTEXT yet. */
7274 outer
= current_template_args ();
7277 arglist
= add_to_template_args (outer
, arglist
);
7279 arglist2
= coerce_template_parms (parmlist
, arglist
, templ
,
7281 /*require_all_args=*/true,
7282 /*use_default_args=*/true);
7283 if (arglist2
== error_mark_node
7284 || (!uses_template_parms (arglist2
)
7285 && check_instantiated_args (templ
, arglist2
, complain
)))
7286 return error_mark_node
;
7288 parm
= bind_template_template_parm (TREE_TYPE (templ
), arglist2
);
7293 tree template_type
= TREE_TYPE (templ
);
7296 tree found
= NULL_TREE
;
7299 int is_dependent_type
;
7300 int use_partial_inst_tmpl
= false;
7302 if (template_type
== error_mark_node
)
7303 /* An error occured while building the template TEMPL, and a
7304 diagnostic has most certainly been emitted for that
7305 already. Let's propagate that error. */
7306 return error_mark_node
;
7308 gen_tmpl
= most_general_template (templ
);
7309 parmlist
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7310 parm_depth
= TMPL_PARMS_DEPTH (parmlist
);
7311 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7313 if (arg_depth
== 1 && parm_depth
> 1)
7315 /* We've been given an incomplete set of template arguments.
7318 template <class T> struct S1 {
7319 template <class U> struct S2 {};
7320 template <class U> struct S2<U*> {};
7323 we will be called with an ARGLIST of `U*', but the
7324 TEMPLATE will be `template <class T> template
7325 <class U> struct S1<T>::S2'. We must fill in the missing
7328 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ
)),
7330 arg_depth
= TMPL_ARGS_DEPTH (arglist
);
7333 /* Now we should have enough arguments. */
7334 gcc_assert (parm_depth
== arg_depth
);
7336 /* From here on, we're only interested in the most general
7339 /* Calculate the BOUND_ARGS. These will be the args that are
7340 actually tsubst'd into the definition to create the
7344 /* We have multiple levels of arguments to coerce, at once. */
7346 int saved_depth
= TMPL_ARGS_DEPTH (arglist
);
7348 tree bound_args
= make_tree_vec (parm_depth
);
7350 for (i
= saved_depth
,
7351 t
= DECL_TEMPLATE_PARMS (gen_tmpl
);
7352 i
> 0 && t
!= NULL_TREE
;
7353 --i
, t
= TREE_CHAIN (t
))
7356 if (i
== saved_depth
)
7357 a
= coerce_template_parms (TREE_VALUE (t
),
7360 /*require_all_args=*/true,
7361 /*use_default_args=*/true);
7363 /* Outer levels should have already been coerced. */
7364 a
= TMPL_ARGS_LEVEL (arglist
, i
);
7366 /* Don't process further if one of the levels fails. */
7367 if (a
== error_mark_node
)
7369 /* Restore the ARGLIST to its full size. */
7370 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7371 return error_mark_node
;
7374 SET_TMPL_ARGS_LEVEL (bound_args
, i
, a
);
7376 /* We temporarily reduce the length of the ARGLIST so
7377 that coerce_template_parms will see only the arguments
7378 corresponding to the template parameters it is
7380 TREE_VEC_LENGTH (arglist
)--;
7383 /* Restore the ARGLIST to its full size. */
7384 TREE_VEC_LENGTH (arglist
) = saved_depth
;
7386 arglist
= bound_args
;
7390 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist
),
7391 INNERMOST_TEMPLATE_ARGS (arglist
),
7394 /*require_all_args=*/true,
7395 /*use_default_args=*/true);
7397 if (arglist
== error_mark_node
)
7398 /* We were unable to bind the arguments. */
7399 return error_mark_node
;
7401 /* In the scope of a template class, explicit references to the
7402 template class refer to the type of the template, not any
7403 instantiation of it. For example, in:
7405 template <class T> class C { void f(C<T>); }
7407 the `C<T>' is just the same as `C'. Outside of the
7408 class, however, such a reference is an instantiation. */
7410 || !PRIMARY_TEMPLATE_P (gen_tmpl
)
7411 || currently_open_class (template_type
))
7412 /* comp_template_args is expensive, check it last. */
7413 && comp_template_args (TYPE_TI_ARGS (template_type
),
7415 return template_type
;
7417 /* If we already have this specialization, return it. */
7418 elt
.tmpl
= gen_tmpl
;
7420 hash
= hash_specialization (&elt
);
7421 entry
= (spec_entry
*) htab_find_with_hash (type_specializations
,
7427 is_dependent_type
= uses_template_parms (arglist
);
7429 /* If the deduced arguments are invalid, then the binding
7431 if (!is_dependent_type
7432 && check_instantiated_args (gen_tmpl
,
7433 INNERMOST_TEMPLATE_ARGS (arglist
),
7435 return error_mark_node
;
7437 if (!is_dependent_type
7438 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
7439 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl
))
7440 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl
)) == NAMESPACE_DECL
)
7442 found
= xref_tag_from_type (TREE_TYPE (gen_tmpl
),
7443 DECL_NAME (gen_tmpl
),
7444 /*tag_scope=*/ts_global
);
7448 context
= tsubst (DECL_CONTEXT (gen_tmpl
), arglist
,
7450 if (context
== error_mark_node
)
7451 return error_mark_node
;
7454 context
= global_namespace
;
7456 /* Create the type. */
7457 if (TREE_CODE (template_type
) == ENUMERAL_TYPE
)
7459 if (!is_dependent_type
)
7461 set_current_access_from_decl (TYPE_NAME (template_type
));
7462 t
= start_enum (TYPE_IDENTIFIER (template_type
), NULL_TREE
,
7463 tsubst (ENUM_UNDERLYING_TYPE (template_type
),
7464 arglist
, complain
, in_decl
),
7465 SCOPED_ENUM_P (template_type
), NULL
);
7469 /* We don't want to call start_enum for this type, since
7470 the values for the enumeration constants may involve
7471 template parameters. And, no one should be interested
7472 in the enumeration constants for such a type. */
7473 t
= cxx_make_type (ENUMERAL_TYPE
);
7474 SET_SCOPED_ENUM_P (t
, SCOPED_ENUM_P (template_type
));
7476 SET_OPAQUE_ENUM_P (t
, OPAQUE_ENUM_P (template_type
));
7477 ENUM_FIXED_UNDERLYING_TYPE_P (t
)
7478 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type
);
7480 else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl
))
7482 /* The user referred to a specialization of an alias
7483 template represented by GEN_TMPL.
7485 [temp.alias]/2 says:
7487 When a template-id refers to the specialization of an
7488 alias template, it is equivalent to the associated
7489 type obtained by substitution of its
7490 template-arguments for the template-parameters in the
7491 type-id of the alias template. */
7493 t
= tsubst (TREE_TYPE (gen_tmpl
), arglist
, complain
, in_decl
);
7494 /* Note that the call above (by indirectly calling
7495 register_specialization in tsubst_decl) registers the
7496 TYPE_DECL representing the specialization of the alias
7497 template. So next time someone substitutes ARGLIST for
7498 the template parms into the alias template (GEN_TMPL),
7499 she'll get that TYPE_DECL back. */
7501 if (t
== error_mark_node
)
7504 else if (CLASS_TYPE_P (template_type
))
7506 t
= make_class_type (TREE_CODE (template_type
));
7507 CLASSTYPE_DECLARED_CLASS (t
)
7508 = CLASSTYPE_DECLARED_CLASS (template_type
);
7509 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
7510 TYPE_FOR_JAVA (t
) = TYPE_FOR_JAVA (template_type
);
7512 /* A local class. Make sure the decl gets registered properly. */
7513 if (context
== current_function_decl
)
7514 pushtag (DECL_NAME (gen_tmpl
), t
, /*tag_scope=*/ts_current
);
7516 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type
), arglist
))
7517 /* This instantiation is another name for the primary
7518 template type. Set the TYPE_CANONICAL field
7520 TYPE_CANONICAL (t
) = template_type
;
7521 else if (any_template_arguments_need_structural_equality_p (arglist
))
7522 /* Some of the template arguments require structural
7523 equality testing, so this template class requires
7524 structural equality testing. */
7525 SET_TYPE_STRUCTURAL_EQUALITY (t
);
7530 /* If we called start_enum or pushtag above, this information
7531 will already be set up. */
7534 TYPE_CONTEXT (t
) = FROB_CONTEXT (context
);
7536 type_decl
= create_implicit_typedef (DECL_NAME (gen_tmpl
), t
);
7537 DECL_CONTEXT (type_decl
) = TYPE_CONTEXT (t
);
7538 DECL_SOURCE_LOCATION (type_decl
)
7539 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type
));
7542 type_decl
= TYPE_NAME (t
);
7544 if (CLASS_TYPE_P (template_type
))
7546 TREE_PRIVATE (type_decl
)
7547 = TREE_PRIVATE (TYPE_STUB_DECL (template_type
));
7548 TREE_PROTECTED (type_decl
)
7549 = TREE_PROTECTED (TYPE_STUB_DECL (template_type
));
7550 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type
))
7552 DECL_VISIBILITY_SPECIFIED (type_decl
) = 1;
7553 DECL_VISIBILITY (type_decl
) = CLASSTYPE_VISIBILITY (template_type
);
7557 /* Let's consider the explicit specialization of a member
7558 of a class template specialization that is implicitely instantiated,
7563 template<class U> struct M {}; //#0
7568 struct S<int>::M<char> //#1
7572 [temp.expl.spec]/4 says this is valid.
7574 In this case, when we write:
7577 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
7580 When we encounter #1, we want to store the partial instantiation
7581 of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
7583 For all cases other than this "explicit specialization of member of a
7584 class template", we just want to store the most general template into
7585 the CLASSTYPE_TI_TEMPLATE of M.
7587 This case of "explicit specialization of member of a class template"
7589 1/ the enclosing class is an instantiation of, and therefore not
7590 the same as, the context of the most general template, and
7591 2/ we aren't looking at the partial instantiation itself, i.e.
7592 the innermost arguments are not the same as the innermost parms of
7593 the most general template.
7595 So it's only when 1/ and 2/ happens that we want to use the partial
7596 instantiation of the member template in lieu of its most general
7599 if (PRIMARY_TEMPLATE_P (gen_tmpl
)
7600 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist
)
7601 /* the enclosing class must be an instantiation... */
7602 && CLASS_TYPE_P (context
)
7603 && !same_type_p (context
, DECL_CONTEXT (gen_tmpl
)))
7605 tree partial_inst_args
;
7606 TREE_VEC_LENGTH (arglist
)--;
7607 ++processing_template_decl
;
7609 tsubst (INNERMOST_TEMPLATE_ARGS
7610 (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl
))),
7611 arglist
, complain
, NULL_TREE
);
7612 --processing_template_decl
;
7613 TREE_VEC_LENGTH (arglist
)++;
7614 use_partial_inst_tmpl
=
7615 /*...and we must not be looking at the partial instantiation
7617 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist
),
7621 if (!use_partial_inst_tmpl
)
7622 /* This case is easy; there are no member templates involved. */
7626 /* This is a full instantiation of a member template. Find
7627 the partial instantiation of which this is an instance. */
7629 /* Temporarily reduce by one the number of levels in the ARGLIST
7630 so as to avoid comparing the last set of arguments. */
7631 TREE_VEC_LENGTH (arglist
)--;
7632 found
= tsubst (gen_tmpl
, arglist
, complain
, NULL_TREE
);
7633 TREE_VEC_LENGTH (arglist
)++;
7634 /* FOUND is either a proper class type, or an alias
7635 template specialization. In the later case, it's a
7636 TYPE_DECL, resulting from the substituting of arguments
7637 for parameters in the TYPE_DECL of the alias template
7638 done earlier. So be careful while getting the template
7640 found
= TREE_CODE (found
) == TYPE_DECL
7641 ? TYPE_TI_TEMPLATE (TREE_TYPE (found
))
7642 : CLASSTYPE_TI_TEMPLATE (found
);
7645 SET_TYPE_TEMPLATE_INFO (t
, build_template_info (found
, arglist
));
7648 slot
= htab_find_slot_with_hash (type_specializations
,
7649 &elt
, hash
, INSERT
);
7650 entry
= ggc_alloc_spec_entry ();
7654 /* Note this use of the partial instantiation so we can check it
7655 later in maybe_process_partial_specialization. */
7656 DECL_TEMPLATE_INSTANTIATIONS (templ
)
7657 = tree_cons (arglist
, t
,
7658 DECL_TEMPLATE_INSTANTIATIONS (templ
));
7660 if (TREE_CODE (t
) == ENUMERAL_TYPE
&& !is_dependent_type
)
7661 /* Now that the type has been registered on the instantiations
7662 list, we set up the enumerators. Because the enumeration
7663 constants may involve the enumeration type itself, we make
7664 sure to register the type first, and then create the
7665 constants. That way, doing tsubst_expr for the enumeration
7666 constants won't result in recursive calls here; we'll find
7667 the instantiation and exit above. */
7668 tsubst_enum (template_type
, t
, arglist
);
7670 if (CLASS_TYPE_P (template_type
) && is_dependent_type
)
7671 /* If the type makes use of template parameters, the
7672 code that generates debugging information will crash. */
7673 DECL_IGNORED_P (TYPE_STUB_DECL (t
)) = 1;
7675 /* Possibly limit visibility based on template args. */
7676 TREE_PUBLIC (type_decl
) = 1;
7677 determine_visibility (type_decl
);
7683 /* Wrapper for lookup_template_class_1. */
7686 lookup_template_class (tree d1
, tree arglist
, tree in_decl
, tree context
,
7687 int entering_scope
, tsubst_flags_t complain
)
7690 timevar_push (TV_TEMPLATE_INST
);
7691 ret
= lookup_template_class_1 (d1
, arglist
, in_decl
, context
,
7692 entering_scope
, complain
);
7693 timevar_pop (TV_TEMPLATE_INST
);
7701 /* True when we should also visit template parameters that occur in
7702 non-deduced contexts. */
7703 bool include_nondeduced_p
;
7704 struct pointer_set_t
*visited
;
7707 /* Called from for_each_template_parm via walk_tree. */
7710 for_each_template_parm_r (tree
*tp
, int *walk_subtrees
, void *d
)
7713 struct pair_fn_data
*pfd
= (struct pair_fn_data
*) d
;
7714 tree_fn_t fn
= pfd
->fn
;
7715 void *data
= pfd
->data
;
7718 && (pfd
->include_nondeduced_p
|| TREE_CODE (t
) != TYPENAME_TYPE
)
7719 && for_each_template_parm (TYPE_CONTEXT (t
), fn
, data
, pfd
->visited
,
7720 pfd
->include_nondeduced_p
))
7721 return error_mark_node
;
7723 switch (TREE_CODE (t
))
7726 if (TYPE_PTRMEMFUNC_P (t
))
7732 if (!TYPE_TEMPLATE_INFO (t
))
7734 else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t
)),
7735 fn
, data
, pfd
->visited
,
7736 pfd
->include_nondeduced_p
))
7737 return error_mark_node
;
7741 if (for_each_template_parm (TYPE_MIN_VALUE (t
),
7742 fn
, data
, pfd
->visited
,
7743 pfd
->include_nondeduced_p
)
7744 || for_each_template_parm (TYPE_MAX_VALUE (t
),
7745 fn
, data
, pfd
->visited
,
7746 pfd
->include_nondeduced_p
))
7747 return error_mark_node
;
7751 /* Since we're not going to walk subtrees, we have to do this
7753 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
,
7754 pfd
->visited
, pfd
->include_nondeduced_p
))
7755 return error_mark_node
;
7759 /* Check the return type. */
7760 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7761 pfd
->include_nondeduced_p
))
7762 return error_mark_node
;
7764 /* Check the parameter types. Since default arguments are not
7765 instantiated until they are needed, the TYPE_ARG_TYPES may
7766 contain expressions that involve template parameters. But,
7767 no-one should be looking at them yet. And, once they're
7768 instantiated, they don't contain template parameters, so
7769 there's no point in looking at them then, either. */
7773 for (parm
= TYPE_ARG_TYPES (t
); parm
; parm
= TREE_CHAIN (parm
))
7774 if (for_each_template_parm (TREE_VALUE (parm
), fn
, data
,
7775 pfd
->visited
, pfd
->include_nondeduced_p
))
7776 return error_mark_node
;
7778 /* Since we've already handled the TYPE_ARG_TYPES, we don't
7779 want walk_tree walking into them itself. */
7785 case UNDERLYING_TYPE
:
7786 if (pfd
->include_nondeduced_p
7787 && for_each_template_parm (TYPE_FIELDS (t
), fn
, data
,
7789 pfd
->include_nondeduced_p
))
7790 return error_mark_node
;
7795 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
7796 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
,
7797 pfd
->visited
, pfd
->include_nondeduced_p
))
7798 return error_mark_node
;
7803 if (TREE_CODE (t
) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (t
)
7804 && for_each_template_parm (DECL_INITIAL (t
), fn
, data
,
7805 pfd
->visited
, pfd
->include_nondeduced_p
))
7806 return error_mark_node
;
7807 if (DECL_CONTEXT (t
)
7808 && pfd
->include_nondeduced_p
7809 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
,
7810 pfd
->visited
, pfd
->include_nondeduced_p
))
7811 return error_mark_node
;
7814 case BOUND_TEMPLATE_TEMPLATE_PARM
:
7815 /* Record template parameters such as `T' inside `TT<T>'. */
7816 if (for_each_template_parm (TYPE_TI_ARGS (t
), fn
, data
, pfd
->visited
,
7817 pfd
->include_nondeduced_p
))
7818 return error_mark_node
;
7821 case TEMPLATE_TEMPLATE_PARM
:
7822 case TEMPLATE_TYPE_PARM
:
7823 case TEMPLATE_PARM_INDEX
:
7824 if (fn
&& (*fn
)(t
, data
))
7825 return error_mark_node
;
7827 return error_mark_node
;
7831 /* A template template parameter is encountered. */
7832 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
)
7833 && for_each_template_parm (TREE_TYPE (t
), fn
, data
, pfd
->visited
,
7834 pfd
->include_nondeduced_p
))
7835 return error_mark_node
;
7837 /* Already substituted template template parameter */
7843 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t
), fn
,
7845 pfd
->include_nondeduced_p
))
7846 return error_mark_node
;
7850 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
))
7851 && pfd
->include_nondeduced_p
7852 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
7853 (TREE_TYPE (t
)), fn
, data
,
7854 pfd
->visited
, pfd
->include_nondeduced_p
))
7855 return error_mark_node
;
7860 /* If there's no type, then this thing must be some expression
7861 involving template parameters. */
7862 if (!fn
&& !TREE_TYPE (t
))
7863 return error_mark_node
;
7868 case IMPLICIT_CONV_EXPR
:
7869 case REINTERPRET_CAST_EXPR
:
7870 case CONST_CAST_EXPR
:
7871 case STATIC_CAST_EXPR
:
7872 case DYNAMIC_CAST_EXPR
:
7876 case PSEUDO_DTOR_EXPR
:
7878 return error_mark_node
;
7885 /* We didn't find any template parameters we liked. */
7889 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
7890 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
7891 call FN with the parameter and the DATA.
7892 If FN returns nonzero, the iteration is terminated, and
7893 for_each_template_parm returns 1. Otherwise, the iteration
7894 continues. If FN never returns a nonzero value, the value
7895 returned by for_each_template_parm is 0. If FN is NULL, it is
7896 considered to be the function which always returns 1.
7898 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
7899 parameters that occur in non-deduced contexts. When false, only
7900 visits those template parameters that can be deduced. */
7903 for_each_template_parm (tree t
, tree_fn_t fn
, void* data
,
7904 struct pointer_set_t
*visited
,
7905 bool include_nondeduced_p
)
7907 struct pair_fn_data pfd
;
7913 pfd
.include_nondeduced_p
= include_nondeduced_p
;
7915 /* Walk the tree. (Conceptually, we would like to walk without
7916 duplicates, but for_each_template_parm_r recursively calls
7917 for_each_template_parm, so we would need to reorganize a fair
7918 bit to use walk_tree_without_duplicates, so we keep our own
7921 pfd
.visited
= visited
;
7923 pfd
.visited
= pointer_set_create ();
7924 result
= cp_walk_tree (&t
,
7925 for_each_template_parm_r
,
7927 pfd
.visited
) != NULL_TREE
;
7932 pointer_set_destroy (pfd
.visited
);
7939 /* Returns true if T depends on any template parameter. */
7942 uses_template_parms (tree t
)
7945 int saved_processing_template_decl
;
7947 saved_processing_template_decl
= processing_template_decl
;
7948 if (!saved_processing_template_decl
)
7949 processing_template_decl
= 1;
7951 dependent_p
= dependent_type_p (t
);
7952 else if (TREE_CODE (t
) == TREE_VEC
)
7953 dependent_p
= any_dependent_template_arguments_p (t
);
7954 else if (TREE_CODE (t
) == TREE_LIST
)
7955 dependent_p
= (uses_template_parms (TREE_VALUE (t
))
7956 || uses_template_parms (TREE_CHAIN (t
)));
7957 else if (TREE_CODE (t
) == TYPE_DECL
)
7958 dependent_p
= dependent_type_p (TREE_TYPE (t
));
7961 || TREE_CODE (t
) == TEMPLATE_PARM_INDEX
7962 || TREE_CODE (t
) == OVERLOAD
7964 || TREE_CODE (t
) == IDENTIFIER_NODE
7965 || TREE_CODE (t
) == TRAIT_EXPR
7966 || TREE_CODE (t
) == CONSTRUCTOR
7967 || CONSTANT_CLASS_P (t
))
7968 dependent_p
= (type_dependent_expression_p (t
)
7969 || value_dependent_expression_p (t
));
7972 gcc_assert (t
== error_mark_node
);
7973 dependent_p
= false;
7976 processing_template_decl
= saved_processing_template_decl
;
7981 /* Returns true if T depends on any template parameter with level LEVEL. */
7984 uses_template_parms_level (tree t
, int level
)
7986 return for_each_template_parm (t
, template_parm_this_level_p
, &level
, NULL
,
7987 /*include_nondeduced_p=*/true);
7990 /* Returns TRUE iff INST is an instantiation we don't need to do in an
7991 ill-formed translation unit, i.e. a variable or function that isn't
7992 usable in a constant expression. */
7995 neglectable_inst_p (tree d
)
7998 && !(TREE_CODE (d
) == FUNCTION_DECL
? DECL_DECLARED_CONSTEXPR_P (d
)
7999 : decl_maybe_constant_var_p (d
)));
8002 /* Returns TRUE iff we should refuse to instantiate DECL because it's
8003 neglectable and instantiated from within an erroneous instantiation. */
8006 limit_bad_template_recursion (tree decl
)
8008 struct tinst_level
*lev
= current_tinst_level
;
8009 int errs
= errorcount
+ sorrycount
;
8010 if (lev
== NULL
|| errs
== 0 || !neglectable_inst_p (decl
))
8013 for (; lev
; lev
= lev
->next
)
8014 if (neglectable_inst_p (lev
->decl
))
8017 return (lev
&& errs
> lev
->errors
);
8020 static int tinst_depth
;
8021 extern int max_tinst_depth
;
8022 #ifdef GATHER_STATISTICS
8025 static GTY(()) struct tinst_level
*last_error_tinst_level
;
8027 /* We're starting to instantiate D; record the template instantiation context
8028 for diagnostics and to restore it later. */
8031 push_tinst_level (tree d
)
8033 struct tinst_level
*new_level
;
8035 if (tinst_depth
>= max_tinst_depth
)
8037 last_error_tinst_level
= current_tinst_level
;
8038 if (TREE_CODE (d
) == TREE_LIST
)
8039 error ("template instantiation depth exceeds maximum of %d (use "
8040 "-ftemplate-depth= to increase the maximum) substituting %qS",
8041 max_tinst_depth
, d
);
8043 error ("template instantiation depth exceeds maximum of %d (use "
8044 "-ftemplate-depth= to increase the maximum) instantiating %qD",
8045 max_tinst_depth
, d
);
8047 print_instantiation_context ();
8052 /* If the current instantiation caused problems, don't let it instantiate
8053 anything else. Do allow deduction substitution and decls usable in
8054 constant expressions. */
8055 if (limit_bad_template_recursion (d
))
8058 new_level
= ggc_alloc_tinst_level ();
8059 new_level
->decl
= d
;
8060 new_level
->locus
= input_location
;
8061 new_level
->errors
= errorcount
+sorrycount
;
8062 new_level
->in_system_header_p
= in_system_header
;
8063 new_level
->next
= current_tinst_level
;
8064 current_tinst_level
= new_level
;
8067 #ifdef GATHER_STATISTICS
8068 if (tinst_depth
> depth_reached
)
8069 depth_reached
= tinst_depth
;
8075 /* We're done instantiating this template; return to the instantiation
8079 pop_tinst_level (void)
8081 /* Restore the filename and line number stashed away when we started
8082 this instantiation. */
8083 input_location
= current_tinst_level
->locus
;
8084 current_tinst_level
= current_tinst_level
->next
;
8088 /* We're instantiating a deferred template; restore the template
8089 instantiation context in which the instantiation was requested, which
8090 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
8093 reopen_tinst_level (struct tinst_level
*level
)
8095 struct tinst_level
*t
;
8098 for (t
= level
; t
; t
= t
->next
)
8101 current_tinst_level
= level
;
8103 if (current_tinst_level
)
8104 current_tinst_level
->errors
= errorcount
+sorrycount
;
8108 /* Returns the TINST_LEVEL which gives the original instantiation
8111 struct tinst_level
*
8112 outermost_tinst_level (void)
8114 struct tinst_level
*level
= current_tinst_level
;
8117 level
= level
->next
;
8121 /* Returns TRUE if PARM is a parameter of the template TEMPL. */
8124 parameter_of_template_p (tree parm
, tree templ
)
8129 if (!parm
|| !templ
)
8132 gcc_assert (DECL_TEMPLATE_PARM_P (parm
));
8133 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8135 parms
= DECL_TEMPLATE_PARMS (templ
);
8136 parms
= INNERMOST_TEMPLATE_PARMS (parms
);
8138 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
8140 tree p
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
8141 if (p
== error_mark_node
)
8145 || (DECL_INITIAL (parm
)
8146 && DECL_INITIAL (parm
) == DECL_INITIAL (p
)))
8153 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
8154 vector of template arguments, as for tsubst.
8156 Returns an appropriate tsubst'd friend declaration. */
8159 tsubst_friend_function (tree decl
, tree args
)
8163 if (TREE_CODE (decl
) == FUNCTION_DECL
8164 && DECL_TEMPLATE_INSTANTIATION (decl
)
8165 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
8166 /* This was a friend declared with an explicit template
8167 argument list, e.g.:
8171 to indicate that f was a template instantiation, not a new
8172 function declaration. Now, we have to figure out what
8173 instantiation of what template. */
8175 tree template_id
, arglist
, fns
;
8178 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (current_class_type
));
8180 /* Friend functions are looked up in the containing namespace scope.
8181 We must enter that scope, to avoid finding member functions of the
8182 current class with same name. */
8183 push_nested_namespace (ns
);
8184 fns
= tsubst_expr (DECL_TI_TEMPLATE (decl
), args
,
8185 tf_warning_or_error
, NULL_TREE
,
8186 /*integral_constant_expression_p=*/false);
8187 pop_nested_namespace (ns
);
8188 arglist
= tsubst (DECL_TI_ARGS (decl
), args
,
8189 tf_warning_or_error
, NULL_TREE
);
8190 template_id
= lookup_template_function (fns
, arglist
);
8192 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8193 tmpl
= determine_specialization (template_id
, new_friend
,
8195 /*need_member_template=*/0,
8196 TREE_VEC_LENGTH (args
),
8198 return instantiate_template (tmpl
, new_args
, tf_error
);
8201 new_friend
= tsubst (decl
, args
, tf_warning_or_error
, NULL_TREE
);
8203 /* The NEW_FRIEND will look like an instantiation, to the
8204 compiler, but is not an instantiation from the point of view of
8205 the language. For example, we might have had:
8207 template <class T> struct S {
8208 template <class U> friend void f(T, U);
8211 Then, in S<int>, template <class U> void f(int, U) is not an
8212 instantiation of anything. */
8213 if (new_friend
== error_mark_node
)
8214 return error_mark_node
;
8216 DECL_USE_TEMPLATE (new_friend
) = 0;
8217 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
8219 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
8220 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend
))
8221 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl
));
8224 /* The mangled name for the NEW_FRIEND is incorrect. The function
8225 is not a template instantiation and should not be mangled like
8226 one. Therefore, we forget the mangling here; we'll recompute it
8227 later if we need it. */
8228 if (TREE_CODE (new_friend
) != TEMPLATE_DECL
)
8230 SET_DECL_RTL (new_friend
, NULL
);
8231 SET_DECL_ASSEMBLER_NAME (new_friend
, NULL_TREE
);
8234 if (DECL_NAMESPACE_SCOPE_P (new_friend
))
8237 tree new_friend_template_info
;
8238 tree new_friend_result_template_info
;
8240 int new_friend_is_defn
;
8242 /* We must save some information from NEW_FRIEND before calling
8243 duplicate decls since that function will free NEW_FRIEND if
8245 new_friend_template_info
= DECL_TEMPLATE_INFO (new_friend
);
8246 new_friend_is_defn
=
8247 (DECL_INITIAL (DECL_TEMPLATE_RESULT
8248 (template_for_substitution (new_friend
)))
8250 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
8252 /* This declaration is a `primary' template. */
8253 DECL_PRIMARY_TEMPLATE (new_friend
) = new_friend
;
8255 new_friend_result_template_info
8256 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend
));
8259 new_friend_result_template_info
= NULL_TREE
;
8261 /* Make the init_value nonzero so pushdecl knows this is a defn. */
8262 if (new_friend_is_defn
)
8263 DECL_INITIAL (new_friend
) = error_mark_node
;
8265 /* Inside pushdecl_namespace_level, we will push into the
8266 current namespace. However, the friend function should go
8267 into the namespace of the template. */
8268 ns
= decl_namespace_context (new_friend
);
8269 push_nested_namespace (ns
);
8270 old_decl
= pushdecl_namespace_level (new_friend
, /*is_friend=*/true);
8271 pop_nested_namespace (ns
);
8273 if (old_decl
== error_mark_node
)
8274 return error_mark_node
;
8276 if (old_decl
!= new_friend
)
8278 /* This new friend declaration matched an existing
8279 declaration. For example, given:
8281 template <class T> void f(T);
8282 template <class U> class C {
8283 template <class T> friend void f(T) {}
8286 the friend declaration actually provides the definition
8287 of `f', once C has been instantiated for some type. So,
8288 old_decl will be the out-of-class template declaration,
8289 while new_friend is the in-class definition.
8291 But, if `f' was called before this point, the
8292 instantiation of `f' will have DECL_TI_ARGS corresponding
8293 to `T' but not to `U', references to which might appear
8294 in the definition of `f'. Previously, the most general
8295 template for an instantiation of `f' was the out-of-class
8296 version; now it is the in-class version. Therefore, we
8297 run through all specialization of `f', adding to their
8298 DECL_TI_ARGS appropriately. In particular, they need a
8299 new set of outer arguments, corresponding to the
8300 arguments for this class instantiation.
8302 The same situation can arise with something like this:
8305 template <class T> class C {
8309 when `C<int>' is instantiated. Now, `f(int)' is defined
8312 if (!new_friend_is_defn
)
8313 /* On the other hand, if the in-class declaration does
8314 *not* provide a definition, then we don't want to alter
8315 existing definitions. We can just leave everything
8320 tree new_template
= TI_TEMPLATE (new_friend_template_info
);
8321 tree new_args
= TI_ARGS (new_friend_template_info
);
8323 /* Overwrite whatever template info was there before, if
8324 any, with the new template information pertaining to
8326 DECL_TEMPLATE_INFO (old_decl
) = new_friend_template_info
;
8328 if (TREE_CODE (old_decl
) != TEMPLATE_DECL
)
8330 /* We should have called reregister_specialization in
8332 gcc_assert (retrieve_specialization (new_template
,
8336 /* Instantiate it if the global has already been used. */
8337 if (DECL_ODR_USED (old_decl
))
8338 instantiate_decl (old_decl
, /*defer_ok=*/true,
8339 /*expl_inst_class_mem_p=*/false);
8345 /* Indicate that the old function template is a partial
8347 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl
))
8348 = new_friend_result_template_info
;
8350 gcc_assert (new_template
8351 == most_general_template (new_template
));
8352 gcc_assert (new_template
!= old_decl
);
8354 /* Reassign any specializations already in the hash table
8355 to the new more general template, and add the
8356 additional template args. */
8357 for (t
= DECL_TEMPLATE_INSTANTIATIONS (old_decl
);
8361 tree spec
= TREE_VALUE (t
);
8364 elt
.tmpl
= old_decl
;
8365 elt
.args
= DECL_TI_ARGS (spec
);
8366 elt
.spec
= NULL_TREE
;
8368 htab_remove_elt (decl_specializations
, &elt
);
8371 = add_outermost_template_args (new_args
,
8372 DECL_TI_ARGS (spec
));
8374 register_specialization
8375 (spec
, new_template
, DECL_TI_ARGS (spec
), true, 0);
8378 DECL_TEMPLATE_INSTANTIATIONS (old_decl
) = NULL_TREE
;
8382 /* The information from NEW_FRIEND has been merged into OLD_DECL
8383 by duplicate_decls. */
8384 new_friend
= old_decl
;
8389 tree context
= DECL_CONTEXT (new_friend
);
8393 template <class T> class C {
8394 template <class U> friend void C1<U>::f (); // case 1
8395 friend void C2<T>::f (); // case 2
8397 we only need to make sure CONTEXT is a complete type for
8398 case 2. To distinguish between the two cases, we note that
8399 CONTEXT of case 1 remains dependent type after tsubst while
8400 this isn't true for case 2. */
8401 ++processing_template_decl
;
8402 dependent_p
= dependent_type_p (context
);
8403 --processing_template_decl
;
8406 && !complete_type_or_else (context
, NULL_TREE
))
8407 return error_mark_node
;
8409 if (COMPLETE_TYPE_P (context
))
8411 /* Check to see that the declaration is really present, and,
8412 possibly obtain an improved declaration. */
8413 tree fn
= check_classfn (context
,
8414 new_friend
, NULL_TREE
);
8424 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
8425 template arguments, as for tsubst.
8427 Returns an appropriate tsubst'd friend type or error_mark_node on
8431 tsubst_friend_class (tree friend_tmpl
, tree args
)
8437 context
= CP_DECL_CONTEXT (friend_tmpl
);
8439 if (context
!= global_namespace
)
8441 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8442 push_nested_namespace (context
);
8444 push_nested_class (tsubst (context
, args
, tf_none
, NULL_TREE
));
8447 /* Look for a class template declaration. We look for hidden names
8448 because two friend declarations of the same template are the
8449 same. For example, in:
8452 template <typename> friend class F;
8454 template <typename> struct B {
8455 template <typename> friend class F;
8458 both F templates are the same. */
8459 tmpl
= lookup_name_real (DECL_NAME (friend_tmpl
), 0, 0,
8460 /*block_p=*/true, 0,
8461 LOOKUP_COMPLAIN
| LOOKUP_HIDDEN
);
8463 /* But, if we don't find one, it might be because we're in a
8464 situation like this:
8472 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
8473 for `S<int>', not the TEMPLATE_DECL. */
8474 if (!tmpl
|| !DECL_CLASS_TEMPLATE_P (tmpl
))
8476 tmpl
= lookup_name_prefer_type (DECL_NAME (friend_tmpl
), 1);
8477 tmpl
= maybe_get_template_decl_from_type_decl (tmpl
);
8480 if (tmpl
&& DECL_CLASS_TEMPLATE_P (tmpl
))
8482 /* The friend template has already been declared. Just
8483 check to see that the declarations match, and install any new
8484 default parameters. We must tsubst the default parameters,
8485 of course. We only need the innermost template parameters
8486 because that is all that redeclare_class_template will look
8488 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl
))
8489 > TMPL_ARGS_DEPTH (args
))
8492 location_t saved_input_location
;
8493 parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl
),
8494 args
, tf_warning_or_error
);
8496 saved_input_location
= input_location
;
8497 input_location
= DECL_SOURCE_LOCATION (friend_tmpl
);
8498 redeclare_class_template (TREE_TYPE (tmpl
), parms
);
8499 input_location
= saved_input_location
;
8503 friend_type
= TREE_TYPE (tmpl
);
8507 /* The friend template has not already been declared. In this
8508 case, the instantiation of the template class will cause the
8509 injection of this template into the global scope. */
8510 tmpl
= tsubst (friend_tmpl
, args
, tf_warning_or_error
, NULL_TREE
);
8511 if (tmpl
== error_mark_node
)
8512 return error_mark_node
;
8514 /* The new TMPL is not an instantiation of anything, so we
8515 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
8516 the new type because that is supposed to be the corresponding
8517 template decl, i.e., TMPL. */
8518 DECL_USE_TEMPLATE (tmpl
) = 0;
8519 DECL_TEMPLATE_INFO (tmpl
) = NULL_TREE
;
8520 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl
)) = 0;
8521 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
))
8522 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl
)));
8524 /* Inject this template into the global scope. */
8525 friend_type
= TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl
, true));
8528 if (context
!= global_namespace
)
8530 if (TREE_CODE (context
) == NAMESPACE_DECL
)
8531 pop_nested_namespace (context
);
8533 pop_nested_class ();
8539 /* Returns zero if TYPE cannot be completed later due to circularity.
8540 Otherwise returns one. */
8543 can_complete_type_without_circularity (tree type
)
8545 if (type
== NULL_TREE
|| type
== error_mark_node
)
8547 else if (COMPLETE_TYPE_P (type
))
8549 else if (TREE_CODE (type
) == ARRAY_TYPE
&& TYPE_DOMAIN (type
))
8550 return can_complete_type_without_circularity (TREE_TYPE (type
));
8551 else if (CLASS_TYPE_P (type
)
8552 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type
)))
8558 /* Apply any attributes which had to be deferred until instantiation
8559 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
8560 ARGS, COMPLAIN, IN_DECL are as tsubst. */
8563 apply_late_template_attributes (tree
*decl_p
, tree attributes
, int attr_flags
,
8564 tree args
, tsubst_flags_t complain
, tree in_decl
)
8566 tree last_dep
= NULL_TREE
;
8570 for (t
= attributes
; t
; t
= TREE_CHAIN (t
))
8571 if (ATTR_IS_DEPENDENT (t
))
8574 attributes
= copy_list (attributes
);
8578 if (DECL_P (*decl_p
))
8580 if (TREE_TYPE (*decl_p
) == error_mark_node
)
8582 p
= &DECL_ATTRIBUTES (*decl_p
);
8585 p
= &TYPE_ATTRIBUTES (*decl_p
);
8589 tree late_attrs
= NULL_TREE
;
8590 tree
*q
= &late_attrs
;
8592 for (*p
= attributes
; *p
; )
8595 if (ATTR_IS_DEPENDENT (t
))
8597 *p
= TREE_CHAIN (t
);
8598 TREE_CHAIN (t
) = NULL_TREE
;
8599 /* If the first attribute argument is an identifier, don't
8600 pass it through tsubst. Attributes like mode, format,
8601 cleanup and several target specific attributes expect it
8604 && TREE_CODE (TREE_VALUE (t
)) == TREE_LIST
8605 && TREE_VALUE (TREE_VALUE (t
))
8606 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t
)))
8607 == IDENTIFIER_NODE
))
8610 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t
)), args
, complain
,
8612 /*integral_constant_expression_p=*/false);
8613 if (chain
!= TREE_CHAIN (TREE_VALUE (t
)))
8615 = tree_cons (NULL_TREE
, TREE_VALUE (TREE_VALUE (t
)),
8620 = tsubst_expr (TREE_VALUE (t
), args
, complain
, in_decl
,
8621 /*integral_constant_expression_p=*/false);
8623 q
= &TREE_CHAIN (t
);
8626 p
= &TREE_CHAIN (t
);
8629 cplus_decl_attributes (decl_p
, late_attrs
, attr_flags
);
8633 /* Perform (or defer) access check for typedefs that were referenced
8634 from within the template TMPL code.
8635 This is a subroutine of instantiate_template and instantiate_class_template.
8636 TMPL is the template to consider and TARGS is the list of arguments of
8640 perform_typedefs_access_check (tree tmpl
, tree targs
)
8642 location_t saved_location
;
8644 qualified_typedef_usage_t
*iter
;
8647 || (!CLASS_TYPE_P (tmpl
)
8648 && TREE_CODE (tmpl
) != FUNCTION_DECL
))
8651 saved_location
= input_location
;
8652 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
8653 get_types_needing_access_check (tmpl
),
8656 tree type_decl
= iter
->typedef_decl
;
8657 tree type_scope
= iter
->context
;
8659 if (!type_decl
|| !type_scope
|| !CLASS_TYPE_P (type_scope
))
8662 if (uses_template_parms (type_decl
))
8663 type_decl
= tsubst (type_decl
, targs
, tf_error
, NULL_TREE
);
8664 if (uses_template_parms (type_scope
))
8665 type_scope
= tsubst (type_scope
, targs
, tf_error
, NULL_TREE
);
8667 /* Make access check error messages point to the location
8668 of the use of the typedef. */
8669 input_location
= iter
->locus
;
8670 perform_or_defer_access_check (TYPE_BINFO (type_scope
),
8671 type_decl
, type_decl
);
8673 input_location
= saved_location
;
8677 instantiate_class_template_1 (tree type
)
8679 tree templ
, args
, pattern
, t
, member
;
8683 unsigned int saved_maximum_field_alignment
;
8685 if (type
== error_mark_node
)
8686 return error_mark_node
;
8688 if (COMPLETE_OR_OPEN_TYPE_P (type
)
8689 || uses_template_parms (type
))
8692 /* Figure out which template is being instantiated. */
8693 templ
= most_general_template (CLASSTYPE_TI_TEMPLATE (type
));
8694 gcc_assert (TREE_CODE (templ
) == TEMPLATE_DECL
);
8696 /* Determine what specialization of the original template to
8698 t
= most_specialized_class (type
, templ
, tf_warning_or_error
);
8699 if (t
== error_mark_node
)
8701 TYPE_BEING_DEFINED (type
) = 1;
8702 return error_mark_node
;
8706 /* This TYPE is actually an instantiation of a partial
8707 specialization. We replace the innermost set of ARGS with
8708 the arguments appropriate for substitution. For example,
8711 template <class T> struct S {};
8712 template <class T> struct S<T*> {};
8714 and supposing that we are instantiating S<int*>, ARGS will
8715 presently be {int*} -- but we need {int}. */
8716 pattern
= TREE_TYPE (t
);
8717 args
= TREE_PURPOSE (t
);
8721 pattern
= TREE_TYPE (templ
);
8722 args
= CLASSTYPE_TI_ARGS (type
);
8725 /* If the template we're instantiating is incomplete, then clearly
8726 there's nothing we can do. */
8727 if (!COMPLETE_TYPE_P (pattern
))
8730 /* If we've recursively instantiated too many templates, stop. */
8731 if (! push_tinst_level (type
))
8734 /* Now we're really doing the instantiation. Mark the type as in
8735 the process of being defined. */
8736 TYPE_BEING_DEFINED (type
) = 1;
8738 /* We may be in the middle of deferred access check. Disable
8740 push_deferring_access_checks (dk_no_deferred
);
8742 push_to_top_level ();
8743 /* Use #pragma pack from the template context. */
8744 saved_maximum_field_alignment
= maximum_field_alignment
;
8745 maximum_field_alignment
= TYPE_PRECISION (pattern
);
8747 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
8749 /* Set the input location to the most specialized template definition.
8750 This is needed if tsubsting causes an error. */
8751 typedecl
= TYPE_MAIN_DECL (pattern
);
8752 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (type
)) =
8753 DECL_SOURCE_LOCATION (typedecl
);
8755 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
8756 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
8757 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (pattern
);
8758 TYPE_FOR_JAVA (type
) = TYPE_FOR_JAVA (pattern
); /* For libjava's JArray<T> */
8759 if (ANON_AGGR_TYPE_P (pattern
))
8760 SET_ANON_AGGR_TYPE_P (type
);
8761 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern
))
8763 CLASSTYPE_VISIBILITY_SPECIFIED (type
) = 1;
8764 CLASSTYPE_VISIBILITY (type
) = CLASSTYPE_VISIBILITY (pattern
);
8765 /* Adjust visibility for template arguments. */
8766 determine_visibility (TYPE_MAIN_DECL (type
));
8768 CLASSTYPE_FINAL (type
) = CLASSTYPE_FINAL (pattern
);
8770 pbinfo
= TYPE_BINFO (pattern
);
8772 /* We should never instantiate a nested class before its enclosing
8773 class; we need to look up the nested class by name before we can
8774 instantiate it, and that lookup should instantiate the enclosing
8776 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern
))
8777 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type
)));
8779 base_list
= NULL_TREE
;
8780 if (BINFO_N_BASE_BINFOS (pbinfo
))
8786 /* We must enter the scope containing the type, as that is where
8787 the accessibility of types named in dependent bases are
8789 pushed_scope
= push_scope (CP_TYPE_CONTEXT (type
));
8791 /* Substitute into each of the bases to determine the actual
8793 for (i
= 0; BINFO_BASE_ITERATE (pbinfo
, i
, pbase_binfo
); i
++)
8796 tree access
= BINFO_BASE_ACCESS (pbinfo
, i
);
8797 tree expanded_bases
= NULL_TREE
;
8800 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo
)))
8803 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo
),
8804 args
, tf_error
, NULL_TREE
);
8805 if (expanded_bases
== error_mark_node
)
8808 len
= TREE_VEC_LENGTH (expanded_bases
);
8811 for (idx
= 0; idx
< len
; idx
++)
8814 /* Extract the already-expanded base class. */
8815 base
= TREE_VEC_ELT (expanded_bases
, idx
);
8817 /* Substitute to figure out the base class. */
8818 base
= tsubst (BINFO_TYPE (pbase_binfo
), args
, tf_error
,
8821 if (base
== error_mark_node
)
8824 base_list
= tree_cons (access
, base
, base_list
);
8825 if (BINFO_VIRTUAL_P (pbase_binfo
))
8826 TREE_TYPE (base_list
) = integer_type_node
;
8830 /* The list is now in reverse order; correct that. */
8831 base_list
= nreverse (base_list
);
8834 pop_scope (pushed_scope
);
8836 /* Now call xref_basetypes to set up all the base-class
8838 xref_basetypes (type
, base_list
);
8840 apply_late_template_attributes (&type
, TYPE_ATTRIBUTES (pattern
),
8841 (int) ATTR_FLAG_TYPE_IN_PLACE
,
8842 args
, tf_error
, NULL_TREE
);
8843 fixup_attribute_variants (type
);
8845 /* Now that our base classes are set up, enter the scope of the
8846 class, so that name lookups into base classes, etc. will work
8847 correctly. This is precisely analogous to what we do in
8848 begin_class_definition when defining an ordinary non-template
8849 class, except we also need to push the enclosing classes. */
8850 push_nested_class (type
);
8852 /* Now members are processed in the order of declaration. */
8853 for (member
= CLASSTYPE_DECL_LIST (pattern
);
8854 member
; member
= TREE_CHAIN (member
))
8856 tree t
= TREE_VALUE (member
);
8858 if (TREE_PURPOSE (member
))
8862 /* Build new CLASSTYPE_NESTED_UTDS. */
8865 bool class_template_p
;
8867 class_template_p
= (TREE_CODE (t
) != ENUMERAL_TYPE
8868 && TYPE_LANG_SPECIFIC (t
)
8869 && CLASSTYPE_IS_TEMPLATE (t
));
8870 /* If the member is a class template, then -- even after
8871 substitution -- there may be dependent types in the
8872 template argument list for the class. We increment
8873 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
8874 that function will assume that no types are dependent
8875 when outside of a template. */
8876 if (class_template_p
)
8877 ++processing_template_decl
;
8878 newtag
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8879 if (class_template_p
)
8880 --processing_template_decl
;
8881 if (newtag
== error_mark_node
)
8884 if (TREE_CODE (newtag
) != ENUMERAL_TYPE
)
8886 tree name
= TYPE_IDENTIFIER (t
);
8888 if (class_template_p
)
8889 /* Unfortunately, lookup_template_class sets
8890 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
8891 instantiation (i.e., for the type of a member
8892 template class nested within a template class.)
8893 This behavior is required for
8894 maybe_process_partial_specialization to work
8895 correctly, but is not accurate in this case;
8896 the TAG is not an instantiation of anything.
8897 (The corresponding TEMPLATE_DECL is an
8898 instantiation, but the TYPE is not.) */
8899 CLASSTYPE_USE_TEMPLATE (newtag
) = 0;
8901 /* Now, we call pushtag to put this NEWTAG into the scope of
8902 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
8903 pushtag calling push_template_decl. We don't have to do
8904 this for enums because it will already have been done in
8907 SET_IDENTIFIER_TYPE_VALUE (name
, newtag
);
8908 pushtag (name
, newtag
, /*tag_scope=*/ts_current
);
8911 else if (TREE_CODE (t
) == FUNCTION_DECL
8912 || DECL_FUNCTION_TEMPLATE_P (t
))
8914 /* Build new TYPE_METHODS. */
8917 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8918 ++processing_template_decl
;
8919 r
= tsubst (t
, args
, tf_error
, NULL_TREE
);
8920 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8921 --processing_template_decl
;
8922 set_current_access_from_decl (r
);
8923 finish_member_declaration (r
);
8924 /* Instantiate members marked with attribute used. */
8925 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8930 /* Build new TYPE_FIELDS. */
8931 if (TREE_CODE (t
) == STATIC_ASSERT
)
8934 tsubst_expr (STATIC_ASSERT_CONDITION (t
), args
,
8935 tf_warning_or_error
, NULL_TREE
,
8936 /*integral_constant_expression_p=*/true);
8937 finish_static_assert (condition
,
8938 STATIC_ASSERT_MESSAGE (t
),
8939 STATIC_ASSERT_SOURCE_LOCATION (t
),
8942 else if (TREE_CODE (t
) != CONST_DECL
)
8946 /* The file and line for this declaration, to
8947 assist in error message reporting. Since we
8948 called push_tinst_level above, we don't need to
8950 input_location
= DECL_SOURCE_LOCATION (t
);
8952 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8953 ++processing_template_decl
;
8954 r
= tsubst (t
, args
, tf_warning_or_error
, NULL_TREE
);
8955 if (TREE_CODE (t
) == TEMPLATE_DECL
)
8956 --processing_template_decl
;
8957 if (TREE_CODE (r
) == VAR_DECL
)
8961 [t]he initialization (and any associated
8962 side-effects) of a static data member does
8963 not occur unless the static data member is
8964 itself used in a way that requires the
8965 definition of the static data member to
8968 Therefore, we do not substitute into the
8969 initialized for the static data member here. */
8970 finish_static_data_member_decl
8973 /*init_const_expr_p=*/false,
8974 /*asmspec_tree=*/NULL_TREE
,
8976 /* Instantiate members marked with attribute used. */
8977 if (r
!= error_mark_node
&& DECL_PRESERVE_P (r
))
8980 else if (TREE_CODE (r
) == FIELD_DECL
)
8982 /* Determine whether R has a valid type and can be
8983 completed later. If R is invalid, then it is
8984 replaced by error_mark_node so that it will not be
8985 added to TYPE_FIELDS. */
8986 tree rtype
= TREE_TYPE (r
);
8987 if (can_complete_type_without_circularity (rtype
))
8988 complete_type (rtype
);
8990 if (!COMPLETE_TYPE_P (rtype
))
8992 cxx_incomplete_type_error (r
, rtype
);
8993 r
= error_mark_node
;
8997 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
8998 such a thing will already have been added to the field
8999 list by tsubst_enum in finish_member_declaration in the
9000 CLASSTYPE_NESTED_UTDS case above. */
9001 if (!(TREE_CODE (r
) == TYPE_DECL
9002 && TREE_CODE (TREE_TYPE (r
)) == ENUMERAL_TYPE
9003 && DECL_ARTIFICIAL (r
)))
9005 set_current_access_from_decl (r
);
9006 finish_member_declaration (r
);
9013 if (TYPE_P (t
) || DECL_CLASS_TEMPLATE_P (t
))
9015 /* Build new CLASSTYPE_FRIEND_CLASSES. */
9017 tree friend_type
= t
;
9018 bool adjust_processing_template_decl
= false;
9020 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9022 /* template <class T> friend class C; */
9023 friend_type
= tsubst_friend_class (friend_type
, args
);
9024 adjust_processing_template_decl
= true;
9026 else if (TREE_CODE (friend_type
) == UNBOUND_CLASS_TEMPLATE
)
9028 /* template <class T> friend class C::D; */
9029 friend_type
= tsubst (friend_type
, args
,
9030 tf_warning_or_error
, NULL_TREE
);
9031 if (TREE_CODE (friend_type
) == TEMPLATE_DECL
)
9032 friend_type
= TREE_TYPE (friend_type
);
9033 adjust_processing_template_decl
= true;
9035 else if (TREE_CODE (friend_type
) == TYPENAME_TYPE
9036 || TREE_CODE (friend_type
) == TEMPLATE_TYPE_PARM
)
9038 /* This could be either
9042 when dependent_type_p is false or
9044 template <class U> friend class T::C;
9047 friend_type
= tsubst (friend_type
, args
,
9048 tf_warning_or_error
, NULL_TREE
);
9049 /* Bump processing_template_decl for correct
9050 dependent_type_p calculation. */
9051 ++processing_template_decl
;
9052 if (dependent_type_p (friend_type
))
9053 adjust_processing_template_decl
= true;
9054 --processing_template_decl
;
9056 else if (!CLASSTYPE_USE_TEMPLATE (friend_type
)
9057 && hidden_name_p (TYPE_NAME (friend_type
)))
9061 where C hasn't been declared yet. Let's lookup name
9062 from namespace scope directly, bypassing any name that
9063 come from dependent base class. */
9064 tree ns
= decl_namespace_context (TYPE_MAIN_DECL (friend_type
));
9066 /* The call to xref_tag_from_type does injection for friend
9068 push_nested_namespace (ns
);
9070 xref_tag_from_type (friend_type
, NULL_TREE
,
9071 /*tag_scope=*/ts_current
);
9072 pop_nested_namespace (ns
);
9074 else if (uses_template_parms (friend_type
))
9075 /* friend class C<T>; */
9076 friend_type
= tsubst (friend_type
, args
,
9077 tf_warning_or_error
, NULL_TREE
);
9082 where C is already declared or
9084 friend class C<int>;
9086 We don't have to do anything in these cases. */
9088 if (adjust_processing_template_decl
)
9089 /* Trick make_friend_class into realizing that the friend
9090 we're adding is a template, not an ordinary class. It's
9091 important that we use make_friend_class since it will
9092 perform some error-checking and output cross-reference
9094 ++processing_template_decl
;
9096 if (friend_type
!= error_mark_node
)
9097 make_friend_class (type
, friend_type
, /*complain=*/false);
9099 if (adjust_processing_template_decl
)
9100 --processing_template_decl
;
9104 /* Build new DECL_FRIENDLIST. */
9107 /* The file and line for this declaration, to
9108 assist in error message reporting. Since we
9109 called push_tinst_level above, we don't need to
9111 input_location
= DECL_SOURCE_LOCATION (t
);
9113 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9115 ++processing_template_decl
;
9116 push_deferring_access_checks (dk_no_check
);
9119 r
= tsubst_friend_function (t
, args
);
9120 add_friend (type
, r
, /*complain=*/false);
9121 if (TREE_CODE (t
) == TEMPLATE_DECL
)
9123 pop_deferring_access_checks ();
9124 --processing_template_decl
;
9130 if (CLASSTYPE_LAMBDA_EXPR (type
))
9132 tree decl
= lambda_function (type
);
9135 tree lambda
= CLASSTYPE_LAMBDA_EXPR (type
);
9136 if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda
))
9138 apply_lambda_return_type (lambda
, void_type_node
);
9139 LAMBDA_EXPR_RETURN_TYPE (lambda
) = NULL_TREE
;
9141 instantiate_decl (decl
, false, false);
9142 maybe_add_lambda_conv_op (type
);
9145 gcc_assert (errorcount
);
9148 /* Set the file and line number information to whatever is given for
9149 the class itself. This puts error messages involving generated
9150 implicit functions at a predictable point, and the same point
9151 that would be used for non-template classes. */
9152 input_location
= DECL_SOURCE_LOCATION (typedecl
);
9154 unreverse_member_declarations (type
);
9155 finish_struct_1 (type
);
9156 TYPE_BEING_DEFINED (type
) = 0;
9158 /* We don't instantiate default arguments for member functions. 14.7.1:
9160 The implicit instantiation of a class template specialization causes
9161 the implicit instantiation of the declarations, but not of the
9162 definitions or default arguments, of the class member functions,
9163 member classes, static data members and member templates.... */
9165 /* Some typedefs referenced from within the template code need to be access
9166 checked at template instantiation time, i.e now. These types were
9167 added to the template at parsing time. Let's get those and perform
9168 the access checks then. */
9169 perform_typedefs_access_check (pattern
, args
);
9170 perform_deferred_access_checks ();
9171 pop_nested_class ();
9172 maximum_field_alignment
= saved_maximum_field_alignment
;
9173 pop_from_top_level ();
9174 pop_deferring_access_checks ();
9177 /* The vtable for a template class can be emitted in any translation
9178 unit in which the class is instantiated. When there is no key
9179 method, however, finish_struct_1 will already have added TYPE to
9180 the keyed_classes list. */
9181 if (TYPE_CONTAINS_VPTR_P (type
) && CLASSTYPE_KEY_METHOD (type
))
9182 keyed_classes
= tree_cons (NULL_TREE
, type
, keyed_classes
);
9187 /* Wrapper for instantiate_class_template_1. */
9190 instantiate_class_template (tree type
)
9193 timevar_push (TV_TEMPLATE_INST
);
9194 ret
= instantiate_class_template_1 (type
);
9195 timevar_pop (TV_TEMPLATE_INST
);
9200 tsubst_template_arg (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9206 else if (TYPE_P (t
))
9207 r
= tsubst (t
, args
, complain
, in_decl
);
9210 if (!(complain
& tf_warning
))
9211 ++c_inhibit_evaluation_warnings
;
9212 r
= tsubst_expr (t
, args
, complain
, in_decl
,
9213 /*integral_constant_expression_p=*/true);
9214 if (!(complain
& tf_warning
))
9215 --c_inhibit_evaluation_warnings
;
9216 /* Preserve the raw-reference nature of T. */
9217 if (TREE_TYPE (t
) && TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
9218 && REFERENCE_REF_P (r
))
9219 r
= TREE_OPERAND (r
, 0);
9224 /* Given a function parameter pack TMPL_PARM and some function parameters
9225 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
9226 and set *SPEC_P to point at the next point in the list. */
9229 extract_fnparm_pack (tree tmpl_parm
, tree
*spec_p
)
9231 /* Collect all of the extra "packed" parameters into an
9235 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
9236 tree argtypepack
= cxx_make_type (TYPE_ARGUMENT_PACK
);
9237 tree spec_parm
= *spec_p
;
9240 for (len
= 0; spec_parm
; ++len
, spec_parm
= TREE_CHAIN (spec_parm
))
9242 && !function_parameter_expanded_from_pack_p (spec_parm
, tmpl_parm
))
9245 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
9246 parmvec
= make_tree_vec (len
);
9247 parmtypevec
= make_tree_vec (len
);
9248 spec_parm
= *spec_p
;
9249 for (i
= 0; i
< len
; i
++, spec_parm
= DECL_CHAIN (spec_parm
))
9251 TREE_VEC_ELT (parmvec
, i
) = spec_parm
;
9252 TREE_VEC_ELT (parmtypevec
, i
) = TREE_TYPE (spec_parm
);
9255 /* Build the argument packs. */
9256 SET_ARGUMENT_PACK_ARGS (argpack
, parmvec
);
9257 SET_ARGUMENT_PACK_ARGS (argtypepack
, parmtypevec
);
9258 TREE_TYPE (argpack
) = argtypepack
;
9259 *spec_p
= spec_parm
;
9264 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
9265 NONTYPE_ARGUMENT_PACK. */
9268 make_fnparm_pack (tree spec_parm
)
9270 return extract_fnparm_pack (NULL_TREE
, &spec_parm
);
9273 /* Substitute ARGS into T, which is an pack expansion
9274 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
9275 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
9276 (if only a partial substitution could be performed) or
9277 ERROR_MARK_NODE if there was an error. */
9279 tsubst_pack_expansion (tree t
, tree args
, tsubst_flags_t complain
,
9283 tree pack
, packs
= NULL_TREE
;
9284 bool unsubstituted_packs
= false;
9285 bool real_packs
= false;
9286 int missing_level
= 0;
9289 struct pointer_map_t
*saved_local_specializations
= NULL
;
9290 bool need_local_specializations
= false;
9293 gcc_assert (PACK_EXPANSION_P (t
));
9294 pattern
= PACK_EXPANSION_PATTERN (t
);
9296 /* Add in any args remembered from an earlier partial instantiation. */
9297 args
= add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t
), args
);
9299 levels
= TMPL_ARGS_DEPTH (args
);
9301 /* Determine the argument packs that will instantiate the parameter
9302 packs used in the expansion expression. While we're at it,
9303 compute the number of arguments to be expanded and make sure it
9305 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (t
); pack
;
9306 pack
= TREE_CHAIN (pack
))
9308 tree parm_pack
= TREE_VALUE (pack
);
9309 tree arg_pack
= NULL_TREE
;
9310 tree orig_arg
= NULL_TREE
;
9313 if (TREE_CODE (parm_pack
) == BASES
)
9315 if (BASES_DIRECT (parm_pack
))
9316 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9317 args
, complain
, in_decl
, false));
9319 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack
),
9320 args
, complain
, in_decl
, false));
9322 if (TREE_CODE (parm_pack
) == PARM_DECL
)
9324 if (PACK_EXPANSION_LOCAL_P (t
))
9325 arg_pack
= retrieve_local_specialization (parm_pack
);
9328 /* We can't rely on local_specializations for a parameter
9329 name used later in a function declaration (such as in a
9330 late-specified return type). Even if it exists, it might
9331 have the wrong value for a recursive call. Just make a
9332 dummy decl, since it's only used for its type. */
9333 arg_pack
= tsubst_decl (parm_pack
, args
, complain
);
9334 if (arg_pack
&& FUNCTION_PARAMETER_PACK_P (arg_pack
))
9335 /* Partial instantiation of the parm_pack, we can't build
9336 up an argument pack yet. */
9337 arg_pack
= NULL_TREE
;
9339 arg_pack
= make_fnparm_pack (arg_pack
);
9340 need_local_specializations
= true;
9346 template_parm_level_and_index (parm_pack
, &level
, &idx
);
9348 if (level
<= levels
)
9349 arg_pack
= TMPL_ARG (args
, level
, idx
);
9352 orig_arg
= arg_pack
;
9353 if (arg_pack
&& TREE_CODE (arg_pack
) == ARGUMENT_PACK_SELECT
)
9354 arg_pack
= ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack
);
9356 if (arg_pack
&& !ARGUMENT_PACK_P (arg_pack
))
9357 /* This can only happen if we forget to expand an argument
9358 pack somewhere else. Just return an error, silently. */
9360 result
= make_tree_vec (1);
9361 TREE_VEC_ELT (result
, 0) = error_mark_node
;
9365 if (arg_from_parm_pack_p (arg_pack
, parm_pack
))
9366 /* The argument pack that the parameter maps to is just an
9367 expansion of the parameter itself, such as one would find
9368 in the implicit typedef of a class inside the class itself.
9369 Consider this parameter "unsubstituted", so that we will
9370 maintain the outer pack expansion. */
9371 arg_pack
= NULL_TREE
;
9376 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
));
9378 /* Don't bother trying to do a partial substitution with
9379 incomplete packs; we'll try again after deduction. */
9380 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack
))
9385 else if (len
!= my_len
)
9387 if (!(complain
& tf_error
))
9388 /* Fail quietly. */;
9389 else if (TREE_CODE (t
) == TYPE_PACK_EXPANSION
)
9390 error ("mismatched argument pack lengths while expanding "
9394 error ("mismatched argument pack lengths while expanding "
9397 return error_mark_node
;
9400 if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack
)) == 1
9401 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack
),
9403 /* This isn't a real argument pack yet. */;
9407 /* Keep track of the parameter packs and their corresponding
9409 packs
= tree_cons (parm_pack
, arg_pack
, packs
);
9410 TREE_TYPE (packs
) = orig_arg
;
9414 /* We can't substitute for this parameter pack. We use a flag as
9415 well as the missing_level counter because function parameter
9416 packs don't have a level. */
9417 unsubstituted_packs
= true;
9418 if (!missing_level
|| missing_level
> level
)
9419 missing_level
= level
;
9423 /* We cannot expand this expansion expression, because we don't have
9424 all of the argument packs we need. */
9425 if (unsubstituted_packs
)
9429 /* We got some full packs, but we can't substitute them in until we
9430 have values for all the packs. So remember these until then. */
9433 t
= make_pack_expansion (pattern
);
9435 /* The call to add_to_template_args above assumes no overlap
9436 between saved args and new args, so prune away any fake
9437 args, i.e. those that satisfied arg_from_parm_pack_p above. */
9438 if (missing_level
&& levels
>= missing_level
)
9440 gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
)
9441 && missing_level
> 1);
9442 TREE_VEC_LENGTH (args
) = missing_level
- 1;
9443 save_args
= copy_node (args
);
9444 TREE_VEC_LENGTH (args
) = levels
;
9449 PACK_EXPANSION_EXTRA_ARGS (t
) = save_args
;
9453 /* There were no real arguments, we're just replacing a parameter
9454 pack with another version of itself. Substitute into the
9455 pattern and return a PACK_EXPANSION_*. The caller will need to
9457 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9458 t
= tsubst_expr (pattern
, args
, complain
, in_decl
,
9459 /*integral_constant_expression_p=*/false);
9461 t
= tsubst (pattern
, args
, complain
, in_decl
);
9462 t
= make_pack_expansion (t
);
9467 /* We could not find any argument packs that work. */
9469 return error_mark_node
;
9471 if (need_local_specializations
)
9473 /* We're in a late-specified return type, so create our own local
9474 specializations map; the current map is either NULL or (in the
9475 case of recursive unification) might have bindings that we don't
9476 want to use or alter. */
9477 saved_local_specializations
= local_specializations
;
9478 local_specializations
= pointer_map_create ();
9481 /* For each argument in each argument pack, substitute into the
9483 result
= make_tree_vec (len
);
9484 for (i
= 0; i
< len
; ++i
)
9486 /* For parameter pack, change the substitution of the parameter
9487 pack to the ith argument in its argument pack, then expand
9489 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9491 tree parm
= TREE_PURPOSE (pack
);
9494 /* Select the Ith argument from the pack. */
9495 if (TREE_CODE (parm
) == PARM_DECL
)
9499 arg
= make_node (ARGUMENT_PACK_SELECT
);
9500 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9502 register_local_specialization (arg
, parm
);
9505 arg
= retrieve_local_specialization (parm
);
9510 template_parm_level_and_index (parm
, &level
, &idx
);
9514 arg
= make_node (ARGUMENT_PACK_SELECT
);
9515 ARGUMENT_PACK_SELECT_FROM_PACK (arg
) = TREE_VALUE (pack
);
9516 /* Update the corresponding argument. */
9517 TMPL_ARG (args
, level
, idx
) = arg
;
9520 /* Re-use the ARGUMENT_PACK_SELECT. */
9521 arg
= TMPL_ARG (args
, level
, idx
);
9523 ARGUMENT_PACK_SELECT_INDEX (arg
) = i
;
9526 /* Substitute into the PATTERN with the altered arguments. */
9527 if (TREE_CODE (t
) == EXPR_PACK_EXPANSION
)
9528 TREE_VEC_ELT (result
, i
) =
9529 tsubst_expr (pattern
, args
, complain
, in_decl
,
9530 /*integral_constant_expression_p=*/false);
9532 TREE_VEC_ELT (result
, i
) = tsubst (pattern
, args
, complain
, in_decl
);
9534 if (TREE_VEC_ELT (result
, i
) == error_mark_node
)
9536 result
= error_mark_node
;
9541 /* Update ARGS to restore the substitution from parameter packs to
9542 their argument packs. */
9543 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
9545 tree parm
= TREE_PURPOSE (pack
);
9547 if (TREE_CODE (parm
) == PARM_DECL
)
9548 register_local_specialization (TREE_TYPE (pack
), parm
);
9552 template_parm_level_and_index (parm
, &level
, &idx
);
9554 /* Update the corresponding argument. */
9555 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
9556 TREE_VEC_ELT (TREE_VEC_ELT (args
, level
-1 ), idx
) =
9559 TREE_VEC_ELT (args
, idx
) = TREE_TYPE (pack
);
9563 if (need_local_specializations
)
9565 pointer_map_destroy (local_specializations
);
9566 local_specializations
= saved_local_specializations
;
9572 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
9573 TMPL. We do this using DECL_PARM_INDEX, which should work even with
9574 parameter packs; all parms generated from a function parameter pack will
9575 have the same DECL_PARM_INDEX. */
9578 get_pattern_parm (tree parm
, tree tmpl
)
9580 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
9583 if (DECL_ARTIFICIAL (parm
))
9585 for (patparm
= DECL_ARGUMENTS (pattern
);
9586 patparm
; patparm
= DECL_CHAIN (patparm
))
9587 if (DECL_ARTIFICIAL (patparm
)
9588 && DECL_NAME (parm
) == DECL_NAME (patparm
))
9593 patparm
= FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl
));
9594 patparm
= chain_index (DECL_PARM_INDEX (parm
)-1, patparm
);
9595 gcc_assert (DECL_PARM_INDEX (patparm
)
9596 == DECL_PARM_INDEX (parm
));
9602 /* Substitute ARGS into the vector or list of template arguments T. */
9605 tsubst_template_args (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
9608 int len
, need_new
= 0, i
, expanded_len_adjust
= 0, out
;
9611 if (t
== error_mark_node
)
9612 return error_mark_node
;
9614 len
= TREE_VEC_LENGTH (t
);
9615 elts
= XALLOCAVEC (tree
, len
);
9617 for (i
= 0; i
< len
; i
++)
9619 tree orig_arg
= TREE_VEC_ELT (t
, i
);
9622 if (TREE_CODE (orig_arg
) == TREE_VEC
)
9623 new_arg
= tsubst_template_args (orig_arg
, args
, complain
, in_decl
);
9624 else if (PACK_EXPANSION_P (orig_arg
))
9626 /* Substitute into an expansion expression. */
9627 new_arg
= tsubst_pack_expansion (orig_arg
, args
, complain
, in_decl
);
9629 if (TREE_CODE (new_arg
) == TREE_VEC
)
9630 /* Add to the expanded length adjustment the number of
9631 expanded arguments. We subtract one from this
9632 measurement, because the argument pack expression
9633 itself is already counted as 1 in
9634 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
9635 the argument pack is empty. */
9636 expanded_len_adjust
+= TREE_VEC_LENGTH (new_arg
) - 1;
9638 else if (ARGUMENT_PACK_P (orig_arg
))
9640 /* Substitute into each of the arguments. */
9641 new_arg
= TYPE_P (orig_arg
)
9642 ? cxx_make_type (TREE_CODE (orig_arg
))
9643 : make_node (TREE_CODE (orig_arg
));
9645 SET_ARGUMENT_PACK_ARGS (
9647 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg
),
9648 args
, complain
, in_decl
));
9650 if (ARGUMENT_PACK_ARGS (new_arg
) == error_mark_node
)
9651 new_arg
= error_mark_node
;
9653 if (TREE_CODE (new_arg
) == NONTYPE_ARGUMENT_PACK
) {
9654 TREE_TYPE (new_arg
) = tsubst (TREE_TYPE (orig_arg
), args
,
9656 TREE_CONSTANT (new_arg
) = TREE_CONSTANT (orig_arg
);
9658 if (TREE_TYPE (new_arg
) == error_mark_node
)
9659 new_arg
= error_mark_node
;
9663 new_arg
= tsubst_template_arg (orig_arg
, args
, complain
, in_decl
);
9665 if (new_arg
== error_mark_node
)
9666 return error_mark_node
;
9669 if (new_arg
!= orig_arg
)
9676 /* Make space for the expanded arguments coming from template
9678 t
= make_tree_vec (len
+ expanded_len_adjust
);
9679 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
9680 arguments for a member template.
9681 In that case each TREE_VEC in ORIG_T represents a level of template
9682 arguments, and ORIG_T won't carry any non defaulted argument count.
9683 It will rather be the nested TREE_VECs that will carry one.
9684 In other words, ORIG_T carries a non defaulted argument count only
9685 if it doesn't contain any nested TREE_VEC. */
9686 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
))
9688 int count
= GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t
);
9689 count
+= expanded_len_adjust
;
9690 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t
, count
);
9692 for (i
= 0, out
= 0; i
< len
; i
++)
9694 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t
, i
))
9695 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t
, i
)))
9696 && TREE_CODE (elts
[i
]) == TREE_VEC
)
9700 /* Now expand the template argument pack "in place". */
9701 for (idx
= 0; idx
< TREE_VEC_LENGTH (elts
[i
]); idx
++, out
++)
9702 TREE_VEC_ELT (t
, out
) = TREE_VEC_ELT (elts
[i
], idx
);
9706 TREE_VEC_ELT (t
, out
) = elts
[i
];
9714 /* Return the result of substituting ARGS into the template parameters
9715 given by PARMS. If there are m levels of ARGS and m + n levels of
9716 PARMS, then the result will contain n levels of PARMS. For
9717 example, if PARMS is `template <class T> template <class U>
9718 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
9719 result will be `template <int*, double, class V>'. */
9722 tsubst_template_parms (tree parms
, tree args
, tsubst_flags_t complain
)
9727 /* When substituting into a template, we must set
9728 PROCESSING_TEMPLATE_DECL as the template parameters may be
9729 dependent if they are based on one-another, and the dependency
9730 predicates are short-circuit outside of templates. */
9731 ++processing_template_decl
;
9733 for (new_parms
= &r
;
9734 TMPL_PARMS_DEPTH (parms
) > TMPL_ARGS_DEPTH (args
);
9735 new_parms
= &(TREE_CHAIN (*new_parms
)),
9736 parms
= TREE_CHAIN (parms
))
9739 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
9742 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
9746 if (parms
== error_mark_node
)
9749 tuple
= TREE_VEC_ELT (TREE_VALUE (parms
), i
);
9751 if (tuple
== error_mark_node
)
9754 TREE_VEC_ELT (new_vec
, i
) =
9755 tsubst_template_parm (tuple
, args
, complain
);
9759 tree_cons (size_int (TMPL_PARMS_DEPTH (parms
)
9760 - TMPL_ARGS_DEPTH (args
)),
9761 new_vec
, NULL_TREE
);
9764 --processing_template_decl
;
9769 /* Return the result of substituting ARGS into one template parameter
9770 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
9771 parameter and which TREE_PURPOSE is the default argument of the
9772 template parameter. */
9775 tsubst_template_parm (tree t
, tree args
, tsubst_flags_t complain
)
9777 tree default_value
, parm_decl
;
9779 if (args
== NULL_TREE
9781 || t
== error_mark_node
)
9784 gcc_assert (TREE_CODE (t
) == TREE_LIST
);
9786 default_value
= TREE_PURPOSE (t
);
9787 parm_decl
= TREE_VALUE (t
);
9789 parm_decl
= tsubst (parm_decl
, args
, complain
, NULL_TREE
);
9790 if (TREE_CODE (parm_decl
) == PARM_DECL
9791 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl
), complain
))
9792 parm_decl
= error_mark_node
;
9793 default_value
= tsubst_template_arg (default_value
, args
,
9794 complain
, NULL_TREE
);
9796 return build_tree_list (default_value
, parm_decl
);
9799 /* Substitute the ARGS into the indicated aggregate (or enumeration)
9800 type T. If T is not an aggregate or enumeration type, it is
9801 handled as if by tsubst. IN_DECL is as for tsubst. If
9802 ENTERING_SCOPE is nonzero, T is the context for a template which
9803 we are presently tsubst'ing. Return the substituted value. */
9806 tsubst_aggr_type (tree t
,
9808 tsubst_flags_t complain
,
9815 switch (TREE_CODE (t
))
9818 if (TYPE_PTRMEMFUNC_P (t
))
9819 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, complain
, in_decl
);
9821 /* Else fall through. */
9824 if (TYPE_TEMPLATE_INFO (t
) && uses_template_parms (t
))
9829 int saved_unevaluated_operand
;
9830 int saved_inhibit_evaluation_warnings
;
9832 /* In "sizeof(X<I>)" we need to evaluate "I". */
9833 saved_unevaluated_operand
= cp_unevaluated_operand
;
9834 cp_unevaluated_operand
= 0;
9835 saved_inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
9836 c_inhibit_evaluation_warnings
= 0;
9838 /* First, determine the context for the type we are looking
9840 context
= TYPE_CONTEXT (t
);
9841 if (context
&& TYPE_P (context
))
9843 context
= tsubst_aggr_type (context
, args
, complain
,
9844 in_decl
, /*entering_scope=*/1);
9845 /* If context is a nested class inside a class template,
9846 it may still need to be instantiated (c++/33959). */
9847 context
= complete_type (context
);
9850 /* Then, figure out what arguments are appropriate for the
9851 type we are trying to find. For example, given:
9853 template <class T> struct S;
9854 template <class T, class U> void f(T, U) { S<U> su; }
9856 and supposing that we are instantiating f<int, double>,
9857 then our ARGS will be {int, double}, but, when looking up
9858 S we only want {double}. */
9859 argvec
= tsubst_template_args (TYPE_TI_ARGS (t
), args
,
9861 if (argvec
== error_mark_node
)
9862 r
= error_mark_node
;
9865 r
= lookup_template_class (t
, argvec
, in_decl
, context
,
9866 entering_scope
, complain
);
9867 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
9870 cp_unevaluated_operand
= saved_unevaluated_operand
;
9871 c_inhibit_evaluation_warnings
= saved_inhibit_evaluation_warnings
;
9876 /* This is not a template type, so there's nothing to do. */
9880 return tsubst (t
, args
, complain
, in_decl
);
9884 /* Substitute into the default argument ARG (a default argument for
9885 FN), which has the indicated TYPE. */
9888 tsubst_default_argument (tree fn
, tree type
, tree arg
)
9890 tree saved_class_ptr
= NULL_TREE
;
9891 tree saved_class_ref
= NULL_TREE
;
9893 /* This can happen in invalid code. */
9894 if (TREE_CODE (arg
) == DEFAULT_ARG
)
9897 /* This default argument came from a template. Instantiate the
9898 default argument here, not in tsubst. In the case of
9907 we must be careful to do name lookup in the scope of S<T>,
9908 rather than in the current class. */
9909 push_access_scope (fn
);
9910 /* The "this" pointer is not valid in a default argument. */
9913 saved_class_ptr
= current_class_ptr
;
9914 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
9915 saved_class_ref
= current_class_ref
;
9916 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
9919 push_deferring_access_checks(dk_no_deferred
);
9920 /* The default argument expression may cause implicitly defined
9921 member functions to be synthesized, which will result in garbage
9922 collection. We must treat this situation as if we were within
9923 the body of function so as to avoid collecting live data on the
9926 arg
= tsubst_expr (arg
, DECL_TI_ARGS (fn
),
9927 tf_warning_or_error
, NULL_TREE
,
9928 /*integral_constant_expression_p=*/false);
9930 pop_deferring_access_checks();
9932 /* Restore the "this" pointer. */
9935 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
9936 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
9939 /* Make sure the default argument is reasonable. */
9940 arg
= check_default_argument (type
, arg
);
9942 pop_access_scope (fn
);
9947 /* Substitute into all the default arguments for FN. */
9950 tsubst_default_arguments (tree fn
)
9955 tmpl_args
= DECL_TI_ARGS (fn
);
9957 /* If this function is not yet instantiated, we certainly don't need
9958 its default arguments. */
9959 if (uses_template_parms (tmpl_args
))
9961 /* Don't do this again for clones. */
9962 if (DECL_CLONED_FUNCTION_P (fn
))
9965 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
9967 arg
= TREE_CHAIN (arg
))
9968 if (TREE_PURPOSE (arg
))
9969 TREE_PURPOSE (arg
) = tsubst_default_argument (fn
,
9971 TREE_PURPOSE (arg
));
9974 /* Substitute the ARGS into the T, which is a _DECL. Return the
9975 result of the substitution. Issue error and warning messages under
9976 control of COMPLAIN. */
9979 tsubst_decl (tree t
, tree args
, tsubst_flags_t complain
)
9981 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
9982 location_t saved_loc
;
9987 /* Set the filename and linenumber to improve error-reporting. */
9988 saved_loc
= input_location
;
9989 input_location
= DECL_SOURCE_LOCATION (t
);
9991 switch (TREE_CODE (t
))
9995 /* We can get here when processing a member function template,
9996 member class template, or template template parameter. */
9997 tree decl
= DECL_TEMPLATE_RESULT (t
);
10002 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
10004 /* Template template parameter is treated here. */
10005 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10006 if (new_type
== error_mark_node
)
10007 RETURN (error_mark_node
);
10010 DECL_CHAIN (r
) = NULL_TREE
;
10011 TREE_TYPE (r
) = new_type
;
10012 DECL_TEMPLATE_RESULT (r
)
10013 = build_decl (DECL_SOURCE_LOCATION (decl
),
10014 TYPE_DECL
, DECL_NAME (decl
), new_type
);
10015 DECL_TEMPLATE_PARMS (r
)
10016 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10018 TYPE_NAME (new_type
) = r
;
10022 /* We might already have an instance of this template.
10023 The ARGS are for the surrounding class type, so the
10024 full args contain the tsubst'd args for the context,
10025 plus the innermost args from the template decl. */
10026 tmpl_args
= DECL_CLASS_TEMPLATE_P (t
)
10027 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t
))
10028 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t
));
10029 /* Because this is a template, the arguments will still be
10030 dependent, even after substitution. If
10031 PROCESSING_TEMPLATE_DECL is not set, the dependency
10032 predicates will short-circuit. */
10033 ++processing_template_decl
;
10034 full_args
= tsubst_template_args (tmpl_args
, args
,
10035 complain
, in_decl
);
10036 --processing_template_decl
;
10037 if (full_args
== error_mark_node
)
10038 RETURN (error_mark_node
);
10040 /* If this is a default template template argument,
10041 tsubst might not have changed anything. */
10042 if (full_args
== tmpl_args
)
10045 hash
= hash_tmpl_and_args (t
, full_args
);
10046 spec
= retrieve_specialization (t
, full_args
, hash
);
10047 if (spec
!= NULL_TREE
)
10053 /* Make a new template decl. It will be similar to the
10054 original, but will record the current template arguments.
10055 We also create a new function declaration, which is just
10056 like the old one, but points to this new template, rather
10057 than the old one. */
10059 gcc_assert (DECL_LANG_SPECIFIC (r
) != 0);
10060 DECL_CHAIN (r
) = NULL_TREE
;
10062 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10064 if (TREE_CODE (decl
) == TYPE_DECL
10065 && !TYPE_DECL_ALIAS_P (decl
))
10068 ++processing_template_decl
;
10069 new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10070 --processing_template_decl
;
10071 if (new_type
== error_mark_node
)
10072 RETURN (error_mark_node
);
10074 TREE_TYPE (r
) = new_type
;
10075 CLASSTYPE_TI_TEMPLATE (new_type
) = r
;
10076 DECL_TEMPLATE_RESULT (r
) = TYPE_MAIN_DECL (new_type
);
10077 DECL_TI_ARGS (r
) = CLASSTYPE_TI_ARGS (new_type
);
10078 DECL_CONTEXT (r
) = TYPE_CONTEXT (new_type
);
10083 ++processing_template_decl
;
10084 new_decl
= tsubst (decl
, args
, complain
, in_decl
);
10085 --processing_template_decl
;
10086 if (new_decl
== error_mark_node
)
10087 RETURN (error_mark_node
);
10089 DECL_TEMPLATE_RESULT (r
) = new_decl
;
10090 DECL_TI_TEMPLATE (new_decl
) = r
;
10091 TREE_TYPE (r
) = TREE_TYPE (new_decl
);
10092 DECL_TI_ARGS (r
) = DECL_TI_ARGS (new_decl
);
10093 DECL_CONTEXT (r
) = DECL_CONTEXT (new_decl
);
10096 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10097 DECL_TEMPLATE_INSTANTIATIONS (r
) = NULL_TREE
;
10098 DECL_TEMPLATE_SPECIALIZATIONS (r
) = NULL_TREE
;
10100 /* The template parameters for this new template are all the
10101 template parameters for the old template, except the
10102 outermost level of parameters. */
10103 DECL_TEMPLATE_PARMS (r
)
10104 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t
), args
,
10107 if (PRIMARY_TEMPLATE_P (t
))
10108 DECL_PRIMARY_TEMPLATE (r
) = r
;
10110 if (TREE_CODE (decl
) != TYPE_DECL
)
10111 /* Record this non-type partial instantiation. */
10112 register_specialization (r
, t
,
10113 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r
)),
10118 case FUNCTION_DECL
:
10121 tree argvec
= NULL_TREE
;
10129 /* Nobody should be tsubst'ing into non-template functions. */
10130 gcc_assert (DECL_TEMPLATE_INFO (t
) != NULL_TREE
);
10132 if (TREE_CODE (DECL_TI_TEMPLATE (t
)) == TEMPLATE_DECL
)
10137 /* If T is not dependent, just return it. We have to
10138 increment PROCESSING_TEMPLATE_DECL because
10139 value_dependent_expression_p assumes that nothing is
10140 dependent when PROCESSING_TEMPLATE_DECL is zero. */
10141 ++processing_template_decl
;
10142 dependent_p
= value_dependent_expression_p (t
);
10143 --processing_template_decl
;
10147 /* Calculate the most general template of which R is a
10148 specialization, and the complete set of arguments used to
10150 gen_tmpl
= most_general_template (DECL_TI_TEMPLATE (t
));
10151 argvec
= tsubst_template_args (DECL_TI_ARGS
10152 (DECL_TEMPLATE_RESULT
10153 (DECL_TI_TEMPLATE (t
))),
10154 args
, complain
, in_decl
);
10155 if (argvec
== error_mark_node
)
10156 RETURN (error_mark_node
);
10158 /* Check to see if we already have this specialization. */
10159 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10160 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10168 /* We can see more levels of arguments than parameters if
10169 there was a specialization of a member template, like
10172 template <class T> struct S { template <class U> void f(); }
10173 template <> template <class U> void S<int>::f(U);
10175 Here, we'll be substituting into the specialization,
10176 because that's where we can find the code we actually
10177 want to generate, but we'll have enough arguments for
10178 the most general template.
10180 We also deal with the peculiar case:
10182 template <class T> struct S {
10183 template <class U> friend void f();
10185 template <class U> void f() {}
10187 template void f<double>();
10189 Here, the ARGS for the instantiation of will be {int,
10190 double}. But, we only need as many ARGS as there are
10191 levels of template parameters in CODE_PATTERN. We are
10192 careful not to get fooled into reducing the ARGS in
10195 template <class T> struct S { template <class U> void f(U); }
10196 template <class T> template <> void S<T>::f(int) {}
10198 which we can spot because the pattern will be a
10199 specialization in this case. */
10200 args_depth
= TMPL_ARGS_DEPTH (args
);
10202 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t
)));
10203 if (args_depth
> parms_depth
10204 && !DECL_TEMPLATE_SPECIALIZATION (t
))
10205 args
= get_innermost_template_args (args
, parms_depth
);
10209 /* This special case arises when we have something like this:
10211 template <class T> struct S {
10212 friend void f<int>(int, double);
10215 Here, the DECL_TI_TEMPLATE for the friend declaration
10216 will be an IDENTIFIER_NODE. We are being called from
10217 tsubst_friend_function, and we want only to create a
10218 new decl (R) with appropriate types so that we can call
10219 determine_specialization. */
10220 gen_tmpl
= NULL_TREE
;
10223 if (DECL_CLASS_SCOPE_P (t
))
10225 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
10229 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
,
10230 complain
, t
, /*entering_scope=*/1);
10235 ctx
= DECL_CONTEXT (t
);
10237 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10238 if (type
== error_mark_node
)
10239 RETURN (error_mark_node
);
10241 /* We do NOT check for matching decls pushed separately at this
10242 point, as they may not represent instantiations of this
10243 template, and in any case are considered separate under the
10246 DECL_USE_TEMPLATE (r
) = 0;
10247 TREE_TYPE (r
) = type
;
10248 /* Clear out the mangled name and RTL for the instantiation. */
10249 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10250 SET_DECL_RTL (r
, NULL
);
10251 /* Leave DECL_INITIAL set on deleted instantiations. */
10252 if (!DECL_DELETED_FN (r
))
10253 DECL_INITIAL (r
) = NULL_TREE
;
10254 DECL_CONTEXT (r
) = ctx
;
10256 if (member
&& DECL_CONV_FN_P (r
))
10257 /* Type-conversion operator. Reconstruct the name, in
10258 case it's the name of one of the template's parameters. */
10259 DECL_NAME (r
) = mangle_conv_op_name_for_type (TREE_TYPE (type
));
10261 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
,
10263 DECL_RESULT (r
) = NULL_TREE
;
10265 TREE_STATIC (r
) = 0;
10266 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
10267 DECL_EXTERNAL (r
) = 1;
10268 /* If this is an instantiation of a function with internal
10269 linkage, we already know what object file linkage will be
10270 assigned to the instantiation. */
10271 DECL_INTERFACE_KNOWN (r
) = !TREE_PUBLIC (r
);
10272 DECL_DEFER_OUTPUT (r
) = 0;
10273 DECL_CHAIN (r
) = NULL_TREE
;
10274 DECL_PENDING_INLINE_INFO (r
) = 0;
10275 DECL_PENDING_INLINE_P (r
) = 0;
10276 DECL_SAVED_TREE (r
) = NULL_TREE
;
10277 DECL_STRUCT_FUNCTION (r
) = NULL
;
10279 /* We'll re-clone as appropriate in instantiate_template. */
10280 DECL_CLONED_FUNCTION (r
) = NULL_TREE
;
10282 /* If we aren't complaining now, return on error before we register
10283 the specialization so that we'll complain eventually. */
10284 if ((complain
& tf_error
) == 0
10285 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10286 && !grok_op_properties (r
, /*complain=*/false))
10287 RETURN (error_mark_node
);
10289 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
10290 this in the special friend case mentioned above where
10291 GEN_TMPL is NULL. */
10294 DECL_TEMPLATE_INFO (r
)
10295 = build_template_info (gen_tmpl
, argvec
);
10296 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10297 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10299 /* We're not supposed to instantiate default arguments
10300 until they are called, for a template. But, for a
10303 template <class T> void f ()
10304 { extern void g(int i = T()); }
10306 we should do the substitution when the template is
10307 instantiated. We handle the member function case in
10308 instantiate_class_template since the default arguments
10309 might refer to other members of the class. */
10311 && !PRIMARY_TEMPLATE_P (gen_tmpl
)
10312 && !uses_template_parms (argvec
))
10313 tsubst_default_arguments (r
);
10316 DECL_TEMPLATE_INFO (r
) = NULL_TREE
;
10318 /* Copy the list of befriending classes. */
10319 for (friends
= &DECL_BEFRIENDING_CLASSES (r
);
10321 friends
= &TREE_CHAIN (*friends
))
10323 *friends
= copy_node (*friends
);
10324 TREE_VALUE (*friends
) = tsubst (TREE_VALUE (*friends
),
10329 if (DECL_CONSTRUCTOR_P (r
) || DECL_DESTRUCTOR_P (r
))
10331 maybe_retrofit_in_chrg (r
);
10332 if (DECL_CONSTRUCTOR_P (r
))
10333 grok_ctor_properties (ctx
, r
);
10334 /* If this is an instantiation of a member template, clone it.
10335 If it isn't, that'll be handled by
10336 clone_constructors_and_destructors. */
10337 if (PRIMARY_TEMPLATE_P (gen_tmpl
))
10338 clone_function_decl (r
, /*update_method_vec_p=*/0);
10340 else if ((complain
& tf_error
) != 0
10341 && IDENTIFIER_OPNAME_P (DECL_NAME (r
))
10342 && !grok_op_properties (r
, /*complain=*/true))
10343 RETURN (error_mark_node
);
10345 if (DECL_FRIEND_P (t
) && DECL_FRIEND_CONTEXT (t
))
10346 SET_DECL_FRIEND_CONTEXT (r
,
10347 tsubst (DECL_FRIEND_CONTEXT (t
),
10348 args
, complain
, in_decl
));
10350 /* Possibly limit visibility based on template args. */
10351 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10352 if (DECL_VISIBILITY_SPECIFIED (t
))
10354 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10355 DECL_ATTRIBUTES (r
)
10356 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10358 determine_visibility (r
);
10359 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r
)
10360 && !processing_template_decl
)
10361 defaulted_late_check (r
);
10363 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10364 args
, complain
, in_decl
);
10370 tree type
= NULL_TREE
;
10372 tree expanded_types
= NULL_TREE
;
10373 tree prev_r
= NULL_TREE
;
10374 tree first_r
= NULL_TREE
;
10376 if (FUNCTION_PARAMETER_PACK_P (t
))
10378 /* If there is a local specialization that isn't a
10379 parameter pack, it means that we're doing a "simple"
10380 substitution from inside tsubst_pack_expansion. Just
10381 return the local specialization (which will be a single
10383 tree spec
= retrieve_local_specialization (t
);
10385 && TREE_CODE (spec
) == PARM_DECL
10386 && TREE_CODE (TREE_TYPE (spec
)) != TYPE_PACK_EXPANSION
)
10389 /* Expand the TYPE_PACK_EXPANSION that provides the types for
10390 the parameters in this function parameter pack. */
10391 expanded_types
= tsubst_pack_expansion (TREE_TYPE (t
), args
,
10392 complain
, in_decl
);
10393 if (TREE_CODE (expanded_types
) == TREE_VEC
)
10395 len
= TREE_VEC_LENGTH (expanded_types
);
10397 /* Zero-length parameter packs are boring. Just substitute
10400 RETURN (tsubst (TREE_CHAIN (t
), args
, complain
,
10405 /* All we did was update the type. Make a note of that. */
10406 type
= expanded_types
;
10407 expanded_types
= NULL_TREE
;
10411 /* Loop through all of the parameter's we'll build. When T is
10412 a function parameter pack, LEN is the number of expanded
10413 types in EXPANDED_TYPES; otherwise, LEN is 1. */
10415 for (i
= 0; i
< len
; ++i
)
10419 if (DECL_TEMPLATE_PARM_P (t
))
10420 SET_DECL_TEMPLATE_PARM_P (r
);
10422 if (expanded_types
)
10423 /* We're on the Ith parameter of the function parameter
10426 /* An argument of a function parameter pack is not a parameter
10428 FUNCTION_PARAMETER_PACK_P (r
) = false;
10430 /* Get the Ith type. */
10431 type
= TREE_VEC_ELT (expanded_types
, i
);
10434 /* Rename the parameter to include the index. */
10436 make_ith_pack_parameter_name (DECL_NAME (r
), i
);
10439 /* We're dealing with a normal parameter. */
10440 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10442 type
= type_decays_to (type
);
10443 TREE_TYPE (r
) = type
;
10444 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10446 if (DECL_INITIAL (r
))
10448 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
10449 DECL_INITIAL (r
) = TREE_TYPE (r
);
10451 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
,
10452 complain
, in_decl
);
10455 DECL_CONTEXT (r
) = NULL_TREE
;
10457 if (!DECL_TEMPLATE_PARM_P (r
))
10458 DECL_ARG_TYPE (r
) = type_passed_as (type
);
10460 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10461 args
, complain
, in_decl
);
10463 /* Keep track of the first new parameter we
10464 generate. That's what will be returned to the
10469 /* Build a proper chain of parameters when substituting
10470 into a function parameter pack. */
10472 DECL_CHAIN (prev_r
) = r
;
10475 if (DECL_CHAIN (t
))
10476 DECL_CHAIN (r
) = tsubst (DECL_CHAIN (t
), args
,
10477 complain
, DECL_CHAIN (t
));
10479 /* FIRST_R contains the start of the chain we've built. */
10489 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10490 if (type
== error_mark_node
)
10491 RETURN (error_mark_node
);
10492 TREE_TYPE (r
) = type
;
10493 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10495 if (DECL_C_BIT_FIELD (r
))
10496 /* For bit-fields, DECL_INITIAL gives the number of bits. For
10497 non-bit-fields DECL_INITIAL is a non-static data member
10498 initializer, which gets deferred instantiation. */
10500 = tsubst_expr (DECL_INITIAL (t
), args
,
10502 /*integral_constant_expression_p=*/true);
10503 else if (DECL_INITIAL (t
))
10505 /* Set up DECL_TEMPLATE_INFO so that we can get at the
10506 NSDMI in perform_member_init. Still set DECL_INITIAL
10507 so that we know there is one. */
10508 DECL_INITIAL (r
) = void_zero_node
;
10509 gcc_assert (DECL_LANG_SPECIFIC (r
) == NULL
);
10510 retrofit_lang_decl (r
);
10511 DECL_TEMPLATE_INFO (r
) = build_template_info (t
, args
);
10513 /* We don't have to set DECL_CONTEXT here; it is set by
10514 finish_member_declaration. */
10515 DECL_CHAIN (r
) = NULL_TREE
;
10516 if (VOID_TYPE_P (type
))
10517 error ("instantiation of %q+D as type %qT", r
, type
);
10519 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
), 0,
10520 args
, complain
, in_decl
);
10525 /* We reach here only for member using decls. */
10526 if (DECL_DEPENDENT_P (t
))
10528 r
= do_class_using_decl
10529 (tsubst_copy (USING_DECL_SCOPE (t
), args
, complain
, in_decl
),
10530 tsubst_copy (DECL_NAME (t
), args
, complain
, in_decl
));
10532 r
= error_mark_node
;
10535 TREE_PROTECTED (r
) = TREE_PROTECTED (t
);
10536 TREE_PRIVATE (r
) = TREE_PRIVATE (t
);
10542 DECL_CHAIN (r
) = NULL_TREE
;
10549 tree argvec
= NULL_TREE
;
10550 tree gen_tmpl
= NULL_TREE
;
10552 tree tmpl
= NULL_TREE
;
10554 tree type
= NULL_TREE
;
10557 if (TREE_CODE (t
) == TYPE_DECL
10558 && t
== TYPE_MAIN_DECL (TREE_TYPE (t
)))
10560 /* If this is the canonical decl, we don't have to
10561 mess with instantiations, and often we can't (for
10562 typename, template type parms and such). Note that
10563 TYPE_NAME is not correct for the above test if
10564 we've copied the type for a typedef. */
10565 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10566 if (type
== error_mark_node
)
10567 RETURN (error_mark_node
);
10568 r
= TYPE_NAME (type
);
10572 /* Check to see if we already have the specialization we
10575 if (DECL_CLASS_SCOPE_P (t
) || DECL_NAMESPACE_SCOPE_P (t
))
10577 /* T is a static data member or namespace-scope entity.
10578 We have to substitute into namespace-scope variables
10579 (even though such entities are never templates) because
10582 template <class T> void f() { extern T t; }
10584 where the entity referenced is not known until
10585 instantiation time. */
10587 ctx
= DECL_CONTEXT (t
);
10588 if (DECL_CLASS_SCOPE_P (t
))
10590 ctx
= tsubst_aggr_type (ctx
, args
,
10592 in_decl
, /*entering_scope=*/1);
10593 /* If CTX is unchanged, then T is in fact the
10594 specialization we want. That situation occurs when
10595 referencing a static data member within in its own
10596 class. We can use pointer equality, rather than
10597 same_type_p, because DECL_CONTEXT is always
10599 if (ctx
== DECL_CONTEXT (t
)
10600 && (TREE_CODE (t
) != TYPE_DECL
10601 /* ... unless T is a member template; in which
10602 case our caller can be willing to create a
10603 specialization of that template represented
10605 || !(DECL_TI_TEMPLATE (t
)
10606 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))))
10612 tmpl
= DECL_TI_TEMPLATE (t
);
10613 gen_tmpl
= most_general_template (tmpl
);
10614 argvec
= tsubst (DECL_TI_ARGS (t
), args
, complain
, in_decl
);
10615 hash
= hash_tmpl_and_args (gen_tmpl
, argvec
);
10616 spec
= retrieve_specialization (gen_tmpl
, argvec
, hash
);
10621 /* A local variable. */
10623 /* Subsequent calls to pushdecl will fill this in. */
10625 spec
= retrieve_local_specialization (t
);
10627 /* If we already have the specialization we need, there is
10628 nothing more to do. */
10635 /* Create a new node for the specialization we need. */
10637 if (type
== NULL_TREE
)
10639 if (is_typedef_decl (t
))
10640 type
= DECL_ORIGINAL_TYPE (t
);
10642 type
= TREE_TYPE (t
);
10643 if (TREE_CODE (t
) == VAR_DECL
10644 && VAR_HAD_UNKNOWN_BOUND (t
)
10645 && type
!= error_mark_node
)
10646 type
= strip_array_domain (type
);
10647 type
= tsubst (type
, args
, complain
, in_decl
);
10649 if (TREE_CODE (r
) == VAR_DECL
)
10651 /* Even if the original location is out of scope, the
10652 newly substituted one is not. */
10653 DECL_DEAD_FOR_LOCAL (r
) = 0;
10654 DECL_INITIALIZED_P (r
) = 0;
10655 DECL_TEMPLATE_INSTANTIATED (r
) = 0;
10656 if (type
== error_mark_node
)
10657 RETURN (error_mark_node
);
10658 if (TREE_CODE (type
) == FUNCTION_TYPE
)
10660 /* It may seem that this case cannot occur, since:
10665 declares a function, not a variable. However:
10668 template <typename T> void g() { T t; }
10669 template void g<f>();
10671 is an attempt to declare a variable with function
10673 error ("variable %qD has function type",
10674 /* R is not yet sufficiently initialized, so we
10675 just use its name. */
10677 RETURN (error_mark_node
);
10679 type
= complete_type (type
);
10680 /* Wait until cp_finish_decl to set this again, to handle
10681 circular dependency (template/instantiate6.C). */
10682 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
) = 0;
10683 type
= check_var_type (DECL_NAME (r
), type
);
10685 if (DECL_HAS_VALUE_EXPR_P (t
))
10687 tree ve
= DECL_VALUE_EXPR (t
);
10688 ve
= tsubst_expr (ve
, args
, complain
, in_decl
,
10689 /*constant_expression_p=*/false);
10690 if (REFERENCE_REF_P (ve
))
10692 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
10693 ve
= TREE_OPERAND (ve
, 0);
10695 SET_DECL_VALUE_EXPR (r
, ve
);
10698 else if (DECL_SELF_REFERENCE_P (t
))
10699 SET_DECL_SELF_REFERENCE_P (r
);
10700 TREE_TYPE (r
) = type
;
10701 cp_apply_type_quals_to_decl (cp_type_quals (type
), r
);
10702 DECL_CONTEXT (r
) = ctx
;
10703 /* Clear out the mangled name and RTL for the instantiation. */
10704 SET_DECL_ASSEMBLER_NAME (r
, NULL_TREE
);
10705 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10706 SET_DECL_RTL (r
, NULL
);
10707 /* The initializer must not be expanded until it is required;
10708 see [temp.inst]. */
10709 DECL_INITIAL (r
) = NULL_TREE
;
10710 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_WRTL
))
10711 SET_DECL_RTL (r
, NULL
);
10712 DECL_SIZE (r
) = DECL_SIZE_UNIT (r
) = 0;
10713 if (TREE_CODE (r
) == VAR_DECL
)
10715 /* Possibly limit visibility based on template args. */
10716 DECL_VISIBILITY (r
) = VISIBILITY_DEFAULT
;
10717 if (DECL_VISIBILITY_SPECIFIED (t
))
10719 DECL_VISIBILITY_SPECIFIED (r
) = 0;
10720 DECL_ATTRIBUTES (r
)
10721 = remove_attribute ("visibility", DECL_ATTRIBUTES (r
));
10723 determine_visibility (r
);
10728 /* A static data member declaration is always marked
10729 external when it is declared in-class, even if an
10730 initializer is present. We mimic the non-template
10731 processing here. */
10732 DECL_EXTERNAL (r
) = 1;
10734 register_specialization (r
, gen_tmpl
, argvec
, false, hash
);
10735 DECL_TEMPLATE_INFO (r
) = build_template_info (tmpl
, argvec
);
10736 SET_DECL_IMPLICIT_INSTANTIATION (r
);
10738 else if (cp_unevaluated_operand
)
10740 /* We're substituting this var in a decltype outside of its
10741 scope, such as for a lambda return type. Don't add it to
10742 local_specializations, do perform auto deduction. */
10743 tree auto_node
= type_uses_auto (type
);
10747 = tsubst_expr (DECL_INITIAL (t
), args
, complain
, in_decl
,
10748 /*constant_expression_p=*/false);
10749 init
= resolve_nondeduced_context (init
);
10750 TREE_TYPE (r
) = type
10751 = do_auto_deduction (type
, init
, auto_node
);
10755 register_local_specialization (r
, t
);
10757 DECL_CHAIN (r
) = NULL_TREE
;
10759 apply_late_template_attributes (&r
, DECL_ATTRIBUTES (r
),
10761 args
, complain
, in_decl
);
10763 /* Preserve a typedef that names a type. */
10764 if (is_typedef_decl (r
))
10766 DECL_ORIGINAL_TYPE (r
) = NULL_TREE
;
10767 set_underlying_type (r
);
10770 layout_decl (r
, 0);
10775 gcc_unreachable ();
10780 /* Restore the file and line information. */
10781 input_location
= saved_loc
;
10786 /* Substitute into the ARG_TYPES of a function type. */
10789 tsubst_arg_types (tree arg_types
,
10791 tsubst_flags_t complain
,
10794 tree remaining_arg_types
;
10795 tree type
= NULL_TREE
;
10797 tree expanded_args
= NULL_TREE
;
10800 if (!arg_types
|| arg_types
== void_list_node
)
10803 remaining_arg_types
= tsubst_arg_types (TREE_CHAIN (arg_types
),
10804 args
, complain
, in_decl
);
10805 if (remaining_arg_types
== error_mark_node
)
10806 return error_mark_node
;
10808 if (PACK_EXPANSION_P (TREE_VALUE (arg_types
)))
10810 /* For a pack expansion, perform substitution on the
10811 entire expression. Later on, we'll handle the arguments
10813 expanded_args
= tsubst_pack_expansion (TREE_VALUE (arg_types
),
10814 args
, complain
, in_decl
);
10816 if (TREE_CODE (expanded_args
) == TREE_VEC
)
10817 /* So that we'll spin through the parameters, one by one. */
10818 i
= TREE_VEC_LENGTH (expanded_args
);
10821 /* We only partially substituted into the parameter
10822 pack. Our type is TYPE_PACK_EXPANSION. */
10823 type
= expanded_args
;
10824 expanded_args
= NULL_TREE
;
10832 type
= TREE_VEC_ELT (expanded_args
, i
);
10834 type
= tsubst (TREE_VALUE (arg_types
), args
, complain
, in_decl
);
10836 if (type
== error_mark_node
)
10837 return error_mark_node
;
10838 if (VOID_TYPE_P (type
))
10840 if (complain
& tf_error
)
10842 error ("invalid parameter type %qT", type
);
10844 error ("in declaration %q+D", in_decl
);
10846 return error_mark_node
;
10849 /* Do array-to-pointer, function-to-pointer conversion, and ignore
10850 top-level qualifiers as required. */
10851 type
= cv_unqualified (type_decays_to (type
));
10853 /* We do not substitute into default arguments here. The standard
10854 mandates that they be instantiated only when needed, which is
10855 done in build_over_call. */
10856 default_arg
= TREE_PURPOSE (arg_types
);
10858 if (default_arg
&& TREE_CODE (default_arg
) == DEFAULT_ARG
)
10860 /* We've instantiated a template before its default arguments
10861 have been parsed. This can happen for a nested template
10862 class, and is not an error unless we require the default
10863 argument in a call of this function. */
10864 remaining_arg_types
=
10865 tree_cons (default_arg
, type
, remaining_arg_types
);
10866 VEC_safe_push (tree
, gc
, DEFARG_INSTANTIATIONS (default_arg
),
10867 remaining_arg_types
);
10870 remaining_arg_types
=
10871 hash_tree_cons (default_arg
, type
, remaining_arg_types
);
10874 return remaining_arg_types
;
10877 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
10878 *not* handle the exception-specification for FNTYPE, because the
10879 initial substitution of explicitly provided template parameters
10880 during argument deduction forbids substitution into the
10881 exception-specification:
10885 All references in the function type of the function template to the
10886 corresponding template parameters are replaced by the specified tem-
10887 plate argument values. If a substitution in a template parameter or
10888 in the function type of the function template results in an invalid
10889 type, type deduction fails. [Note: The equivalent substitution in
10890 exception specifications is done only when the function is instanti-
10891 ated, at which point a program is ill-formed if the substitution
10892 results in an invalid type.] */
10895 tsubst_function_type (tree t
,
10897 tsubst_flags_t complain
,
10904 /* The TYPE_CONTEXT is not used for function/method types. */
10905 gcc_assert (TYPE_CONTEXT (t
) == NULL_TREE
);
10907 /* Substitute the return type. */
10908 return_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
10909 if (return_type
== error_mark_node
)
10910 return error_mark_node
;
10911 /* The standard does not presently indicate that creation of a
10912 function type with an invalid return type is a deduction failure.
10913 However, that is clearly analogous to creating an array of "void"
10914 or a reference to a reference. This is core issue #486. */
10915 if (TREE_CODE (return_type
) == ARRAY_TYPE
10916 || TREE_CODE (return_type
) == FUNCTION_TYPE
)
10918 if (complain
& tf_error
)
10920 if (TREE_CODE (return_type
) == ARRAY_TYPE
)
10921 error ("function returning an array");
10923 error ("function returning a function");
10925 return error_mark_node
;
10928 /* Substitute the argument types. */
10929 arg_types
= tsubst_arg_types (TYPE_ARG_TYPES (t
), args
,
10930 complain
, in_decl
);
10931 if (arg_types
== error_mark_node
)
10932 return error_mark_node
;
10934 /* Construct a new type node and return it. */
10935 if (TREE_CODE (t
) == FUNCTION_TYPE
)
10937 fntype
= build_function_type (return_type
, arg_types
);
10938 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (t
));
10942 tree r
= TREE_TYPE (TREE_VALUE (arg_types
));
10943 if (! MAYBE_CLASS_TYPE_P (r
))
10947 Type deduction may fail for any of the following
10950 -- Attempting to create "pointer to member of T" when T
10951 is not a class type. */
10952 if (complain
& tf_error
)
10953 error ("creating pointer to member function of non-class type %qT",
10955 return error_mark_node
;
10958 fntype
= build_method_type_directly (r
, return_type
,
10959 TREE_CHAIN (arg_types
));
10961 fntype
= cp_build_type_attribute_variant (fntype
, TYPE_ATTRIBUTES (t
));
10966 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
10967 ARGS into that specification, and return the substituted
10968 specification. If there is no specification, return NULL_TREE. */
10971 tsubst_exception_specification (tree fntype
,
10973 tsubst_flags_t complain
,
10980 specs
= TYPE_RAISES_EXCEPTIONS (fntype
);
10981 new_specs
= NULL_TREE
;
10982 if (specs
&& TREE_PURPOSE (specs
))
10984 /* A noexcept-specifier. */
10985 tree expr
= TREE_PURPOSE (specs
);
10986 if (expr
== boolean_true_node
|| expr
== boolean_false_node
)
10990 /* Defer instantiation of noexcept-specifiers to avoid
10991 excessive instantiations (c++/49107). */
10992 new_specs
= make_node (DEFERRED_NOEXCEPT
);
10993 if (DEFERRED_NOEXCEPT_SPEC_P (specs
))
10995 /* We already partially instantiated this member template,
10996 so combine the new args with the old. */
10997 DEFERRED_NOEXCEPT_PATTERN (new_specs
)
10998 = DEFERRED_NOEXCEPT_PATTERN (expr
);
10999 DEFERRED_NOEXCEPT_ARGS (new_specs
)
11000 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr
), args
);
11004 DEFERRED_NOEXCEPT_PATTERN (new_specs
) = expr
;
11005 DEFERRED_NOEXCEPT_ARGS (new_specs
) = args
;
11009 new_specs
= tsubst_copy_and_build
11010 (expr
, args
, complain
, in_decl
, /*function_p=*/false,
11011 /*integral_constant_expression_p=*/true);
11012 new_specs
= build_noexcept_spec (new_specs
, complain
);
11016 if (! TREE_VALUE (specs
))
11023 tree expanded_specs
= NULL_TREE
;
11025 if (PACK_EXPANSION_P (TREE_VALUE (specs
)))
11027 /* Expand the pack expansion type. */
11028 expanded_specs
= tsubst_pack_expansion (TREE_VALUE (specs
),
11032 if (expanded_specs
== error_mark_node
)
11033 return error_mark_node
;
11034 else if (TREE_CODE (expanded_specs
) == TREE_VEC
)
11035 len
= TREE_VEC_LENGTH (expanded_specs
);
11038 /* We're substituting into a member template, so
11039 we got a TYPE_PACK_EXPANSION back. Add that
11040 expansion and move on. */
11041 gcc_assert (TREE_CODE (expanded_specs
)
11042 == TYPE_PACK_EXPANSION
);
11043 new_specs
= add_exception_specifier (new_specs
,
11046 specs
= TREE_CHAIN (specs
);
11051 for (i
= 0; i
< len
; ++i
)
11053 if (expanded_specs
)
11054 spec
= TREE_VEC_ELT (expanded_specs
, i
);
11056 spec
= tsubst (TREE_VALUE (specs
), args
, complain
, in_decl
);
11057 if (spec
== error_mark_node
)
11059 new_specs
= add_exception_specifier (new_specs
, spec
,
11063 specs
= TREE_CHAIN (specs
);
11069 /* Take the tree structure T and replace template parameters used
11070 therein with the argument vector ARGS. IN_DECL is an associated
11071 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
11072 Issue error and warning messages under control of COMPLAIN. Note
11073 that we must be relatively non-tolerant of extensions here, in
11074 order to preserve conformance; if we allow substitutions that
11075 should not be allowed, we may allow argument deductions that should
11076 not succeed, and therefore report ambiguous overload situations
11077 where there are none. In theory, we could allow the substitution,
11078 but indicate that it should have failed, and allow our caller to
11079 make sure that the right thing happens, but we don't try to do this
11082 This function is used for dealing with types, decls and the like;
11083 for expressions, use tsubst_expr or tsubst_copy. */
11086 tsubst (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
11088 enum tree_code code
;
11089 tree type
, r
= NULL_TREE
;
11091 if (t
== NULL_TREE
|| t
== error_mark_node
11092 || t
== integer_type_node
11093 || t
== void_type_node
11094 || t
== char_type_node
11095 || t
== unknown_type_node
11096 || TREE_CODE (t
) == NAMESPACE_DECL
11097 || TREE_CODE (t
) == TRANSLATION_UNIT_DECL
)
11101 return tsubst_decl (t
, args
, complain
);
11103 if (args
== NULL_TREE
)
11106 code
= TREE_CODE (t
);
11108 if (code
== IDENTIFIER_NODE
)
11109 type
= IDENTIFIER_TYPE_VALUE (t
);
11111 type
= TREE_TYPE (t
);
11113 gcc_assert (type
!= unknown_type_node
);
11115 /* Reuse typedefs. We need to do this to handle dependent attributes,
11116 such as attribute aligned. */
11118 && typedef_variant_p (t
))
11120 tree decl
= TYPE_NAME (t
);
11122 if (TYPE_DECL_ALIAS_P (decl
)
11123 && DECL_LANG_SPECIFIC (decl
)
11124 && DECL_TEMPLATE_INFO (decl
)
11125 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
11127 /* DECL represents an alias template and we want to
11128 instantiate it. Let's substitute our arguments for the
11129 template parameters into the declaration and get the
11131 r
= tsubst (decl
, args
, complain
, decl
);
11133 else if (DECL_CLASS_SCOPE_P (decl
)
11134 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11135 && uses_template_parms (DECL_CONTEXT (decl
)))
11137 tree tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
11138 tree gen_args
= tsubst (DECL_TI_ARGS (decl
), args
, complain
, in_decl
);
11139 r
= retrieve_specialization (tmpl
, gen_args
, 0);
11141 else if (DECL_FUNCTION_SCOPE_P (decl
)
11142 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl
))
11143 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl
))))
11144 r
= retrieve_local_specialization (decl
);
11146 /* The typedef is from a non-template context. */
11152 r
= cp_build_qualified_type_real
11153 (r
, cp_type_quals (t
) | cp_type_quals (r
),
11154 complain
| tf_ignore_bad_quals
);
11158 /* We don't have an instantiation yet, so drop the typedef. */
11159 t
= DECL_ORIGINAL_TYPE (decl
);
11163 && code
!= TYPENAME_TYPE
11164 && code
!= TEMPLATE_TYPE_PARM
11165 && code
!= IDENTIFIER_NODE
11166 && code
!= FUNCTION_TYPE
11167 && code
!= METHOD_TYPE
)
11168 type
= tsubst (type
, args
, complain
, in_decl
);
11169 if (type
== error_mark_node
)
11170 return error_mark_node
;
11176 case ENUMERAL_TYPE
:
11177 return tsubst_aggr_type (t
, args
, complain
, in_decl
,
11178 /*entering_scope=*/0);
11181 case IDENTIFIER_NODE
:
11192 if (t
== integer_type_node
)
11195 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
11196 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
11200 tree max
, omax
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
11202 max
= tsubst_expr (omax
, args
, complain
, in_decl
,
11203 /*integral_constant_expression_p=*/false);
11205 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
11207 if (TREE_CODE (max
) == NOP_EXPR
11208 && TREE_SIDE_EFFECTS (omax
)
11209 && !TREE_TYPE (max
))
11210 TREE_TYPE (max
) = TREE_TYPE (TREE_OPERAND (max
, 0));
11212 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
11213 with TREE_SIDE_EFFECTS that indicates this is not an integral
11214 constant expression. */
11215 if (processing_template_decl
11216 && TREE_SIDE_EFFECTS (omax
) && TREE_CODE (omax
) == NOP_EXPR
)
11218 gcc_assert (TREE_CODE (max
) == NOP_EXPR
);
11219 TREE_SIDE_EFFECTS (max
) = 1;
11222 return compute_array_index_type (NULL_TREE
, max
, complain
);
11225 case TEMPLATE_TYPE_PARM
:
11226 case TEMPLATE_TEMPLATE_PARM
:
11227 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11228 case TEMPLATE_PARM_INDEX
:
11233 tree arg
= NULL_TREE
;
11237 gcc_assert (TREE_VEC_LENGTH (args
) > 0);
11238 template_parm_level_and_index (t
, &level
, &idx
);
11240 levels
= TMPL_ARGS_DEPTH (args
);
11241 if (level
<= levels
)
11243 arg
= TMPL_ARG (args
, level
, idx
);
11245 if (arg
&& TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
11246 /* See through ARGUMENT_PACK_SELECT arguments. */
11247 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
11250 if (arg
== error_mark_node
)
11251 return error_mark_node
;
11252 else if (arg
!= NULL_TREE
)
11254 if (ARGUMENT_PACK_P (arg
))
11255 /* If ARG is an argument pack, we don't actually want to
11256 perform a substitution here, because substitutions
11257 for argument packs are only done
11258 element-by-element. We can get to this point when
11259 substituting the type of a non-type template
11260 parameter pack, when that type actually contains
11261 template parameter packs from an outer template, e.g.,
11263 template<typename... Types> struct A {
11264 template<Types... Values> struct B { };
11268 if (code
== TEMPLATE_TYPE_PARM
)
11271 gcc_assert (TYPE_P (arg
));
11273 quals
= cp_type_quals (arg
) | cp_type_quals (t
);
11275 return cp_build_qualified_type_real
11276 (arg
, quals
, complain
| tf_ignore_bad_quals
);
11278 else if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11280 /* We are processing a type constructed from a
11281 template template parameter. */
11282 tree argvec
= tsubst (TYPE_TI_ARGS (t
),
11283 args
, complain
, in_decl
);
11284 if (argvec
== error_mark_node
)
11285 return error_mark_node
;
11287 gcc_assert (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
11288 || TREE_CODE (arg
) == TEMPLATE_DECL
11289 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
);
11291 if (TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
11292 /* Consider this code:
11294 template <template <class> class Template>
11296 template <class Arg> using Bind = Template<Arg>;
11299 template <template <class> class Template, class Arg>
11300 using Instantiate = Template<Arg>; //#0
11302 template <template <class> class Template,
11305 Instantiate<Internal<Template>::template Bind,
11308 When #1 is parsed, the
11309 BOUND_TEMPLATE_TEMPLATE_PARM representing the
11310 parameter `Template' in #0 matches the
11311 UNBOUND_CLASS_TEMPLATE representing the argument
11312 `Internal<Template>::template Bind'; We then want
11313 to assemble the type `Bind<Argument>' that can't
11314 be fully created right now, because
11315 `Internal<Template>' not being complete, the Bind
11316 template cannot be looked up in that context. So
11317 we need to "store" `Bind<Argument>' for later
11318 when the context of Bind becomes complete. Let's
11319 store that in a TYPENAME_TYPE. */
11320 return make_typename_type (TYPE_CONTEXT (arg
),
11321 build_nt (TEMPLATE_ID_EXPR
,
11322 TYPE_IDENTIFIER (arg
),
11327 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
11328 are resolving nested-types in the signature of a
11329 member function templates. Otherwise ARG is a
11330 TEMPLATE_DECL and is the real template to be
11332 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
11333 arg
= TYPE_NAME (arg
);
11335 r
= lookup_template_class (arg
,
11337 DECL_CONTEXT (arg
),
11338 /*entering_scope=*/0,
11340 return cp_build_qualified_type_real
11341 (r
, cp_type_quals (t
), complain
);
11344 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
11345 return convert_from_reference (unshare_expr (arg
));
11349 /* This can happen during the attempted tsubst'ing in
11350 unify. This means that we don't yet have any information
11351 about the template parameter in question. */
11354 /* If we get here, we must have been looking at a parm for a
11355 more deeply nested template. Make a new version of this
11356 template parameter, but with a lower level. */
11359 case TEMPLATE_TYPE_PARM
:
11360 case TEMPLATE_TEMPLATE_PARM
:
11361 case BOUND_TEMPLATE_TEMPLATE_PARM
:
11362 if (cp_type_quals (t
))
11364 r
= tsubst (TYPE_MAIN_VARIANT (t
), args
, complain
, in_decl
);
11365 r
= cp_build_qualified_type_real
11366 (r
, cp_type_quals (t
),
11367 complain
| (code
== TEMPLATE_TYPE_PARM
11368 ? tf_ignore_bad_quals
: 0));
11373 TEMPLATE_TYPE_PARM_INDEX (r
)
11374 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
11375 r
, levels
, args
, complain
);
11376 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
11377 TYPE_MAIN_VARIANT (r
) = r
;
11378 TYPE_POINTER_TO (r
) = NULL_TREE
;
11379 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
11381 if (TREE_CODE (r
) == TEMPLATE_TEMPLATE_PARM
)
11382 /* We have reduced the level of the template
11383 template parameter, but not the levels of its
11384 template parameters, so canonical_type_parameter
11385 will not be able to find the canonical template
11386 template parameter for this level. Thus, we
11387 require structural equality checking to compare
11388 TEMPLATE_TEMPLATE_PARMs. */
11389 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11390 else if (TYPE_STRUCTURAL_EQUALITY_P (t
))
11391 SET_TYPE_STRUCTURAL_EQUALITY (r
);
11393 TYPE_CANONICAL (r
) = canonical_type_parameter (r
);
11395 if (code
== BOUND_TEMPLATE_TEMPLATE_PARM
)
11397 tree argvec
= tsubst (TYPE_TI_ARGS (t
), args
,
11398 complain
, in_decl
);
11399 if (argvec
== error_mark_node
)
11400 return error_mark_node
;
11402 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r
)
11403 = build_template_info (TYPE_TI_TEMPLATE (t
), argvec
);
11408 case TEMPLATE_PARM_INDEX
:
11409 r
= reduce_template_parm_level (t
, type
, levels
, args
, complain
);
11413 gcc_unreachable ();
11421 tree purpose
, value
, chain
;
11423 if (t
== void_list_node
)
11426 purpose
= TREE_PURPOSE (t
);
11429 purpose
= tsubst (purpose
, args
, complain
, in_decl
);
11430 if (purpose
== error_mark_node
)
11431 return error_mark_node
;
11433 value
= TREE_VALUE (t
);
11436 value
= tsubst (value
, args
, complain
, in_decl
);
11437 if (value
== error_mark_node
)
11438 return error_mark_node
;
11440 chain
= TREE_CHAIN (t
);
11441 if (chain
&& chain
!= void_type_node
)
11443 chain
= tsubst (chain
, args
, complain
, in_decl
);
11444 if (chain
== error_mark_node
)
11445 return error_mark_node
;
11447 if (purpose
== TREE_PURPOSE (t
)
11448 && value
== TREE_VALUE (t
)
11449 && chain
== TREE_CHAIN (t
))
11451 return hash_tree_cons (purpose
, value
, chain
);
11455 /* We should never be tsubsting a binfo. */
11456 gcc_unreachable ();
11459 /* A vector of template arguments. */
11460 gcc_assert (!type
);
11461 return tsubst_template_args (t
, args
, complain
, in_decl
);
11464 case REFERENCE_TYPE
:
11466 if (type
== TREE_TYPE (t
) && TREE_CODE (type
) != METHOD_TYPE
)
11471 Type deduction may fail for any of the following
11474 -- Attempting to create a pointer to reference type.
11475 -- Attempting to create a reference to a reference type or
11476 a reference to void.
11478 Core issue 106 says that creating a reference to a reference
11479 during instantiation is no longer a cause for failure. We
11480 only enforce this check in strict C++98 mode. */
11481 if ((TREE_CODE (type
) == REFERENCE_TYPE
11482 && (((cxx_dialect
== cxx98
) && flag_iso
) || code
!= REFERENCE_TYPE
))
11483 || (code
== REFERENCE_TYPE
&& TREE_CODE (type
) == VOID_TYPE
))
11485 static location_t last_loc
;
11487 /* We keep track of the last time we issued this error
11488 message to avoid spewing a ton of messages during a
11489 single bad template instantiation. */
11490 if (complain
& tf_error
11491 && last_loc
!= input_location
)
11493 if (TREE_CODE (type
) == VOID_TYPE
)
11494 error ("forming reference to void");
11495 else if (code
== POINTER_TYPE
)
11496 error ("forming pointer to reference type %qT", type
);
11498 error ("forming reference to reference type %qT", type
);
11499 last_loc
= input_location
;
11502 return error_mark_node
;
11504 else if (code
== POINTER_TYPE
)
11506 r
= build_pointer_type (type
);
11507 if (TREE_CODE (type
) == METHOD_TYPE
)
11508 r
= build_ptrmemfunc_type (r
);
11510 else if (TREE_CODE (type
) == REFERENCE_TYPE
)
11511 /* In C++0x, during template argument substitution, when there is an
11512 attempt to create a reference to a reference type, reference
11513 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
11515 "If a template-argument for a template-parameter T names a type
11516 that is a reference to a type A, an attempt to create the type
11517 'lvalue reference to cv T' creates the type 'lvalue reference to
11518 A,' while an attempt to create the type type rvalue reference to
11519 cv T' creates the type T"
11521 r
= cp_build_reference_type
11523 TYPE_REF_IS_RVALUE (t
) && TYPE_REF_IS_RVALUE (type
));
11525 r
= cp_build_reference_type (type
, TYPE_REF_IS_RVALUE (t
));
11526 r
= cp_build_qualified_type_real (r
, cp_type_quals (t
), complain
);
11528 if (r
!= error_mark_node
)
11529 /* Will this ever be needed for TYPE_..._TO values? */
11536 r
= tsubst (TYPE_OFFSET_BASETYPE (t
), args
, complain
, in_decl
);
11537 if (r
== error_mark_node
|| !MAYBE_CLASS_TYPE_P (r
))
11541 Type deduction may fail for any of the following
11544 -- Attempting to create "pointer to member of T" when T
11545 is not a class type. */
11546 if (complain
& tf_error
)
11547 error ("creating pointer to member of non-class type %qT", r
);
11548 return error_mark_node
;
11550 if (TREE_CODE (type
) == REFERENCE_TYPE
)
11552 if (complain
& tf_error
)
11553 error ("creating pointer to member reference type %qT", type
);
11554 return error_mark_node
;
11556 if (TREE_CODE (type
) == VOID_TYPE
)
11558 if (complain
& tf_error
)
11559 error ("creating pointer to member of type void");
11560 return error_mark_node
;
11562 gcc_assert (TREE_CODE (type
) != METHOD_TYPE
);
11563 if (TREE_CODE (type
) == FUNCTION_TYPE
)
11565 /* The type of the implicit object parameter gets its
11566 cv-qualifiers from the FUNCTION_TYPE. */
11568 tree method_type
= build_memfn_type (type
, r
, type_memfn_quals (type
));
11569 memptr
= build_ptrmemfunc_type (build_pointer_type (method_type
));
11570 return cp_build_qualified_type_real (memptr
, cp_type_quals (t
),
11574 return cp_build_qualified_type_real (build_ptrmem_type (r
, type
),
11578 case FUNCTION_TYPE
:
11583 fntype
= tsubst_function_type (t
, args
, complain
, in_decl
);
11584 if (fntype
== error_mark_node
)
11585 return error_mark_node
;
11587 /* Substitute the exception specification. */
11588 specs
= tsubst_exception_specification (t
, args
, complain
,
11589 in_decl
, /*defer_ok*/true);
11590 if (specs
== error_mark_node
)
11591 return error_mark_node
;
11593 fntype
= build_exception_variant (fntype
, specs
);
11598 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, complain
, in_decl
);
11599 if (domain
== error_mark_node
)
11600 return error_mark_node
;
11602 /* As an optimization, we avoid regenerating the array type if
11603 it will obviously be the same as T. */
11604 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
11607 /* These checks should match the ones in grokdeclarator.
11611 The deduction may fail for any of the following reasons:
11613 -- Attempting to create an array with an element type that
11614 is void, a function type, or a reference type, or [DR337]
11615 an abstract class type. */
11616 if (TREE_CODE (type
) == VOID_TYPE
11617 || TREE_CODE (type
) == FUNCTION_TYPE
11618 || TREE_CODE (type
) == REFERENCE_TYPE
)
11620 if (complain
& tf_error
)
11621 error ("creating array of %qT", type
);
11622 return error_mark_node
;
11624 if (CLASS_TYPE_P (type
) && CLASSTYPE_PURE_VIRTUALS (type
))
11626 if (complain
& tf_error
)
11627 error ("creating array of %qT, which is an abstract class type",
11629 return error_mark_node
;
11632 r
= build_cplus_array_type (type
, domain
);
11634 if (TYPE_USER_ALIGN (t
))
11636 TYPE_ALIGN (r
) = TYPE_ALIGN (t
);
11637 TYPE_USER_ALIGN (r
) = 1;
11643 case TYPENAME_TYPE
:
11645 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11646 in_decl
, /*entering_scope=*/1);
11647 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
,
11648 complain
, in_decl
);
11650 if (ctx
== error_mark_node
|| f
== error_mark_node
)
11651 return error_mark_node
;
11653 if (!MAYBE_CLASS_TYPE_P (ctx
))
11655 if (complain
& tf_error
)
11656 error ("%qT is not a class, struct, or union type", ctx
);
11657 return error_mark_node
;
11659 else if (!uses_template_parms (ctx
) && !TYPE_BEING_DEFINED (ctx
))
11661 /* Normally, make_typename_type does not require that the CTX
11662 have complete type in order to allow things like:
11664 template <class T> struct S { typename S<T>::X Y; };
11666 But, such constructs have already been resolved by this
11667 point, so here CTX really should have complete type, unless
11668 it's a partial instantiation. */
11669 ctx
= complete_type (ctx
);
11670 if (!COMPLETE_TYPE_P (ctx
))
11672 if (complain
& tf_error
)
11673 cxx_incomplete_type_error (NULL_TREE
, ctx
);
11674 return error_mark_node
;
11678 f
= make_typename_type (ctx
, f
, typename_type
,
11679 (complain
& tf_error
) | tf_keep_type_decl
);
11680 if (f
== error_mark_node
)
11682 if (TREE_CODE (f
) == TYPE_DECL
)
11684 complain
|= tf_ignore_bad_quals
;
11688 if (TREE_CODE (f
) != TYPENAME_TYPE
)
11690 if (TYPENAME_IS_ENUM_P (t
) && TREE_CODE (f
) != ENUMERAL_TYPE
)
11692 if (complain
& tf_error
)
11693 error ("%qT resolves to %qT, which is not an enumeration type",
11696 return error_mark_node
;
11698 else if (TYPENAME_IS_CLASS_P (t
) && !CLASS_TYPE_P (f
))
11700 if (complain
& tf_error
)
11701 error ("%qT resolves to %qT, which is is not a class type",
11704 return error_mark_node
;
11708 return cp_build_qualified_type_real
11709 (f
, cp_type_quals (f
) | cp_type_quals (t
), complain
);
11712 case UNBOUND_CLASS_TEMPLATE
:
11714 tree ctx
= tsubst_aggr_type (TYPE_CONTEXT (t
), args
, complain
,
11715 in_decl
, /*entering_scope=*/1);
11716 tree name
= TYPE_IDENTIFIER (t
);
11717 tree parm_list
= DECL_TEMPLATE_PARMS (TYPE_NAME (t
));
11719 if (ctx
== error_mark_node
|| name
== error_mark_node
)
11720 return error_mark_node
;
11723 parm_list
= tsubst_template_parms (parm_list
, args
, complain
);
11724 return make_unbound_class_template (ctx
, name
, parm_list
, complain
);
11731 ++cp_unevaluated_operand
;
11732 ++c_inhibit_evaluation_warnings
;
11734 type
= tsubst_expr (TYPEOF_TYPE_EXPR (t
), args
,
11736 /*integral_constant_expression_p=*/false);
11738 --cp_unevaluated_operand
;
11739 --c_inhibit_evaluation_warnings
;
11741 type
= finish_typeof (type
);
11742 return cp_build_qualified_type_real (type
,
11744 | cp_type_quals (type
),
11748 case DECLTYPE_TYPE
:
11752 ++cp_unevaluated_operand
;
11753 ++c_inhibit_evaluation_warnings
;
11755 type
= tsubst_expr (DECLTYPE_TYPE_EXPR (t
), args
,
11757 /*integral_constant_expression_p=*/false);
11759 --cp_unevaluated_operand
;
11760 --c_inhibit_evaluation_warnings
;
11762 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t
))
11763 type
= lambda_capture_field_type (type
);
11764 else if (DECLTYPE_FOR_LAMBDA_PROXY (t
))
11765 type
= lambda_proxy_type (type
);
11767 type
= finish_decltype_type
11768 (type
, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t
), complain
);
11769 return cp_build_qualified_type_real (type
,
11771 | cp_type_quals (type
),
11775 case UNDERLYING_TYPE
:
11777 tree type
= tsubst (UNDERLYING_TYPE_TYPE (t
), args
,
11778 complain
, in_decl
);
11779 return finish_underlying_type (type
);
11782 case TYPE_ARGUMENT_PACK
:
11783 case NONTYPE_ARGUMENT_PACK
:
11785 tree r
= TYPE_P (t
) ? cxx_make_type (code
) : make_node (code
);
11787 tsubst_template_args (ARGUMENT_PACK_ARGS (t
),
11791 SET_ARGUMENT_PACK_ARGS (r
, packed_out
);
11793 /* For template nontype argument packs, also substitute into
11795 if (code
== NONTYPE_ARGUMENT_PACK
)
11796 TREE_TYPE (r
) = tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
11814 /* We should use one of the expression tsubsts for these codes. */
11815 gcc_unreachable ();
11818 sorry ("use of %qs in template", tree_code_name
[(int) code
]);
11819 return error_mark_node
;
11823 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
11824 type of the expression on the left-hand side of the "." or "->"
11828 tsubst_baselink (tree baselink
, tree object_type
,
11829 tree args
, tsubst_flags_t complain
, tree in_decl
)
11832 tree qualifying_scope
;
11835 tree template_args
= 0;
11836 bool template_id_p
= false;
11838 /* A baselink indicates a function from a base class. Both the
11839 BASELINK_ACCESS_BINFO and the base class referenced may
11840 indicate bases of the template class, rather than the
11841 instantiated class. In addition, lookups that were not
11842 ambiguous before may be ambiguous now. Therefore, we perform
11843 the lookup again. */
11844 qualifying_scope
= BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink
));
11845 qualifying_scope
= tsubst (qualifying_scope
, args
,
11846 complain
, in_decl
);
11847 fns
= BASELINK_FUNCTIONS (baselink
);
11848 optype
= tsubst (BASELINK_OPTYPE (baselink
), args
, complain
, in_decl
);
11849 if (TREE_CODE (fns
) == TEMPLATE_ID_EXPR
)
11851 template_id_p
= true;
11852 template_args
= TREE_OPERAND (fns
, 1);
11853 fns
= TREE_OPERAND (fns
, 0);
11855 template_args
= tsubst_template_args (template_args
, args
,
11856 complain
, in_decl
);
11858 name
= DECL_NAME (get_first_fn (fns
));
11859 if (IDENTIFIER_TYPENAME_P (name
))
11860 name
= mangle_conv_op_name_for_type (optype
);
11861 baselink
= lookup_fnfields (qualifying_scope
, name
, /*protect=*/1);
11863 return error_mark_node
;
11865 /* If lookup found a single function, mark it as used at this
11866 point. (If it lookup found multiple functions the one selected
11867 later by overload resolution will be marked as used at that
11869 if (BASELINK_P (baselink
))
11870 fns
= BASELINK_FUNCTIONS (baselink
);
11871 if (!template_id_p
&& !really_overloaded_fn (fns
))
11872 mark_used (OVL_CURRENT (fns
));
11874 /* Add back the template arguments, if present. */
11875 if (BASELINK_P (baselink
) && template_id_p
)
11876 BASELINK_FUNCTIONS (baselink
)
11877 = build_nt (TEMPLATE_ID_EXPR
,
11878 BASELINK_FUNCTIONS (baselink
),
11880 /* Update the conversion operator type. */
11881 BASELINK_OPTYPE (baselink
) = optype
;
11884 object_type
= current_class_type
;
11885 return adjust_result_of_qualified_name_lookup (baselink
,
11890 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
11891 true if the qualified-id will be a postfix-expression in-and-of
11892 itself; false if more of the postfix-expression follows the
11893 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
11897 tsubst_qualified_id (tree qualified_id
, tree args
,
11898 tsubst_flags_t complain
, tree in_decl
,
11899 bool done
, bool address_p
)
11905 tree template_args
;
11907 gcc_assert (TREE_CODE (qualified_id
) == SCOPE_REF
);
11909 /* Figure out what name to look up. */
11910 name
= TREE_OPERAND (qualified_id
, 1);
11911 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
11913 is_template
= true;
11914 template_args
= TREE_OPERAND (name
, 1);
11916 template_args
= tsubst_template_args (template_args
, args
,
11917 complain
, in_decl
);
11918 name
= TREE_OPERAND (name
, 0);
11922 is_template
= false;
11923 template_args
= NULL_TREE
;
11926 /* Substitute into the qualifying scope. When there are no ARGS, we
11927 are just trying to simplify a non-dependent expression. In that
11928 case the qualifying scope may be dependent, and, in any case,
11929 substituting will not help. */
11930 scope
= TREE_OPERAND (qualified_id
, 0);
11933 scope
= tsubst (scope
, args
, complain
, in_decl
);
11934 expr
= tsubst_copy (name
, args
, complain
, in_decl
);
11939 if (dependent_scope_p (scope
))
11942 expr
= build_min_nt (TEMPLATE_ID_EXPR
, expr
, template_args
);
11943 return build_qualified_name (NULL_TREE
, scope
, expr
,
11944 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
));
11947 if (!BASELINK_P (name
) && !DECL_P (expr
))
11949 if (TREE_CODE (expr
) == BIT_NOT_EXPR
)
11951 /* A BIT_NOT_EXPR is used to represent a destructor. */
11952 if (!check_dtor_name (scope
, TREE_OPERAND (expr
, 0)))
11954 error ("qualifying type %qT does not match destructor name ~%qT",
11955 scope
, TREE_OPERAND (expr
, 0));
11956 expr
= error_mark_node
;
11959 expr
= lookup_qualified_name (scope
, complete_dtor_identifier
,
11960 /*is_type_p=*/0, false);
11963 expr
= lookup_qualified_name (scope
, expr
, /*is_type_p=*/0, false);
11964 if (TREE_CODE (TREE_CODE (expr
) == TEMPLATE_DECL
11965 ? DECL_TEMPLATE_RESULT (expr
) : expr
) == TYPE_DECL
)
11967 if (complain
& tf_error
)
11969 error ("dependent-name %qE is parsed as a non-type, but "
11970 "instantiation yields a type", qualified_id
);
11971 inform (input_location
, "say %<typename %E%> if a type is meant", qualified_id
);
11973 return error_mark_node
;
11979 check_accessibility_of_qualified_id (expr
, /*object_type=*/NULL_TREE
,
11981 /* Remember that there was a reference to this entity. */
11985 if (expr
== error_mark_node
|| TREE_CODE (expr
) == TREE_LIST
)
11987 if (complain
& tf_error
)
11988 qualified_name_lookup_error (scope
,
11989 TREE_OPERAND (qualified_id
, 1),
11990 expr
, input_location
);
11991 return error_mark_node
;
11995 expr
= lookup_template_function (expr
, template_args
);
11997 if (expr
== error_mark_node
&& complain
& tf_error
)
11998 qualified_name_lookup_error (scope
, TREE_OPERAND (qualified_id
, 1),
11999 expr
, input_location
);
12000 else if (TYPE_P (scope
))
12002 expr
= (adjust_result_of_qualified_name_lookup
12003 (expr
, scope
, current_class_type
));
12004 expr
= (finish_qualified_id_expr
12005 (scope
, expr
, done
, address_p
&& PTRMEM_OK_P (qualified_id
),
12006 QUALIFIED_NAME_IS_TEMPLATE (qualified_id
),
12007 /*template_arg_p=*/false));
12010 /* Expressions do not generally have reference type. */
12011 if (TREE_CODE (expr
) != SCOPE_REF
12012 /* However, if we're about to form a pointer-to-member, we just
12013 want the referenced member referenced. */
12014 && TREE_CODE (expr
) != OFFSET_REF
)
12015 expr
= convert_from_reference (expr
);
12020 /* Like tsubst, but deals with expressions. This function just replaces
12021 template parms; to finish processing the resultant expression, use
12022 tsubst_copy_and_build or tsubst_expr. */
12025 tsubst_copy (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
)
12027 enum tree_code code
;
12030 if (t
== NULL_TREE
|| t
== error_mark_node
|| args
== NULL_TREE
)
12033 code
= TREE_CODE (t
);
12038 r
= retrieve_local_specialization (t
);
12044 /* We get here for a use of 'this' in an NSDMI. */
12045 if (DECL_NAME (t
) == this_identifier
12046 && at_function_scope_p ()
12047 && DECL_CONSTRUCTOR_P (current_function_decl
))
12048 return current_class_ptr
;
12050 /* This can happen for a parameter name used later in a function
12051 declaration (such as in a late-specified return type). Just
12052 make a dummy decl, since it's only used for its type. */
12053 gcc_assert (cp_unevaluated_operand
!= 0);
12054 /* We copy T because want to tsubst the PARM_DECL only,
12055 not the following PARM_DECLs that are chained to T. */
12057 r
= tsubst_decl (c
, args
, complain
);
12058 /* Give it the template pattern as its context; its true context
12059 hasn't been instantiated yet and this is good enough for
12061 DECL_CONTEXT (r
) = DECL_CONTEXT (t
);
12064 if (TREE_CODE (r
) == ARGUMENT_PACK_SELECT
)
12065 r
= ARGUMENT_PACK_SELECT_ARG (r
);
12074 if (DECL_TEMPLATE_PARM_P (t
))
12075 return tsubst_copy (DECL_INITIAL (t
), args
, complain
, in_decl
);
12076 /* There is no need to substitute into namespace-scope
12078 if (DECL_NAMESPACE_SCOPE_P (t
))
12080 /* If ARGS is NULL, then T is known to be non-dependent. */
12081 if (args
== NULL_TREE
)
12082 return integral_constant_value (t
);
12084 /* Unfortunately, we cannot just call lookup_name here.
12087 template <int I> int f() {
12089 struct S { void g() { E e = a; } };
12092 When we instantiate f<7>::S::g(), say, lookup_name is not
12093 clever enough to find f<7>::a. */
12095 = tsubst_aggr_type (TREE_TYPE (t
), args
, complain
, in_decl
,
12096 /*entering_scope=*/0);
12098 for (v
= TYPE_VALUES (enum_type
);
12100 v
= TREE_CHAIN (v
))
12101 if (TREE_PURPOSE (v
) == DECL_NAME (t
))
12102 return TREE_VALUE (v
);
12104 /* We didn't find the name. That should never happen; if
12105 name-lookup found it during preliminary parsing, we
12106 should find it again here during instantiation. */
12107 gcc_unreachable ();
12112 if (DECL_CONTEXT (t
))
12116 ctx
= tsubst_aggr_type (DECL_CONTEXT (t
), args
, complain
, in_decl
,
12117 /*entering_scope=*/1);
12118 if (ctx
!= DECL_CONTEXT (t
))
12120 tree r
= lookup_field (ctx
, DECL_NAME (t
), 0, false);
12123 if (complain
& tf_error
)
12124 error ("using invalid field %qD", t
);
12125 return error_mark_node
;
12134 case FUNCTION_DECL
:
12135 if ((DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
12136 || local_variable_p (t
))
12137 t
= tsubst (t
, args
, complain
, in_decl
);
12141 case NAMESPACE_DECL
:
12145 /* An OVERLOAD will always be a non-dependent overload set; an
12146 overload set from function scope will just be represented with an
12147 IDENTIFIER_NODE, and from class scope with a BASELINK. */
12148 gcc_assert (!uses_template_parms (t
));
12152 return tsubst_baselink (t
, current_class_type
, args
, complain
, in_decl
);
12154 case TEMPLATE_DECL
:
12155 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
12156 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t
)),
12157 args
, complain
, in_decl
);
12158 else if (DECL_FUNCTION_TEMPLATE_P (t
) && DECL_MEMBER_TEMPLATE_P (t
))
12159 return tsubst (t
, args
, complain
, in_decl
);
12160 else if (DECL_CLASS_SCOPE_P (t
)
12161 && uses_template_parms (DECL_CONTEXT (t
)))
12163 /* Template template argument like the following example need
12166 template <template <class> class TT> struct C {};
12167 template <class T> struct D {
12168 template <class U> struct E {};
12173 We are processing the template argument `E' in #1 for
12174 the template instantiation #2. Originally, `E' is a
12175 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
12176 have to substitute this with one having context `D<int>'. */
12178 tree context
= tsubst (DECL_CONTEXT (t
), args
, complain
, in_decl
);
12179 return lookup_field (context
, DECL_NAME(t
), 0, false);
12182 /* Ordinary template template argument. */
12186 case REINTERPRET_CAST_EXPR
:
12187 case CONST_CAST_EXPR
:
12188 case STATIC_CAST_EXPR
:
12189 case DYNAMIC_CAST_EXPR
:
12190 case IMPLICIT_CONV_EXPR
:
12194 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12195 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12198 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
12204 ++cp_unevaluated_operand
;
12205 ++c_inhibit_evaluation_warnings
;
12206 /* We only want to compute the number of arguments. */
12207 expanded
= tsubst_pack_expansion (TREE_OPERAND (t
, 0), args
,
12208 complain
, in_decl
);
12209 --cp_unevaluated_operand
;
12210 --c_inhibit_evaluation_warnings
;
12212 if (TREE_CODE (expanded
) == TREE_VEC
)
12213 len
= TREE_VEC_LENGTH (expanded
);
12215 if (expanded
== error_mark_node
)
12216 return error_mark_node
;
12217 else if (PACK_EXPANSION_P (expanded
)
12218 || (TREE_CODE (expanded
) == TREE_VEC
12220 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded
, len
-1))))
12222 if (TREE_CODE (expanded
) == TREE_VEC
)
12223 expanded
= TREE_VEC_ELT (expanded
, len
- 1);
12225 if (TYPE_P (expanded
))
12226 return cxx_sizeof_or_alignof_type (expanded
, SIZEOF_EXPR
,
12227 complain
& tf_error
);
12229 return cxx_sizeof_or_alignof_expr (expanded
, SIZEOF_EXPR
,
12230 complain
& tf_error
);
12233 return build_int_cst (size_type_node
, len
);
12239 case TRUTH_NOT_EXPR
:
12242 case UNARY_PLUS_EXPR
: /* Unary + */
12244 case AT_ENCODE_EXPR
:
12248 case REALPART_EXPR
:
12249 case IMAGPART_EXPR
:
12251 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12252 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
));
12254 case COMPONENT_REF
:
12259 object
= tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
);
12260 name
= TREE_OPERAND (t
, 1);
12261 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
12263 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12264 complain
, in_decl
);
12265 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12267 else if (TREE_CODE (name
) == SCOPE_REF
12268 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
12270 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12271 complain
, in_decl
);
12272 name
= TREE_OPERAND (name
, 1);
12273 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
,
12274 complain
, in_decl
);
12275 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
12276 name
= build_qualified_name (/*type=*/NULL_TREE
,
12278 /*template_p=*/false);
12280 else if (BASELINK_P (name
))
12281 name
= tsubst_baselink (name
,
12282 non_reference (TREE_TYPE (object
)),
12286 name
= tsubst_copy (name
, args
, complain
, in_decl
);
12287 return build_nt (COMPONENT_REF
, object
, name
, NULL_TREE
);
12293 case TRUNC_DIV_EXPR
:
12294 case CEIL_DIV_EXPR
:
12295 case FLOOR_DIV_EXPR
:
12296 case ROUND_DIV_EXPR
:
12297 case EXACT_DIV_EXPR
:
12301 case TRUNC_MOD_EXPR
:
12302 case FLOOR_MOD_EXPR
:
12303 case TRUTH_ANDIF_EXPR
:
12304 case TRUTH_ORIF_EXPR
:
12305 case TRUTH_AND_EXPR
:
12306 case TRUTH_OR_EXPR
:
12319 case COMPOUND_EXPR
:
12322 case PREDECREMENT_EXPR
:
12323 case PREINCREMENT_EXPR
:
12324 case POSTDECREMENT_EXPR
:
12325 case POSTINCREMENT_EXPR
:
12327 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12328 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12331 return build_qualified_name (/*type=*/NULL_TREE
,
12332 tsubst_copy (TREE_OPERAND (t
, 0),
12333 args
, complain
, in_decl
),
12334 tsubst_copy (TREE_OPERAND (t
, 1),
12335 args
, complain
, in_decl
),
12336 QUALIFIED_NAME_IS_TEMPLATE (t
));
12341 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12342 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12343 NULL_TREE
, NULL_TREE
);
12347 int n
= VL_EXP_OPERAND_LENGTH (t
);
12348 tree result
= build_vl_exp (CALL_EXPR
, n
);
12350 for (i
= 0; i
< n
; i
++)
12351 TREE_OPERAND (t
, i
) = tsubst_copy (TREE_OPERAND (t
, i
), args
,
12352 complain
, in_decl
);
12358 case PSEUDO_DTOR_EXPR
:
12361 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12362 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12363 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12364 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
12371 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12372 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
12373 tsubst_copy (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
12374 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
12381 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12382 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12383 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
12384 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
12388 case TEMPLATE_ID_EXPR
:
12390 /* Substituted template arguments */
12391 tree fn
= TREE_OPERAND (t
, 0);
12392 tree targs
= TREE_OPERAND (t
, 1);
12394 fn
= tsubst_copy (fn
, args
, complain
, in_decl
);
12396 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
12398 return lookup_template_function (fn
, targs
);
12403 tree purpose
, value
, chain
;
12405 if (t
== void_list_node
)
12408 purpose
= TREE_PURPOSE (t
);
12410 purpose
= tsubst_copy (purpose
, args
, complain
, in_decl
);
12411 value
= TREE_VALUE (t
);
12413 value
= tsubst_copy (value
, args
, complain
, in_decl
);
12414 chain
= TREE_CHAIN (t
);
12415 if (chain
&& chain
!= void_type_node
)
12416 chain
= tsubst_copy (chain
, args
, complain
, in_decl
);
12417 if (purpose
== TREE_PURPOSE (t
)
12418 && value
== TREE_VALUE (t
)
12419 && chain
== TREE_CHAIN (t
))
12421 return tree_cons (purpose
, value
, chain
);
12426 case ENUMERAL_TYPE
:
12428 case TEMPLATE_TYPE_PARM
:
12429 case TEMPLATE_TEMPLATE_PARM
:
12430 case BOUND_TEMPLATE_TEMPLATE_PARM
:
12431 case TEMPLATE_PARM_INDEX
:
12433 case REFERENCE_TYPE
:
12435 case FUNCTION_TYPE
:
12438 case TYPENAME_TYPE
:
12439 case UNBOUND_CLASS_TEMPLATE
:
12441 case DECLTYPE_TYPE
:
12443 return tsubst (t
, args
, complain
, in_decl
);
12445 case IDENTIFIER_NODE
:
12446 if (IDENTIFIER_TYPENAME_P (t
))
12448 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12449 return mangle_conv_op_name_for_type (new_type
);
12455 /* This is handled by tsubst_copy_and_build. */
12456 gcc_unreachable ();
12459 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
,
12461 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
12463 case CLEANUP_POINT_EXPR
:
12464 /* We shouldn't have built any of these during initial template
12465 generation. Instead, they should be built during instantiation
12466 in response to the saved STMT_IS_FULL_EXPR_P setting. */
12467 gcc_unreachable ();
12471 (code
, tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
12472 tsubst_copy (TREE_OPERAND (t
, 0), args
, complain
, in_decl
),
12473 tsubst_copy (TREE_OPERAND (t
, 1), args
, complain
, in_decl
));
12474 PTRMEM_OK_P (r
) = PTRMEM_OK_P (t
);
12475 mark_used (TREE_OPERAND (r
, 1));
12478 case EXPR_PACK_EXPANSION
:
12479 error ("invalid use of pack expansion expression");
12480 return error_mark_node
;
12482 case NONTYPE_ARGUMENT_PACK
:
12483 error ("use %<...%> to expand argument pack");
12484 return error_mark_node
;
12491 /* Instantiate any typedefs in the type. */
12492 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
12493 r
= fold_convert (type
, t
);
12494 gcc_assert (TREE_CODE (r
) == code
);
12499 /* These can sometimes show up in a partial instantiation, but never
12500 involve template parms. */
12501 gcc_assert (!uses_template_parms (t
));
12505 /* We shouldn't get here, but keep going if !ENABLE_CHECKING. */
12506 gcc_checking_assert (false);
12511 /* Like tsubst_copy, but specifically for OpenMP clauses. */
12514 tsubst_omp_clauses (tree clauses
, tree args
, tsubst_flags_t complain
,
12517 tree new_clauses
= NULL
, nc
, oc
;
12519 for (oc
= clauses
; oc
; oc
= OMP_CLAUSE_CHAIN (oc
))
12521 nc
= copy_node (oc
);
12522 OMP_CLAUSE_CHAIN (nc
) = new_clauses
;
12525 switch (OMP_CLAUSE_CODE (nc
))
12527 case OMP_CLAUSE_LASTPRIVATE
:
12528 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc
))
12530 OMP_CLAUSE_LASTPRIVATE_STMT (nc
) = push_stmt_list ();
12531 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc
), args
, complain
,
12532 in_decl
, /*integral_constant_expression_p=*/false);
12533 OMP_CLAUSE_LASTPRIVATE_STMT (nc
)
12534 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc
));
12537 case OMP_CLAUSE_PRIVATE
:
12538 case OMP_CLAUSE_SHARED
:
12539 case OMP_CLAUSE_FIRSTPRIVATE
:
12540 case OMP_CLAUSE_REDUCTION
:
12541 case OMP_CLAUSE_COPYIN
:
12542 case OMP_CLAUSE_COPYPRIVATE
:
12543 case OMP_CLAUSE_IF
:
12544 case OMP_CLAUSE_NUM_THREADS
:
12545 case OMP_CLAUSE_SCHEDULE
:
12546 case OMP_CLAUSE_COLLAPSE
:
12547 case OMP_CLAUSE_FINAL
:
12548 OMP_CLAUSE_OPERAND (nc
, 0)
12549 = tsubst_expr (OMP_CLAUSE_OPERAND (oc
, 0), args
, complain
,
12550 in_decl
, /*integral_constant_expression_p=*/false);
12552 case OMP_CLAUSE_NOWAIT
:
12553 case OMP_CLAUSE_ORDERED
:
12554 case OMP_CLAUSE_DEFAULT
:
12555 case OMP_CLAUSE_UNTIED
:
12556 case OMP_CLAUSE_MERGEABLE
:
12559 gcc_unreachable ();
12563 return finish_omp_clauses (nreverse (new_clauses
));
12566 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
12569 tsubst_copy_asm_operands (tree t
, tree args
, tsubst_flags_t complain
,
12572 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
12574 tree purpose
, value
, chain
;
12579 if (TREE_CODE (t
) != TREE_LIST
)
12580 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
12581 /*function_p=*/false,
12582 /*integral_constant_expression_p=*/false);
12584 if (t
== void_list_node
)
12587 purpose
= TREE_PURPOSE (t
);
12589 purpose
= RECUR (purpose
);
12590 value
= TREE_VALUE (t
);
12593 if (TREE_CODE (value
) != LABEL_DECL
)
12594 value
= RECUR (value
);
12597 value
= lookup_label (DECL_NAME (value
));
12598 gcc_assert (TREE_CODE (value
) == LABEL_DECL
);
12599 TREE_USED (value
) = 1;
12602 chain
= TREE_CHAIN (t
);
12603 if (chain
&& chain
!= void_type_node
)
12604 chain
= RECUR (chain
);
12605 return tree_cons (purpose
, value
, chain
);
12609 /* Substitute one OMP_FOR iterator. */
12612 tsubst_omp_for_iterator (tree t
, int i
, tree declv
, tree initv
,
12613 tree condv
, tree incrv
, tree
*clauses
,
12614 tree args
, tsubst_flags_t complain
, tree in_decl
,
12615 bool integral_constant_expression_p
)
12617 #define RECUR(NODE) \
12618 tsubst_expr ((NODE), args, complain, in_decl, \
12619 integral_constant_expression_p)
12620 tree decl
, init
, cond
, incr
, auto_node
;
12622 init
= TREE_VEC_ELT (OMP_FOR_INIT (t
), i
);
12623 gcc_assert (TREE_CODE (init
) == MODIFY_EXPR
);
12624 decl
= RECUR (TREE_OPERAND (init
, 0));
12625 init
= TREE_OPERAND (init
, 1);
12626 auto_node
= type_uses_auto (TREE_TYPE (decl
));
12627 if (auto_node
&& init
)
12629 tree init_expr
= init
;
12630 if (TREE_CODE (init_expr
) == DECL_EXPR
)
12631 init_expr
= DECL_INITIAL (DECL_EXPR_DECL (init_expr
));
12632 init_expr
= RECUR (init_expr
);
12634 = do_auto_deduction (TREE_TYPE (decl
), init_expr
, auto_node
);
12636 gcc_assert (!type_dependent_expression_p (decl
));
12638 if (!CLASS_TYPE_P (TREE_TYPE (decl
)))
12640 cond
= RECUR (TREE_VEC_ELT (OMP_FOR_COND (t
), i
));
12641 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12642 if (TREE_CODE (incr
) == MODIFY_EXPR
)
12643 incr
= build_x_modify_expr (RECUR (TREE_OPERAND (incr
, 0)), NOP_EXPR
,
12644 RECUR (TREE_OPERAND (incr
, 1)),
12647 incr
= RECUR (incr
);
12648 TREE_VEC_ELT (declv
, i
) = decl
;
12649 TREE_VEC_ELT (initv
, i
) = init
;
12650 TREE_VEC_ELT (condv
, i
) = cond
;
12651 TREE_VEC_ELT (incrv
, i
) = incr
;
12655 if (init
&& TREE_CODE (init
) != DECL_EXPR
)
12658 for (c
= *clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
12660 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
12661 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
12662 && OMP_CLAUSE_DECL (c
) == decl
)
12664 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
12665 && OMP_CLAUSE_DECL (c
) == decl
)
12666 error ("iteration variable %qD should not be firstprivate", decl
);
12667 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
12668 && OMP_CLAUSE_DECL (c
) == decl
)
12669 error ("iteration variable %qD should not be reduction", decl
);
12673 c
= build_omp_clause (input_location
, OMP_CLAUSE_PRIVATE
);
12674 OMP_CLAUSE_DECL (c
) = decl
;
12675 c
= finish_omp_clauses (c
);
12678 OMP_CLAUSE_CHAIN (c
) = *clauses
;
12683 cond
= TREE_VEC_ELT (OMP_FOR_COND (t
), i
);
12684 if (COMPARISON_CLASS_P (cond
))
12685 cond
= build2 (TREE_CODE (cond
), boolean_type_node
,
12686 RECUR (TREE_OPERAND (cond
, 0)),
12687 RECUR (TREE_OPERAND (cond
, 1)));
12689 cond
= RECUR (cond
);
12690 incr
= TREE_VEC_ELT (OMP_FOR_INCR (t
), i
);
12691 switch (TREE_CODE (incr
))
12693 case PREINCREMENT_EXPR
:
12694 case PREDECREMENT_EXPR
:
12695 case POSTINCREMENT_EXPR
:
12696 case POSTDECREMENT_EXPR
:
12697 incr
= build2 (TREE_CODE (incr
), TREE_TYPE (decl
),
12698 RECUR (TREE_OPERAND (incr
, 0)), NULL_TREE
);
12701 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12702 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12704 tree rhs
= TREE_OPERAND (incr
, 1);
12705 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12706 RECUR (TREE_OPERAND (incr
, 0)),
12707 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12708 RECUR (TREE_OPERAND (rhs
, 0)),
12709 RECUR (TREE_OPERAND (rhs
, 1))));
12712 incr
= RECUR (incr
);
12715 if (TREE_CODE (TREE_OPERAND (incr
, 1)) == PLUS_EXPR
12716 || TREE_CODE (TREE_OPERAND (incr
, 1)) == MINUS_EXPR
)
12718 tree lhs
= RECUR (TREE_OPERAND (incr
, 0));
12719 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
), lhs
,
12720 build2 (TREE_CODE (TREE_OPERAND (incr
, 1)),
12721 TREE_TYPE (decl
), lhs
,
12722 RECUR (TREE_OPERAND (incr
, 2))));
12724 else if (TREE_CODE (TREE_OPERAND (incr
, 1)) == NOP_EXPR
12725 && (TREE_CODE (TREE_OPERAND (incr
, 2)) == PLUS_EXPR
12726 || (TREE_CODE (TREE_OPERAND (incr
, 2)) == MINUS_EXPR
)))
12728 tree rhs
= TREE_OPERAND (incr
, 2);
12729 incr
= build2 (MODIFY_EXPR
, TREE_TYPE (decl
),
12730 RECUR (TREE_OPERAND (incr
, 0)),
12731 build2 (TREE_CODE (rhs
), TREE_TYPE (decl
),
12732 RECUR (TREE_OPERAND (rhs
, 0)),
12733 RECUR (TREE_OPERAND (rhs
, 1))));
12736 incr
= RECUR (incr
);
12739 incr
= RECUR (incr
);
12743 TREE_VEC_ELT (declv
, i
) = decl
;
12744 TREE_VEC_ELT (initv
, i
) = init
;
12745 TREE_VEC_ELT (condv
, i
) = cond
;
12746 TREE_VEC_ELT (incrv
, i
) = incr
;
12750 /* Like tsubst_copy for expressions, etc. but also does semantic
12754 tsubst_expr (tree t
, tree args
, tsubst_flags_t complain
, tree in_decl
,
12755 bool integral_constant_expression_p
)
12757 #define RECUR(NODE) \
12758 tsubst_expr ((NODE), args, complain, in_decl, \
12759 integral_constant_expression_p)
12763 if (t
== NULL_TREE
|| t
== error_mark_node
)
12766 if (EXPR_HAS_LOCATION (t
))
12767 input_location
= EXPR_LOCATION (t
);
12768 if (STATEMENT_CODE_P (TREE_CODE (t
)))
12769 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
12771 switch (TREE_CODE (t
))
12773 case STATEMENT_LIST
:
12775 tree_stmt_iterator i
;
12776 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
12777 RECUR (tsi_stmt (i
));
12781 case CTOR_INITIALIZER
:
12782 finish_mem_initializers (tsubst_initializer_list
12783 (TREE_OPERAND (t
, 0), args
));
12787 finish_return_stmt (RECUR (TREE_OPERAND (t
, 0)));
12791 tmp
= RECUR (EXPR_STMT_EXPR (t
));
12792 if (EXPR_STMT_STMT_EXPR_RESULT (t
))
12793 finish_stmt_expr_expr (tmp
, cur_stmt_expr
);
12795 finish_expr_stmt (tmp
);
12799 do_using_directive (USING_STMT_NAMESPACE (t
));
12804 tree decl
, pattern_decl
;
12807 pattern_decl
= decl
= DECL_EXPR_DECL (t
);
12808 if (TREE_CODE (decl
) == LABEL_DECL
)
12809 finish_label_decl (DECL_NAME (decl
));
12810 else if (TREE_CODE (decl
) == USING_DECL
)
12812 tree scope
= USING_DECL_SCOPE (decl
);
12813 tree name
= DECL_NAME (decl
);
12816 scope
= tsubst (scope
, args
, complain
, in_decl
);
12817 decl
= lookup_qualified_name (scope
, name
,
12818 /*is_type_p=*/false,
12819 /*complain=*/false);
12820 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
12821 qualified_name_lookup_error (scope
, name
, decl
, input_location
);
12823 do_local_using_decl (decl
, scope
, name
);
12827 init
= DECL_INITIAL (decl
);
12828 decl
= tsubst (decl
, args
, complain
, in_decl
);
12829 if (decl
!= error_mark_node
)
12831 /* By marking the declaration as instantiated, we avoid
12832 trying to instantiate it. Since instantiate_decl can't
12833 handle local variables, and since we've already done
12834 all that needs to be done, that's the right thing to
12836 if (TREE_CODE (decl
) == VAR_DECL
)
12837 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
12838 if (TREE_CODE (decl
) == VAR_DECL
12839 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
12840 /* Anonymous aggregates are a special case. */
12841 finish_anon_union (decl
);
12842 else if (is_capture_proxy (DECL_EXPR_DECL (t
)))
12844 DECL_CONTEXT (decl
) = current_function_decl
;
12845 insert_capture_proxy (decl
);
12849 int const_init
= false;
12850 maybe_push_decl (decl
);
12851 if (TREE_CODE (decl
) == VAR_DECL
12852 && DECL_PRETTY_FUNCTION_P (decl
))
12854 /* For __PRETTY_FUNCTION__ we have to adjust the
12856 const char *const name
12857 = cxx_printable_name (current_function_decl
, 2);
12858 init
= cp_fname_init (name
, &TREE_TYPE (decl
));
12862 tree t
= RECUR (init
);
12866 /* If we had an initializer but it
12867 instantiated to nothing,
12868 value-initialize the object. This will
12869 only occur when the initializer was a
12870 pack expansion where the parameter packs
12871 used in that expansion were of length
12873 init
= build_value_init (TREE_TYPE (decl
),
12875 if (TREE_CODE (init
) == AGGR_INIT_EXPR
)
12876 init
= get_target_expr_sfinae (init
, complain
);
12882 if (TREE_CODE (decl
) == VAR_DECL
)
12883 const_init
= (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
12885 cp_finish_decl (decl
, init
, const_init
, NULL_TREE
, 0);
12890 /* A DECL_EXPR can also be used as an expression, in the condition
12891 clause of an if/for/while construct. */
12896 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12897 RECUR (FOR_INIT_STMT (t
));
12898 finish_for_init_stmt (stmt
);
12899 tmp
= RECUR (FOR_COND (t
));
12900 finish_for_cond (tmp
, stmt
);
12901 tmp
= RECUR (FOR_EXPR (t
));
12902 finish_for_expr (tmp
, stmt
);
12903 RECUR (FOR_BODY (t
));
12904 finish_for_stmt (stmt
);
12907 case RANGE_FOR_STMT
:
12910 stmt
= begin_for_stmt (NULL_TREE
, NULL_TREE
);
12911 decl
= RANGE_FOR_DECL (t
);
12912 decl
= tsubst (decl
, args
, complain
, in_decl
);
12913 maybe_push_decl (decl
);
12914 expr
= RECUR (RANGE_FOR_EXPR (t
));
12915 stmt
= cp_convert_range_for (stmt
, decl
, expr
);
12916 RECUR (RANGE_FOR_BODY (t
));
12917 finish_for_stmt (stmt
);
12922 stmt
= begin_while_stmt ();
12923 tmp
= RECUR (WHILE_COND (t
));
12924 finish_while_stmt_cond (tmp
, stmt
);
12925 RECUR (WHILE_BODY (t
));
12926 finish_while_stmt (stmt
);
12930 stmt
= begin_do_stmt ();
12931 RECUR (DO_BODY (t
));
12932 finish_do_body (stmt
);
12933 tmp
= RECUR (DO_COND (t
));
12934 finish_do_stmt (tmp
, stmt
);
12938 stmt
= begin_if_stmt ();
12939 tmp
= RECUR (IF_COND (t
));
12940 finish_if_stmt_cond (tmp
, stmt
);
12941 RECUR (THEN_CLAUSE (t
));
12942 finish_then_clause (stmt
);
12944 if (ELSE_CLAUSE (t
))
12946 begin_else_clause (stmt
);
12947 RECUR (ELSE_CLAUSE (t
));
12948 finish_else_clause (stmt
);
12951 finish_if_stmt (stmt
);
12955 if (BIND_EXPR_BODY_BLOCK (t
))
12956 stmt
= begin_function_body ();
12958 stmt
= begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t
)
12959 ? BCS_TRY_BLOCK
: 0);
12961 RECUR (BIND_EXPR_BODY (t
));
12963 if (BIND_EXPR_BODY_BLOCK (t
))
12964 finish_function_body (stmt
);
12966 finish_compound_stmt (stmt
);
12970 finish_break_stmt ();
12973 case CONTINUE_STMT
:
12974 finish_continue_stmt ();
12978 stmt
= begin_switch_stmt ();
12979 tmp
= RECUR (SWITCH_STMT_COND (t
));
12980 finish_switch_cond (tmp
, stmt
);
12981 RECUR (SWITCH_STMT_BODY (t
));
12982 finish_switch_stmt (stmt
);
12985 case CASE_LABEL_EXPR
:
12986 finish_case_label (EXPR_LOCATION (t
),
12987 RECUR (CASE_LOW (t
)),
12988 RECUR (CASE_HIGH (t
)));
12993 tree decl
= LABEL_EXPR_LABEL (t
);
12996 label
= finish_label_stmt (DECL_NAME (decl
));
12997 if (DECL_ATTRIBUTES (decl
) != NULL_TREE
)
12998 cplus_decl_attributes (&label
, DECL_ATTRIBUTES (decl
), 0);
13003 tmp
= GOTO_DESTINATION (t
);
13004 if (TREE_CODE (tmp
) != LABEL_DECL
)
13005 /* Computed goto's must be tsubst'd into. On the other hand,
13006 non-computed gotos must not be; the identifier in question
13007 will have no binding. */
13010 tmp
= DECL_NAME (tmp
);
13011 finish_goto_stmt (tmp
);
13015 tmp
= finish_asm_stmt
13016 (ASM_VOLATILE_P (t
),
13017 RECUR (ASM_STRING (t
)),
13018 tsubst_copy_asm_operands (ASM_OUTPUTS (t
), args
, complain
, in_decl
),
13019 tsubst_copy_asm_operands (ASM_INPUTS (t
), args
, complain
, in_decl
),
13020 tsubst_copy_asm_operands (ASM_CLOBBERS (t
), args
, complain
, in_decl
),
13021 tsubst_copy_asm_operands (ASM_LABELS (t
), args
, complain
, in_decl
));
13023 tree asm_expr
= tmp
;
13024 if (TREE_CODE (asm_expr
) == CLEANUP_POINT_EXPR
)
13025 asm_expr
= TREE_OPERAND (asm_expr
, 0);
13026 ASM_INPUT_P (asm_expr
) = ASM_INPUT_P (t
);
13033 stmt
= begin_try_block ();
13034 RECUR (TRY_STMTS (t
));
13035 finish_cleanup_try_block (stmt
);
13036 finish_cleanup (RECUR (TRY_HANDLERS (t
)), stmt
);
13040 tree compound_stmt
= NULL_TREE
;
13042 if (FN_TRY_BLOCK_P (t
))
13043 stmt
= begin_function_try_block (&compound_stmt
);
13045 stmt
= begin_try_block ();
13047 RECUR (TRY_STMTS (t
));
13049 if (FN_TRY_BLOCK_P (t
))
13050 finish_function_try_block (stmt
);
13052 finish_try_block (stmt
);
13054 RECUR (TRY_HANDLERS (t
));
13055 if (FN_TRY_BLOCK_P (t
))
13056 finish_function_handler_sequence (stmt
, compound_stmt
);
13058 finish_handler_sequence (stmt
);
13064 tree decl
= HANDLER_PARMS (t
);
13068 decl
= tsubst (decl
, args
, complain
, in_decl
);
13069 /* Prevent instantiate_decl from trying to instantiate
13070 this variable. We've already done all that needs to be
13072 if (decl
!= error_mark_node
)
13073 DECL_TEMPLATE_INSTANTIATED (decl
) = 1;
13075 stmt
= begin_handler ();
13076 finish_handler_parms (decl
, stmt
);
13077 RECUR (HANDLER_BODY (t
));
13078 finish_handler (stmt
);
13083 tsubst (TREE_TYPE (t
), args
, complain
, NULL_TREE
);
13086 case STATIC_ASSERT
:
13089 tsubst_expr (STATIC_ASSERT_CONDITION (t
),
13092 /*integral_constant_expression_p=*/true);
13093 finish_static_assert (condition
,
13094 STATIC_ASSERT_MESSAGE (t
),
13095 STATIC_ASSERT_SOURCE_LOCATION (t
),
13096 /*member_p=*/false);
13101 tmp
= tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t
),
13102 args
, complain
, in_decl
);
13103 stmt
= begin_omp_parallel ();
13104 RECUR (OMP_PARALLEL_BODY (t
));
13105 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp
, stmt
))
13106 = OMP_PARALLEL_COMBINED (t
);
13110 tmp
= tsubst_omp_clauses (OMP_TASK_CLAUSES (t
),
13111 args
, complain
, in_decl
);
13112 stmt
= begin_omp_task ();
13113 RECUR (OMP_TASK_BODY (t
));
13114 finish_omp_task (tmp
, stmt
);
13119 tree clauses
, body
, pre_body
;
13120 tree declv
, initv
, condv
, incrv
;
13123 clauses
= tsubst_omp_clauses (OMP_FOR_CLAUSES (t
),
13124 args
, complain
, in_decl
);
13125 declv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13126 initv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13127 condv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13128 incrv
= make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t
)));
13130 for (i
= 0; i
< TREE_VEC_LENGTH (OMP_FOR_INIT (t
)); i
++)
13131 tsubst_omp_for_iterator (t
, i
, declv
, initv
, condv
, incrv
,
13132 &clauses
, args
, complain
, in_decl
,
13133 integral_constant_expression_p
);
13135 stmt
= begin_omp_structured_block ();
13137 for (i
= 0; i
< TREE_VEC_LENGTH (initv
); i
++)
13138 if (TREE_VEC_ELT (initv
, i
) == NULL
13139 || TREE_CODE (TREE_VEC_ELT (initv
, i
)) != DECL_EXPR
)
13140 TREE_VEC_ELT (initv
, i
) = RECUR (TREE_VEC_ELT (initv
, i
));
13141 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv
, i
))))
13143 tree init
= RECUR (TREE_VEC_ELT (initv
, i
));
13144 gcc_assert (init
== TREE_VEC_ELT (declv
, i
));
13145 TREE_VEC_ELT (initv
, i
) = NULL_TREE
;
13149 tree decl_expr
= TREE_VEC_ELT (initv
, i
);
13150 tree init
= DECL_INITIAL (DECL_EXPR_DECL (decl_expr
));
13151 gcc_assert (init
!= NULL
);
13152 TREE_VEC_ELT (initv
, i
) = RECUR (init
);
13153 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = NULL
;
13155 DECL_INITIAL (DECL_EXPR_DECL (decl_expr
)) = init
;
13158 pre_body
= push_stmt_list ();
13159 RECUR (OMP_FOR_PRE_BODY (t
));
13160 pre_body
= pop_stmt_list (pre_body
);
13162 body
= push_stmt_list ();
13163 RECUR (OMP_FOR_BODY (t
));
13164 body
= pop_stmt_list (body
);
13166 t
= finish_omp_for (EXPR_LOCATION (t
), declv
, initv
, condv
, incrv
,
13167 body
, pre_body
, clauses
);
13169 add_stmt (finish_omp_structured_block (stmt
));
13175 tmp
= tsubst_omp_clauses (OMP_CLAUSES (t
), args
, complain
, in_decl
);
13176 stmt
= push_stmt_list ();
13177 RECUR (OMP_BODY (t
));
13178 stmt
= pop_stmt_list (stmt
);
13181 OMP_BODY (t
) = stmt
;
13182 OMP_CLAUSES (t
) = tmp
;
13190 stmt
= push_stmt_list ();
13191 RECUR (OMP_BODY (t
));
13192 stmt
= pop_stmt_list (stmt
);
13195 OMP_BODY (t
) = stmt
;
13200 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t
));
13201 if (TREE_CODE (TREE_OPERAND (t
, 1)) != MODIFY_EXPR
)
13203 tree op1
= TREE_OPERAND (t
, 1);
13204 tree rhs1
= NULL_TREE
;
13206 if (TREE_CODE (op1
) == COMPOUND_EXPR
)
13208 rhs1
= RECUR (TREE_OPERAND (op1
, 0));
13209 op1
= TREE_OPERAND (op1
, 1);
13211 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13212 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13213 finish_omp_atomic (OMP_ATOMIC
, TREE_CODE (op1
), lhs
, rhs
,
13214 NULL_TREE
, NULL_TREE
, rhs1
);
13218 tree op1
= TREE_OPERAND (t
, 1);
13219 tree v
= NULL_TREE
, lhs
, rhs
= NULL_TREE
, lhs1
= NULL_TREE
;
13220 tree rhs1
= NULL_TREE
;
13221 enum tree_code code
= TREE_CODE (TREE_OPERAND (op1
, 1));
13222 enum tree_code opcode
= NOP_EXPR
;
13223 if (code
== OMP_ATOMIC_READ
)
13225 v
= RECUR (TREE_OPERAND (op1
, 0));
13226 lhs
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13228 else if (code
== OMP_ATOMIC_CAPTURE_OLD
13229 || code
== OMP_ATOMIC_CAPTURE_NEW
)
13231 tree op11
= TREE_OPERAND (TREE_OPERAND (op1
, 1), 1);
13232 v
= RECUR (TREE_OPERAND (op1
, 0));
13233 lhs1
= RECUR (TREE_OPERAND (TREE_OPERAND (op1
, 1), 0));
13234 if (TREE_CODE (op11
) == COMPOUND_EXPR
)
13236 rhs1
= RECUR (TREE_OPERAND (op11
, 0));
13237 op11
= TREE_OPERAND (op11
, 1);
13239 lhs
= RECUR (TREE_OPERAND (op11
, 0));
13240 rhs
= RECUR (TREE_OPERAND (op11
, 1));
13241 opcode
= TREE_CODE (op11
);
13246 lhs
= RECUR (TREE_OPERAND (op1
, 0));
13247 rhs
= RECUR (TREE_OPERAND (op1
, 1));
13249 finish_omp_atomic (code
, opcode
, lhs
, rhs
, v
, lhs1
, rhs1
);
13253 case TRANSACTION_EXPR
:
13256 flags
|= (TRANSACTION_EXPR_OUTER (t
) ? TM_STMT_ATTR_OUTER
: 0);
13257 flags
|= (TRANSACTION_EXPR_RELAXED (t
) ? TM_STMT_ATTR_RELAXED
: 0);
13259 if (TRANSACTION_EXPR_IS_STMT (t
))
13261 tree body
= TRANSACTION_EXPR_BODY (t
);
13262 tree noex
= NULL_TREE
;
13263 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
13265 noex
= MUST_NOT_THROW_COND (body
);
13266 if (noex
== NULL_TREE
)
13267 noex
= boolean_true_node
;
13268 body
= TREE_OPERAND (body
, 0);
13270 stmt
= begin_transaction_stmt (input_location
, NULL
, flags
);
13272 finish_transaction_stmt (stmt
, NULL
, flags
, RECUR (noex
));
13276 stmt
= build_transaction_expr (EXPR_LOCATION (t
),
13277 RECUR (TRANSACTION_EXPR_BODY (t
)),
13284 case MUST_NOT_THROW_EXPR
:
13285 return build_must_not_throw_expr (RECUR (TREE_OPERAND (t
, 0)),
13286 RECUR (MUST_NOT_THROW_COND (t
)));
13288 case EXPR_PACK_EXPANSION
:
13289 error ("invalid use of pack expansion expression");
13290 return error_mark_node
;
13292 case NONTYPE_ARGUMENT_PACK
:
13293 error ("use %<...%> to expand argument pack");
13294 return error_mark_node
;
13297 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t
)));
13299 return tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13300 /*function_p=*/false,
13301 integral_constant_expression_p
);
13308 /* T is a postfix-expression that is not being used in a function
13309 call. Return the substituted version of T. */
13312 tsubst_non_call_postfix_expression (tree t
, tree args
,
13313 tsubst_flags_t complain
,
13316 if (TREE_CODE (t
) == SCOPE_REF
)
13317 t
= tsubst_qualified_id (t
, args
, complain
, in_decl
,
13318 /*done=*/false, /*address_p=*/false);
13320 t
= tsubst_copy_and_build (t
, args
, complain
, in_decl
,
13321 /*function_p=*/false,
13322 /*integral_constant_expression_p=*/false);
13327 /* Like tsubst but deals with expressions and performs semantic
13328 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
13331 tsubst_copy_and_build (tree t
,
13333 tsubst_flags_t complain
,
13336 bool integral_constant_expression_p
)
13338 #define RECUR(NODE) \
13339 tsubst_copy_and_build (NODE, args, complain, in_decl, \
13340 /*function_p=*/false, \
13341 integral_constant_expression_p)
13345 if (t
== NULL_TREE
|| t
== error_mark_node
)
13348 switch (TREE_CODE (t
))
13352 /* Fall through. */
13353 case IDENTIFIER_NODE
:
13357 bool non_integral_constant_expression_p
;
13358 const char *error_msg
;
13360 if (IDENTIFIER_TYPENAME_P (t
))
13362 tree new_type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13363 t
= mangle_conv_op_name_for_type (new_type
);
13366 /* Look up the name. */
13367 decl
= lookup_name (t
);
13369 /* By convention, expressions use ERROR_MARK_NODE to indicate
13370 failure, not NULL_TREE. */
13371 if (decl
== NULL_TREE
)
13372 decl
= error_mark_node
;
13374 decl
= finish_id_expression (t
, decl
, NULL_TREE
,
13376 integral_constant_expression_p
,
13377 /*allow_non_integral_constant_expression_p=*/(cxx_dialect
>= cxx0x
),
13378 &non_integral_constant_expression_p
,
13379 /*template_p=*/false,
13381 /*address_p=*/false,
13382 /*template_arg_p=*/false,
13387 if (!function_p
&& TREE_CODE (decl
) == IDENTIFIER_NODE
)
13389 if (complain
& tf_error
)
13390 unqualified_name_lookup_error (decl
);
13391 decl
= error_mark_node
;
13396 case TEMPLATE_ID_EXPR
:
13399 tree templ
= RECUR (TREE_OPERAND (t
, 0));
13400 tree targs
= TREE_OPERAND (t
, 1);
13403 targs
= tsubst_template_args (targs
, args
, complain
, in_decl
);
13405 if (TREE_CODE (templ
) == COMPONENT_REF
)
13407 object
= TREE_OPERAND (templ
, 0);
13408 templ
= TREE_OPERAND (templ
, 1);
13411 object
= NULL_TREE
;
13412 templ
= lookup_template_function (templ
, targs
);
13415 return build3 (COMPONENT_REF
, TREE_TYPE (templ
),
13416 object
, templ
, NULL_TREE
);
13418 return baselink_for_fns (templ
);
13423 tree r
= RECUR (TREE_OPERAND (t
, 0));
13425 if (REFERENCE_REF_P (t
))
13427 /* A type conversion to reference type will be enclosed in
13428 such an indirect ref, but the substitution of the cast
13429 will have also added such an indirect ref. */
13430 if (TREE_CODE (TREE_TYPE (r
)) == REFERENCE_TYPE
)
13431 r
= convert_from_reference (r
);
13434 r
= build_x_indirect_ref (r
, RO_UNARY_STAR
, complain
);
13440 (tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13441 RECUR (TREE_OPERAND (t
, 0)));
13443 case IMPLICIT_CONV_EXPR
:
13445 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13446 tree expr
= RECUR (TREE_OPERAND (t
, 0));
13447 int flags
= LOOKUP_IMPLICIT
;
13448 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t
))
13449 flags
= LOOKUP_NORMAL
;
13450 return perform_implicit_conversion_flags (type
, expr
, complain
,
13457 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
),
13458 RECUR (TREE_OPERAND (t
, 0)));
13461 case REINTERPRET_CAST_EXPR
:
13462 case CONST_CAST_EXPR
:
13463 case DYNAMIC_CAST_EXPR
:
13464 case STATIC_CAST_EXPR
:
13469 type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
13470 if (integral_constant_expression_p
13471 && !cast_valid_in_integral_constant_expression_p (type
))
13473 if (complain
& tf_error
)
13474 error ("a cast to a type other than an integral or "
13475 "enumeration type cannot appear in a constant-expression");
13476 return error_mark_node
;
13479 op
= RECUR (TREE_OPERAND (t
, 0));
13481 switch (TREE_CODE (t
))
13484 return build_functional_cast (type
, op
, complain
);
13485 case REINTERPRET_CAST_EXPR
:
13486 return build_reinterpret_cast (type
, op
, complain
);
13487 case CONST_CAST_EXPR
:
13488 return build_const_cast (type
, op
, complain
);
13489 case DYNAMIC_CAST_EXPR
:
13490 return build_dynamic_cast (type
, op
, complain
);
13491 case STATIC_CAST_EXPR
:
13492 return build_static_cast (type
, op
, complain
);
13494 gcc_unreachable ();
13498 case POSTDECREMENT_EXPR
:
13499 case POSTINCREMENT_EXPR
:
13500 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13501 args
, complain
, in_decl
);
13502 return build_x_unary_op (TREE_CODE (t
), op1
, complain
);
13504 case PREDECREMENT_EXPR
:
13505 case PREINCREMENT_EXPR
:
13509 case TRUTH_NOT_EXPR
:
13510 case UNARY_PLUS_EXPR
: /* Unary + */
13511 case REALPART_EXPR
:
13512 case IMAGPART_EXPR
:
13513 return build_x_unary_op (TREE_CODE (t
), RECUR (TREE_OPERAND (t
, 0)),
13516 case FIX_TRUNC_EXPR
:
13517 return cp_build_unary_op (FIX_TRUNC_EXPR
, RECUR (TREE_OPERAND (t
, 0)),
13521 op1
= TREE_OPERAND (t
, 0);
13522 if (TREE_CODE (op1
) == LABEL_DECL
)
13523 return finish_label_address_expr (DECL_NAME (op1
),
13524 EXPR_LOCATION (op1
));
13525 if (TREE_CODE (op1
) == SCOPE_REF
)
13526 op1
= tsubst_qualified_id (op1
, args
, complain
, in_decl
,
13527 /*done=*/true, /*address_p=*/true);
13529 op1
= tsubst_non_call_postfix_expression (op1
, args
, complain
,
13531 return build_x_unary_op (ADDR_EXPR
, op1
, complain
);
13536 case TRUNC_DIV_EXPR
:
13537 case CEIL_DIV_EXPR
:
13538 case FLOOR_DIV_EXPR
:
13539 case ROUND_DIV_EXPR
:
13540 case EXACT_DIV_EXPR
:
13544 case TRUNC_MOD_EXPR
:
13545 case FLOOR_MOD_EXPR
:
13546 case TRUTH_ANDIF_EXPR
:
13547 case TRUTH_ORIF_EXPR
:
13548 case TRUTH_AND_EXPR
:
13549 case TRUTH_OR_EXPR
:
13565 tree r
= build_x_binary_op
13567 RECUR (TREE_OPERAND (t
, 0)),
13568 (TREE_NO_WARNING (TREE_OPERAND (t
, 0))
13570 : TREE_CODE (TREE_OPERAND (t
, 0))),
13571 RECUR (TREE_OPERAND (t
, 1)),
13572 (TREE_NO_WARNING (TREE_OPERAND (t
, 1))
13574 : TREE_CODE (TREE_OPERAND (t
, 1))),
13577 if (EXPR_P (r
) && TREE_NO_WARNING (t
))
13578 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13583 return tsubst_qualified_id (t
, args
, complain
, in_decl
, /*done=*/true,
13584 /*address_p=*/false);
13586 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13587 args
, complain
, in_decl
);
13588 return build_x_array_ref (op1
, RECUR (TREE_OPERAND (t
, 1)), complain
);
13591 if (PACK_EXPANSION_P (TREE_OPERAND (t
, 0)))
13592 return tsubst_copy (t
, args
, complain
, in_decl
);
13596 op1
= TREE_OPERAND (t
, 0);
13599 /* When there are no ARGS, we are trying to evaluate a
13600 non-dependent expression from the parser. Trying to do
13601 the substitutions may not work. */
13603 op1
= TREE_TYPE (op1
);
13607 ++cp_unevaluated_operand
;
13608 ++c_inhibit_evaluation_warnings
;
13609 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13610 /*function_p=*/false,
13611 /*integral_constant_expression_p=*/false);
13612 --cp_unevaluated_operand
;
13613 --c_inhibit_evaluation_warnings
;
13616 return cxx_sizeof_or_alignof_type (op1
, TREE_CODE (t
),
13617 complain
& tf_error
);
13619 return cxx_sizeof_or_alignof_expr (op1
, TREE_CODE (t
),
13620 complain
& tf_error
);
13622 case AT_ENCODE_EXPR
:
13624 op1
= TREE_OPERAND (t
, 0);
13625 ++cp_unevaluated_operand
;
13626 ++c_inhibit_evaluation_warnings
;
13627 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13628 /*function_p=*/false,
13629 /*integral_constant_expression_p=*/false);
13630 --cp_unevaluated_operand
;
13631 --c_inhibit_evaluation_warnings
;
13632 return objc_build_encode_expr (op1
);
13635 case NOEXCEPT_EXPR
:
13636 op1
= TREE_OPERAND (t
, 0);
13637 ++cp_unevaluated_operand
;
13638 ++c_inhibit_evaluation_warnings
;
13639 op1
= tsubst_copy_and_build (op1
, args
, complain
, in_decl
,
13640 /*function_p=*/false,
13641 /*integral_constant_expression_p=*/false);
13642 --cp_unevaluated_operand
;
13643 --c_inhibit_evaluation_warnings
;
13644 return finish_noexcept_expr (op1
, complain
);
13648 tree r
= build_x_modify_expr
13649 (RECUR (TREE_OPERAND (t
, 0)),
13650 TREE_CODE (TREE_OPERAND (t
, 1)),
13651 RECUR (TREE_OPERAND (t
, 2)),
13653 /* TREE_NO_WARNING must be set if either the expression was
13654 parenthesized or it uses an operator such as >>= rather
13655 than plain assignment. In the former case, it was already
13656 set and must be copied. In the latter case,
13657 build_x_modify_expr sets it and it must not be reset
13659 if (TREE_NO_WARNING (t
))
13660 TREE_NO_WARNING (r
) = TREE_NO_WARNING (t
);
13665 op1
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
13666 args
, complain
, in_decl
);
13667 /* Remember that there was a reference to this entity. */
13670 return build_x_arrow (op1
, complain
);
13674 tree placement
= RECUR (TREE_OPERAND (t
, 0));
13675 tree init
= RECUR (TREE_OPERAND (t
, 3));
13676 VEC(tree
,gc
) *placement_vec
;
13677 VEC(tree
,gc
) *init_vec
;
13680 if (placement
== NULL_TREE
)
13681 placement_vec
= NULL
;
13684 placement_vec
= make_tree_vector ();
13685 for (; placement
!= NULL_TREE
; placement
= TREE_CHAIN (placement
))
13686 VEC_safe_push (tree
, gc
, placement_vec
, TREE_VALUE (placement
));
13689 /* If there was an initializer in the original tree, but it
13690 instantiated to an empty list, then we should pass a
13691 non-NULL empty vector to tell build_new that it was an
13692 empty initializer() rather than no initializer. This can
13693 only happen when the initializer is a pack expansion whose
13694 parameter packs are of length zero. */
13695 if (init
== NULL_TREE
&& TREE_OPERAND (t
, 3) == NULL_TREE
)
13699 init_vec
= make_tree_vector ();
13700 if (init
== void_zero_node
)
13701 gcc_assert (init_vec
!= NULL
);
13704 for (; init
!= NULL_TREE
; init
= TREE_CHAIN (init
))
13705 VEC_safe_push (tree
, gc
, init_vec
, TREE_VALUE (init
));
13709 ret
= build_new (&placement_vec
,
13710 tsubst (TREE_OPERAND (t
, 1), args
, complain
, in_decl
),
13711 RECUR (TREE_OPERAND (t
, 2)),
13713 NEW_EXPR_USE_GLOBAL (t
),
13716 if (placement_vec
!= NULL
)
13717 release_tree_vector (placement_vec
);
13718 if (init_vec
!= NULL
)
13719 release_tree_vector (init_vec
);
13725 return delete_sanity
13726 (RECUR (TREE_OPERAND (t
, 0)),
13727 RECUR (TREE_OPERAND (t
, 1)),
13728 DELETE_EXPR_USE_VEC (t
),
13729 DELETE_EXPR_USE_GLOBAL (t
),
13732 case COMPOUND_EXPR
:
13733 return build_x_compound_expr (RECUR (TREE_OPERAND (t
, 0)),
13734 RECUR (TREE_OPERAND (t
, 1)),
13740 VEC(tree
,gc
) *call_args
;
13741 unsigned int nargs
, i
;
13746 function
= CALL_EXPR_FN (t
);
13747 /* When we parsed the expression, we determined whether or
13748 not Koenig lookup should be performed. */
13749 koenig_p
= KOENIG_LOOKUP_P (t
);
13750 if (TREE_CODE (function
) == SCOPE_REF
)
13752 qualified_p
= true;
13753 function
= tsubst_qualified_id (function
, args
, complain
, in_decl
,
13755 /*address_p=*/false);
13757 else if (koenig_p
&& TREE_CODE (function
) == IDENTIFIER_NODE
)
13759 /* Do nothing; calling tsubst_copy_and_build on an identifier
13760 would incorrectly perform unqualified lookup again.
13762 Note that we can also have an IDENTIFIER_NODE if the earlier
13763 unqualified lookup found a member function; in that case
13764 koenig_p will be false and we do want to do the lookup
13765 again to find the instantiated member function.
13767 FIXME but doing that causes c++/15272, so we need to stop
13768 using IDENTIFIER_NODE in that situation. */
13769 qualified_p
= false;
13773 if (TREE_CODE (function
) == COMPONENT_REF
)
13775 tree op
= TREE_OPERAND (function
, 1);
13777 qualified_p
= (TREE_CODE (op
) == SCOPE_REF
13778 || (BASELINK_P (op
)
13779 && BASELINK_QUALIFIED_P (op
)));
13782 qualified_p
= false;
13784 function
= tsubst_copy_and_build (function
, args
, complain
,
13787 integral_constant_expression_p
);
13789 if (BASELINK_P (function
))
13790 qualified_p
= true;
13793 nargs
= call_expr_nargs (t
);
13794 call_args
= make_tree_vector ();
13795 for (i
= 0; i
< nargs
; ++i
)
13797 tree arg
= CALL_EXPR_ARG (t
, i
);
13799 if (!PACK_EXPANSION_P (arg
))
13800 VEC_safe_push (tree
, gc
, call_args
,
13801 RECUR (CALL_EXPR_ARG (t
, i
)));
13804 /* Expand the pack expansion and push each entry onto
13806 arg
= tsubst_pack_expansion (arg
, args
, complain
, in_decl
);
13807 if (TREE_CODE (arg
) == TREE_VEC
)
13809 unsigned int len
, j
;
13811 len
= TREE_VEC_LENGTH (arg
);
13812 for (j
= 0; j
< len
; ++j
)
13814 tree value
= TREE_VEC_ELT (arg
, j
);
13815 if (value
!= NULL_TREE
)
13816 value
= convert_from_reference (value
);
13817 VEC_safe_push (tree
, gc
, call_args
, value
);
13822 /* A partial substitution. Add one entry. */
13823 VEC_safe_push (tree
, gc
, call_args
, arg
);
13828 /* We do not perform argument-dependent lookup if normal
13829 lookup finds a non-function, in accordance with the
13830 expected resolution of DR 218. */
13832 && ((is_overloaded_fn (function
)
13833 /* If lookup found a member function, the Koenig lookup is
13834 not appropriate, even if an unqualified-name was used
13835 to denote the function. */
13836 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function
)))
13837 || TREE_CODE (function
) == IDENTIFIER_NODE
)
13838 /* Only do this when substitution turns a dependent call
13839 into a non-dependent call. */
13840 && type_dependent_expression_p_push (t
)
13841 && !any_type_dependent_arguments_p (call_args
))
13842 function
= perform_koenig_lookup (function
, call_args
, false,
13845 if (TREE_CODE (function
) == IDENTIFIER_NODE
13846 && !any_type_dependent_arguments_p (call_args
))
13848 if (koenig_p
&& (complain
& tf_warning_or_error
))
13850 /* For backwards compatibility and good diagnostics, try
13851 the unqualified lookup again if we aren't in SFINAE
13853 tree unq
= (tsubst_copy_and_build
13854 (function
, args
, complain
, in_decl
, true,
13855 integral_constant_expression_p
));
13856 if (unq
== error_mark_node
)
13857 return error_mark_node
;
13859 if (unq
!= function
)
13862 if (TREE_CODE (fn
) == INDIRECT_REF
)
13863 fn
= TREE_OPERAND (fn
, 0);
13864 if (TREE_CODE (fn
) == COMPONENT_REF
)
13865 fn
= TREE_OPERAND (fn
, 1);
13866 if (is_overloaded_fn (fn
))
13867 fn
= get_first_fn (fn
);
13868 permerror (EXPR_LOC_OR_HERE (t
),
13869 "%qD was not declared in this scope, "
13870 "and no declarations were found by "
13871 "argument-dependent lookup at the point "
13872 "of instantiation", function
);
13874 /* Can't say anything more. */;
13875 else if (DECL_CLASS_SCOPE_P (fn
))
13877 inform (EXPR_LOC_OR_HERE (t
),
13878 "declarations in dependent base %qT are "
13879 "not found by unqualified lookup",
13880 DECL_CLASS_CONTEXT (fn
));
13881 if (current_class_ptr
)
13882 inform (EXPR_LOC_OR_HERE (t
),
13883 "use %<this->%D%> instead", function
);
13885 inform (EXPR_LOC_OR_HERE (t
),
13886 "use %<%T::%D%> instead",
13887 current_class_name
, function
);
13890 inform (0, "%q+D declared here, later in the "
13891 "translation unit", fn
);
13895 if (TREE_CODE (function
) == IDENTIFIER_NODE
)
13897 unqualified_name_lookup_error (function
);
13898 release_tree_vector (call_args
);
13899 return error_mark_node
;
13903 /* Remember that there was a reference to this entity. */
13904 if (DECL_P (function
))
13905 mark_used (function
);
13907 if (TREE_CODE (function
) == OFFSET_REF
)
13908 ret
= build_offset_ref_call_from_tree (function
, &call_args
);
13909 else if (TREE_CODE (function
) == COMPONENT_REF
)
13911 tree instance
= TREE_OPERAND (function
, 0);
13912 tree fn
= TREE_OPERAND (function
, 1);
13914 if (processing_template_decl
13915 && (type_dependent_expression_p (instance
)
13916 || (!BASELINK_P (fn
)
13917 && TREE_CODE (fn
) != FIELD_DECL
)
13918 || type_dependent_expression_p (fn
)
13919 || any_type_dependent_arguments_p (call_args
)))
13920 ret
= build_nt_call_vec (function
, call_args
);
13921 else if (!BASELINK_P (fn
))
13922 ret
= finish_call_expr (function
, &call_args
,
13923 /*disallow_virtual=*/false,
13924 /*koenig_p=*/false,
13927 ret
= (build_new_method_call
13929 &call_args
, NULL_TREE
,
13930 qualified_p
? LOOKUP_NONVIRTUAL
: LOOKUP_NORMAL
,
13935 ret
= finish_call_expr (function
, &call_args
,
13936 /*disallow_virtual=*/qualified_p
,
13940 release_tree_vector (call_args
);
13946 return build_x_conditional_expr
13947 (RECUR (TREE_OPERAND (t
, 0)),
13948 RECUR (TREE_OPERAND (t
, 1)),
13949 RECUR (TREE_OPERAND (t
, 2)),
13952 case PSEUDO_DTOR_EXPR
:
13953 return finish_pseudo_destructor_expr
13954 (RECUR (TREE_OPERAND (t
, 0)),
13955 RECUR (TREE_OPERAND (t
, 1)),
13956 tsubst (TREE_OPERAND (t
, 2), args
, complain
, in_decl
));
13960 tree purpose
, value
, chain
;
13962 if (t
== void_list_node
)
13965 if ((TREE_PURPOSE (t
) && PACK_EXPANSION_P (TREE_PURPOSE (t
)))
13966 || (TREE_VALUE (t
) && PACK_EXPANSION_P (TREE_VALUE (t
))))
13968 /* We have pack expansions, so expand those and
13969 create a new list out of it. */
13970 tree purposevec
= NULL_TREE
;
13971 tree valuevec
= NULL_TREE
;
13975 /* Expand the argument expressions. */
13976 if (TREE_PURPOSE (t
))
13977 purposevec
= tsubst_pack_expansion (TREE_PURPOSE (t
), args
,
13978 complain
, in_decl
);
13979 if (TREE_VALUE (t
))
13980 valuevec
= tsubst_pack_expansion (TREE_VALUE (t
), args
,
13981 complain
, in_decl
);
13983 /* Build the rest of the list. */
13984 chain
= TREE_CHAIN (t
);
13985 if (chain
&& chain
!= void_type_node
)
13986 chain
= RECUR (chain
);
13988 /* Determine the number of arguments. */
13989 if (purposevec
&& TREE_CODE (purposevec
) == TREE_VEC
)
13991 len
= TREE_VEC_LENGTH (purposevec
);
13992 gcc_assert (!valuevec
|| len
== TREE_VEC_LENGTH (valuevec
));
13994 else if (TREE_CODE (valuevec
) == TREE_VEC
)
13995 len
= TREE_VEC_LENGTH (valuevec
);
13998 /* Since we only performed a partial substitution into
13999 the argument pack, we only return a single list
14001 if (purposevec
== TREE_PURPOSE (t
)
14002 && valuevec
== TREE_VALUE (t
)
14003 && chain
== TREE_CHAIN (t
))
14006 return tree_cons (purposevec
, valuevec
, chain
);
14009 /* Convert the argument vectors into a TREE_LIST */
14013 /* Grab the Ith values. */
14015 purpose
= purposevec
? TREE_VEC_ELT (purposevec
, i
)
14018 = valuevec
? convert_from_reference (TREE_VEC_ELT (valuevec
, i
))
14021 /* Build the list (backwards). */
14022 chain
= tree_cons (purpose
, value
, chain
);
14028 purpose
= TREE_PURPOSE (t
);
14030 purpose
= RECUR (purpose
);
14031 value
= TREE_VALUE (t
);
14033 value
= RECUR (value
);
14034 chain
= TREE_CHAIN (t
);
14035 if (chain
&& chain
!= void_type_node
)
14036 chain
= RECUR (chain
);
14037 if (purpose
== TREE_PURPOSE (t
)
14038 && value
== TREE_VALUE (t
)
14039 && chain
== TREE_CHAIN (t
))
14041 return tree_cons (purpose
, value
, chain
);
14044 case COMPONENT_REF
:
14050 object
= tsubst_non_call_postfix_expression (TREE_OPERAND (t
, 0),
14051 args
, complain
, in_decl
);
14052 /* Remember that there was a reference to this entity. */
14053 if (DECL_P (object
))
14054 mark_used (object
);
14055 object_type
= TREE_TYPE (object
);
14057 member
= TREE_OPERAND (t
, 1);
14058 if (BASELINK_P (member
))
14059 member
= tsubst_baselink (member
,
14060 non_reference (TREE_TYPE (object
)),
14061 args
, complain
, in_decl
);
14063 member
= tsubst_copy (member
, args
, complain
, in_decl
);
14064 if (member
== error_mark_node
)
14065 return error_mark_node
;
14067 if (type_dependent_expression_p (object
))
14068 /* We can't do much here. */;
14069 else if (!CLASS_TYPE_P (object_type
))
14071 if (SCALAR_TYPE_P (object_type
))
14073 tree s
= NULL_TREE
;
14074 tree dtor
= member
;
14076 if (TREE_CODE (dtor
) == SCOPE_REF
)
14078 s
= TREE_OPERAND (dtor
, 0);
14079 dtor
= TREE_OPERAND (dtor
, 1);
14081 if (TREE_CODE (dtor
) == BIT_NOT_EXPR
)
14083 dtor
= TREE_OPERAND (dtor
, 0);
14085 return finish_pseudo_destructor_expr (object
, s
, dtor
);
14089 else if (TREE_CODE (member
) == SCOPE_REF
14090 && TREE_CODE (TREE_OPERAND (member
, 1)) == TEMPLATE_ID_EXPR
)
14092 /* Lookup the template functions now that we know what the
14094 tree scope
= TREE_OPERAND (member
, 0);
14095 tree tmpl
= TREE_OPERAND (TREE_OPERAND (member
, 1), 0);
14096 tree args
= TREE_OPERAND (TREE_OPERAND (member
, 1), 1);
14097 member
= lookup_qualified_name (scope
, tmpl
,
14098 /*is_type_p=*/false,
14099 /*complain=*/false);
14100 if (BASELINK_P (member
))
14102 BASELINK_FUNCTIONS (member
)
14103 = build_nt (TEMPLATE_ID_EXPR
, BASELINK_FUNCTIONS (member
),
14105 member
= (adjust_result_of_qualified_name_lookup
14106 (member
, BINFO_TYPE (BASELINK_BINFO (member
)),
14111 qualified_name_lookup_error (scope
, tmpl
, member
,
14113 return error_mark_node
;
14116 else if (TREE_CODE (member
) == SCOPE_REF
14117 && !CLASS_TYPE_P (TREE_OPERAND (member
, 0))
14118 && TREE_CODE (TREE_OPERAND (member
, 0)) != NAMESPACE_DECL
)
14120 if (complain
& tf_error
)
14122 if (TYPE_P (TREE_OPERAND (member
, 0)))
14123 error ("%qT is not a class or namespace",
14124 TREE_OPERAND (member
, 0));
14126 error ("%qD is not a class or namespace",
14127 TREE_OPERAND (member
, 0));
14129 return error_mark_node
;
14131 else if (TREE_CODE (member
) == FIELD_DECL
)
14132 return finish_non_static_data_member (member
, object
, NULL_TREE
);
14134 return finish_class_member_access_expr (object
, member
,
14135 /*template_p=*/false,
14141 (RECUR (TREE_OPERAND (t
, 0)));
14145 VEC(constructor_elt
,gc
) *n
;
14146 constructor_elt
*ce
;
14147 unsigned HOST_WIDE_INT idx
;
14148 tree type
= tsubst (TREE_TYPE (t
), args
, complain
, in_decl
);
14149 bool process_index_p
;
14151 bool need_copy_p
= false;
14154 if (type
== error_mark_node
)
14155 return error_mark_node
;
14157 /* digest_init will do the wrong thing if we let it. */
14158 if (type
&& TYPE_PTRMEMFUNC_P (type
))
14161 /* We do not want to process the index of aggregate
14162 initializers as they are identifier nodes which will be
14163 looked up by digest_init. */
14164 process_index_p
= !(type
&& MAYBE_CLASS_TYPE_P (type
));
14166 n
= VEC_copy (constructor_elt
, gc
, CONSTRUCTOR_ELTS (t
));
14167 newlen
= VEC_length (constructor_elt
, n
);
14168 FOR_EACH_VEC_ELT (constructor_elt
, n
, idx
, ce
)
14170 if (ce
->index
&& process_index_p
)
14171 ce
->index
= RECUR (ce
->index
);
14173 if (PACK_EXPANSION_P (ce
->value
))
14175 /* Substitute into the pack expansion. */
14176 ce
->value
= tsubst_pack_expansion (ce
->value
, args
, complain
,
14179 if (ce
->value
== error_mark_node
14180 || PACK_EXPANSION_P (ce
->value
))
14182 else if (TREE_VEC_LENGTH (ce
->value
) == 1)
14183 /* Just move the argument into place. */
14184 ce
->value
= TREE_VEC_ELT (ce
->value
, 0);
14187 /* Update the length of the final CONSTRUCTOR
14188 arguments vector, and note that we will need to
14190 newlen
= newlen
+ TREE_VEC_LENGTH (ce
->value
) - 1;
14191 need_copy_p
= true;
14195 ce
->value
= RECUR (ce
->value
);
14200 VEC(constructor_elt
,gc
) *old_n
= n
;
14202 n
= VEC_alloc (constructor_elt
, gc
, newlen
);
14203 FOR_EACH_VEC_ELT (constructor_elt
, old_n
, idx
, ce
)
14205 if (TREE_CODE (ce
->value
) == TREE_VEC
)
14207 int i
, len
= TREE_VEC_LENGTH (ce
->value
);
14208 for (i
= 0; i
< len
; ++i
)
14209 CONSTRUCTOR_APPEND_ELT (n
, 0,
14210 TREE_VEC_ELT (ce
->value
, i
));
14213 CONSTRUCTOR_APPEND_ELT (n
, 0, ce
->value
);
14217 r
= build_constructor (init_list_type_node
, n
);
14218 CONSTRUCTOR_IS_DIRECT_INIT (r
) = CONSTRUCTOR_IS_DIRECT_INIT (t
);
14220 if (TREE_HAS_CONSTRUCTOR (t
))
14221 return finish_compound_literal (type
, r
, complain
);
14223 TREE_TYPE (r
) = type
;
14229 tree operand_0
= TREE_OPERAND (t
, 0);
14230 if (TYPE_P (operand_0
))
14232 operand_0
= tsubst (operand_0
, args
, complain
, in_decl
);
14233 return get_typeid (operand_0
);
14237 operand_0
= RECUR (operand_0
);
14238 return build_typeid (operand_0
);
14249 tree r
= tsubst_copy (t
, args
, complain
, in_decl
);
14251 if (TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
14252 /* If the original type was a reference, we'll be wrapped in
14253 the appropriate INDIRECT_REF. */
14254 r
= convert_from_reference (r
);
14259 return build_x_va_arg (RECUR (TREE_OPERAND (t
, 0)),
14260 tsubst (TREE_TYPE (t
), args
, complain
, in_decl
));
14262 case OFFSETOF_EXPR
:
14263 return finish_offsetof (RECUR (TREE_OPERAND (t
, 0)));
14267 tree type1
= tsubst_copy (TRAIT_EXPR_TYPE1 (t
), args
,
14268 complain
, in_decl
);
14270 tree type2
= TRAIT_EXPR_TYPE2 (t
);
14272 type2
= tsubst_copy (type2
, args
, complain
, in_decl
);
14274 return finish_trait_expr (TRAIT_EXPR_KIND (t
), type1
, type2
);
14279 tree old_stmt_expr
= cur_stmt_expr
;
14280 tree stmt_expr
= begin_stmt_expr ();
14282 cur_stmt_expr
= stmt_expr
;
14283 tsubst_expr (STMT_EXPR_STMT (t
), args
, complain
, in_decl
,
14284 integral_constant_expression_p
);
14285 stmt_expr
= finish_stmt_expr (stmt_expr
, false);
14286 cur_stmt_expr
= old_stmt_expr
;
14288 /* If the resulting list of expression statement is empty,
14289 fold it further into void_zero_node. */
14290 if (empty_expr_stmt_p (stmt_expr
))
14291 stmt_expr
= void_zero_node
;
14297 t
= tsubst_copy (t
, args
, complain
, in_decl
);
14298 /* As in finish_id_expression, we resolve enumeration constants
14299 to their underlying values. */
14300 if (TREE_CODE (t
) == CONST_DECL
&& !processing_template_decl
)
14302 used_types_insert (TREE_TYPE (t
));
14303 return DECL_INITIAL (t
);
14309 tree r
= build_lambda_expr ();
14311 tree type
= tsubst (LAMBDA_EXPR_CLOSURE (t
), args
, complain
, NULL_TREE
);
14312 LAMBDA_EXPR_CLOSURE (r
) = type
;
14313 CLASSTYPE_LAMBDA_EXPR (type
) = r
;
14315 LAMBDA_EXPR_LOCATION (r
)
14316 = LAMBDA_EXPR_LOCATION (t
);
14317 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r
)
14318 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t
);
14319 LAMBDA_EXPR_MUTABLE_P (r
) = LAMBDA_EXPR_MUTABLE_P (t
);
14320 LAMBDA_EXPR_DISCRIMINATOR (r
)
14321 = (LAMBDA_EXPR_DISCRIMINATOR (t
));
14322 LAMBDA_EXPR_EXTRA_SCOPE (r
)
14323 = RECUR (LAMBDA_EXPR_EXTRA_SCOPE (t
));
14324 if (LAMBDA_EXPR_RETURN_TYPE (t
) == dependent_lambda_return_type_node
)
14326 LAMBDA_EXPR_RETURN_TYPE (r
) = dependent_lambda_return_type_node
;
14327 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (r
) = true;
14330 LAMBDA_EXPR_RETURN_TYPE (r
)
14331 = tsubst (LAMBDA_EXPR_RETURN_TYPE (t
), args
, complain
, in_decl
);
14333 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t
) == NULL_TREE
14334 && LAMBDA_EXPR_PENDING_PROXIES (t
) == NULL
);
14336 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
14337 determine_visibility (TYPE_NAME (type
));
14338 /* Now that we know visibility, instantiate the type so we have a
14339 declaration of the op() for later calls to lambda_function. */
14340 complete_type (type
);
14342 /* The capture list refers to closure members, so this needs to
14343 wait until after we finish instantiating the type. */
14344 LAMBDA_EXPR_CAPTURE_LIST (r
)
14345 = RECUR (LAMBDA_EXPR_CAPTURE_LIST (t
));
14347 return build_lambda_object (r
);
14351 /* We can get here for a constant initializer of non-dependent type.
14352 FIXME stop folding in cp_parser_initializer_clause. */
14353 gcc_assert (TREE_CONSTANT (t
));
14355 tree r
= get_target_expr (RECUR (TARGET_EXPR_INITIAL (t
)));
14356 TREE_CONSTANT (r
) = true;
14360 case TRANSACTION_EXPR
:
14361 return tsubst_expr(t
, args
, complain
, in_decl
,
14362 integral_constant_expression_p
);
14365 /* Handle Objective-C++ constructs, if appropriate. */
14368 = objcp_tsubst_copy_and_build (t
, args
, complain
,
14369 in_decl
, /*function_p=*/false);
14373 return tsubst_copy (t
, args
, complain
, in_decl
);
14379 /* Verify that the instantiated ARGS are valid. For type arguments,
14380 make sure that the type's linkage is ok. For non-type arguments,
14381 make sure they are constants if they are integral or enumerations.
14382 Emit an error under control of COMPLAIN, and return TRUE on error. */
14385 check_instantiated_arg (tree tmpl
, tree t
, tsubst_flags_t complain
)
14387 if (ARGUMENT_PACK_P (t
))
14389 tree vec
= ARGUMENT_PACK_ARGS (t
);
14390 int len
= TREE_VEC_LENGTH (vec
);
14391 bool result
= false;
14394 for (i
= 0; i
< len
; ++i
)
14395 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (vec
, i
), complain
))
14399 else if (TYPE_P (t
))
14401 /* [basic.link]: A name with no linkage (notably, the name
14402 of a class or enumeration declared in a local scope)
14403 shall not be used to declare an entity with linkage.
14404 This implies that names with no linkage cannot be used as
14407 DR 757 relaxes this restriction for C++0x. */
14408 tree nt
= (cxx_dialect
> cxx98
? NULL_TREE
14409 : no_linkage_check (t
, /*relaxed_p=*/false));
14413 /* DR 488 makes use of a type with no linkage cause
14414 type deduction to fail. */
14415 if (complain
& tf_error
)
14417 if (TYPE_ANONYMOUS_P (nt
))
14418 error ("%qT is/uses anonymous type", t
);
14420 error ("template argument for %qD uses local type %qT",
14425 /* In order to avoid all sorts of complications, we do not
14426 allow variably-modified types as template arguments. */
14427 else if (variably_modified_type_p (t
, NULL_TREE
))
14429 if (complain
& tf_error
)
14430 error ("%qT is a variably modified type", t
);
14434 /* A non-type argument of integral or enumerated type must be a
14436 else if (TREE_TYPE (t
)
14437 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
))
14438 && !TREE_CONSTANT (t
))
14440 if (complain
& tf_error
)
14441 error ("integral expression %qE is not constant", t
);
14448 check_instantiated_args (tree tmpl
, tree args
, tsubst_flags_t complain
)
14450 int ix
, len
= DECL_NTPARMS (tmpl
);
14451 bool result
= false;
14453 for (ix
= 0; ix
!= len
; ix
++)
14455 if (check_instantiated_arg (tmpl
, TREE_VEC_ELT (args
, ix
), complain
))
14458 if (result
&& (complain
& tf_error
))
14459 error (" trying to instantiate %qD", tmpl
);
14463 /* In C++0x, it's possible to have a function template whose type depends
14464 on itself recursively. This is most obvious with decltype, but can also
14465 occur with enumeration scope (c++/48969). So we need to catch infinite
14466 recursion and reject the substitution at deduction time; this function
14467 will return error_mark_node for any repeated substitution.
14469 This also catches excessive recursion such as when f<N> depends on
14470 f<N-1> across all integers, and returns error_mark_node for all the
14471 substitutions back up to the initial one.
14473 This is, of course, not reentrant. */
14476 deduction_tsubst_fntype (tree fn
, tree targs
, tsubst_flags_t complain
)
14478 static bool excessive_deduction_depth
;
14479 static int deduction_depth
;
14480 struct pending_template
*old_last_pend
= last_pending_template
;
14481 struct tinst_level
*old_error_tinst
= last_error_tinst_level
;
14483 tree fntype
= TREE_TYPE (fn
);
14487 if (excessive_deduction_depth
)
14488 return error_mark_node
;
14490 tinst
= build_tree_list (fn
, targs
);
14491 if (!push_tinst_level (tinst
))
14493 excessive_deduction_depth
= true;
14495 return error_mark_node
;
14498 input_location
= DECL_SOURCE_LOCATION (fn
);
14500 push_deduction_access_scope (fn
);
14501 r
= tsubst (fntype
, targs
, complain
, NULL_TREE
);
14502 pop_deduction_access_scope (fn
);
14505 if (excessive_deduction_depth
)
14507 r
= error_mark_node
;
14508 if (deduction_depth
== 0)
14509 /* Reset once we're all the way out. */
14510 excessive_deduction_depth
= false;
14513 pop_tinst_level ();
14514 /* We can't free this if a pending_template entry or last_error_tinst_level
14515 is pointing at it. */
14516 if (last_pending_template
== old_last_pend
14517 && last_error_tinst_level
== old_error_tinst
)
14522 /* Instantiate the indicated variable or function template TMPL with
14523 the template arguments in TARG_PTR. */
14526 instantiate_template_1 (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14528 tree targ_ptr
= orig_args
;
14533 if (tmpl
== error_mark_node
)
14534 return error_mark_node
;
14536 gcc_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
);
14538 /* If this function is a clone, handle it specially. */
14539 if (DECL_CLONED_FUNCTION_P (tmpl
))
14544 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
14545 DECL_CLONED_FUNCTION. */
14546 spec
= instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl
),
14547 targ_ptr
, complain
);
14548 if (spec
== error_mark_node
)
14549 return error_mark_node
;
14551 /* Look for the clone. */
14552 FOR_EACH_CLONE (clone
, spec
)
14553 if (DECL_NAME (clone
) == DECL_NAME (tmpl
))
14555 /* We should always have found the clone by now. */
14556 gcc_unreachable ();
14560 /* Check to see if we already have this specialization. */
14561 gen_tmpl
= most_general_template (tmpl
);
14562 if (tmpl
!= gen_tmpl
)
14563 /* The TMPL is a partial instantiation. To get a full set of
14564 arguments we must add the arguments used to perform the
14565 partial instantiation. */
14566 targ_ptr
= add_outermost_template_args (DECL_TI_ARGS (tmpl
),
14569 /* It would be nice to avoid hashing here and then again in tsubst_decl,
14570 but it doesn't seem to be on the hot path. */
14571 spec
= retrieve_specialization (gen_tmpl
, targ_ptr
, 0);
14573 gcc_assert (tmpl
== gen_tmpl
14574 || ((fndecl
= retrieve_specialization (tmpl
, orig_args
, 0))
14576 || fndecl
== NULL_TREE
);
14578 if (spec
!= NULL_TREE
)
14581 if (check_instantiated_args (gen_tmpl
, INNERMOST_TEMPLATE_ARGS (targ_ptr
),
14583 return error_mark_node
;
14585 /* We are building a FUNCTION_DECL, during which the access of its
14586 parameters and return types have to be checked. However this
14587 FUNCTION_DECL which is the desired context for access checking
14588 is not built yet. We solve this chicken-and-egg problem by
14589 deferring all checks until we have the FUNCTION_DECL. */
14590 push_deferring_access_checks (dk_deferred
);
14592 /* Instantiation of the function happens in the context of the function
14593 template, not the context of the overload resolution we're doing. */
14594 push_to_top_level ();
14595 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14597 tree ctx
= tsubst (DECL_CONTEXT (gen_tmpl
), targ_ptr
,
14598 complain
, gen_tmpl
);
14599 push_nested_class (ctx
);
14601 /* Substitute template parameters to obtain the specialization. */
14602 fndecl
= tsubst (DECL_TEMPLATE_RESULT (gen_tmpl
),
14603 targ_ptr
, complain
, gen_tmpl
);
14604 if (DECL_CLASS_SCOPE_P (gen_tmpl
))
14605 pop_nested_class ();
14606 pop_from_top_level ();
14608 if (fndecl
== error_mark_node
)
14609 return error_mark_node
;
14611 /* Now we know the specialization, compute access previously
14613 push_access_scope (fndecl
);
14615 /* Some typedefs referenced from within the template code need to be access
14616 checked at template instantiation time, i.e now. These types were
14617 added to the template at parsing time. Let's get those and perfom
14618 the acces checks then. */
14619 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl
), targ_ptr
);
14620 perform_deferred_access_checks ();
14621 pop_access_scope (fndecl
);
14622 pop_deferring_access_checks ();
14624 /* The DECL_TI_TEMPLATE should always be the immediate parent
14625 template, not the most general template. */
14626 DECL_TI_TEMPLATE (fndecl
) = tmpl
;
14628 /* If we've just instantiated the main entry point for a function,
14629 instantiate all the alternate entry points as well. We do this
14630 by cloning the instantiation of the main entry point, not by
14631 instantiating the template clones. */
14632 if (DECL_CHAIN (gen_tmpl
) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl
)))
14633 clone_function_decl (fndecl
, /*update_method_vec_p=*/0);
14638 /* Wrapper for instantiate_template_1. */
14641 instantiate_template (tree tmpl
, tree orig_args
, tsubst_flags_t complain
)
14644 timevar_push (TV_TEMPLATE_INST
);
14645 ret
= instantiate_template_1 (tmpl
, orig_args
, complain
);
14646 timevar_pop (TV_TEMPLATE_INST
);
14650 /* We're going to do deduction substitution on the type of TMPL, a function
14651 template. In C++11 mode, push into that access scope. In C++03 mode,
14652 disable access checking. */
14655 push_deduction_access_scope (tree tmpl
)
14657 if (cxx_dialect
>= cxx0x
)
14659 int ptd
= processing_template_decl
;
14660 push_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
14661 /* Preserve processing_template_decl across push_to_top_level. */
14662 if (ptd
&& !processing_template_decl
)
14663 ++processing_template_decl
;
14666 push_deferring_access_checks (dk_no_check
);
14669 /* And pop back out. */
14672 pop_deduction_access_scope (tree tmpl
)
14674 if (cxx_dialect
>= cxx0x
)
14675 pop_access_scope (DECL_TEMPLATE_RESULT (tmpl
));
14677 pop_deferring_access_checks ();
14680 /* PARM is a template parameter pack for FN. Returns true iff
14681 PARM is used in a deducible way in the argument list of FN. */
14684 pack_deducible_p (tree parm
, tree fn
)
14686 tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
14687 for (; t
; t
= TREE_CHAIN (t
))
14689 tree type
= TREE_VALUE (t
);
14691 if (!PACK_EXPANSION_P (type
))
14693 for (packs
= PACK_EXPANSION_PARAMETER_PACKS (type
);
14694 packs
; packs
= TREE_CHAIN (packs
))
14695 if (TREE_VALUE (packs
) == parm
)
14697 /* The template parameter pack is used in a function parameter
14698 pack. If this is the end of the parameter list, the
14699 template parameter pack is deducible. */
14700 if (TREE_CHAIN (t
) == void_list_node
)
14703 /* Otherwise, not. Well, it could be deduced from
14704 a non-pack parameter, but doing so would end up with
14705 a deduction mismatch, so don't bother. */
14709 /* The template parameter pack isn't used in any function parameter
14710 packs, but it might be used deeper, e.g. tuple<Args...>. */
14714 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
14715 NARGS elements of the arguments that are being used when calling
14716 it. TARGS is a vector into which the deduced template arguments
14719 Return zero for success, 2 for an incomplete match that doesn't resolve
14720 all the types, and 1 for complete failure. An error message will be
14721 printed only for an incomplete match.
14723 If FN is a conversion operator, or we are trying to produce a specific
14724 specialization, RETURN_TYPE is the return type desired.
14726 The EXPLICIT_TARGS are explicit template arguments provided via a
14729 The parameter STRICT is one of:
14732 We are deducing arguments for a function call, as in
14733 [temp.deduct.call].
14736 We are deducing arguments for a conversion function, as in
14737 [temp.deduct.conv].
14740 We are deducing arguments when doing an explicit instantiation
14741 as in [temp.explicit], when determining an explicit specialization
14742 as in [temp.expl.spec], or when taking the address of a function
14743 template, as in [temp.deduct.funcaddr]. */
14746 fn_type_unification (tree fn
,
14747 tree explicit_targs
,
14750 unsigned int nargs
,
14752 unification_kind_t strict
,
14760 gcc_assert (TREE_CODE (fn
) == TEMPLATE_DECL
);
14762 fntype
= TREE_TYPE (fn
);
14763 if (explicit_targs
)
14767 The specified template arguments must match the template
14768 parameters in kind (i.e., type, nontype, template), and there
14769 must not be more arguments than there are parameters;
14770 otherwise type deduction fails.
14772 Nontype arguments must match the types of the corresponding
14773 nontype template parameters, or must be convertible to the
14774 types of the corresponding nontype parameters as specified in
14775 _temp.arg.nontype_, otherwise type deduction fails.
14777 All references in the function type of the function template
14778 to the corresponding template parameters are replaced by the
14779 specified template argument values. If a substitution in a
14780 template parameter or in the function type of the function
14781 template results in an invalid type, type deduction fails. */
14782 tree tparms
= DECL_INNERMOST_TEMPLATE_PARMS (fn
);
14783 int i
, len
= TREE_VEC_LENGTH (tparms
);
14784 tree converted_args
;
14785 bool incomplete
= false;
14787 if (explicit_targs
== error_mark_node
)
14788 return unify_invalid (explain_p
);
14791 = (coerce_template_parms (tparms
, explicit_targs
, NULL_TREE
,
14793 ? tf_warning_or_error
14795 /*require_all_args=*/false,
14796 /*use_default_args=*/false));
14797 if (converted_args
== error_mark_node
)
14800 /* Substitute the explicit args into the function type. This is
14801 necessary so that, for instance, explicitly declared function
14802 arguments can match null pointed constants. If we were given
14803 an incomplete set of explicit args, we must not do semantic
14804 processing during substitution as we could create partial
14806 for (i
= 0; i
< len
; i
++)
14808 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
14809 bool parameter_pack
= false;
14810 tree targ
= TREE_VEC_ELT (converted_args
, i
);
14812 /* Dig out the actual parm. */
14813 if (TREE_CODE (parm
) == TYPE_DECL
14814 || TREE_CODE (parm
) == TEMPLATE_DECL
)
14816 parm
= TREE_TYPE (parm
);
14817 parameter_pack
= TEMPLATE_TYPE_PARAMETER_PACK (parm
);
14819 else if (TREE_CODE (parm
) == PARM_DECL
)
14821 parm
= DECL_INITIAL (parm
);
14822 parameter_pack
= TEMPLATE_PARM_PARAMETER_PACK (parm
);
14825 if (!parameter_pack
&& targ
== NULL_TREE
)
14826 /* No explicit argument for this template parameter. */
14829 if (parameter_pack
&& pack_deducible_p (parm
, fn
))
14831 /* Mark the argument pack as "incomplete". We could
14832 still deduce more arguments during unification.
14833 We remove this mark in type_unification_real. */
14836 ARGUMENT_PACK_INCOMPLETE_P(targ
) = 1;
14837 ARGUMENT_PACK_EXPLICIT_ARGS (targ
)
14838 = ARGUMENT_PACK_ARGS (targ
);
14841 /* We have some incomplete argument packs. */
14846 processing_template_decl
+= incomplete
;
14847 fntype
= deduction_tsubst_fntype (fn
, converted_args
,
14849 ? tf_warning_or_error
14851 processing_template_decl
-= incomplete
;
14853 if (fntype
== error_mark_node
)
14856 /* Place the explicitly specified arguments in TARGS. */
14857 for (i
= NUM_TMPL_ARGS (converted_args
); i
--;)
14858 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (converted_args
, i
);
14861 /* Never do unification on the 'this' parameter. */
14862 parms
= skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (fntype
));
14868 parms
= tree_cons (NULL_TREE
, TREE_TYPE (fntype
), parms
);
14869 new_args
= XALLOCAVEC (tree
, nargs
+ 1);
14870 new_args
[0] = return_type
;
14871 memcpy (new_args
+ 1, args
, nargs
* sizeof (tree
));
14876 /* We allow incomplete unification without an error message here
14877 because the standard doesn't seem to explicitly prohibit it. Our
14878 callers must be ready to deal with unification failures in any
14880 result
= type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
14881 targs
, parms
, args
, nargs
, /*subr=*/0,
14882 strict
, flags
, explain_p
);
14884 /* Now that we have bindings for all of the template arguments,
14885 ensure that the arguments deduced for the template template
14886 parameters have compatible template parameter lists. We cannot
14887 check this property before we have deduced all template
14888 arguments, because the template parameter types of a template
14889 template parameter might depend on prior template parameters
14890 deduced after the template template parameter. The following
14891 ill-formed example illustrates this issue:
14893 template<typename T, template<T> class C> void f(C<5>, T);
14895 template<int N> struct X {};
14898 f(X<5>(), 5l); // error: template argument deduction fails
14901 The template parameter list of 'C' depends on the template type
14902 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
14903 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
14904 time that we deduce 'C'. */
14906 && !template_template_parm_bindings_ok_p
14907 (DECL_INNERMOST_TEMPLATE_PARMS (fn
), targs
))
14908 return unify_inconsistent_template_template_parameters (explain_p
);
14911 /* All is well so far. Now, check:
14915 When all template arguments have been deduced, all uses of
14916 template parameters in nondeduced contexts are replaced with
14917 the corresponding deduced argument values. If the
14918 substitution results in an invalid type, as described above,
14919 type deduction fails. */
14921 tree substed
= deduction_tsubst_fntype (fn
, targs
,
14923 ? tf_warning_or_error
14925 if (substed
== error_mark_node
)
14928 /* If we're looking for an exact match, check that what we got
14929 is indeed an exact match. It might not be if some template
14930 parameters are used in non-deduced contexts. */
14931 if (strict
== DEDUCE_EXACT
)
14936 = skip_artificial_parms_for (fn
, TYPE_ARG_TYPES (substed
));
14938 sarg
= tree_cons (NULL_TREE
, TREE_TYPE (substed
), sarg
);
14939 for (i
= 0; i
< nargs
&& sarg
; ++i
, sarg
= TREE_CHAIN (sarg
))
14940 if (!same_type_p (args
[i
], TREE_VALUE (sarg
)))
14941 return unify_type_mismatch (explain_p
, args
[i
],
14942 TREE_VALUE (sarg
));
14949 /* Adjust types before performing type deduction, as described in
14950 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
14951 sections are symmetric. PARM is the type of a function parameter
14952 or the return type of the conversion function. ARG is the type of
14953 the argument passed to the call, or the type of the value
14954 initialized with the result of the conversion function.
14955 ARG_EXPR is the original argument expression, which may be null. */
14958 maybe_adjust_types_for_deduction (unification_kind_t strict
,
14972 /* Swap PARM and ARG throughout the remainder of this
14973 function; the handling is precisely symmetric since PARM
14974 will initialize ARG rather than vice versa. */
14982 /* Core issue #873: Do the DR606 thing (see below) for these cases,
14983 too, but here handle it by stripping the reference from PARM
14984 rather than by adding it to ARG. */
14985 if (TREE_CODE (*parm
) == REFERENCE_TYPE
14986 && TYPE_REF_IS_RVALUE (*parm
)
14987 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
14988 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
14989 && TREE_CODE (*arg
) == REFERENCE_TYPE
14990 && !TYPE_REF_IS_RVALUE (*arg
))
14991 *parm
= TREE_TYPE (*parm
);
14992 /* Nothing else to do in this case. */
14996 gcc_unreachable ();
14999 if (TREE_CODE (*parm
) != REFERENCE_TYPE
)
15001 /* [temp.deduct.call]
15003 If P is not a reference type:
15005 --If A is an array type, the pointer type produced by the
15006 array-to-pointer standard conversion (_conv.array_) is
15007 used in place of A for type deduction; otherwise,
15009 --If A is a function type, the pointer type produced by
15010 the function-to-pointer standard conversion
15011 (_conv.func_) is used in place of A for type deduction;
15014 --If A is a cv-qualified type, the top level
15015 cv-qualifiers of A's type are ignored for type
15017 if (TREE_CODE (*arg
) == ARRAY_TYPE
)
15018 *arg
= build_pointer_type (TREE_TYPE (*arg
));
15019 else if (TREE_CODE (*arg
) == FUNCTION_TYPE
)
15020 *arg
= build_pointer_type (*arg
);
15022 *arg
= TYPE_MAIN_VARIANT (*arg
);
15025 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
15026 of the form T&&, where T is a template parameter, and the argument
15027 is an lvalue, T is deduced as A& */
15028 if (TREE_CODE (*parm
) == REFERENCE_TYPE
15029 && TYPE_REF_IS_RVALUE (*parm
)
15030 && TREE_CODE (TREE_TYPE (*parm
)) == TEMPLATE_TYPE_PARM
15031 && cp_type_quals (TREE_TYPE (*parm
)) == TYPE_UNQUALIFIED
15032 && (arg_expr
? real_lvalue_p (arg_expr
)
15033 /* try_one_overload doesn't provide an arg_expr, but
15034 functions are always lvalues. */
15035 : TREE_CODE (*arg
) == FUNCTION_TYPE
))
15036 *arg
= build_reference_type (*arg
);
15038 /* [temp.deduct.call]
15040 If P is a cv-qualified type, the top level cv-qualifiers
15041 of P's type are ignored for type deduction. If P is a
15042 reference type, the type referred to by P is used for
15044 *parm
= TYPE_MAIN_VARIANT (*parm
);
15045 if (TREE_CODE (*parm
) == REFERENCE_TYPE
)
15047 *parm
= TREE_TYPE (*parm
);
15048 result
|= UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
15051 /* DR 322. For conversion deduction, remove a reference type on parm
15052 too (which has been swapped into ARG). */
15053 if (strict
== DEDUCE_CONV
&& TREE_CODE (*arg
) == REFERENCE_TYPE
)
15054 *arg
= TREE_TYPE (*arg
);
15059 /* Subroutine of unify_one_argument. PARM is a function parameter of a
15060 template which does contain any deducible template parameters; check if
15061 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
15062 unify_one_argument. */
15065 check_non_deducible_conversion (tree parm
, tree arg
, int strict
,
15066 int flags
, bool explain_p
)
15071 type
= TREE_TYPE (arg
);
15075 if (same_type_p (parm
, type
))
15076 return unify_success (explain_p
);
15078 if (strict
== DEDUCE_CONV
)
15080 if (can_convert_arg (type
, parm
, NULL_TREE
, flags
))
15081 return unify_success (explain_p
);
15083 else if (strict
!= DEDUCE_EXACT
)
15085 if (can_convert_arg (parm
, type
,
15086 TYPE_P (arg
) ? NULL_TREE
: arg
,
15088 return unify_success (explain_p
);
15091 if (strict
== DEDUCE_EXACT
)
15092 return unify_type_mismatch (explain_p
, parm
, arg
);
15094 return unify_arg_conversion (explain_p
, parm
, type
, arg
);
15097 /* Subroutine of type_unification_real and unify_pack_expansion to
15098 handle unification of a single P/A pair. Parameters are as
15099 for those functions. */
15102 unify_one_argument (tree tparms
, tree targs
, tree parm
, tree arg
,
15103 int subr
, unification_kind_t strict
, int flags
,
15106 tree arg_expr
= NULL_TREE
;
15109 if (arg
== error_mark_node
|| parm
== error_mark_node
)
15110 return unify_invalid (explain_p
);
15111 if (arg
== unknown_type_node
)
15112 /* We can't deduce anything from this, but we might get all the
15113 template args from other function args. */
15114 return unify_success (explain_p
);
15116 /* FIXME uses_deducible_template_parms */
15117 if (TYPE_P (parm
) && !uses_template_parms (parm
))
15118 return check_non_deducible_conversion (parm
, arg
, strict
, flags
,
15124 arg_strict
= (UNIFY_ALLOW_OUTER_LEVEL
15125 | UNIFY_ALLOW_MORE_CV_QUAL
15126 | UNIFY_ALLOW_DERIVED
);
15130 arg_strict
= UNIFY_ALLOW_LESS_CV_QUAL
;
15134 arg_strict
= UNIFY_ALLOW_NONE
;
15138 gcc_unreachable ();
15141 /* We only do these transformations if this is the top-level
15142 parameter_type_list in a call or declaration matching; in other
15143 situations (nested function declarators, template argument lists) we
15144 won't be comparing a type to an expression, and we don't do any type
15150 gcc_assert (TREE_TYPE (arg
) != NULL_TREE
);
15151 if (type_unknown_p (arg
))
15153 /* [temp.deduct.type] A template-argument can be
15154 deduced from a pointer to function or pointer
15155 to member function argument if the set of
15156 overloaded functions does not contain function
15157 templates and at most one of a set of
15158 overloaded functions provides a unique
15161 if (resolve_overloaded_unification
15162 (tparms
, targs
, parm
, arg
, strict
,
15163 arg_strict
, explain_p
))
15164 return unify_success (explain_p
);
15165 return unify_overload_resolution_failure (explain_p
, arg
);
15169 arg
= unlowered_expr_type (arg
);
15170 if (arg
== error_mark_node
)
15171 return unify_invalid (explain_p
);
15175 maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, arg_expr
);
15178 gcc_assert ((TYPE_P (parm
) || TREE_CODE (parm
) == TEMPLATE_DECL
)
15179 == (TYPE_P (arg
) || TREE_CODE (arg
) == TEMPLATE_DECL
));
15181 /* For deduction from an init-list we need the actual list. */
15182 if (arg_expr
&& BRACE_ENCLOSED_INITIALIZER_P (arg_expr
))
15184 return unify (tparms
, targs
, parm
, arg
, arg_strict
, explain_p
);
15187 /* Most parms like fn_type_unification.
15189 If SUBR is 1, we're being called recursively (to unify the
15190 arguments of a function or method parameter of a function
15194 type_unification_real (tree tparms
,
15198 unsigned int xnargs
,
15200 unification_kind_t strict
,
15206 int ntparms
= TREE_VEC_LENGTH (tparms
);
15207 int saw_undeduced
= 0;
15210 unsigned int nargs
;
15213 gcc_assert (TREE_CODE (tparms
) == TREE_VEC
);
15214 gcc_assert (xparms
== NULL_TREE
|| TREE_CODE (xparms
) == TREE_LIST
);
15215 gcc_assert (ntparms
> 0);
15217 /* Reset the number of non-defaulted template arguments contained
15219 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
) = NULL_TREE
;
15227 while (parms
&& parms
!= void_list_node
15230 parm
= TREE_VALUE (parms
);
15232 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
15233 && (!TREE_CHAIN (parms
) || TREE_CHAIN (parms
) == void_list_node
))
15234 /* For a function parameter pack that occurs at the end of the
15235 parameter-declaration-list, the type A of each remaining
15236 argument of the call is compared with the type P of the
15237 declarator-id of the function parameter pack. */
15240 parms
= TREE_CHAIN (parms
);
15242 if (TREE_CODE (parm
) == TYPE_PACK_EXPANSION
)
15243 /* For a function parameter pack that does not occur at the
15244 end of the parameter-declaration-list, the type of the
15245 parameter pack is a non-deduced context. */
15251 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15257 && parms
!= void_list_node
15258 && TREE_CODE (TREE_VALUE (parms
)) == TYPE_PACK_EXPANSION
)
15260 /* Unify the remaining arguments with the pack expansion type. */
15262 tree parmvec
= make_tree_vec (1);
15264 /* Allocate a TREE_VEC and copy in all of the arguments */
15265 argvec
= make_tree_vec (nargs
- ia
);
15266 for (i
= 0; ia
< nargs
; ++ia
, ++i
)
15267 TREE_VEC_ELT (argvec
, i
) = args
[ia
];
15269 /* Copy the parameter into parmvec. */
15270 TREE_VEC_ELT (parmvec
, 0) = TREE_VALUE (parms
);
15271 if (unify_pack_expansion (tparms
, targs
, parmvec
, argvec
, strict
,
15272 /*subr=*/subr
, explain_p
))
15275 /* Advance to the end of the list of parameters. */
15276 parms
= TREE_CHAIN (parms
);
15279 /* Fail if we've reached the end of the parm list, and more args
15280 are present, and the parm list isn't variadic. */
15281 if (ia
< nargs
&& parms
== void_list_node
)
15282 return unify_too_many_arguments (explain_p
, nargs
, ia
);
15283 /* Fail if parms are left and they don't have default values. */
15284 if (parms
&& parms
!= void_list_node
15285 && TREE_PURPOSE (parms
) == NULL_TREE
)
15287 unsigned int count
= nargs
;
15289 while (p
&& p
!= void_list_node
)
15292 p
= TREE_CHAIN (p
);
15294 return unify_too_few_arguments (explain_p
, ia
, count
);
15299 tsubst_flags_t complain
= (explain_p
15300 ? tf_warning_or_error
15303 /* Check to see if we need another pass before we start clearing
15304 ARGUMENT_PACK_INCOMPLETE_P. */
15305 for (i
= 0; i
< ntparms
; i
++)
15307 tree targ
= TREE_VEC_ELT (targs
, i
);
15308 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15310 if (targ
|| tparm
== error_mark_node
)
15312 tparm
= TREE_VALUE (tparm
);
15314 /* If this is an undeduced nontype parameter that depends on
15315 a type parameter, try another pass; its type may have been
15316 deduced from a later argument than the one from which
15317 this parameter can be deduced. */
15318 if (TREE_CODE (tparm
) == PARM_DECL
15319 && uses_template_parms (TREE_TYPE (tparm
))
15320 && !saw_undeduced
++)
15324 for (i
= 0; i
< ntparms
; i
++)
15326 tree targ
= TREE_VEC_ELT (targs
, i
);
15327 tree tparm
= TREE_VEC_ELT (tparms
, i
);
15329 /* Clear the "incomplete" flags on all argument packs now so that
15330 substituting them into later default arguments works. */
15331 if (targ
&& ARGUMENT_PACK_P (targ
))
15333 ARGUMENT_PACK_INCOMPLETE_P (targ
) = 0;
15334 ARGUMENT_PACK_EXPLICIT_ARGS (targ
) = NULL_TREE
;
15337 if (targ
|| tparm
== error_mark_node
)
15339 tparm
= TREE_VALUE (tparm
);
15341 /* Core issue #226 (C++0x) [temp.deduct]:
15343 If a template argument has not been deduced, its
15344 default template argument, if any, is used.
15346 When we are in C++98 mode, TREE_PURPOSE will either
15347 be NULL_TREE or ERROR_MARK_NODE, so we do not need
15348 to explicitly check cxx_dialect here. */
15349 if (TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
)))
15351 tree parm
= TREE_VALUE (TREE_VEC_ELT (tparms
, i
));
15352 tree arg
= TREE_PURPOSE (TREE_VEC_ELT (tparms
, i
));
15353 location_t save_loc
= input_location
;
15355 input_location
= DECL_SOURCE_LOCATION (parm
);
15356 arg
= tsubst_template_arg (arg
, targs
, complain
, NULL_TREE
);
15357 arg
= convert_template_argument (parm
, arg
, targs
, complain
,
15359 input_location
= save_loc
;
15360 if (arg
== error_mark_node
)
15364 TREE_VEC_ELT (targs
, i
) = arg
;
15365 /* The position of the first default template argument,
15366 is also the number of non-defaulted arguments in TARGS.
15368 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15369 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, i
);
15374 /* If the type parameter is a parameter pack, then it will
15375 be deduced to an empty parameter pack. */
15376 if (template_parameter_pack_p (tparm
))
15380 if (TREE_CODE (tparm
) == TEMPLATE_PARM_INDEX
)
15382 arg
= make_node (NONTYPE_ARGUMENT_PACK
);
15383 TREE_TYPE (arg
) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm
));
15384 TREE_CONSTANT (arg
) = 1;
15387 arg
= cxx_make_type (TYPE_ARGUMENT_PACK
);
15389 SET_ARGUMENT_PACK_ARGS (arg
, make_tree_vec (0));
15391 TREE_VEC_ELT (targs
, i
) = arg
;
15395 return unify_parameter_deduction_failure (explain_p
, tparm
);
15398 #ifdef ENABLE_CHECKING
15399 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
))
15400 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs
, TREE_VEC_LENGTH (targs
));
15403 return unify_success (explain_p
);
15406 /* Subroutine of type_unification_real. Args are like the variables
15407 at the call site. ARG is an overloaded function (or template-id);
15408 we try deducing template args from each of the overloads, and if
15409 only one succeeds, we go with that. Modifies TARGS and returns
15410 true on success. */
15413 resolve_overloaded_unification (tree tparms
,
15417 unification_kind_t strict
,
15421 tree tempargs
= copy_node (targs
);
15423 tree goodfn
= NULL_TREE
;
15426 if (TREE_CODE (arg
) == ADDR_EXPR
)
15428 arg
= TREE_OPERAND (arg
, 0);
15434 if (TREE_CODE (arg
) == COMPONENT_REF
)
15435 /* Handle `&x' where `x' is some static or non-static member
15437 arg
= TREE_OPERAND (arg
, 1);
15439 if (TREE_CODE (arg
) == OFFSET_REF
)
15440 arg
= TREE_OPERAND (arg
, 1);
15442 /* Strip baselink information. */
15443 if (BASELINK_P (arg
))
15444 arg
= BASELINK_FUNCTIONS (arg
);
15446 if (TREE_CODE (arg
) == TEMPLATE_ID_EXPR
)
15448 /* If we got some explicit template args, we need to plug them into
15449 the affected templates before we try to unify, in case the
15450 explicit args will completely resolve the templates in question. */
15453 tree expl_subargs
= TREE_OPERAND (arg
, 1);
15454 arg
= TREE_OPERAND (arg
, 0);
15456 for (; arg
; arg
= OVL_NEXT (arg
))
15458 tree fn
= OVL_CURRENT (arg
);
15459 tree subargs
, elem
;
15461 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15464 ++processing_template_decl
;
15465 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
15466 expl_subargs
, /*check_ret=*/false);
15467 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
15469 elem
= tsubst (TREE_TYPE (fn
), subargs
, tf_none
, NULL_TREE
);
15470 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15471 elem
, strict
, sub_strict
, addr_p
, explain_p
)
15472 && (!goodfn
|| !same_type_p (goodfn
, elem
)))
15480 --processing_template_decl
;
15482 /* If no templates (or more than one) are fully resolved by the
15483 explicit arguments, this template-id is a non-deduced context; it
15484 could still be OK if we deduce all template arguments for the
15485 enclosing call through other arguments. */
15489 else if (TREE_CODE (arg
) != OVERLOAD
15490 && TREE_CODE (arg
) != FUNCTION_DECL
)
15491 /* If ARG is, for example, "(0, &f)" then its type will be unknown
15492 -- but the deduction does not succeed because the expression is
15493 not just the function on its own. */
15496 for (; arg
; arg
= OVL_NEXT (arg
))
15497 if (try_one_overload (tparms
, targs
, tempargs
, parm
,
15498 TREE_TYPE (OVL_CURRENT (arg
)),
15499 strict
, sub_strict
, addr_p
, explain_p
)
15500 && (!goodfn
|| !decls_match (goodfn
, OVL_CURRENT (arg
))))
15502 goodfn
= OVL_CURRENT (arg
);
15506 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15507 to function or pointer to member function argument if the set of
15508 overloaded functions does not contain function templates and at most
15509 one of a set of overloaded functions provides a unique match.
15511 So if we found multiple possibilities, we return success but don't
15512 deduce anything. */
15516 int i
= TREE_VEC_LENGTH (targs
);
15518 if (TREE_VEC_ELT (tempargs
, i
))
15519 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (tempargs
, i
);
15527 /* Core DR 115: In contexts where deduction is done and fails, or in
15528 contexts where deduction is not done, if a template argument list is
15529 specified and it, along with any default template arguments, identifies
15530 a single function template specialization, then the template-id is an
15531 lvalue for the function template specialization. */
15534 resolve_nondeduced_context (tree orig_expr
)
15536 tree expr
, offset
, baselink
;
15539 if (!type_unknown_p (orig_expr
))
15544 offset
= NULL_TREE
;
15545 baselink
= NULL_TREE
;
15547 if (TREE_CODE (expr
) == ADDR_EXPR
)
15549 expr
= TREE_OPERAND (expr
, 0);
15552 if (TREE_CODE (expr
) == OFFSET_REF
)
15555 expr
= TREE_OPERAND (expr
, 1);
15557 if (BASELINK_P (expr
))
15560 expr
= BASELINK_FUNCTIONS (expr
);
15563 if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
15566 tree goodfn
= NULL_TREE
;
15568 /* If we got some explicit template args, we need to plug them into
15569 the affected templates before we try to unify, in case the
15570 explicit args will completely resolve the templates in question. */
15572 tree expl_subargs
= TREE_OPERAND (expr
, 1);
15573 tree arg
= TREE_OPERAND (expr
, 0);
15574 tree badfn
= NULL_TREE
;
15575 tree badargs
= NULL_TREE
;
15577 for (; arg
; arg
= OVL_NEXT (arg
))
15579 tree fn
= OVL_CURRENT (arg
);
15580 tree subargs
, elem
;
15582 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
15585 ++processing_template_decl
;
15586 subargs
= get_bindings (fn
, DECL_TEMPLATE_RESULT (fn
),
15587 expl_subargs
, /*check_ret=*/false);
15588 if (subargs
&& !any_dependent_template_arguments_p (subargs
))
15590 elem
= instantiate_template (fn
, subargs
, tf_none
);
15591 if (elem
== error_mark_node
)
15596 else if (elem
&& (!goodfn
|| !decls_match (goodfn
, elem
)))
15602 --processing_template_decl
;
15606 mark_used (goodfn
);
15609 expr
= build_baselink (BASELINK_BINFO (baselink
),
15610 BASELINK_ACCESS_BINFO (baselink
),
15611 expr
, BASELINK_OPTYPE (baselink
));
15615 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset
, 0)));
15616 expr
= build_offset_ref (base
, expr
, addr
);
15619 expr
= cp_build_addr_expr (expr
, tf_warning_or_error
);
15622 else if (good
== 0 && badargs
)
15623 /* There were no good options and at least one bad one, so let the
15624 user know what the problem is. */
15625 instantiate_template (badfn
, badargs
, tf_warning_or_error
);
15630 /* Subroutine of resolve_overloaded_unification; does deduction for a single
15631 overload. Fills TARGS with any deduced arguments, or error_mark_node if
15632 different overloads deduce different arguments for a given parm.
15633 ADDR_P is true if the expression for which deduction is being
15634 performed was of the form "& fn" rather than simply "fn".
15636 Returns 1 on success. */
15639 try_one_overload (tree tparms
,
15644 unification_kind_t strict
,
15653 /* [temp.deduct.type] A template-argument can be deduced from a pointer
15654 to function or pointer to member function argument if the set of
15655 overloaded functions does not contain function templates and at most
15656 one of a set of overloaded functions provides a unique match.
15658 So if this is a template, just return success. */
15660 if (uses_template_parms (arg
))
15663 if (TREE_CODE (arg
) == METHOD_TYPE
)
15664 arg
= build_ptrmemfunc_type (build_pointer_type (arg
));
15666 arg
= build_pointer_type (arg
);
15668 sub_strict
|= maybe_adjust_types_for_deduction (strict
, &parm
, &arg
, NULL
);
15670 /* We don't copy orig_targs for this because if we have already deduced
15671 some template args from previous args, unify would complain when we
15672 try to deduce a template parameter for the same argument, even though
15673 there isn't really a conflict. */
15674 nargs
= TREE_VEC_LENGTH (targs
);
15675 tempargs
= make_tree_vec (nargs
);
15677 if (unify (tparms
, tempargs
, parm
, arg
, sub_strict
, explain_p
))
15680 /* First make sure we didn't deduce anything that conflicts with
15681 explicitly specified args. */
15682 for (i
= nargs
; i
--; )
15684 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15685 tree oldelt
= TREE_VEC_ELT (orig_targs
, i
);
15689 else if (uses_template_parms (elt
))
15690 /* Since we're unifying against ourselves, we will fill in
15691 template args used in the function parm list with our own
15692 template parms. Discard them. */
15693 TREE_VEC_ELT (tempargs
, i
) = NULL_TREE
;
15694 else if (oldelt
&& !template_args_equal (oldelt
, elt
))
15698 for (i
= nargs
; i
--; )
15700 tree elt
= TREE_VEC_ELT (tempargs
, i
);
15703 TREE_VEC_ELT (targs
, i
) = elt
;
15709 /* PARM is a template class (perhaps with unbound template
15710 parameters). ARG is a fully instantiated type. If ARG can be
15711 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
15712 TARGS are as for unify. */
15715 try_class_unification (tree tparms
, tree targs
, tree parm
, tree arg
,
15718 tree copy_of_targs
;
15720 if (!CLASSTYPE_TEMPLATE_INFO (arg
)
15721 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg
))
15722 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm
))))
15725 /* We need to make a new template argument vector for the call to
15726 unify. If we used TARGS, we'd clutter it up with the result of
15727 the attempted unification, even if this class didn't work out.
15728 We also don't want to commit ourselves to all the unifications
15729 we've already done, since unification is supposed to be done on
15730 an argument-by-argument basis. In other words, consider the
15731 following pathological case:
15733 template <int I, int J, int K>
15736 template <int I, int J>
15737 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
15739 template <int I, int J, int K>
15740 void f(S<I, J, K>, S<I, I, I>);
15749 Now, by the time we consider the unification involving `s2', we
15750 already know that we must have `f<0, 0, 0>'. But, even though
15751 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
15752 because there are two ways to unify base classes of S<0, 1, 2>
15753 with S<I, I, I>. If we kept the already deduced knowledge, we
15754 would reject the possibility I=1. */
15755 copy_of_targs
= make_tree_vec (TREE_VEC_LENGTH (targs
));
15757 /* If unification failed, we're done. */
15758 if (unify (tparms
, copy_of_targs
, CLASSTYPE_TI_ARGS (parm
),
15759 CLASSTYPE_TI_ARGS (arg
), UNIFY_ALLOW_NONE
, explain_p
))
15765 /* Given a template type PARM and a class type ARG, find the unique
15766 base type in ARG that is an instance of PARM. We do not examine
15767 ARG itself; only its base-classes. If there is not exactly one
15768 appropriate base class, return NULL_TREE. PARM may be the type of
15769 a partial specialization, as well as a plain template type. Used
15772 static enum template_base_result
15773 get_template_base (tree tparms
, tree targs
, tree parm
, tree arg
,
15774 bool explain_p
, tree
*result
)
15776 tree rval
= NULL_TREE
;
15779 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg
)));
15781 binfo
= TYPE_BINFO (complete_type (arg
));
15784 /* The type could not be completed. */
15785 *result
= NULL_TREE
;
15786 return tbr_incomplete_type
;
15789 /* Walk in inheritance graph order. The search order is not
15790 important, and this avoids multiple walks of virtual bases. */
15791 for (binfo
= TREE_CHAIN (binfo
); binfo
; binfo
= TREE_CHAIN (binfo
))
15793 tree r
= try_class_unification (tparms
, targs
, parm
,
15794 BINFO_TYPE (binfo
), explain_p
);
15798 /* If there is more than one satisfactory baseclass, then:
15802 If they yield more than one possible deduced A, the type
15806 if (rval
&& !same_type_p (r
, rval
))
15808 *result
= NULL_TREE
;
15809 return tbr_ambiguous_baseclass
;
15817 return tbr_success
;
15820 /* Returns the level of DECL, which declares a template parameter. */
15823 template_decl_level (tree decl
)
15825 switch (TREE_CODE (decl
))
15828 case TEMPLATE_DECL
:
15829 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
15832 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
15835 gcc_unreachable ();
15840 /* Decide whether ARG can be unified with PARM, considering only the
15841 cv-qualifiers of each type, given STRICT as documented for unify.
15842 Returns nonzero iff the unification is OK on that basis. */
15845 check_cv_quals_for_unify (int strict
, tree arg
, tree parm
)
15847 int arg_quals
= cp_type_quals (arg
);
15848 int parm_quals
= cp_type_quals (parm
);
15850 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15851 && !(strict
& UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15853 /* Although a CVR qualifier is ignored when being applied to a
15854 substituted template parameter ([8.3.2]/1 for example), that
15855 does not allow us to unify "const T" with "int&" because both
15856 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
15857 It is ok when we're allowing additional CV qualifiers
15858 at the outer level [14.8.2.1]/3,1st bullet. */
15859 if ((TREE_CODE (arg
) == REFERENCE_TYPE
15860 || TREE_CODE (arg
) == FUNCTION_TYPE
15861 || TREE_CODE (arg
) == METHOD_TYPE
)
15862 && (parm_quals
& (TYPE_QUAL_CONST
| TYPE_QUAL_VOLATILE
)))
15865 if ((!POINTER_TYPE_P (arg
) && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
)
15866 && (parm_quals
& TYPE_QUAL_RESTRICT
))
15870 if (!(strict
& (UNIFY_ALLOW_MORE_CV_QUAL
| UNIFY_ALLOW_OUTER_MORE_CV_QUAL
))
15871 && (arg_quals
& parm_quals
) != parm_quals
)
15874 if (!(strict
& (UNIFY_ALLOW_LESS_CV_QUAL
| UNIFY_ALLOW_OUTER_LESS_CV_QUAL
))
15875 && (parm_quals
& arg_quals
) != arg_quals
)
15881 /* Determines the LEVEL and INDEX for the template parameter PARM. */
15883 template_parm_level_and_index (tree parm
, int* level
, int* index
)
15885 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
15886 || TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
15887 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
15889 *index
= TEMPLATE_TYPE_IDX (parm
);
15890 *level
= TEMPLATE_TYPE_LEVEL (parm
);
15894 *index
= TEMPLATE_PARM_IDX (parm
);
15895 *level
= TEMPLATE_PARM_LEVEL (parm
);
15899 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
15901 if (unify (TP, TA, P, A, S, EP)) \
15905 /* Unifies the remaining arguments in PACKED_ARGS with the pack
15906 expansion at the end of PACKED_PARMS. Returns 0 if the type
15907 deduction succeeds, 1 otherwise. STRICT is the same as in
15908 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
15909 call argument list. We'll need to adjust the arguments to make them
15910 types. SUBR tells us if this is from a recursive call to
15911 type_unification_real, or for comparing two template argument
15915 unify_pack_expansion (tree tparms
, tree targs
, tree packed_parms
,
15916 tree packed_args
, unification_kind_t strict
,
15917 bool subr
, bool explain_p
)
15920 = TREE_VEC_ELT (packed_parms
, TREE_VEC_LENGTH (packed_parms
) - 1);
15921 tree pattern
= PACK_EXPANSION_PATTERN (parm
);
15922 tree pack
, packs
= NULL_TREE
;
15923 int i
, start
= TREE_VEC_LENGTH (packed_parms
) - 1;
15924 int len
= TREE_VEC_LENGTH (packed_args
);
15926 /* Determine the parameter packs we will be deducing from the
15927 pattern, and record their current deductions. */
15928 for (pack
= PACK_EXPANSION_PARAMETER_PACKS (parm
);
15929 pack
; pack
= TREE_CHAIN (pack
))
15931 tree parm_pack
= TREE_VALUE (pack
);
15934 /* Determine the index and level of this parameter pack. */
15935 template_parm_level_and_index (parm_pack
, &level
, &idx
);
15937 /* Keep track of the parameter packs and their corresponding
15939 packs
= tree_cons (parm_pack
, TMPL_ARG (targs
, level
, idx
), packs
);
15940 TREE_TYPE (packs
) = make_tree_vec (len
- start
);
15943 /* Loop through all of the arguments that have not yet been
15944 unified and unify each with the pattern. */
15945 for (i
= start
; i
< len
; i
++)
15948 bool any_explicit
= false;
15949 tree arg
= TREE_VEC_ELT (packed_args
, i
);
15951 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
15952 or the element of its argument pack at the current index if
15953 this argument was explicitly specified. */
15954 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
15958 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
15961 if (TREE_VALUE (pack
)
15962 && (pargs
= ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack
)))
15963 && (i
< TREE_VEC_LENGTH (pargs
)))
15965 any_explicit
= true;
15966 arg
= TREE_VEC_ELT (pargs
, i
);
15968 TMPL_ARG (targs
, level
, idx
) = arg
;
15971 /* If we had explicit template arguments, substitute them into the
15972 pattern before deduction. */
15975 /* Some arguments might still be unspecified or dependent. */
15977 ++processing_template_decl
;
15978 dependent
= any_dependent_template_arguments_p (targs
);
15980 --processing_template_decl
;
15981 parm
= tsubst (pattern
, targs
,
15982 explain_p
? tf_warning_or_error
: tf_none
,
15985 --processing_template_decl
;
15986 if (parm
== error_mark_node
)
15992 /* Unify the pattern with the current argument. */
15993 if (unify_one_argument (tparms
, targs
, parm
, arg
, subr
, strict
,
15994 LOOKUP_IMPLICIT
, explain_p
))
15997 /* For each parameter pack, collect the deduced value. */
15998 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16001 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16003 TREE_VEC_ELT (TREE_TYPE (pack
), i
- start
) =
16004 TMPL_ARG (targs
, level
, idx
);
16008 /* Verify that the results of unification with the parameter packs
16009 produce results consistent with what we've seen before, and make
16010 the deduced argument packs available. */
16011 for (pack
= packs
; pack
; pack
= TREE_CHAIN (pack
))
16013 tree old_pack
= TREE_VALUE (pack
);
16014 tree new_args
= TREE_TYPE (pack
);
16015 int i
, len
= TREE_VEC_LENGTH (new_args
);
16017 bool nondeduced_p
= false;
16019 /* By default keep the original deduced argument pack.
16020 If necessary, more specific code is going to update the
16021 resulting deduced argument later down in this function. */
16022 template_parm_level_and_index (TREE_PURPOSE (pack
), &level
, &idx
);
16023 TMPL_ARG (targs
, level
, idx
) = old_pack
;
16025 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
16026 actually deduce anything. */
16027 for (i
= 0; i
< len
&& !nondeduced_p
; ++i
)
16028 if (TREE_VEC_ELT (new_args
, i
) == NULL_TREE
)
16029 nondeduced_p
= true;
16033 if (old_pack
&& ARGUMENT_PACK_INCOMPLETE_P (old_pack
))
16035 /* If we had fewer function args than explicit template args,
16036 just use the explicits. */
16037 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16038 int explicit_len
= TREE_VEC_LENGTH (explicit_args
);
16039 if (len
< explicit_len
)
16040 new_args
= explicit_args
;
16046 /* Build the deduced *_ARGUMENT_PACK. */
16047 if (TREE_CODE (TREE_PURPOSE (pack
)) == TEMPLATE_PARM_INDEX
)
16049 result
= make_node (NONTYPE_ARGUMENT_PACK
);
16050 TREE_TYPE (result
) =
16051 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack
)));
16052 TREE_CONSTANT (result
) = 1;
16055 result
= cxx_make_type (TYPE_ARGUMENT_PACK
);
16057 SET_ARGUMENT_PACK_ARGS (result
, new_args
);
16059 /* Note the deduced argument packs for this parameter
16061 TMPL_ARG (targs
, level
, idx
) = result
;
16063 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack
)
16064 && (ARGUMENT_PACK_ARGS (old_pack
)
16065 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
)))
16067 /* We only had the explicitly-provided arguments before, but
16068 now we have a complete set of arguments. */
16069 tree explicit_args
= ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
);
16071 SET_ARGUMENT_PACK_ARGS (old_pack
, new_args
);
16072 ARGUMENT_PACK_INCOMPLETE_P (old_pack
) = 1;
16073 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack
) = explicit_args
;
16077 tree bad_old_arg
= NULL_TREE
, bad_new_arg
= NULL_TREE
;
16078 tree old_args
= ARGUMENT_PACK_ARGS (old_pack
);
16080 if (!comp_template_args_with_info (old_args
, new_args
,
16081 &bad_old_arg
, &bad_new_arg
))
16082 /* Inconsistent unification of this parameter pack. */
16083 return unify_parameter_pack_inconsistent (explain_p
,
16089 return unify_success (explain_p
);
16092 /* Deduce the value of template parameters. TPARMS is the (innermost)
16093 set of template parameters to a template. TARGS is the bindings
16094 for those template parameters, as determined thus far; TARGS may
16095 include template arguments for outer levels of template parameters
16096 as well. PARM is a parameter to a template function, or a
16097 subcomponent of that parameter; ARG is the corresponding argument.
16098 This function attempts to match PARM with ARG in a manner
16099 consistent with the existing assignments in TARGS. If more values
16100 are deduced, then TARGS is updated.
16102 Returns 0 if the type deduction succeeds, 1 otherwise. The
16103 parameter STRICT is a bitwise or of the following flags:
16106 Require an exact match between PARM and ARG.
16107 UNIFY_ALLOW_MORE_CV_QUAL:
16108 Allow the deduced ARG to be more cv-qualified (by qualification
16109 conversion) than ARG.
16110 UNIFY_ALLOW_LESS_CV_QUAL:
16111 Allow the deduced ARG to be less cv-qualified than ARG.
16112 UNIFY_ALLOW_DERIVED:
16113 Allow the deduced ARG to be a template base class of ARG,
16114 or a pointer to a template base class of the type pointed to by
16116 UNIFY_ALLOW_INTEGER:
16117 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
16118 case for more information.
16119 UNIFY_ALLOW_OUTER_LEVEL:
16120 This is the outermost level of a deduction. Used to determine validity
16121 of qualification conversions. A valid qualification conversion must
16122 have const qualified pointers leading up to the inner type which
16123 requires additional CV quals, except at the outer level, where const
16124 is not required [conv.qual]. It would be normal to set this flag in
16125 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
16126 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
16127 This is the outermost level of a deduction, and PARM can be more CV
16128 qualified at this point.
16129 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
16130 This is the outermost level of a deduction, and PARM can be less CV
16131 qualified at this point. */
16134 unify (tree tparms
, tree targs
, tree parm
, tree arg
, int strict
,
16140 int strict_in
= strict
;
16142 /* I don't think this will do the right thing with respect to types.
16143 But the only case I've seen it in so far has been array bounds, where
16144 signedness is the only information lost, and I think that will be
16146 while (TREE_CODE (parm
) == NOP_EXPR
)
16147 parm
= TREE_OPERAND (parm
, 0);
16149 if (arg
== error_mark_node
)
16150 return unify_invalid (explain_p
);
16151 if (arg
== unknown_type_node
16152 || arg
== init_list_type_node
)
16153 /* We can't deduce anything from this, but we might get all the
16154 template args from other function args. */
16155 return unify_success (explain_p
);
16157 /* If PARM uses template parameters, then we can't bail out here,
16158 even if ARG == PARM, since we won't record unifications for the
16159 template parameters. We might need them if we're trying to
16160 figure out which of two things is more specialized. */
16161 if (arg
== parm
&& !uses_template_parms (parm
))
16162 return unify_success (explain_p
);
16164 /* Handle init lists early, so the rest of the function can assume
16165 we're dealing with a type. */
16166 if (BRACE_ENCLOSED_INITIALIZER_P (arg
))
16170 tree orig_parm
= parm
;
16172 /* Replace T with std::initializer_list<T> for deduction. */
16173 if (TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16174 && flag_deduce_init_list
)
16175 parm
= listify (parm
);
16177 if (!is_std_init_list (parm
))
16178 /* We can only deduce from an initializer list argument if the
16179 parameter is std::initializer_list; otherwise this is a
16180 non-deduced context. */
16181 return unify_success (explain_p
);
16183 elttype
= TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm
), 0);
16185 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg
), i
, elt
)
16187 int elt_strict
= strict
;
16189 if (elt
== error_mark_node
)
16190 return unify_invalid (explain_p
);
16192 if (!BRACE_ENCLOSED_INITIALIZER_P (elt
))
16194 tree type
= TREE_TYPE (elt
);
16195 /* It should only be possible to get here for a call. */
16196 gcc_assert (elt_strict
& UNIFY_ALLOW_OUTER_LEVEL
);
16197 elt_strict
|= maybe_adjust_types_for_deduction
16198 (DEDUCE_CALL
, &elttype
, &type
, elt
);
16202 RECUR_AND_CHECK_FAILURE (tparms
, targs
, elttype
, elt
, elt_strict
,
16206 /* If the std::initializer_list<T> deduction worked, replace the
16207 deduced A with std::initializer_list<A>. */
16208 if (orig_parm
!= parm
)
16210 idx
= TEMPLATE_TYPE_IDX (orig_parm
);
16211 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16212 targ
= listify (targ
);
16213 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = targ
;
16215 return unify_success (explain_p
);
16218 /* Immediately reject some pairs that won't unify because of
16219 cv-qualification mismatches. */
16220 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16222 /* It is the elements of the array which hold the cv quals of an array
16223 type, and the elements might be template type parms. We'll check
16224 when we recurse. */
16225 && TREE_CODE (arg
) != ARRAY_TYPE
16226 /* We check the cv-qualifiers when unifying with template type
16227 parameters below. We want to allow ARG `const T' to unify with
16228 PARM `T' for example, when computing which of two templates
16229 is more specialized, for example. */
16230 && TREE_CODE (arg
) != TEMPLATE_TYPE_PARM
16231 && !check_cv_quals_for_unify (strict_in
, arg
, parm
))
16232 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16234 if (!(strict
& UNIFY_ALLOW_OUTER_LEVEL
)
16235 && TYPE_P (parm
) && !CP_TYPE_CONST_P (parm
))
16236 strict
&= ~UNIFY_ALLOW_MORE_CV_QUAL
;
16237 strict
&= ~UNIFY_ALLOW_OUTER_LEVEL
;
16238 strict
&= ~UNIFY_ALLOW_DERIVED
;
16239 strict
&= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL
;
16240 strict
&= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL
;
16242 switch (TREE_CODE (parm
))
16244 case TYPENAME_TYPE
:
16246 case UNBOUND_CLASS_TEMPLATE
:
16247 /* In a type which contains a nested-name-specifier, template
16248 argument values cannot be deduced for template parameters used
16249 within the nested-name-specifier. */
16250 return unify_success (explain_p
);
16252 case TEMPLATE_TYPE_PARM
:
16253 case TEMPLATE_TEMPLATE_PARM
:
16254 case BOUND_TEMPLATE_TEMPLATE_PARM
:
16255 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16256 if (tparm
== error_mark_node
)
16257 return unify_invalid (explain_p
);
16259 if (TEMPLATE_TYPE_LEVEL (parm
)
16260 != template_decl_level (tparm
))
16261 /* The PARM is not one we're trying to unify. Just check
16262 to see if it matches ARG. */
16264 if (TREE_CODE (arg
) == TREE_CODE (parm
)
16265 && same_type_p (parm
, arg
))
16266 return unify_success (explain_p
);
16268 return unify_type_mismatch (explain_p
, parm
, arg
);
16270 idx
= TEMPLATE_TYPE_IDX (parm
);
16271 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16272 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
16273 if (tparm
== error_mark_node
)
16274 return unify_invalid (explain_p
);
16276 /* Check for mixed types and values. */
16277 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
16278 && TREE_CODE (tparm
) != TYPE_DECL
)
16279 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16280 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
16281 gcc_unreachable ();
16283 if (TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16285 /* ARG must be constructed from a template class or a template
16286 template parameter. */
16287 if (TREE_CODE (arg
) != BOUND_TEMPLATE_TEMPLATE_PARM
16288 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg
))
16289 return unify_template_deduction_failure (explain_p
, parm
, arg
);
16292 tree parmvec
= TYPE_TI_ARGS (parm
);
16293 tree argvec
= INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg
));
16294 tree full_argvec
= add_to_template_args (targs
, argvec
);
16296 = DECL_INNERMOST_TEMPLATE_PARMS
16297 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm
));
16299 int parm_variadic_p
= 0;
16301 /* The resolution to DR150 makes clear that default
16302 arguments for an N-argument may not be used to bind T
16303 to a template template parameter with fewer than N
16304 parameters. It is not safe to permit the binding of
16305 default arguments as an extension, as that may change
16306 the meaning of a conforming program. Consider:
16308 struct Dense { static const unsigned int dim = 1; };
16310 template <template <typename> class View,
16312 void operator+(float, View<Block> const&);
16314 template <typename Block,
16315 unsigned int Dim = Block::dim>
16316 struct Lvalue_proxy { operator float() const; };
16320 Lvalue_proxy<Dense> p;
16325 Here, if Lvalue_proxy is permitted to bind to View, then
16326 the global operator+ will be used; if they are not, the
16327 Lvalue_proxy will be converted to float. */
16328 if (coerce_template_parms (parm_parms
,
16330 TYPE_TI_TEMPLATE (parm
),
16332 ? tf_warning_or_error
16334 /*require_all_args=*/true,
16335 /*use_default_args=*/false)
16336 == error_mark_node
)
16339 /* Deduce arguments T, i from TT<T> or TT<i>.
16340 We check each element of PARMVEC and ARGVEC individually
16341 rather than the whole TREE_VEC since they can have
16342 different number of elements. */
16344 parmvec
= expand_template_argument_pack (parmvec
);
16345 argvec
= expand_template_argument_pack (argvec
);
16347 len
= TREE_VEC_LENGTH (parmvec
);
16349 /* Check if the parameters end in a pack, making them
16352 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec
, len
- 1)))
16353 parm_variadic_p
= 1;
16355 if (TREE_VEC_LENGTH (argvec
) < len
- parm_variadic_p
)
16356 return unify_too_few_arguments (explain_p
,
16357 TREE_VEC_LENGTH (argvec
), len
);
16359 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16361 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16362 TREE_VEC_ELT (parmvec
, i
),
16363 TREE_VEC_ELT (argvec
, i
),
16364 UNIFY_ALLOW_NONE
, explain_p
);
16367 if (parm_variadic_p
16368 && unify_pack_expansion (tparms
, targs
,
16371 /*subr=*/true, explain_p
))
16374 arg
= TYPE_TI_TEMPLATE (arg
);
16376 /* Fall through to deduce template name. */
16379 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
16380 || TREE_CODE (parm
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
16382 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
16384 /* Simple cases: Value already set, does match or doesn't. */
16385 if (targ
!= NULL_TREE
&& template_args_equal (targ
, arg
))
16386 return unify_success (explain_p
);
16388 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16392 /* If PARM is `const T' and ARG is only `int', we don't have
16393 a match unless we are allowing additional qualification.
16394 If ARG is `const int' and PARM is just `T' that's OK;
16395 that binds `const int' to `T'. */
16396 if (!check_cv_quals_for_unify (strict_in
| UNIFY_ALLOW_LESS_CV_QUAL
,
16398 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16400 /* Consider the case where ARG is `const volatile int' and
16401 PARM is `const T'. Then, T should be `volatile int'. */
16402 arg
= cp_build_qualified_type_real
16403 (arg
, cp_type_quals (arg
) & ~cp_type_quals (parm
), tf_none
);
16404 if (arg
== error_mark_node
)
16405 return unify_invalid (explain_p
);
16407 /* Simple cases: Value already set, does match or doesn't. */
16408 if (targ
!= NULL_TREE
&& same_type_p (targ
, arg
))
16409 return unify_success (explain_p
);
16411 return unify_inconsistency (explain_p
, parm
, targ
, arg
);
16413 /* Make sure that ARG is not a variable-sized array. (Note
16414 that were talking about variable-sized arrays (like
16415 `int[n]'), rather than arrays of unknown size (like
16416 `int[]').) We'll get very confused by such a type since
16417 the bound of the array is not constant, and therefore
16418 not mangleable. Besides, such types are not allowed in
16419 ISO C++, so we can do as we please here. We do allow
16420 them for 'auto' deduction, since that isn't ABI-exposed. */
16421 if (!is_auto (parm
) && variably_modified_type_p (arg
, NULL_TREE
))
16422 return unify_vla_arg (explain_p
, arg
);
16424 /* Strip typedefs as in convert_template_argument. */
16425 arg
= canonicalize_type_argument (arg
, tf_none
);
16428 /* If ARG is a parameter pack or an expansion, we cannot unify
16429 against it unless PARM is also a parameter pack. */
16430 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16431 && !template_parameter_pack_p (parm
))
16432 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16434 /* If the argument deduction results is a METHOD_TYPE,
16435 then there is a problem.
16436 METHOD_TYPE doesn't map to any real C++ type the result of
16437 the deduction can not be of that type. */
16438 if (TREE_CODE (arg
) == METHOD_TYPE
)
16439 return unify_method_type_error (explain_p
, arg
);
16441 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16442 return unify_success (explain_p
);
16444 case TEMPLATE_PARM_INDEX
:
16445 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
16446 if (tparm
== error_mark_node
)
16447 return unify_invalid (explain_p
);
16449 if (TEMPLATE_PARM_LEVEL (parm
)
16450 != template_decl_level (tparm
))
16452 /* The PARM is not one we're trying to unify. Just check
16453 to see if it matches ARG. */
16454 int result
= !(TREE_CODE (arg
) == TREE_CODE (parm
)
16455 && cp_tree_equal (parm
, arg
));
16457 unify_expression_unequal (explain_p
, parm
, arg
);
16461 idx
= TEMPLATE_PARM_IDX (parm
);
16462 targ
= TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
);
16466 int x
= !cp_tree_equal (targ
, arg
);
16468 unify_inconsistency (explain_p
, parm
, targ
, arg
);
16472 /* [temp.deduct.type] If, in the declaration of a function template
16473 with a non-type template-parameter, the non-type
16474 template-parameter is used in an expression in the function
16475 parameter-list and, if the corresponding template-argument is
16476 deduced, the template-argument type shall match the type of the
16477 template-parameter exactly, except that a template-argument
16478 deduced from an array bound may be of any integral type.
16479 The non-type parameter might use already deduced type parameters. */
16480 tparm
= tsubst (TREE_TYPE (parm
), targs
, 0, NULL_TREE
);
16481 if (!TREE_TYPE (arg
))
16482 /* Template-parameter dependent expression. Just accept it for now.
16483 It will later be processed in convert_template_argument. */
16485 else if (same_type_p (TREE_TYPE (arg
), tparm
))
16487 else if ((strict
& UNIFY_ALLOW_INTEGER
)
16488 && (TREE_CODE (tparm
) == INTEGER_TYPE
16489 || TREE_CODE (tparm
) == BOOLEAN_TYPE
))
16490 /* Convert the ARG to the type of PARM; the deduced non-type
16491 template argument must exactly match the types of the
16492 corresponding parameter. */
16493 arg
= fold (build_nop (tparm
, arg
));
16494 else if (uses_template_parms (tparm
))
16495 /* We haven't deduced the type of this parameter yet. Try again
16497 return unify_success (explain_p
);
16499 return unify_type_mismatch (explain_p
, tparm
, arg
);
16501 /* If ARG is a parameter pack or an expansion, we cannot unify
16502 against it unless PARM is also a parameter pack. */
16503 if ((template_parameter_pack_p (arg
) || PACK_EXPANSION_P (arg
))
16504 && !TEMPLATE_PARM_PARAMETER_PACK (parm
))
16505 return unify_parameter_pack_mismatch (explain_p
, parm
, arg
);
16507 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs
), idx
) = arg
;
16508 return unify_success (explain_p
);
16512 /* A pointer-to-member constant can be unified only with
16513 another constant. */
16514 if (TREE_CODE (arg
) != PTRMEM_CST
)
16515 return unify_ptrmem_cst_mismatch (explain_p
, parm
, arg
);
16517 /* Just unify the class member. It would be useless (and possibly
16518 wrong, depending on the strict flags) to unify also
16519 PTRMEM_CST_CLASS, because we want to be sure that both parm and
16520 arg refer to the same variable, even if through different
16521 classes. For instance:
16523 struct A { int x; };
16526 Unification of &A::x and &B::x must succeed. */
16527 return unify (tparms
, targs
, PTRMEM_CST_MEMBER (parm
),
16528 PTRMEM_CST_MEMBER (arg
), strict
, explain_p
);
16533 if (TREE_CODE (arg
) != POINTER_TYPE
)
16534 return unify_type_mismatch (explain_p
, parm
, arg
);
16536 /* [temp.deduct.call]
16538 A can be another pointer or pointer to member type that can
16539 be converted to the deduced A via a qualification
16540 conversion (_conv.qual_).
16542 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
16543 This will allow for additional cv-qualification of the
16544 pointed-to types if appropriate. */
16546 if (TREE_CODE (TREE_TYPE (arg
)) == RECORD_TYPE
)
16547 /* The derived-to-base conversion only persists through one
16548 level of pointers. */
16549 strict
|= (strict_in
& UNIFY_ALLOW_DERIVED
);
16551 return unify (tparms
, targs
, TREE_TYPE (parm
),
16552 TREE_TYPE (arg
), strict
, explain_p
);
16555 case REFERENCE_TYPE
:
16556 if (TREE_CODE (arg
) != REFERENCE_TYPE
)
16557 return unify_type_mismatch (explain_p
, parm
, arg
);
16558 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16559 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16562 if (TREE_CODE (arg
) != ARRAY_TYPE
)
16563 return unify_type_mismatch (explain_p
, parm
, arg
);
16564 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
16565 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
16566 return unify_type_mismatch (explain_p
, parm
, arg
);
16567 if (TYPE_DOMAIN (parm
) != NULL_TREE
)
16574 /* Our representation of array types uses "N - 1" as the
16575 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
16576 not an integer constant. We cannot unify arbitrarily
16577 complex expressions, so we eliminate the MINUS_EXPRs
16579 parm_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (parm
));
16580 parm_cst
= TREE_CODE (parm_max
) == INTEGER_CST
;
16583 gcc_assert (TREE_CODE (parm_max
) == MINUS_EXPR
);
16584 parm_max
= TREE_OPERAND (parm_max
, 0);
16586 arg_max
= TYPE_MAX_VALUE (TYPE_DOMAIN (arg
));
16587 arg_cst
= TREE_CODE (arg_max
) == INTEGER_CST
;
16590 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
16591 trying to unify the type of a variable with the type
16592 of a template parameter. For example:
16594 template <unsigned int N>
16595 void f (char (&) [N]);
16602 Here, the type of the ARG will be "int [g(i)]", and
16603 may be a SAVE_EXPR, etc. */
16604 if (TREE_CODE (arg_max
) != MINUS_EXPR
)
16605 return unify_vla_arg (explain_p
, arg
);
16606 arg_max
= TREE_OPERAND (arg_max
, 0);
16609 /* If only one of the bounds used a MINUS_EXPR, compensate
16610 by adding one to the other bound. */
16611 if (parm_cst
&& !arg_cst
)
16612 parm_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16616 else if (arg_cst
&& !parm_cst
)
16617 arg_max
= fold_build2_loc (input_location
, PLUS_EXPR
,
16622 RECUR_AND_CHECK_FAILURE (tparms
, targs
, parm_max
, arg_max
,
16623 UNIFY_ALLOW_INTEGER
, explain_p
);
16625 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16626 strict
& UNIFY_ALLOW_MORE_CV_QUAL
, explain_p
);
16633 case ENUMERAL_TYPE
:
16636 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16637 return unify_type_mismatch (explain_p
, parm
, arg
);
16639 /* We have already checked cv-qualification at the top of the
16641 if (!same_type_ignoring_top_level_qualifiers_p (arg
, parm
))
16642 return unify_type_mismatch (explain_p
, parm
, arg
);
16644 /* As far as unification is concerned, this wins. Later checks
16645 will invalidate it if necessary. */
16646 return unify_success (explain_p
);
16648 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
16649 /* Type INTEGER_CST can come from ordinary constant template args. */
16651 while (TREE_CODE (arg
) == NOP_EXPR
)
16652 arg
= TREE_OPERAND (arg
, 0);
16654 if (TREE_CODE (arg
) != INTEGER_CST
)
16655 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16656 return (tree_int_cst_equal (parm
, arg
)
16657 ? unify_success (explain_p
)
16658 : unify_template_argument_mismatch (explain_p
, parm
, arg
));
16662 int i
, len
, argslen
;
16663 int parm_variadic_p
= 0;
16665 if (TREE_CODE (arg
) != TREE_VEC
)
16666 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16668 len
= TREE_VEC_LENGTH (parm
);
16669 argslen
= TREE_VEC_LENGTH (arg
);
16671 /* Check for pack expansions in the parameters. */
16672 for (i
= 0; i
< len
; ++i
)
16674 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm
, i
)))
16677 /* We can unify against something with a trailing
16679 parm_variadic_p
= 1;
16681 /* [temp.deduct.type]/9: If the template argument list of
16682 P contains a pack expansion that is not the last
16683 template argument, the entire template argument list
16684 is a non-deduced context. */
16685 return unify_success (explain_p
);
16689 /* If we don't have enough arguments to satisfy the parameters
16690 (not counting the pack expression at the end), or we have
16691 too many arguments for a parameter list that doesn't end in
16692 a pack expression, we can't unify. */
16693 if (parm_variadic_p
16694 ? argslen
< len
- parm_variadic_p
16696 return unify_arity (explain_p
, TREE_VEC_LENGTH (arg
), len
);
16698 /* Unify all of the parameters that precede the (optional)
16699 pack expression. */
16700 for (i
= 0; i
< len
- parm_variadic_p
; ++i
)
16702 RECUR_AND_CHECK_FAILURE (tparms
, targs
,
16703 TREE_VEC_ELT (parm
, i
),
16704 TREE_VEC_ELT (arg
, i
),
16705 UNIFY_ALLOW_NONE
, explain_p
);
16707 if (parm_variadic_p
)
16708 return unify_pack_expansion (tparms
, targs
, parm
, arg
,
16710 /*subr=*/true, explain_p
);
16711 return unify_success (explain_p
);
16716 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16717 return unify_type_mismatch (explain_p
, parm
, arg
);
16719 if (TYPE_PTRMEMFUNC_P (parm
))
16721 if (!TYPE_PTRMEMFUNC_P (arg
))
16722 return unify_type_mismatch (explain_p
, parm
, arg
);
16724 return unify (tparms
, targs
,
16725 TYPE_PTRMEMFUNC_FN_TYPE (parm
),
16726 TYPE_PTRMEMFUNC_FN_TYPE (arg
),
16727 strict
, explain_p
);
16730 if (CLASSTYPE_TEMPLATE_INFO (parm
))
16732 tree t
= NULL_TREE
;
16734 if (strict_in
& UNIFY_ALLOW_DERIVED
)
16736 /* First, we try to unify the PARM and ARG directly. */
16737 t
= try_class_unification (tparms
, targs
,
16738 parm
, arg
, explain_p
);
16742 /* Fallback to the special case allowed in
16743 [temp.deduct.call]:
16745 If P is a class, and P has the form
16746 template-id, then A can be a derived class of
16747 the deduced A. Likewise, if P is a pointer to
16748 a class of the form template-id, A can be a
16749 pointer to a derived class pointed to by the
16751 enum template_base_result r
;
16752 r
= get_template_base (tparms
, targs
, parm
, arg
,
16756 return unify_no_common_base (explain_p
, r
, parm
, arg
);
16759 else if (CLASSTYPE_TEMPLATE_INFO (arg
)
16760 && (CLASSTYPE_TI_TEMPLATE (parm
)
16761 == CLASSTYPE_TI_TEMPLATE (arg
)))
16762 /* Perhaps PARM is something like S<U> and ARG is S<int>.
16763 Then, we should unify `int' and `U'. */
16766 /* There's no chance of unification succeeding. */
16767 return unify_type_mismatch (explain_p
, parm
, arg
);
16769 return unify (tparms
, targs
, CLASSTYPE_TI_ARGS (parm
),
16770 CLASSTYPE_TI_ARGS (t
), UNIFY_ALLOW_NONE
, explain_p
);
16772 else if (!same_type_ignoring_top_level_qualifiers_p (parm
, arg
))
16773 return unify_type_mismatch (explain_p
, parm
, arg
);
16774 return unify_success (explain_p
);
16777 case FUNCTION_TYPE
:
16779 unsigned int nargs
;
16784 if (TREE_CODE (arg
) != TREE_CODE (parm
))
16785 return unify_type_mismatch (explain_p
, parm
, arg
);
16787 /* CV qualifications for methods can never be deduced, they must
16788 match exactly. We need to check them explicitly here,
16789 because type_unification_real treats them as any other
16790 cv-qualified parameter. */
16791 if (TREE_CODE (parm
) == METHOD_TYPE
16792 && (!check_cv_quals_for_unify
16794 class_of_this_parm (arg
),
16795 class_of_this_parm (parm
))))
16796 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16798 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TREE_TYPE (parm
),
16799 TREE_TYPE (arg
), UNIFY_ALLOW_NONE
, explain_p
);
16801 nargs
= list_length (TYPE_ARG_TYPES (arg
));
16802 args
= XALLOCAVEC (tree
, nargs
);
16803 for (a
= TYPE_ARG_TYPES (arg
), i
= 0;
16804 a
!= NULL_TREE
&& a
!= void_list_node
;
16805 a
= TREE_CHAIN (a
), ++i
)
16806 args
[i
] = TREE_VALUE (a
);
16809 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
16810 args
, nargs
, 1, DEDUCE_EXACT
,
16811 LOOKUP_NORMAL
, explain_p
);
16815 /* Unify a pointer to member with a pointer to member function, which
16816 deduces the type of the member as a function type. */
16817 if (TYPE_PTRMEMFUNC_P (arg
))
16822 /* Check top-level cv qualifiers */
16823 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE
, arg
, parm
))
16824 return unify_cv_qual_mismatch (explain_p
, parm
, arg
);
16826 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16827 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg
),
16828 UNIFY_ALLOW_NONE
, explain_p
);
16830 /* Determine the type of the function we are unifying against. */
16831 method_type
= TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg
));
16833 build_function_type (TREE_TYPE (method_type
),
16834 TREE_CHAIN (TYPE_ARG_TYPES (method_type
)));
16836 /* Extract the cv-qualifiers of the member function from the
16837 implicit object parameter and place them on the function
16838 type to be restored later. */
16839 fntype
= apply_memfn_quals (fntype
, type_memfn_quals (method_type
));
16840 return unify (tparms
, targs
, TREE_TYPE (parm
), fntype
, strict
, explain_p
);
16843 if (TREE_CODE (arg
) != OFFSET_TYPE
)
16844 return unify_type_mismatch (explain_p
, parm
, arg
);
16845 RECUR_AND_CHECK_FAILURE (tparms
, targs
, TYPE_OFFSET_BASETYPE (parm
),
16846 TYPE_OFFSET_BASETYPE (arg
),
16847 UNIFY_ALLOW_NONE
, explain_p
);
16848 return unify (tparms
, targs
, TREE_TYPE (parm
), TREE_TYPE (arg
),
16849 strict
, explain_p
);
16852 if (DECL_TEMPLATE_PARM_P (parm
))
16853 return unify (tparms
, targs
, DECL_INITIAL (parm
), arg
, strict
, explain_p
);
16854 if (arg
!= integral_constant_value (parm
))
16855 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16856 return unify_success (explain_p
);
16859 case TEMPLATE_DECL
:
16860 /* Matched cases are handled by the ARG == PARM test above. */
16861 return unify_template_argument_mismatch (explain_p
, parm
, arg
);
16864 /* A non-type template parameter that is a variable should be a
16865 an integral constant, in which case, it whould have been
16866 folded into its (constant) value. So we should not be getting
16867 a variable here. */
16868 gcc_unreachable ();
16870 case TYPE_ARGUMENT_PACK
:
16871 case NONTYPE_ARGUMENT_PACK
:
16872 return unify (tparms
, targs
, ARGUMENT_PACK_ARGS (parm
),
16873 ARGUMENT_PACK_ARGS (arg
), strict
, explain_p
);
16876 case DECLTYPE_TYPE
:
16877 case UNDERLYING_TYPE
:
16878 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
16879 or UNDERLYING_TYPE nodes. */
16880 return unify_success (explain_p
);
16883 /* Unification fails if we hit an error node. */
16884 return unify_invalid (explain_p
);
16887 /* An unresolved overload is a nondeduced context. */
16888 if (is_overloaded_fn (parm
) || type_unknown_p (parm
))
16889 return unify_success (explain_p
);
16890 gcc_assert (EXPR_P (parm
));
16892 /* We must be looking at an expression. This can happen with
16896 void foo(S<I>, S<I + 2>);
16898 This is a "nondeduced context":
16902 The nondeduced contexts are:
16904 --A type that is a template-id in which one or more of
16905 the template-arguments is an expression that references
16906 a template-parameter.
16908 In these cases, we assume deduction succeeded, but don't
16909 actually infer any unifications. */
16911 if (!uses_template_parms (parm
)
16912 && !template_args_equal (parm
, arg
))
16913 return unify_expression_unequal (explain_p
, parm
, arg
);
16915 return unify_success (explain_p
);
16918 #undef RECUR_AND_CHECK_FAILURE
16920 /* Note that DECL can be defined in this translation unit, if
16924 mark_definable (tree decl
)
16927 DECL_NOT_REALLY_EXTERN (decl
) = 1;
16928 FOR_EACH_CLONE (clone
, decl
)
16929 DECL_NOT_REALLY_EXTERN (clone
) = 1;
16932 /* Called if RESULT is explicitly instantiated, or is a member of an
16933 explicitly instantiated class. */
16936 mark_decl_instantiated (tree result
, int extern_p
)
16938 SET_DECL_EXPLICIT_INSTANTIATION (result
);
16940 /* If this entity has already been written out, it's too late to
16941 make any modifications. */
16942 if (TREE_ASM_WRITTEN (result
))
16945 if (TREE_CODE (result
) != FUNCTION_DECL
)
16946 /* The TREE_PUBLIC flag for function declarations will have been
16947 set correctly by tsubst. */
16948 TREE_PUBLIC (result
) = 1;
16950 /* This might have been set by an earlier implicit instantiation. */
16951 DECL_COMDAT (result
) = 0;
16954 DECL_NOT_REALLY_EXTERN (result
) = 0;
16957 mark_definable (result
);
16958 /* Always make artificials weak. */
16959 if (DECL_ARTIFICIAL (result
) && flag_weak
)
16960 comdat_linkage (result
);
16961 /* For WIN32 we also want to put explicit instantiations in
16962 linkonce sections. */
16963 else if (TREE_PUBLIC (result
))
16964 maybe_make_one_only (result
);
16967 /* If EXTERN_P, then this function will not be emitted -- unless
16968 followed by an explicit instantiation, at which point its linkage
16969 will be adjusted. If !EXTERN_P, then this function will be
16970 emitted here. In neither circumstance do we want
16971 import_export_decl to adjust the linkage. */
16972 DECL_INTERFACE_KNOWN (result
) = 1;
16975 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
16976 important template arguments. If any are missing, we check whether
16977 they're important by using error_mark_node for substituting into any
16978 args that were used for partial ordering (the ones between ARGS and END)
16979 and seeing if it bubbles up. */
16982 check_undeduced_parms (tree targs
, tree args
, tree end
)
16984 bool found
= false;
16986 for (i
= TREE_VEC_LENGTH (targs
) - 1; i
>= 0; --i
)
16987 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
16990 TREE_VEC_ELT (targs
, i
) = error_mark_node
;
16994 for (; args
!= end
; args
= TREE_CHAIN (args
))
16996 tree substed
= tsubst (TREE_VALUE (args
), targs
, tf_none
, NULL_TREE
);
16997 if (substed
== error_mark_node
)
17004 /* Given two function templates PAT1 and PAT2, return:
17006 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
17007 -1 if PAT2 is more specialized than PAT1.
17008 0 if neither is more specialized.
17010 LEN indicates the number of parameters we should consider
17011 (defaulted parameters should not be considered).
17013 The 1998 std underspecified function template partial ordering, and
17014 DR214 addresses the issue. We take pairs of arguments, one from
17015 each of the templates, and deduce them against each other. One of
17016 the templates will be more specialized if all the *other*
17017 template's arguments deduce against its arguments and at least one
17018 of its arguments *does* *not* deduce against the other template's
17019 corresponding argument. Deduction is done as for class templates.
17020 The arguments used in deduction have reference and top level cv
17021 qualifiers removed. Iff both arguments were originally reference
17022 types *and* deduction succeeds in both directions, the template
17023 with the more cv-qualified argument wins for that pairing (if
17024 neither is more cv-qualified, they both are equal). Unlike regular
17025 deduction, after all the arguments have been deduced in this way,
17026 we do *not* verify the deduced template argument values can be
17027 substituted into non-deduced contexts.
17029 The logic can be a bit confusing here, because we look at deduce1 and
17030 targs1 to see if pat2 is at least as specialized, and vice versa; if we
17031 can find template arguments for pat1 to make arg1 look like arg2, that
17032 means that arg2 is at least as specialized as arg1. */
17035 more_specialized_fn (tree pat1
, tree pat2
, int len
)
17037 tree decl1
= DECL_TEMPLATE_RESULT (pat1
);
17038 tree decl2
= DECL_TEMPLATE_RESULT (pat2
);
17039 tree targs1
= make_tree_vec (DECL_NTPARMS (pat1
));
17040 tree targs2
= make_tree_vec (DECL_NTPARMS (pat2
));
17041 tree tparms1
= DECL_INNERMOST_TEMPLATE_PARMS (pat1
);
17042 tree tparms2
= DECL_INNERMOST_TEMPLATE_PARMS (pat2
);
17043 tree args1
= TYPE_ARG_TYPES (TREE_TYPE (decl1
));
17044 tree args2
= TYPE_ARG_TYPES (TREE_TYPE (decl2
));
17045 tree origs1
, origs2
;
17046 bool lose1
= false;
17047 bool lose2
= false;
17049 /* Remove the this parameter from non-static member functions. If
17050 one is a non-static member function and the other is not a static
17051 member function, remove the first parameter from that function
17052 also. This situation occurs for operator functions where we
17053 locate both a member function (with this pointer) and non-member
17054 operator (with explicit first operand). */
17055 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1
))
17057 len
--; /* LEN is the number of significant arguments for DECL1 */
17058 args1
= TREE_CHAIN (args1
);
17059 if (!DECL_STATIC_FUNCTION_P (decl2
))
17060 args2
= TREE_CHAIN (args2
);
17062 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2
))
17064 args2
= TREE_CHAIN (args2
);
17065 if (!DECL_STATIC_FUNCTION_P (decl1
))
17068 args1
= TREE_CHAIN (args1
);
17072 /* If only one is a conversion operator, they are unordered. */
17073 if (DECL_CONV_FN_P (decl1
) != DECL_CONV_FN_P (decl2
))
17076 /* Consider the return type for a conversion function */
17077 if (DECL_CONV_FN_P (decl1
))
17079 args1
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl1
)), args1
);
17080 args2
= tree_cons (NULL_TREE
, TREE_TYPE (TREE_TYPE (decl2
)), args2
);
17084 processing_template_decl
++;
17090 /* Stop when an ellipsis is seen. */
17091 && args1
!= NULL_TREE
&& args2
!= NULL_TREE
)
17093 tree arg1
= TREE_VALUE (args1
);
17094 tree arg2
= TREE_VALUE (args2
);
17095 int deduce1
, deduce2
;
17099 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17100 && TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17102 /* When both arguments are pack expansions, we need only
17103 unify the patterns themselves. */
17104 arg1
= PACK_EXPANSION_PATTERN (arg1
);
17105 arg2
= PACK_EXPANSION_PATTERN (arg2
);
17107 /* This is the last comparison we need to do. */
17111 if (TREE_CODE (arg1
) == REFERENCE_TYPE
)
17113 arg1
= TREE_TYPE (arg1
);
17114 quals1
= cp_type_quals (arg1
);
17117 if (TREE_CODE (arg2
) == REFERENCE_TYPE
)
17119 arg2
= TREE_TYPE (arg2
);
17120 quals2
= cp_type_quals (arg2
);
17123 if ((quals1
< 0) != (quals2
< 0))
17125 /* Only of the args is a reference, see if we should apply
17126 array/function pointer decay to it. This is not part of
17127 DR214, but is, IMHO, consistent with the deduction rules
17128 for the function call itself, and with our earlier
17129 implementation of the underspecified partial ordering
17130 rules. (nathan). */
17133 switch (TREE_CODE (arg1
))
17136 arg1
= TREE_TYPE (arg1
);
17138 case FUNCTION_TYPE
:
17139 arg1
= build_pointer_type (arg1
);
17148 switch (TREE_CODE (arg2
))
17151 arg2
= TREE_TYPE (arg2
);
17153 case FUNCTION_TYPE
:
17154 arg2
= build_pointer_type (arg2
);
17163 arg1
= TYPE_MAIN_VARIANT (arg1
);
17164 arg2
= TYPE_MAIN_VARIANT (arg2
);
17166 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
)
17168 int i
, len2
= list_length (args2
);
17169 tree parmvec
= make_tree_vec (1);
17170 tree argvec
= make_tree_vec (len2
);
17173 /* Setup the parameter vector, which contains only ARG1. */
17174 TREE_VEC_ELT (parmvec
, 0) = arg1
;
17176 /* Setup the argument vector, which contains the remaining
17178 for (i
= 0; i
< len2
; i
++, ta
= TREE_CHAIN (ta
))
17179 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17181 deduce1
= (unify_pack_expansion (tparms1
, targs1
, parmvec
,
17182 argvec
, DEDUCE_EXACT
,
17183 /*subr=*/true, /*explain_p=*/false)
17186 /* We cannot deduce in the other direction, because ARG1 is
17187 a pack expansion but ARG2 is not. */
17190 else if (TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17192 int i
, len1
= list_length (args1
);
17193 tree parmvec
= make_tree_vec (1);
17194 tree argvec
= make_tree_vec (len1
);
17197 /* Setup the parameter vector, which contains only ARG1. */
17198 TREE_VEC_ELT (parmvec
, 0) = arg2
;
17200 /* Setup the argument vector, which contains the remaining
17202 for (i
= 0; i
< len1
; i
++, ta
= TREE_CHAIN (ta
))
17203 TREE_VEC_ELT (argvec
, i
) = TREE_VALUE (ta
);
17205 deduce2
= (unify_pack_expansion (tparms2
, targs2
, parmvec
,
17206 argvec
, DEDUCE_EXACT
,
17207 /*subr=*/true, /*explain_p=*/false)
17210 /* We cannot deduce in the other direction, because ARG2 is
17211 a pack expansion but ARG1 is not.*/
17217 /* The normal case, where neither argument is a pack
17219 deduce1
= (unify (tparms1
, targs1
, arg1
, arg2
,
17220 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17222 deduce2
= (unify (tparms2
, targs2
, arg2
, arg1
,
17223 UNIFY_ALLOW_NONE
, /*explain_p=*/false)
17227 /* If we couldn't deduce arguments for tparms1 to make arg1 match
17228 arg2, then arg2 is not as specialized as arg1. */
17234 /* "If, for a given type, deduction succeeds in both directions
17235 (i.e., the types are identical after the transformations above)
17236 and if the type from the argument template is more cv-qualified
17237 than the type from the parameter template (as described above)
17238 that type is considered to be more specialized than the other. If
17239 neither type is more cv-qualified than the other then neither type
17240 is more specialized than the other." */
17242 if (deduce1
&& deduce2
17243 && quals1
!= quals2
&& quals1
>= 0 && quals2
>= 0)
17245 if ((quals1
& quals2
) == quals2
)
17247 if ((quals1
& quals2
) == quals1
)
17251 if (lose1
&& lose2
)
17252 /* We've failed to deduce something in either direction.
17253 These must be unordered. */
17256 if (TREE_CODE (arg1
) == TYPE_PACK_EXPANSION
17257 || TREE_CODE (arg2
) == TYPE_PACK_EXPANSION
)
17258 /* We have already processed all of the arguments in our
17259 handing of the pack expansion type. */
17262 args1
= TREE_CHAIN (args1
);
17263 args2
= TREE_CHAIN (args2
);
17266 /* "In most cases, all template parameters must have values in order for
17267 deduction to succeed, but for partial ordering purposes a template
17268 parameter may remain without a value provided it is not used in the
17269 types being used for partial ordering."
17271 Thus, if we are missing any of the targs1 we need to substitute into
17272 origs1, then pat2 is not as specialized as pat1. This can happen when
17273 there is a nondeduced context. */
17274 if (!lose2
&& check_undeduced_parms (targs1
, origs1
, args1
))
17276 if (!lose1
&& check_undeduced_parms (targs2
, origs2
, args2
))
17279 processing_template_decl
--;
17281 /* All things being equal, if the next argument is a pack expansion
17282 for one function but not for the other, prefer the
17283 non-variadic function. FIXME this is bogus; see c++/41958. */
17285 && args1
&& TREE_VALUE (args1
)
17286 && args2
&& TREE_VALUE (args2
))
17288 lose1
= TREE_CODE (TREE_VALUE (args1
)) == TYPE_PACK_EXPANSION
;
17289 lose2
= TREE_CODE (TREE_VALUE (args2
)) == TYPE_PACK_EXPANSION
;
17292 if (lose1
== lose2
)
17300 /* Determine which of two partial specializations is more specialized.
17302 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
17303 to the first partial specialization. The TREE_VALUE is the
17304 innermost set of template parameters for the partial
17305 specialization. PAT2 is similar, but for the second template.
17307 Return 1 if the first partial specialization is more specialized;
17308 -1 if the second is more specialized; 0 if neither is more
17311 See [temp.class.order] for information about determining which of
17312 two templates is more specialized. */
17315 more_specialized_class (tree pat1
, tree pat2
)
17320 bool any_deductions
= false;
17322 tmpl1
= TREE_TYPE (pat1
);
17323 tmpl2
= TREE_TYPE (pat2
);
17325 /* Just like what happens for functions, if we are ordering between
17326 different class template specializations, we may encounter dependent
17327 types in the arguments, and we need our dependency check functions
17328 to behave correctly. */
17329 ++processing_template_decl
;
17330 targs
= get_class_bindings (TREE_VALUE (pat1
),
17331 CLASSTYPE_TI_ARGS (tmpl1
),
17332 CLASSTYPE_TI_ARGS (tmpl2
));
17336 any_deductions
= true;
17339 targs
= get_class_bindings (TREE_VALUE (pat2
),
17340 CLASSTYPE_TI_ARGS (tmpl2
),
17341 CLASSTYPE_TI_ARGS (tmpl1
));
17345 any_deductions
= true;
17347 --processing_template_decl
;
17349 /* In the case of a tie where at least one of the class templates
17350 has a parameter pack at the end, the template with the most
17351 non-packed parameters wins. */
17354 && (template_args_variadic_p (TREE_PURPOSE (pat1
))
17355 || template_args_variadic_p (TREE_PURPOSE (pat2
))))
17357 tree args1
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1
));
17358 tree args2
= INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2
));
17359 int len1
= TREE_VEC_LENGTH (args1
);
17360 int len2
= TREE_VEC_LENGTH (args2
);
17362 /* We don't count the pack expansion at the end. */
17363 if (template_args_variadic_p (TREE_PURPOSE (pat1
)))
17365 if (template_args_variadic_p (TREE_PURPOSE (pat2
)))
17370 else if (len1
< len2
)
17377 /* Return the template arguments that will produce the function signature
17378 DECL from the function template FN, with the explicit template
17379 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
17380 also match. Return NULL_TREE if no satisfactory arguments could be
17384 get_bindings (tree fn
, tree decl
, tree explicit_args
, bool check_rettype
)
17386 int ntparms
= DECL_NTPARMS (fn
);
17387 tree targs
= make_tree_vec (ntparms
);
17389 tree decl_arg_types
;
17391 unsigned int nargs
, ix
;
17394 /* Substitute the explicit template arguments into the type of DECL.
17395 The call to fn_type_unification will handle substitution into the
17397 decl_type
= TREE_TYPE (decl
);
17398 if (explicit_args
&& uses_template_parms (decl_type
))
17401 tree converted_args
;
17403 if (DECL_TEMPLATE_INFO (decl
))
17404 tmpl
= DECL_TI_TEMPLATE (decl
);
17406 /* We can get here for some invalid specializations. */
17410 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17411 explicit_args
, NULL_TREE
,
17413 /*require_all_args=*/false,
17414 /*use_default_args=*/false);
17415 if (converted_args
== error_mark_node
)
17418 decl_type
= tsubst (decl_type
, converted_args
, tf_none
, NULL_TREE
);
17419 if (decl_type
== error_mark_node
)
17423 /* Never do unification on the 'this' parameter. */
17424 decl_arg_types
= skip_artificial_parms_for (decl
,
17425 TYPE_ARG_TYPES (decl_type
));
17427 nargs
= list_length (decl_arg_types
);
17428 args
= XALLOCAVEC (tree
, nargs
);
17429 for (arg
= decl_arg_types
, ix
= 0;
17430 arg
!= NULL_TREE
&& arg
!= void_list_node
;
17431 arg
= TREE_CHAIN (arg
), ++ix
)
17432 args
[ix
] = TREE_VALUE (arg
);
17434 if (fn_type_unification (fn
, explicit_args
, targs
,
17436 (check_rettype
|| DECL_CONV_FN_P (fn
)
17437 ? TREE_TYPE (decl_type
) : NULL_TREE
),
17438 DEDUCE_EXACT
, LOOKUP_NORMAL
, /*explain_p=*/false))
17444 /* Return the innermost template arguments that, when applied to a
17445 template specialization whose innermost template parameters are
17446 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
17449 For example, suppose we have:
17451 template <class T, class U> struct S {};
17452 template <class T> struct S<T*, int> {};
17454 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
17455 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
17456 int}. The resulting vector will be {double}, indicating that `T'
17457 is bound to `double'. */
17460 get_class_bindings (tree tparms
, tree spec_args
, tree args
)
17462 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
17464 tree innermost_deduced_args
;
17466 innermost_deduced_args
= make_tree_vec (ntparms
);
17467 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17469 deduced_args
= copy_node (args
);
17470 SET_TMPL_ARGS_LEVEL (deduced_args
,
17471 TMPL_ARGS_DEPTH (deduced_args
),
17472 innermost_deduced_args
);
17475 deduced_args
= innermost_deduced_args
;
17477 if (unify (tparms
, deduced_args
,
17478 INNERMOST_TEMPLATE_ARGS (spec_args
),
17479 INNERMOST_TEMPLATE_ARGS (args
),
17480 UNIFY_ALLOW_NONE
, /*explain_p=*/false))
17483 for (i
= 0; i
< ntparms
; ++i
)
17484 if (! TREE_VEC_ELT (innermost_deduced_args
, i
))
17487 /* Verify that nondeduced template arguments agree with the type
17488 obtained from argument deduction.
17492 struct A { typedef int X; };
17493 template <class T, class U> struct C {};
17494 template <class T> struct C<T, typename T::X> {};
17496 Then with the instantiation `C<A, int>', we can deduce that
17497 `T' is `A' but unify () does not check whether `typename T::X'
17499 spec_args
= tsubst (spec_args
, deduced_args
, tf_none
, NULL_TREE
);
17500 if (spec_args
== error_mark_node
17501 /* We only need to check the innermost arguments; the other
17502 arguments will always agree. */
17503 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args
),
17504 INNERMOST_TEMPLATE_ARGS (args
)))
17507 /* Now that we have bindings for all of the template arguments,
17508 ensure that the arguments deduced for the template template
17509 parameters have compatible template parameter lists. See the use
17510 of template_template_parm_bindings_ok_p in fn_type_unification
17511 for more information. */
17512 if (!template_template_parm_bindings_ok_p (tparms
, deduced_args
))
17515 return deduced_args
;
17518 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
17519 Return the TREE_LIST node with the most specialized template, if
17520 any. If there is no most specialized template, the error_mark_node
17523 Note that this function does not look at, or modify, the
17524 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
17525 returned is one of the elements of INSTANTIATIONS, callers may
17526 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
17527 and retrieve it from the value returned. */
17530 most_specialized_instantiation (tree templates
)
17534 ++processing_template_decl
;
17537 for (fn
= TREE_CHAIN (templates
); fn
; fn
= TREE_CHAIN (fn
))
17541 if (get_bindings (TREE_VALUE (champ
),
17542 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17543 NULL_TREE
, /*check_ret=*/true))
17546 if (get_bindings (TREE_VALUE (fn
),
17547 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17548 NULL_TREE
, /*check_ret=*/true))
17555 /* Equally specialized, move to next function. If there
17556 is no next function, nothing's most specialized. */
17557 fn
= TREE_CHAIN (fn
);
17565 /* Now verify that champ is better than everything earlier in the
17566 instantiation list. */
17567 for (fn
= templates
; fn
!= champ
; fn
= TREE_CHAIN (fn
))
17568 if (get_bindings (TREE_VALUE (champ
),
17569 DECL_TEMPLATE_RESULT (TREE_VALUE (fn
)),
17570 NULL_TREE
, /*check_ret=*/true)
17571 || !get_bindings (TREE_VALUE (fn
),
17572 DECL_TEMPLATE_RESULT (TREE_VALUE (champ
)),
17573 NULL_TREE
, /*check_ret=*/true))
17579 processing_template_decl
--;
17582 return error_mark_node
;
17587 /* If DECL is a specialization of some template, return the most
17588 general such template. Otherwise, returns NULL_TREE.
17590 For example, given:
17592 template <class T> struct S { template <class U> void f(U); };
17594 if TMPL is `template <class U> void S<int>::f(U)' this will return
17595 the full template. This function will not trace past partial
17596 specializations, however. For example, given in addition:
17598 template <class T> struct S<T*> { template <class U> void f(U); };
17600 if TMPL is `template <class U> void S<int*>::f(U)' this will return
17601 `template <class T> template <class U> S<T*>::f(U)'. */
17604 most_general_template (tree decl
)
17606 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
17607 an immediate specialization. */
17608 if (TREE_CODE (decl
) == FUNCTION_DECL
)
17610 if (DECL_TEMPLATE_INFO (decl
)) {
17611 decl
= DECL_TI_TEMPLATE (decl
);
17613 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
17614 template friend. */
17615 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
17621 /* Look for more and more general templates. */
17622 while (DECL_TEMPLATE_INFO (decl
))
17624 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
17625 (See cp-tree.h for details.) */
17626 if (TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
17629 if (CLASS_TYPE_P (TREE_TYPE (decl
))
17630 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
17633 /* Stop if we run into an explicitly specialized class template. */
17634 if (!DECL_NAMESPACE_SCOPE_P (decl
)
17635 && DECL_CONTEXT (decl
)
17636 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl
)))
17639 decl
= DECL_TI_TEMPLATE (decl
);
17645 /* Return the most specialized of the class template partial
17646 specializations of TMPL which can produce TYPE, a specialization of
17647 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
17648 a _TYPE node corresponding to the partial specialization, while the
17649 TREE_PURPOSE is the set of template arguments that must be
17650 substituted into the TREE_TYPE in order to generate TYPE.
17652 If the choice of partial specialization is ambiguous, a diagnostic
17653 is issued, and the error_mark_node is returned. If there are no
17654 partial specializations of TMPL matching TYPE, then NULL_TREE is
17658 most_specialized_class (tree type
, tree tmpl
, tsubst_flags_t complain
)
17660 tree list
= NULL_TREE
;
17666 tree outer_args
= NULL_TREE
;
17668 tmpl
= most_general_template (tmpl
);
17669 args
= CLASSTYPE_TI_ARGS (type
);
17671 /* For determining which partial specialization to use, only the
17672 innermost args are interesting. */
17673 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args
))
17675 outer_args
= strip_innermost_template_args (args
, 1);
17676 args
= INNERMOST_TEMPLATE_ARGS (args
);
17679 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
); t
; t
= TREE_CHAIN (t
))
17681 tree partial_spec_args
;
17683 tree parms
= TREE_VALUE (t
);
17685 partial_spec_args
= CLASSTYPE_TI_ARGS (TREE_TYPE (t
));
17687 ++processing_template_decl
;
17693 /* Discard the outer levels of args, and then substitute in the
17694 template args from the enclosing class. */
17695 partial_spec_args
= INNERMOST_TEMPLATE_ARGS (partial_spec_args
);
17696 partial_spec_args
= tsubst_template_args
17697 (partial_spec_args
, outer_args
, tf_none
, NULL_TREE
);
17699 /* PARMS already refers to just the innermost parms, but the
17700 template parms in partial_spec_args had their levels lowered
17701 by tsubst, so we need to do the same for the parm list. We
17702 can't just tsubst the TREE_VEC itself, as tsubst wants to
17703 treat a TREE_VEC as an argument vector. */
17704 parms
= copy_node (parms
);
17705 for (i
= TREE_VEC_LENGTH (parms
) - 1; i
>= 0; --i
)
17706 TREE_VEC_ELT (parms
, i
) =
17707 tsubst (TREE_VEC_ELT (parms
, i
), outer_args
, tf_none
, NULL_TREE
);
17711 partial_spec_args
=
17712 coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
17713 add_to_template_args (outer_args
,
17714 partial_spec_args
),
17716 /*require_all_args=*/true,
17717 /*use_default_args=*/true);
17719 --processing_template_decl
;
17721 if (partial_spec_args
== error_mark_node
)
17722 return error_mark_node
;
17724 spec_args
= get_class_bindings (parms
,
17730 spec_args
= add_to_template_args (outer_args
, spec_args
);
17731 list
= tree_cons (spec_args
, TREE_VALUE (t
), list
);
17732 TREE_TYPE (list
) = TREE_TYPE (t
);
17739 ambiguous_p
= false;
17742 t
= TREE_CHAIN (t
);
17743 for (; t
; t
= TREE_CHAIN (t
))
17745 fate
= more_specialized_class (champ
, t
);
17752 t
= TREE_CHAIN (t
);
17755 ambiguous_p
= true;
17764 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
17766 fate
= more_specialized_class (champ
, t
);
17769 ambiguous_p
= true;
17777 char *spaces
= NULL
;
17778 if (!(complain
& tf_error
))
17779 return error_mark_node
;
17780 error ("ambiguous class template instantiation for %q#T", type
);
17781 str
= ngettext ("candidate is:", "candidates are:", list_length (list
));
17782 for (t
= list
; t
; t
= TREE_CHAIN (t
))
17784 error ("%s %+#T", spaces
? spaces
: str
, TREE_TYPE (t
));
17785 spaces
= spaces
? spaces
: get_spaces (str
);
17788 return error_mark_node
;
17794 /* Explicitly instantiate DECL. */
17797 do_decl_instantiation (tree decl
, tree storage
)
17799 tree result
= NULL_TREE
;
17802 if (!decl
|| decl
== error_mark_node
)
17803 /* An error occurred, for which grokdeclarator has already issued
17804 an appropriate message. */
17806 else if (! DECL_LANG_SPECIFIC (decl
))
17808 error ("explicit instantiation of non-template %q#D", decl
);
17811 else if (TREE_CODE (decl
) == VAR_DECL
)
17813 /* There is an asymmetry here in the way VAR_DECLs and
17814 FUNCTION_DECLs are handled by grokdeclarator. In the case of
17815 the latter, the DECL we get back will be marked as a
17816 template instantiation, and the appropriate
17817 DECL_TEMPLATE_INFO will be set up. This does not happen for
17818 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
17819 should handle VAR_DECLs as it currently handles
17821 if (!DECL_CLASS_SCOPE_P (decl
))
17823 error ("%qD is not a static data member of a class template", decl
);
17826 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, false);
17827 if (!result
|| TREE_CODE (result
) != VAR_DECL
)
17829 error ("no matching template for %qD found", decl
);
17832 if (!same_type_p (TREE_TYPE (result
), TREE_TYPE (decl
)))
17834 error ("type %qT for explicit instantiation %qD does not match "
17835 "declared type %qT", TREE_TYPE (result
), decl
,
17840 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
17842 error ("explicit instantiation of %q#D", decl
);
17848 /* Check for various error cases. Note that if the explicit
17849 instantiation is valid the RESULT will currently be marked as an
17850 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
17851 until we get here. */
17853 if (DECL_TEMPLATE_SPECIALIZATION (result
))
17855 /* DR 259 [temp.spec].
17857 Both an explicit instantiation and a declaration of an explicit
17858 specialization shall not appear in a program unless the explicit
17859 instantiation follows a declaration of the explicit specialization.
17861 For a given set of template parameters, if an explicit
17862 instantiation of a template appears after a declaration of an
17863 explicit specialization for that template, the explicit
17864 instantiation has no effect. */
17867 else if (DECL_EXPLICIT_INSTANTIATION (result
))
17871 No program shall explicitly instantiate any template more
17874 We check DECL_NOT_REALLY_EXTERN so as not to complain when
17875 the first instantiation was `extern' and the second is not,
17876 and EXTERN_P for the opposite case. */
17877 if (DECL_NOT_REALLY_EXTERN (result
) && !extern_p
)
17878 permerror (input_location
, "duplicate explicit instantiation of %q#D", result
);
17879 /* If an "extern" explicit instantiation follows an ordinary
17880 explicit instantiation, the template is instantiated. */
17884 else if (!DECL_IMPLICIT_INSTANTIATION (result
))
17886 error ("no matching template for %qD found", result
);
17889 else if (!DECL_TEMPLATE_INFO (result
))
17891 permerror (input_location
, "explicit instantiation of non-template %q#D", result
);
17895 if (storage
== NULL_TREE
)
17897 else if (storage
== ridpointers
[(int) RID_EXTERN
])
17899 if (!in_system_header
&& (cxx_dialect
== cxx98
))
17900 pedwarn (input_location
, OPT_pedantic
,
17901 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
17906 error ("storage class %qD applied to template instantiation", storage
);
17908 check_explicit_instantiation_namespace (result
);
17909 mark_decl_instantiated (result
, extern_p
);
17911 instantiate_decl (result
, /*defer_ok=*/1,
17912 /*expl_inst_class_mem_p=*/false);
17916 mark_class_instantiated (tree t
, int extern_p
)
17918 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
17919 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
17920 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
17921 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
17924 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
17925 rest_of_type_compilation (t
, 1);
17929 /* Called from do_type_instantiation through binding_table_foreach to
17930 do recursive instantiation for the type bound in ENTRY. */
17932 bt_instantiate_type_proc (binding_entry entry
, void *data
)
17934 tree storage
= *(tree
*) data
;
17936 if (MAYBE_CLASS_TYPE_P (entry
->type
)
17937 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry
->type
)))
17938 do_type_instantiation (TYPE_MAIN_DECL (entry
->type
), storage
, 0);
17941 /* Called from do_type_instantiation to instantiate a member
17942 (a member function or a static member variable) of an
17943 explicitly instantiated class template. */
17945 instantiate_class_member (tree decl
, int extern_p
)
17947 mark_decl_instantiated (decl
, extern_p
);
17949 instantiate_decl (decl
, /*defer_ok=*/1,
17950 /*expl_inst_class_mem_p=*/true);
17953 /* Perform an explicit instantiation of template class T. STORAGE, if
17954 non-null, is the RID for extern, inline or static. COMPLAIN is
17955 nonzero if this is called from the parser, zero if called recursively,
17956 since the standard is unclear (as detailed below). */
17959 do_type_instantiation (tree t
, tree storage
, tsubst_flags_t complain
)
17964 int previous_instantiation_extern_p
= 0;
17966 if (TREE_CODE (t
) == TYPE_DECL
)
17969 if (! CLASS_TYPE_P (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
17972 (TYPE_TEMPLATE_INFO (t
)) ? TYPE_TI_TEMPLATE (t
) : NULL
;
17974 error ("explicit instantiation of non-class template %qD", tmpl
);
17976 error ("explicit instantiation of non-template type %qT", t
);
17982 if (!COMPLETE_TYPE_P (t
))
17984 if (complain
& tf_error
)
17985 error ("explicit instantiation of %q#T before definition of template",
17990 if (storage
!= NULL_TREE
)
17992 if (!in_system_header
)
17994 if (storage
== ridpointers
[(int) RID_EXTERN
])
17996 if (cxx_dialect
== cxx98
)
17997 pedwarn (input_location
, OPT_pedantic
,
17998 "ISO C++ 1998 forbids the use of %<extern%> on "
17999 "explicit instantiations");
18002 pedwarn (input_location
, OPT_pedantic
,
18003 "ISO C++ forbids the use of %qE"
18004 " on explicit instantiations", storage
);
18007 if (storage
== ridpointers
[(int) RID_INLINE
])
18009 else if (storage
== ridpointers
[(int) RID_EXTERN
])
18011 else if (storage
== ridpointers
[(int) RID_STATIC
])
18015 error ("storage class %qD applied to template instantiation",
18021 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
18023 /* DR 259 [temp.spec].
18025 Both an explicit instantiation and a declaration of an explicit
18026 specialization shall not appear in a program unless the explicit
18027 instantiation follows a declaration of the explicit specialization.
18029 For a given set of template parameters, if an explicit
18030 instantiation of a template appears after a declaration of an
18031 explicit specialization for that template, the explicit
18032 instantiation has no effect. */
18035 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
))
18039 No program shall explicitly instantiate any template more
18042 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
18043 instantiation was `extern'. If EXTERN_P then the second is.
18044 These cases are OK. */
18045 previous_instantiation_extern_p
= CLASSTYPE_INTERFACE_ONLY (t
);
18047 if (!previous_instantiation_extern_p
&& !extern_p
18048 && (complain
& tf_error
))
18049 permerror (input_location
, "duplicate explicit instantiation of %q#T", t
);
18051 /* If we've already instantiated the template, just return now. */
18052 if (!CLASSTYPE_INTERFACE_ONLY (t
))
18056 check_explicit_instantiation_namespace (TYPE_NAME (t
));
18057 mark_class_instantiated (t
, extern_p
);
18065 /* In contrast to implicit instantiation, where only the
18066 declarations, and not the definitions, of members are
18067 instantiated, we have here:
18071 The explicit instantiation of a class template specialization
18072 implies the instantiation of all of its members not
18073 previously explicitly specialized in the translation unit
18074 containing the explicit instantiation.
18076 Of course, we can't instantiate member template classes, since
18077 we don't have any arguments for them. Note that the standard
18078 is unclear on whether the instantiation of the members are
18079 *explicit* instantiations or not. However, the most natural
18080 interpretation is that it should be an explicit instantiation. */
18083 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18084 if (TREE_CODE (tmp
) == FUNCTION_DECL
18085 && DECL_TEMPLATE_INSTANTIATION (tmp
))
18086 instantiate_class_member (tmp
, extern_p
);
18088 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= DECL_CHAIN (tmp
))
18089 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
18090 instantiate_class_member (tmp
, extern_p
);
18092 if (CLASSTYPE_NESTED_UTDS (t
))
18093 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t
),
18094 bt_instantiate_type_proc
, &storage
);
18098 /* Given a function DECL, which is a specialization of TMPL, modify
18099 DECL to be a re-instantiation of TMPL with the same template
18100 arguments. TMPL should be the template into which tsubst'ing
18101 should occur for DECL, not the most general template.
18103 One reason for doing this is a scenario like this:
18106 void f(const T&, int i);
18108 void g() { f(3, 7); }
18111 void f(const T& t, const int i) { }
18113 Note that when the template is first instantiated, with
18114 instantiate_template, the resulting DECL will have no name for the
18115 first parameter, and the wrong type for the second. So, when we go
18116 to instantiate the DECL, we regenerate it. */
18119 regenerate_decl_from_template (tree decl
, tree tmpl
)
18121 /* The arguments used to instantiate DECL, from the most general
18126 args
= DECL_TI_ARGS (decl
);
18127 code_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
18129 /* Make sure that we can see identifiers, and compute access
18131 push_access_scope (decl
);
18133 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18141 args_depth
= TMPL_ARGS_DEPTH (args
);
18142 parms_depth
= TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
));
18143 if (args_depth
> parms_depth
)
18144 args
= get_innermost_template_args (args
, parms_depth
);
18146 specs
= tsubst_exception_specification (TREE_TYPE (code_pattern
),
18147 args
, tf_error
, NULL_TREE
,
18148 /*defer_ok*/false);
18149 if (specs
&& specs
!= error_mark_node
)
18150 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
),
18153 /* Merge parameter declarations. */
18154 decl_parm
= skip_artificial_parms_for (decl
,
18155 DECL_ARGUMENTS (decl
));
18157 = skip_artificial_parms_for (code_pattern
,
18158 DECL_ARGUMENTS (code_pattern
));
18159 while (decl_parm
&& !FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18164 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18165 DECL_NAME (decl_parm
) = DECL_NAME (pattern_parm
);
18166 parm_type
= tsubst (TREE_TYPE (pattern_parm
), args
, tf_error
,
18168 parm_type
= type_decays_to (parm_type
);
18169 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18170 TREE_TYPE (decl_parm
) = parm_type
;
18171 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18172 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18174 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18175 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18177 decl_parm
= DECL_CHAIN (decl_parm
);
18178 pattern_parm
= DECL_CHAIN (pattern_parm
);
18180 /* Merge any parameters that match with the function parameter
18182 if (pattern_parm
&& FUNCTION_PARAMETER_PACK_P (pattern_parm
))
18185 tree expanded_types
;
18186 /* Expand the TYPE_PACK_EXPANSION that provides the types for
18187 the parameters in this function parameter pack. */
18188 expanded_types
= tsubst_pack_expansion (TREE_TYPE (pattern_parm
),
18189 args
, tf_error
, NULL_TREE
);
18190 len
= TREE_VEC_LENGTH (expanded_types
);
18191 for (i
= 0; i
< len
; i
++)
18196 if (DECL_NAME (decl_parm
) != DECL_NAME (pattern_parm
))
18197 /* Rename the parameter to include the index. */
18198 DECL_NAME (decl_parm
) =
18199 make_ith_pack_parameter_name (DECL_NAME (pattern_parm
), i
);
18200 parm_type
= TREE_VEC_ELT (expanded_types
, i
);
18201 parm_type
= type_decays_to (parm_type
);
18202 if (!same_type_p (TREE_TYPE (decl_parm
), parm_type
))
18203 TREE_TYPE (decl_parm
) = parm_type
;
18204 attributes
= DECL_ATTRIBUTES (pattern_parm
);
18205 if (DECL_ATTRIBUTES (decl_parm
) != attributes
)
18207 DECL_ATTRIBUTES (decl_parm
) = attributes
;
18208 cplus_decl_attributes (&decl_parm
, attributes
, /*flags=*/0);
18210 decl_parm
= DECL_CHAIN (decl_parm
);
18213 /* Merge additional specifiers from the CODE_PATTERN. */
18214 if (DECL_DECLARED_INLINE_P (code_pattern
)
18215 && !DECL_DECLARED_INLINE_P (decl
))
18216 DECL_DECLARED_INLINE_P (decl
) = 1;
18218 else if (TREE_CODE (decl
) == VAR_DECL
)
18220 DECL_INITIAL (decl
) =
18221 tsubst_expr (DECL_INITIAL (code_pattern
), args
,
18222 tf_error
, DECL_TI_TEMPLATE (decl
),
18223 /*integral_constant_expression_p=*/false);
18224 if (VAR_HAD_UNKNOWN_BOUND (decl
))
18225 TREE_TYPE (decl
) = tsubst (TREE_TYPE (code_pattern
), args
,
18226 tf_error
, DECL_TI_TEMPLATE (decl
));
18229 gcc_unreachable ();
18231 pop_access_scope (decl
);
18234 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
18235 substituted to get DECL. */
18238 template_for_substitution (tree decl
)
18240 tree tmpl
= DECL_TI_TEMPLATE (decl
);
18242 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
18243 for the instantiation. This is not always the most general
18244 template. Consider, for example:
18247 struct S { template <class U> void f();
18248 template <> void f<int>(); };
18250 and an instantiation of S<double>::f<int>. We want TD to be the
18251 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
18252 while (/* An instantiation cannot have a definition, so we need a
18253 more general template. */
18254 DECL_TEMPLATE_INSTANTIATION (tmpl
)
18255 /* We must also deal with friend templates. Given:
18257 template <class T> struct S {
18258 template <class U> friend void f() {};
18261 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
18262 so far as the language is concerned, but that's still
18263 where we get the pattern for the instantiation from. On
18264 other hand, if the definition comes outside the class, say:
18266 template <class T> struct S {
18267 template <class U> friend void f();
18269 template <class U> friend void f() {}
18271 we don't need to look any further. That's what the check for
18272 DECL_INITIAL is for. */
18273 || (TREE_CODE (decl
) == FUNCTION_DECL
18274 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl
)
18275 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl
))))
18277 /* The present template, TD, should not be a definition. If it
18278 were a definition, we should be using it! Note that we
18279 cannot restructure the loop to just keep going until we find
18280 a template with a definition, since that might go too far if
18281 a specialization was declared, but not defined. */
18282 gcc_assert (TREE_CODE (decl
) != VAR_DECL
18283 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl
)));
18285 /* Fetch the more general template. */
18286 tmpl
= DECL_TI_TEMPLATE (tmpl
);
18292 /* Returns true if we need to instantiate this template instance even if we
18293 know we aren't going to emit it.. */
18296 always_instantiate_p (tree decl
)
18298 /* We always instantiate inline functions so that we can inline them. An
18299 explicit instantiation declaration prohibits implicit instantiation of
18300 non-inline functions. With high levels of optimization, we would
18301 normally inline non-inline functions -- but we're not allowed to do
18302 that for "extern template" functions. Therefore, we check
18303 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
18304 return ((TREE_CODE (decl
) == FUNCTION_DECL
18305 && DECL_DECLARED_INLINE_P (decl
))
18306 /* And we need to instantiate static data members so that
18307 their initializers are available in integral constant
18309 || (TREE_CODE (decl
) == VAR_DECL
18310 && decl_maybe_constant_var_p (decl
)));
18313 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
18314 instantiate it now, modifying TREE_TYPE (fn). */
18317 maybe_instantiate_noexcept (tree fn
)
18319 tree fntype
, spec
, noex
, clone
;
18321 if (DECL_CLONED_FUNCTION_P (fn
))
18322 fn
= DECL_CLONED_FUNCTION (fn
);
18323 fntype
= TREE_TYPE (fn
);
18324 spec
= TYPE_RAISES_EXCEPTIONS (fntype
);
18326 if (!DEFERRED_NOEXCEPT_SPEC_P (spec
))
18329 noex
= TREE_PURPOSE (spec
);
18331 if (TREE_CODE (noex
) == DEFERRED_NOEXCEPT
)
18333 if (push_tinst_level (fn
))
18335 push_access_scope (fn
);
18336 input_location
= DECL_SOURCE_LOCATION (fn
);
18337 noex
= tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex
),
18338 DEFERRED_NOEXCEPT_ARGS (noex
),
18339 tf_warning_or_error
, fn
,
18340 /*function_p=*/false,
18341 /*integral_constant_expression_p=*/true);
18342 pop_access_scope (fn
);
18343 pop_tinst_level ();
18344 spec
= build_noexcept_spec (noex
, tf_warning_or_error
);
18345 if (spec
== error_mark_node
)
18346 spec
= noexcept_false_spec
;
18349 spec
= noexcept_false_spec
;
18353 /* This is an implicitly declared function, so NOEX is a list of
18354 other functions to evaluate and merge. */
18356 spec
= noexcept_true_spec
;
18357 for (elt
= noex
; elt
; elt
= OVL_NEXT (elt
))
18359 tree fn
= OVL_CURRENT (elt
);
18361 maybe_instantiate_noexcept (fn
);
18362 subspec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
18363 spec
= merge_exception_specifiers (spec
, subspec
, NULL_TREE
);
18367 TREE_TYPE (fn
) = build_exception_variant (fntype
, spec
);
18369 FOR_EACH_CLONE (clone
, fn
)
18371 if (TREE_TYPE (clone
) == fntype
)
18372 TREE_TYPE (clone
) = TREE_TYPE (fn
);
18374 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
18378 /* Produce the definition of D, a _DECL generated from a template. If
18379 DEFER_OK is nonzero, then we don't have to actually do the
18380 instantiation now; we just have to do it sometime. Normally it is
18381 an error if this is an explicit instantiation but D is undefined.
18382 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
18383 explicitly instantiated class template. */
18386 instantiate_decl (tree d
, int defer_ok
,
18387 bool expl_inst_class_mem_p
)
18389 tree tmpl
= DECL_TI_TEMPLATE (d
);
18396 bool pattern_defined
;
18398 location_t saved_loc
= input_location
;
18401 /* This function should only be used to instantiate templates for
18402 functions and static member variables. */
18403 gcc_assert (TREE_CODE (d
) == FUNCTION_DECL
18404 || TREE_CODE (d
) == VAR_DECL
);
18406 /* Variables are never deferred; if instantiation is required, they
18407 are instantiated right away. That allows for better code in the
18408 case that an expression refers to the value of the variable --
18409 if the variable has a constant value the referring expression can
18410 take advantage of that fact. */
18411 if (TREE_CODE (d
) == VAR_DECL
18412 || DECL_DECLARED_CONSTEXPR_P (d
))
18415 /* Don't instantiate cloned functions. Instead, instantiate the
18416 functions they cloned. */
18417 if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_CLONED_FUNCTION_P (d
))
18418 d
= DECL_CLONED_FUNCTION (d
);
18420 if (DECL_TEMPLATE_INSTANTIATED (d
)
18421 || (TREE_CODE (d
) == FUNCTION_DECL
18422 && DECL_DEFAULTED_FN (d
) && DECL_INITIAL (d
))
18423 || DECL_TEMPLATE_SPECIALIZATION (d
))
18424 /* D has already been instantiated or explicitly specialized, so
18425 there's nothing for us to do here.
18427 It might seem reasonable to check whether or not D is an explicit
18428 instantiation, and, if so, stop here. But when an explicit
18429 instantiation is deferred until the end of the compilation,
18430 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
18431 the instantiation. */
18434 /* Check to see whether we know that this template will be
18435 instantiated in some other file, as with "extern template"
18437 external_p
= (DECL_INTERFACE_KNOWN (d
) && DECL_REALLY_EXTERN (d
));
18439 /* In general, we do not instantiate such templates. */
18440 if (external_p
&& !always_instantiate_p (d
))
18443 gen_tmpl
= most_general_template (tmpl
);
18444 gen_args
= DECL_TI_ARGS (d
);
18446 if (tmpl
!= gen_tmpl
)
18447 /* We should already have the extra args. */
18448 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl
))
18449 == TMPL_ARGS_DEPTH (gen_args
));
18450 /* And what's in the hash table should match D. */
18451 gcc_assert ((spec
= retrieve_specialization (gen_tmpl
, gen_args
, 0)) == d
18452 || spec
== NULL_TREE
);
18454 /* This needs to happen before any tsubsting. */
18455 if (! push_tinst_level (d
))
18458 timevar_push (TV_TEMPLATE_INST
);
18460 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
18461 for the instantiation. */
18462 td
= template_for_substitution (d
);
18463 code_pattern
= DECL_TEMPLATE_RESULT (td
);
18465 /* We should never be trying to instantiate a member of a class
18466 template or partial specialization. */
18467 gcc_assert (d
!= code_pattern
);
18469 if ((DECL_NAMESPACE_SCOPE_P (d
) && !DECL_INITIALIZED_IN_CLASS_P (d
))
18470 || DECL_TEMPLATE_SPECIALIZATION (td
))
18471 /* In the case of a friend template whose definition is provided
18472 outside the class, we may have too many arguments. Drop the
18473 ones we don't need. The same is true for specializations. */
18474 args
= get_innermost_template_args
18475 (gen_args
, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td
)));
18479 if (TREE_CODE (d
) == FUNCTION_DECL
)
18480 pattern_defined
= (DECL_SAVED_TREE (code_pattern
) != NULL_TREE
18481 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern
));
18483 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
18485 /* We may be in the middle of deferred access check. Disable it now. */
18486 push_deferring_access_checks (dk_no_deferred
);
18488 /* Unless an explicit instantiation directive has already determined
18489 the linkage of D, remember that a definition is available for
18491 if (pattern_defined
18492 && !DECL_INTERFACE_KNOWN (d
)
18493 && !DECL_NOT_REALLY_EXTERN (d
))
18494 mark_definable (d
);
18496 DECL_SOURCE_LOCATION (td
) = DECL_SOURCE_LOCATION (code_pattern
);
18497 DECL_SOURCE_LOCATION (d
) = DECL_SOURCE_LOCATION (code_pattern
);
18498 input_location
= DECL_SOURCE_LOCATION (d
);
18500 /* If D is a member of an explicitly instantiated class template,
18501 and no definition is available, treat it like an implicit
18503 if (!pattern_defined
&& expl_inst_class_mem_p
18504 && DECL_EXPLICIT_INSTANTIATION (d
))
18506 /* Leave linkage flags alone on instantiations with anonymous
18508 if (TREE_PUBLIC (d
))
18510 DECL_NOT_REALLY_EXTERN (d
) = 0;
18511 DECL_INTERFACE_KNOWN (d
) = 0;
18513 SET_DECL_IMPLICIT_INSTANTIATION (d
);
18516 if (TREE_CODE (d
) == FUNCTION_DECL
)
18517 maybe_instantiate_noexcept (d
);
18519 /* Recheck the substitutions to obtain any warning messages
18520 about ignoring cv qualifiers. Don't do this for artificial decls,
18521 as it breaks the context-sensitive substitution for lambda op(). */
18522 if (!defer_ok
&& !DECL_ARTIFICIAL (d
))
18524 tree gen
= DECL_TEMPLATE_RESULT (gen_tmpl
);
18525 tree type
= TREE_TYPE (gen
);
18527 /* Make sure that we can see identifiers, and compute access
18528 correctly. D is already the target FUNCTION_DECL with the
18530 push_access_scope (d
);
18532 if (TREE_CODE (gen
) == FUNCTION_DECL
)
18534 tsubst (DECL_ARGUMENTS (gen
), gen_args
, tf_warning_or_error
, d
);
18535 tsubst_exception_specification (type
, gen_args
, tf_warning_or_error
,
18536 d
, /*defer_ok*/true);
18537 /* Don't simply tsubst the function type, as that will give
18538 duplicate warnings about poor parameter qualifications.
18539 The function arguments are the same as the decl_arguments
18540 without the top level cv qualifiers. */
18541 type
= TREE_TYPE (type
);
18543 tsubst (type
, gen_args
, tf_warning_or_error
, d
);
18545 pop_access_scope (d
);
18548 /* Defer all other templates, unless we have been explicitly
18549 forbidden from doing so. */
18550 if (/* If there is no definition, we cannot instantiate the
18553 /* If it's OK to postpone instantiation, do so. */
18555 /* If this is a static data member that will be defined
18556 elsewhere, we don't want to instantiate the entire data
18557 member, but we do want to instantiate the initializer so that
18558 we can substitute that elsewhere. */
18559 || (external_p
&& TREE_CODE (d
) == VAR_DECL
))
18561 /* The definition of the static data member is now required so
18562 we must substitute the initializer. */
18563 if (TREE_CODE (d
) == VAR_DECL
18564 && !DECL_INITIAL (d
)
18565 && DECL_INITIAL (code_pattern
))
18569 bool const_init
= false;
18571 ns
= decl_namespace_context (d
);
18572 push_nested_namespace (ns
);
18573 push_nested_class (DECL_CONTEXT (d
));
18574 init
= tsubst_expr (DECL_INITIAL (code_pattern
),
18576 tf_warning_or_error
, NULL_TREE
,
18577 /*integral_constant_expression_p=*/false);
18578 /* Make sure the initializer is still constant, in case of
18579 circular dependency (template/instantiate6.C). */
18581 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18582 cp_finish_decl (d
, init
, /*init_const_expr_p=*/const_init
,
18583 /*asmspec_tree=*/NULL_TREE
,
18584 LOOKUP_ONLYCONVERTING
);
18585 pop_nested_class ();
18586 pop_nested_namespace (ns
);
18589 /* We restore the source position here because it's used by
18590 add_pending_template. */
18591 input_location
= saved_loc
;
18593 if (at_eof
&& !pattern_defined
18594 && DECL_EXPLICIT_INSTANTIATION (d
)
18595 && DECL_NOT_REALLY_EXTERN (d
))
18598 The definition of a non-exported function template, a
18599 non-exported member function template, or a non-exported
18600 member function or static data member of a class template
18601 shall be present in every translation unit in which it is
18602 explicitly instantiated. */
18603 permerror (input_location
, "explicit instantiation of %qD "
18604 "but no definition available", d
);
18606 /* If we're in unevaluated context, we just wanted to get the
18607 constant value; this isn't an odr use, so don't queue
18608 a full instantiation. */
18609 if (cp_unevaluated_operand
!= 0)
18611 /* ??? Historically, we have instantiated inline functions, even
18612 when marked as "extern template". */
18613 if (!(external_p
&& TREE_CODE (d
) == VAR_DECL
))
18614 add_pending_template (d
);
18617 /* Tell the repository that D is available in this translation unit
18618 -- and see if it is supposed to be instantiated here. */
18619 if (TREE_PUBLIC (d
) && !DECL_REALLY_EXTERN (d
) && !repo_emit_p (d
))
18621 /* In a PCH file, despite the fact that the repository hasn't
18622 requested instantiation in the PCH it is still possible that
18623 an instantiation will be required in a file that includes the
18626 add_pending_template (d
);
18627 /* Instantiate inline functions so that the inliner can do its
18628 job, even though we'll not be emitting a copy of this
18630 if (!(TREE_CODE (d
) == FUNCTION_DECL
&& possibly_inlined_p (d
)))
18634 need_push
= !cfun
|| !global_bindings_p ();
18636 push_to_top_level ();
18638 /* Mark D as instantiated so that recursive calls to
18639 instantiate_decl do not try to instantiate it again. */
18640 DECL_TEMPLATE_INSTANTIATED (d
) = 1;
18642 /* Regenerate the declaration in case the template has been modified
18643 by a subsequent redeclaration. */
18644 regenerate_decl_from_template (d
, td
);
18646 /* We already set the file and line above. Reset them now in case
18647 they changed as a result of calling regenerate_decl_from_template. */
18648 input_location
= DECL_SOURCE_LOCATION (d
);
18650 if (TREE_CODE (d
) == VAR_DECL
)
18653 bool const_init
= false;
18655 /* Clear out DECL_RTL; whatever was there before may not be right
18656 since we've reset the type of the declaration. */
18657 SET_DECL_RTL (d
, NULL
);
18658 DECL_IN_AGGR_P (d
) = 0;
18660 /* The initializer is placed in DECL_INITIAL by
18661 regenerate_decl_from_template so we don't need to
18662 push/pop_access_scope again here. Pull it out so that
18663 cp_finish_decl can process it. */
18664 init
= DECL_INITIAL (d
);
18665 DECL_INITIAL (d
) = NULL_TREE
;
18666 DECL_INITIALIZED_P (d
) = 0;
18668 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
18669 initializer. That function will defer actual emission until
18670 we have a chance to determine linkage. */
18671 DECL_EXTERNAL (d
) = 0;
18673 /* Enter the scope of D so that access-checking works correctly. */
18674 push_nested_class (DECL_CONTEXT (d
));
18675 const_init
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern
);
18676 cp_finish_decl (d
, init
, const_init
, NULL_TREE
, 0);
18677 pop_nested_class ();
18679 else if (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_DEFAULTED_FN (code_pattern
))
18680 synthesize_method (d
);
18681 else if (TREE_CODE (d
) == FUNCTION_DECL
)
18683 struct pointer_map_t
*saved_local_specializations
;
18688 /* Save away the current list, in case we are instantiating one
18689 template from within the body of another. */
18690 saved_local_specializations
= local_specializations
;
18692 /* Set up the list of local specializations. */
18693 local_specializations
= pointer_map_create ();
18695 /* Set up context. */
18696 start_preparsed_function (d
, NULL_TREE
, SF_PRE_PARSED
);
18698 /* Create substitution entries for the parameters. */
18699 subst_decl
= DECL_TEMPLATE_RESULT (template_for_substitution (d
));
18700 tmpl_parm
= DECL_ARGUMENTS (subst_decl
);
18701 spec_parm
= DECL_ARGUMENTS (d
);
18702 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d
))
18704 register_local_specialization (spec_parm
, tmpl_parm
);
18705 spec_parm
= skip_artificial_parms_for (d
, spec_parm
);
18706 tmpl_parm
= skip_artificial_parms_for (subst_decl
, tmpl_parm
);
18708 for (; tmpl_parm
; tmpl_parm
= DECL_CHAIN (tmpl_parm
))
18710 if (!FUNCTION_PARAMETER_PACK_P (tmpl_parm
))
18712 register_local_specialization (spec_parm
, tmpl_parm
);
18713 spec_parm
= DECL_CHAIN (spec_parm
);
18717 /* Register the (value) argument pack as a specialization of
18718 TMPL_PARM, then move on. */
18719 tree argpack
= extract_fnparm_pack (tmpl_parm
, &spec_parm
);
18720 register_local_specialization (argpack
, tmpl_parm
);
18723 gcc_assert (!spec_parm
);
18725 /* Substitute into the body of the function. */
18726 tsubst_expr (DECL_SAVED_TREE (code_pattern
), args
,
18727 tf_warning_or_error
, tmpl
,
18728 /*integral_constant_expression_p=*/false);
18730 /* Set the current input_location to the end of the function
18731 so that finish_function knows where we are. */
18732 input_location
= DECL_STRUCT_FUNCTION (code_pattern
)->function_end_locus
;
18734 /* We don't need the local specializations any more. */
18735 pointer_map_destroy (local_specializations
);
18736 local_specializations
= saved_local_specializations
;
18738 /* Finish the function. */
18739 d
= finish_function (0);
18740 expand_or_defer_fn (d
);
18743 /* We're not deferring instantiation any more. */
18744 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d
)) = 0;
18747 pop_from_top_level ();
18750 input_location
= saved_loc
;
18751 pop_deferring_access_checks ();
18752 pop_tinst_level ();
18754 timevar_pop (TV_TEMPLATE_INST
);
18759 /* Run through the list of templates that we wish we could
18760 instantiate, and instantiate any we can. RETRIES is the
18761 number of times we retry pending template instantiation. */
18764 instantiate_pending_templates (int retries
)
18767 location_t saved_loc
= input_location
;
18769 /* Instantiating templates may trigger vtable generation. This in turn
18770 may require further template instantiations. We place a limit here
18771 to avoid infinite loop. */
18772 if (pending_templates
&& retries
>= max_tinst_depth
)
18774 tree decl
= pending_templates
->tinst
->decl
;
18776 error ("template instantiation depth exceeds maximum of %d"
18777 " instantiating %q+D, possibly from virtual table generation"
18778 " (use -ftemplate-depth= to increase the maximum)",
18779 max_tinst_depth
, decl
);
18780 if (TREE_CODE (decl
) == FUNCTION_DECL
)
18781 /* Pretend that we defined it. */
18782 DECL_INITIAL (decl
) = error_mark_node
;
18788 struct pending_template
**t
= &pending_templates
;
18789 struct pending_template
*last
= NULL
;
18793 tree instantiation
= reopen_tinst_level ((*t
)->tinst
);
18794 bool complete
= false;
18796 if (TYPE_P (instantiation
))
18800 if (!COMPLETE_TYPE_P (instantiation
))
18802 instantiate_class_template (instantiation
);
18803 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation
))
18804 for (fn
= TYPE_METHODS (instantiation
);
18806 fn
= TREE_CHAIN (fn
))
18807 if (! DECL_ARTIFICIAL (fn
))
18808 instantiate_decl (fn
,
18810 /*expl_inst_class_mem_p=*/false);
18811 if (COMPLETE_TYPE_P (instantiation
))
18815 complete
= COMPLETE_TYPE_P (instantiation
);
18819 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18820 && !DECL_TEMPLATE_INSTANTIATED (instantiation
))
18823 = instantiate_decl (instantiation
,
18825 /*expl_inst_class_mem_p=*/false);
18826 if (DECL_TEMPLATE_INSTANTIATED (instantiation
))
18830 complete
= (DECL_TEMPLATE_SPECIALIZATION (instantiation
)
18831 || DECL_TEMPLATE_INSTANTIATED (instantiation
));
18835 /* If INSTANTIATION has been instantiated, then we don't
18836 need to consider it again in the future. */
18844 current_tinst_level
= NULL
;
18846 last_pending_template
= last
;
18848 while (reconsider
);
18850 input_location
= saved_loc
;
18853 /* Substitute ARGVEC into T, which is a list of initializers for
18854 either base class or a non-static data member. The TREE_PURPOSEs
18855 are DECLs, and the TREE_VALUEs are the initializer values. Used by
18856 instantiate_decl. */
18859 tsubst_initializer_list (tree t
, tree argvec
)
18861 tree inits
= NULL_TREE
;
18863 for (; t
; t
= TREE_CHAIN (t
))
18867 tree expanded_bases
= NULL_TREE
;
18868 tree expanded_arguments
= NULL_TREE
;
18871 if (TREE_CODE (TREE_PURPOSE (t
)) == TYPE_PACK_EXPANSION
)
18876 /* Expand the base class expansion type into separate base
18878 expanded_bases
= tsubst_pack_expansion (TREE_PURPOSE (t
), argvec
,
18879 tf_warning_or_error
,
18881 if (expanded_bases
== error_mark_node
)
18884 /* We'll be building separate TREE_LISTs of arguments for
18886 len
= TREE_VEC_LENGTH (expanded_bases
);
18887 expanded_arguments
= make_tree_vec (len
);
18888 for (i
= 0; i
< len
; i
++)
18889 TREE_VEC_ELT (expanded_arguments
, i
) = NULL_TREE
;
18891 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
18892 expand each argument in the TREE_VALUE of t. */
18893 expr
= make_node (EXPR_PACK_EXPANSION
);
18894 PACK_EXPANSION_LOCAL_P (expr
) = true;
18895 PACK_EXPANSION_PARAMETER_PACKS (expr
) =
18896 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t
));
18898 if (TREE_VALUE (t
) == void_type_node
)
18899 /* VOID_TYPE_NODE is used to indicate
18900 value-initialization. */
18902 for (i
= 0; i
< len
; i
++)
18903 TREE_VEC_ELT (expanded_arguments
, i
) = void_type_node
;
18907 /* Substitute parameter packs into each argument in the
18909 in_base_initializer
= 1;
18910 for (arg
= TREE_VALUE (t
); arg
; arg
= TREE_CHAIN (arg
))
18912 tree expanded_exprs
;
18914 /* Expand the argument. */
18915 SET_PACK_EXPANSION_PATTERN (expr
, TREE_VALUE (arg
));
18917 = tsubst_pack_expansion (expr
, argvec
,
18918 tf_warning_or_error
,
18920 if (expanded_exprs
== error_mark_node
)
18923 /* Prepend each of the expanded expressions to the
18924 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
18925 for (i
= 0; i
< len
; i
++)
18927 TREE_VEC_ELT (expanded_arguments
, i
) =
18928 tree_cons (NULL_TREE
,
18929 TREE_VEC_ELT (expanded_exprs
, i
),
18930 TREE_VEC_ELT (expanded_arguments
, i
));
18933 in_base_initializer
= 0;
18935 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
18936 since we built them backwards. */
18937 for (i
= 0; i
< len
; i
++)
18939 TREE_VEC_ELT (expanded_arguments
, i
) =
18940 nreverse (TREE_VEC_ELT (expanded_arguments
, i
));
18945 for (i
= 0; i
< len
; ++i
)
18947 if (expanded_bases
)
18949 decl
= TREE_VEC_ELT (expanded_bases
, i
);
18950 decl
= expand_member_init (decl
);
18951 init
= TREE_VEC_ELT (expanded_arguments
, i
);
18955 decl
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
18956 tf_warning_or_error
, NULL_TREE
);
18958 decl
= expand_member_init (decl
);
18959 if (decl
&& !DECL_P (decl
))
18960 in_base_initializer
= 1;
18962 init
= TREE_VALUE (t
);
18963 if (init
!= void_type_node
)
18964 init
= tsubst_expr (init
, argvec
,
18965 tf_warning_or_error
, NULL_TREE
,
18966 /*integral_constant_expression_p=*/false);
18967 in_base_initializer
= 0;
18972 init
= build_tree_list (decl
, init
);
18973 TREE_CHAIN (init
) = inits
;
18981 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
18984 set_current_access_from_decl (tree decl
)
18986 if (TREE_PRIVATE (decl
))
18987 current_access_specifier
= access_private_node
;
18988 else if (TREE_PROTECTED (decl
))
18989 current_access_specifier
= access_protected_node
;
18991 current_access_specifier
= access_public_node
;
18994 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
18995 is the instantiation (which should have been created with
18996 start_enum) and ARGS are the template arguments to use. */
18999 tsubst_enum (tree tag
, tree newtag
, tree args
)
19003 if (SCOPED_ENUM_P (newtag
))
19004 begin_scope (sk_scoped_enum
, newtag
);
19006 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
19011 decl
= TREE_VALUE (e
);
19012 /* Note that in a template enum, the TREE_VALUE is the
19013 CONST_DECL, not the corresponding INTEGER_CST. */
19014 value
= tsubst_expr (DECL_INITIAL (decl
),
19015 args
, tf_warning_or_error
, NULL_TREE
,
19016 /*integral_constant_expression_p=*/true);
19018 /* Give this enumeration constant the correct access. */
19019 set_current_access_from_decl (decl
);
19021 /* Actually build the enumerator itself. */
19023 (DECL_NAME (decl
), value
, newtag
, DECL_SOURCE_LOCATION (decl
));
19026 if (SCOPED_ENUM_P (newtag
))
19029 finish_enum_value_list (newtag
);
19030 finish_enum (newtag
);
19032 DECL_SOURCE_LOCATION (TYPE_NAME (newtag
))
19033 = DECL_SOURCE_LOCATION (TYPE_NAME (tag
));
19036 /* DECL is a FUNCTION_DECL that is a template specialization. Return
19037 its type -- but without substituting the innermost set of template
19038 arguments. So, innermost set of template parameters will appear in
19042 get_mostly_instantiated_function_type (tree decl
)
19050 tmpl
= most_general_template (DECL_TI_TEMPLATE (decl
));
19051 targs
= DECL_TI_ARGS (decl
);
19052 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
19053 parm_depth
= TMPL_PARMS_DEPTH (tparms
);
19055 /* There should be as many levels of arguments as there are levels
19057 gcc_assert (parm_depth
== TMPL_ARGS_DEPTH (targs
));
19059 fn_type
= TREE_TYPE (tmpl
);
19061 if (parm_depth
== 1)
19062 /* No substitution is necessary. */
19069 /* Replace the innermost level of the TARGS with NULL_TREEs to
19070 let tsubst know not to substitute for those parameters. */
19071 partial_args
= make_tree_vec (TREE_VEC_LENGTH (targs
));
19072 for (i
= 1; i
< TMPL_ARGS_DEPTH (targs
); ++i
)
19073 SET_TMPL_ARGS_LEVEL (partial_args
, i
,
19074 TMPL_ARGS_LEVEL (targs
, i
));
19075 SET_TMPL_ARGS_LEVEL (partial_args
,
19076 TMPL_ARGS_DEPTH (targs
),
19077 make_tree_vec (DECL_NTPARMS (tmpl
)));
19079 /* Make sure that we can see identifiers, and compute access
19081 push_access_scope (decl
);
19083 ++processing_template_decl
;
19084 /* Now, do the (partial) substitution to figure out the
19085 appropriate function type. */
19086 fn_type
= tsubst (fn_type
, partial_args
, tf_error
, NULL_TREE
);
19087 --processing_template_decl
;
19089 /* Substitute into the template parameters to obtain the real
19090 innermost set of parameters. This step is important if the
19091 innermost set of template parameters contains value
19092 parameters whose types depend on outer template parameters. */
19093 TREE_VEC_LENGTH (partial_args
)--;
19094 tparms
= tsubst_template_parms (tparms
, partial_args
, tf_error
);
19096 pop_access_scope (decl
);
19102 /* Return truthvalue if we're processing a template different from
19103 the last one involved in diagnostics. */
19105 problematic_instantiation_changed (void)
19107 return current_tinst_level
!= last_error_tinst_level
;
19110 /* Remember current template involved in diagnostics. */
19112 record_last_problematic_instantiation (void)
19114 last_error_tinst_level
= current_tinst_level
;
19117 struct tinst_level
*
19118 current_instantiation (void)
19120 return current_tinst_level
;
19123 /* [temp.param] Check that template non-type parm TYPE is of an allowable
19124 type. Return zero for ok, nonzero for disallowed. Issue error and
19125 warning messages under control of COMPLAIN. */
19128 invalid_nontype_parm_type_p (tree type
, tsubst_flags_t complain
)
19130 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
19132 else if (POINTER_TYPE_P (type
))
19134 else if (TYPE_PTR_TO_MEMBER_P (type
))
19136 else if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
)
19138 else if (TREE_CODE (type
) == TYPENAME_TYPE
)
19140 else if (TREE_CODE (type
) == DECLTYPE_TYPE
)
19142 else if (TREE_CODE (type
) == NULLPTR_TYPE
)
19145 if (complain
& tf_error
)
19147 if (type
== error_mark_node
)
19148 inform (input_location
, "invalid template non-type parameter");
19150 error ("%q#T is not a valid type for a template non-type parameter",
19156 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
19157 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
19160 dependent_type_p_r (tree type
)
19166 A type is dependent if it is:
19168 -- a template parameter. Template template parameters are types
19169 for us (since TYPE_P holds true for them) so we handle
19171 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
19172 || TREE_CODE (type
) == TEMPLATE_TEMPLATE_PARM
)
19174 /* -- a qualified-id with a nested-name-specifier which contains a
19175 class-name that names a dependent type or whose unqualified-id
19176 names a dependent type. */
19177 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19179 /* -- a cv-qualified type where the cv-unqualified type is
19181 type
= TYPE_MAIN_VARIANT (type
);
19182 /* -- a compound type constructed from any dependent type. */
19183 if (TYPE_PTR_TO_MEMBER_P (type
))
19184 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type
))
19185 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
19187 else if (TREE_CODE (type
) == POINTER_TYPE
19188 || TREE_CODE (type
) == REFERENCE_TYPE
)
19189 return dependent_type_p (TREE_TYPE (type
));
19190 else if (TREE_CODE (type
) == FUNCTION_TYPE
19191 || TREE_CODE (type
) == METHOD_TYPE
)
19195 if (dependent_type_p (TREE_TYPE (type
)))
19197 for (arg_type
= TYPE_ARG_TYPES (type
);
19199 arg_type
= TREE_CHAIN (arg_type
))
19200 if (dependent_type_p (TREE_VALUE (arg_type
)))
19204 /* -- an array type constructed from any dependent type or whose
19205 size is specified by a constant expression that is
19208 We checked for type- and value-dependence of the bounds in
19209 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
19210 if (TREE_CODE (type
) == ARRAY_TYPE
)
19212 if (TYPE_DOMAIN (type
)
19213 && dependent_type_p (TYPE_DOMAIN (type
)))
19215 return dependent_type_p (TREE_TYPE (type
));
19218 /* -- a template-id in which either the template name is a template
19220 if (TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
19222 /* ... or any of the template arguments is a dependent type or
19223 an expression that is type-dependent or value-dependent. */
19224 else if (CLASS_TYPE_P (type
) && CLASSTYPE_TEMPLATE_INFO (type
)
19225 && (any_dependent_template_arguments_p
19226 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
)))))
19229 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
19230 dependent; if the argument of the `typeof' expression is not
19231 type-dependent, then it should already been have resolved. */
19232 if (TREE_CODE (type
) == TYPEOF_TYPE
19233 || TREE_CODE (type
) == DECLTYPE_TYPE
19234 || TREE_CODE (type
) == UNDERLYING_TYPE
)
19237 /* A template argument pack is dependent if any of its packed
19239 if (TREE_CODE (type
) == TYPE_ARGUMENT_PACK
)
19241 tree args
= ARGUMENT_PACK_ARGS (type
);
19242 int i
, len
= TREE_VEC_LENGTH (args
);
19243 for (i
= 0; i
< len
; ++i
)
19244 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19248 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
19249 be template parameters. */
19250 if (TREE_CODE (type
) == TYPE_PACK_EXPANSION
)
19253 /* The standard does not specifically mention types that are local
19254 to template functions or local classes, but they should be
19255 considered dependent too. For example:
19257 template <int I> void f() {
19262 The size of `E' cannot be known until the value of `I' has been
19263 determined. Therefore, `E' must be considered dependent. */
19264 scope
= TYPE_CONTEXT (type
);
19265 if (scope
&& TYPE_P (scope
))
19266 return dependent_type_p (scope
);
19267 /* Don't use type_dependent_expression_p here, as it can lead
19268 to infinite recursion trying to determine whether a lambda
19269 nested in a lambda is dependent (c++/47687). */
19270 else if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
19271 && DECL_LANG_SPECIFIC (scope
)
19272 && DECL_TEMPLATE_INFO (scope
)
19273 && (any_dependent_template_arguments_p
19274 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope
)))))
19277 /* Other types are non-dependent. */
19281 /* Returns TRUE if TYPE is dependent, in the sense of
19282 [temp.dep.type]. Note that a NULL type is considered dependent. */
19285 dependent_type_p (tree type
)
19287 /* If there are no template parameters in scope, then there can't be
19288 any dependent types. */
19289 if (!processing_template_decl
)
19291 /* If we are not processing a template, then nobody should be
19292 providing us with a dependent type. */
19294 gcc_assert (TREE_CODE (type
) != TEMPLATE_TYPE_PARM
|| is_auto (type
));
19298 /* If the type is NULL, we have not computed a type for the entity
19299 in question; in that case, the type is dependent. */
19303 /* Erroneous types can be considered non-dependent. */
19304 if (type
== error_mark_node
)
19307 /* If we have not already computed the appropriate value for TYPE,
19309 if (!TYPE_DEPENDENT_P_VALID (type
))
19311 TYPE_DEPENDENT_P (type
) = dependent_type_p_r (type
);
19312 TYPE_DEPENDENT_P_VALID (type
) = 1;
19315 return TYPE_DEPENDENT_P (type
);
19318 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
19319 lookup. In other words, a dependent type that is not the current
19323 dependent_scope_p (tree scope
)
19325 return (scope
&& TYPE_P (scope
) && dependent_type_p (scope
)
19326 && !currently_open_class (scope
));
19329 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
19330 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
19333 /* Note that this predicate is not appropriate for general expressions;
19334 only constant expressions (that satisfy potential_constant_expression)
19335 can be tested for value dependence.
19337 We should really also have a predicate for "instantiation-dependent".
19339 fold_non_dependent_expr: fold if constant and not type-dependent and not value-dependent.
19340 (what about instantiation-dependent constant-expressions?)
19341 is_late_template_attribute: defer if instantiation-dependent.
19342 compute_array_index_type: proceed if constant and not t- or v-dependent
19343 if instantiation-dependent, need to remember full expression
19344 uses_template_parms: FIXME - need to audit callers
19345 tsubst_decl [function_decl]: Why is this using value_dependent_expression_p?
19346 dependent_type_p [array_type]: dependent if index type is dependent
19348 static_assert - instantiation-dependent */
19351 value_dependent_expression_p (tree expression
)
19353 if (!processing_template_decl
)
19356 /* A name declared with a dependent type. */
19357 if (DECL_P (expression
) && type_dependent_expression_p (expression
))
19360 switch (TREE_CODE (expression
))
19362 case IDENTIFIER_NODE
:
19363 /* A name that has not been looked up -- must be dependent. */
19366 case TEMPLATE_PARM_INDEX
:
19367 /* A non-type template parm. */
19371 /* A non-type template parm. */
19372 if (DECL_TEMPLATE_PARM_P (expression
))
19374 return value_dependent_expression_p (DECL_INITIAL (expression
));
19377 /* A constant with literal type and is initialized
19378 with an expression that is value-dependent. */
19379 if (DECL_INITIAL (expression
)
19380 && decl_constant_var_p (expression
)
19381 && value_dependent_expression_p (DECL_INITIAL (expression
)))
19385 case DYNAMIC_CAST_EXPR
:
19386 case STATIC_CAST_EXPR
:
19387 case CONST_CAST_EXPR
:
19388 case REINTERPRET_CAST_EXPR
:
19390 /* These expressions are value-dependent if the type to which
19391 the cast occurs is dependent or the expression being casted
19392 is value-dependent. */
19394 tree type
= TREE_TYPE (expression
);
19396 if (dependent_type_p (type
))
19399 /* A functional cast has a list of operands. */
19400 expression
= TREE_OPERAND (expression
, 0);
19403 /* If there are no operands, it must be an expression such
19404 as "int()". This should not happen for aggregate types
19405 because it would form non-constant expressions. */
19406 gcc_assert (cxx_dialect
>= cxx0x
19407 || INTEGRAL_OR_ENUMERATION_TYPE_P (type
));
19412 if (TREE_CODE (expression
) == TREE_LIST
)
19413 return any_value_dependent_elements_p (expression
);
19415 return value_dependent_expression_p (expression
);
19421 /* A `sizeof' expression is value-dependent if the operand is
19422 type-dependent or is a pack expansion. */
19423 expression
= TREE_OPERAND (expression
, 0);
19424 if (PACK_EXPANSION_P (expression
))
19426 else if (TYPE_P (expression
))
19427 return dependent_type_p (expression
);
19428 return type_dependent_expression_p (expression
);
19430 case AT_ENCODE_EXPR
:
19431 /* An 'encode' expression is value-dependent if the operand is
19433 expression
= TREE_OPERAND (expression
, 0);
19434 return dependent_type_p (expression
);
19436 case NOEXCEPT_EXPR
:
19437 expression
= TREE_OPERAND (expression
, 0);
19438 return type_dependent_expression_p (expression
);
19442 tree name
= TREE_OPERAND (expression
, 1);
19443 return value_dependent_expression_p (name
);
19446 case COMPONENT_REF
:
19447 return (value_dependent_expression_p (TREE_OPERAND (expression
, 0))
19448 || value_dependent_expression_p (TREE_OPERAND (expression
, 1)));
19450 case NONTYPE_ARGUMENT_PACK
:
19451 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
19452 is value-dependent. */
19454 tree values
= ARGUMENT_PACK_ARGS (expression
);
19455 int i
, len
= TREE_VEC_LENGTH (values
);
19457 for (i
= 0; i
< len
; ++i
)
19458 if (value_dependent_expression_p (TREE_VEC_ELT (values
, i
)))
19466 tree type2
= TRAIT_EXPR_TYPE2 (expression
);
19467 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression
))
19468 || (type2
? dependent_type_p (type2
) : false));
19472 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19473 || (value_dependent_expression_p (TREE_OPERAND (expression
, 2))));
19476 return ((value_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19477 || (value_dependent_expression_p (TREE_OPERAND (expression
, 1))));
19481 tree op
= TREE_OPERAND (expression
, 0);
19482 return (value_dependent_expression_p (op
)
19483 || has_value_dependent_address (op
));
19488 tree fn
= get_callee_fndecl (expression
);
19490 if (!fn
&& value_dependent_expression_p (CALL_EXPR_FN (expression
)))
19492 nargs
= call_expr_nargs (expression
);
19493 for (i
= 0; i
< nargs
; ++i
)
19495 tree op
= CALL_EXPR_ARG (expression
, i
);
19496 /* In a call to a constexpr member function, look through the
19497 implicit ADDR_EXPR on the object argument so that it doesn't
19498 cause the call to be considered value-dependent. We also
19499 look through it in potential_constant_expression. */
19500 if (i
== 0 && fn
&& DECL_DECLARED_CONSTEXPR_P (fn
)
19501 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
19502 && TREE_CODE (op
) == ADDR_EXPR
)
19503 op
= TREE_OPERAND (op
, 0);
19504 if (value_dependent_expression_p (op
))
19510 case TEMPLATE_ID_EXPR
:
19511 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
19513 return type_dependent_expression_p (expression
);
19519 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), ix
, val
)
19520 if (value_dependent_expression_p (val
))
19526 /* Treat a GNU statement expression as dependent to avoid crashing
19527 under fold_non_dependent_expr; it can't be constant. */
19531 /* A constant expression is value-dependent if any subexpression is
19532 value-dependent. */
19533 switch (TREE_CODE_CLASS (TREE_CODE (expression
)))
19535 case tcc_reference
:
19537 case tcc_comparison
:
19539 case tcc_expression
:
19542 int i
, len
= cp_tree_operand_length (expression
);
19544 for (i
= 0; i
< len
; i
++)
19546 tree t
= TREE_OPERAND (expression
, i
);
19548 /* In some cases, some of the operands may be missing.l
19549 (For example, in the case of PREDECREMENT_EXPR, the
19550 amount to increment by may be missing.) That doesn't
19551 make the expression dependent. */
19552 if (t
&& value_dependent_expression_p (t
))
19563 /* The expression is not value-dependent. */
19567 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
19568 [temp.dep.expr]. Note that an expression with no type is
19569 considered dependent. Other parts of the compiler arrange for an
19570 expression with type-dependent subexpressions to have no type, so
19571 this function doesn't have to be fully recursive. */
19574 type_dependent_expression_p (tree expression
)
19576 if (!processing_template_decl
)
19579 if (expression
== error_mark_node
)
19582 /* An unresolved name is always dependent. */
19583 if (TREE_CODE (expression
) == IDENTIFIER_NODE
19584 || TREE_CODE (expression
) == USING_DECL
)
19587 /* Some expression forms are never type-dependent. */
19588 if (TREE_CODE (expression
) == PSEUDO_DTOR_EXPR
19589 || TREE_CODE (expression
) == SIZEOF_EXPR
19590 || TREE_CODE (expression
) == ALIGNOF_EXPR
19591 || TREE_CODE (expression
) == AT_ENCODE_EXPR
19592 || TREE_CODE (expression
) == NOEXCEPT_EXPR
19593 || TREE_CODE (expression
) == TRAIT_EXPR
19594 || TREE_CODE (expression
) == TYPEID_EXPR
19595 || TREE_CODE (expression
) == DELETE_EXPR
19596 || TREE_CODE (expression
) == VEC_DELETE_EXPR
19597 || TREE_CODE (expression
) == THROW_EXPR
)
19600 /* The types of these expressions depends only on the type to which
19601 the cast occurs. */
19602 if (TREE_CODE (expression
) == DYNAMIC_CAST_EXPR
19603 || TREE_CODE (expression
) == STATIC_CAST_EXPR
19604 || TREE_CODE (expression
) == CONST_CAST_EXPR
19605 || TREE_CODE (expression
) == REINTERPRET_CAST_EXPR
19606 || TREE_CODE (expression
) == IMPLICIT_CONV_EXPR
19607 || TREE_CODE (expression
) == CAST_EXPR
)
19608 return dependent_type_p (TREE_TYPE (expression
));
19610 /* The types of these expressions depends only on the type created
19611 by the expression. */
19612 if (TREE_CODE (expression
) == NEW_EXPR
19613 || TREE_CODE (expression
) == VEC_NEW_EXPR
)
19615 /* For NEW_EXPR tree nodes created inside a template, either
19616 the object type itself or a TREE_LIST may appear as the
19618 tree type
= TREE_OPERAND (expression
, 1);
19619 if (TREE_CODE (type
) == TREE_LIST
)
19620 /* This is an array type. We need to check array dimensions
19622 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type
)))
19623 || value_dependent_expression_p
19624 (TREE_OPERAND (TREE_VALUE (type
), 1));
19626 return dependent_type_p (type
);
19629 if (TREE_CODE (expression
) == SCOPE_REF
)
19631 tree scope
= TREE_OPERAND (expression
, 0);
19632 tree name
= TREE_OPERAND (expression
, 1);
19634 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
19635 contains an identifier associated by name lookup with one or more
19636 declarations declared with a dependent type, or...a
19637 nested-name-specifier or qualified-id that names a member of an
19638 unknown specialization. */
19639 return (type_dependent_expression_p (name
)
19640 || dependent_scope_p (scope
));
19643 if (TREE_CODE (expression
) == FUNCTION_DECL
19644 && DECL_LANG_SPECIFIC (expression
)
19645 && DECL_TEMPLATE_INFO (expression
)
19646 && (any_dependent_template_arguments_p
19647 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression
)))))
19650 if (TREE_CODE (expression
) == TEMPLATE_DECL
19651 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression
))
19654 if (TREE_CODE (expression
) == STMT_EXPR
)
19655 expression
= stmt_expr_value_expr (expression
);
19657 if (BRACE_ENCLOSED_INITIALIZER_P (expression
))
19662 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression
), i
, elt
)
19664 if (type_dependent_expression_p (elt
))
19670 /* A static data member of the current instantiation with incomplete
19671 array type is type-dependent, as the definition and specializations
19672 can have different bounds. */
19673 if (TREE_CODE (expression
) == VAR_DECL
19674 && DECL_CLASS_SCOPE_P (expression
)
19675 && dependent_type_p (DECL_CONTEXT (expression
))
19676 && VAR_HAD_UNKNOWN_BOUND (expression
))
19679 if (TREE_TYPE (expression
) == unknown_type_node
)
19681 if (TREE_CODE (expression
) == ADDR_EXPR
)
19682 return type_dependent_expression_p (TREE_OPERAND (expression
, 0));
19683 if (TREE_CODE (expression
) == COMPONENT_REF
19684 || TREE_CODE (expression
) == OFFSET_REF
)
19686 if (type_dependent_expression_p (TREE_OPERAND (expression
, 0)))
19688 expression
= TREE_OPERAND (expression
, 1);
19689 if (TREE_CODE (expression
) == IDENTIFIER_NODE
)
19692 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
19693 if (TREE_CODE (expression
) == SCOPE_REF
)
19696 if (BASELINK_P (expression
))
19697 expression
= BASELINK_FUNCTIONS (expression
);
19699 if (TREE_CODE (expression
) == TEMPLATE_ID_EXPR
)
19701 if (any_dependent_template_arguments_p
19702 (TREE_OPERAND (expression
, 1)))
19704 expression
= TREE_OPERAND (expression
, 0);
19706 gcc_assert (TREE_CODE (expression
) == OVERLOAD
19707 || TREE_CODE (expression
) == FUNCTION_DECL
);
19711 if (type_dependent_expression_p (OVL_CURRENT (expression
)))
19713 expression
= OVL_NEXT (expression
);
19718 gcc_assert (TREE_CODE (expression
) != TYPE_DECL
);
19720 return (dependent_type_p (TREE_TYPE (expression
)));
19723 /* Like type_dependent_expression_p, but it also works while not processing
19724 a template definition, i.e. during substitution or mangling. */
19727 type_dependent_expression_p_push (tree expr
)
19730 ++processing_template_decl
;
19731 b
= type_dependent_expression_p (expr
);
19732 --processing_template_decl
;
19736 /* Returns TRUE if ARGS contains a type-dependent expression. */
19739 any_type_dependent_arguments_p (const VEC(tree
,gc
) *args
)
19744 FOR_EACH_VEC_ELT (tree
, args
, i
, arg
)
19746 if (type_dependent_expression_p (arg
))
19752 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19753 expressions) contains any type-dependent expressions. */
19756 any_type_dependent_elements_p (const_tree list
)
19758 for (; list
; list
= TREE_CHAIN (list
))
19759 if (value_dependent_expression_p (TREE_VALUE (list
)))
19765 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
19766 expressions) contains any value-dependent expressions. */
19769 any_value_dependent_elements_p (const_tree list
)
19771 for (; list
; list
= TREE_CHAIN (list
))
19772 if (value_dependent_expression_p (TREE_VALUE (list
)))
19778 /* Returns TRUE if the ARG (a template argument) is dependent. */
19781 dependent_template_arg_p (tree arg
)
19783 if (!processing_template_decl
)
19786 /* Assume a template argument that was wrongly written by the user
19787 is dependent. This is consistent with what
19788 any_dependent_template_arguments_p [that calls this function]
19790 if (!arg
|| arg
== error_mark_node
)
19793 if (TREE_CODE (arg
) == ARGUMENT_PACK_SELECT
)
19794 arg
= ARGUMENT_PACK_SELECT_ARG (arg
);
19796 if (TREE_CODE (arg
) == TEMPLATE_DECL
19797 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19798 return dependent_template_p (arg
);
19799 else if (ARGUMENT_PACK_P (arg
))
19801 tree args
= ARGUMENT_PACK_ARGS (arg
);
19802 int i
, len
= TREE_VEC_LENGTH (args
);
19803 for (i
= 0; i
< len
; ++i
)
19805 if (dependent_template_arg_p (TREE_VEC_ELT (args
, i
)))
19811 else if (TYPE_P (arg
))
19812 return dependent_type_p (arg
);
19814 return (type_dependent_expression_p (arg
)
19815 || value_dependent_expression_p (arg
));
19818 /* Returns true if ARGS (a collection of template arguments) contains
19819 any types that require structural equality testing. */
19822 any_template_arguments_need_structural_equality_p (tree args
)
19829 if (args
== error_mark_node
)
19832 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19834 tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19835 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19837 tree arg
= TREE_VEC_ELT (level
, j
);
19838 tree packed_args
= NULL_TREE
;
19841 if (ARGUMENT_PACK_P (arg
))
19843 /* Look inside the argument pack. */
19844 packed_args
= ARGUMENT_PACK_ARGS (arg
);
19845 len
= TREE_VEC_LENGTH (packed_args
);
19848 for (k
= 0; k
< len
; ++k
)
19851 arg
= TREE_VEC_ELT (packed_args
, k
);
19853 if (error_operand_p (arg
))
19855 else if (TREE_CODE (arg
) == TEMPLATE_DECL
19856 || TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
19858 else if (TYPE_P (arg
) && TYPE_STRUCTURAL_EQUALITY_P (arg
))
19860 else if (!TYPE_P (arg
) && TREE_TYPE (arg
)
19861 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg
)))
19870 /* Returns true if ARGS (a collection of template arguments) contains
19871 any dependent arguments. */
19874 any_dependent_template_arguments_p (const_tree args
)
19881 if (args
== error_mark_node
)
19884 for (i
= 0; i
< TMPL_ARGS_DEPTH (args
); ++i
)
19886 const_tree level
= TMPL_ARGS_LEVEL (args
, i
+ 1);
19887 for (j
= 0; j
< TREE_VEC_LENGTH (level
); ++j
)
19888 if (dependent_template_arg_p (TREE_VEC_ELT (level
, j
)))
19895 /* Returns TRUE if the template TMPL is dependent. */
19898 dependent_template_p (tree tmpl
)
19900 if (TREE_CODE (tmpl
) == OVERLOAD
)
19904 if (dependent_template_p (OVL_CURRENT (tmpl
)))
19906 tmpl
= OVL_NEXT (tmpl
);
19911 /* Template template parameters are dependent. */
19912 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)
19913 || TREE_CODE (tmpl
) == TEMPLATE_TEMPLATE_PARM
)
19915 /* So are names that have not been looked up. */
19916 if (TREE_CODE (tmpl
) == SCOPE_REF
19917 || TREE_CODE (tmpl
) == IDENTIFIER_NODE
)
19919 /* So are member templates of dependent classes. */
19920 if (TYPE_P (CP_DECL_CONTEXT (tmpl
)))
19921 return dependent_type_p (DECL_CONTEXT (tmpl
));
19925 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
19928 dependent_template_id_p (tree tmpl
, tree args
)
19930 return (dependent_template_p (tmpl
)
19931 || any_dependent_template_arguments_p (args
));
19934 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
19938 dependent_omp_for_p (tree declv
, tree initv
, tree condv
, tree incrv
)
19942 if (!processing_template_decl
)
19945 for (i
= 0; i
< TREE_VEC_LENGTH (declv
); i
++)
19947 tree decl
= TREE_VEC_ELT (declv
, i
);
19948 tree init
= TREE_VEC_ELT (initv
, i
);
19949 tree cond
= TREE_VEC_ELT (condv
, i
);
19950 tree incr
= TREE_VEC_ELT (incrv
, i
);
19952 if (type_dependent_expression_p (decl
))
19955 if (init
&& type_dependent_expression_p (init
))
19958 if (type_dependent_expression_p (cond
))
19961 if (COMPARISON_CLASS_P (cond
)
19962 && (type_dependent_expression_p (TREE_OPERAND (cond
, 0))
19963 || type_dependent_expression_p (TREE_OPERAND (cond
, 1))))
19966 if (TREE_CODE (incr
) == MODOP_EXPR
)
19968 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0))
19969 || type_dependent_expression_p (TREE_OPERAND (incr
, 2)))
19972 else if (type_dependent_expression_p (incr
))
19974 else if (TREE_CODE (incr
) == MODIFY_EXPR
)
19976 if (type_dependent_expression_p (TREE_OPERAND (incr
, 0)))
19978 else if (BINARY_CLASS_P (TREE_OPERAND (incr
, 1)))
19980 tree t
= TREE_OPERAND (incr
, 1);
19981 if (type_dependent_expression_p (TREE_OPERAND (t
, 0))
19982 || type_dependent_expression_p (TREE_OPERAND (t
, 1)))
19991 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
19992 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
19993 no such TYPE can be found. Note that this function peers inside
19994 uninstantiated templates and therefore should be used only in
19995 extremely limited situations. ONLY_CURRENT_P restricts this
19996 peering to the currently open classes hierarchy (which is required
19997 when comparing types). */
20000 resolve_typename_type (tree type
, bool only_current_p
)
20009 gcc_assert (TREE_CODE (type
) == TYPENAME_TYPE
);
20011 scope
= TYPE_CONTEXT (type
);
20012 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
20013 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
20014 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
20015 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
20016 identifier of the TYPENAME_TYPE anymore.
20017 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
20018 TYPENAME_TYPE instead, we avoid messing up with a possible
20019 typedef variant case. */
20020 name
= TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
20022 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
20023 it first before we can figure out what NAME refers to. */
20024 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20025 scope
= resolve_typename_type (scope
, only_current_p
);
20026 /* If we don't know what SCOPE refers to, then we cannot resolve the
20028 if (TREE_CODE (scope
) == TYPENAME_TYPE
)
20030 /* If the SCOPE is a template type parameter, we have no way of
20031 resolving the name. */
20032 if (TREE_CODE (scope
) == TEMPLATE_TYPE_PARM
)
20034 /* If the SCOPE is not the current instantiation, there's no reason
20035 to look inside it. */
20036 if (only_current_p
&& !currently_open_class (scope
))
20038 /* If this is a typedef, we don't want to look inside (c++/11987). */
20039 if (typedef_variant_p (type
))
20041 /* If SCOPE isn't the template itself, it will not have a valid
20042 TYPE_FIELDS list. */
20043 if (same_type_p (scope
, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
)))
20044 /* scope is either the template itself or a compatible instantiation
20045 like X<T>, so look up the name in the original template. */
20046 scope
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope
);
20048 /* scope is a partial instantiation, so we can't do the lookup or we
20049 will lose the template arguments. */
20051 /* Enter the SCOPE so that name lookup will be resolved as if we
20052 were in the class definition. In particular, SCOPE will no
20053 longer be considered a dependent type. */
20054 pushed_scope
= push_scope (scope
);
20055 /* Look up the declaration. */
20056 decl
= lookup_member (scope
, name
, /*protect=*/0, /*want_type=*/true,
20057 tf_warning_or_error
);
20059 result
= NULL_TREE
;
20061 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
20062 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
20065 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == IDENTIFIER_NODE
20066 && TREE_CODE (decl
) == TYPE_DECL
)
20068 result
= TREE_TYPE (decl
);
20069 if (result
== error_mark_node
)
20070 result
= NULL_TREE
;
20072 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type
)) == TEMPLATE_ID_EXPR
20073 && DECL_CLASS_TEMPLATE_P (decl
))
20077 /* Obtain the template and the arguments. */
20078 tmpl
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 0);
20079 args
= TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type
), 1);
20080 /* Instantiate the template. */
20081 result
= lookup_template_class (tmpl
, args
, NULL_TREE
, NULL_TREE
,
20082 /*entering_scope=*/0,
20083 tf_error
| tf_user
);
20084 if (result
== error_mark_node
)
20085 result
= NULL_TREE
;
20088 /* Leave the SCOPE. */
20090 pop_scope (pushed_scope
);
20092 /* If we failed to resolve it, return the original typename. */
20096 /* If lookup found a typename type, resolve that too. */
20097 if (TREE_CODE (result
) == TYPENAME_TYPE
&& !TYPENAME_IS_RESOLVING_P (result
))
20099 /* Ill-formed programs can cause infinite recursion here, so we
20100 must catch that. */
20101 TYPENAME_IS_RESOLVING_P (type
) = 1;
20102 result
= resolve_typename_type (result
, only_current_p
);
20103 TYPENAME_IS_RESOLVING_P (type
) = 0;
20106 /* Qualify the resulting type. */
20107 quals
= cp_type_quals (type
);
20109 result
= cp_build_qualified_type (result
, cp_type_quals (result
) | quals
);
20114 /* EXPR is an expression which is not type-dependent. Return a proxy
20115 for EXPR that can be used to compute the types of larger
20116 expressions containing EXPR. */
20119 build_non_dependent_expr (tree expr
)
20123 #ifdef ENABLE_CHECKING
20124 /* Try to get a constant value for all non-type-dependent expressions in
20125 order to expose bugs in *_dependent_expression_p and constexpr. */
20126 if (cxx_dialect
>= cxx0x
)
20127 maybe_constant_value (fold_non_dependent_expr_sfinae (expr
, tf_none
));
20130 /* Preserve OVERLOADs; the functions must be available to resolve
20133 if (TREE_CODE (inner_expr
) == STMT_EXPR
)
20134 inner_expr
= stmt_expr_value_expr (inner_expr
);
20135 if (TREE_CODE (inner_expr
) == ADDR_EXPR
)
20136 inner_expr
= TREE_OPERAND (inner_expr
, 0);
20137 if (TREE_CODE (inner_expr
) == COMPONENT_REF
)
20138 inner_expr
= TREE_OPERAND (inner_expr
, 1);
20139 if (is_overloaded_fn (inner_expr
)
20140 || TREE_CODE (inner_expr
) == OFFSET_REF
)
20142 /* There is no need to return a proxy for a variable. */
20143 if (TREE_CODE (expr
) == VAR_DECL
)
20145 /* Preserve string constants; conversions from string constants to
20146 "char *" are allowed, even though normally a "const char *"
20147 cannot be used to initialize a "char *". */
20148 if (TREE_CODE (expr
) == STRING_CST
)
20150 /* Preserve arithmetic constants, as an optimization -- there is no
20151 reason to create a new node. */
20152 if (TREE_CODE (expr
) == INTEGER_CST
|| TREE_CODE (expr
) == REAL_CST
)
20154 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
20155 There is at least one place where we want to know that a
20156 particular expression is a throw-expression: when checking a ?:
20157 expression, there are special rules if the second or third
20158 argument is a throw-expression. */
20159 if (TREE_CODE (expr
) == THROW_EXPR
)
20162 /* Don't wrap an initializer list, we need to be able to look inside. */
20163 if (BRACE_ENCLOSED_INITIALIZER_P (expr
))
20166 if (TREE_CODE (expr
) == COND_EXPR
)
20167 return build3 (COND_EXPR
,
20169 TREE_OPERAND (expr
, 0),
20170 (TREE_OPERAND (expr
, 1)
20171 ? build_non_dependent_expr (TREE_OPERAND (expr
, 1))
20172 : build_non_dependent_expr (TREE_OPERAND (expr
, 0))),
20173 build_non_dependent_expr (TREE_OPERAND (expr
, 2)));
20174 if (TREE_CODE (expr
) == COMPOUND_EXPR
20175 && !COMPOUND_EXPR_OVERLOADED (expr
))
20176 return build2 (COMPOUND_EXPR
,
20178 TREE_OPERAND (expr
, 0),
20179 build_non_dependent_expr (TREE_OPERAND (expr
, 1)));
20181 /* If the type is unknown, it can't really be non-dependent */
20182 gcc_assert (TREE_TYPE (expr
) != unknown_type_node
);
20184 /* Otherwise, build a NON_DEPENDENT_EXPR. */
20185 return build1 (NON_DEPENDENT_EXPR
, TREE_TYPE (expr
), expr
);
20188 /* ARGS is a vector of expressions as arguments to a function call.
20189 Replace the arguments with equivalent non-dependent expressions.
20190 This modifies ARGS in place. */
20193 make_args_non_dependent (VEC(tree
,gc
) *args
)
20198 FOR_EACH_VEC_ELT (tree
, args
, ix
, arg
)
20200 tree newarg
= build_non_dependent_expr (arg
);
20202 VEC_replace (tree
, args
, ix
, newarg
);
20206 /* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
20207 with a level one deeper than the actual template parms. */
20212 tree au
= cxx_make_type (TEMPLATE_TYPE_PARM
);
20213 TYPE_NAME (au
) = build_decl (BUILTINS_LOCATION
,
20214 TYPE_DECL
, get_identifier ("auto"), au
);
20215 TYPE_STUB_DECL (au
) = TYPE_NAME (au
);
20216 TEMPLATE_TYPE_PARM_INDEX (au
) = build_template_parm_index
20217 (0, processing_template_decl
+ 1, processing_template_decl
+ 1,
20218 0, TYPE_NAME (au
), NULL_TREE
);
20219 TYPE_CANONICAL (au
) = canonical_type_parameter (au
);
20220 DECL_ARTIFICIAL (TYPE_NAME (au
)) = 1;
20221 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au
));
20226 /* Given type ARG, return std::initializer_list<ARG>. */
20231 tree std_init_list
= namespace_binding
20232 (get_identifier ("initializer_list"), std_node
);
20234 if (!std_init_list
|| !DECL_CLASS_TEMPLATE_P (std_init_list
))
20236 error ("deducing from brace-enclosed initializer list requires "
20237 "#include <initializer_list>");
20238 return error_mark_node
;
20240 argvec
= make_tree_vec (1);
20241 TREE_VEC_ELT (argvec
, 0) = arg
;
20242 return lookup_template_class (std_init_list
, argvec
, NULL_TREE
,
20243 NULL_TREE
, 0, tf_warning_or_error
);
20246 /* Replace auto in TYPE with std::initializer_list<auto>. */
20249 listify_autos (tree type
, tree auto_node
)
20251 tree init_auto
= listify (auto_node
);
20252 tree argvec
= make_tree_vec (1);
20253 TREE_VEC_ELT (argvec
, 0) = init_auto
;
20254 if (processing_template_decl
)
20255 argvec
= add_to_template_args (current_template_args (), argvec
);
20256 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20259 /* walk_tree helper for do_auto_deduction. */
20262 contains_auto_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
20265 /* Is this a variable with the type we're looking for? */
20267 && TREE_TYPE (*tp
) == type
)
20273 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
20274 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
20277 do_auto_deduction (tree type
, tree init
, tree auto_node
)
20279 tree parms
, tparms
, targs
;
20284 if (processing_template_decl
20285 && (TREE_TYPE (init
) == NULL_TREE
20286 || BRACE_ENCLOSED_INITIALIZER_P (init
)))
20287 /* Not enough information to try this yet. */
20290 /* The name of the object being declared shall not appear in the
20291 initializer expression. */
20292 decl
= cp_walk_tree_without_duplicates (&init
, contains_auto_r
, type
);
20295 error ("variable %q#D with %<auto%> type used in its own "
20296 "initializer", decl
);
20297 return error_mark_node
;
20300 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
20301 with either a new invented type template parameter U or, if the
20302 initializer is a braced-init-list (8.5.4), with
20303 std::initializer_list<U>. */
20304 if (BRACE_ENCLOSED_INITIALIZER_P (init
))
20305 type
= listify_autos (type
, auto_node
);
20307 init
= resolve_nondeduced_context (init
);
20309 parms
= build_tree_list (NULL_TREE
, type
);
20311 tparms
= make_tree_vec (1);
20312 targs
= make_tree_vec (1);
20313 TREE_VEC_ELT (tparms
, 0)
20314 = build_tree_list (NULL_TREE
, TYPE_NAME (auto_node
));
20315 val
= type_unification_real (tparms
, targs
, parms
, args
, 1, 0,
20316 DEDUCE_CALL
, LOOKUP_NORMAL
,
20317 /*explain_p=*/false);
20320 if (processing_template_decl
)
20321 /* Try again at instantiation time. */
20323 if (type
&& type
!= error_mark_node
)
20324 /* If type is error_mark_node a diagnostic must have been
20325 emitted by now. Also, having a mention to '<type error>'
20326 in the diagnostic is not really useful to the user. */
20327 error ("unable to deduce %qT from %qE", type
, init
);
20328 return error_mark_node
;
20331 /* If the list of declarators contains more than one declarator, the type
20332 of each declared variable is determined as described above. If the
20333 type deduced for the template parameter U is not the same in each
20334 deduction, the program is ill-formed. */
20335 if (TREE_TYPE (auto_node
)
20336 && !same_type_p (TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0)))
20338 error ("inconsistent deduction for %qT: %qT and then %qT",
20339 auto_node
, TREE_TYPE (auto_node
), TREE_VEC_ELT (targs
, 0));
20340 return error_mark_node
;
20342 TREE_TYPE (auto_node
) = TREE_VEC_ELT (targs
, 0);
20344 if (processing_template_decl
)
20345 targs
= add_to_template_args (current_template_args (), targs
);
20346 return tsubst (type
, targs
, tf_warning_or_error
, NULL_TREE
);
20349 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
20353 splice_late_return_type (tree type
, tree late_return_type
)
20357 if (late_return_type
== NULL_TREE
)
20359 argvec
= make_tree_vec (1);
20360 TREE_VEC_ELT (argvec
, 0) = late_return_type
;
20361 if (processing_template_parmlist
)
20362 /* For a late-specified return type in a template type-parameter, we
20363 need to add a dummy argument level for its parmlist. */
20364 argvec
= add_to_template_args
20365 (make_tree_vec (processing_template_parmlist
), argvec
);
20366 if (current_template_parms
)
20367 argvec
= add_to_template_args (current_template_args (), argvec
);
20368 return tsubst (type
, argvec
, tf_warning_or_error
, NULL_TREE
);
20371 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
20374 is_auto (const_tree type
)
20376 if (TREE_CODE (type
) == TEMPLATE_TYPE_PARM
20377 && TYPE_IDENTIFIER (type
) == get_identifier ("auto"))
20383 /* Returns true iff TYPE contains a use of 'auto'. Since auto can only
20384 appear as a type-specifier for the declaration in question, we don't
20385 have to look through the whole type. */
20388 type_uses_auto (tree type
)
20390 enum tree_code code
;
20391 if (is_auto (type
))
20394 code
= TREE_CODE (type
);
20396 if (code
== POINTER_TYPE
|| code
== REFERENCE_TYPE
20397 || code
== OFFSET_TYPE
|| code
== FUNCTION_TYPE
20398 || code
== METHOD_TYPE
|| code
== ARRAY_TYPE
)
20399 return type_uses_auto (TREE_TYPE (type
));
20401 if (TYPE_PTRMEMFUNC_P (type
))
20402 return type_uses_auto (TREE_TYPE (TREE_TYPE
20403 (TYPE_PTRMEMFUNC_FN_TYPE (type
))));
20408 /* For a given template T, return the vector of typedefs referenced
20409 in T for which access check is needed at T instantiation time.
20410 T is either a FUNCTION_DECL or a RECORD_TYPE.
20411 Those typedefs were added to T by the function
20412 append_type_to_template_for_access_check. */
20414 VEC(qualified_typedef_usage_t
,gc
)*
20415 get_types_needing_access_check (tree t
)
20418 VEC(qualified_typedef_usage_t
,gc
) *result
= NULL
;
20420 if (!t
|| t
== error_mark_node
)
20423 if (!(ti
= get_template_info (t
)))
20426 if (CLASS_TYPE_P (t
)
20427 || TREE_CODE (t
) == FUNCTION_DECL
)
20429 if (!TI_TEMPLATE (ti
))
20432 result
= TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
);
20438 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
20439 tied to T. That list of typedefs will be access checked at
20440 T instantiation time.
20441 T is either a FUNCTION_DECL or a RECORD_TYPE.
20442 TYPE_DECL is a TYPE_DECL node representing a typedef.
20443 SCOPE is the scope through which TYPE_DECL is accessed.
20444 LOCATION is the location of the usage point of TYPE_DECL.
20446 This function is a subroutine of
20447 append_type_to_template_for_access_check. */
20450 append_type_to_template_for_access_check_1 (tree t
,
20453 location_t location
)
20455 qualified_typedef_usage_t typedef_usage
;
20458 if (!t
|| t
== error_mark_node
)
20461 gcc_assert ((TREE_CODE (t
) == FUNCTION_DECL
20462 || CLASS_TYPE_P (t
))
20464 && TREE_CODE (type_decl
) == TYPE_DECL
20467 if (!(ti
= get_template_info (t
)))
20470 gcc_assert (TI_TEMPLATE (ti
));
20472 typedef_usage
.typedef_decl
= type_decl
;
20473 typedef_usage
.context
= scope
;
20474 typedef_usage
.locus
= location
;
20476 VEC_safe_push (qualified_typedef_usage_t
, gc
,
20477 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti
),
20481 /* Append TYPE_DECL to the template TEMPL.
20482 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
20483 At TEMPL instanciation time, TYPE_DECL will be checked to see
20484 if it can be accessed through SCOPE.
20485 LOCATION is the location of the usage point of TYPE_DECL.
20487 e.g. consider the following code snippet:
20494 template<class U> struct S
20496 C::myint mi; // <-- usage point of the typedef C::myint
20501 At S<char> instantiation time, we need to check the access of C::myint
20502 In other words, we need to check the access of the myint typedef through
20503 the C scope. For that purpose, this function will add the myint typedef
20504 and the scope C through which its being accessed to a list of typedefs
20505 tied to the template S. That list will be walked at template instantiation
20506 time and access check performed on each typedefs it contains.
20507 Note that this particular code snippet should yield an error because
20508 myint is private to C. */
20511 append_type_to_template_for_access_check (tree templ
,
20514 location_t location
)
20516 qualified_typedef_usage_t
*iter
;
20519 gcc_assert (type_decl
&& (TREE_CODE (type_decl
) == TYPE_DECL
));
20521 /* Make sure we don't append the type to the template twice. */
20522 FOR_EACH_VEC_ELT (qualified_typedef_usage_t
,
20523 get_types_needing_access_check (templ
),
20525 if (iter
->typedef_decl
== type_decl
&& scope
== iter
->context
)
20528 append_type_to_template_for_access_check_1 (templ
, type_decl
,
20532 /* Set up the hash tables for template instantiations. */
20535 init_template_processing (void)
20537 decl_specializations
= htab_create_ggc (37,
20538 hash_specialization
,
20539 eq_specializations
,
20541 type_specializations
= htab_create_ggc (37,
20542 hash_specialization
,
20543 eq_specializations
,
20547 /* Print stats about the template hash tables for -fstats. */
20550 print_template_statistics (void)
20552 fprintf (stderr
, "decl_specializations: size %ld, %ld elements, "
20553 "%f collisions\n", (long) htab_size (decl_specializations
),
20554 (long) htab_elements (decl_specializations
),
20555 htab_collisions (decl_specializations
));
20556 fprintf (stderr
, "type_specializations: size %ld, %ld elements, "
20557 "%f collisions\n", (long) htab_size (type_specializations
),
20558 (long) htab_elements (type_specializations
),
20559 htab_collisions (type_specializations
));
20562 #include "gt-cp-pt.h"